def sendMessagesOnShutdown(self):
		if config.enableShutdownMessage:
			msg = lang.get('alert_shutdown')
			if config.customShutdownMessage: 
				msg = config.customShutdownMessage 
			for jid in self.sessions:
				LogEvent(INFO, msg='Msg on shutdown to %r' % jid)
				tmpjid = config.jid
				if self.sessions[jid].registeredmunge:
					tmpjid = tmpjid + '/registered'
				jabw.sendErrorMessage(self, jid, tmpjid, 'cancel', 'undefined-condition', '', msg)
示例#2
0
文件: main.py 项目: zfortier/yakalope
            froj = internJID(fro)
        except Exception, e:
            LogEvent(WARN, msg="Failed stringprep")
            return
        if self.sessions.has_key(froj.userhost()):
            self.sessions[froj.userhost()].onMessage(el)
        elif mtype != "error":
            ulang = utils.getLang(el)
            body = None
            for child in el.elements():
                if child.name == "body":
                    body = child.__str__()
            LogEvent(
                INFO,
                msg="Sending error response to a message outside of seession")
            jabw.sendErrorMessage(self, fro, to, "auth", "not-authorized",
                                  lang.get("notloggedin", ulang), body)

    def onPresence(self, el):
        fro = el.getAttribute("from")
        to = el.getAttribute("to")
        # Ignore any presence broadcasts about other JD2 components
        if to == None: return
        try:
            froj = internJID(fro)
            toj = internJID(to)
        except Exception, e:
            LogEvent(WARN, msg="Failed stringprep")
            return

        if self.sessions.has_key(froj.userhost()):
            self.sessions[froj.userhost()].onPresence(el)
		mtype = el.getAttribute("type")
		try:
			froj = internJID(fro)
		except Exception, e:
			LogEvent(WARN, msg="Failed stringprep")
			return
		if self.sessions.has_key(froj.userhost()):
			self.sessions[froj.userhost()].onMessage(el)
		elif mtype != "error":
			ulang = utils.getLang(el)
			body = None
			for child in el.elements():
				if child.name == "body":
					body = child.__str__()
					LogEvent(INFO, msg="Sending error response to a message outside of seession")
					jabw.sendErrorMessage(self, fro, to, "auth", "not-authorized", lang.get("notloggedin", ulang), body)
	
	def onPresence(self, el):
		fro = el.getAttribute("from")
		to = el.getAttribute("to")
		# Ignore any presence broadcasts about other JD2 components
		if to == None: return
		try:
			froj = internJID(fro)
			toj = internJID(to)
		except Exception, e:
			LogEvent(WARN, msg="Failed stringprep")
			return

		if self.sessions.has_key(froj.userhost()):
			self.sessions[froj.userhost()].onPresence(el)
示例#4
0
class PyTransport(component.Service):
    def __init__(self):
        LogEvent(INFO)
        LogEvent(INFO, msg="Reactor: " + str(reactor))

        # Discovery, as well as some builtin features
        self.discovery = disco.ServerDiscovery(self)
        self.discovery.addIdentity("gateway", legacy.id, config.discoName,
                                   config.jid)
        self.discovery.addIdentity("conference", "text",
                                   config.discoName + " Chatrooms", config.jid)
        self.discovery.addFeature(
            disco.XCONFERENCE, None, config.jid
        )  # So that clients know you can create groupchat rooms on the server
        self.discovery.addFeature(
            "jabber:iq:conference", None, config.jid
        )  # We don't actually support this, but Psi has a bug where it looks for this instead of the above
        self.discovery.addIdentity("client", "pc", "MSN Messenger", "USER")
        self.discovery.addIdentity("conference", "text", "MSN Groupchat",
                                   "ROOM")

        self.xdb = xdb.XDB(config.jid, legacy.mangle)
        self.avatarCache = avatar.AvatarCache()
        self.registermanager = register.RegisterManager(self)
        self.gatewayTranslator = misciq.GatewayTranslator(self)
        self.versionTeller = misciq.VersionTeller(self)
        self.pingService = misciq.PingService(self)
        self.adHocCommands = misciq.AdHocCommands(self)
        self.vCardFactory = misciq.VCardFactory(self)
        self.iqAvatarFactor = misciq.IqAvatarFactory(self)
        self.connectUsers = misciq.ConnectUsers(self)
        if config.ftJabberPort:
            self.ftSOCKS5Receive = ft.Proxy65(int(config.ftJabberPort))
            self.ftSOCKS5Send = misciq.Socks5FileTransfer(self)
        if config.ftOOBPort:
            self.ftOOBReceive = ft.FileTransferOOBReceive(int(
                config.ftOOBPort))
            self.ftOOBSend = misciq.FileTransferOOBSend(self)
        self.statistics = misciq.Statistics(self)
        self.startTime = int(time.time())

        self.xmlstream = None
        self.sessions = {}

        # Groupchat ID handling
        self.lastID = 0
        self.reservedIDs = []

        # Message IDs
        self.messageID = 0

        self.loopTask = task.LoopingCall(self.loopFunc)
        self.loopTask.start(60.0)

    def removeMe(self):
        LogEvent(INFO)
        for session in self.sessions.copy():
            self.sessions[session].removeMe()

    def makeMessageID(self):
        self.messageID += 1
        return str(self.messageID)

    def makeID(self):
        newID = "r" + str(self.lastID)
        self.lastID += 1
        if self.reservedIDs.count(newID) > 0:
            # Ack, it's already used.. Try again
            return self.makeID()
        else:
            return newID

    def reserveID(self, ID):
        self.reservedIDs.append(ID)

    def loopFunc(self):
        numsessions = len(self.sessions)

        #if config.debugOn and numsessions > 0:
        #	print "Sessions:"
        #	for key in self.sessions:
        #		print "\t" + self.sessions[key].jabberID

        self.statistics.stats["Uptime"] = int(time.time()) - self.startTime
        self.statistics.stats["OnlineUsers"] = numsessions
        legacy.updateStats(self.statistics)
        if numsessions > 0:
            oldDict = self.sessions.copy()
            self.sessions = {}
            for key in oldDict:
                s = oldDict[key]
                if not s.alive:
                    LogEvent(WARN, "", "Ghost session found.")
                    # Don't add it to the new dictionary. Effectively removing it
                else:
                    self.sessions[key] = s

    def componentConnected(self, xmlstream):
        LogEvent(INFO)
        self.xmlstream = xmlstream
        self.xmlstream.addObserver("/iq", self.discovery.onIq)
        self.xmlstream.addObserver("/presence", self.onPresence)
        self.xmlstream.addObserver("/message", self.onMessage)
        self.xmlstream.addObserver("/route", self.onRouteMessage)
        if config.useXCP:
            pres = Element((None, "presence"))
            pres.attributes["to"] = "presence@-internal"
            pres.attributes["from"] = config.compjid
            x = pres.addElement("x")
            x.attributes[
                "xmlns"] = "http://www.jabber.com/schemas/component-presence.xsd"
            x.attributes["xmlns:config"] = "http://www.jabber.com/config"
            x.attributes["config:version"] = "1"
            x.attributes["protocol-version"] = "1.0"
            x.attributes["config-ns"] = legacy.url + "/component"
            self.send(pres)

    def componentDisconnected(self):
        LogEvent(INFO)
        self.xmlstream = None

    def onRouteMessage(self, el):
        for child in el.elements():
            if child.name == "message":
                self.onMessage(child)
            elif child.name == "presence":
                # Ignore any presence broadcasts about other XCP components
                if child.getAttribute("to") and child.getAttribute("to").find(
                        "@-internal") > 0:
                    return
                self.onPresence(child)
            elif child.name == "iq":
                self.discovery.onIq(child)

    def onMessage(self, el):
        fro = el.getAttribute("from")
        try:
            froj = internJID(fro)
        except Exception, e:
            LogEvent(WARN, "", "Failed stringprep.")
            return
        mtype = el.getAttribute("type")
        s = self.sessions.get(froj.userhost(), None)
        if mtype == "error" and s:
            LogEvent(INFO, s.jabberID,
                     "Removing session because of message type=error")
            s.removeMe()
        elif s:
            s.onMessage(el)
        elif mtype != "error":
            to = el.getAttribute("to")
            ulang = utils.getLang(el)
            body = None
            for child in el.elements():
                if child.name == "body":
                    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")