diff options
Diffstat (limited to 'sleekxmpp/basexmpp.py')
-rw-r--r-- | sleekxmpp/basexmpp.py | 53 |
1 files changed, 27 insertions, 26 deletions
diff --git a/sleekxmpp/basexmpp.py b/sleekxmpp/basexmpp.py index 2c2bb91e..b7b605b0 100644 --- a/sleekxmpp/basexmpp.py +++ b/sleekxmpp/basexmpp.py @@ -25,6 +25,7 @@ from . stanza.roster import Roster from . stanza.nick import Nick from . stanza.htmlim import HTMLIM from . stanza.error import Error +from sleekxmpp.xmlstream.tostring import tostring import logging import threading @@ -60,7 +61,7 @@ class basexmpp(object): registerStanzaPlugin(Iq, Roster) registerStanzaPlugin(Message, Nick) registerStanzaPlugin(Message, HTMLIM) - + def Message(self, *args, **kwargs): return Message(self, *args, **kwargs) @@ -69,7 +70,7 @@ class basexmpp(object): def Presence(self, *args, **kwargs): return Presence(self, *args, **kwargs) - + def set_jid(self, jid): """Rip a JID apart and claim it as our own.""" self.fulljid = jid @@ -77,12 +78,12 @@ class basexmpp(object): self.jid = self.getjidbare(jid) self.username = jid.split('@', 1)[0] self.server = jid.split('@',1)[-1].split('/', 1)[0] - + def process(self, *args, **kwargs): for idx in self.plugin: if not self.plugin[idx].post_inited: self.plugin[idx].post_init() return super(basexmpp, self).process(*args, **kwargs) - + def registerPlugin(self, plugin, pconfig = {}): """Register a plugin not in plugins.__init__.__all__ but in the plugins directory.""" @@ -97,7 +98,7 @@ class basexmpp(object): if hasattr(self.plugin[plugin], 'xep'): xep = "(XEP-%s) " % self.plugin[plugin].xep logging.debug("Loaded Plugin %s%s" % (xep, self.plugin[plugin].description)) - + def register_plugins(self): """Initiates all plugins in the plugins/__init__.__all__""" if self.plugin_whitelist: @@ -112,24 +113,24 @@ class basexmpp(object): # run post_init() for cross-plugin interaction for plugin in self.plugin: self.plugin[plugin].post_init() - + def getNewId(self): with self.id_lock: self.id += 1 return self.getId() - + def add_handler(self, mask, pointer, name=None, disposable=False, threaded=False, filter=False, instream=False): # threaded is no longer needed, but leaving it for backwards compatibility for now if name is None: name = 'add_handler_%s' % self.getNewId() self.registerHandler(XMLCallback(name, MatchXMLMask(mask), pointer, threaded, disposable, instream)) - + def getId(self): return "%x".upper() % self.id def sendXML(self, data, mask=None, timeout=10): - return self.send(self.tostring(data), mask, timeout) - + return self.send(tostring(data), mask, timeout) + def send(self, data, mask=None, timeout=10): #logging.warning("Deprecated send used for \"%s\"" % (data,)) #if not type(data) == type(''): @@ -144,19 +145,19 @@ class basexmpp(object): self.sendRaw(data) if mask is not None: return waitfor.wait(timeout) - + def makeIq(self, id=0, ifrom=None): return self.Iq().setStanzaValues({'id': str(id), 'from': ifrom}) - + def makeIqGet(self, queryxmlns = None): iq = self.Iq().setStanzaValues({'type': 'get'}) if queryxmlns: iq.append(ET.Element("{%s}query" % queryxmlns)) return iq - + def makeIqResult(self, id): return self.Iq().setStanzaValues({'id': id, 'type': 'result'}) - + def makeIqSet(self, sub=None): iq = self.Iq().setStanzaValues({'type': 'set'}) if sub != None: @@ -172,13 +173,13 @@ class basexmpp(object): query = ET.Element("{%s}query" % xmlns) iq.append(query) return iq - + def makeQueryRoster(self, iq=None): query = ET.Element("{jabber:iq:roster}query") if iq: iq.append(query) return query - + def add_event_handler(self, name, pointer, threaded=False, disposable=False): if not name in self.event_handlers: self.event_handlers[name] = [] @@ -188,13 +189,13 @@ class basexmpp(object): """Remove a handler for an event.""" if not name in self.event_handlers: return - + # Need to keep handlers that do not use # the given function pointer def filter_pointers(handler): return handler[0] != pointer - self.event_handlers[name] = filter(filter_pointers, + self.event_handlers[name] = filter(filter_pointers, self.event_handlers[name]) def event(self, name, eventdata = {}): # called on an event @@ -209,7 +210,7 @@ class basexmpp(object): if handler[2]: #disposable with self.lock: self.event_handlers[name].pop(self.event_handlers[name].index(handler)) - + def makeMessage(self, mto, mbody=None, msubject=None, mtype=None, mhtml=None, mfrom=None, mnick=None): message = self.Message(sto=mto, stype=mtype, sfrom=mfrom) message['body'] = mbody @@ -217,7 +218,7 @@ class basexmpp(object): if mnick is not None: message['nick'] = mnick if mhtml is not None: message['html']['html'] = mhtml return message - + def makePresence(self, pshow=None, pstatus=None, ppriority=None, pto=None, ptype=None, pfrom=None): presence = self.Presence(stype=ptype, sfrom=pfrom, sto=pto) if pshow is not None: presence['type'] = pshow @@ -226,10 +227,10 @@ class basexmpp(object): presence['priority'] = ppriority presence['status'] = pstatus return presence - + def sendMessage(self, mto, mbody, msubject=None, mtype=None, mhtml=None, mfrom=None, mnick=None): self.send(self.makeMessage(mto,mbody,msubject,mtype,mhtml,mfrom,mnick)) - + def sendPresence(self, pshow=None, pstatus=None, ppriority=None, pto=None, pfrom=None, ptype=None): self.send(self.makePresence(pshow,pstatus,ppriority,pto, ptype=ptype, pfrom=pfrom)) if not self.sentpresence: @@ -243,19 +244,19 @@ class basexmpp(object): nick.text = pnick presence.append(nick) self.send(presence) - + def getjidresource(self, fulljid): if '/' in fulljid: return fulljid.split('/', 1)[-1] else: return '' - + def getjidbare(self, fulljid): return fulljid.split('/', 1)[0] def _handleMessage(self, msg): self.event('message', msg) - + def _handlePresence(self, presence): """Update roster items based on presence""" self.event("presence_%s" % presence['type'], presence) @@ -296,7 +297,7 @@ class basexmpp(object): if name: name = "(%s) " % name logging.debug("STATUS: %s%s/%s[%s]: %s" % (name, jid, resource, show,status)) - + def _handlePresenceSubscribe(self, presence): """Handling subscriptions automatically.""" if self.auto_authorize == True: |