Python
  Home arrow Python arrow Page 5 - Python and IRC
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python and IRC
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 29
    2005-02-16

    Table of Contents:
  • Python and IRC
  • Hello IRC World
  • Can You Hear Me?
  • I Can Hear You!
  • IRC Mathematics

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Dell PowerEdge Servers

    Python and IRC - IRC Mathematics
    (Page 5 of 5 )

    Let's apply our knowledge to something useful – a Python-powered IRC bot that performs basic mathematical functions. Let's make the bot perform arithmetic calculations and a few trigonometric functions: sine, cosine and tangent.

    We will first create a file to perform the calculations for us. Create a file named ircMath.py and insert the following code.

    import math

    def arithmatic ( args ):

       args [ 0 ] = args [ 0 ].replace ( '\r\n', '' )
       for letter in 'abcdefghijklmnopqrstuvwxyz':
          args [ 0 ] = args [ 0 ].replace ( letter, '' )
       solution = str ( eval ( args [ 0 ], { '__builtins__' : {} } ) )
       return solution

    def sine ( args ):

       solution = str ( math.sin ( float ( args [ 0 ] ) * ( 2 * math.pi ) / 360 ) )
       return solution

    def cosine ( args ):

       solution = str ( math.cos ( float ( args [ 0 ] ) * ( 2 * math.pi ) / 360 ) )
       return solution

    def tangent ( args ):

       solution = str ( math.tan ( float ( args [ 0 ] ) * ( 2 * math.pi ) / 360 ) )
       return solution

    The guts of ircMath.py aren't too important, and the code should be pretty straightforward, so I won't get into detail.

    We will now create the bot. Its code will be a modified version of the last section's script. We will search the incoming message for the string "%PyIRC" to discriminate between messages we do and do not need. We will then split up the message into a function and arguments. Finally, we will call the appropriate function.

    import ircMath
    import socket

    network = 'irc.insert.a.network.here'
    port = 6667
    irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
    irc.connect ( ( network, port ) )
    irc.recv ( 4096 )
    irc.send ( 'NICK PyIRC\r\n' )
    irc.send ( 'USER PyIRC PyIRC PyIRC :Python IRC\r\n' )
    irc.send ( 'JOIN #pyirc\r\n' )
    irc.send ( 'PRIVMSG #pyirc :Hello.\r\n' )
    while True:
       data = irc.recv ( 4096 )
       if data.find ( 'PING' ) != -1:
          irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
       elif data.find ( 'PRIVMSG' ) != -1:
          message = ':'.join ( data.split ( ':' ) [ 2: ] )
          if message.lower().find ( '%pyirc' ) == 0:
             nick = data.split ( '!' ) [ 0 ].replace ( ':', '' )
             destination = ''.join ( data.split ( ':' ) [ :2 ] ).split ( ' ' ) [ -2 ]
             function = message.split ( ' ' ) [ 1 ]
             if function == 'calc':
                try:
                   args = message.split ( ' ' ) [ 2: ]
                   solution = ircMath.arithmatic ( args )
                   irc.send ( 'PRIVMSG ' +  destination + ' :' + nick + ': ' + solution + '\r\n' )
                except:
                   pass
             if function == 'sin':
                try:
                   args = message.split ( ' ' ) [ 2: ]
                   solution = ircMath.sine ( args )
                   irc.send ( 'PRIVMSG ' +  destination + ' :' + nick + ': ' + solution + '\r\n' )
                except:
                   pass
             if function == 'cos':
                try:
                   args = message.split ( ' ' ) [ 2: ]
                   solution = ircMath.cosine ( args )
                   irc.send ( 'PRIVMSG ' +  destination + ' :' + nick + ': ' + solution + '\r\n' )
                except:
                   pass
             if function == 'tan':
                try:
                   args = message.split ( ' ' ) [ 2: ]
                   solution = ircMath.tangent ( args )
                   irc.send ( 'PRIVMSG ' +  destination + ' :' + nick + ': ' + solution + '\r\n' )
                except:
                   pass

    Start up the script and enter the specified channel on the specified network. Try saying these lines:

    %PyIRC calc 2+2
    %PyIRC calc 8+10
    %PyIRC sin 30
    %PyIRC cos 45
    %PyIRC tan 27

    Our bot should give us the answer to each of the problems. Pretty neat, huh?

    Conclusion

    You should now know the basics of Python and IRC connectivity. Try to expand on the examples provided in this article to create your own unique scripts. If you would like to learn more about the IRC protocol, the protocol is documented here:

    http://www.irchelp.org/irchelp/rfc


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hey, all. I thought this would be a unique topic to write about. I haven't seen...
       · very nice article, it's very rare to see someone talking about writing scripts for...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway