Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19

Warning: Function ereg() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 4

Warning: Function split() is deprecated in ..../includes/class_postbit.php(345) : eval()'d code on line 19
newbie perl question using Net::OSCAR module
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: newbie perl question using Net::OSCAR module

  1. #1

    newbie perl question using Net::OSCAR module

    I'm trying to write a bot using the Net::OSCAR module, and I keep getting this wierd error.

    syntax error at bot.pl line 14, near ")
    {"
    syntax error at bot.pl line 18, near "else"
    Execution of bot.pl aborted due to compilation errors.

    here's basically that whole bot.pl

    Code:
           use PHP::Strings;
           use Net::OSCAR qw(:standard);
            $users = ("some users i wanna give access to commands");
            $aim = Net::OSCAR->new();
            $aim->set_callback_im_in(\&im_in);
            $aim->set_callback_evil(\&evil);
            $aim->signon(username, password);
    while(1)
    {
     $aim->do_one_loop();
    }
    sub im_in($aim, $from, $msg)
    {
     $from = lc($from);
     $from =~ s/ //g;
     $test_user = strchr($users, $from)
     if (!$test_user)
      {
       $aim->send_im($from, "nice try");
       return;
      }
      else
      {
       $msg  =~ s/<(.|\n)+?>//g;
       $warn = strchr($msg, "/warn ");
        if ($warn)
         {
           $victim = str_replace("/warn ", "", $msg);
           $aim->evil($victim);
           return;
         }
       }
    }
    sub evil($from)
    {
     $aim->send_im($from, "don't warn me");
     while(1)
      {
     $aim->evil($from)
      }
    }

    This thing is probably riddled with errors, I'm a complete perl newbie

    [edit:gorn]added code tags[/edit]

  2. #2

    Re:newbie perl question using Net::OSCAR module

    You forgot a ; on the end of the predecessing line. Here is a reformated version of the code. It's eaiser for other programmers to read when formated in this way .

    Code:
    #!/usr/bin/perl
    use PHP::Strings;
    use Net::OSCAR qw(:standard);
    
    # Set configuration params --------------------------------
    $users = ("some users i wanna give access to commands");
    $aim = Net::OSCAR->new();
    $aim->set_callback_im_in(\&im_in);
    $aim->set_callback_evil(\&evil);
    $aim->signon(username, password);
    # ------------------------------------------------------------
    
    # Main Loop
    while(1) {
      $aim->do_one_loop();
    }
    
    # When an IM comes in do the following ...
    sub im_in($aim, $from, $msg) {
      $from = lc($from);
      $from =~ s/ //g;
    
      # !!!THE FOLLOWING LINE HAD THE ERROR!!!
      $test_user = strchr($users, $from); 
      # !!THE ABOVE LINE HAD THE ERROR!!!
    
      if (!$test_user) {
        $aim->send_im($from, "nice try");
        return;
      } else {
        $msg  =~ s/<(.|\n)+?>//g;
        $warn = strchr($msg, "/warn ");
          if ($warn) {
            $victim = str_replace("/warn ", "", $msg);
            $aim->evil($victim);
            return;
          }
      }
    }
    
    # If someone 'warns' the bot then ...
    sub evil($from) {
      $aim->send_im($from, "don't warn me");
      while(1) {
        $aim->evil($from)
      }
    }

  3. #3

    Re:newbie perl question using Net::OSCAR module

    Ah yes... I am very bad at commenting and keeping the code, thanks for fixing it up.
    too bad PHP::Strings is largely incomplete, and str_replace() (replace a part of a string) and strchr() (search within a string) are not yet implemented, would anyone happen to know the perl equivlants?

  4. #4

    Re:newbie perl question using Net::OSCAR module

    [quote author=countach44 link=board=9;threadid=8256;start=0#msg74922 date=1070829557]
    Ah yes... I am very bad at commenting and keeping the code, thanks for fixing it up.
    [/quote]

    No problem. If you end up releasing the code as OSS submit it to me and I'll 'clean it up' a bit for you. It can't hurt

    too bad PHP::Strings is largely incomplete, and str_replace() (replace a part of a string) and strchr() (search within a string) are not yet implemented, would anyone happen to know the perl equivlants?
    You probably want to use regex. Perl is the king of regex and allows you to do just about anything with a string.

    However, you could use substr for replacements (See http://www.perldoc.com/perl5.8.0/pod/func/substr.html) instead of making a regex statement.

    When seraching through a string it is best to use regex. For instance, to find if the string abc is in $string you could use:

    Code:
    $string =~ /abc/;
    The statement will ring true if it is found.

    In fact, you can use the following subroutine for ease of use:

    Code:
    # Returns the number of times a string/char is found
    # in string.
    sub matchstr($$) {
      my ($string, $match) = @_;
    
      @matches = $string =~ /($match)/g;
      return scalar(@matches);
    }
    You can find out more by reading Japhy's online book on Regular Expressions (http://japhy.perlmonk.org/book/).

  5. #5

    Re:newbie perl question using Net::OSCAR module

    so would this work?
    Code:
    sub im_in($aim, $from, $msg) {
      $from = lc($from);
      $from =~ s/ //g;
      if (!$users =~ /$from/) {
        $aim->send_im($from, "nice try");
        return;
      } else {
        $msg  =~ s/<(.|\n)+?>//g;
          if ($warn =~ /!warn/) {
            $victim = str_replace("!warn ", "", $msg);
            $aim->evil($victim);
            $aim->send_im($from, '$victim warned');
            return;
          }
      }
    }
    because i get nothing when i im the bot with the !warn $victim... I'm probably taking a completely wrong approach to this..

    I also tried this, implementing the subroutine you gave me... I think I did this wrong as well...
    Code:
    sub im_in($aim, $from, $msg) {
      $from = lc($from);
      $from =~ s/ //g;
      $test_user = matchstr($from, $users);
      if ($test_user == 0) {
        $aim->send_im($from, "nice try");
        return;
      } else {
        $msg  =~ s/<(.|\n)+?>//g;
          $warn = matchstr($msg, !warn);
          if ($warn != 0) {
            $victim = str_replace("!warn ", "", $msg);
            $aim->evil($victim);
            $aim->send_im($from, '$victim warned');
            return;
          }
      }
    }
    P.S.
    i could submit it as OSS, but i doubt it's good enough to be of any use, or would the philosophy behind OSS be to make it better?

  6. #6

    Re:newbie perl question using Net::OSCAR module

    Neither of those should work because you are trying to call str_replace() which isn't a perl function (unless you are implementing your own). You would need to use a search and replace regex instead. The rest of the code looks like it should work with the exception of !warn being a bareword (should be "!warn&quot.

    Code:
    sub im_in($aim, $from, $msg) {
      # Make $from all lowercase
    * $from = lc($from);
      # replace all spaces to nothing in the $from string
    * $from =~ s/ //g;
      # if the $users matches (in part or full) $from
    * $test_user = matchstr($from, $users);
      # if it didn't match ...
    * if ($test_user == 0) {
    * * $aim->send_im($from, "nice try");
    * * return;
    * } else {
        # if it did match
        # replace 'something' with nothing
    * * $msg* =~ s/<(.|\n)+?>//g;
        # see if !warn matches $msg in part or full
    * * * $warn = matchstr($msg, "!warn");
          # if there was a match
    * * * if ($warn != 0) {
            # replace with a subroutine !warn to nothing and call it victim
    * * * * $victim = str_replace("!warn ", "", $msg);
    * * * * $aim->evil($victim);
    * * * * $aim->send_im($from, '$victim warned');
    * * * * return;
    * * * }
    * }
    }

    Here is some code for a basic str_replace($$$):

    Code:
    # Returns the updated version of the string updating globaly
    sub str_replace($$$) {
      my ($from, $to, $string) = @_;
    
      $string =~ s/$from/$to/g;
      return $string;
    }
    I am going to go step by step through the code and explain whats happening to make sure thats what you are wanting to happen.

    Making it OSS is a good idea, but it depends on what this bot is for. If you are making a general bot for fun/utility then open it even on the small scale (with friends or board members). If it is just a small toy with no real want to expand it to do other functions (like email through IM and stuff) then just keeping it to yourself is probably the way to go.

  7. #7

    Re:newbie perl question using Net::OSCAR module

    i need to keep reminding myself that Php and perl aren't 100% cross compatitble (speling?)

    im getting confused with this whole str_replace() thing here's a basic definition in the php context
    str_replace($thing_to_be_replaced, $what_to_replace_with, $where _to_replace)

    I'm confused at what the str_replace you defined does (it may be the same thing as what i described above)
    also i tried getting my bot to warn somebody on a user not defined in $users, and i pulled the same errors with str_replace, meaning the user got past
    Code:
      $test_user = matchstr($from, $users);
      # if it didn't match ...
      if ($test_user == 0) {
        $aim->send_im($from, "nice try");
        return;

  8. #8

    Re:newbie perl question using Net::OSCAR module

    Both operations you've described can be done with regexp's. The definitive guide: http://www.perldoc.com/perl5.8.0/pod/perlre.html is where I learned everything I know abour RE in perl, and I use them heavily every single day for my job. Extremely powerful stuff, if a little gory.

    You've already been introduced to the "search within a string" part of your query (using the =~ mapping operator). In order to replace part of a string, use the s/pattern/replacement/ operator. It should also be in the perlre documentation. Basically if you've ever done pattern replacement in vim it's very similar.

    Use as follows:

    $variable = "The cat is in the barn";
    $variable =~ s/barn/bar/;

    print $variable;

    This will yield "The cat is in the bar". Use "g" at the end of the expression to find and replace all instances instead of the first. For example:

    $variable = "The barn cat is in the barn";
    $variable =~ s/barn/bar/g;
    print $variable;

    Output: "The bar cat is in the bar";

    Do a little reading and you'll pick it up. It's incredibly fun, and powerful too. If only it weren't so gory to read and write.

  9. #9

    Re:newbie perl question using Net::OSCAR module

    thanks...
    i was going through some perldoc stuff and it was just driving me nuts...

  10. #10

    Re:newbie perl question using Net::OSCAR module

    i dunno what's with this thing... i got some help from someone (who is on these boards, don't know whether they'd like me mention their name, but here's what i got so far

    Code:
    #!/usr/bin/perl -w
    use PHP::Strings;
    use Net::OSCAR qw(:standard);
    
    # Set configuration params --------------------------------
    %users = ('allowed_user' => 1, 'unauthorized' => 0);
    $aim = Net::OSCAR->new();
    $aim->set_callback_im_in(\&im_in);
    $aim->set_callback_evil(\&evil);
    $aim->signon(screenname => "sn", password => "passwd", OSCAR_SVC_AIM)
    ;
    $aim->set_visibility(VISMODE_PERMITALL);
    # ------------------------------------------------------------
    
    # Main Loop
    while(1) {
      $aim->do_one_loop();
    }
    
    # When an IM comes in do the following ...
    sub im_in($aim, $from, $msg) {
      $from = lc($from);
      $from =~ s/ //g;
      if (!$users{$from}) {
        $aim->send_im($from, "nice try");
        return;
      } else {
        $msg  =~ s/<.*?>//g;
        @command = split(/ /, $msg);
          if ($command[0] eq "!warn") {
            $victim = $command[1];
            $aim->evil($victim);
            $aim->send_im($from, "$victim warned");
            return;
          }
          if ($command[0] eq "!dc") {
            $aim->send_im($from, "disconnecting");
            $aim->signoff;
          }
          if ($command[0] eq "hello") {
            $aim->send_im($from, "hello");
            return;
      }
     }
    }
    
    # If someone 'warns' the bot then ...
    sub evil($aim, $newevil, $from) {
        $aim->send_im($from, "don't warn me");
        $aim->evil($from);
        $aim->evil($from);
        $aim->evil($from);
        $aim->evil($from);
    }
    i've gone through a bunch of revisions... problem is that when i tell the bot commands, it does nothing.. i don't think i quite understand callbacks yet

Similar Threads

  1. Perl module problem
    By Outlaw in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 05-04-2005, 02:17 PM
  2. Some Perl Questions, File::Find Module
    By shebang in forum Linux - Software, Applications & Programming
    Replies: 18
    Last Post: 04-03-2004, 07:25 PM
  3. perl question from a perl newb
    By tolstoy in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 09-06-2002, 01:16 AM
  4. MP3::Info (perl module)
    By in forum Linux - Software, Applications & Programming
    Replies: 2
    Last Post: 07-20-2002, 10:10 PM
  5. web sites for newbie Perl coders
    By cga in forum Linux - Software, Applications & Programming
    Replies: 1
    Last Post: 03-21-2002, 02:41 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •