Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I can't seem to get my code to send im's and receive im's at the same time, it seems it keeps getting stuck in a loop. how would i remedy this? using this code below (from a thread i found on here) how would i modify this to actually receive messages as well, because if i add a subroutine for im_in, it never receives the event, since its stuck in a loop for sending messages... any help is greatly appreciated, thanks!
#!/usr/bin/perl use Net::OSCAR qw(:standard); %signon = ( screenname => 'myscreenname', password => 'mypassword', local_ip => 'myip', ); $recipient = "myfriend"; $message = "mymessage"; sub signon_done { print "signed on\n"; $online = 1; } $oscar = Net::OSCAR->new(); $oscar->set_callback_signon_done(\&signon_done); $oscar->signon(%signon); while(1) { $oscar->do_one_loop(); if ($online) { $oscar->send_im($recipient, $message); sleep(5); } }

Replies are listed 'Best First'.
Re: Net::OSCAR - a bot that will send and receive?
by marcussen (Pilgrim) on Dec 08, 2008 at 00:35 UTC

    Did you check the documentation? Granted it's not brimming with code examples, but there is quite a bit of text in Net::OSCAR. From a quick browse it appears that you need to declare a callback for:

    im_in (OSCAR, FROM, MESSAGE[, AWAY]) Called when someone sends you an instant message. If the AWAY para +meter is non-zero, the message was generated as an automatic reply, p +erhaps because you sent that person a message and they had an away me +ssage set.

    Confucius says kill mosquito unless cannon
      I understand, and have this:
      sub im_in { my($oscar, $sender, $message, $is_away) = @_; print "$sender: $message\n"; }
      this handles messages received, and of course we use this:
      $oscar->set_callback_im_in(\&im_in);
      but the problem is, it doesnt do anything if I go inside a loop of sending messages every X amount of seconds like the example above, it never goes into the callback function, because its stuck inside the loop.. thats the problem im having.

        I do not see the callback set in the code snippet you've posted. I'm fairly certain you need to call $oscar->set_callback_im_in(\&im_in) before you can expect that to be mapped. If it is called before your loop, then you should post the complete code where it is called (perhaps \&signon_done?). The do_one_loop method should call your im_in method if there's a message on the server (assuming your sleep(5) isn't causing connection problems).

Re: Net::OSCAR - a bot that will send and receive?
by otto (Beadle) on Dec 08, 2008 at 05:28 UTC
    Assuming this is a small piece in a bigger problem, you might check out POE on cpan.