Python IRC Bot, source v2

Oct 29, 2011 02:18 AM

#!/usr/bin/env python2.7

import sys #List of pre-made libararies to import

import socket

import string

import re

from time import sleep

safe = ['AlexLong'] #Users that can control the bots actions

#Global Variables

host="irc.freenode.org" #This is the IRC server variable.

port=6667 #This is the port

nick="NullBot" #Bot name

ident="NullBot" #ID to NickServ with this name

realname="NullBot" #Bots real name for server identification

channel="#nullbytez" #This is the channel name

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP ) #Creates a new socket

s.connect((host, port)) #Connect to the host IRC network through port 6667

s.sendall("NICK %s\r\n" % nick) #Sets the Bot's nick name

s.sendall("USER %s %s bla :%s\r\n" % (ident, host, realname)) #Logs Bot into IRC and ID's

s.send("JOIN :%s\r\n" % channel) #Join #nullbytez

s.send("PRIVMSG %s :%s\r\n" % (channel, "Hi :3 Imma robot")) #Send messages to channel

s.send("PRIVMSG %s :%s\r\n"% (channel, "Give me awpz!"))

f =s.makefile()

while 1: #Loop forever because 1 == always True (keeps us connected to IRC)

    line = f.readline().rstrip() #New readline for buffer

    print line

    if re.search(".*\001.*\001.*", line): #This block searches chan msgs for strings

        user = line.split('!~')[0][1:]

        s.sendall('PRIVMSG {0} :stop plox\r\n'.format(user))

    else:

        if '!quit' in line: #If a user PRIVMSG's '!quit' quit

            s.sendall("QUIT :Quit: Leaving..\r\n")

            break



        if '!op' in line:

            user = line[line.find('!op')+len('!op'):].rstrip().lstrip()

            if user in safe:

                s.sendall("MODE {0} +o {1}\r\n".format(channel, user))



        if 'PING' in line: #If the server pings, ping back (keeps connection)

            msg = line.split(':')[1].lstrip().rstrip()

            s.sendall("PONG {0}\r\n".format(msg))

Comments

No Comments Exist

Be the first, drop a comment!