Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import thread
- # A minimalistic library for connecting to IRC
- class IRCCommand:
- def __init__(self, msg=""):
- if msg == "": return
- msg = msg.strip()
- self.msg = msg
- while " " in msg:
- msg = msg.replace(" ", " ")
- if msg[0] == ":":
- prefix = msg[1:msg.find(" ")]
- else:
- prefix = ""
- nick = ""
- user = ""
- host = ""
- servername = ""
- if prefix != "":
- if prefix.find("!") == -1:
- nick = ""
- user = ""
- host = ""
- servername = prefix
- else:
- servername = ""
- nick = prefix[0:prefix.find("!")]
- user = prefix[prefix.find("!")+1:prefix.find("@")]
- host = prefix[prefix.find("@")+1:]
- if prefix != "":
- msg = msg[msg.find(" ")+1:]
- if msg.find(" ") == -1:
- command = msg.upper()
- params = ""
- trailing = ""
- else:
- command = msg[0:msg.find(" ")].upper()
- params = msg[msg.find(" ")+1:]
- if params != "":
- if params.find(":") == -1:
- trailing = ""
- else:
- trailing = params[params.find(":")+1:]
- params = params[0:params.find(":")-1]
- paramList = params.split(" ")
- self.prefix = prefix
- self.servername = servername
- self.nick = nick
- self.user = user
- self.host = host
- self.command = command
- self.params = params
- self.trailing = trailing
- self.paramList = paramList
- def __str__(self):
- return self.msg
- def formatted(self):
- if self.command in ["PRIVMSG", "NOTICE"]:
- return self.paramList[0] + ": " + self.nick + ": " + self.trailing
- return 0
- class IRCServer:
- def __init__(self, host, port=6667):
- self.host = host
- self.port = port
- self.sock = socket.socket()
- self.favoriteChannels = []
- self.connected = False
- self.identified = False
- self.joined = False
- self._buffer = ""
- self._messages = []
- self.verbose = False
- self.pretty = False
- self.logMode = False
- self.rawLogFile = "logRAW.txt"
- self.formattedLogFile = "log.txt"
- self.nick = ""
- self.password = ""
- self.echo = False
- def addFavoriteChannel(self, channel):
- self.favoriteChannels.append(channel)
- def removeFavoriteChannel(self, channel):
- self.favoriteChannels.remove(channel)
- def connect(self, nick, password="", user="", real=""):
- self.nick = nick
- self.password = password
- if user == "": user = nick.lower()
- if real == "": real = nick
- self.sock.connect((self.host, self.port))
- self.connected = True
- thread.start_newthread(self._listen, ())
- self._sendCommand("nick", [nick])
- self._sendCommand("user", [user, "8", "*"], real)
- def disconnect(self, msg=""):
- self._sendCommand("quit", [msg])
- self.sock.close()
- self.connected = False
- self.identified = False
- def poll(self):
- messages = self._messages
- self._messages = []
- return messages
- def kick(self, user, channel, reason=""):
- self._sendCommand("kick", [channel, user], reason)
- def ban(self, user, channel):
- self._sendCommand("mode", [channel, "+b", user+"!*@*"])
- def op(self, user, channel):
- self._sendCommand("mode", [channel, "+o", user])
- def action(self, msg, channel):
- self.say(chr(1)+"ACTION " + msg + chr(1), channel)
- def say(self, msg, channel):
- if self.echo:
- cmd = IRCCommand()
- cmd.nick = self.nick
- cmd.command = "PRIVMSG"
- cmd.trailing = msg
- cmd.paramList = [channel]
- self._messages.append(cmd)
- for msg in msg.split("\n"):
- self._sendCommand("privmsg", [channel], msg)
- self.logPretty2(channel + ": " + self.nick + ": " + msg)
- def notice(self, msg, channel):
- self._sendCommand("notice", [channel], msg)
- self.logPretty2(channel + ": " + self.nick + ": " + msg)
- def join(self, channel):
- self._sendCommand("join", [channel])
- def part(self, channel):
- self._sendCommand("part", [channel])
- def joinFavorites(self):
- for fav in self.favoriteChannels:
- self.join(fav)
- def verboseOut(self, msg):
- if self.verbose: print( msg)
- if self.logMode:
- f = open(self.rawLogFile, 'a')
- f.write(msg + "\n")
- f.close()
- def logPretty(self, cmd):
- self.logPretty2(cmd.formatted())
- def logPretty2(self, msg):
- if msg == 0: return
- if self.pretty: print(msg)
- if self.logMode:
- f = open(self.formattedLogFile, 'a')
- f.write(msg + "\n")
- f.close()
- def _listen(self):
- while self.connected:
- self._buffer += self.sock.recv(1024).decode()
- if(self._buffer.find("\r\n")):
- for msg in self._buffer.split("\r\n")[0:-1]:
- self.verboseOut("SERVER: " + msg)
- cmd = self._parse(msg)
- self.logPretty(cmd)
- self._handle(cmd)
- self._messages.append(cmd)
- self._buffer = self._buffer.split("\r\n")[-1]
- def _handle(self, cmd):
- if cmd.command == "PING":
- self._sendCommand("pong", trailing=cmd.trailing)
- elif cmd.command == "376":
- self.joinFavorites()
- self.joined = True
- elif not self.identified and cmd.nick.lower() == "nickserv":
- self._sendCommand("privmsg", ["nickserv"], "IDENTIFY " + self.password)
- self.identified = True
- def _parse(self, msg):
- return IRCCommand(msg)
- def _sendCommand(self, command, params=[], trailing=""):
- par = ""
- for p in params:
- if p.strip() != "": par += " " + p.strip()
- if trailing != "": trailing = " :" + trailing
- cmd = command.upper() + par + trailing
- self.verboseOut("CLIENT: " + cmd)
- self.sock.send((cmd + "\r\n").encode())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement