From: jamesbunton Date: Thu, 26 Jan 2006 01:50:09 +0000 (+0000) Subject: Moved presence probe to session.py X-Git-Url: https://code.delx.au/pymsnt/commitdiff_plain/e936caef2f7d4e989e1341572ecfb70a60686c4b Moved presence probe to session.py Fixed bug with personal messages. Moved default avatar images to a data dir. Switch to using twisted.scripts.twistd code for daemonising and pid file Cleaned up config-example.xml Fixed GETALLAVATARS option so it works again. git-svn-id: http://delx.cjb.net/svn/pymsnt/trunk@104 55fbd22a-6204-0410-b2f0-b6c764c7e90a committer: jamesbunton --- diff --git a/config-example.xml b/config-example.xml index c25dcc3..e809269 100644 --- a/config-example.xml +++ b/config-example.xml @@ -17,22 +17,24 @@ Do not include the jid of the transport --> PyMSNt.pid + + 127.0.0.1 - -http://host.com 5347 secret - - + + en + +http://host.com @@ -46,8 +48,6 @@ Do not include the jid of the transport --> - - @@ -74,6 +74,6 @@ Do not include the jid of the transport --> - + diff --git a/src/legacy/defaultJabberAvatar.png b/data/defaultJabberAvatar.png similarity index 100% rename from src/legacy/defaultJabberAvatar.png rename to data/defaultJabberAvatar.png diff --git a/src/legacy/defaultAvatar.png b/data/defaultMSNAvatar.png similarity index 100% rename from src/legacy/defaultAvatar.png rename to data/defaultMSNAvatar.png diff --git a/src/jabw.py b/src/jabw.py index f063cef..ec2904f 100644 --- a/src/jabw.py +++ b/src/jabw.py @@ -253,9 +253,6 @@ class JabberConnection: if ptype and (ptype.startswith("subscribe") or ptype.startswith("unsubscribe")): LogEvent(INFO, self.jabberID, "Parsed subscription presence packet") self.subscriptionReceived(toj.userhost(), ptype) - elif ptype == "probe": - LogEvent(INFO, self.jabberID, "Parsed presence probe") - self.contactList.getContact(toj.userhost()).sendPresence(fro) else: status = None show = None diff --git a/src/legacy/glue.py b/src/legacy/glue.py index 67b788f..4c06e0a 100644 --- a/src/legacy/glue.py +++ b/src/legacy/glue.py @@ -1,13 +1,13 @@ # Copyright 2004-2005 James Bunton # Licensed for distribution under the GPL version 2, check COPYING for details +import os.path import utils from twisted.internet import task from tlib.xmlw import Element from tlib import msn from debug import LogEvent, INFO, WARN, ERROR import disco -import sha import groupchat import ft import avatar @@ -25,18 +25,18 @@ id = "msn" # The transport identifier # Load the default avatars -f = open("src/legacy/defaultJabberAvatar.png") +f = open(os.path.join("data", "defaultJabberAvatar.png")) defaultJabberAvatarData = f.read() f.close() -f = open("src/legacy/defaultAvatar.png") +f = open(os.path.join("data", "defaultMSNAvatar.png")) defaultAvatarData = f.read() f.close() defaultAvatar = avatar.AvatarCache().setAvatar(defaultAvatarData) def reloadConfig(): - msn.GETALLAVATARS = config.getAllAvatars + msn.MSNConnection.GETALLAVATARS = config.getAllAvatars def isGroupJID(jid): """ Returns True if the JID passed is a valid groupchat JID (for MSN, does not contain '%') """ diff --git a/src/main.py b/src/main.py index 598d245..f4f80f9 100644 --- a/src/main.py +++ b/src/main.py @@ -11,7 +11,7 @@ import config import xmlconfig configFile = "config.xml" configOptions = {} -opts, args = getopt.getopt(sys.argv[1:], "bc:o:dDgtl:h", ["background", "config=", "option=", "debug", "Debug", "garbage", "traceback", "log=", "help"]) +opts, args = getopt.getopt(sys.argv[1:], "bc:o:dDgtlp:h", ["background", "config=", "option=", "debug", "Debug", "garbage", "traceback", "log=", "pid=", "help"]) for o, v in opts: if o in ("-c", "--config"): configFile = v @@ -28,6 +28,8 @@ for o, v in opts: config.debugLevel = "1" elif o in ("-l", "--log"): config.debugFile = v + elif o in ("-p", "--pid"): + config.pid = v elif o in ("-o", "--option"): var, setting = v.split("=", 2) configOptions[var] = setting @@ -41,6 +43,7 @@ for o, v in opts: print " -g print garbage collection output" print " -t print debugging only on traceback" print " -l write debugging output to file" + print " -p write process ID to file" print " -o = set config var to setting" sys.exit(0) @@ -278,15 +281,17 @@ class PyTransport(component.Service): class App: def __init__(self): # Check for any other instances + if config.pid and os.name != "posix": + config.pid = "" if config.pid: - self.checkPID() + twistd.checkPID(config.pid) # Do any auto-update stuff housekeep.init() # Daemonise the process and write the PID file - if config.background: - self.daemonise() + if config.background and os.name == "posix": + twistd.daemonize() if config.pid: self.writePID() @@ -304,21 +309,6 @@ class App: self.c.startService() reactor.addSystemEventTrigger('before', 'shutdown', self.shuttingDown) - def checkPID(self): - # Check that we're not already running - if os.path.isfile(config.pid): - if os.name == "posix": - pf = open(config.pid) - pid = int(str(pf.readline().strip())) - pf.close() - try: - os.kill(pid, signal.SIGHUP) - self.alreadyRunning() - except OSError: - pass - else: - self.alreadyRunning() - def writePID(self): # Create a PID file pid = str(os.getpid()) @@ -330,21 +320,12 @@ class App: sys.__stdout__.write("There is already a transport instance running with this configuration.\nExiting...\n\n") sys.exit(1) - def daemonise(self): - try: - pid = os.fork() - if pid > 0: - sys.exit(0) - except OSError, e: - sys.stderr.write("Daemonise failed: (%d) %s\n" % (e.errno, e.strerror)) - sys.exit(1) - def shuttingDown(self): self.transportSvc.removeMe() # Keep the transport running for another 3 seconds def cb(ignored=None): if config.pid: - os.remove(config.pid) + twistd.removePID(config.pid) d = Deferred() d.addCallback(cb) reactor.callLater(3.0, d.callback, None) @@ -355,6 +336,8 @@ class App: def SIGHUPstuff(*args): global configFile, configOptions xmlconfig.reloadConfig(configFile, configOptions) + if config.pid and os.name != "posix": + config.pid = "" debug.reloadConfig() legacy.reloadConfig() @@ -362,6 +345,8 @@ if os.name == "posix": import signal # 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 def main(): diff --git a/src/session.py b/src/session.py index 22c00c0..57e7ff4 100644 --- a/src/session.py +++ b/src/session.py @@ -264,6 +264,9 @@ class Session(jabw.JabberConnection): groupchat = legacy.LegacyGroupchat(self, resource, gcID) # Creates an empty groupchat groupchat.userJoined(tor) + elif ptype == "probe": + LogEvent(INFO, self.jabberID, "Responding to presence probe") + self.contactList.getContact(toj.userhost()).sendPresence(fro) else: # Not for groupchat self.handleResourcePresence(source, resource, to, tor, priority, ptype, show, status) diff --git a/src/tlib/msn/__init__.py b/src/tlib/msn/__init__.py index f4870c5..edc622a 100644 --- a/src/tlib/msn/__init__.py +++ b/src/tlib/msn/__init__.py @@ -1,4 +1,4 @@ -from msnw import MSNConnection, MultiSwitchboardSession, GETALLAVATARS +from msnw import MSNConnection, MultiSwitchboardSession from msn import FORWARD_LIST, ALLOW_LIST, BLOCK_LIST, REVERSE_LIST, PENDING_LIST from msn import STATUS_ONLINE, STATUS_OFFLINE, STATUS_HIDDEN, STATUS_IDLE, STATUS_AWAY, STATUS_BUSY, STATUS_BRB, STATUS_PHONE, STATUS_LUNCH from msn import MSNContact, MSNContactList diff --git a/src/tlib/msn/msn.py b/src/tlib/msn/msn.py index cc665ad..0e82e6f 100644 --- a/src/tlib/msn/msn.py +++ b/src/tlib/msn/msn.py @@ -1045,16 +1045,17 @@ class NotificationClient(MSNEventBase): self.gotMSNAlert(bodytext, actionurl, subscrurl) def _gotUBX(self, message): + msnContact = self.factory.contacts.getContact(message.userHandle) + if not msnContact: return lm = message.message.lower() p1 = lm.find("") + 5 p2 = lm.find("") if p1 >= 0 and p2 >= 0: personal = unescapeFromXml(message.message[p1:p2]) - msnContact = self.factory.contacts.getContact(message.userHandle) - if not msnContact: return msnContact.personal = personal self.contactPersonalChanged(message.userHandle, personal) else: + msnContact.personal = '' self.contactPersonalChanged(message.userHandle, '') def checkMessage(self, message): @@ -1368,7 +1369,7 @@ class NotificationClient(MSNEventBase): self.currentMessage = MSNMessage(length=messageLen, userHandle=params[0], screenName="UBX", specialMessage=True) self.setRawMode() else: - self.contactPersonalChanged(params[0], '') + self._gotUBX(MSNMessage(userHandle=params[0])) def handle_UUX(self, params): checkParamLen(len(params), 2, 'UUX') diff --git a/src/tlib/msn/msnw.py b/src/tlib/msn/msnw.py index ace11f1..d0e132f 100644 --- a/src/tlib/msn/msnw.py +++ b/src/tlib/msn/msnw.py @@ -14,10 +14,6 @@ from debug import LogEvent, INFO, WARN, ERROR from tlib.msn import msn -MAXMESSAGESIZE = 1400 -SWITCHBOARDTIMEOUT = 30.0*60.0 -GETALLAVATARS = False - """ All interaction should be with the MSNConnection and MultiSwitchboardSession classes. @@ -26,6 +22,10 @@ You should not directly instantiate any objects of other classes. class MSNConnection: """ Manages all the Twisted factories, etc """ + MAXMESSAGESIZE = 1400 + SWITCHBOARDTIMEOUT = 30.0*60.0 + GETALLAVATARS = False + def __init__(self, username, password, ident): """ Connects to the MSN servers. @param username: the MSN passport to connect with. @@ -127,7 +127,7 @@ class MSNConnection: LogEvent(INFO, self.ident) if not self.notificationClient: return - if GETALLAVATARS: + if MSNConnection.GETALLAVATARS: self._ensureSwitchboardSession(userHandle) sb = self.switchboardSessions.get(userHandle) if sb: return sb.sendAvatarRequest() @@ -526,7 +526,7 @@ class SwitchboardSessionBase(msn.SwitchboardClient): if not noerror: self.failedMessage(text) - if len(text) < MAXMESSAGESIZE: + if len(text) < MSNConnection.MAXMESSAGESIZE: message = msn.MSNMessage(message=str(text.replace("\n", "\r\n").encode("utf-8"))) message.setHeader("Content-Type", "text/plain; charset=UTF-8") message.ack = msn.MSNMessage.MESSAGE_NACK @@ -536,12 +536,12 @@ class SwitchboardSessionBase(msn.SwitchboardClient): d.addCallback(failedMessage) else: - chunks = int(math.ceil(len(text) / float(MAXMESSAGESIZE))) + chunks = int(math.ceil(len(text) / float(MSNConnection.MAXMESSAGESIZE))) chunk = 0 guid = msn.random_guid() while chunk < chunks: - offset = chunk * MAXMESSAGESIZE - text = message[offset : offset + MAXMESSAGESIZE] + offset = chunk * MSNConnection.MAXMESSAGESIZE + text = message[offset : offset + MSNConnection.MAXMESSAGESIZE] message = msn.MSNMessage(message=str(text.replace("\n", "\r\n").encode("utf-8"))) message.ack = msn.MSNMessage.MESSAGE_NACK if chunk == 0: