]> jfr.im git - irc/quakenet/qwebirc.git/blob - qwebirc/ircclient.py
First stage of username/password stuff for hyperion.
[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, pass_ = f["nick"], f["ident"], f["ip"], f["realname"], f["hostname"], f.get("pass")
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 if pass_ is not None:
96 self.write("PASS :%s" % pass_)
97 self.write("NICK %s" % nick)
98
99 self.factory.client = self
100 self("connect")
101
102 def __str__(self):
103 return "<QWebIRCClient: %s!%s@%s>" % (self.__nickname, self.factory.ircinit["ident"], self.factory.ircinit["ip"])
104
105 def connectionLost(self, reason):
106 if self.lastError:
107 self.disconnect("Connection to IRC server lost: %s" % self.lastError)
108 else:
109 self.disconnect("Connection to IRC server lost.")
110 self.factory.client = None
111 basic.LineReceiver.connectionLost(self, reason)
112
113 def error(self, message):
114 self.lastError = message
115 self.write("QUIT :qwebirc exception: %s" % message)
116 self.transport.loseConnection()
117
118 def disconnect(self, reason):
119 self("disconnect", reason)
120 self.factory.publisher.disconnect()
121
122 class QWebIRCFactory(protocol.ClientFactory):
123 protocol = QWebIRCClient
124 def __init__(self, publisher, **kwargs):
125 self.client = None
126 self.publisher = publisher
127 self.ircinit = kwargs
128
129 def write(self, data):
130 self.client.write(data)
131
132 def error(self, reason):
133 self.client.error(reason)
134
135 def clientConnectionFailed(self, connector, reason):
136 protocol.ClientFactory.clientConnectionFailed(self, connector, reason)
137 self.publisher.event(["disconnect", "Connection to IRC server failed."])
138 self.publisher.disconnect()
139
140 def createIRC(*args, **kwargs):
141 f = QWebIRCFactory(*args, **kwargs)
142 reactor.connectTCP(config.IRCSERVER, config.IRCPORT, f)
143 return f
144
145 if __name__ == "__main__":
146 e = createIRC(lambda x: 2, nick="slug__moo", ident="mooslug", ip="1.2.3.6", realname="mooooo", hostname="1.2.3.4")
147 reactor.run()