Esempio n. 1
0
File: tox.py Progetto: 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)
Esempio n. 2
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()
Esempio n. 3
0
File: tox.py Progetto: qznc/err
 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)
Esempio n. 4
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()
Esempio n. 5
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()
Esempio n. 6
0
 def send_command(self, text):
     self.new_message(text, False)
     msg = Message(text)
     msg.setFrom(config.BOT_ADMINS[0])  # assume this is the admin talking
     msg.setTo(self.jid)  # To me only
     self.callback_message(self.conn, msg)
     self.input.clear()
Esempio n. 7
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()
Esempio n. 8
0
    def serve_forever(self):
        import config

        self.jid = 'Err@localhost'  # whatever
        self.connect()  # be sure we are "connected" before the first command
        self.connect_callback()  # notify that the connection occured
        try:
            while True:
                entry = incoming_message_queue.get()
                if entry == QUIT_MESSAGE:
                    logging.info("Stop magic message received, quitting...")
                    break
                msg = Message(entry)
                msg.setFrom(config.BOT_ADMINS[0])  # assume this is the admin talking
                msg.setTo(self.jid)  # To me only
                self.callback_message(self.conn, msg)
        except EOFError as _:
            pass
        except KeyboardInterrupt as _:
            pass
        finally:
            logging.debug("Trigger disconnect callback")
            self.disconnect_callback()
            logging.debug("Trigger shutdown")
            self.shutdown()
Esempio n. 9
0
File: test.py Progetto: J4LP/rooster
    def serve_forever(self):
        import config

        self.jid = 'Err@localhost'  # whatever
        self.connect()  # be sure we are "connected" before the first command
        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.setFrom(self.sender)
                    msg.setTo(self.jid)  # To me only
                    self.callback_message(self.conn, msg)
                elif stanza_type is STZ_PRE:
                    logging.info("Presence stanza received.")
                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()
Esempio n. 10
0
File: slack.py Progetto: 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)
Esempio n. 11
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()
Esempio n. 12
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)
Esempio n. 13
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, typ='groupchat')  # it is always a groupchat in campfire
         msg.setFrom(user + '@' + message.room.get_data()['name'] + '/' + user)
         msg.setTo(self.jid)  # assume it is for me
         self.callback_message(self.conn, msg)
Esempio n. 14
0
File: irc.py Progetto: 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)
Esempio n. 15
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)
Esempio n. 16
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)
Esempio n. 17
0
File: slack.py Progetto: 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)
Esempio n. 18
0
File: irc.py Progetto: P-Product/err
 def on_pubmsg(self, c, e):
     msg = Message(e.arguments[0])
     msg.setFrom(e.target)
     msg.setTo(self.callback.jid)
     msg.setMuckNick(e.source.split('!')[0])  # FIXME find the real nick in the channel
     msg.setType('groupchat')
     self.callback.callback_message(self, msg)
Esempio n. 19
0
File: irc.py Progetto: CiaranG/err
 def irc_PRIVMSG(self, prefix, params):
     fr, line = params
     if fr == self.nickname: # it is a private message
         fr = prefix.split('!')[0] # reextract the real from
         typ = 'chat'
     else:
         typ = 'groupchat'
     logging.debug('IRC message received from %s [%s]' % (fr, line))
     msg = Message(line, typ=typ)
     msg.setFrom(fr) # already a compatible format
     self.callback.callback_message(self, msg)
Esempio n. 20
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()
Esempio n. 21
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()
Esempio n. 22
0
    def irc_PRIVMSG(self, prefix, params):
        fr, line = params
        if fr == self.nickname:  # it is a private message
            from_identity = Identifier(node=self.extract_from_from_prefix(prefix))  # reextract the real from
            typ = 'chat'
        else:
            from_identity = Identifier(node=fr, resource=self.extract_from_from_prefix(prefix))  # So we don't loose the original sender and the chatroom
            typ = 'groupchat'
        logging.debug('IRC message received from %s [%s]' % (fr, line))

        msg = Message(line, typ=typ)
        msg.setFrom(from_identity)
        msg.setTo(params[0])
        self.callback.callback_message(self, msg)
Esempio n. 23
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])
Esempio n. 24
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()
Esempio n. 25
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)
    def build_reply(self, mess, text=None, private=False, threaded=False):
        """
        Create reply message object.

        Used by `self.send_simple_reply`.

        :param mess: The original message object.

        :param text: Reply message text.

        :param private: Whether the reply message is private.

        :return: Message object.
        """
        # Create reply message object
        reply = Message(
            body=text,
            frm=mess.to,
            to=mess.frm,
            extras={
                # 5QXGV
                # Store the original message object
                'orig_msg': mess
            })

        # Return reply message object
        return reply
Esempio n. 27
0
    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)
 def build_reply(self, msg, text=None, private=False, threaded=False):
     return Message(
         body=text,
         parent=msg,
         frm=msg.frm,
         to=msg.to,
         extras=msg.extras,
     )
 def build_reply(self, msg, text=None, private=False, threaded=False):
     log.debug("Calling self.build_reply")
     return Message(
         body=text,
         parent=msg,
         frm=msg.frm,
         to=msg.to,
         extras=msg.extras,
     )
Esempio n. 30
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)
Esempio n. 31
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)
Esempio n. 32
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()
Esempio n. 33
0
 def serve_forever(self):
     self.jid = 'Err@localhost'  # whatever
     self.connect()  # be sure we are "connected" before the first command
     self.connect_callback()  # notify that the connection occured
     try:
         while True:
             entry = input("Talk to  me >>")
             msg = Message(entry)
             msg.setFrom(config.BOT_ADMINS[0])  # assume this is the admin talking
             msg.setTo(self.jid)  # To me only
             self.callback_message(self.conn, msg)
     except EOFError as eof:
         pass
     except KeyboardInterrupt as ki:
         pass
     finally:
         logging.debug("Trigger disconnect callback")
         self.disconnect_callback()
         logging.debug("Trigger shutdown")
         self.shutdown()
Esempio n. 34
0
    def _handle_message(self, message):
        try:
            data = json.loads(message.data.decode('utf-8'))
        except Exception:
            log.warning('Received malformed message: {}'.format(message.data))
            message.ack()
            return

        if not data.get('message') or not data.get('message', {}).get('text'):
            message.ack()
            return
        sender_blob = data['message']['sender']
        sender = HangoutsChatUser(sender_blob['name'],
                                  sender_blob['displayName'],
                                  sender_blob['email'], sender_blob['type'])
        message_body = data['message']['text']
        message.ack()
        # message.ack() may fail silently, so we should ensure our messages are somewhat idempotent
        time = data.get('eventTime', 0)
        if time == 0:
            log.warning('Received 0 eventTime from message')

        send_name = sender_blob.get('name', '')
        thread_name = data.get('message', {}).get('thread', {}).get('name', '')
        body_length = len(message_body)
        message_id = "{}{}{}{}".format(time, send_name, thread_name,
                                       body_length)
        cached = self.message_cache.get(message_id)
        if cached is not None:
            return
        self.message_cache[message_id] = True

        context = {
            'space_id': data['space']['name'],
            'thread_id': data['message']['thread']['name']
        }
        msg = Message(body=message_body.strip(), frm=sender, extras=context)
        is_dm = data['message']['space']['type'] == 'DM'
        if is_dm:
            msg.to = self.bot_identifier
        self.callback_message(msg)
Esempio n. 35
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(console_hdlr)
            root.addHandler(logging.NullHandler())
        self.connect_callback()  # notify that the connection occured
        self.callback_presence(Presence(identifier=self.user, 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 = self.user
                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=self.user, status=OFFLINE))
            log.debug("Trigger disconnect callback")
            self.disconnect_callback()
            log.debug("Trigger shutdown")
            self.shutdown()
Esempio n. 36
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)
Esempio n. 37
0
 def _handle_message(self, message):
     data = json.loads(message.data.decode('utf-8'))
     if not data.get('message'):
         message.ack()
         return
     sender_blob = data['message']['sender']
     sender = HangoutsChatUser(sender_blob['name'], 
                               sender_blob['displayName'], 
                               sender_blob['email'],
                               sender_blob['type'])
     message_body = data['message']['text']
     message.ack()
     context = {
         'space_id': data['space']['name'],
         'thread_id': data['message']['thread']['name']
     }
     msg = Message(body=message_body.strip(), frm=sender, extras=context)
     is_dm = data['message']['space']['type'] == 'DM'
     if is_dm:
         msg.to = self.bot_identifier
     self.callback_message(msg)
Esempio n. 38
0
 def send_command(self, text):
     self.new_message(text, False)
     msg = Message(text)
     msg.setFrom(config.BOT_ADMINS[0])  # assume this is the admin talking
     msg.setTo(self.jid)  # To me only
     self.callback_message(self.conn, msg)
     self.input.clear()
Esempio n. 39
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
                ],
            )
Esempio n. 40
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.extras['url'] = 'https://gitter.im/%s?at=%s' % (
                            self.room.uri, m.extras['id'])
                    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: ')
Esempio n. 41
0
    def serve_forever(self):
        import config

        self.jid = 'Err@localhost'  # whatever
        self.connect()  # be sure we are "connected" before the first command
        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:
                entry = incoming_message_queue.get()
                if entry == QUIT_MESSAGE:
                    logging.info("Stop magic message received, quitting...")
                    break
                msg = Message(entry)
                msg.setFrom(self.sender)
                msg.setTo(self.jid)  # To me only
                self.callback_message(self.conn, msg)
        except EOFError as _:
            pass
        except KeyboardInterrupt as _:
            pass
        finally:
            logging.debug("Trigger disconnect callback")
            self.disconnect_callback()
            logging.debug("Trigger shutdown")
            self.shutdown()
Esempio n. 42
0
File: xmpp.py Progetto: 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)
Esempio n. 43
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)
Esempio n. 44
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()
Esempio n. 45
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)
Esempio n. 46
0
 def receive_from_chat():
     # Debug
     log.debug("received message | args:{0} | form:{1}".format(request.args, request.form))
     # Verify that the request contains the token, otherwise it's an unauthorized request
     if not request.form or not request.form['token'] or request.form['token'] != self.token_out:
         ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
         log.warning("unauthorized request: wrong or missing token | ip:{0} | args:{1} | form:{2}".format(ip, request.args, request.form))
         return "Unauthorized\n", 401 
     # Send to ErrBot
     msg = Message(body = request.form['text'],
                   frm  = SynologyChatUser(request.form['user_id'], request.form['username']),
                   to   = self.bot_identifier)
     self.callback_message(msg)
     # Send an empty reply to Synology Chat
     return "", 200
Esempio n. 47
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)
Esempio n. 48
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(.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()
Esempio n. 49
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)
Esempio n. 50
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)
Esempio n. 51
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)
Esempio n. 52
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)
 def test_send_stale_message_to_approvers_channel(self, mocked_testbot):
     accessbot = mocked_testbot.bot.plugin_manager.plugins['AccessBot']
     sender_id = accessbot.build_identifier(
         accessbot.config['SENDER_EMAIL_OVERRIDE'])
     sender_id.room = create_room_mock(self.regular_channel_name)
     accessbot.enter_grant_request(
         access_request_id, Message(frm=sender_id),
         DummyResource('resource',
                       {'approvers-channel': self.approvers_channel_name}),
         MagicMock(), MagicMock())
     assert access_request_id in accessbot.get_grant_request_ids()
     assert "timed out" in mocked_testbot.pop_message()
     assert "not approved" in mocked_testbot.pop_message()
     assert self.raw_messages[
         0].to.person == f"#{self.approvers_channel_name}"
     assert self.raw_messages[
         1].to.person == f"#{self.regular_channel_name}"
    def test_when_handling_messages_from_a_channel(self, mocked_testbot):
        accessbot = mocked_testbot.bot.plugin_manager.plugins['AccessBot']
        sender_id = accessbot.build_identifier(
            accessbot.config['SENDER_EMAIL_OVERRIDE'])
        sender_id.room = create_room_mock(self.channel_name)
        accessbot.enter_grant_request(access_request_id,
                                      Message(frm=sender_id), MagicMock(),
                                      MagicMock(), MagicMock())
        assert access_request_id in accessbot.get_grant_request_ids()

        PollerHelper(accessbot).stale_grant_requests_cleaner()

        assert access_request_id not in accessbot.get_grant_request_ids()
        assert "timed out" in mocked_testbot.pop_message()
        assert "not approved" in mocked_testbot.pop_message()
        assert self.raw_messages[0].to.person == self.sdm_admin
        assert self.raw_messages[1].to.person == f"#{self.channel_name}"
Esempio n. 55
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)
    def test_when_handling_direct_messages(self, mocked_testbot):
        os.environ['SDM_ADMINS'] = self.sdm_admin

        accessbot = mocked_testbot.bot.plugin_manager.plugins['AccessBot']
        sender_id = accessbot.build_identifier(
            accessbot.config['SENDER_EMAIL_OVERRIDE'])
        accessbot.enter_grant_request(access_request_id,
                                      Message(frm=sender_id), MagicMock(),
                                      MagicMock(), MagicMock())
        assert access_request_id in accessbot.get_grant_request_ids()

        PollerHelper(accessbot).stale_grant_requests_cleaner()

        assert access_request_id not in accessbot.get_grant_request_ids()
        assert "timed out" in mocked_testbot.pop_message()
        assert "not approved" in mocked_testbot.pop_message()
        assert self.raw_messages[0].to.person == self.sdm_admin
        assert self.raw_messages[1].to.person == self.sdm_admin
Esempio n. 57
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,
             typ='groupchat')  # it is always a groupchat in campfire
         msg.setFrom(user + '@' + message.room.get_data()['name'] + '/' +
                     user)
         msg.setTo(self.jid)  # assume it is for me
         self.callback_message(self.conn, msg)
Esempio n. 58
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])
Esempio n. 59
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)