]> jfr.im git - irc/quakenet/qwebirc.git/blob - qwebirc/ircclient.py
Add ability to get a stacktrace by sending SIGUSR1.
[irc/quakenet/qwebirc.git] / qwebirc / ircclient.py
1 import twisted, sys, codecs, traceback
2 from twisted.words.protocols import irc
3 from twisted.internet import reactor, protocol
4 from twisted.web import resource, server
5 from twisted.protocols import basic
6
7 import hmac, time, config
8 from config import HMACTEMPORAL
9
10 if config.WEBIRC_MODE == "hmac":
11 HMACKEY = hmac.HMAC(key=config.HMACKEY)
12
13 def hmacfn(*args):
14 h = HMACKEY.copy()
15 h.update("%d %s" % (int(time.time() / HMACTEMPORAL), " ".join(args)))
16 return h.hexdigest()
17
18 def utf8_iso8859_1(data, table=dict((x, x.decode("iso-8859-1")) for x in map(chr, range(0, 256)))):
19 return (table.get(data.object[data.start]), data.start+1)
20
21 codecs.register_error("mixed-iso-8859-1", utf8_iso8859_1)
22
23 def irc_decode(x):
24 try:
25 return x.decode("utf-8", "mixed-iso-8859-1")
26 except UnicodeDecodeError:
27 return x.decode("iso-8859-1", "ignore")
28
29 class QWebIRCClient(basic.LineReceiver):
30 delimiter = "\n"
31 def __init__(self, *args, **kwargs):
32 self.__nickname = "(unregistered)"
33
34 def dataReceived(self, data):
35 basic.LineReceiver.dataReceived(self, data.replace("\r", ""))
36
37 def lineReceived(self, line):
38 line = irc_decode(irc.lowDequote(line))
39
40 try:
41 prefix, command, params = irc.parsemsg(line)
42 self.handleCommand(command, prefix, params)
43 except irc.IRCBadMessage:
44 # emit and ignore
45 traceback.print_exc()
46 return
47
48 if command == "001":
49 self.__nickname = params[0]
50
51 if self.__perform is not None:
52 for x in self.__perform:
53 self.write(x)
54 self.__perform = None
55 elif command == "NICK":
56 nick = prefix.split("!", 1)[0]
57 if nick == self.__nickname:
58 self.__nickname = params[0]
59
60 def handleCommand(self, command, prefix, params):
61 self("c", command, prefix, params)
62
63 def __call__(self, *args):
64 self.factory.publisher.event(args)
65
66 def write(self, data):
67 self.transport.write("%s\r\n" % irc.lowQuote(data.encode("utf-8")))
68
69 def connectionMade(self):
70 basic.LineReceiver.connectionMade(self)
71
72 self.lastError = None
73 f = self.factory.ircinit
74 nick, ident, ip, realname, hostname = f["nick"], f["ident"], f["ip"], f["realname"], f["hostname"]
75 self.__nickname = nick
76 self.__perform = f.get("perform")
77
78 if config.WEBIRC_MODE == "hmac":
79 hmac = hmacfn(ident, ip)
80 self.write("USER %s bleh bleh %s %s :%s" % (ident, ip, hmac, realname))
81 elif config.WEBIRC_MODE == "webirc":
82 self.write("WEBIRC %s qwebirc %s %s" % (config.WEBIRC_PASSWORD, hostname, ip))
83 self.write("USER %s bleh %s :%s" % (ident, ip, realname))
84 elif config.WEBIRC_MODE == "cgiirc":
85 self.write("PASS %s_%s_%s" % (config.CGIIRC_STRING, ip, hostname))
86 self.write("USER %s bleh %s :%s" % (ident, ip, realname))
87 else:
88 if ip == hostname:
89 dispip = ip
90 else:
91 dispip = "%s/%s" % (hostname, ip)
92
93 self.write("USER %s bleh bleh :%s - %s" % (ident, dispip, realname))
94
95 self.write("NICK %s" % nick)
96
97 self.factory.client = self
98 self("connect")
99
100 def __str__(self):
101 return "<QWebIRCClient: %s!%s@%s>" % (self.__nickname, self.factory.ircinit["ident"], self.factory.ircinit["ip"])
102
103 def connectionLost(self, reason):
104 if self.lastError:
105 self.disconnect("Connection to IRC server lost: %s" % self.lastError)
106 else:
107 self.disconnect("Connection to IRC server lost.")
108 self.factory.client = None
109 basic.LineReceiver.connectionLost(self, reason)
110
111 def error(self, message):
112 self.lastError = message
113 self.write("QUIT :qwebirc exception: %s" % message)
114 self.transport.loseConnection()
115
116 def disconnect(self, reason):
117 self("disconnect", reason)
118 self.factory.publisher.disconnect()
119
120 class QWebIRCFactory(protocol.ClientFactory):
121 protocol = QWebIRCClient
122 def __init__(self, publisher, **kwargs):
123 self.client = None
124 self.publisher = publisher
125 self.ircinit = kwargs
126
127 def write(self, data):
128 self.client.write(data)
129
130 def error(self, reason):
131 self.client.error(reason)
132
133 def clientConnectionFailed(self, connector, reason):
134 protocol.ClientFactory.clientConnectionFailed(self, connector, reason)
135 self.publisher.event(["disconnect", "Connection to IRC server failed."])
136 self.publisher.disconnect()
137
138 def createIRC(*args, **kwargs):
139 f = QWebIRCFactory(*args, **kwargs)
140 reactor.connectTCP(config.IRCSERVER, config.IRCPORT, f)
141 return f
142
143 if __name__ == "__main__":
144 e = createIRC(lambda x: 2, nick="slug__moo", ident="mooslug", ip="1.2.3.6", realname="mooooo", hostname="1.2.3.4")
145 reactor.run()