AIM Bot using PHP

Discussion in 'OT Technology' started by Astro, Dec 14, 2003.

  1. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    A few months ago I ran into some PHP code to talk to the AIM network (using the TOC protocol). It was rather bare bones and some might think it still is. But I've added a little bit of code as well as a cheap handler class. It provides enough functionality for me. It shouldn't be too difficult to add functionality that you'd want (chat room support, warnings, etc).

    http://www.rsbauer.com/aim/aim.txt
    http://www.rsbauer.com/aim/aimfunctions.txt

    All you'll need is a box running PHP (ideally v4.x) that supports sockets. I haven't tried this on a Windows box. In theory, it should work as is. Remember if you grab these files, save them as .php and not .txt.

    Before starting, there's 3 lines you want to fiddle with in aim.txt: line 14,15,16 (and 13 if you put aimfunctions.txt in a goofy spot)

    Line 14: the screen name the bot will log in as
    Line 15: the bot's password
    Line 16: Ideally this should be your screen name. You can shut down the bot and this screen name will be the only one allowed to do that.

    After fiddling with aim.php, you can have all the fun you want with aimfunctions.php. This file works something like this:

    In aim.php, there's a handler that tries to figure out what to do based on the message that came in (and when I mean message, I mean the TOC protocol message). I've added 2 links towards the top of aim.php where one of them goes into nice detail about the TOC protocol messages. If you want to add a handler for a message, you just add a function to aimfunctions.php and call it "Do_" and the TOC protocol you want the function for. For example, the one in aimfunctions.php that signifies an imbound chat message is "IM_IN". So the handler function for that will be "Do_IM_IN($im)" <-- $im is my class which keeps track of the message info.


    You can fire up the bot either from a command prompt (using "php aim.php") or from putting the file in a web accessible path and then pointing your browser to it. Aimfunctions.php includes the "quit" command to allow you to shut it down from the screen name you specified in aim.php.

    Also included are commands to show a random quote and show the server's uptime assuming you're on a Linux box. If you don't want these commands, just delete the functions out of the file. If you want to play with the random quotes, you can download these files and make sure they're in the same directory:

    Lock Stock and 2 Smoking Barrels:
    http://www.rsbauer.com/aim/lstsb_quotes.txt

    Office Space:
    http://www.rsbauer.com/aim/osquotes.txt

    Snatch:
    http://www.rsbauer.com/aim/snatch_quotes.txt

    If you're mildly familiar with PHP, you should be able to see how I work the quotes and you're more than welcome to create your own quote files - as you can see, the hard part is actually collecting the quotes.

    Now for adding functionality to commands or things entered in an AIM convo:

    aimfunctions.php is where you're going to add this functionality. Right now I have code setup to act more as a command driven bot then one that holds a conversations with you. To add a command is pretty simple - just add a function prefixed with "msg_" then following the "_" put the first word of the command you want and in lower case (example: msg_menu($im) - user enters "menu" this funtion will get called. $im is the IM class). Then put whatever code in the function you want the bot to run. To send a reply back, just call $im->send([string to send]) and thats it.

    This is a bit rough around the edges, but it seems to perform well on my P100 box. You can test drive the code by talking to LizardMozi.

    I'd be curious if anyone finds this remotely useful or is able to get a bot online. And if you add more features/functionality, post it here...
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    bad ass man! I'll have to play with this when i get some free time!
     
  3. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Hmm, maybe I'll look at the protocol stuff in your code and integrate it into my TCL email checker :naughty:
     
  4. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Sweet! I think me re-login code works IF you do this:

    At lines 189 and 190, drop this in:

    PHP:
    //     if( !sendRaw("FLAPON\r\n\r\n") )
    //               die("Error sending the FLAPON packet.");
         
    if(!sendRaw("FLAPON\r\n\r\n"))
            return 
    1;
    The two commented out lines are the original lines 189 and 190.

    Here's been my problem and it just happened at 11:30EST: cable drops my connection. Its almost as often as the power goes out here! When cable gets dropped, everything comes back online at some point, except my bot which would drive me nuts. Well, I think this one die() function was root of my problems. It didn't remove the other die() functions so aim.php still can bomb out, but I gave it a whirl a few minutes ago. I unplugged my router. Verified on my PC that AIM was dropped and LizardMozi was definitely no longer logged in. I then plugged the router in. I logged in on the PC as myself. LizardMozi came back online automatically and I was able to send and receive a response from it. All the magic for this to work takes place at the end of aim.php in the while() loop.

    I wanted to do this with the Perl code for the longest time but it was a bit too messy for me to wrap my head around.

    I hadn't thought about having a bot as an email notifier. Good idea! I had thought about firing up my sensor computer and have it IM me when my apartment door opens and closes during the day (which would be a result of apartment management or thugs). But its fussy to setup so I'll wait to set it up when I move into a house (so far its set for April '04!)

    I may also mention, this code does receive buddy notifications and in theory, can act on them (I'm seeing messages come up when I go idle or log in). I saw a web site which plays off of this and lets you send a message to a person who is offline and when they come online, the message will get delivered.

    I also discovered HTTPQ which is a web interface to Winamp. All you do is send it query strings to do things and you can read the responses back. For some reason, I think it would be cool to be able to not only view the current song being played, but ALL the data associated with it, the play list, and having the ability to change to the next song (plus more). Although not overly practical, it would be pretty easy to do.
     
  5. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    heh, just implimented this.. added a few system functions like "who" and a couple others... pretty cool man :cool:
     
  6. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    doh!! what's the sign on timeout?? I restarted too many times in a row, now it won't let me sign on :hs:
     
  7. I used to love ICQ for this reason alone ... having msg's queue'd to offline users in AIM would ROCK! I may have to put up a web interface to AIM on one of my machines ... in all my spare time.
     
  8. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Yeah, thats the biggest problem I have is to add features, you have to shut the bot down and then restart it. Initially, you can stop/start pretty quickly, but after X amount of times (maybe 5 or so) it then stops you and tells you to wait 10 minutes which sucks. In 10 minutes, you can register another bot and bounce between the two :).

    One approach I use is after realizing I need to add functoinality, I swear at the bot for a minute. Then shut it down (ideally, you should shut it down and THEN swear at it). Swear at the code for a bit and then tweak it. Then start up the bot. The swearing part helps take up some time :).

    I have looked into having PHP swap out function definitions on the fly and its very grumpy about doing it. There's some trick where you setup a class and then you can swap the contents of the class but it was still complaining to me that the code had already been defined. Other than that, I haven't figured out a way to preserver the socket connection for the brief moment that the code gets reloaded.

    In recent news, I tried setting up the php aim bot with a socket server attached to it. I can setup a stand along and working php socket server, but the socket server is a 100% dedicated listener and doesn't want to move onto other code. There's nonblocking option which I set and it upchucks and says the socket is invalid. I haven't figured out what the deal is with this. But ideally, I'd like to have it setup so the bot could stay on 24/7 and when I want to update a web page, the web page just fires up a socket connection to the bot's socket server and they could talk amongst themselves without having to re-logon the bot (which as you know if you do enough you get bounced out). I've come SO CLOSE to making this work that its almost driving me nuts. I could setup a bot relay with this. Or a IM notification system based off of cron jobs or system log events. Or a web based IM buddy web page status thing. There's all sorts of possibilities. Hmm. Maybe when I have more time I'll post the socket code for you folks to hammer out. Maybe my PHP v4.3.0 is a little too out dated and this is a bug.
     
  9. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    Astro, I might have a little time over the holiday break to play around with some code, and this is definately pretty interesting so if you want another brain to look at it, let me know!
     
  10. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
  11. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Agh!

    I somewhat figured out sockets. Once you put the socket in nonblocking mode, you have to have code to do socket juggling which I wasn't aware of. So I found some socket juggling code and by itself, it works great. I had the test script busy printing an incremented variable and it continued to handle the socket connections. I plug the code into my AIM code and kind of works. Problem is the AIM code doesn't play nice - it gets stuck on fread() which causes my socket server to wait to do any processing until an IM has been sent. The IM then releases fread()'s grip and my socket server goes off and does its thing again.

    So I'm kind of leaning towards rewriting the PHP TOC functions to use the socket functions (and get rid of a global variable and maybe make a class out of it). If I do this, in theory, it may be very possible to have the one PHP script manage several AIM bots (multiple bots under different screen names using the same code). But I'll probably leave that for someone else to implement.

    If folks want the code to play with over the holidays, find me on IM, or send me an email or something and I'll hook you up. I can give you the latest and greatest I'm working with, but it may or may not be functional...
     
  12. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Agh again - but in a good way...

    After putting together and writing about 100 lines of code, I found out on php.net that fsockopen() can also be put into nonblocking mode (meaning the socket data gets buffered while the PHP code goes off and does something else). So just ONE line of code fixed the problem. So what this boils down to is I now have a working socket server to IM gateway - which also means whatever you have that can talk to a socket server could be tweaked to work with IM.

    Right now the code is just a "proof of concept" so now I'm going to put in some useful (well, to me at least) functionality. I'll post the code up on rsbauer.com when its presentable and I'll update this thread...
     
  13. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Ok - code update.

    You can find 2 php files here for my AIM bot project: http://www.rsbauer.com/webdev.php

    Examples of this code in action:
    AIM bot: LizardMozi

    If the IP address doesn't change any time soon:
    http://68.232.169.71:7777/aim/budinfo.php?user=robsbauer
    (swap out robsbauer with another user's screen name - use lower case and remove spaces) - The code for this is a bit rough around the edges. The images do not come up since they're supposed to point to another server. But otherwise, it will show you the user's profile if they are online.

    http://www.rsbauer.com/about.php
    The form can now be submitted as email or as an IM message. Just below the form, you can view my currently online status.

    The sky is almost the limit with the code. The AIM socket connection code should best be put in a class and switched from using fsockopen() to using PHP's socket functions. But I haven't gotten that far yet.

    For this code to work, you'll need sockets enabled in PHP. You'll also want to have Curl installed and enabled in PHP (not required - if you don't have curl, just remove the functions that use curl, or rewrite to use fsockopen()).

    In aimfunctions.php, you'll find my X10 code. So, if you have some X10 gear, and you have the Firecracker connected to a Linux box, just install Flipit and you too can control your X10 gear via AIM.
     
  14. nice!

    I'd been thinking of getting into some x10 stuff ... I've away from home for the holidays, but when I get back, I am DEFINITELY digging through this code!
     
  15. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Whoops... I didn't provide any examples on how to interface with the socket server. My bad.

    I've updated http://www.rsbauer.com/webdev.php to include a file called imrob.php. I've stripped out the IP, port, and screen name, but you can see where those go. This is the script that my about page interacts with. All parameters are seperated by '|'. The first parameter is always the command. This command matches up exactly to functions found in aimfunctions.php. All socket commands will be prefixed with "sserver_". If you wish to add a command, just think of the command and then prefix it with "sserver_" in aimfunctions.php and it will be immediately available (well, once you restart the php code). How you want to handle the parameters is completely up to you. Your function will receive the entire string you sent it (found in the $data variable if I remember correctly). Check out the other "sserver_" functions to get an idea how to do this.

    I think having this file will help those who are interested in interfacing this with the web or some other client. Yes, I believe if you know how to code plugins for Winamp, you should be easily able to use this socket server to control Winamp or have Winamp control an IM session. But this is just one idea...
     
  16. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    I wish I actually had time over the holidays to play with this :(
     
  17. Dustro

    Dustro New Member

    Joined:
    Nov 1, 2003
    Messages:
    2,123
    Likes Received:
    0
    Location:
    NC
    wish i knew how to play with this, im very interested in the end product though. As far as i have gotten is the original being able to see incoming messages but nothing as far as sending anything :(
     
  18. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    What exactly are you stuck on?
     
  19. Woodmaster

    Woodmaster New Member

    Joined:
    Dec 25, 2002
    Messages:
    15,448
    Likes Received:
    0
    Location:
    Brasil
    Nice job man I can't wait to check this out.
     
  20. Dustro

    Dustro New Member

    Joined:
    Nov 1, 2003
    Messages:
    2,123
    Likes Received:
    0
    Location:
    NC
    ok www.dustro.com/aim.php

    thats the only fuctionality i have gotten, i can send it messages and they show. im wanting to be able to send wheter it be through another screen or whatever like you have your page setup to send IM. Im at work behind a bs proxy and want aim functionality through my domain lol
     
  21. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    dude, if all you want is to use aim at work, AIM express will do the job
    http://www.aim.com/get_aim/express/aim_expr.adp?aolp=
     
  22. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Well, technically you could use this code to re-engineer AIM Express. But you'd probably want to build the web interface to interact with the socket server instead of letting the web interface directly with the bot itself (which is what it appears you've done with aim.php). Check out the sserver_send() function in aimfunctions.php. This function handles sending a message and then prints "Message sent" back to the socket. What you want to do though is send the inbound IM message back through the socket. So you might do something like:

    PHP:
    while(strlen($result['data'] < 10)
      
    $result readFlap();  // continually scans for inbound data

    $aResult explode(':'$result['data']);  // split data 
    if(strtoupper($aResult[0] == 'IM_IN')
      
    send_single($sock$aResult[3]);  // send inbound message back to the socket
    I haven't tested this, but it looks ok. You'd need some more code. Like you'd need to put the above code in a sserver_ function which would initiate socket communications. Then you'd have to build some sort of web page that handles taking the socket data and displaying it. Problem with this is its not really "real-time". Now, if you use client-side Java (not JavaScript), you should be able to build a socket client in Java and let the browser connect to that. Or, Mozilla has built in Chatzilla. I wonder if it could be tweaked to talk to the socket server. Hmm...

    Basically, the code I posted is a long ways from doing what you wish to do, but the key components are there so I don't think its impossible. It would just take some time to figure out how you want it all to interact.

    Yes, AIM Express would otherwise solve your problem. You might also check out GAIM (gaim.sourceforce.net ?). It looks and feels very much like the AIM client minus the ads and plus a ton of features. I believe if you go into its advanced account setup, you can persuade GAIM to use HTTP proxy and a couple other network settings which might be enough to work around your work's proxy.
     

Share This Page