]>
jfr.im git - irc/quakenet/qwebirc.git/blob - qwebirc/ajaxengine.py
1 from twisted
.web
import resource
, server
, static
2 from twisted
.names
import client
3 from twisted
.internet
import reactor
4 import simplejson
, md5
, sys
, os
, ircclient
, time
, config
9 return md5
.md5(os
.urandom(16)).hexdigest()
12 def decorator(*args
, **kwargs
):
13 x
= fn(*args
, **kwargs
)
14 if isinstance(x
, list):
15 return simplejson
.dumps(x
)
20 def __init__(self
, id):
22 self
.subscriptions
= []
27 def subscribe(self
, channel
):
28 self
.subscriptions
.append(channel
)
31 def flush(self
, scheduled
=False):
35 if not self
.buffer or not self
.subscriptions
:
42 self
.schedule
= reactor
.callLater(self
.throttle
- t
, self
.flush
, True)
45 # process the rest of the packet
48 self
.schedule
= reactor
.callLater(0, self
.flush
, True)
51 self
.throttle
= t
+ config
.UPDATE_FREQ
53 encdata
= simplejson
.dumps(self
.buffer)
57 for x
in self
.subscriptions
:
61 self
.subscriptions
= newsubs
63 def event(self
, data
):
64 self
.buffer.append(data
)
68 self
.client
.write(data
)
71 def __init__(self
, request
):
72 self
.request
= request
74 class SingleUseChannel(Channel
):
75 def write(self
, data
):
76 self
.request
.write(data
)
80 class MultipleUseChannel(Channel
):
81 def write(self
, data
):
82 self
.request
.write(data
)
85 class AJAXEngine(resource
.Resource
):
88 def __init__(self
, prefix
):
92 def render_GET(self
, request
):
93 path
= request
.path
[len(self
.prefix
):]
95 ip
= request
.transport
.getPeer()
98 nick
, ident
= request
.args
.get("nick"), "webchat"
100 return [False, "Nickname not supplied"]
104 id = get_session_id()
106 session
= IRCSession(id)
108 client
= ircclient
.createIRC(session
, nick
=nick
, ident
=ident
, ip
=ip
, realname
=nick
)
109 session
.client
= client
111 Sessions
[id] = session
115 if path
.startswith("/s/"):
117 session
= Sessions
.get(sessionid
)
120 return [False, "Bad session ID"]
122 session
.subscribe(SingleUseChannel(request
))
123 return server
.NOT_DONE_YET
124 if path
.startswith("/p/"):
125 command
= request
.args
.get("c")
127 return [False, "No command specified"]
132 session
= Sessions
.get(sessionid
)
134 return [False, "Bad session ID"]
137 decoded
= command
.decode("utf-8")
138 except UnicodeDecodeError:
139 decoded
= command
.decode("iso-8859-1", "ignore")
140 session
.push(decoded
)
143 return [False, "404"]