use Net::ICQ; $uin = 1234; $password = "password"; $filename = ".micqbot"; $icq = Net::ICQ->new($uin,$password); $icq->connect() || die ("could not connect!?"); $icq->add_handler('SRV_SYS_DELIVERED_MESS', \&on_msg); print ("Logged in\n"); $SIG{INT} = \&disconnect; print ("Loading users\n"); load(); print ("Starting event loop\n"); $icq->start(); ####################### subs ####################### ################### on_msg ################### sub on_msg { my ($icq, $event) = @_; my ($in_params, $out_params, $uin); print("Message received\n"); $in_params = $event->{params}; $uin=$in_params->{uin}; if ($in_params->{type} == 1) { # normal text message print("$uin: \t$in_params->{text}\n"); $out_params = {}; $out_params->{type} = 1; $out_params->{receiver_uin} = $uin; if ($in_params->{text} =~ /^#/) { if ($in_params->{text} =~ /^#help$/) { help($uin); } elsif ($in_params->{text} =~ /^#all$/) { foreach $key (keys %users) { $out_params->{text} = "$users{'$key'}\n$out_params->{text}"; } if ($out_params->{text} eq "") { $out_params->{text} = " [none]\n"; } $out_params->{text} = "User List:\n\n$out_params->{text}"; $icq->send_event('CMD_SEND_MESSAGE',$out_params); } elsif ($in_params->{text} =~ /^#who$/) { ### NOTDONE foreach $key (keys %users) { $out_params->{text} = "$users{'$key'}\n$out_params->{text}"; } if ($out_params->{text} eq "") { $out_params->{text} = " [none]\n"; } $out_params->{text} = "User List (online):\n\n$out_params->{text}"; $icq->send_event('CMD_SEND_MESSAGE',$out_params); } elsif ($in_params->{text} =~ /^#subscribe .*/) { #verify the user number isn't already subscribed my ($name) = substr($in_params->{text},11,length($in_params->{text})); my (%ids) = reverse(%users); if (exists($users{$uin})) { $out_params->{text} = "You are already subscribed as $users{$uin}"; $icq->send_event('CMD_SEND_MESSAGE',$out_params); #verify the user name doesn't already exist } elsif (exists($ids{$name})) { $out_params->{text} = "You are already subscribed as $users{$uin}"; $icq->send_event('CMD_SEND_MESSAGE',$out_params); #subscribe the user } else { #alert any $users{$uin} = $name; spam("[$users{$uin} has subscribed]"); } } elsif ($in_params->{text} =~ /^#unsubscribe$/) { spam("[$users{$uin} has unsubscribed]"); delete ($users{$uin}); } else { # unknown command help($uin,"Unknown command: $in_params->{text}"); } } else { # not a command #go through all users, figure out who is online, send message to them spam ("Message from $users{$uin}:\n\n$in_params->{text}",$uin); } } else { # some othersort of message print("Some other sort of message\n"); } } ################### disconnect ################### sub disconnect { $icq->disconnect(); #save users to a file? print ("Saving users.\n"); save(); exit(); } ################### save ################### sub save { open(ELIF,">$filename"); foreach $key (keys %users) { print ELIF "$key\n$users{$key}\n"; } close ELIF ; } ################### load ################### sub load { my($id,$name); open(ELIF,"<$filename"); while() { $id=$_; $name=; chop($id); chop($name); $users{$id}=$name; print("user: '$name'->'$id'\n"); } close ELIF ; } ################### help ################### sub help { my ($uin,$message) = @_; my ($params); $params = {}; $params->{type} = 1; $params->{text} = " #help -- this menu #who -- subscribed users currently on #all -- all subscribed users #subscribe -- subscribe #unsubscribe -- unsubscribe "; if ($message ne "") { $params->{text} = "$message\n$params->{text}"; } $params->{receiver_uin} = $uin; print("SENDING HELP TO $uin\n"); $icq->send_event('CMD_SEND_MESSAGE',$params); } ################### spam ################### sub spam { #send a message to all online folks except for uin my ($message,$uin) = @_; my ($params); $params = {}; $params->{type} = 1; $params->{text} = $message; foreach $key (keys %users) { if ($key == $uin) { next; } $params->{receiver_uin}=$key; $icq->send_event('CMD_SEND_MESSAGE',$params); } }