from twisted.web import resource, server, static
from twisted.names import client
-from twisted.internet import reactor
+from twisted.internet import reactor, error
from authgateengine import login_optional, getSessionData
import simplejson, md5, sys, os, time, config, weakref, traceback
import qwebirc.ircclient as ircclient
self.closed = False
self.cleanupschedule = None
- def subscribe(self, channel):
+ def subscribe(self, channel, notifier):
+ timeout_entry = reactor.callLater(config.HTTP_AJAX_REQUEST_TIMEOUT, self.timeout, channel)
+ def cancel_timeout(result):
+ if channel in self.subscriptions:
+ self.subscriptions.remove(channel)
+ try:
+ timeout_entry.cancel()
+ except error.AlreadyCalled:
+ pass
+ notifier.addCallbacks(cancel_timeout, cancel_timeout)
+
if len(self.subscriptions) >= config.MAXSUBSCRIPTIONS:
self.subscriptions.pop(0).close()
self.subscriptions.append(channel)
self.flush()
+ def timeout(self, channel):
+ if self.schedule:
+ return
+
+ channel.write(simplejson.dumps([]))
+ if channel in self.subscriptions:
+ self.subscriptions.remove(channel)
+
def flush(self, scheduled=False):
if scheduled:
self.schedule = None
handler = self.COMMANDS.get(path[1:])
if handler is not None:
return handler(self, request)
- raise AJAXException("404")
+ raise AJAXException, "404"
# def render_GET(self, request):
# return self.render_POST(request)
_, ip, port = request.transport.getPeer()
- nick, ident, realname = request.args.get("nick"), "webchat", config.REALNAME
-
+ nick = request.args.get("nick")
if not nick:
- raise AJAXException("Nickname not supplied")
-
- nick = nick[0]
+ raise AJAXException, "Nickname not supplied"
+ nick = ircclient.irc_decode(nick[0])
+ ident, realname = "webchat", config.REALNAME
+
for i in xrange(10):
id = get_session_id()
if not Sessions.get(id):
return id
def getSession(self, request):
+ bad_session_message = "Invalid session, this most likely means the server has restarted; close this dialog and then try refreshing the page."
+
sessionid = request.args.get("s")
if sessionid is None:
- raise AJAXException("Bad session ID")
+ raise AJAXException, bad_session_message
session = Sessions.get(sessionid[0])
if not session:
- raise AJAXException("Bad session ID")
+ raise AJAXException, bad_session_message
return session
def subscribe(self, request):
- self.getSession(request).subscribe(SingleUseChannel(request))
+ request.channel.cancelTimeout()
+ self.getSession(request).subscribe(SingleUseChannel(request), request.notifyFinish())
return NOT_DONE_YET
def push(self, request):
command = request.args.get("c")
if command is None:
- raise AJAXException("No command specified")
+ raise AJAXException, "No command specified"
self.__total_hit()
- command = command[0]
+ decoded = ircclient.irc_decode(command[0])
session = self.getSession(request)
- try:
- decoded = command.decode("utf-8")
- except UnicodeDecodeError:
- decoded = command.decode("iso-8859-1", "ignore")
-
if len(decoded) > config.MAXLINELEN:
session.disconnect()
- raise AJAXException("Line too long")
+ raise AJAXException, "Line too long"
try:
session.push(decoded)
except AttributeError: # occurs when we haven't noticed an error
session.disconnect()
- raise AJAXException("Connection closed by server.")
+ raise AJAXException, "Connection closed by server."
except Exception, e: # catch all
session.disconnect()
traceback.print_exc(file=sys.stderr)
- raise AJAXException("Unknown error.")
+ raise AJAXException, "Unknown error."
return True