-import twisted, sys\r
-from twisted.words.protocols import irc\r
-from twisted.internet import reactor, protocol\r
-from twisted.web import resource, server\r
-from twisted.protocols import basic\r
-\r
-import hmac, time, config\r
-from config import HMACTEMPORAL\r
-HMACKEY = hmac.HMAC(key=config.HMACKEY)\r
-\r
-def hmacfn(*args):\r
- h = HMACKEY.copy()\r
- h.update("%d %s" % (int(time.time() / HMACTEMPORAL), " ".join(args)))\r
- return h.hexdigest()\r
-\r
-class QWebIRCClient(basic.LineReceiver):\r
- delimiter = "\n"\r
- \r
- def dataReceived(self, data):\r
- basic.LineReceiver.dataReceived(self, data.replace("\r", ""))\r
-\r
- def lineReceived(self, line):\r
- line = irc.lowDequote(line)\r
- try:\r
- line = line.decode("utf-8")\r
- except UnicodeDecodeError:\r
- line = line.decode("iso-8859-1", "ignore")\r
- \r
- try:\r
- prefix, command, params = irc.parsemsg(line)\r
- self.handleCommand(command, prefix, params)\r
- except irc.IRCBadMessage:\r
- self.badMessage(line, *sys.exc_info())\r
- \r
- def badMessage(self, args):\r
- self("badmessage", args)\r
- \r
- def handleCommand(self, command, prefix, params):\r
- self("c", command, prefix, params)\r
- \r
- def __call__(self, *args):\r
- self.factory.publisher.event(args)\r
- \r
- def write(self, data):\r
- self.transport.write("%s\r\n" % irc.lowQuote(data.encode("utf-8")))\r
- \r
- def connectionMade(self):\r
- basic.LineReceiver.connectionMade(self)\r
- \r
- f = self.factory.ircinit\r
- nick, ident, ip, realname = f["nick"], f["ident"], f["ip"], f["realname"]\r
- \r
- hmac = hmacfn(ident, ip)\r
- self.write("NICK %s" % nick)\r
- self.write("USER %s bleh bleh %s %s :%s" % (ident, ip, hmac, realname))\r
- \r
- self.factory.client = self\r
- self("connect")\r
-\r
- def connectionLost(self, reason):\r
- self.factory.client = None\r
- basic.LineReceiver.connectionLost(self, reason)\r
- self("disconnect")\r
- \r
-class QWebIRCFactory(protocol.ClientFactory):\r
- protocol = QWebIRCClient\r
- def __init__(self, publisher, **kwargs):\r
- self.client = None\r
- self.publisher = publisher\r
- self.ircinit = kwargs\r
- \r
- def write(self, data):\r
- self.client.write(data)\r
- \r
-def createIRC(*args, **kwargs):\r
- f = QWebIRCFactory(*args, **kwargs)\r
- reactor.connectTCP(config.IRCSERVER, config.IRCPORT, f)\r
- return f\r
-\r
-if __name__ == "__main__":\r
- e = createIRC(lambda x: 2, nick="slug__moo", ident="mooslug", ip="1.2.3.6", realname="mooooo")\r
- reactor.run()
\ No newline at end of file
+import twisted, sys
+from twisted.words.protocols import irc
+from twisted.internet import reactor, protocol
+from twisted.web import resource, server
+from twisted.protocols import basic
+
+import hmac, time, config
+from config import HMACTEMPORAL
+HMACKEY = hmac.HMAC(key=config.HMACKEY)
+
+def hmacfn(*args):
+ h = HMACKEY.copy()
+ h.update("%d %s" % (int(time.time() / HMACTEMPORAL), " ".join(args)))
+ return h.hexdigest()
+
+class QWebIRCClient(basic.LineReceiver):
+ delimiter = "\n"
+
+ def dataReceived(self, data):
+ basic.LineReceiver.dataReceived(self, data.replace("\r", ""))
+
+ def lineReceived(self, line):
+ line = irc.lowDequote(line)
+ try:
+ line = line.decode("utf-8")
+ except UnicodeDecodeError:
+ line = line.decode("iso-8859-1", "ignore")
+
+ try:
+ prefix, command, params = irc.parsemsg(line)
+ self.handleCommand(command, prefix, params)
+ except irc.IRCBadMessage:
+ self.badMessage(line, *sys.exc_info())
+
+ if command == "001" and self.__perform:
+ for x in self.__perform:
+ self.write(x)
+
+ def badMessage(self, args):
+ self("badmessage", args)
+
+ def handleCommand(self, command, prefix, params):
+ self("c", command, prefix, params)
+
+ def __call__(self, *args):
+ self.factory.publisher.event(args)
+
+ def write(self, data):
+ self.transport.write("%s\r\n" % irc.lowQuote(data.encode("utf-8")))
+
+ def connectionMade(self):
+ basic.LineReceiver.connectionMade(self)
+
+ self.lastError = None
+ f = self.factory.ircinit
+ nick, ident, ip, realname = f["nick"], f["ident"], f["ip"], f["realname"]
+ self.__perform = f.get("perform")
+
+ hmac = hmacfn(ident, ip)
+ self.write("USER %s bleh bleh %s %s :%s" % (ident, ip, hmac, realname))
+ self.write("NICK %s" % nick)
+
+ self.factory.client = self
+ self("connect")
+
+ def connectionLost(self, reason):
+ if self.lastError:
+ self.disconnect("Connection to IRC server lost: %s" % self.lastError)
+ else:
+ self.disconnect("Connection to IRC server lost.")
+ self.factory.client = None
+ basic.LineReceiver.connectionLost(self, reason)
+
+ def error(self, message):
+ self.lastError = message
+ self.write("QUIT :qwebirc exception: %s" % message)
+ self.transport.loseConnection()
+
+ def disconnect(self, reason):
+ self("disconnect", reason)
+ self.factory.publisher.disconnect()
+
+class QWebIRCFactory(protocol.ClientFactory):
+ protocol = QWebIRCClient
+ def __init__(self, publisher, **kwargs):
+ self.client = None
+ self.publisher = publisher
+ self.ircinit = kwargs
+
+ def write(self, data):
+ self.client.write(data)
+
+ def error(self, reason):
+ self.client.error(reason)
+
+ def clientConnectionFailed(self, connector, reason):
+ protocol.ClientFactory.clientConnectionFailed(self, connector, reason)
+ self.publisher.event(["disconnect", "Connection to IRC server failed."])
+ self.publisher.disconnect()
+
+def createIRC(*args, **kwargs):
+ f = QWebIRCFactory(*args, **kwargs)
+ reactor.connectTCP(config.IRCSERVER, config.IRCPORT, f)
+ return f
+
+if __name__ == "__main__":
+ e = createIRC(lambda x: 2, nick="slug__moo", ident="mooslug", ip="1.2.3.6", realname="mooooo")
+ reactor.run()