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. |