]>
jfr.im git - irc/quakenet/qwebirc.git/blob - 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
7 import hmac
, time
, config
, qwebirc
.config_options
as config_options
8 from config
import HMACTEMPORAL
10 if hasattr(config
, "WEBIRC_MODE") and config
.WEBIRC_MODE
== "hmac":
11 HMACKEY
= hmac
.HMAC(key
=config
.HMACKEY
)
15 h
.update("%d %s" % (int(time
.time() / HMACTEMPORAL
), " ".join(args
)))
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)
21 codecs
.register_error("mixed-iso-8859-1", utf8_iso8859_1
)
25 return x
.decode("utf-8", "mixed-iso-8859-1")
26 except UnicodeDecodeError:
27 return x
.decode("iso-8859-1", "ignore")
29 class QWebIRCClient(basic
.LineReceiver
):
31 def __init__(self
, *args
, **kwargs
):
32 self
.__nickname
= "(unregistered)"
34 def dataReceived(self
, data
):
35 basic
.LineReceiver
.dataReceived(self
, data
.replace("\r", ""))
37 def lineReceived(self
, line
):
38 line
= irc_decode(irc
.lowDequote(line
))
41 prefix
, command
, params
= irc
.parsemsg(line
)
42 self
.handleCommand(command
, prefix
, params
)
43 except irc
.IRCBadMessage
:
49 self
.__nickname
= params
[0]
51 if self
.__perform
is not None:
52 for x
in self
.__perform
:
55 elif command
== "NICK":
56 nick
= prefix
.split("!", 1)[0]
57 if nick
== self
.__nickname
:
58 self
.__nickname
= params
[0]
60 def handleCommand(self
, command
, prefix
, params
):
61 self("c", command
, prefix
, params
)
63 def __call__(self
, *args
):
64 self
.factory
.publisher
.event(args
)
66 def write(self
, data
):
67 self
.transport
.write("%s\r\n" % irc
.lowQuote(data
.encode("utf-8")))
69 def connectionMade(self
):
70 basic
.LineReceiver
.connectionMade(self
)
73 f
= self
.factory
.ircinit
74 nick
, ident
, ip
, realname
, hostname
, pass_
= f
["nick"], f
["ident"], f
["ip"], f
["realname"], f
["hostname"], f
.get("password")
75 self
.__nickname
= nick
76 self
.__perform
= f
.get("perform")
78 if not hasattr(config
, "WEBIRC_MODE"):
79 self
.write("USER %s bleh bleh %s :%s" % (ident
, ip
, realname
))
80 elif config
.WEBIRC_MODE
== "hmac":
81 hmac
= hmacfn(ident
, ip
)
82 self
.write("USER %s bleh bleh %s %s :%s" % (ident
, ip
, hmac
, realname
))
83 elif config
.WEBIRC_MODE
== "webirc":
84 self
.write("WEBIRC %s qwebirc %s %s" % (config
.WEBIRC_PASSWORD
, hostname
, ip
))
85 self
.write("USER %s bleh %s :%s" % (ident
, ip
, realname
))
86 elif config
.WEBIRC_MODE
== "cgiirc":
87 self
.write("PASS %s_%s_%s" % (config
.CGIIRC_STRING
, ip
, hostname
))
88 self
.write("USER %s bleh %s :%s" % (ident
, ip
, realname
))
89 elif config
.WEBIRC_MODE
== config_options
.WEBIRC_REALNAME
or config
.WEBIRC_MODE
is None: # last bit is legacy
93 dispip
= "%s/%s" % (hostname
, ip
)
95 self
.write("USER %s bleh bleh :%s - %s" % (ident
, dispip
, realname
))
98 self
.write("PASS :%s" % pass_
)
99 self
.write("NICK %s" % nick
)
101 self
.factory
.client
= self
105 return "<QWebIRCClient: %s!%s@%s>" % (self
.__nickname
, self
.factory
.ircinit
["ident"], self
.factory
.ircinit
["ip"])
107 def connectionLost(self
, reason
):
109 self
.disconnect("Connection to IRC server lost: %s" % self
.lastError
)
111 self
.disconnect("Connection to IRC server lost.")
112 self
.factory
.client
= None
113 basic
.LineReceiver
.connectionLost(self
, reason
)
115 def error(self
, message
):
116 self
.lastError
= message
117 self
.write("QUIT :qwebirc exception: %s" % message
)
118 self
.transport
.loseConnection()
120 def disconnect(self
, reason
):
121 self("disconnect", reason
)
122 self
.factory
.publisher
.disconnect()
124 class QWebIRCFactory(protocol
.ClientFactory
):
125 protocol
= QWebIRCClient
126 def __init__(self
, publisher
, **kwargs
):
128 self
.publisher
= publisher
129 self
.ircinit
= kwargs
131 def write(self
, data
):
132 self
.client
.write(data
)
134 def error(self
, reason
):
135 self
.client
.error(reason
)
137 def clientConnectionFailed(self
, connector
, reason
):
138 protocol
.ClientFactory
.clientConnectionFailed(self
, connector
, reason
)
139 self
.publisher
.event(["disconnect", "Connection to IRC server failed."])
140 self
.publisher
.disconnect()
142 def createIRC(*args
, **kwargs
):
143 f
= QWebIRCFactory(*args
, **kwargs
)
144 reactor
.connectTCP(config
.IRCSERVER
, config
.IRCPORT
, f
)
147 if __name__
== "__main__":
148 e
= createIRC(lambda x
: 2, nick
="slug__moo", ident
="mooslug", ip
="1.2.3.6", realname
="mooooo", hostname
="1.2.3.4")