]>
jfr.im git - irc/quakenet/qwebirc.git/blob - qwebirc/ircclient.py
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
7 import hmac
, time
, config
8 from config
import HMACTEMPORAL
10 if 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
:
44 self
.badMessage(line
, *sys
.exc_info())
47 self
.__nickname
= params
[0]
49 if self
.__perform
is not None:
50 for x
in self
.__perform
:
53 elif command
== "NICK":
54 nick
= prefix
.split("!", 1)[0]
55 if nick
== self
.__nickname
:
56 self
.__nickname
= params
[0]
58 def badMessage(self
, args
):
59 self("badmessage", args
)
61 def handleCommand(self
, command
, prefix
, params
):
62 self("c", command
, prefix
, params
)
64 def __call__(self
, *args
):
65 self
.factory
.publisher
.event(args
)
67 def write(self
, data
):
68 self
.transport
.write("%s\r\n" % irc
.lowQuote(data
.encode("utf-8")))
70 def connectionMade(self
):
71 basic
.LineReceiver
.connectionMade(self
)
74 f
= self
.factory
.ircinit
75 nick
, ident
, ip
, realname
, hostname
= f
["nick"], f
["ident"], f
["ip"], f
["realname"], f
["hostname"]
76 self
.__nickname
= nick
77 self
.__perform
= f
.get("perform")
79 if config
.WEBIRC_MODE
== "hmac":
80 hmac
= hmacfn(ident
, ip
)
81 self
.write("USER %s bleh bleh %s %s :%s" % (ident
, ip
, hmac
, realname
))
82 elif config
.WEBIRC_MODE
== "webirc":
83 self
.write("WEBIRC %s qwebirc %s %s" % (config
.WEBIRC_PASSWORD
, hostname
, ip
))
84 self
.write("USER %s bleh %s :%s" % (ident
, ip
, realname
))
85 elif config
.WEBIRC_MODE
== "cgiirc":
86 self
.write("PASS %s_%s_%s" % (config
.CGIIRC_STRING
, ip
, hostname
))
87 self
.write("USER %s bleh %s :%s" % (ident
, ip
, realname
))
92 dispip
= "%s/%s" % (hostname
, ip
)
94 self
.write("USER %s bleh bleh :%s - %s" % (ident
, dispip
, realname
))
96 self
.write("NICK %s" % nick
)
98 self
.factory
.client
= self
102 return "<QWebIRCClient: %s!%s@%s>" % (self
.__nickname
, self
.factory
.ircinit
["ident"], self
.factory
.ircinit
["ip"])
104 def connectionLost(self
, reason
):
106 self
.disconnect("Connection to IRC server lost: %s" % self
.lastError
)
108 self
.disconnect("Connection to IRC server lost.")
109 self
.factory
.client
= None
110 basic
.LineReceiver
.connectionLost(self
, reason
)
112 def error(self
, message
):
113 self
.lastError
= message
114 self
.write("QUIT :qwebirc exception: %s" % message
)
115 self
.transport
.loseConnection()
117 def disconnect(self
, reason
):
118 self("disconnect", reason
)
119 self
.factory
.publisher
.disconnect()
121 class QWebIRCFactory(protocol
.ClientFactory
):
122 protocol
= QWebIRCClient
123 def __init__(self
, publisher
, **kwargs
):
125 self
.publisher
= publisher
126 self
.ircinit
= kwargs
128 def write(self
, data
):
129 self
.client
.write(data
)
131 def error(self
, reason
):
132 self
.client
.error(reason
)
134 def clientConnectionFailed(self
, connector
, reason
):
135 protocol
.ClientFactory
.clientConnectionFailed(self
, connector
, reason
)
136 self
.publisher
.event(["disconnect", "Connection to IRC server failed."])
137 self
.publisher
.disconnect()
139 def createIRC(*args
, **kwargs
):
140 f
= QWebIRCFactory(*args
, **kwargs
)
141 reactor
.connectTCP(config
.IRCSERVER
, config
.IRCPORT
, f
)
144 if __name__
== "__main__":
145 e
= createIRC(lambda x
: 2, nick
="slug__moo", ident
="mooslug", ip
="1.2.3.6", realname
="mooooo", hostname
="1.2.3.4")