Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Python] IRC Bot
#1
Simple IRC Bot.

Code:
import socket
import thread
import time
import sys
import re

class IRCBot:
    class IRCMessage:
        def __init__(self, rawdata, mesreg):
            try:
                self.raw = rawdata
                #self.regexmatch = mesreg.match(rawdata)
                self.regexmatch = None
                if not (self.regexmatch == None):
                    self.type = self.regexmatch.group(2)
                    self.message = self.regexmatch.group(3)
                    self.userdata = self.regexmatch.group(1)
            except:
                print "Message parse failed:",sys.exc_info()[0]
                self.regexmath = None
                self.type = None
                self.message = None
                self.userdata = None
    
    def __init__(self, _server, _port, _nick, _channel, _owner):
        self.server = _server
        self.port = _port
        self.nick = _nick
        self.channel = _channel
        self.owner = _owner
        self.quit = False
        self.restart = False
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock = s
        print "Connecting to server:",self.server
        contup = (self.server, self.port)
        self.sock.connect(contup)
        print "Connected, sending user info"
        self.sock.send("NICK " + self.nick + "\r\n")
        self.sock.send("USER " + self.nick + " " + self.nick + " " + self.nick + " :" + self.nick + "\r\n")
        self.msg = ""
        print "Joining channel:",self.channel
        self.join(self.channel)
        print "Initialising message handler"
        self._mesreg = re.compile("^(?:[:@]([^\\s]+) )?([^\\s]+)(?: ((?:[^:\\s][^\\s]* ?)*))?(?: ?:(.*))?$")
        print "Starting listen thread"
        self._thread = thread.start_new(self.receiveloop, (None,))
        
        

    def sendraw(self,raw):
        self.sock.send(raw + "\r\n")

    def receiveloop(self, _a):
        while True:
            try:
                if (self.quit):
                    break;
                self.msg += self.sock.recv(1024)
                msgs = self.msg.split("\n")
                while (len(msgs) >= 1):
                    _temp_msg = msgs.pop()
                    if ("PING" in _temp_msg):
                        _temp_spl = _temp_msg.split()
                        self.sendraw("PONG " + _temp_spl[1])
                    self.msg = self.msg[len(_temp_msg)+2:]
                    #handle message here
                    thread.start_new(self.handlemessage,(_temp_msg,))
            except:
                print "Unexpected error:", sys.exc_info()[0]
                time.sleep(2)

    def handlemessage(self,raw):
        try:
            
            _msg = self.IRCMessage(raw,self._mesreg)
            if not (_msg.regexmatch == None):
                print _msg.type + ":" + _msg.userdata +">",_msg.message
            else:
                print "RAW ---",_msg.raw
        except:
            print "Failed to parse message:",sys.exc_info()[0]
        

    def join(self,channel):
        self.sendraw("JOIN " + channel)
    
    def part(self,channel):
        self.sendraw("PART " + channel)

    def privmsg(self,target,message):
        self.sendraw("PRIVMSG " + target + " " + message)
    
    def sendquit(self,message):
        self.sendraw("QUIT " + message)
        time.sleep(3)

    def notice(self,target,message):
        self.sendraw("NOTICE " + target + " " + message)

    def beginrestart(self,message):
        self.sendquit(message)
        self.restart = True
Reply


Messages In This Thread
[Python] IRC Bot - by wchar_t - 08-05-2010, 11:42 PM
RE: [Python] IRC Bot - by Canoris - 08-05-2010, 11:43 PM
RE: [Python] IRC Bot - by wchar_t - 08-05-2010, 11:45 PM
RE: [Python] IRC Bot - by Bursihido - 12-11-2010, 04:12 PM
RE: [Python] IRC Bot - by dan08 - 03-29-2011, 01:06 PM
RE: [Python] IRC Bot - by Calx - 03-29-2011, 01:22 PM
RE: [Python] IRC Bot - by h4yr0 - 03-31-2011, 02:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Help Dεlluzion 3 2,003 09-30-2019, 12:59 AM
Last Post: samsmith001
  Simple Python Python Compiler Canoris 21 9,173 06-01-2011, 06:30 PM
Last Post: Filefinder
  Python 2 vs 3? Jake 8 2,661 12-11-2010, 04:13 PM
Last Post: Bursihido
  Python help Kharnage 2 889 02-12-2010, 09:07 PM
Last Post: Kharnage
  "==" and "is" in Python Canoris 1 846 02-07-2010, 03:55 PM
Last Post: uber1337

Forum Jump:


Users browsing this thread: 3 Guest(s)