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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from jabber.objects.x_event import X_Event, X_EVENT_NS
  5. from jabber.objects.chatstates import ChatState, CHATSTATES_NS
  6. from common.Conversation import Conversation
  7. from pyxmpp.message import Message
  8. from logging import getLogger
  9. from jabber import JabberBuddy
  10. from util import callsback
  11. from pyxmpp.utils import from_utf8
  12. from jabber.objects.x_delay import X_DELAY_NS, X_Delay
  13. import libxml2
  14. from common import pref
  15. from util.primitives.fmtstr import fmtstr
  16. log = getLogger('jabber.JabberConversation')
  17. XHTML_IM_NS = 'http://jabber.org/protocol/xhtml-im'
  18. XHTML_NS = 'http://www.w3.org/1999/xhtml'
  19. xdata_namespaces = {
  20.     'xhtmlim': XHTML_IM_NS,
  21.     'xhtml': XHTML_NS }
  22. typing_chatstates = dict(typing = 'composing', typed = 'paused')
  23.  
  24. class JabberConversation(Conversation):
  25.     
  26.     def __init__(self, protocol, buddy, jid_to, thread = None):
  27.         if not isinstance(buddy, JabberBuddy.JabberBuddy):
  28.             raise TypeError
  29.         isinstance(buddy, JabberBuddy.JabberBuddy)
  30.         Conversation.__init__(self, protocol)
  31.         self.buddy_to = buddy
  32.         self.jid_to = jid_to
  33.         self.buddies = protocol.buddies
  34.         self.thread = thread
  35.         self.name = buddy.alias
  36.         self.reset_chat_states()
  37.  
  38.     ischat = False
  39.     
  40.     def self_buddy(self):
  41.         return self.protocol.self_buddy
  42.  
  43.     self_buddy = property(self_buddy)
  44.     
  45.     def buddy(self):
  46.         return self.buddy_to
  47.  
  48.     buddy = property(buddy)
  49.     
  50.     def reset_chat_states(self):
  51.         self.chat_states_allowed = None
  52.         self.x_events_allowed = None
  53.  
  54.     
  55.     def _send_message(self, message, auto = False, callback = None, **opts):
  56.         if self.jid_to not in self.buddy.resources:
  57.             self.reset_chat_states()
  58.             self.jid_to = self.buddy.jid
  59.         
  60.         m = Message(stanza_type = 'chat', to_jid = self.jid_to, body = message.format_as('plaintext'))
  61.         append_formatted_html(m, message)
  62.         if pref('privacy.send_typing_notifications', False):
  63.             ChatState('active').as_xml(m.xmlnode)
  64.             X_Event(composing = True).as_xml(m.xmlnode)
  65.         
  66.         
  67.         try:
  68.             self.protocol.send_message(m)
  69.         except Exception:
  70.             e = None
  71.             callback.error(e)
  72.  
  73.         callback.success()
  74.  
  75.     _send_message = callsback(_send_message)
  76.     
  77.     def send_typing_status(self, status):
  78.         if not any((self.x_events_allowed, self.chat_states_allowed)):
  79.             return None
  80.         m = Message(to_jid = self.jid_to, stanza_type = 'chat')
  81.         node = m.xmlnode
  82.         if self.x_events_allowed:
  83.             X_Event(composing = status == 'typing').as_xml(node)
  84.         
  85.         if self.chat_states_allowed:
  86.             ChatState(typing_chatstates.get(status, 'active')).as_xml(node)
  87.         
  88.         self.protocol.send_message(m)
  89.  
  90.     
  91.     def buddy_join(self, buddy):
  92.         if buddy not in self.room_list:
  93.             self.room_list.append(buddy)
  94.         
  95.         self.typing_status[buddy] = None
  96.  
  97.     
  98.     def incoming_message(self, buddy, message):
  99.         from_jid = message.get_from()
  100.         if from_jid != self.jid_to:
  101.             self.reset_chat_states()
  102.         
  103.         self.jid_to = from_jid
  104.         body = get_message_body(message)
  105.         if body:
  106.             stamp = get_message_timestamp(message)
  107.             if stamp:
  108.                 did_receive = self.received_message(buddy, body, timestamp = stamp, offline = True, content_type = 'text/html')
  109.             else:
  110.                 did_receive = self.received_message(buddy, body, content_type = 'text/html')
  111.             if did_receive:
  112.                 Conversation.incoming_message(self)
  113.             
  114.         
  115.         chatstate = self.get_message_chatstate(message, body)
  116.         if chatstate is False:
  117.             chatstate = None
  118.             if pref('jabber.system_message.show_gone', type = bool, default = False):
  119.                 self.system_message(_('%s has left the conversation.') % from_jid)
  120.             
  121.         
  122.         self.typing_status[buddy] = chatstate
  123.  
  124.     
  125.     def get_message_chatstate(self, message, body):
  126.         retval = None
  127.         xevents = message.xpath_eval(u'jxe:x', {
  128.             'jxe': X_EVENT_NS })
  129.         chatstates = message.xpath_eval('cs:*', {
  130.             'cs': CHATSTATES_NS })
  131.         if chatstates:
  132.             self.chat_states_allowed = True
  133.             chatstate = ChatState(chatstates[0]).xml_element_name
  134.             retval = {
  135.                 'composing': 'typing',
  136.                 'paused': 'typed',
  137.                 'gone': False,
  138.                 'inactive': None,
  139.                 'active': None }.get(chatstate)
  140.         
  141.         if xevents:
  142.             found_composing = X_Event(xevents[0]).composing
  143.             if found_composing:
  144.                 self.x_events_allowed = True
  145.             
  146.             if not chatstates:
  147.                 retval = None if found_composing and not body else None
  148.             
  149.         
  150.         return retval
  151.  
  152.     
  153.     def id(self):
  154.         return (self.buddy_to,)
  155.  
  156.     id = property(id)
  157.     
  158.     def exit(self):
  159.         if self.chat_states_allowed and pref('privacy.send_typing_notifications', False):
  160.             m = Message(to_jid = self.jid_to, stanza_type = 'chat')
  161.             node = m.xmlnode
  162.             ChatState('gone').as_xml(node)
  163.             self.protocol.send_message(m)
  164.         
  165.         self.protocol.conversations.pop(self.id, None)
  166.         Conversation.exit(self)
  167.  
  168.  
  169.  
  170. def append_formatted_html(message_tag, message):
  171.     html = message_tag.xmlnode.newChild(None, 'html', None)
  172.     xhtml_ns = html.newNs(XHTML_IM_NS, None)
  173.     span_text = message.format_as('xhtml')
  174.     body_text = '<body xmlns="%s">%s</body>' % (XHTML_NS, span_text)
  175.     
  176.     try:
  177.         message_doc = libxml2.parseDoc(body_text.encode('utf-8'))
  178.     except Exception:
  179.         import traceback
  180.         traceback.print_exc()
  181.         print 'This text failed: %r' % body_text
  182.         raise 
  183.  
  184.     message_node = message_doc.get_children()
  185.     message_node_copy = message_node.docCopyNode(message_tag.xmlnode.doc, 1)
  186.     html.addChild(message_node_copy)
  187.     message_doc.freeDoc()
  188.     return span_text
  189.  
  190.  
  191. def get_message_timestamp(message):
  192.     xdelays = message.xpath_eval(u'jxd:x', {
  193.         'jxd': X_DELAY_NS })
  194.     if xdelays:
  195.         delay = X_Delay(xdelays[0])
  196.         if delay.timestamp is not None:
  197.             return delay.timestamp
  198.     
  199.  
  200.  
  201. def get_message_body(message):
  202.     jid = message.get_from()
  203.     xdata = message.xpath_eval(u'xhtmlim:html/xhtml:body[1]/node()', xdata_namespaces)
  204.     if xdata:
  205.         body = from_utf8(''.join((lambda .0: for child in .0:
  206. child.serialize())(xdata)))
  207.     else:
  208.         body = message.get_body()
  209.         body = None if body else None
  210.         if body is not None:
  211.             body = body.replace('\n', '<br />')
  212.         
  213.     return body
  214.  
  215.