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