summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0045.py
diff options
context:
space:
mode:
Diffstat (limited to 'sleekxmpp/plugins/xep_0045.py')
-rw-r--r--sleekxmpp/plugins/xep_0045.py89
1 files changed, 50 insertions, 39 deletions
diff --git a/sleekxmpp/plugins/xep_0045.py b/sleekxmpp/plugins/xep_0045.py
index bf472a46..db41cdb3 100644
--- a/sleekxmpp/plugins/xep_0045.py
+++ b/sleekxmpp/plugins/xep_0045.py
@@ -2,7 +2,7 @@
SleekXMPP: The Sleek XMPP Library
Copyright (C) 2010 Nathanael C. Fritz
This file is part of SleekXMPP.
-
+
See the file LICENSE for copying permission.
"""
from __future__ import with_statement
@@ -15,6 +15,10 @@ from .. xmlstream.handler.callback import Callback
from .. xmlstream.matcher.xpath import MatchXPath
from .. xmlstream.matcher.xmlmask import MatchXMLMask
+
+log = logging.getLogger(__name__)
+
+
class MUCPresence(ElementBase):
name = 'x'
namespace = 'http://jabber.org/protocol/muc#user'
@@ -34,79 +38,79 @@ class MUCPresence(ElementBase):
#TODO if no affilation, set it to the default and return default
item = self.getXMLItem()
return item.get('affiliation', '')
-
+
def setAffiliation(self, value):
item = self.getXMLItem()
#TODO check for valid affiliation
item.attrib['affiliation'] = value
return self
-
+
def delAffiliation(self):
item = self.getXMLItem()
#TODO set default affiliation
if 'affiliation' in item.attrib: del item.attrib['affiliation']
return self
-
+
def getJid(self):
item = self.getXMLItem()
return JID(item.get('jid', ''))
-
+
def setJid(self, value):
item = self.getXMLItem()
if not isinstance(value, str):
value = str(value)
item.attrib['jid'] = value
return self
-
+
def delJid(self):
item = self.getXMLItem()
if 'jid' in item.attrib: del item.attrib['jid']
return self
-
+
def getRole(self):
item = self.getXMLItem()
#TODO get default role, set default role if none
return item.get('role', '')
-
+
def setRole(self, value):
item = self.getXMLItem()
#TODO check for valid role
item.attrib['role'] = value
return self
-
+
def delRole(self):
item = self.getXMLItem()
#TODO set default role
if 'role' in item.attrib: del item.attrib['role']
return self
-
+
def getNick(self):
return self.parent()['from'].resource
-
+
def getRoom(self):
return self.parent()['from'].bare
-
+
def setNick(self, value):
- logging.warning("Cannot set nick through mucpresence plugin.")
+ log.warning("Cannot set nick through mucpresence plugin.")
return self
-
+
def setRoom(self, value):
- logging.warning("Cannot set room through mucpresence plugin.")
+ log.warning("Cannot set room through mucpresence plugin.")
return self
-
+
def delNick(self):
- logging.warning("Cannot delete nick through mucpresence plugin.")
+ log.warning("Cannot delete nick through mucpresence plugin.")
return self
-
+
def delRoom(self):
- logging.warning("Cannot delete room through mucpresence plugin.")
+ log.warning("Cannot delete room through mucpresence plugin.")
return self
class xep_0045(base.base_plugin):
"""
Impliments XEP-0045 Multi User Chat
"""
-
+
def plugin_init(self):
self.rooms = {}
self.ourNicks = {}
@@ -116,7 +120,8 @@ class xep_0045(base.base_plugin):
registerStanzaPlugin(Presence, MUCPresence)
self.xmpp.registerHandler(Callback('MUCPresence', MatchXMLMask("<presence xmlns='%s' />" % self.xmpp.default_ns), self.handle_groupchat_presence))
self.xmpp.registerHandler(Callback('MUCMessage', MatchXMLMask("<message xmlns='%s' type='groupchat'><body/></message>" % self.xmpp.default_ns), self.handle_groupchat_message))
-
+ self.xmpp.registerHandler(Callback('MUCSubject', MatchXMLMask("<message xmlns='%s' type='groupchat'><subject/></message>" % self.xmpp.default_ns), self.handle_groupchat_subject))
+
def handle_groupchat_presence(self, pr):
""" Handle a presence in a muc.
"""
@@ -135,27 +140,33 @@ class xep_0045(base.base_plugin):
if entry['nick'] not in self.rooms[entry['room']]:
got_online = True
self.rooms[entry['room']][entry['nick']] = entry
- logging.debug("MUC presence from %s/%s : %s" % (entry['room'],entry['nick'], entry))
+ log.debug("MUC presence from %s/%s : %s" % (entry['room'],entry['nick'], entry))
self.xmpp.event("groupchat_presence", pr)
self.xmpp.event("muc::%s::presence" % entry['room'], pr)
if got_offline:
self.xmpp.event("muc::%s::got_offline" % entry['room'], pr)
if got_online:
self.xmpp.event("muc::%s::got_online" % entry['room'], pr)
-
+
def handle_groupchat_message(self, msg):
""" Handle a message event in a muc.
"""
self.xmpp.event('groupchat_message', msg)
self.xmpp.event("muc::%s::message" % msg['from'].bare, msg)
-
+
+ def handle_groupchat_subject(self, msg):
+ """ Handle a message coming from a muc indicating
+ a change of subject (or announcing it when joining the room)
+ """
+ self.xmpp.event('groupchat_subject', msg)
+
def jidInRoom(self, room, jid):
for nick in self.rooms[room]:
entry = self.rooms[room][nick]
if entry is not None and entry['jid'].full == jid:
return True
return False
-
+
def getNick(self, room, jid):
for nick in self.rooms[room]:
entry = self.rooms[room][nick]
@@ -176,12 +187,12 @@ class xep_0045(base.base_plugin):
if xform is None: return False
form = self.xmpp.plugin['old_0004'].buildForm(xform)
return form
-
+
def configureRoom(self, room, form=None, ifrom=None):
if form is None:
form = self.getRoomForm(room, ifrom=ifrom)
#form = self.xmpp.plugin['old_0004'].makeForm(ftype='submit')
- #form.addField('FORM_TYPE', value='http://jabber.org/protocol/muc#roomconfig')
+ #form.addField('FORM_TYPE', value='http://jabber.org/protocol/muc#roomconfig')
iq = self.xmpp.makeIqSet()
iq['to'] = room
if ifrom is not None:
@@ -194,7 +205,7 @@ class xep_0045(base.base_plugin):
if result['type'] == 'error':
return False
return True
-
+
def joinMUC(self, room, nick, maxhistory="0", password='', wait=False, pstatus=None, pshow=None):
""" Join the specified room, requesting 'maxhistory' lines of history.
"""
@@ -220,7 +231,7 @@ class xep_0045(base.base_plugin):
self.xmpp.send(stanza, expect)
self.rooms[room] = {}
self.ourNicks[room] = nick
-
+
def destroy(self, room, reason='', altroom = '', ifrom=None):
iq = self.xmpp.makeIqSet()
if ifrom is not None:
@@ -246,9 +257,9 @@ class xep_0045(base.base_plugin):
raise TypeError
query = ET.Element('{http://jabber.org/protocol/muc#admin}query')
if nick is not None:
- item = ET.Element('item', {'affiliation':affiliation, 'nick':nick})
+ item = ET.Element('item', {'affiliation':affiliation, 'nick':nick})
else:
- item = ET.Element('item', {'affiliation':affiliation, 'jid':jid})
+ item = ET.Element('item', {'affiliation':affiliation, 'jid':jid})
query.append(item)
iq = self.xmpp.makeIqSet(query)
iq['to'] = room
@@ -256,7 +267,7 @@ class xep_0045(base.base_plugin):
if result is False or result['type'] != 'result':
raise ValueError
return True
-
+
def invite(self, room, jid, reason=''):
""" Invite a jid to a room."""
msg = self.xmpp.makeMessage(room)
@@ -279,7 +290,7 @@ class xep_0045(base.base_plugin):
else:
self.xmpp.sendPresence(pshow='unavailable', pto="%s/%s" % (room, nick))
del self.rooms[room]
-
+
def getRoomConfig(self, room):
iq = self.xmpp.makeIqGet('http://jabber.org/protocol/muc#owner')
iq['to'] = room
@@ -291,14 +302,14 @@ class xep_0045(base.base_plugin):
if form is None:
raise ValueError
return self.xmpp.plugin['xep_0004'].buildForm(form)
-
+
def cancelConfig(self, room):
query = ET.Element('{http://jabber.org/protocol/muc#owner}query')
x = ET.Element('{jabber:x:data}x', type='cancel')
query.append(x)
iq = self.xmpp.makeIqSet(query)
iq.send()
-
+
def setRoomConfig(self, room, config):
query = ET.Element('{http://jabber.org/protocol/muc#owner}query')
x = config.getXML('submit')
@@ -307,15 +318,15 @@ class xep_0045(base.base_plugin):
iq['to'] = room
iq['from'] = self.xmpp.jid
iq.send()
-
+
def getJoinedRooms(self):
return self.rooms.keys()
-
+
def getOurJidInRoom(self, roomJid):
""" Return the jid we're using in a room.
"""
return "%s/%s" % (roomJid, self.ourNicks[roomJid])
-
+
def getJidProperty(self, room, nick, jidProperty):
""" Get the property of a nick in a room, such as its 'jid' or 'affiliation'
If not found, return None.
@@ -324,7 +335,7 @@ class xep_0045(base.base_plugin):
return self.rooms[room][nick][jidProperty]
else:
return None
-
+
def getRoster(self, room):
""" Get the list of nicks in a room.
"""