home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyo (Python 2.6) from __future__ import with_statement from logging import getLogger from actions import ActionMeta from Buddy import get_bname from common import pref, profile, netcall, urlhandler from datetime import datetime from util.observe import Observable, ObservableList, observable_dict from util import traceguard log = getLogger('common.conversation') AUTORESP = _('[Auto-Response] ') class Conversation(Observable): __metaclass__ = ActionMeta AUTORESP = AUTORESP def __init__(self, protocol): Observable.__init__(self) self.room_list = ObservableList() self.protocol = protocol self.autoresponded = False self.typing_status = observable_dict() self.just_had_error = False self.pending_contacts_callbacks = set() self.start_time_utc = datetime.utcnow() self._bind_reconnect() self.protocol.add_observer(self._Conversation__on_proto_state, 'state') self.videochat_urlhandler = None def _bind_reconnect(self): if self.protocol is not None: self.protocol.when_reconnect(self._Conversation__on_account_reconnect) def __on_proto_state(self, src, attr, old, new): if new == src.Statuses.OFFLINE and self.ischat: import wx wx.CallLater((500,), (lambda : self.system_message(_('Disconnected')))) self.protocol.remove_observer(self._Conversation__on_proto_state, 'state') def conversation_reconnected(self): try: d = self._conversation_reconnected except AttributeError: Delegate = Delegate import util.primitives.funcs d = self._conversation_reconnected = Delegate() return d conversation_reconnected = property(conversation_reconnected) def __on_account_reconnect(self, new_connection): def foo(): log.warning('on_account_reconnect - connection: %r', new_connection) log.warning(' ischat: %r', self.ischat) if self.ischat: def success(convo): log.warning('rejoin_chat success: %r', convo) self.conversation_reconnected(convo) log.warning('rejoin_chat') new_connection.rejoin_chat(self, success = success) def later(): netcall(foo) import wx wx.CallLater(1000, later) def add_pending_contacts(self, contacts): for cb in self.pending_contacts_callbacks: cb(contacts) def maybe_send_typing_status(self, status): if pref('privacy.send_typing_notifications', False): self.send_typing_status(status) def icon(self): if self.ischat: skin = skin import gui return skin.get('serviceicons.digsby') return self.buddy.icon icon = property(icon) def send_plaintext_message(self, message): return self._send_message(default_formatted_text(message)) def send_message(self, message, *args, **kwargs): import hooks message = hooks.reduce('digsby.im.msg.send', message) echonow = pref('messaging.echo_immediately', type = bool, default = True) def error(e = (None, None)): log.info('Error sending message: %r', message.format_as('plaintext')) log.info('Message error callback received %r: %r', type(e), e) if self.just_had_error: return None emsg = getattr(e, 'error_message', '') if emsg: self.system_message(emsg, content_type = 'text/plain') else: self.system_message(_('Some of the messages you sent may not have been received.'), content_type = 'text/plain') self.just_had_error = True def message_sent(): self.just_had_error = False def echo_message(msg = (None, (None,), None)): if not msg: pass echomsg = message if kwargs.get('auto', False): if 'has_autotext' not in kwargs: kwargs.update(has_autotext = True) self.sent_message(echomsg, **kwargs) if echonow: def success(msg = (None,)): message_sent() echo_message() else: def success(msg = (None, None)): message_sent() echo_message(msg) conn = profile.connection if conn is not None: conn.send_message_intercept(self.buddy, message.format_as('plaintext')) (None, None, None, None, None, netcall)((lambda : self._send_message(message, success = success, error = error, *args, **kwargs))) if not self.ischat: b = self.buddy if b is not None and b is not self.protocol.self_buddy: profile.blist.add_tofrom('im', b, self.protocol) def system_message(self, message, **opts): log.debug('System message: message=%r, opts=%r', message, opts) opts['buddy'] = None opts['type'] = 'status' return self._message(message = message, **opts) def important_system_message(self, message, **opts): opts['system_message_raises'] = True return self.system_message(message, **opts) def buddy_says(self, buddy, message, **options): if not isinstance(message, unicode): raise TypeError, 'buddy_says needs unicode got type %r: %r' % (type(message), message) isinstance(message, unicode) try: timestamp = options.get('timestamp', None) if timestamp is None: timestamp = datetime.utcnow() content_type = options.pop('content_type', 'text/plain') Message = Message import common.message messageobj = Message(buddy = buddy, message = message, timestamp = timestamp, content_type = content_type) plugin_hub = plugin_hub import plugin_manager msgtype = options.get('type', None) plugin_hub.act('digsby.im.msg.pre', messageobj, msgtype) if messageobj.message != '': return self._message(buddy, messageobj.message, content_type = messageobj.content_type, **options) except Exception: e = None log.error('Failed to parse message %r', e) return self._message(buddy, message, **options) def _message(self, buddy, message, **options): if not options.get('offline', True): options.pop('timestamp', None) if options.get('offline', False): self.autoresponded = True timestamp = options.pop('timestamp', None) if timestamp is None: timestamp = datetime.utcnow() Message = Message import common.message messageobj = Message(buddy = buddy, message = message, timestamp = timestamp, conversation = self, **options) plugin_hub = plugin_hub import plugin_manager plugin_hub.act('digsby.im.msg.async', self, messageobj, options.get('type', None)) profile = profile import common return profile.on_message(messageobj) is not profile.on_message.VETO def received_message(self, buddy, message, **options): if not isinstance(message, unicode): raise TypeError('message argument must be unicode') isinstance(message, unicode) return self.buddy_says(buddy, message, type = 'incoming', **options) def sent_message(self, message, **options): buddy = self.self_buddy traceguard.__enter__() try: message = message.format_as('xhtml') options['content_type'] = 'text/xhtml' finally: pass self.autoresponded = True return self.buddy_says(buddy, message, type = 'outgoing', **options) def incoming_message(self, autoresp = True): status = profile.status.for_account(self.protocol) StatusMessage = StatusMessage import common if all((autoresp, status.away, not (status.invisible), status != StatusMessage.Offline, pref('messaging.when_away.autorespond', False), not (self.autoresponded))): if status.message: self.autoresponded = True import wx def later(): if getattr(status, '_a_href', False): content_type = 'text/html' else: content_type = 'text/plain' self.autorespond(status.message, content_type = content_type) later = (None, wx.CallAfter)(later) def __contains__(self, buddy): bname = get_bname(buddy) buddy = self.buddies[bname] return buddy in self.room_list def tingle(self): self.system_message('Your digsby sense is tingling!') def buddy(self): r = self.other_buddies if len(r) == 1: return r[0] if self.room_list: return self.room_list[0] return self.self_buddy buddy = property(buddy) def other_buddies(self): r = self.room_list[:] while True: try: r.remove(self.self_buddy) continue except ValueError: break continue None<EXCEPTION MATCH>ValueError return r other_buddies = property(other_buddies) def chat_member_count(self): if self.protocol.connected: return len(self.room_list) return 0 chat_member_count = property(chat_member_count) def send_typing_status(self, status): raise NotImplementedError did_explicit_exit = False def exit(self): self.unregister_videochat_urlhandler() def explicit_exit(self): self.did_explicit_exit = True self.exit() def autorespond(self, msg, format = None, **kws): if not self.ischat: if self.buddy.isbot: log.info('Not sending autoresponse to bot: %r', self.buddy) return None if 'has_autotext' not in kws: kws.update(has_autotext = True) self.send_message(default_formatted_text(AUTORESP + msg), auto = True, **kws) _inwindow = False def queued_system_messages(self): try: return self._queued_system_messages except AttributeError: self._queued_system_messages = [] return self._queued_system_messages queued_system_messages = property(queued_system_messages) def play_queued_messages(self): self._inwindow = True if hasattr(self, '_queued_system_messages'): try: for kws in self._queued_system_messages: traceguard.__enter__() try: self.system_message(**kws) finally: pass finally: del self._queued_system_messages[:] def buddy_join(self, buddy): if self.ischat: self._presence_message(_(u'%s joined the group chat') % buddy.name) def buddy_leave(self, buddy): if self.ischat and buddy is not self.protocol.self_buddy: self._presence_message(_(u'%s left the group chat') % buddy.name) def received_native_videochat_request(self, vidoechat_response_callback = None): log.info('native_video_chat_request') urlarg = 'makeavcall/%s' % id(self) if self.videochat_urlhandler is None: def videochat_urlhandler(): if vidoechat_response_callback is not None: vidoechat_response_callback(False) self.send_generic_videochat_request() self.videochat_urlhandler = (urlarg, videochat_urlhandler) urlhandler.register(*self.videochat_urlhandler) link = 'digsby://%s' % urlarg msg = _('%(name)s wants to have an Audio/Video chat. <a href="%(link)s">Send them an invite.</a>') % dict(name = self.buddy.name, link = link) self.important_system_message(msg, content_type = 'text/html') def send_generic_videochat_request(self): VideoChat = VideoChat import digsby.videochat VideoChat(self.buddy) def unregister_videochat_urlhandler(self): if self.videochat_urlhandler != None: urlhandler.unregister(*self.videochat_urlhandler) self.videochat_urlhandler = None def _presence_message(self, msg): kws = dict(message = msg, content_type = 'text/plain', linkify = False) if self._inwindow: self.system_message(**kws) else: self.queued_system_messages.append(kws) def default_formatted_text(msg): fmtstr = fmtstr import util.primitives.fmtstr get_default_format = get_default_format import gui.uberwidgets.formattedinput return fmtstr.singleformat(msg, get_default_format()) if __name__ == '__main__': convo = Conversation() def convo_changed(source, attr, old, new): print attr, old, new convo.add_observer(convo_changed) convo.last_message = 'test message'