2009-06-03 22:56:51 +00:00
|
|
|
from .. xmlstream.stanzabase import StanzaBase
|
2009-12-11 01:29:46 +00:00
|
|
|
from xml.etree import cElementTree as ET
|
|
|
|
from . error import Error
|
2010-01-05 21:56:48 +00:00
|
|
|
from . rootstanza import RootStanza
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2010-01-05 21:56:48 +00:00
|
|
|
class Presence(RootStanza):
|
2009-12-11 01:29:46 +00:00
|
|
|
interfaces = set(('type', 'to', 'from', 'id', 'status', 'priority'))
|
|
|
|
types = set(('available', 'unavailable', 'error', 'probe', 'subscribe', 'subscribed', 'unsubscribe', 'unsubscribed'))
|
2010-02-27 02:02:08 +00:00
|
|
|
showtypes = set(('dnd', 'chat', 'xa', 'away'))
|
2009-12-11 01:29:46 +00:00
|
|
|
sub_interfaces = set(('status', 'priority'))
|
|
|
|
name = 'presence'
|
|
|
|
namespace = 'jabber:client'
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2009-12-11 01:29:46 +00:00
|
|
|
def getShowElement(self):
|
|
|
|
return self.xml.find("{%s}show" % self.namespace)
|
|
|
|
|
|
|
|
def setType(self, value):
|
2010-01-23 09:08:21 +00:00
|
|
|
show = self.getShowElement()
|
2009-12-11 01:29:46 +00:00
|
|
|
if value in self.types:
|
2010-01-23 09:08:21 +00:00
|
|
|
if show is not None:
|
|
|
|
self.xml.remove(show)
|
|
|
|
if value == 'available':
|
|
|
|
value = ''
|
|
|
|
self._setAttr('type', value)
|
|
|
|
elif value in self.showtypes:
|
|
|
|
if show is None:
|
|
|
|
show = ET.Element("{%s}show" % self.namespace)
|
|
|
|
self.xml.append(show)
|
|
|
|
show.text = value
|
2009-12-11 01:29:46 +00:00
|
|
|
return self
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2009-12-11 01:29:46 +00:00
|
|
|
def setPriority(self, value):
|
|
|
|
self._setSubText('priority', str(value))
|
|
|
|
|
|
|
|
def getPriority(self):
|
|
|
|
p = self._getSubText('priority')
|
|
|
|
if not p: p = 0
|
|
|
|
return int(p)
|
|
|
|
|
|
|
|
def getType(self):
|
|
|
|
out = self._getAttr('type')
|
|
|
|
if not out:
|
|
|
|
show = self.getShowElement()
|
|
|
|
if show is not None:
|
|
|
|
out = show.text
|
|
|
|
if not out or out is None:
|
|
|
|
out = 'available'
|
|
|
|
return out
|
|
|
|
|
2009-12-17 01:54:22 +00:00
|
|
|
def reply(self):
|
|
|
|
if self['type'] == 'unsubscribe':
|
|
|
|
self['type'] = 'unsubscribed'
|
|
|
|
elif self['type'] == 'subscribe':
|
|
|
|
self['type'] = 'subscribed'
|
|
|
|
return StanzaBase.reply(self)
|