예제 #1
0
 def do_notify(self, message, fro):
     body = get_body_from_message_stanza(message, use_xhtml=False)
     if not body:
         return
     command_str = self.config.get('command', '').strip()
     if not command_str:
         self.api.information(
             'No notification command was provided in the configuration file',
             'Warning')
         return
     command = [
         arg % {
             'body': body.replace('\n', ' '),
             'from': fro
         } for arg in shlex.split(command_str)
     ]
     self.core.exec_command(command)
     after_command_str = self.config.get('after_command', '').strip()
     if not after_command_str:
         return
     after_command = [
         arg % {
             'body': body.replace('\n', ' '),
             'from': fro
         } for arg in shlex.split(after_command_str)
     ]
     delayed_event = DelayedEvent(self.config.get('delay', 1),
                                  self.core.exec_command, after_command)
     self.api.add_timed_event(delayed_event)
예제 #2
0
 def do_notify(self, message, fro):
     body = clean_text(get_body_from_message_stanza(message))
     if not body:
         return
     command = self.config.get('command', '').strip()
     if not command:
         self.core.information('No notification command was provided in the configuration file', 'Warning')
         return
     self.core.exec_command(command % {'body':pipes.quote(body), 'from':pipes.quote(fro)})
     after_command = self.config.get('after_command', '').strip()
     if not after_command:
         return
     delayed_event = DelayedEvent(self.config.get('delay', 1), self.core.exec_command, after_command % {'body':pipes.quote(body), 'from':pipes.quote(fro)})
     self.core.add_timed_event(delayed_event)
예제 #3
0
 def do_notify(self, message, fro):
     body = clean_text(get_body_from_message_stanza(message))
     if not body:
         return
     command_str = self.config.get('command', '').strip()
     if not command_str:
         self.core.information('No notification command was provided in the configuration file', 'Warning')
         return
     command = [arg % {'body': body.replace('\n', ' '), 'from': fro} for arg in shlex.split(command_str)]
     self.core.exec_command(command)
     after_command_str = self.config.get('after_command', '').strip()
     if not after_command_str:
         return
     after_command = [arg % {'body': body.replace('\n', ' '), 'from': fro} for arg in shlex.split(after_command_str)]
     delayed_event = DelayedEvent(self.config.get('delay', 1), self.core.exec_command, after_command)
     self.core.add_timed_event(delayed_event)
예제 #4
0
    def on_groupchat_message(self, message):
        try:
            room_from = message.getMucroom()
            if message['type'] == 'error':
                return

            if room_from in self.config.options():
                target_lang = self.config.get(room_from, self.config.get('default', TARGET_LANG))
                nick_from = message['mucnick']
                body = xhtml.get_body_from_message_stanza(message)
                room = self.core.get_tab_by_name(room_from)
                text, lang = translate(body, target=target_lang)
                if lang != TARGET_LANG:
                    room.add_message(text, nickname=nick_from)
                    self.core.refresh_window()
        except Exception as e:
            import traceback
            self.core.information("Exception in translator! %s" % (traceback.format_exc(),))
예제 #5
0
파일: handlers.py 프로젝트: Perdu/poezio
def on_groupchat_private_message(self, message):
    """
    We received a Private Message (from someone in a Muc)
    """
    jid = message['from']
    nick_from = jid.resource
    if not nick_from:
        return self.on_groupchat_message(message)

    room_from = jid.bare
    use_xhtml = config.get('enable_xhtml_im')
    tmp_dir = config.get('tmp_image_dir') or path.join(CACHE_DIR, 'images')
    extract_images = config.get('extract_inline_images')
    body = xhtml.get_body_from_message_stanza(message, use_xhtml=use_xhtml,
                                              tmp_dir=tmp_dir,
                                              extract_images=extract_images)
    tab = self.get_tab_by_name(jid.full, tabs.PrivateTab) # get the tab with the private conversation
    ignore = config.get_by_tabname('ignore_private', room_from)
    if not tab: # It's the first message we receive: create the tab
        if body and not ignore:
            tab = self.open_private_window(room_from, nick_from, False)
    if ignore:
        self.events.trigger('ignored_private', message, tab)
        msg = config.get_by_tabname('private_auto_response', room_from)
        if msg and body:
            self.xmpp.send_message(mto=jid.full, mbody=msg, mtype='chat')
        return
    self.events.trigger('private_msg', message, tab)
    body = xhtml.get_body_from_message_stanza(message, use_xhtml=use_xhtml,
                                              tmp_dir=tmp_dir,
                                              extract_images=extract_images)
    if not body or not tab:
        return
    replaced_id = message['replace']['id']
    replaced = False
    user = tab.parent_muc.get_user_by_name(nick_from)
    if replaced_id is not '' and config.get_by_tabname('group_corrections',
                                                       room_from):
        try:
            tab.modify_message(body, replaced_id, message['id'], user=user, jid=message['from'],
                    nickname=nick_from)
            replaced = True
        except CorrectionError:
            log.debug('Unable to correct a message', exc_info=True)
    if not replaced:
        tab.add_message(body, time=None, nickname=nick_from,
                        forced_user=user,
                        identifier=message['id'],
                        jid=message['from'],
                        typ=1)

    if tab.remote_wants_chatstates is None:
        if message['chat_state']:
            tab.remote_wants_chatstates = True
        else:
            tab.remote_wants_chatstates = False
    if 'private' in config.get('beep_on').split():
        if not config.get_by_tabname('disable_beep', jid.full):
            curses.beep()
    if tab is self.current_tab():
        self.refresh_window()
    else:
        tab.state = 'private'
        self.refresh_tab_win()
예제 #6
0
파일: handlers.py 프로젝트: Perdu/poezio
def on_groupchat_message(self, message):
    """
    Triggered whenever a message is received from a multi-user chat room.
    """
    if message['subject']:
        return
    room_from = message['from'].bare

    if message['type'] == 'error': # Check if it's an error
        return self.room_error(message, room_from)

    tab = self.get_tab_by_name(room_from, tabs.MucTab)
    if not tab:
        self.information(_("message received for a non-existing room: %s") % (room_from))
        muc.leave_groupchat(self.xmpp, room_from, self.own_nick, msg='')
        return

    nick_from = message['mucnick']
    user = tab.get_user_by_name(nick_from)
    if user and user in tab.ignores:
        return

    self.events.trigger('muc_msg', message, tab)
    use_xhtml = config.get('enable_xhtml_im')
    tmp_dir = config.get('tmp_image_dir') or path.join(CACHE_DIR, 'images')
    extract_images = config.get('extract_inline_images')
    body = xhtml.get_body_from_message_stanza(message, use_xhtml=use_xhtml,
                                              tmp_dir=tmp_dir,
                                              extract_images=extract_images)
    if not body:
        return

    old_state = tab.state
    delayed, date = common.find_delayed_tag(message)
    replaced_id = message['replace']['id']
    replaced = False
    if replaced_id is not '' and config.get_by_tabname('group_corrections',
                                                       message['from'].bare):
        try:
            if tab.modify_message(body, replaced_id, message['id'], time=date,
                    nickname=nick_from, user=user):
                self.events.trigger('highlight', message, tab)
            replaced = True
        except CorrectionError:
            log.debug('Unable to correct a message', exc_info=True)
    if not replaced and tab.add_message(body, date, nick_from, history=delayed, identifier=message['id'], jid=message['from'], typ=1):
        self.events.trigger('highlight', message, tab)

    if message['from'].resource == tab.own_nick:
        tab.last_sent_message = message

    if tab is self.current_tab():
        tab.text_win.refresh()
        tab.info_header.refresh(tab, tab.text_win)
        tab.input.refresh()
        self.doupdate()
    elif tab.state != old_state:
        self.refresh_tab_win()
        current = self.current_tab()
        if hasattr(current, 'input') and current.input:
            current.input.refresh()
        self.doupdate()

    if 'message' in config.get('beep_on').split():
        if (not config.get_by_tabname('disable_beep', room_from)
                and self.own_nick != message['from'].resource):
            curses.beep()
예제 #7
0
파일: handlers.py 프로젝트: Perdu/poezio
def on_normal_message(self, message):
    """
    When receiving "normal" messages (not a private message from a
    muc participant)
    """
    if message['type'] == 'error':
        return self.information(self.get_error_message(message, deprecated=True), 'Error')
    elif message['type'] == 'headline' and message['body']:
        return self.information('%s says: %s' % (message['from'], message['body']), 'Headline')

    use_xhtml = config.get('enable_xhtml_im')
    tmp_dir = config.get('tmp_image_dir') or path.join(CACHE_DIR, 'images')
    extract_images = config.get('extract_inline_images')
    body = xhtml.get_body_from_message_stanza(message, use_xhtml=use_xhtml,
                                              tmp_dir=tmp_dir,
                                              extract_images=extract_images)
    if not body:
        return

    remote_nick = ''
    # normal message, we are the recipient
    if message['to'].bare == self.xmpp.boundjid.bare:
        conv_jid = message['from']
        jid = conv_jid
        color = get_theme().COLOR_REMOTE_USER
        # check for a name
        if conv_jid.bare in roster:
            remote_nick = roster[conv_jid.bare].name
        # check for a received nick
        if not remote_nick and config.get('enable_user_nick'):
            if message.xml.find('{http://jabber.org/protocol/nick}nick') is not None:
                remote_nick = message['nick']['nick']
        if not remote_nick:
            remote_nick = conv_jid.user
            if not remote_nick:
                remote_nick = conv_jid.full
        own = False
    # we wrote the message (happens with carbons)
    elif message['from'].bare == self.xmpp.boundjid.bare:
        conv_jid = message['to']
        jid = self.xmpp.boundjid
        color = get_theme().COLOR_OWN_NICK
        remote_nick = self.own_nick
        own = True
    # we are not part of that message, drop it
    else:
        return

    conversation = self.get_conversation_by_jid(conv_jid, create=True)
    if isinstance(conversation, tabs.DynamicConversationTab) and conv_jid.resource:
        conversation.lock(conv_jid.resource)

    if not own and not conversation.nick:
        conversation.nick = remote_nick
    elif not own: # keep a fixed nick during the whole conversation
        remote_nick = conversation.nick

    self.events.trigger('conversation_msg', message, conversation)
    if not message['body']:
        return
    body = xhtml.get_body_from_message_stanza(message, use_xhtml=use_xhtml,
                                              tmp_dir=tmp_dir,
                                              extract_images=extract_images)
    delayed, date = common.find_delayed_tag(message)

    def try_modify():
        replaced_id = message['replace']['id']
        if replaced_id and config.get_by_tabname('group_corrections',
                                                 conv_jid.bare):
            try:
                conversation.modify_message(body, replaced_id, message['id'], jid=jid,
                        nickname=remote_nick)
                return True
            except CorrectionError:
                log.debug('Unable to correct a message', exc_info=True)
        return False

    if not try_modify():
        conversation.add_message(body, date,
                nickname=remote_nick,
                nick_color=color,
                history=delayed,
                identifier=message['id'],
                jid=jid,
                typ=1)

    if conversation.remote_wants_chatstates is None and not delayed:
        if message['chat_state']:
            conversation.remote_wants_chatstates = True
        else:
            conversation.remote_wants_chatstates = False
    if 'private' in config.get('beep_on').split():
        if not config.get_by_tabname('disable_beep', conv_jid.bare):
            curses.beep()
    if self.current_tab() is not conversation:
        conversation.state = 'private'
        self.refresh_tab_win()
    else:
        self.refresh_window()