home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / pyxmpp / error.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  12.9 KB  |  345 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __revision__ = '$Id: error.py 714 2010-04-05 10:20:10Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import libxml2
  7. from pyxmpp.utils import from_utf8, to_utf8
  8. from pyxmpp.xmlextra import common_doc, common_root, common_ns
  9. from pyxmpp import xmlextra
  10. from pyxmpp.exceptions import ProtocolError
  11. stream_errors = {
  12.     u'bad-format': ('Received XML cannot be processed',),
  13.     u'bad-namespace-prefix': ('Bad namespace prefix',),
  14.     u'conflict': ('Closing stream because of conflicting stream being opened',),
  15.     u'connection-timeout': ('Connection was idle too long',),
  16.     u'host-gone': ('Hostname is no longer hosted on the server',),
  17.     u'host-unknown': ('Hostname requested is not known to the server',),
  18.     u'improper-addressing': ('Improper addressing',),
  19.     u'internal-server-error': ('Internal server error',),
  20.     u'invalid-from': ('Invalid sender address',),
  21.     u'invalid-id': ('Invalid stream ID',),
  22.     u'invalid-namespace': ('Invalid namespace',),
  23.     u'invalid-xml': ('Invalid XML',),
  24.     u'not-authorized': ('Not authorized',),
  25.     u'policy-violation': ('Local policy violation',),
  26.     u'remote-connection-failed': ('Remote connection failed',),
  27.     u'resource-constraint': ('Remote connection failed',),
  28.     u'restricted-xml': ('Restricted XML received',),
  29.     u'see-other-host': ('Redirection required',),
  30.     u'system-shutdown': ('The server is being shut down',),
  31.     u'undefined-condition': ('Unknown error',),
  32.     u'unsupported-encoding': ('Unsupported encoding',),
  33.     u'unsupported-stanza-type': ('Unsupported stanza type',),
  34.     u'unsupported-version': ('Unsupported protocol version',),
  35.     u'xml-not-well-formed': ('XML sent by client is not well formed',) }
  36. stanza_errors = {
  37.     u'bad-request': ('Bad request', 'modify', 400),
  38.     u'conflict': ('Named session or resource already exists', 'cancel', 409),
  39.     u'feature-not-implemented': ('Feature requested is not implemented', 'cancel', 501),
  40.     u'forbidden': ('You are forbidden to perform requested action', 'auth', 403),
  41.     u'gone': ('Recipient or server can no longer be contacted at this address', 'modify', 302),
  42.     u'internal-server-error': ('Internal server error', 'wait', 500),
  43.     u'item-not-found': ('Item not found', 'cancel', 404),
  44.     u'jid-malformed': ('JID malformed', 'modify', 400),
  45.     u'not-acceptable': ('Requested action is not acceptable', 'modify', 406),
  46.     u'not-allowed': ('Requested action is not allowed', 'cancel', 405),
  47.     u'not-authorized': ('Not authorized', 'auth', 401),
  48.     u'payment-required': ('Payment required', 'auth', 402),
  49.     u'recipient-unavailable': ('Recipient is not available', 'wait', 404),
  50.     u'redirect': ('Redirection', 'modify', 302),
  51.     u'registration-required': ('Registration required', 'auth', 407),
  52.     u'remote-server-not-found': ('Remote server not found', 'cancel', 404),
  53.     u'remote-server-timeout': ('Remote server timeout', 'wait', 504),
  54.     u'resource-constraint': ('Resource constraint', 'wait', 500),
  55.     u'service-unavailable': ('Service is not available', 'cancel', 503),
  56.     u'subscription-required': ('Subscription is required', 'auth', 407),
  57.     u'undefined-condition': ('Unknown error', 'cancel', 500),
  58.     u'unexpected-request': ('Unexpected request', 'wait', 400) }
  59. legacy_codes = {
  60.     302: 'redirect',
  61.     400: 'bad-request',
  62.     401: 'not-authorized',
  63.     402: 'payment-required',
  64.     403: 'forbidden',
  65.     404: 'item-not-found',
  66.     405: 'not-allowed',
  67.     406: 'not-acceptable',
  68.     407: 'registration-required',
  69.     408: 'remote-server-timeout',
  70.     409: 'conflict',
  71.     500: 'internal-server-error',
  72.     501: 'feature-not-implemented',
  73.     502: 'service-unavailable',
  74.     503: 'service-unavailable',
  75.     504: 'remote-server-timeout',
  76.     510: 'service-unavailable' }
  77. STANZA_ERROR_NS = 'urn:ietf:params:xml:ns:xmpp-stanzas'
  78. STREAM_ERROR_NS = 'urn:ietf:params:xml:ns:xmpp-streams'
  79. PYXMPP_ERROR_NS = 'http://pyxmpp.jajcus.net/xmlns/errors'
  80. STREAM_NS = 'http://etherx.jabber.org/streams'
  81.  
  82. class ErrorNode:
  83.     
  84.     def __init__(self, xmlnode_or_cond, ns = None, copy = True, parent = None):
  85.         if type(xmlnode_or_cond) is str:
  86.             xmlnode_or_cond = unicode(xmlnode_or_cond, 'utf-8')
  87.         
  88.         self.xmlnode = None
  89.         self.borrowed = 0
  90.         if isinstance(xmlnode_or_cond, libxml2.xmlNode):
  91.             self._ErrorNode__from_xml(xmlnode_or_cond, ns, copy, parent)
  92.         elif isinstance(xmlnode_or_cond, ErrorNode):
  93.             if not copy:
  94.                 raise TypeError, 'ErrorNodes may only be copied'
  95.             copy
  96.             self.ns = from_utf8(xmlnode_or_cond.ns.getContent())
  97.             self.xmlnode = xmlnode_or_cond.xmlnode.docCopyNode(common_doc, 1)
  98.             if not parent:
  99.                 parent = common_root
  100.             
  101.             parent.addChild(self.xmlnode)
  102.         elif ns is None:
  103.             raise ValueError, 'Condition namespace not given'
  104.         elif parent:
  105.             self.xmlnode = parent.newChild(common_ns, 'error', None)
  106.             self.borrowed = 1
  107.         else:
  108.             self.xmlnode = common_root.newChild(common_ns, 'error', None)
  109.         cond = self.xmlnode.newChild(None, to_utf8(xmlnode_or_cond), None)
  110.         ns = cond.newNs(ns, None)
  111.         cond.setNs(ns)
  112.         self.ns = from_utf8(ns.getContent())
  113.  
  114.     
  115.     def __from_xml(self, xmlnode, ns, copy, parent):
  116.         if not ns:
  117.             ns = None
  118.             c = xmlnode.children
  119.             while c:
  120.                 ns = c.ns().getContent()
  121.                 if ns in (STREAM_ERROR_NS, STANZA_ERROR_NS):
  122.                     break
  123.                 
  124.                 ns = None
  125.                 c = c.next
  126.             if ns == None:
  127.                 raise ProtocolError, 'Bad error namespace'
  128.             ns == None
  129.         
  130.         self.ns = from_utf8(ns)
  131.         if copy:
  132.             self.xmlnode = xmlnode.docCopyNode(common_doc, 1)
  133.             if not parent:
  134.                 parent = common_root
  135.             
  136.             parent.addChild(self.xmlnode)
  137.         else:
  138.             self.xmlnode = xmlnode
  139.             self.borrowed = 1
  140.         if copy:
  141.             ns1 = xmlnode.ns()
  142.             xmlextra.replace_ns(self.xmlnode, ns1, common_ns)
  143.         
  144.  
  145.     
  146.     def __del__(self):
  147.         if self.xmlnode:
  148.             self.free()
  149.         
  150.  
  151.     
  152.     def free(self):
  153.         if not self.borrowed:
  154.             self.xmlnode.unlinkNode()
  155.             self.xmlnode.freeNode()
  156.         
  157.         self.xmlnode = None
  158.  
  159.     
  160.     def free_borrowed(self):
  161.         self.xmlnode = None
  162.  
  163.     
  164.     def is_legacy(self):
  165.         return not self.xmlnode.hasProp('type')
  166.  
  167.     
  168.     def xpath_eval(self, expr, namespaces = None):
  169.         ctxt = common_doc.xpathNewContext()
  170.         ctxt.setContextNode(self.xmlnode)
  171.         ctxt.xpathRegisterNs('ns', to_utf8(self.ns))
  172.         if namespaces:
  173.             for prefix, uri in namespaces.items():
  174.                 ctxt.xpathRegisterNs(prefix, uri)
  175.             
  176.         
  177.         ret = ctxt.xpathEval(expr)
  178.         ctxt.xpathFreeContext()
  179.         return ret
  180.  
  181.     
  182.     def get_condition(self, ns = None):
  183.         if ns is None:
  184.             ns = self.ns
  185.         
  186.         c = self.xpath_eval('ns:*')
  187.         if not c:
  188.             self.upgrade()
  189.             c = self.xpath_eval('ns:*')
  190.         
  191.         if not c:
  192.             return None
  193.         return c[0]
  194.  
  195.     
  196.     def get_text(self):
  197.         c = self.xpath_eval('ns:*')
  198.         if not c:
  199.             self.upgrade()
  200.         
  201.         t = self.xpath_eval('ns:text')
  202.         if not t:
  203.             return None
  204.         return from_utf8(t[0].getContent())
  205.  
  206.     
  207.     def add_custom_condition(self, ns, cond, content = None):
  208.         c = self.xmlnode.newTextChild(None, to_utf8(cond), content)
  209.         ns = c.newNs(to_utf8(ns), None)
  210.         c.setNs(ns)
  211.         return c
  212.  
  213.     
  214.     def upgrade(self):
  215.         if not self.xmlnode.hasProp('code'):
  216.             code = None
  217.         else:
  218.             
  219.             try:
  220.                 code = int(self.xmlnode.prop('code'))
  221.             except (ValueError, KeyError):
  222.                 code = None
  223.  
  224.         if code and legacy_codes.has_key(code):
  225.             cond = legacy_codes[code]
  226.         else:
  227.             cond = None
  228.         condition = self.xpath_eval('ns:*')
  229.         if condition:
  230.             return None
  231.         if cond is None:
  232.             condition = self.xmlnode.newChild(None, 'undefined-condition', None)
  233.             ns = condition.newNs(to_utf8(self.ns), None)
  234.             condition.setNs(ns)
  235.             condition = self.xmlnode.newChild(None, 'unknown-legacy-error', None)
  236.             ns = condition.newNs(PYXMPP_ERROR_NS, None)
  237.             condition.setNs(ns)
  238.         else:
  239.             condition = self.xmlnode.newChild(None, cond, None)
  240.             ns = condition.newNs(to_utf8(self.ns), None)
  241.             condition.setNs(ns)
  242.         txt = self.xmlnode.getContent()
  243.         if txt:
  244.             text = self.xmlnode.newTextChild(None, 'text', txt)
  245.             ns = text.newNs(to_utf8(self.ns), None)
  246.             text.setNs(ns)
  247.         
  248.  
  249.     
  250.     def downgrade(self):
  251.         if self.xmlnode.hasProp('code'):
  252.             return None
  253.         cond = self.get_condition()
  254.         if not cond:
  255.             return None
  256.         cond = cond.name
  257.  
  258.     
  259.     def serialize(self):
  260.         return self.xmlnode.serialize(encoding = 'utf-8')
  261.  
  262.  
  263.  
  264. class StreamErrorNode(ErrorNode):
  265.     
  266.     def __init__(self, xmlnode_or_cond, copy = 1, parent = None):
  267.         if type(xmlnode_or_cond) is str:
  268.             xmlnode_or_cond = xmlnode_or_cond.decode('utf-8')
  269.         
  270.         if type(xmlnode_or_cond) is unicode:
  271.             if not stream_errors.has_key(xmlnode_or_cond):
  272.                 raise ValueError, 'Bad error condition'
  273.             stream_errors.has_key(xmlnode_or_cond)
  274.         
  275.         ErrorNode.__init__(self, xmlnode_or_cond, STREAM_ERROR_NS, copy = copy, parent = parent)
  276.  
  277.     
  278.     def get_message(self):
  279.         cond = self.get_condition()
  280.         if not cond:
  281.             self.upgrade()
  282.             cond = self.get_condition()
  283.             if not cond:
  284.                 return None
  285.         
  286.         cond = cond.name
  287.         if not stream_errors.has_key(cond):
  288.             return None
  289.         return stream_errors[cond][0]
  290.  
  291.  
  292.  
  293. class StanzaErrorNode(ErrorNode):
  294.     
  295.     def __init__(self, xmlnode_or_cond, error_type = None, copy = 1, parent = None):
  296.         if type(xmlnode_or_cond) is str:
  297.             xmlnode_or_cond = unicode(xmlnode_or_cond, 'utf-8')
  298.         
  299.         if type(xmlnode_or_cond) is unicode:
  300.             if not stanza_errors.has_key(xmlnode_or_cond):
  301.                 raise ValueError, 'Bad error condition'
  302.             stanza_errors.has_key(xmlnode_or_cond)
  303.         
  304.         ErrorNode.__init__(self, xmlnode_or_cond, STANZA_ERROR_NS, copy = copy, parent = parent)
  305.         if type(xmlnode_or_cond) is unicode:
  306.             if error_type is None:
  307.                 error_type = stanza_errors[xmlnode_or_cond][1]
  308.             
  309.             self.xmlnode.setProp('type', to_utf8(error_type))
  310.         
  311.  
  312.     
  313.     def get_type(self):
  314.         if not self.xmlnode.hasProp('type'):
  315.             self.upgrade()
  316.         
  317.         return from_utf8(self.xmlnode.prop('type'))
  318.  
  319.     
  320.     def upgrade(self):
  321.         ErrorNode.upgrade(self)
  322.         if self.xmlnode.hasProp('type'):
  323.             return None
  324.         cond = self.get_condition().name
  325.         if stanza_errors.has_key(cond):
  326.             typ = stanza_errors[cond][1]
  327.             self.xmlnode.setProp('type', typ)
  328.         
  329.  
  330.     
  331.     def get_message(self):
  332.         cond = self.get_condition()
  333.         if not cond:
  334.             self.upgrade()
  335.             cond = self.get_condition()
  336.             if not cond:
  337.                 return None
  338.         
  339.         cond = cond.name
  340.         if not stanza_errors.has_key(cond):
  341.             return None
  342.         return stanza_errors[cond][0]
  343.  
  344.  
  345.