Show DevBest [Python] IRC Bot

Status
Not open for further replies.

Pro123

New Member
Feb 20, 2011
21
0
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



This is a simple one
 

Dayron1234

Rapnameiszero,cuzIhavezero,toleranceforidiots
Jun 30, 2010
772
35
Download Python 2.7.1 or what ever is the latest release for it.
 

Jo$h

Posting Freak
Jul 7, 2010
1,030
79
You need Python to Run The Script because of the simple fact, it is coded in Python. No way to get around it.
 
Status
Not open for further replies.

Users who are viewing this thread

Top