-
Notifications
You must be signed in to change notification settings - Fork 8
/
aimproxy.pl
executable file
·379 lines (274 loc) · 9.53 KB
/
aimproxy.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/perl -w
#
# This bot is a proxy between AIM and IRC. You give the bot an AIM
# username, and any messages sent to it by people on its buddy list get
# forwarded to IRC. Originally written to allow poor disadvantaged
# Hiptop users to get on IRC.
#
# -- dennis taylor, <dennis@funkplanet.com>
use strict;
use Socket;
use POE qw( Wheel::SocketFactory Wheel::ReadWrite Filter::Line Driver::SysRW );
use POE::Component::IRC;
use Time::HiRes qw(gettimeofday tv_interval);
use Net::AIM;
use constant MSG_INTERVAL => 2.2;
my $channel = '#tempura';
my $irc_server = $ARGV[1] || "scissorman.phreeow.net";
my $irc_port = $ARGV[2] || 6667;
my ($aim, $aimconn);
sub _start {
my ($kernel, $heap) = @_[KERNEL, HEAP];
$aim = Net::AIM->new();
$aim->newconn(
Screenname => 'vscairc',
Password => $ARGV[0],
AutoReconnect => 1,
) or die "Can't connect to AIM server: $!";
$aimconn = $aim->getconn();
$aimconn->set_handler( 'update_buddy', \&_net_aim_update_buddy );
$aimconn->set_handler( 'config', \&_net_aim_config );
$aimconn->set_handler( 'im_in', \&_net_aim_im_in );
$aimconn->set_handler( 'error', \&_net_aim_error );
$kernel->alias_set( 'control' );
$kernel->yield( 'aim_listen' );
$heap->{aimqueue} = [];
$heap->{lastsend} = [gettimeofday];
}
sub _stop {
my ($kernel, $heap) = @_[KERNEL, HEAP];
print "Control session killed.\n";
foreach my $user (keys %{$heap->{queue}}) {
$kernel->call( "irc_$user", 'quit', '[aimproxy] Control session killed.' );
}
$aimconn->disconnect();
$kernel->alias_remove( 'control' );
}
sub aim_listen {
$aim->do_one_loop();
$_[KERNEL]->yield( 'aim_send' );
$_[KERNEL]->delay( 'aim_listen', 0.5 );
}
sub _net_aim_update_buddy {
my ($self, $evt) = @_;
my ($buddy, $online) = @{$evt->args()};
$poe_kernel->post( 'control', 'aim_buddy_update', $buddy, ($online == "T") );
}
sub aim_buddy_update {
my ($kernel, $heap, $buddy, $online) = @_[KERNEL, HEAP, ARG0, ARG1];
if ($online) {
$heap->{friends}->{$buddy} = 1;
} elsif (not $online and $kernel->alias_resolve( "irc_$buddy" )) {
$heap->{friends}->{$buddy} = 0;
$kernel->post( "irc_$buddy", 'quit',
"[aimproxy] $buddy has signed off AIM." );
}
}
sub _net_aim_config {
my ($self, $evt, $from, $to) = @_;
my $str = shift @{$evt->args()};
my @friends;
$self->set_config_str($str, 1);
$self->send_config();
foreach (split /[\r\n]+/, $str) {
if (/^b (\S+)$/) {
push @friends, $1;
print "$1 is my friend.\n";
}
}
$poe_kernel->post( 'control', 'aim_friends', \@friends );
}
sub aim_friends {
my ($heap, $friends) = @_[HEAP, ARG0];
$heap->{friends}->{$_} = 0 foreach @$friends;
}
sub _net_aim_im_in {
my ($self, $evt) = @_;
my ($nick, $auto_msg, $msg) = @{$evt->args()};
my $stripped = $msg;
return if $auto_msg eq 'T';
$stripped =~ s/<[^>]+>//g;
# $stripped =~ s/^\s+//g; will this interfere with /commands?
# $stripped =~ s/\s+$//g;
if (length $stripped) {
$poe_kernel->post( 'control', 'aim_got_message', $nick, $stripped );
}
}
sub aim_got_message {
my ($kernel, $heap, $nick, $msg) = @_[KERNEL, HEAP, ARG0, ARG1];
return unless exists $heap->{friends}->{$nick};
if ($kernel->alias_resolve( "irc_$nick" )) {
if ($msg =~ m|^/msg\s+(\S+)\s+(.*)$|i) {
$kernel->post( "irc_$nick", 'privmsg', $1, $2 );
} elsif ($msg =~ m|^/me\s+(.*)$|i) {
$kernel->post( "irc_$nick", 'ctcp', $channel, "ACTION $1" );
} elsif ($msg =~ m!^/(?:quit|part|leave)(?:\s+(.*))?$!i) {
my $quitmsg = $1 || "Client Exiting";
$kernel->post( "irc_$nick", 'quit', "[aimproxy] $quitmsg" );
} elsif ($msg =~ m|^/(\S+)|i) {
$kernel->yield( 'aim_queue', $nick, "[aimproxy] Unknown command: /$1" );
} else {
$kernel->post( "irc_$nick", 'privmsg', $channel, $msg );
}
} else {
$heap->{friends}->{$nick} = 1;
push @{$heap->{queue}->{$nick}}, $msg;
my $irc_nick = $nick;
$irc_nick =~ tr/A-Za-z0-9\-[]\\\`^{}/_/cs;
$irc_nick = substr $irc_nick, 0, 9;
POE::Component::IRC->new( "irc_$nick" )
or die "Can't create new IRC component for $nick: $!\n";
$kernel->post( "irc_$nick", 'register', 'all');
$kernel->post( "irc_$nick", 'connect', { Debug => 0,
Nick => $irc_nick,
Server => $irc_server,
Port => $irc_port,
Username => 'aimbot',
Ircname => 'VSCA AIM->IRC Proxy Bot', }
);
}
}
sub _net_aim_error {
my ($self, $evt) = @_;
my ($error, @stuff) = @{$evt->args()};
my $errstr = $evt->trans($error);
$errstr =~ s/\$(\d+)/$stuff[$1]/ge;
warn "AIM ERROR: $errstr\n";
}
sub aim_queue {
my ($kernel, $heap, $nick, $msg) = @_[KERNEL, HEAP, ARG0, ARG1];
return unless $heap->{friends}->{$nick};
push @{$heap->{aimqueue}}, [$nick, $msg];
$kernel->yield( 'aim_send' );
}
sub aim_send {
my ($kernel, $heap) = @_[KERNEL, HEAP];
my $timenow = [gettimeofday];
if (@{$heap->{aimqueue}} > 0 and
tv_interval( $heap->{lastsend}, $timenow ) > MSG_INTERVAL) {
my $msg = shift @{$heap->{aimqueue}};
$aim->send_im( $msg->[0], $msg->[1] );
$heap->{lastsend} = $timenow;
}
}
sub irc_001 {
my $kernel = $_[KERNEL];
$kernel->post( $_[SENDER], "join", $channel );
}
sub irc_433 {
my ($kernel, $sender) = @_[KERNEL, SENDER];
my $user = _get_aim_username( @_ );
my $irc_nick = $user;
$irc_nick =~ tr/A-Za-z0-9\-[]\\\`^{}/_/cs;
$irc_nick = substr $irc_nick, 0, 8;
my @punct = ('^', '`', '_', '\\', '-');
$kernel->post( $sender, 'nick', $irc_nick . $punct[ int( rand @punct ) ] );
}
sub _get_aim_username {
my ($kernel, $sender, $heap) = @_[KERNEL, SENDER, HEAP];
my $user = (split /_/, ($kernel->alias_list( $sender ))[0], 2)[1];
die "No such user: \"$user\"" unless exists $heap->{friends}->{$user};
return $user;
}
sub irc_ctcp_action {
my ($kernel, $heap, $who, $msg) = @_[KERNEL, HEAP, ARG0, ARG2];
my $user = _get_aim_username( @_ );
my ($nick) = ($who =~ /^(.*)?!/);
$kernel->yield( 'aim_queue', $user, "* $nick $msg" );
}
sub irc_disconnected {
my ($kernel, $sender, $heap, $server) = @_[KERNEL, SENDER, HEAP, ARG0];
my $user = _get_aim_username( @_ );
print "$user: Lost connection to server $server.\n";
delete $heap->{queue}->{$user};
$kernel->post( $sender, "shutdown" );
$kernel->yield( 'aim_queue', $user,
"[aimproxy] Lost connection to IRC server!" );
}
sub irc_error {
my ($kernel, $heap, $err) = @_[KERNEL, HEAP, ARG0];
my $user = _get_aim_username( @_ );
print "$user: Server error occurred! $err\n";
$kernel->yield( 'aim_queue', $user,
"[aimproxy] Error from $irc_server: $err" );
}
sub irc_join {
my ($kernel, $heap, $who, $chan) = @_[KERNEL, HEAP, ARG0, ARG1];
my $user = _get_aim_username( @_ );
my ($nick) = ($who =~ /^(.*)?!/);
$kernel->yield( 'aim_queue', $user, "*** $nick joined channel $channel." );
if ($heap->{friends}->{$user} and @{$heap->{queue}->{$user}} > 0) {
$kernel->yield( 'aim_got_message', $user,
shift @{$heap->{queue}->{$user}} );
}
}
sub irc_kick {
my ($kernel, $heap, $who, $chan, $victim, $msg) =
@_[KERNEL, HEAP, ARG0 .. $#_];
my $user = _get_aim_username( @_ );
my ($nick) = ($who =~ /^(.*)?!/);
$kernel->yield( 'aim_queue', $user,
"*** $victim was kicked from $channel by $nick ($msg)" );
}
sub irc_mode {
my ($kernel, $heap, $who, $chan, $modes) = @_[KERNEL, HEAP, ARG0 .. $#_];
my $user = _get_aim_username( @_ );
my ($nick) = ($who =~ /^(.*)?!/);
$modes .= " " . join( ' ', @_[ARG3 .. $#_] );
$kernel->yield( 'aim_queue', $user,
"*** Mode change on $chan by $nick: $modes" );
}
sub irc_msg {
my ($kernel, $heap, $who, $msg) = @_[KERNEL, HEAP, ARG0, ARG2];
my $user = _get_aim_username( @_ );
my ($nick) = ($who =~ /^(.*)?!/);
$kernel->yield( 'aim_queue', $user, "*$nick* $msg" );
}
sub irc_nick {
my ($kernel, $heap, $who, $newnick) = @_[KERNEL, HEAP, ARG0, ARG1];
my $user = _get_aim_username( @_ );
my ($nick) = ($who =~ /^(.*)?!/);
$kernel->yield( 'aim_queue', $user, "*** $nick is now known as $newnick." );
}
sub irc_notice {
my ($kernel, $heap, $who, $msg) = @_[KERNEL, HEAP, ARG0, ARG2];
my $user = _get_aim_username( @_ );
my ($nick) = ($who =~ /^(.*)?!/);
$kernel->yield( 'aim_queue', $user, "-$nick- $msg" );
}
sub irc_part {
my ($kernel, $heap, $who, $chan) = @_[KERNEL, HEAP, ARG0, ARG1];
my $user = _get_aim_username( @_ );
my ($nick) = ($who =~ /^(.*)?!/);
$kernel->yield( 'aim_queue', $user, "*** $nick has left $channel." );
}
sub irc_public {
my ($kernel, $heap, $who, $msg) = @_[KERNEL, HEAP, ARG0, ARG2];
my $user = _get_aim_username( @_ );
my ($nick) = ($who =~ /^(.*)?!/);
$kernel->yield( 'aim_queue', $user, "<$nick> $msg" );
}
sub irc_quit {
my ($kernel, $heap, $who, $msg) = @_[KERNEL, HEAP, ARG0, ARG1];
my $user = _get_aim_username( @_ );
my ($nick) = ($who =~ /^(.*)?!/);
$kernel->yield( 'aim_queue', $user, "*** $nick has quit IRC ($msg)." );
}
sub irc_socketerr {
my ($kernel, $heap, $err) = @_[KERNEL, HEAP, ARG0];
my $user = _get_aim_username( @_ );
print "$user: Can't connect to $irc_server:$irc_port! $err\n";
$kernel->yield( 'aim_queue', $user,
"[aimproxy] Can't connect to $irc_server:$irc_port: $err" );
}
POE::Session->create( package_states => [
'main' => [qw( _start _stop aim_buddy_update aim_friends
aim_got_message aim_listen aim_queue aim_send
irc_001 irc_433 irc_ctcp_action
irc_disconnected irc_error irc_join irc_kick
irc_mode irc_msg irc_nick irc_notice irc_part
irc_public irc_quit irc_socketerr )],
],
);
$poe_kernel->run();
exit 0;