]> jfr.im git - irc/quakenet/qwebirc.git/blobdiff - qwebirc/engines/ajaxengine.py
tidy up autobahn support -- now requires 0.17.2
[irc/quakenet/qwebirc.git] / qwebirc / engines / ajaxengine.py
index 70d8d191e3542f860aa8e2f64eac9791f930df08..33bc67bf8585601125ff3424e037535d9339de06 100644 (file)
@@ -9,16 +9,26 @@ from qwebirc.util import HitCounter
 import qwebirc.dns as qdns
 import qwebirc.util.qjson as json
 import urlparse
+import qwebirc.util.autobahn_check as autobahn_check
 
 TRANSPORTS = ["longpoll"]
 
-try:
-  import autobahn.websocket
-  import autobahn.resource
+has_websocket = False
+autobahn_status = autobahn_check.check()
+if autobahn_status == True:
+  import autobahn
+  import autobahn.twisted.websocket
+  import autobahn.twisted.resource
   has_websocket = True
   TRANSPORTS.append("websocket")
-except ImportError:
-  has_websocket = False
+elif autobahn_status == False:
+  # they've been warned already
+  pass
+else:
+  print >>sys.stderr, "WARNING:"
+  print >>sys.stderr, "  %s" % autobahn_status
+  print >>sys.stderr, "  as a result websocket support is disabled."
+  print >>sys.stderr, "  upgrade your version of autobahn from http://autobahn.ws/python/getstarted/"
 
 BAD_SESSION_MESSAGE = "Invalid session, this most likely means the server has restarted; close this dialog and then try refreshing the page."
 MAX_SEQNO = 9223372036854775807  # 2**63 - 1... yeah it doesn't wrap
@@ -65,7 +75,7 @@ class IRCSession:
     if len(self.subscriptions) >= config.MAXSUBSCRIPTIONS:
       self.subscriptions.pop(0).close()
 
-    if seqNo < self.subSeqNo:
+    if seqNo is not None and seqNo < self.subSeqNo:
       if self.old_buffer is None or seqNo != self.old_buffer[0]:
         channel.write(json.dumps([False, "Unable to reconnect -- sequence number too old."]), seqNo + 1)
         return
@@ -355,10 +365,11 @@ if has_websocket:
     def close(self):
       self.channel.close()
 
-  class WebSocketEngineProtocol(autobahn.websocket.WebSocketServerProtocol):
+  class WebSocketEngineProtocol(autobahn.twisted.websocket.WebSocketServerProtocol):
     AWAITING_AUTH, AUTHED = 0, 1
 
     def __init__(self, *args, **kwargs):
+      super(WebSocketEngineProtocol, self).__init__(*args, **kwargs)
       self.__state = self.AWAITING_AUTH
       self.__session = None
       self.__channel = None
@@ -373,14 +384,14 @@ if has_websocket:
         self.__session.unsubscribe(self.__channel)
         self.__session = None
 
-    def onMessage(self, msg, binary):
+    def onMessage(self, msg, isBinary):
       # we don't bother checking the Origin header, as if you can auth then you've been able to pass the browser's
       # normal origin handling (POSTed the new connection request and managed to get the session id)
       state = self.__state
       message_type, message = msg[:1], msg[1:]
       if state == self.AWAITING_AUTH:
         if message_type == "s":  # subscribe
-          tokens = message.split(",", 2)
+          tokens = message.split(",", 1)
           if len(tokens) != 2:
             self.close("Bad tokens")
             return
@@ -407,7 +418,7 @@ if has_websocket:
           return
       elif state == self.AUTHED:
         if message_type == "p":  # push
-          tokens = message.split(",", 2)
+          tokens = message.split(",", 1)
           if len(tokens) != 2:
             self.close("Bad tokens")
             return
@@ -435,7 +446,7 @@ if has_websocket:
     def close(self, reason=None):
       self.__cancelTimeout()
       if reason:
-        self.sendClose(4999, reason)
+        self.sendClose(4999, unicode(reason))
       else:
         self.sendClose(4998)
 
@@ -446,24 +457,16 @@ if has_websocket:
     def send(self, message_type, message):
       self.sendMessage(message_type + message)
 
-  class WebSocketResource(autobahn.resource.WebSocketResource):
+  class WebSocketResource(autobahn.twisted.resource.WebSocketResource):
     def render(self, request):
       request.channel.setTimeout(None)
-      return autobahn.resource.WebSocketResource.render(self, request)
+      return autobahn.twisted.resource.WebSocketResource.render(self, request)
 
   def WebSocketEngine(path=None):
-    parsed = urlparse.urlparse(config.BASE_URL)
-    port = parsed.port
-    if port is None:
-      if parsed.scheme == "http":
-        port = 80
-      elif parsed.scheme == "https":
-        port = 443
-      else:
-        raise Exception("Unable to determine port from BASE_URL: " + config.BASE_URL)
-
-    factory = autobahn.websocket.WebSocketServerFactory("ws://localhost:%d" % port)
+    factory = autobahn.twisted.websocket.WebSocketServerFactory("ws://localhost")
+    factory.externalPort = None
     factory.protocol = WebSocketEngineProtocol
     factory.setProtocolOptions(maxMessagePayloadSize=512, maxFramePayloadSize=512, tcpNoDelay=False)
     resource = WebSocketResource(factory)
     return resource
+