1use strict; 2use Irssi 20020101.0250 (); 3 4use vars qw($VERSION %IRSSI); 5$VERSION = "0.2"; 6%IRSSI = ( 7 authors => "Ian Peters", 8 contact => "itp\@ximian.com", 9 name => "Connect Command", 10 description => "run arbitrary shell commands while [dis]connecting to a server", 11 license => "Public Domain", 12 url => "http://irssi.org/", 13 changed => "2017-03-18" 14); 15 16my %preconn_actions; 17my %postconn_actions; 18my %disconn_actions; 19 20sub load_actions { 21 my $fi; 22 23 open $fi, '<', "$ENV{HOME}/.irssi/connectcmd_actions"; 24 25 while (<$fi>) { 26 my @lines = split "\n"; 27 foreach my $line (@lines) { 28 my ($server, $type, $action) = split ":", $line; 29 if ($type eq "preconn") { 30 $preconn_actions{$server} = $action; 31 } elsif ($type eq "postconn") { 32 $postconn_actions{$server} = $action; 33 } elsif ($type eq "disconn") { 34 $disconn_actions{$server} = $action; 35 } 36 } 37 } 38 39 close $fi; 40} 41 42sub save_actions { 43 my $fa; 44 open $fa, q{>}, "$ENV{HOME}/.irssi/connectcmd_actions"; 45 46 foreach my $server (keys %preconn_actions) { 47 print $fa "$server:preconn:$preconn_actions{$server}\n"; 48 } 49 foreach my $server (keys %postconn_actions) { 50 print $fa "$server:postconn:$postconn_actions{$server}\n"; 51 } 52 foreach my $server (keys %disconn_actions) { 53 print $fa "$server:disconn:$disconn_actions{$server}\n"; 54 } 55 56 close $fa; 57} 58 59sub sig_server_looking { 60 my ($server) = @_; 61 62 if (my $action = $preconn_actions{$server->{'address'}}) { 63 system ($action); 64 } 65} 66 67sub sig_server_connected { 68 my ($server) = @_; 69 70 if (my $action = $postconn_actions{$server->{'address'}}) { 71 system ($action); 72 } 73} 74 75sub sig_server_disconnected { 76 my ($server) = @_; 77 78 if (my $action = $disconn_actions{$server->{'address'}}) { 79 system ($action); 80 } 81} 82 83sub cmd_connectcmd { 84 my ($data, $server, $witem) = @_; 85 86 #my ($op, $type, $server, $action) = split " ", $data; 87 $data =~ m/^(\S*)\s+(\S*)\s+(\S*)\s+(.*)$/; 88 my $op=$1; 89 my $type=$2; 90 my $server=$3; 91 my $action=$4; 92 93 $op = lc $op; 94 95 if (!$op) { 96 Irssi::print ("No operation given"); 97 } elsif ($op eq "add") { 98 if (!$type) { 99 Irssi::print ("Type not specified [preconn|postconn|disconn]"); 100 } elsif (!$server) { 101 Irssi::print ("Server not specified"); 102 } elsif (!$action) { 103 Irssi::print ("Action not specified"); 104 } else { 105 if ($type eq "preconn") { 106 $preconn_actions{$server} = $action; 107 Irssi::print ("Added preconnect action of $action on $server"); 108 save_actions; 109 } elsif ($type eq "postconn") { 110 $postconn_actions{$server} = $action; 111 Irssi::print ("Added postconnect action of $action on $server"); 112 save_actions; 113 } elsif ($type eq "disconn") { 114 $disconn_actions{$server} = $action; 115 Irssi::print ("Added disconnect action of $action on $server"); 116 save_actions; 117 } else { 118 Irssi::print ("Unrecognized trigger $type [preconn|postconn|disconn]"); 119 } 120 } 121 } elsif ($op eq "remove") { 122 if (!$type) { 123 Irssi::print ("Type not specified [preconn|postconn|disconn]"); 124 } elsif (!$server) { 125 Irssi::print ("Server not specified"); 126 } else { 127 if ($type eq "preconn") { 128 delete ($preconn_actions{$server}); 129 Irssi::print ("Removed preconnect action on $server"); 130 save_actions; 131 } elsif ($type eq "postconn") { 132 delete ($postconn_actions{$server}); 133 Irssi::print ("Removed postconnect action on $server"); 134 save_actions; 135 } elsif ($type eq "disconn") { 136 delete ($disconn_actions{$server}); 137 Irssi::print ("Removed disconnect action on $server"); 138 save_actions; 139 } else { 140 Irssi::print ("Unrecognized trigger $type [preconn|postconn|disconn]"); 141 } 142 } 143 } elsif ($op eq "list") { 144 Irssi::print ("Preconnect Actions:"); 145 foreach my $server (keys %preconn_actions) { 146 Irssi::print ("$server $preconn_actions{$server}"); 147 } 148 Irssi::print ("Postconnect Actions:"); 149 foreach my $server (keys %postconn_actions) { 150 Irssi::print ("$server $postconn_actions{$server}"); 151 } 152 Irssi::print ("Disconnect Actions:"); 153 foreach my $server (keys %disconn_actions) { 154 Irssi::print ("$server $disconn_actions{$server}"); 155 } 156 } 157} 158 159load_actions(); 160 161Irssi::command_bind ('connectcmd', 'cmd_connectcmd'); 162 163Irssi::signal_add ('server looking', 'sig_server_looking'); 164Irssi::signal_add ('server connected', 'sig_server_connected'); 165Irssi::signal_add ('server disconnected', 'sig_server_disconnected'); 166