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