Python IRC basic framework

By R0K on Dec 24, 2010

I'm starting a new bot on IRC and I've finished the framework for it, so I decided to post it. It only has the IRC protocol's that I'm going to use, so some are excluded.

the variable 'x' will always be a channel except in the case of msg.
the variable 'y' will always be a nickname except in the case of msg.
the variable 'z' will always be a message.
the variable 'arjok' at the top is auto rejoin on kick. 1 means it will auto rejoin on a kick; any other number means it wont.

commands that you make should be put in the while loop as I have showed with pong(), autoReJoinOnKick(), and autoJoinOnInvite().

Everything was made and tested using Python 2.7

I appreciate any comments and feedback.

#!/usr/bin/python
import socket
arjok=1
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
network = 'network.name.here'
irc.connect ( (network, 6667 ) )
irc.send('NICK nickname\r\n')
irc.send('USER <username> <hostname> <servername>: <realname>\r\n')
income=irc.recv (4096)
print income
def pong():
    if income.find('PING') != -1:
        irc.send('PONG ' + income.split()[1] + '\r\n')
def msg(x,z):
    irc.send ('PRIVMSG '+x +' :'+z +'\r\n')
def notice(y,z):
    irc.send('NOTICE '+x+' :'+z+'\r\n')
def join(x):
    irc.send('JOIN '+x+'\r\n')
def part(x):
    irc.send('PART '+x+'\r\n')
def whois(y):
    irc.send('WHOIS '+y+'\r\n')
def kick(x,y,z):
    irc.send('KICK '+x+' '+y+' '+z+'\r\n')
def ban(x,y):
    irc.send('MODE '+x+' +b '+y+'\r\n')
def kickban(x,y,z):
    kick(x,y,z,)
    ban(x,y)
def autoJoinOnInvite():
    if (income.find('INVITE') != -1):
        join(income.split()[3])
def autoReJoinOnKick(x):
    if ((income.find('KICK') != -1) and (arjok == 1)):
        join(x)
while True:
    income = irc.recv ( 4096 )
    pong()
    print income
    x=income.split()[2]
    autoReJoinOnKick(x)
    autoJoinOnInvite()

Comments

Sign in to comment.
FelicianoX   -  Aug 24, 2011

notice(y,z) = y should be x

 Respond  
sunslayer   -  Dec 25, 2010

you should add a check to see if the connection is still active to break out of the loop

 Respond  
R0K   -  Dec 25, 2010

yes, it will continue

 Respond  
sunslayer   -  Dec 24, 2010

i have absolutely no knowledge of Python so i may be wrong but won't the while loop continue even after you have been disconnected?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.