]> code.delx.au - pymsnt/blobdiff - src/main.py
* PyMSNt now supports Twisted 2.5
[pymsnt] / src / main.py
index 6be4c742b811403765ba08a646d28df013c6f307..b31aa8e895e129f12c3ba2011e38eb3bed2f9a23 100644 (file)
@@ -6,29 +6,32 @@ reload(sys)
 sys.setdefaultencoding("utf-8")
 sys.stdout = codecs.lookup('utf-8')[-1](sys.stdout)
 
+
 # Find the best reactor
 selectWarning = "Unable to install any good reactors (kqueue, epoll, poll).\nWe fell back to using select. You may have scalability problems.\nThis reactor will not support more than 1024 connections at a time."
 try:
        from twisted.internet import epollreactor as bestreactor
 except:
+       #try:
+               #from twisted.internet import kqreactor as bestreactor
+       #except:
        try:
-               from twisted.internet import kqreactor as bestreactor
+               from twisted.internet import pollreactor as bestreactor
        except:
                try:
-                       from twisted.internet import pollreactor as bestreactor
+                       from twisted.internet import selectreactor as bestreactor
+                       print selectWarning
                except:
                        try:
-                               from twisted.internet import selectreactor as bestreactor
+                               from twisted.internet import default as bestreactor
                                print selectWarning
                        except:
-                               try:
-                                       from twisted.internet import default as bestreactor
-                                       print selectWarning
-                               except:
-                                       print "Unable to find a reactor.\nExiting..."
-                                       sys.exit(1)
+                               print "Unable to find a reactor. Please make sure you have Twisted properly installed.\nExiting..."
+                               sys.exit(1)
 bestreactor.install()
 
+import twistfix
+twistfix.main()
 
 
 # Must load config before everything else
@@ -92,7 +95,10 @@ if config.reactor:
 
 from twisted.internet import reactor, task
 from twisted.internet.defer import Deferred
-from tlib.xmlw import Element, jid, component
+from twisted.words.xish.domish import Element
+from twisted.words.protocols.jabber import component
+from twisted.words.protocols.jabber.jid import internJID
+
 from debug import LogEvent, INFO, WARN, ERROR
 
 import debug
@@ -116,9 +122,10 @@ class PyTransport(component.Service):
        def __init__(self):
                LogEvent(INFO)
                try:
-                       LogEvent(INFO, msg="SVN r" + svninfo.getSVNVersion())
+                       LogEvent(INFO, msg="SVN r" + str(svninfo.getSVNVersion()))
                except:
                        pass
+               LogEvent(INFO, msg="Reactor: " + str(reactor))
 
                # Discovery, as well as some builtin features
                self.discovery = disco.ServerDiscovery(self)
@@ -240,7 +247,7 @@ class PyTransport(component.Service):
        def onMessage(self, el):
                fro = el.getAttribute("from")
                try:
-                       froj = jid.intern(fro)
+                       froj = internJID(fro)
                except Exception, e:
                        LogEvent(WARN, "", "Failed stringprep.")
                        return
@@ -260,13 +267,14 @@ class PyTransport(component.Service):
                                        body = child.__str__()
                        LogEvent(INFO, "", "Sending error response to a message outside of session.")
                        jabw.sendErrorMessage(self, fro, to, "auth", "not-authorized", lang.get(ulang).notLoggedIn, body)
+                       jabw.sendPresence(self, fro, to, ptype="unavailable")
        
        def onPresence(self, el):
                fro = el.getAttribute("from")
                to = el.getAttribute("to")
                try:
-                       froj = jid.intern(fro)
-                       toj = jid.intern(to)
+                       froj = internJID(fro)
+                       toj = internJID(to)
                except Exception, e:
                        LogEvent(WARN, "", "Failed stringprep.")
                        return
@@ -298,25 +306,24 @@ class PyTransport(component.Service):
                                
                                elif el.getAttribute("type") != "error":
                                        LogEvent(INFO, "", "Sending unavailable presence to non-logged in user.")
-                                       pres = Element((None, "presence"))
-                                       pres.attributes["from"] = to
-                                       pres.attributes["to"] = fro
-                                       pres.attributes["type"] = "unavailable"
-                                       self.send(pres)
+                                       jabw.sendPresence(self, fro, to, ptype="unavailable")
                                        return
                        
                        elif ptype and (ptype.startswith("subscribe") or ptype.startswith("unsubscribe")):
                                # They haven't logged in, and are trying to change subscription to a user
+                               # No, lets not log them in. Lets send an error :)
+                               jabw.sendPresence(self, fro, to, ptype="error")
+                               
                                # Lets log them in and then do it
-                               LogEvent(INFO, "", "Attempting to create a session to do subscription stuff.")
-                               s = session.makeSession(self, froj.userhost(), ulang)
-                               if s:
-                                       self.sessions[froj.userhost()] = s
-                                       LogEvent(INFO, "", "New session created.")
-                                       # Tell the session there's a new resource
-                                       s.handleResourcePresence(froj.userhost(), froj.resource, toj.userhost(), toj.resource, 0, None, None, None)
-                                       # Send this subscription
-                                       s.onPresence(el)
+                               #LogEvent(INFO, "", "Attempting to create a session to do subscription stuff.")
+                               #s = session.makeSession(self, froj.userhost(), ulang)
+                               #if s:
+                               #       self.sessions[froj.userhost()] = s
+                               #       LogEvent(INFO, "", "New session created.")
+                               #       # Tell the session there's a new resource
+                               #       s.handleResourcePresence(froj.userhost(), froj.resource, toj.userhost(), toj.resource, 0, None, None, None)
+                               #       # Send this subscription
+                               #       s.onPresence(el)
 
 
 class App:
@@ -383,7 +390,7 @@ if os.name == "posix":
        # Set SIGHUP to reload the config file & close & open debug file
        signal.signal(signal.SIGHUP, SIGHUPstuff)
        # Load some scripts for PID and daemonising
-       from twisted.scripts import twistd
+       from twisted.scripts import _twistd_unix as twistd
 
 
 def main():