Exemplo n.º 1
0
    async def on_message(self, msg: discord.Message):
        err_msg = Message(msg.content, extras=msg.embeds)

        if isinstance(msg.channel, discord.abc.PrivateChannel):
            err_msg.frm = DiscordPerson(msg.author.id)
            err_msg.to = self.bot_identifier
        else:
            err_msg.to = DiscordRoom.from_id(msg.channel.id)
            err_msg.frm = DiscordRoomOccupant(msg.author.id, msg.channel.id)

        if self.process_message(err_msg):
            # Message contains a command
            recipient = err_msg.frm

            if not isinstance(recipient, DiscordSender):
                raise ValueError("Message object from is not a DiscordSender")

            async with recipient.get_discord_object().typing():
                self._dispatch_to_plugins("callback_message", err_msg)

        if msg.mentions:
            self.callback_mention(
                err_msg,
                [
                    DiscordRoomOccupant(mention.id, msg.channel.id)
                    for mention in msg.mentions
                ],
            )
Exemplo n.º 2
0
 def build_msg(self, msg):
     text = msg['content']
     user = msg['frm']
     sendu = msg['send']
     ttuser = msg['frm1']
     touser = msg['to']
     room = msg['room']
     msgs = Message(text)
     if msg["type"] == "ren":
         msgs.frm = wechatPerson(sendu, touser)
     else:
         msgs.frm = wechatRoomOccupant(userid=sendu, roomid=room)
     #self.usrname=ttuser
     if ttuser.startswith('@@'):
         #msgs.to=wechatPerson(userid=ttuser)
         msgs.to = WechatRoom(name=room)
         msgs.id = "group"
         log.info(msgs.to.name)
     else:
         msgs.to = wechatPerson(userid=ttuser)
         msgs.id = "user"
     msgs.send = sendu
     log.info('---------------- %s---------%s' % (msgs.to, msgs.frm))
     log.info('ssssssssssssssss %s' % msgs)
     log.info('ssssssssssssssss %s' % msgs.is_group)
     self.callback_message(msgs)
Exemplo n.º 3
0
    def _message_event_handler(self, event):
        """Event handler for the 'message' event"""
        channel = event['channel']
        if channel[0] not in 'CGD':
            log.warning("Unknown message type! Unable to handle %s", channel)
            return

        subtype = event.get('subtype', None)

        if subtype == "message_deleted":
            log.debug("Message of type message_deleted, ignoring this event")
            return
        if subtype == "message_changed" and 'attachments' in event['message']:
            # If you paste a link into Slack, it does a call-out to grab details
            # from it so it can display this in the chatroom. These show up as
            # message_changed events with an 'attachments' key in the embedded
            # message. We should completely ignore these events otherwise we
            # could end up processing bot commands twice (user issues a command
            # containing a link, it gets processed, then Slack triggers the
            # message_changed event and we end up processing it again as a new
            # message. This is not what we want).
            log.debug(
                "Ignoring message_changed event with attachments, likely caused "
                "by Slack auto-expanding a link")
            return

        if 'message' in event:
            text = event['message']['text']
            user = event['message'].get('user', event.get('bot_id'))
        else:
            text = event['text']
            user = event.get('user', event.get('bot_id'))

        text, mentioned = self.process_mentions(text)

        text = self.sanitize_uris(text)

        log.debug("Saw an event: %s" % pprint.pformat(event))
        log.debug("Escaped IDs event text: %s" % text)

        msg = Message(text, extras={'attachments': event.get('attachments')})

        if channel.startswith('D'):
            msg.frm = SlackPerson(self.sc, user, event['channel'])
            msg.to = SlackPerson(
                self.sc, self.username_to_userid(self.sc.server.username),
                event['channel'])
        else:
            msg.frm = SlackRoomOccupant(self.sc,
                                        user,
                                        event['channel'],
                                        bot=self)
            msg.to = SlackRoom(channelid=event['channel'], bot=self)

        self.callback_message(msg)

        if mentioned:
            self.callback_mention(msg, mentioned)
Exemplo n.º 4
0
    def _message_event_handler(self, event):
        """Event handler for the 'message' event"""
        channel = event['channel']
        if channel[0] not in 'CGD':
            log.warning("Unknown message type! Unable to handle %s", channel)
            return

        subtype = event.get('subtype', None)

        if subtype == "message_deleted":
            log.debug("Message of type message_deleted, ignoring this event")
            return
        if subtype == "message_changed" and 'attachments' in event['message']:
            # If you paste a link into Slack, it does a call-out to grab details
            # from it so it can display this in the chatroom. These show up as
            # message_changed events with an 'attachments' key in the embedded
            # message. We should completely ignore these events otherwise we
            # could end up processing bot commands twice (user issues a command
            # containing a link, it gets processed, then Slack triggers the
            # message_changed event and we end up processing it again as a new
            # message. This is not what we want).
            log.debug(
                "Ignoring message_changed event with attachments, likely caused "
                "by Slack auto-expanding a link"
            )
            return

        if 'message' in event:
            text = event['message']['text']
            user = event['message'].get('user', event.get('bot_id'))
        else:
            text = event['text']
            user = event.get('user', event.get('bot_id'))

        text, mentioned = self.process_mentions(text)

        text = self.sanitize_uris(text)

        log.debug("Saw an event: %s" % pprint.pformat(event))
        log.debug("Escaped IDs event text: %s" % text)

        msg = Message(
            text,
            extras={'attachments': event.get('attachments')})

        if channel.startswith('D'):
            msg.frm = SlackPerson(self.sc, user, event['channel'])
            msg.to = SlackPerson(self.sc, self.username_to_userid(self.sc.server.username),
                                 event['channel'])
        else:
            msg.frm = SlackRoomOccupant(self.sc, user, event['channel'], bot=self)
            msg.to = SlackRoom(channelid=event['channel'], bot=self)

        self.callback_message(msg)

        if mentioned:
            self.callback_mention(msg, mentioned)
Exemplo n.º 5
0
    def _message_event_handler(self, event):
        """Event handler for the 'message' event"""
        channel = event['channel']
        if channel.startswith('C'):
            log.debug("Handling message from a public channel")
            message_type = 'groupchat'
        elif channel.startswith('G'):
            log.debug("Handling message from a private group")
            message_type = 'groupchat'
        elif channel.startswith('D'):
            log.debug("Handling message from a user")
            message_type = 'chat'
        else:
            log.warning("Unknown message type! Unable to handle")
            return
        subtype = event.get('subtype', None)

        if subtype == "message_deleted":
            log.debug("Message of type message_deleted, ignoring this event")
            return
        if subtype == "message_changed" and 'attachments' in event['message']:
            # If you paste a link into Slack, it does a call-out to grab details
            # from it so it can display this in the chatroom. These show up as
            # message_changed events with an 'attachments' key in the embedded
            # message. We should completely ignore these events otherwise we
            # could end up processing bot commands twice (user issues a command
            # containing a link, it gets processed, then Slack triggers the
            # message_changed event and we end up processing it again as a new
            # message. This is not what we want).
            log.debug(
                "Ignoring message_changed event with attachments, likely caused "
                "by Slack auto-expanding a link")
            return

        if 'message' in event:
            text = event['message']['text']
            user = event['message']['user']
        else:
            text = event['text']
            user = event['user']

        text = re.sub("<[^>]*>", self.remove_angle_brackets_from_uris, text)

        msg = Message(text, type_=message_type)
        if message_type == 'chat':
            msg.frm = SlackIdentifier(self.sc, user, event['channel'])
            msg.to = SlackIdentifier(
                self.sc, self.username_to_userid(self.sc.server.username),
                event['channel'])
        else:
            msg.frm = SlackMUCOccupant(self.sc, user, event['channel'])
            msg.to = SlackMUCOccupant(
                self.sc, self.username_to_userid(self.sc.server.username),
                event['channel'])

        self.callback_message(msg)
Exemplo n.º 6
0
Arquivo: slack.py Projeto: jperras/err
    def _message_event_handler(self, event):
        """Event handler for the 'message' event"""
        channel = event['channel']
        if channel.startswith('C'):
            log.debug("Handling message from a public channel")
            message_type = 'groupchat'
        elif channel.startswith('G'):
            log.debug("Handling message from a private group")
            message_type = 'groupchat'
        elif channel.startswith('D'):
            log.debug("Handling message from a user")
            message_type = 'chat'
        else:
            log.warning("Unknown message type! Unable to handle")
            return
        subtype = event.get('subtype', None)

        if subtype == "message_deleted":
            log.debug("Message of type message_deleted, ignoring this event")
            return
        if subtype == "message_changed" and 'attachments' in event['message']:
            # If you paste a link into Slack, it does a call-out to grab details
            # from it so it can display this in the chatroom. These show up as
            # message_changed events with an 'attachments' key in the embedded
            # message. We should completely ignore these events otherwise we
            # could end up processing bot commands twice (user issues a command
            # containing a link, it gets processed, then Slack triggers the
            # message_changed event and we end up processing it again as a new
            # message. This is not what we want).
            log.debug(
                "Ignoring message_changed event with attachments, likely caused "
                "by Slack auto-expanding a link"
            )
            return

        if 'message' in event:
            text = event['message']['text']
            user = event['message']['user']
        else:
            text = event['text']
            user = event['user']

        text = re.sub("<[^>]*>", self.remove_angle_brackets_from_uris, text)

        msg = Message(text, type_=message_type)
        if message_type == 'chat':
            msg.frm = SlackIdentifier(self.sc, user, event['channel'])
            msg.to = SlackIdentifier(self.sc, self.username_to_userid(self.sc.server.username),
                                     event['channel'])
        else:
            msg.frm = SlackMUCOccupant(self.sc, user, event['channel'])
            msg.to = SlackMUCOccupant(self.sc, self.username_to_userid(self.sc.server.username),
                                      event['channel'])

        self.callback_message(msg)
Exemplo n.º 7
0
    def on_message(self, msg: discord.Message):
        err_msg = Message(msg.content)
        if msg.channel.is_private:
            err_msg.frm = DiscordPerson.from_user(msg.author)
            err_msg.to = self.bot_identifier
        else:
            err_msg.to = DiscordRoom.from_channel(msg.channel)
            err_msg.frm = DiscordRoomOccupant.from_user_and_channel(msg.author, msg.channel)

        log.debug('Received message %s' % msg)
        self.callback_message(err_msg)
        if msg.mentions:
            self.callback_mention(err_msg,
                                  [DiscordRoomOccupant.from_user_and_channel(mention, msg.channel)
                                   for mention in msg.mentions])
Exemplo n.º 8
0
    def on_message(self, msg: discord.Message):
        err_msg = Message(msg.content)
        if msg.channel.is_private:
            err_msg.frm = DiscordPerson.from_user(msg.author)
            err_msg.to = self.bot_identifier
        else:
            err_msg.to = DiscordRoom.from_channel(msg.channel)
            err_msg.frm = DiscordRoomOccupant.from_user_and_channel(msg.author, msg.channel)

        log.debug('Received message %s' % msg)
        self.callback_message(err_msg)
        if msg.mentions:
            self.callback_mention(err_msg,
                                  [DiscordRoomOccupant.from_user_and_channel(mention, msg.channel)
                                   for mention in msg.mentions])
Exemplo n.º 9
0
 def send_command(self, text):
     self.app.new_message(text, False)
     msg = Message(text)
     msg.frm = config.BOT_ADMINS[0]  # assume this is the admin talking
     msg.to = self.jid  # To me only
     self.callback_message(msg)
     self.app.input.clear()
Exemplo n.º 10
0
Arquivo: tox.py Projeto: edux/err
 def on_group_message(self, group_number, friend_group_number, message):
     log.debug('TOX: Group-%i User-%i: %s' % (group_number, friend_group_number, message))
     msg = Message(message, type_='groupchat')
     msg.frm = ToxIdentifier(group_number=str(group_number), friend_group_number=str(friend_group_number))
     msg.to = self.backend.bot_identifier
     log.debug('TOX: callback with type = %s' % msg.type)
     self.backend.callback_message(msg)
Exemplo n.º 11
0
 def serve_forever(self):
     self.jid = Identifier('Err')
     me = Identifier(config.BOT_ADMINS[0])
     self.connect_callback()  # notify that the connection occured
     self.callback_presence(Presence(identifier=me, status=ONLINE))
     try:
         while True:
             if ANSI:
                 entry = input('\n' + A_CYAN + ' >>> ' + A_RESET)
             else:
                 entry = input('\n>>> ')
             msg = Message(entry)
             msg.frm = me
             msg.to = self.jid
             self.callback_message(msg)
     except EOFError as eof:
         pass
     except KeyboardInterrupt as ki:
         pass
     finally:
         # simulate some real presence
         self.callback_presence(Presence(identifier=me, status=OFFLINE))
         logging.debug("Trigger disconnect callback")
         self.disconnect_callback()
         logging.debug("Trigger shutdown")
         self.shutdown()
Exemplo n.º 12
0
Arquivo: irc.py Projeto: qznc/err
 def on_pubmsg(self, _, e):
     msg = Message(e.arguments[0], type_='groupchat')
     msg.frm = e.target
     msg.to = self.callback.jid
     msg.nick = e.source.split('!')[
         0]  # FIXME find the real nick in the channel
     self.callback.callback_message(msg)
Exemplo n.º 13
0
 def send_command(self, text):
     self.app.new_message(text, False)
     msg = Message(text)
     msg.frm = self.build_identifier(self.bot_config.BOT_ADMINS[0])  # assume this is the admin talking
     msg.to = self.bot_identifier  # To me only
     self.callback_message(msg)
     self.app.input.clear()
Exemplo n.º 14
0
    def incoming_message(self, xmppmsg):
        """Callback for message events"""
        msg = Message(xmppmsg['body'])
        if 'html' in xmppmsg.keys():
            msg.html = xmppmsg['html']
        msg.frm = self.build_identifier(xmppmsg['from'].full)
        msg.to = self.build_identifier(xmppmsg['to'].full)
        log.debug("incoming_message frm : %s" % msg.frm)
        if xmppmsg['type'] == 'groupchat':
            room = XMPPRoom(msg.frm.node + '@' + msg.frm.domain, self)
            msg.frm = XMPPRoomOccupant(msg.frm.node, msg.frm.domain, msg.frm.resource, room)
            msg.to = room

        msg.nick = xmppmsg['mucnick']
        msg.delayed = bool(xmppmsg['delay']._get_attr('stamp'))  # this is a bug in sleekxmpp it should be ['from']
        self.callback_message(msg)
Exemplo n.º 15
0
 def on_group_message(self, group_number, friend_group_number, message):
     logging.debug('TOX: Group-%i User-%i: %s' % (group_number, friend_group_number, message))
     msg = Message(message, type_='groupchat')
     msg.frm = Identifier(node=str(group_number), resource=str(friend_group_number))
     msg.to = self.backend.jid
     logging.debug('TOX: callback with type = %s' % msg.type)
     self.backend.callback_message(msg)
Exemplo n.º 16
0
    def serve_once(self):
        """
        チャットシステムから新着メッセージを受け取り処理を行うメソッド

        このserve_onceメソッドはErrBotから定期的にコールされる
        似たようなオーバーライド対象メソッドにserve_foreverもある

        ref. http://errbot.io/en/latest/errbot.backends.base.html#errbot.backends.base.Backend.serve_forever
        """
        # 新着メッセージを取得
        mess = self.client.new_messages()

        # 取得したメッセージを順番に処理
        # メッセージにはユーザー名と発言されたルーム名も格納されている
        for msg in mess:
            # Messageオブジェクトを構築
            m = Message(msg)
            m.frm = HogeRoomOccupant(msg.username, msg.roomname, self)
            m.to = HogeRoom(msg.roomname, self)

            # メッセージのコールバックを呼び出す
            self.callback_message(m)

            # @<username>が含まれている場合はcallback_mentionも呼び出す
            # 詳細な実装を省いているので注意
            if '@' in msg:
                mentions = [HogeUser(username, self), ...]
                self.callback_mention(m, mentions)
Exemplo n.º 17
0
    def stream(self):
        r = self.backend.streamAPIRequest('rooms/%s/chatMessages' %
                                          self.room.idd)
        log.debug("connected %s" % self.room.name)

        try:
            self.reset_reconnection_count()
            for line in r.iter_lines(
                    chunk_size=1):  # it fails with anything else than 1.
                if line.strip():
                    json_message = json.loads(line.decode('utf-8'))
                    from_user = json_message['fromUser']
                    log.debug("Raw message from room %s: %s" %
                              (self.room.name, json_message))
                    m = Message(json_message['text'],
                                extras={'id': json_message['id']})
                    if self.room._uri == from_user['url']:
                        m.to = self.backend.bot_identifier
                    else:
                        m.to = self.room
                    m.frm = GitterRoomOccupant.build_from_json(
                        self.room, from_user)
                    self.backend.callback_message(m)
                else:
                    log.debug('Received keep-alive on %s', self.room.name)
        except:
            log.exception('An exception occured while streaming the room: ')
Exemplo n.º 18
0
 def serve_forever(self):
     me = self.build_identifier(self.bot_config.BOT_ADMINS[0])
     self.connect_callback()  # notify that the connection occured
     self.callback_presence(Presence(identifier=me, status=ONLINE))
     try:
         while True:
             if ANSI:
                 entry = input('\n' + str(fg.cyan) + ' >>> ' + str(fx.reset))
             else:
                 entry = input('\n>>> ')
             msg = Message(entry)
             msg.frm = me
             msg.to = self.bot_identifier
             self.callback_message(msg)
     except EOFError:
         pass
     except KeyboardInterrupt:
         pass
     finally:
         # simulate some real presence
         self.callback_presence(Presence(identifier=me, status=OFFLINE))
         log.debug("Trigger disconnect callback")
         self.disconnect_callback()
         log.debug("Trigger shutdown")
         self.shutdown()
Exemplo n.º 19
0
    def serve_forever(self):
        import config

        self.jid = Identifier('Err')  # whatever
        self.connect_callback()  # notify that the connection occured
        self.sender = config.BOT_ADMINS[0]  # By default, assume this is the admin talking
        try:
            while True:
                stanza_type, entry = incoming_stanza_queue.get()
                if entry == QUIT_MESSAGE:
                    logging.info("Stop magic message received, quitting...")
                    break
                if stanza_type is STZ_MSG:
                    msg = Message(entry)
                    msg.frm = self.sender
                    msg.to = self.jid  # To me only
                    self.callback_message(msg)
                elif stanza_type is STZ_PRE:
                    logging.info("Presence stanza received.")
                    self.callback_presence(entry)
                elif stanza_type is STZ_IQ:
                    logging.info("IQ stanza received.")
                else:
                    logging.error("Unknown stanza type.")

        except EOFError as _:
            pass
        except KeyboardInterrupt as _:
            pass
        finally:
            logging.debug("Trigger disconnect callback")
            self.disconnect_callback()
            logging.debug("Trigger shutdown")
            self.shutdown()
Exemplo n.º 20
0
 def serve_forever(self):
     me = self.build_identifier(self.bot_config.BOT_ADMINS[0])
     self.connect_callback()  # notify that the connection occured
     self.callback_presence(Presence(identifier=me, status=ONLINE))
     try:
         while True:
             if ANSI:
                 entry = input('\n' + str(fg.cyan) + ' >>> ' +
                               str(fx.reset))
             else:
                 entry = input('\n>>> ')
             msg = Message(entry)
             msg.frm = me
             msg.to = self.bot_identifier
             self.callback_message(msg)
     except EOFError:
         pass
     except KeyboardInterrupt:
         pass
     finally:
         # simulate some real presence
         self.callback_presence(Presence(identifier=me, status=OFFLINE))
         log.debug("Trigger disconnect callback")
         self.disconnect_callback()
         log.debug("Trigger shutdown")
         self.shutdown()
Exemplo n.º 21
0
Arquivo: slack.py Projeto: cjfoley/err
    def _message_event_handler(self, event):
        """Event handler for the 'message' event"""
        channel = event['channel']
        if channel.startswith('C'):
            logging.debug("Handling message from a public channel")
            message_type = 'groupchat'
        elif channel.startswith('G'):
            logging.debug("Handling message from a private group")
            message_type = 'groupchat'
        elif channel.startswith('D'):
            logging.debug("Handling message from a user")
            message_type = 'chat'
        else:
            logging.warning("Unknown message type! Unable to handle")
            return

        msg = Message(event['text'], type_=message_type)
        msg.frm = SlackIdentifier(
            node=self.channelid_to_channelname(event['channel']),
            domain=self.sc.server.domain,
            resource=self.userid_to_username(event['user'])
        )
        msg.to = SlackIdentifier(
            node=self.channelid_to_channelname(event['channel']),
            domain=self.sc.server.domain,
            resource=self.sc.server.username
        )
        msg.nick = msg.frm.resource
        self.callback_message(msg)
Exemplo n.º 22
0
    def serve_forever(self):
        self.connect_callback()  # notify that the connection occured
        try:
            while True:
                print('waiting on queue')
                stanza_type, entry = self.incoming_stanza_queue.get()
                print('message received')
                if entry == QUIT_MESSAGE:
                    log.info("Stop magic message received, quitting...")
                    break
                if stanza_type is STZ_MSG:
                    msg = Message(entry)
                    msg.frm = self.sender
                    msg.to = self.bot_identifier  # To me only
                    self.callback_message(msg)
                elif stanza_type is STZ_PRE:
                    log.info("Presence stanza received.")
                    self.callback_presence(entry)
                elif stanza_type is STZ_IQ:
                    log.info("IQ stanza received.")
                else:
                    log.error("Unknown stanza type.")

        except EOFError:
            pass
        except KeyboardInterrupt:
            pass
        finally:
            log.debug("Trigger disconnect callback")
            self.disconnect_callback()
            log.debug("Trigger shutdown")
            self.shutdown()
Exemplo n.º 23
0
    def serve_forever(self):
        me = self.build_identifier(self.bot_config.BOT_ADMINS[0])
        self.connect_callback()  # notify that the connection occured
        self.callback_presence(Presence(identifier=me, status=ONLINE))
        try:
            while True:
                if ANSI:
                    entry = input("\n" + str(fg.cyan) + " >>> " + str(fx.reset))
                else:
                    entry = input("\n>>> ")
                msg = Message(entry)
                msg.frm = me
                msg.to = self.bot_identifier
                self.callback_message(msg)

                mentioned = [
                    self.build_identifier(word[1:]) for word in re.findall(r"@[\w']+", entry) if word.startswith("@")
                ]
                if mentioned:
                    self.callback_mention(msg, mentioned)

                sleep(0.5)
        except EOFError:
            pass
        except KeyboardInterrupt:
            pass
        finally:
            # simulate some real presence
            self.callback_presence(Presence(identifier=me, status=OFFLINE))
            log.debug("Trigger disconnect callback")
            self.disconnect_callback()
            log.debug("Trigger shutdown")
            self.shutdown()
Exemplo n.º 24
0
 def msg_event_handler(self, msg):
     global text, mentioned
     if msg['group']:
         try:
             text = msg['content'][0]
         except:
             text = 'Null'
         user = msg['send_uid']
         touser = msg['to_uid']
         group = msg['group']
     else:
         try:
             text = msg['content'][0]
         except:
             text = 'Null'
         user = msg['send_uid']
         touser = msg['to_uid']
         group = None
     text, mentioned = self.process_mentions(text)
     msg = Message(text)
     msg.frm = QqSlackPerson(self.sc, user, group)
     msg.to = QqSlackPerson(self.sc, touser, group)
     self.callback_message(msg)
     if mentioned:
         self.callback_mention(msg, mentioned)
Exemplo n.º 25
0
    def serve_forever(self):
        import config

        self.jid = Identifier('Err')  # whatever
        self.connect_callback()  # notify that the connection occured
        self.sender = config.BOT_ADMINS[
            0]  # By default, assume this is the admin talking
        try:
            while True:
                stanza_type, entry = incoming_stanza_queue.get()
                if entry == QUIT_MESSAGE:
                    logging.info("Stop magic message received, quitting...")
                    break
                if stanza_type is STZ_MSG:
                    msg = Message(entry)
                    msg.frm = self.sender
                    msg.to = self.jid  # To me only
                    self.callback_message(msg)
                elif stanza_type is STZ_PRE:
                    logging.info("Presence stanza received.")
                    self.callback_presence(entry)
                elif stanza_type is STZ_IQ:
                    logging.info("IQ stanza received.")
                else:
                    logging.error("Unknown stanza type.")

        except EOFError as _:
            pass
        except KeyboardInterrupt as _:
            pass
        finally:
            logging.debug("Trigger disconnect callback")
            self.disconnect_callback()
            logging.debug("Trigger shutdown")
            self.shutdown()
Exemplo n.º 26
0
    def _handle_message(self, message):

        message_instance = Message(message[6],
                                   extras={'forward_messages': message[1]})

        if message[3] > 2000000000:
            # conference chat
            room = VKRoom(id=message[3] - 2000000000, title=message[5])
            user_id = message[7].get("from", "?")
            user = self.get_user_query(user_id)
            message_instance.frm = VKMUCOccupant(id=user_id,
                                                 room=room,
                                                 first_name=user["first_name"],
                                                 last_name=user["last_name"],
                                                 username="******")
            message_instance.to = message[3]
        else:

            # private
            user_id = str(message[3])

            user = self.get_user_query(user_id)
            if user:
                message_instance.frm = VKPerson(id=str(message[3]),
                                                first_name=user["first_name"],
                                                last_name=user["last_name"],
                                                username="******")
            else:
                message_instance.frm = VKPerson(id=str(message[3]),
                                                first_name=None,
                                                last_name=None,
                                                username="******")
            message_instance.to = message[3]

        log.info("[{}]: {}".format(message[3], message[6]))

        message_instance.extras["forward_messages"] = message[1]

        if message[7].get("source_act", None):
            if message[7].get("source_act", None) == "chat_invite_user":
                if int(message[7]["source_mid"]) == int(
                        self.bot_identifier.id):
                    self.callback_room_joined(self)
        else:
            self.callback_message(message_instance)
Exemplo n.º 27
0
 def build_reply(self,
                 msg: Message,
                 text=None,
                 private: bool = False,
                 threaded: bool = False) -> Message:
     msg = self.build_message(text)
     msg.frm = self.bot_identifier
     msg.to = msg.frm
     return msg
Exemplo n.º 28
0
 def on_group_message(self, group_number, friend_group_number, message):
     log.debug('TOX: Group-%i User-%i: %s' %
               (group_number, friend_group_number, message))
     msg = Message(message, type_='groupchat')
     msg.frm = ToxIdentifier(group_number=str(group_number),
                             friend_group_number=str(friend_group_number))
     msg.to = self.backend.bot_identifier
     log.debug('TOX: callback with type = %s' % msg.type)
     self.backend.callback_message(msg)
Exemplo n.º 29
0
Arquivo: irc.py Projeto: edux/err
 def on_pubmsg(self, _, e):
     msg = Message(e.arguments[0], type_='groupchat')
     nick = e.source.split('!')[0]
     room = e.target
     if room[0] != '#' and room[0] != '$':
         raise Exception('[%s] is not a room' % room)
     msg.frm = IRCMUCOccupant(nick, room)
     msg.to = self.callback.bot_identifier
     msg.nick = nick  # FIXME find the real nick in the channel
     self.callback.callback_message(msg)
Exemplo n.º 30
0
 def msg_callback(self, message):
     logging.debug('Incoming message [%s]' % message)
     user = ""
     if message.user:
         user = message.user.name
     if message.is_text():
         msg = Message(message.body, type_='groupchat')  # it is always a groupchat in campfire
         msg.frm = user + '@' + message.room.get_data()['name'] + '/' + user
         msg.to = self.jid  # assume it is for me
         self.callback_message(msg)
Exemplo n.º 31
0
 def msg_callback(self, message):
     logging.debug('Incoming message [%s]' % message)
     user = ""
     if message.user:
         user = message.user.name
     if message.is_text():
         msg = Message(message.body, type_='groupchat')  # it is always a groupchat in campfire
         msg.frm = user + '@' + message.room.get_data()['name'] + '/' + user
         msg.to = self.jid  # assume it is for me
         self.callback_message(msg)
Exemplo n.º 32
0
 def msg_callback(self, message):
     log.debug("Incoming message [%s]" % message)
     user = ""
     if message.user:
         user = message.user.name
     if message.is_text():
         msg = Message(message.body, type_="groupchat")  # it is always a groupchat in campfire
         msg.frm = user + "@" + message.room.get_data()["name"] + "/" + user
         msg.to = self.jid  # assume it is for me
         self.callback_message(msg)
Exemplo n.º 33
0
 def incoming_message(self, xmppmsg):
     """Callback for message events"""
     msg = Message(xmppmsg['body'])
     if 'html' in xmppmsg.keys():
         msg.html = xmppmsg['html']
     msg.frm = self.build_identifier(xmppmsg['from'].full)
     msg.to = self.build_identifier(xmppmsg['to'].full)
     log.debug("incoming_message frm : %s" % msg.frm)
     log.debug("incoming_message frm node: %s" % msg.frm.node)
     log.debug("incoming_message frm domain: %s" % msg.frm.domain)
     log.debug("incoming_message frm resource: %s" % msg.frm.resource)
     msg.type = xmppmsg['type']
     if msg.type == 'groupchat':
         # those are not simple identifiers, they are muc occupants.
         msg.frm = XMPPMUCOccupant(msg.frm.node, msg.frm.domain, msg.frm.resource)
         msg.to = XMPPMUCOccupant(msg.to.node, msg.to.domain, msg.to.resource)
     msg.nick = xmppmsg['mucnick']
     msg.delayed = bool(xmppmsg['delay']._get_attr('stamp'))  # this is a bug in sleekxmpp it should be ['from']
     self.callback_message(msg)
Exemplo n.º 34
0
Arquivo: xmpp.py Projeto: Kelur/errbot
 def incoming_message(self, xmppmsg):
     """Callback for message events"""
     msg = Message(xmppmsg["body"])
     if "html" in xmppmsg.keys():
         msg.html = xmppmsg["html"]
     msg.frm = self.build_identifier(xmppmsg["from"].full)
     msg.to = self.build_identifier(xmppmsg["to"].full)
     log.debug("incoming_message frm : %s" % msg.frm)
     log.debug("incoming_message frm node: %s" % msg.frm.node)
     log.debug("incoming_message frm domain: %s" % msg.frm.domain)
     log.debug("incoming_message frm resource: %s" % msg.frm.resource)
     msg.type = xmppmsg["type"]
     if msg.type == "groupchat":
         # those are not simple identifiers, they are muc occupants.
         msg.frm = XMPPMUCOccupant(msg.frm.node, msg.frm.domain, msg.frm.resource)
         msg.to = XMPPMUCOccupant(msg.to.node, msg.to.domain, msg.to.resource)
     msg.nick = xmppmsg["mucnick"]
     msg.delayed = bool(xmppmsg["delay"]._get_attr("stamp"))  # this is a bug in sleekxmpp it should be ['from']
     self.callback_message(msg)
Exemplo n.º 35
0
 def on_pubmsg(self, _, e):
     msg = Message(e.arguments[0], type_='groupchat')
     nick = e.source.split('!')[0]
     room = e.target
     if room[0] != '#' and room[0] != '$':
         raise Exception('[%s] is not a room' % room)
     msg.frm = IRCMUCOccupant(nick, room)
     msg.to = self.callback.bot_identifier
     msg.nick = nick  # FIXME find the real nick in the channel
     self.callback.callback_message(msg)
Exemplo n.º 36
0
 def msg_callback(self, message):
     log.debug('Incoming message [%s]' % message)
     user = ""
     if message.user:
         user = message.user.name
     if message.is_text():
         msg = Message(message.body, type_='groupchat')  # it is always a groupchat in campfire
         msg.frm = CampfireIdentifier(user)
         msg.to = self.bot_identifier  # assume it is for me
         self.callback_message(msg)
Exemplo n.º 37
0
Arquivo: xmpp.py Projeto: revcozmo/err
 def incoming_message(self, xmppmsg):
     """Callback for message events"""
     msg = Message(xmppmsg['body'])
     if 'html' in xmppmsg.keys():
         msg.html = xmppmsg['html']
     msg.frm = xmppmsg['from'].full
     msg.to = xmppmsg['to'].full
     msg.type = xmppmsg['type']
     msg.nick = xmppmsg['mucnick']
     msg.delayed = bool(xmppmsg['delay']._get_attr('stamp'))  # this is a bug in sleekxmpp it should be ['from']
     self.callback_message(msg)
Exemplo n.º 38
0
    def incoming_message(self, xmppmsg):
        """Callback for message events"""
        if xmppmsg['type'] == "error":
            log.warning("Received error message: %s", xmppmsg)
            return

        msg = Message(xmppmsg['body'])
        if 'html' in xmppmsg.keys():
            msg.html = xmppmsg['html']
        log.debug("incoming_message from: %s", msg.frm)
        if xmppmsg['type'] == 'groupchat':
            msg.frm = self._build_room_occupant(xmppmsg['from'].full)
            msg.to = msg.frm.room
        else:
            msg.frm = self._build_person(xmppmsg['from'].full)
            msg.to = self._build_person(xmppmsg['to'].full)

        msg.nick = xmppmsg['mucnick']
        msg.delayed = bool(xmppmsg['delay']._get_attr('stamp'))  # this is a bug in sleekxmpp it should be ['from']
        self.callback_message(msg)
Exemplo n.º 39
0
 def incoming_message(self, xmppmsg):
     """Callback for message events"""
     msg = Message(xmppmsg['body'])
     if 'html' in xmppmsg.keys():
         msg.html = xmppmsg['html']
     msg.frm = xmppmsg['from'].full
     msg.to = xmppmsg['to'].full
     msg.type = xmppmsg['type']
     msg.nick = xmppmsg['mucnick']
     msg.delayed = bool(xmppmsg['delay']._get_attr('stamp'))  # this is a bug in sleekxmpp it should be ['from']
     self.callback_message(msg)
Exemplo n.º 40
0
 def callback(self, *events):
     for event in events:
         log.debug("Saw event %s.", event)
         if event["type"] == "m.room.message":
             content = event["content"]
             sender = event["sender"]
             if content["msgtype"] == "m.text":
                 msg = Message(content["body"],
                               extras={'room_id': event["room_id"]})
                 msg.frm = MatrixPerson(self._client, sender)
                 msg.to = self.bot_identifier
                 self.callback_message(msg)
Exemplo n.º 41
0
    def send_command(self, text):
        self.app.new_message(text, False)
        msg = Message(text)
        msg.frm = self.user
        msg.to = self.bot_identifier  # To me only
        self.callback_message(msg)
        # implements the mentions.
        mentioned = [self.build_identifier(word[1:]) for word in re.findall(r"@[\w']+", text) if word.startswith("@")]
        if mentioned:
            self.callback_mention(msg, mentioned)

        self.app.input.clear()
Exemplo n.º 42
0
    def incoming_message(self, xmppmsg):
        """Callback for message events"""
        if xmppmsg["type"] == "error":
            log.warning("Received error message: %s", xmppmsg)
            return

        msg = Message(xmppmsg["body"])
        if "html" in xmppmsg.keys():
            msg.html = xmppmsg["html"]
        log.debug("incoming_message from: %s", msg.frm)
        if xmppmsg["type"] == "groupchat":
            msg.frm = self._build_room_occupant(xmppmsg["from"].full)
            msg.to = msg.frm.room
        else:
            msg.frm = self._build_person(xmppmsg["from"].full)
            msg.to = self._build_person(xmppmsg["to"].full)

        msg.nick = xmppmsg["mucnick"]
        msg.delayed = bool(xmppmsg["delay"]._get_attr(
            "stamp"))  # this is a bug in slixmpp it should be ['from']
        self.callback_message(msg)
Exemplo n.º 43
0
    def incoming_message(self, xmppmsg):
        """Callback for message events"""
        if xmppmsg['type'] == "error":
            log.warning("Received error message: %s", xmppmsg)
            return

        msg = Message(xmppmsg['body'])
        if 'html' in xmppmsg.keys():
            msg.html = xmppmsg['html']
        log.debug("incoming_message from: %s", msg.frm)
        if xmppmsg['type'] == 'groupchat':
            msg.frm = self._build_room_occupant(xmppmsg['from'].full)
            msg.to = msg.frm.room
        else:
            msg.frm = self._build_person(xmppmsg['from'].full)
            msg.to = self._build_person(xmppmsg['to'].full)

        msg.nick = xmppmsg['mucnick']
        msg.delayed = bool(xmppmsg['delay']._get_attr(
            'stamp'))  # this is a bug in sleekxmpp it should be ['from']
        self.callback_message(msg)
Exemplo n.º 44
0
 def incoming_message(self, xmppmsg):
     """Callback for message events"""
     msg = Message(xmppmsg['body'])
     if 'html' in xmppmsg.keys():
         msg.html = xmppmsg['html']
     msg.frm = self.build_identifier(xmppmsg['from'].full)
     msg.to = self.build_identifier(xmppmsg['to'].full)
     log.debug("incoming_message frm : %s" % msg.frm)
     log.debug("incoming_message frm node: %s" % msg.frm.node)
     log.debug("incoming_message frm domain: %s" % msg.frm.domain)
     log.debug("incoming_message frm resource: %s" % msg.frm.resource)
     msg.type = xmppmsg['type']
     if msg.type == 'groupchat':
         # those are not simple identifiers, they are muc occupants.
         msg.frm = XMPPMUCOccupant(msg.frm.node, msg.frm.domain,
                                   msg.frm.resource)
         msg.to = XMPPMUCOccupant(msg.to.node, msg.to.domain,
                                  msg.to.resource)
     msg.nick = xmppmsg['mucnick']
     msg.delayed = bool(xmppmsg['delay']._get_attr(
         'stamp'))  # this is a bug in sleekxmpp it should be ['from']
     self.callback_message(msg)
Exemplo n.º 45
0
    def send_command(self, text):
        self.app.new_message(text, False)
        msg = Message(text)
        msg.frm = self.user
        msg.to = self.bot_identifier  # To me only
        self.callback_message(msg)
        # implements the mentions.
        mentioned = [self.build_identifier(word[1:]) for word in re.findall(r"@[\w']+", text)
                     if word.startswith('@')]
        if mentioned:
            self.callback_mention(msg, mentioned)

        self.app.input.clear()
Exemplo n.º 46
0
    def send_command(self, text):
        self.app.new_message(text, False)
        msg = Message(text)
        msg.frm = self.build_identifier(self.bot_config.BOT_ADMINS[0])  # assume this is the admin talking
        msg.to = self.bot_identifier  # To me only
        self.callback_message(msg)
        # implements the mentions.
        mentioned = [self.build_identifier(word[1:]) for word in re.findall(r"@[\w']+", text)
                     if word.startswith('@')]
        if mentioned:
            self.callback_mention(msg, mentioned)

        self.app.input.clear()
Exemplo n.º 47
0
    def incoming_message(self, xmppmsg):
        """Callback for message events"""
        if xmppmsg['type'] == "error":
            log.warning("Received error message: %s", xmppmsg)
            return

        msg = Message(xmppmsg['body'])
        if 'html' in xmppmsg.keys():
            msg.html = xmppmsg['html']
        msg.frm = self.build_identifier(xmppmsg['from'].full)
        msg.to = self.build_identifier(xmppmsg['to'].full)
        log.debug("incoming_message from: %s", msg.frm)
        if xmppmsg['type'] == 'groupchat':
            room = self.room_factory(msg.frm.node + '@' + msg.frm.domain, self)
            msg.frm = self.roomoccupant_factory(msg.frm.node, msg.frm.domain,
                                                msg.frm.resource, room)
            msg.to = room

        msg.nick = xmppmsg['mucnick']
        msg.delayed = bool(xmppmsg['delay']._get_attr(
            'stamp'))  # this is a bug in sleekxmpp it should be ['from']
        self.callback_message(msg)
Exemplo n.º 48
0
        def post_botframework():
            req = request.json
            log.debug('received request: type=[%s] channel=[%s]', req['type'],
                      req['channelId'])
            if req['type'] == 'message':
                msg = Message(req['text'])
                msg.frm = errbot.build_identifier(req['from'])
                msg.to = errbot.build_identifier(req['recipient'])
                msg.extras['conversation'] = errbot.build_conversation(req)

                errbot._set_bot_identifier(msg.to)

                errbot.send_feedback(msg)
                errbot.callback_message(msg)
Exemplo n.º 49
0
    def incoming_message(self, xmppmsg: dict) -> None:
        """Callback for message events"""
        if xmppmsg["type"] == "error":
            log.warning("Received error message: %s", xmppmsg)
            return

        msg = Message(xmppmsg["body"])
        if "html" in xmppmsg.keys():
            msg.html = xmppmsg["html"]
        log.debug("incoming_message from: %s", msg.frm)
        if xmppmsg["type"] == "groupchat":
            msg.frm = self._build_room_occupant(xmppmsg["from"].full)
            msg.to = msg.frm.room
        else:
            msg.frm = self._build_person(xmppmsg["from"].full)
            msg.to = self._build_person(xmppmsg["to"].full)

        msg.nick = xmppmsg["mucnick"]
        now = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
        delay = xmppmsg["delay"]._get_attr(
            "stamp")  # this is a bug in sleekxmpp it should be ['from']
        msg.delayed = bool(delay and delay != now)
        self.callback_message(msg)
Exemplo n.º 50
0
    def on_pubmsg(self, _, e):
        msg = Message(e.arguments[0], type_='groupchat')
        room = e.target
        if room[0] != '#' and room[0] != '$':
            raise Exception('[%s] is not a room' % room)
        msg.frm = IRCMUCOccupant(e.source, room)
        msg.to = self.callback.bot_identifier
        msg.nick = msg.frm.nick  # FIXME find the real nick in the channel
        self.callback.callback_message(msg)

        possible_mentions = re.findall(IRC_NICK_REGEX, e.arguments[0])
        room_users = self.channels[room].users()
        mentions = filter(lambda x: x in room_users, possible_mentions)
        if mentions:
            mentions = [self.build_identifier(mention) for mention in mentions]
            self.callback.callback_mention(msg, mentions)
Exemplo n.º 51
0
Arquivo: irc.py Projeto: elopio/errbot
    def _pubmsg(self, e, notice=False):
        msg = Message(e.arguments[0], extras={'notice': notice})
        room_name = e.target
        if room_name[0] != '#' and room_name[0] != '$':
            raise Exception(f'[{room_name}] is not a room')
        room = IRCRoom(room_name, self.bot)
        msg.frm = IRCRoomOccupant(e.source, room)
        msg.to = room
        msg.nick = msg.frm.nick  # FIXME find the real nick in the channel
        self.bot.callback_message(msg)

        possible_mentions = re.findall(IRC_NICK_REGEX, e.arguments[0])
        room_users = self.channels[room_name].users()
        mentions = filter(lambda x: x in room_users, possible_mentions)
        if mentions:
            mentions = [self.bot.build_identifier(mention) for mention in mentions]
            self.bot.callback_mention(msg, mentions)
Exemplo n.º 52
0
 def background():
     log.debug("thread for %s started" % room.idd)
     r = self.streamAPIRequest('rooms/%s/chatMessages' % room.idd)
     log.debug("connected %s" % room.name)
     for line in r.iter_lines(chunk_size=1):  # it fails with anything else than 1.
         if line.strip():
             json_message = json.loads(line.decode('utf-8'))
             from_user = json_message['fromUser']
             log.debug("Raw message from room %s: %s" % (room.name, json_message))
             if room._uri == from_user['url']:
                 m = Message(json_message['text'])
                 m.to = self.bot_identifier
             else:
                 m = Message(json_message['text'])
                 m.to = room
             m.frm = GitterRoomOccupant.build_from_json(room, from_user)
             self.callback_message(m)
         else:
             log.debug('Received keep-alive on %s', room.name)
Exemplo n.º 53
0
    def _message_event_handler(self, message):
        """
        Event handler for the 'message' event
        """
        roomid = message.get('room', {}).get('id')
        text = message.get('text', '')
        owner = message.get('owner', {}).get('username')

        text, mentioned = self._extract_mentions_from(text)

        msg = Message(text)
        msg.frm = LetschatRoomOccupant(self.client, owner, roomid, self)
        if self.bot_identifier in mentioned:
            msg.to = self.bot_identifier
        else:
            msg.to = LetschatRoom(roomid=roomid, bot=self)

        self.callback_message(msg)

        if mentioned:
            self.callback_mention(msg, mentioned)
Exemplo n.º 54
0
    def serve_forever(self):
        self.connect_callback()  # notify that the connection occured
        try:
            while True:
                print("waiting on queue")
                stanza_type, entry = self.incoming_stanza_queue.get()
                print("message received")
                if entry == QUIT_MESSAGE:
                    log.info("Stop magic message received, quitting...")
                    break
                if stanza_type is STZ_MSG:
                    msg = Message(entry)
                    msg.frm = self.sender
                    msg.to = self.bot_identifier  # To me only

                    self.callback_message(msg)

                    # implements the mentions.
                    mentioned = [self.build_identifier(word[1:]) for word in entry.split() if word.startswith("@")]
                    if mentioned:
                        self.callback_mention(msg, mentioned)

                elif stanza_type is STZ_PRE:
                    log.info("Presence stanza received.")
                    self.callback_presence(entry)
                elif stanza_type is STZ_IQ:
                    log.info("IQ stanza received.")
                else:
                    log.error("Unknown stanza type.")

        except EOFError:
            pass
        except KeyboardInterrupt:
            pass
        finally:
            log.debug("Trigger disconnect callback")
            self.disconnect_callback()
            log.debug("Trigger shutdown")
            self.shutdown()
Exemplo n.º 55
0
    def serve_forever(self):
        if self.demo_mode:
            # disable the console logging once it is serving in demo mode.
            root = logging.getLogger()
            root.removeHandler(err.console_hdlr)
            root.addHandler(logging.NullHandler())
        me = self.build_identifier(self.bot_config.BOT_ADMINS[0])
        self.connect_callback()  # notify that the connection occured
        self.callback_presence(Presence(identifier=me, status=ONLINE))
        try:
            while True:
                if ANSI or self.demo_mode:
                    entry = input('\n' + str(fg.cyan) + ' >>> ' + str(fx.reset))
                else:
                    entry = input('\n>>> ')
                msg = Message(entry)
                msg.frm = me
                msg.to = self.bot_identifier
                self.callback_message(msg)

                mentioned = [self.build_identifier(word[1:]) for word in re.findall(r"@[\w']+", entry)
                             if word.startswith('@')]
                if mentioned:
                    self.callback_mention(msg, mentioned)

                sleep(.5)
        except EOFError:
            pass
        except KeyboardInterrupt:
            pass
        finally:
            # simulate some real presence
            self.callback_presence(Presence(identifier=me, status=OFFLINE))
            log.debug("Trigger disconnect callback")
            self.disconnect_callback()
            log.debug("Trigger shutdown")
            self.shutdown()
Exemplo n.º 56
0
 def on_privmsg(self, _, e):
     msg = Message(e.arguments[0])
     msg.frm = IRCPerson(e.source)
     msg.to = IRCPerson(e.target)
     self.bot.callback_message(msg)