http://qs321.pair.com?node_id=262562
Category: Fun Stuff
Author/Contact Info #include (include@riotmod.com)
http://linux.riotmod.com
Description: This script is a perl plugin for Gaim. To install it, just copy the script into your "plugins" directory, restart Gaim if it is already running, and then turn it on in the "Preferences" menu. This script turns your Gaim client into a Magic 8 Ball. If anyone sends you an instant message containing "!8ball" followed by a question, the script will select a "random" answer, and send it to the requesting client. This won't interrupt your regular IM traffic, and is all done in the background.

Edit: Updated and fixed a bug. Now all possible 8 ball messages are used. Thanks Juerd.
#
# magic8ball.pl
#
# GAIM script that delivers "usefull"
# answers to questions.
#
# Usage:
# !8ball <question>
#

my @ball_messages=
(
  "yes",
  "definitely",
  "the answer is yes",
  "positively sure",
  "no",
  "definitely not",
  "no way",
  "answer isn't clear",
  "there's no way to be sure"
);

sub description {
        my($a, $b, $c, $d, $e, $f) = @_;
        ("Magic 8 Ball", "0.1", "Answers all of your questions", "Incl
+ude &lt;include\@riotmod.com>", "http://linux.riotmod.com", "/dev/nul
+l");
}
$handle = GAIM::register("Magic 8 Ball", "0.1", "", "");
GAIM::add_event_handler($handle, "event_im_recv", "shake_the_ball");

sub shake_the_ball
{
  my($eb_connection,$eb_sender,$eb_message,$eb_flags)=@_;
  
  my @tokmess=split(" ",$eb_message);
  if($eb_message=~/!8ball/)
  {
    if($#tokmess > 0)
    {
      # shake the ball
      my $sm = $ball_messages[ rand scalar @ball_messages ];
      my $eb_message=~s/!8ball//g;
      my $answer_message="The magic 8 ball says...$ball_messages[$sm].
+";
      GAIM::serv_send_im($eb_connection, $eb_sender, $answer_message, 
+1);
      return 1;
    } else {
      GAIM::serv_send_im($eb_connection, $eb_sender, "You need to ask 
+a question!", 1);
      return 1;
    }
  }
}
Replies are listed 'Best First'.
Re: Gaim Magic 8 Ball
by Juerd (Abbot) on Jun 03, 2003 at 07:06 UTC

    my $sm = int rand($#ball_messages);

    How well did you test your application? Did you not notice that you never got the "there's no way to be sure" reply?

    Use @ball_messages instead of $#ball_messages here.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: Gaim Magic 8 Ball
by erasei (Pilgrim) on Jun 26, 2003 at 14:31 UTC
    A fun little script that makes me want to run Gaim just to play with Perl plugins :)

    I know it doesn't matter functionally, but there is a little typo (or maybe it's on purpose?) in your answers:

    "gefinitely not",
    probably wants to be:  "definitely not", Nothing important, I just know that I miss those things when I'm writing code too and always like to have it right.
      Heh heh, you're right. Just fixed it. Thanks :-).

      A genius writes code an idiot can understand, while an idiot writes code the compiler can't understand.
Re: Gaim Magic 8 Ball
by dragonchild (Archbishop) on Sep 16, 2003 at 03:09 UTC
    my $eb_message=~s/!8ball//g;

    That line looks like it's doing nothing useful. Why would you have it there? Also, you return 1; from both execution paths inside the !8ball scenario. Wouldn't it be clearer to remove that from the inner blocks and leave it in the middle block?

    Another suggestion would be to reverse your thought process. Instead of figuring out what you need to do to enter a block, figure out what you need to do to continue processing. Possibly done as such:

    sub shake_the_ball { my ($eb_connection, $eb_sender, $eb_message, $eb_flags) = @_; return unless $eb_message =~ /!8ball/; my $answer = split(' ', $eb_message) > 1 ? "You need to ask a question!" : "The magic 8 ball says..." . $ball_messages[ rand scalar @ball_m +essages ] . ".\n"; GAIM::serv_send_im($eb_connection, $eb_sender, $answer_message, 1); return 1; }

    Take code that's the same except for one variable and combine it so that the variable is assigned, not the code re-written. That improves clarity, reduces bugs, and can often provide a speed boost. (If only in the compile stage ...)

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Gaim Magic 8 Ball
by Coruscate (Sexton) on Jun 04, 2003 at 06:35 UTC

    Might you be able to post which version of perl and gaim are required to do such a thing? I am running perl 5.8.0 with gaim 0.62, and perl plugins do not seem to work.

    Never mind, it works :P


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.