home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / pyxmpp / presence.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  5.5 KB  |  170 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __revision__ = '$Id: presence.py 714 2010-04-05 10:20:10Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import libxml2
  7. from pyxmpp.utils import to_utf8, from_utf8
  8. from pyxmpp.stanza import Stanza
  9. from pyxmpp.xmlextra import common_ns
  10. presence_types = ('available', 'unavailable', 'probe', 'subscribe', 'unsubscribe', 'subscribed', 'unsubscribed', 'invisible', 'error')
  11. accept_responses = {
  12.     'subscribe': 'subscribed',
  13.     'subscribed': 'subscribe',
  14.     'unsubscribe': 'unsubscribed',
  15.     'unsubscribed': 'unsubscribe' }
  16. deny_responses = {
  17.     'subscribe': 'unsubscribed',
  18.     'subscribed': 'unsubscribe',
  19.     'unsubscribe': 'subscribed',
  20.     'unsubscribed': 'subscribe' }
  21.  
  22. class Presence(Stanza):
  23.     stanza_type = 'presence'
  24.     
  25.     def __init__(self, xmlnode = None, from_jid = None, to_jid = None, stanza_type = None, stanza_id = None, show = None, status = None, priority = 0, error = None, error_cond = None, stream = None):
  26.         self.xmlnode = None
  27.         if isinstance(xmlnode, Presence):
  28.             pass
  29.         elif isinstance(xmlnode, Stanza):
  30.             raise TypeError, "Couldn't make Presence from other Stanza"
  31.         elif isinstance(xmlnode, libxml2.xmlNode):
  32.             pass
  33.         elif xmlnode is not None:
  34.             raise TypeError, "Couldn't make Presence from %r" % (type(xmlnode),)
  35.         
  36.         if stanza_type and stanza_type not in presence_types:
  37.             raise ValueError, 'Invalid presence type: %r' % (type,)
  38.         stanza_type not in presence_types
  39.         if stanza_type == 'available':
  40.             stanza_type = None
  41.         
  42.         if xmlnode is None:
  43.             xmlnode = 'presence'
  44.         
  45.         Stanza.__init__(self, xmlnode, from_jid = from_jid, to_jid = to_jid, stanza_type = stanza_type, stanza_id = stanza_id, error = error, error_cond = error_cond, stream = stream)
  46.         if show:
  47.             self.xmlnode.newTextChild(common_ns, 'show', to_utf8(show))
  48.         
  49.         if status:
  50.             self.xmlnode.newTextChild(common_ns, 'status', to_utf8(status))
  51.         
  52.         if priority and priority != 0:
  53.             self.xmlnode.newTextChild(common_ns, 'priority', to_utf8(unicode(priority)))
  54.         
  55.  
  56.     
  57.     def copy(self):
  58.         return Presence(self)
  59.  
  60.     
  61.     def set_status(self, status):
  62.         n = self.xpath_eval('ns:status')
  63.         if not status:
  64.             if n:
  65.                 n[0].unlinkNode()
  66.                 n[0].freeNode()
  67.             else:
  68.                 return None
  69.         n
  70.         if n:
  71.             n[0].setContent(to_utf8(status))
  72.         else:
  73.             self.xmlnode.newTextChild(common_ns, 'status', to_utf8(status))
  74.  
  75.     
  76.     def get_status(self):
  77.         n = self.xpath_eval('ns:status')
  78.         if n:
  79.             return from_utf8(n[0].getContent())
  80.         return None
  81.  
  82.     
  83.     def get_show(self):
  84.         n = self.xpath_eval('ns:show')
  85.         if n:
  86.             return from_utf8(n[0].getContent())
  87.         return None
  88.  
  89.     
  90.     def set_show(self, show):
  91.         n = self.xpath_eval('ns:show')
  92.         if not show:
  93.             if n:
  94.                 n[0].unlinkNode()
  95.                 n[0].freeNode()
  96.             else:
  97.                 return None
  98.         n
  99.         if n:
  100.             n[0].setContent(to_utf8(show))
  101.         else:
  102.             self.xmlnode.newTextChild(common_ns, 'show', to_utf8(show))
  103.  
  104.     
  105.     def get_priority(self):
  106.         n = self.xpath_eval('ns:priority')
  107.         if not n:
  108.             return 0
  109.         
  110.         try:
  111.             prio = int(n[0].getContent())
  112.         except ValueError:
  113.             n
  114.             n
  115.             return 0
  116.  
  117.         return prio
  118.  
  119.     
  120.     def set_priority(self, priority):
  121.         n = self.xpath_eval('ns:priority')
  122.         if not priority:
  123.             if n:
  124.                 n[0].unlinkNode()
  125.                 n[0].freeNode()
  126.             else:
  127.                 return None
  128.         n
  129.         priority = int(priority)
  130.         if priority < -128 or priority > 127:
  131.             raise ValueError, 'Bad priority value'
  132.         priority > 127
  133.         priority = str(priority)
  134.         if n:
  135.             n[0].setContent(priority)
  136.         else:
  137.             self.xmlnode.newTextChild(common_ns, 'priority', priority)
  138.  
  139.     
  140.     def make_accept_response(self):
  141.         if self.get_type() not in ('subscribe', 'subscribed', 'unsubscribe', 'unsubscribed'):
  142.             raise ValueError, "Results may only be generated for 'subscribe','subscribed','unsubscribe' or 'unsubscribed' presence"
  143.         self.get_type() not in ('subscribe', 'subscribed', 'unsubscribe', 'unsubscribed')
  144.         pr = Presence(stanza_type = accept_responses[self.get_type()], from_jid = self.get_to(), to_jid = self.get_from(), stanza_id = self.get_id())
  145.         return pr
  146.  
  147.     
  148.     def make_deny_response(self):
  149.         if self.get_type() not in ('subscribe', 'subscribed', 'unsubscribe', 'unsubscribed'):
  150.             raise ValueError, "Results may only be generated for 'subscribe','subscribed','unsubscribe' or 'unsubscribed' presence"
  151.         self.get_type() not in ('subscribe', 'subscribed', 'unsubscribe', 'unsubscribed')
  152.         pr = Presence(stanza_type = deny_responses[self.get_type()], from_jid = self.get_to(), to_jid = self.get_from(), stanza_id = self.get_id())
  153.         return pr
  154.  
  155.     
  156.     def make_error_response(self, cond):
  157.         if self.get_type() == 'error':
  158.             raise ValueError, 'Errors may not be generated in response to errors'
  159.         self.get_type() == 'error'
  160.         p = Presence(stanza_type = 'error', from_jid = self.get_to(), to_jid = self.get_from(), stanza_id = self.get_id(), error_cond = cond)
  161.         if self.xmlnode.children:
  162.             n = self.xmlnode.children
  163.             while n:
  164.                 p.xmlnode.children.addPrevSibling(n.copyNode(1))
  165.                 n = n.next
  166.         
  167.         return p
  168.  
  169.  
  170.