Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. import socket, ssl, imp
  2. import re, traceback
  3.  
  4. class Error(Exception):
  5.     pass
  6.  
  7. class ConnectionError(Error):
  8.     def __str__(self):
  9.         return "Disconnected!"
  10.  
  11. class IRC(object):
  12.     def __init__(self, nick, ident, name, host, port=6667, ssl=False, password=None, encoding="utf-8"):
  13.         self.connected = False
  14.         self.buffer = ""
  15.         self.irc = ""
  16.         self.dispatcher_prepare()
  17.         self.conf = dict()
  18.         self.conf["host"] = host
  19.         self.conf["port"] = port
  20.         self.conf["ssl"] = ssl
  21.         self.conf["nick"] = nick
  22.         self.conf["ident"] = ident
  23.         self.conf["name"] = name
  24.         self.conf["password"] = password
  25.         self.conf["encoding"] = encoding
  26.  
  27.     def connect(self):
  28.         self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  29.         if self.conf["ssl"]:
  30.             self.irc = ssl.wrap_socket(self.irc)
  31.         self.irc.connect((self.conf["host"], self.conf["port"]))
  32.         self.verbose_msg("Connected to %s:%s" % (self.conf["host"], self.conf["port"]))
  33.         if self.conf["password"]:
  34.             self.msg("PASS %s" % self.conf["password"])
  35.         self.verbose_msg("Identifying as %s (user:%s,name:%s)" % (self.conf["nick"],self.conf["ident"],self.conf["name"]))
  36.         self.msg("NICK %s" % self.conf["nick"])
  37.         self.msg("USER %s %s %s :%s" % (self.conf["ident"], self.conf["host"], self.conf["nick"], self.conf["name"]))
  38.         self.verbose_msg("function # returning from connect()")
  39.    
  40.     def quit(self):
  41.         self.irc.msg("QUIT")
  42.         self.irc.close()
  43.  
  44.     def dispatcher_prepare(self):
  45.         self.patterns = dict()
  46.         self.patterns["cmd"] = r"^\:([^ ]+)[ ]+([^ ]+)[ ]+\:?([^ ].*)?$"
  47.         self.patterns["privmsg"] = r"^\:([^ ]+)[ ]+PRIVMSG[ ]+([^ ]+)[ ]+\:?([^ ].*)?$"
  48.         self.patterns["kick"] = r"^\:([^ ]+)[ ]+KICK[ ]+\:?([^ ].*)?$"
  49.         self.patterns["ping"] = r"^PING[ ]+\:?([^ ].*)?$"
  50.         for p in self.patterns:
  51.             self.patterns[p] = re.compile(self.patterns[p])
  52.  
  53.     def dispatch(self, msg):
  54.         m = self.patterns["cmd"].match(msg)
  55.         if m:
  56.             sender = m.groups()[0]
  57.             cmd = m.groups()[1]
  58.             params = m.groups()[2]
  59.             self.handle_cmd(sender,cmd,params)
  60.         m = self.patterns["privmsg"].match(msg)
  61.         if m:
  62.             sender = m.groups()[0]
  63.             target = m.groups()[1]
  64.             params = m.groups()[2]
  65.             self.handle_privmsg(sender,target,params)
  66.             return
  67.         m = self.patterns["kick"].match(msg)
  68.         if m:
  69.             params = m.groups()[1]
  70.             self.handle_kick(params)
  71.             return
  72.         m = self.patterns["ping"].match(msg)
  73.         if m:
  74.             params = m.groups()[0]
  75.             self.handle_ping(params)
  76.             return
  77.    
  78.     def main_loop(self):
  79.         while 1:
  80.             try:
  81.                 read = self.irc.recv(512)
  82.                 if not read:
  83.                     raise ConnectionError()
  84.                 self.buffer = self.buffer + read.decode(self.conf["encoding"])
  85.                 temp = self.buffer.split("\r\n")
  86.                 self.buffer = temp.pop()
  87.                 for line in temp:
  88.                     self.verbose_msg("read < %s" % line)
  89.                     line = line.rstrip()
  90.                     self.dispatch(line)
  91.             except KeyboardInterrupt as e:
  92.                 raise e
  93.             except ConnectionError as e:
  94.                 raise e
  95.             except:
  96.                 self.msg("error ! unhandled exception")
  97.                 traceback.print_exc()
  98.    
  99.     def verbose_msg(self, msg):
  100.         print("<> %s" % msg)
  101.    
  102.     def msg(self, msg):
  103.         self.verbose_msg("sending > %s" % msg)
  104.         msg = "%s\r\n" % msg
  105.         self.irc.send(msg.encode(self.conf["encoding"]))
  106.    
  107.     def say(self, target, msg):
  108.         self.msg("PRIVMSG %s :%s" % (target, msg))
  109.    
  110.     def handle_kick(self, params):
  111.         pass
  112.  
  113.     def handle_ping(self, params):
  114.         self.msg("PONG :%s" % params)
  115.  
  116.     def handle_privmsg(self, sender, target, params):
  117.         pass
  118.  
  119.     def handle_cmd(self, sender, cmd, params):
  120.         if cmd=="NOTICE" and self.connected==False:
  121.             self.connected = True
  122.  
  123.     def quit(self):
  124.         self.msg("QUIT")
  125.         self.irc.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement