| 1 |
#!/usr/bin/env python |
|---|
| 2 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 3 |
##~ License |
|---|
| 4 |
##~ |
|---|
| 5 |
##- The RuneBlade Foundation library is intended to ease some |
|---|
| 6 |
##- aspects of writing intricate Jabber, XML, and User Interface (wxPython, etc.) |
|---|
| 7 |
##- applications, while providing the flexibility to modularly change the |
|---|
| 8 |
##- architecture. Enjoy. |
|---|
| 9 |
##~ |
|---|
| 10 |
##~ Copyright (C) 2002 TechGame Networks, LLC. |
|---|
| 11 |
##~ |
|---|
| 12 |
##~ This library is free software; you can redistribute it and/or |
|---|
| 13 |
##~ modify it under the terms of the BSD style License as found in the |
|---|
| 14 |
##~ LICENSE file included with this distribution. |
|---|
| 15 |
##~ |
|---|
| 16 |
##~ TechGame Networks, LLC can be reached at: |
|---|
| 17 |
##~ 3578 E. Hartsel Drive #211 |
|---|
| 18 |
##~ Colorado Springs, Colorado, USA, 80920 |
|---|
| 19 |
##~ |
|---|
| 20 |
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 21 |
|
|---|
| 22 |
"""XMLBuilder compatable Jabber nodes for use in RBJabber.Client |
|---|
| 23 |
|
|---|
| 24 |
Dependencies: |
|---|
| 25 |
RBFoundation.XMLObjectify |
|---|
| 26 |
weakref |
|---|
| 27 |
re |
|---|
| 28 |
|
|---|
| 29 |
""" |
|---|
| 30 |
|
|---|
| 31 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 32 |
#~ Imports |
|---|
| 33 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 34 |
|
|---|
| 35 |
import weakref |
|---|
| 36 |
import re |
|---|
| 37 |
from RBFoundation import XMLObjectify |
|---|
| 38 |
|
|---|
| 39 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 40 |
#~ Jabber Node Classes |
|---|
| 41 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 42 |
|
|---|
| 43 |
class JabberNode(XMLObjectify.ObjectifiedXML): |
|---|
| 44 |
"""Default node class for a Jabber XML node.""" |
|---|
| 45 |
|
|---|
| 46 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 47 |
#~ Special |
|---|
| 48 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 49 |
|
|---|
| 50 |
def __init__(self, client, *args, **kw): |
|---|
| 51 |
self._client = weakref.proxy(client) |
|---|
| 52 |
XMLObjectify.ObjectifiedXML.__init__(self, client, *args, **kw) |
|---|
| 53 |
|
|---|
| 54 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 55 |
#~ Public Methods |
|---|
| 56 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 57 |
|
|---|
| 58 |
def Content(self): |
|---|
| 59 |
"""Returns all the PCData of the Node as a list of strings""" |
|---|
| 60 |
return self(None) |
|---|
| 61 |
|
|---|
| 62 |
def Elements(self, node=None, namespace=None): |
|---|
| 63 |
"""Gets all of the child nodes of this node that match node or namespace as regular expressions.""" |
|---|
| 64 |
elementList = self._elements |
|---|
| 65 |
if namespace is not None: |
|---|
| 66 |
match = re.compile(namespace).match |
|---|
| 67 |
elementList = [element for element in elementList if match(element[0][0])] |
|---|
| 68 |
|
|---|
| 69 |
if node is None: elementList = [element[1] for element in elementList if element[0][1]] |
|---|
| 70 |
elif node == '*': elementList = [element[1] for element in elementList] |
|---|
| 71 |
else: |
|---|
| 72 |
match = re.compile(node).match |
|---|
| 73 |
elementList = [element[1] for element in elementList if match(element[0][1])] |
|---|
| 74 |
return elementList |
|---|
| 75 |
|
|---|
| 76 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 77 |
|
|---|
| 78 |
class JabberStream(JabberNode): |
|---|
| 79 |
"""Jabber node for the root Jabber tag. |
|---|
| 80 |
Namely stream:stream where xmlns:stream='http://etherx.jabber.org/streams' |
|---|
| 81 |
|
|---|
| 82 |
Note that we do not care if there is any PCData, |
|---|
| 83 |
nor does the root node keep its child nodes. |
|---|
| 84 |
This allows for those nodes to be deallocated |
|---|
| 85 |
after they are processed. |
|---|
| 86 |
""" |
|---|
| 87 |
|
|---|
| 88 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 89 |
#~ Protected Methods |
|---|
| 90 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 91 |
|
|---|
| 92 |
def _xmlInitStarted(self): |
|---|
| 93 |
self._client.ServerJID = self.from_ |
|---|
| 94 |
self._client.stream.UpdateObservers(settings=self) |
|---|
| 95 |
def _addElement(self, node, element): pass |
|---|
| 96 |
def _addData(self, data): pass |
|---|
| 97 |
|
|---|
| 98 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 99 |
|
|---|
| 100 |
class JabberStreamError(JabberNode): |
|---|
| 101 |
"""Jabber node for stream errors. (Mostly invalid XML in my experience.) |
|---|
| 102 |
stream:error where xmlns:stream='http://etherx.jabber.org/streams' |
|---|
| 103 |
|
|---|
| 104 |
Raises JabberStreamError.JabberStreamError |
|---|
| 105 |
""" |
|---|
| 106 |
|
|---|
| 107 |
class JabberStreamError(Exception): pass |
|---|
| 108 |
|
|---|
| 109 |
def _xmlInitComplete(self): |
|---|
| 110 |
self._client.stream.UpdateObserversEx({self.__node__: self}) |
|---|
| 111 |
self._client.Shutdown() |
|---|
| 112 |
raise JabberStreamError.JabberStreamError, self._toXML() |
|---|
| 113 |
|
|---|
| 114 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 115 |
|
|---|
| 116 |
class JabberClientNode(JabberNode): |
|---|
| 117 |
"""Jabber nodes base for jabber:client nodes. |
|---|
| 118 |
When the node is completely built, the client stream |
|---|
| 119 |
subject's observers are updated""" |
|---|
| 120 |
|
|---|
| 121 |
def _xmlInitComplete(self): |
|---|
| 122 |
if self.__namespace__ == 'jabber:client': |
|---|
| 123 |
self._client.stream.UpdateObserversEx({self.__node__: self}) |
|---|
| 124 |
|
|---|
| 125 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 126 |
|
|---|
| 127 |
class JabberClientPresence(JabberClientNode): |
|---|
| 128 |
"""Provides jabber:client presence specific default attribute values to reduce |
|---|
| 129 |
the quantity of conditional code.""" |
|---|
| 130 |
_default_attributes = {'from':'', 'type':'available', 'id':'default_presence_id'} |
|---|
| 131 |
|
|---|
| 132 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 133 |
|
|---|
| 134 |
class JabberClientMessage(JabberClientNode): |
|---|
| 135 |
"""Provides jabber:client presence specific default attribute values to reduce |
|---|
| 136 |
the quantity of conditional code.""" |
|---|
| 137 |
_default_attributes = {'from':'', 'to':'', 'type':'message', 'id':'default_message_id'} |
|---|
| 138 |
|
|---|
| 139 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 140 |
|
|---|
| 141 |
class JabberClientIQ(JabberClientNode): |
|---|
| 142 |
"""Provides jabber:client presence specific default attribute values to reduce |
|---|
| 143 |
the quantity of conditional code.""" |
|---|
| 144 |
_default_attributes = {'from':'', 'type':'get', 'id':'default_iq_id'} |
|---|
| 145 |
|
|---|