/var/www/www.irssi.org-old/scripts/html/awayproxy.pl
1 # vim:syntax=perl
2 # vim:tabstop=4
3 # vim:shiftwidth=4
4 # vim:foldmethod=marker
5 # vim:foldenable
6 # vim:enc=utf-8
7 ########################################################################################################
8 ## WARNING!! BAD ENGLISH BELOW :P
9 ##
10 ## This script is designed for those who have been using muh irc bouncer.
11 ## Basicly this script just monitors the proxy module and if new client
12 ## connects it sets you automatically back from away state and when client
13 ## disconnects it sets you automatically away if you arent allready away.
14 ##
15 ## Other hand if you dont use irssi-proxy you still have a good reason to
16 ## use this if you want to forward messages that come to you while
17 ## you are away to email box.
18 ## This is usefull for forwarding messages to an SMS-gateway ;)
19 ##
20 ## btw.. if you find any bugs or have any ideas for development of this
21 ## script dont hesitate to send msg to BCOW@IrcNET
22 ## or send email to anttip@n0-life.com
23 ##
24 #### Version history:
25 # 0.1
26 # * basic functionality
27 # 0.2b
28 # * a patch from Wulf that gives a user ability to change the autoaway reason.
29 # * Added away_level parameter that gives you ability to control how many
30 # clients there can be connected to the irssi_proxy module before you are
31 # set away.
32 # * You arent set away when disconnecting from the irssi_proxy if you already
33 # are away. This means that your current away reason isn't changed.
34 # * Sends cumulated away messages back to the client when it connects to the
35 # irssi_proxy module.
36 # 0.2c
37 # * Fixes bug where cummulated messages weren't sent.
38 # * Code cleanup.
39 # * Text wrapping to standart 80x24 text console.
40 # * Added debug mode.
41 # * Added script modes.
42 # * Got rid of crappy irssi setings system.
43 # * New logging expansion capability, either time or line based.
44 # 0.2d
45 # * Micro fix to get back only when needed
46 #### To come / planned / wanted:
47 # * Make expansion system log several channels at once.
48 # * Make this script server based.
49 ########################################################################################################
50
51 use strict;
52 use warnings;
53
54 # irssi imports
55 use Irssi;
56 use Irssi::Irc;
57 use vars qw($VERSION %IRSSI %config);
58
59 $VERSION = "0.2d";
60 %IRSSI = (
61 authors => "BCOW",
62 contact => "anttip\@n0-life.com",
63 name => "awayproxy",
64 description => "Sets nick away when client discconects from the "
65 . "irssi-proxy. If away gathers messages targeted to nick and forwards "
66 . "them to an email address.",
67 license => "GPLv2",
68 url => "http://www.n0-life.com",
69 );
70
71 # After how much seconds we can check if there are any messages to send?
72 $config{check_interval} = 45;
73 # this setting controls that when this amout of clients are connected to the
74 # proxy module script sets you away. If you set this to 0 you are set away when
75 # no clients are connected to the proxy module. If you set this to lets say 5
76 # then you will be set away allways when the amount of clients connected to the
77 # proxy module is 5 or under.
78 $config{away_level} = 0;
79 # Controls expansion mode. This mode records pub msgs that come after one with
80 # your nick in it. you can use line counting or time counting.
81 # 0 - off
82 # line - line counting
83 # time - time counting
84 $config{expansion_mode} = 'time';
85 # How many lines include after start line?
86 $config{expansion_lines} = 12;
87 # After how many seconds stop gathering msgs?
88 $config{expansion_timeout} = 90;
89 # script operation mode:
90 # 0 - to send messages both to email and when you get back to proxy
91 # 1 - only email
92 # 2 - only irc
93 # 3 - off
94 $config{script_mode} = 1;
95 # email address where to send the email
96 $config{emailto} = 'email@email.org';
97 # sendmail location
98 $config{sendmail} = '/usr/sbin/sendmail';
99 # who is the sender of the email
100 $config{emailfrom} = 'email@email.org';
101 # Subject of email
102 $config{emailsubject} = '[irssi-proxy]';
103 # and the awayreason setting (Thanx Wulf)
104 $config{awayreason} = 'Auto-away because client has disconnected from proxy.';
105 # Debugging mode
106 $config{debug} = 0;
107
108 # -- Don't change anything below this line if you don't know Perl. --
109 # number of clients connected
110 $config{clientcount} = 0;
111 # number of lines recorded
112 $config{expansion_lines_count} = 0;
113
114 $config{expansion_started} = 0;
115 # the small list and archive list
116 $config{awaymsglist} = [];
117 $config{awaymsglist2} = [];
118
119 if ( $config{script_mode} == 0 || $config{script_mode} == 1 ) { # {{{
120 # timeouts for check loop
121 debug('Timer on, timeout: ' . $config{check_interval});
122 Irssi::timeout_add($config{check_interval} * 1000, 'msgsend_check', '');
123 } # }}}
124
125 sub debug { # {{{
126 if ($config{debug}) {
127 my $text = shift;
128 my $caller = caller;
129 Irssi::print('From ' . $caller . ":\n" . $text);
130 }
131 } # }}}
132 sub msgsend_check { # {{{
133 # If there are any messages to send
134 my $count = @{$config{awaymsglist}};
135 debug("Checking for messages: $count");
136 # Check if we didn't grep msgs right now
137 if ($count > 0 && !$config{expansion_started}) {
138 # Concentate messages into one text.
139 my $text = join "\n", @{$config{awaymsglist}};
140 # Then empty list.
141 $config{awaymsglist} = [];
142 # Finally send email
143 debug("Concentated msgs: $text");
144 send_mail($text);
145 }
146 } # }}}
147 sub send_mail { # {{{
148 my $text = shift;
149 debug("Sending mail");
150 open MAIL, "|" . $config{sendmail} . " -t";
151 print MAIL "To: $config{emailto}\n";
152 print MAIL "From: $config{emailfrom}\n";
153 print MAIL "Subject: $config{emailsubject}\n";
154 print MAIL "$text";
155 close MAIL;
156 } # }}}
157 sub client_connect { # {{{
158 my (@servers) = Irssi::servers;
159
160 $config{clientcount}++;
161 debug("Client connected, current script mode: $config{script_mode}");
162
163 # setback
164 foreach my $server (@servers) {
165 # if you're away on that server send yourself back
166 if ($server->{usermode_away} == 1) {
167 $server->send_raw('AWAY :');
168 # and then send the current contents of archive list as notify's to
169 # your self ;)
170 # .. weird huh? :)
171 # This sends all the away messages to ALL the servers where you are
172 # connected... this is somewhat weird i know
173 # but if someone wants to make a patch to this i would really
174 # appreciate it.
175 if ($config{script_mode} == 0 || $config{script_mode} == 2) {
176 debug('Sending notices');
177 $server->send_raw('NOTICE ' . $server->{nick} . " :$_")
178 for @{$config{awaymsglist2}};
179 }
180 }
181 }
182 # and "clear" the awaymessage list
183 $config{awaymsglist2} = []
184 if $config{script_mode} == 0 || $config{script_mode} == 2;
185 } # }}}
186 sub client_disconnect { # {{{
187 my (@servers) = Irssi::servers;
188 debug('Client Disconnectted');
189
190 $config{clientcount}-- unless $config{clientcount} == 0;
191
192 # setaway
193 if ($config{clientcount} <= $config{away_level}) {
194 # ok.. we have the away_level of clients connected or less.
195 foreach my $server (@servers) {
196 if ($server->{usermode_away} == "0") {
197 # we are not away on this server allready.. set the autoaway
198 # reason
199 $server->send_raw(
200 'AWAY :' . $config{awayreason}
201 );
202 }
203 }
204 }
205 } # }}}
206 sub msg_pub { # {{{
207 my ($server, $data, $nick, $mask, $target) = @_;
208
209 sub push_into_archive { # {{{
210 my ($nick, $mask, $target, $data) = @_;
211 # simple list that is emptied on the email run
212 push @{$config{awaymsglist}}, "<$nick!$mask\@$target> $data"
213 if $config{script_mode} == 0 || $config{script_mode} == 1;
214 # archive list that is emptied only on the client connect run
215 push @{$config{awaymsglist2}}, "<$nick!$mask\@$target> $data"
216 if $config{script_mode} == 0 || $config{script_mode} == 2;
217 } # }}}
218
219 if ($config{expansion_started}) {
220 if ($config{expansion_mode} eq 'line') {
221 if ($config{expansion_lines_count} <= $config{expansion_lines} -1) {
222 if ($config{expansion_chan} eq $target) {
223 debug("In effect from line expansion, pushing on. Cnt: "
224 . $config{expansion_lines_count});
225 push_into_archive($nick, $mask, $target, $data);
226 $config{expansion_lines_count}++;
227 }
228 }
229 else {
230 debug("Line counter reached max, stopping expansion");
231 $config{expansion_lines_count} = 0;
232 $config{expansion_started} = 0;
233 $config{expansion_chan} = '';
234 }
235 }
236 elsif ($config{expansion_mode} eq 'time') {
237 if ($config{expansion_chan} eq $target) {
238 debug("Time expansion in effect, pushing on.");
239 push_into_archive($nick, $mask, $target, $data);
240 }
241 }
242 }
243 elsif ($server->{usermode_away} == "1" && $data =~ /$server->{nick}/i) {
244 debug("Got pub msg with my name");
245 push_into_archive($nick, $mask, $target, $data);
246 if ($config{expansion_mode}) {
247 debug("Starting expansion in mode: " . $config{expansion_mode});
248 $config{expansion_started} = 1;
249 $config{expansion_chan} = $target;
250 $config{expansion_time_out} = Irssi::timeout_add(
251 $config{expansion_timeout} * 1000, 'expansion_stop', ''
252 ) if $config{expansion_mode} eq 'time';
253 }
254 }
255 } # }}}
256 sub expansion_stop { # {{{
257 debug("Stopping expansion from timer");
258 $config{expansion_started} = 0;
259 $config{expansion_chan} = '';
260 } # }}}
261 sub msg_pri { # {{{
262 my ($server, $data, $nick, $address) = @_;
263 if ($server->{usermode_away} == "1") {
264 debug("Got priv msg");
265 # simple list that is emptied on the email run
266 push @{$config{awaymsglist}}, "<$nick!$address> $data"
267 if $config{script_mode} == 0 || $config{script_mode} == 1;
268 # archive list that is emptied only on the client connect run
269 push @{$config{awaymsglist2}}, "<$nick!$address> $data"
270 if $config{script_mode} == 0 || $config{script_mode} == 2;
271 }
272 } # }}}
273
274 Irssi::signal_add_last('proxy client connected', 'client_connect');
275 Irssi::signal_add_last('proxy client disconnected', 'client_disconnect');
276 Irssi::signal_add_last('message public', 'msg_pub');
277 Irssi::signal_add_last('message private', 'msg_pri');