def _show_help(self, event, session=None): user = get_user_by_jid(event['from'].jid, session) self.xmpp.sendMessage(user.jid, self._COMMANDS_HELP, mfrom=self.jid, mtype='chat')
def _unfollow(self, event, username, session=None): user = get_user_by_jid(event['from'].jid, session) contact = get_user_by_username(username, session) if not contact: body = 'User @%s not found.' % username self.xmpp.sendMessage(event['from'].jid, body, mfrom=self.jid, mtype='chat') return for idx, u in enumerate(user.contacts): if u.username == contact.username: user.contacts.pop(idx) session.commit() self.xmpp.sendMessage(user.jid, 'You don\'t follow @%s anymore.' % username, mfrom=self.jid, mtype='chat') self.xmpp.sendMessage(contact.jid, 'You lost one of your followers: @%s.' % user.username, mfrom=self.jid, mtype='chat') return self.xmpp.sendMessage(user.jid, 'You don\'t folow @%s.' % username, mfrom=self.jid, mtype='chat')
def _remove_search(self, event, word, session=None): user = get_user_by_jid(event['from'].jid, session) search.remove_search(word, user.username) self.xmpp.sendMessage(user.jid, 'Search on "%s" was dropped' % word, mfrom=self.jid, mtype='chat')
def _unfollow(self, event, username, session=None): user = get_user_by_jid(event['from'].jid, session) contact = get_user_by_username(username, session) if not contact: body = 'User @%s not found.' % username self.xmpp.sendMessage(event['from'].jid, body, mfrom=self.jid, mtype='chat') return for idx, u in enumerate(user.contacts): if u.username == contact.username: user.contacts.pop(idx) session.commit() self.xmpp.sendMessage( user.jid, 'You don\'t follow @%s anymore.' % username, mfrom = self.jid, mtype = 'chat' ) self.xmpp.sendMessage( contact.jid, 'You lost one of your followers: @%s.' % user.username, mfrom = self.jid, mtype = 'chat' ) return self.xmpp.sendMessage( user.jid, 'You don\'t folow @%s.' % username, mfrom = self.jid, mtype = 'chat' )
def _show_searches(self, event, session=None): user = get_user_by_jid(event['from'].jid, session) terms = session.query(SearchTerm).filter( SearchTerm.username == user.username) if terms.count() > 0: body = 'Your searches:\n' + '\n'.join(t.term for t in terms) else: body = 'You have no searches.' self.xmpp.sendMessage(user.jid, body, mfrom=self.jid, mtype='chat')
def _reply_message(self, event, username, message, session=None): user = get_user_by_username(username, session) from_ = get_user_by_jid(event['from'].jid, session) if user: body = 'Reply from @%s: %s' % (from_.username, message) self.send_message(user.jid, body, mfrom=self.jid, mtype='chat', payload=event.payload) else: body = 'User @%s not found.' % username self.send_message(from_.jid, body, mfrom=self.jid, mtype='chat', payload=event.payload)
def _show_followers(self, event, session=None): user = get_user_by_jid(event['from'].jid, session) if user: followers = list(user.subscribers) if followers: body = 'Your followers are:\n' + '\n'.join(f.username for f in followers) else: body = 'You have no followers.' self.xmpp.sendMessage(user.jid, body, mfrom=self.jid, mtype='chat')
def _show_contacts(self, event, session=None): user = get_user_by_jid(event['from'].jid, session) if user: contacts = list(user.contacts) if contacts: body = 'Your contacts are:\n' + '\n'.join(f.username for f in contacts) else: body = 'You have no contacts.' self.xmpp.sendMessage(user.jid, body, mfrom=self.jid, mtype='chat')
def _show_searches(self, event, session=None): user = get_user_by_jid(event['from'].jid, session) terms = session.query(SearchTerm).filter(SearchTerm.username == user.username) if terms.count() > 0: body = 'Your searches:\n' + '\n'.join( t.term for t in terms ) else: body = 'You have no searches.' self.xmpp.sendMessage(user.jid, body, mfrom=self.jid, mtype='chat')
def _show_contacts(self, event, session=None): user = get_user_by_jid(event['from'].jid, session) if user: contacts = list(user.contacts) if contacts: body = 'Your contacts are:\n' + '\n'.join( f.username for f in contacts ) else: body = 'You have no contacts.' self.xmpp.sendMessage(user.jid, body, mfrom=self.jid, mtype='chat')
def _show_followers(self, event, session=None): user = get_user_by_jid(event['from'].jid, session) if user: followers = list(user.subscribers) if followers: body = 'Your followers are:\n' + '\n'.join( f.username for f in followers ) else: body = 'You have no followers.' self.xmpp.sendMessage(user.jid, body, mfrom=self.jid, mtype='chat')
def handle_new_message(self, event, session): text = event['body'] from_user = get_user_by_jid(event['from'].jid, session) text_len = len(text) if self.max_tweet_length and text_len > self.max_tweet_length: self.send_message( from_user.jid, 'Your message is longer than %s characters (%s chars). ' 'Your message has not been posted.' % (self.max_tweet_length, text_len), mfrom=self.jid, mtype='chat') return try: tweet = Tweet( username=from_user.username, text=text, ) session.add(tweet) except Exception: self.log.exception('can\'t save tweet') body = '@%s: %s' % (from_user.username, text) for subscriber in from_user.subscribers: self.send_message(subscriber.jid, body, mfrom=self.jid, mtype='chat', payload=event.payload) body = 'Mention by @%s: %s' % (from_user.username, text) for username in re.findall(r'\W@\w+', text): try: user = get_user_by_username(username[1:], session) except UserNotFound: continue if user not in from_user.subscribers: self.send_message(user.jid, body, mfrom=self.jid, mtype='chat', payload=event.payload) search.process_message(event)
def _add_search(self, event, word, session=None): user = get_user_by_jid(event['from'].jid, session) try: neightbours = search.add_search(word, user.username, max_neightbours=21) except IntegrityError: message = 'You already watching for these terms.' else: message = 'Now you are looking for "%s" in all messages.' % word if neightbours: message += '\nThese users are watching for the same terms:\n@' message += '\n@'.join(neightbours[:20]) if len(neightbours) == 21: message += '\nand more...' self.xmpp.sendMessage(user.jid, message, mfrom=self.jid, mtype='chat')
def _find_buddy_node(self): for node in self: if node.tag == '{http://cleartext.net/mblog}x': return node.find('{http://cleartext.net/mblog}buddy') # node not found, create one ns = '{http://cleartext.net/mblog}' user = get_user_by_jid(self._event['from'].jid, self._session) avatar_hash = self._bot.users[user.username].get('photo', '') x_e = ET.Element(ns + 'x') buddy_e = ET.SubElement(x_e, 'buddy', type='sender') ET.SubElement(buddy_e, 'displayName').text = \ user.vcard and unicode(user.vcard.NICKNAME) \ or user.username ET.SubElement(buddy_e, 'userName').text = user.username ET.SubElement(buddy_e, 'jid').text = user.jid ET.SubElement(buddy_e, 'avatar', type='hash').text = avatar_hash ET.SubElement(buddy_e, 'serviceJid').text = self._bot.jid self.append(x_e) return buddy_e
def _add_search(self, event, word, session=None): user = get_user_by_jid(event['from'].jid, session) try: neightbours = search.add_search(word, user.username, max_neightbours=21) except IntegrityError: message = 'You already watching for these terms.' else: message = 'Now you are looking for "%s" in all messages.' % word if neightbours: message += '\nThese users are watching for the same terms:\n@' message += '\n@'.join(neightbours[:20]) if len(neightbours) == 21: message += '\nand more...' self.xmpp.sendMessage( user.jid, message, mfrom = self.jid, mtype = 'chat' )
def _process_event(event, session=None): log.debug('Processing word "%s"' % event['body']) text = event['body'] from_user = get_user_by_jid(event['from'].jid, session) body = 'Search: @%s says "%s"' % (from_user.username, text) num_recipients = 0 text = text.lower() receivers = set() terms = {} # user to terms hash def all_in_text(words, text): return all(map(lambda word: word in text, words)) for words, users in _searches.items(): if all_in_text(words, text): receivers.update(users) for user in users: terms[user] = words for username in receivers: user = get_user_by_username(username, session) if user not in from_user.subscribers and \ user != from_user: payload = copy.deepcopy(event.payload) for term in terms[username]: payload.add_node('searchTerm', term) num_recipients += 1 bot.send_message(user.jid, body, mfrom=bot.jid, mtype='chat', payload=payload) log.debug('This message was received by %s recipients.' % num_recipients)
def _follow(self, event, username, session=None): user = get_user_by_jid(event['from'].jid, session) contact = get_user_by_username(username, session) if not contact: body = 'User @%s not found.' % username self.xmpp.sendMessage(event['from'].jid, body, mfrom=self.jid, mtype='chat') return if user == contact: body = 'You can\'t follow youself.' self.xmpp.sendMessage(event['from'].jid, body, mfrom=self.jid, mtype='chat') return if contact in user.contacts: self.xmpp.sendMessage( user.jid, 'You already follow @%s.' % username, mfrom = self.jid, mtype = 'chat' ) return user.contacts.append(contact) session.commit() self.xmpp.sendMessage( user.jid, 'Now you are following @%s.' % username, mfrom = self.jid, mtype = 'chat' ) self.xmpp.sendMessage( contact.jid, 'You have a new follower: @%s.' % user.username, mfrom = self.jid, mtype = 'chat' )
def _follow(self, event, username, session=None): user = get_user_by_jid(event['from'].jid, session) contact = get_user_by_username(username, session) if not contact: body = 'User @%s not found.' % username self.xmpp.sendMessage(event['from'].jid, body, mfrom=self.jid, mtype='chat') return if user == contact: body = 'You can\'t follow youself.' self.xmpp.sendMessage(event['from'].jid, body, mfrom=self.jid, mtype='chat') return if contact in user.contacts: self.xmpp.sendMessage(user.jid, 'You already follow @%s.' % username, mfrom=self.jid, mtype='chat') return user.contacts.append(contact) session.commit() self.xmpp.sendMessage(user.jid, 'Now you are following @%s.' % username, mfrom=self.jid, mtype='chat') self.xmpp.sendMessage(contact.jid, 'You have a new follower: @%s.' % user.username, mfrom=self.jid, mtype='chat')
def handle_new_message(self, event, session): text = event['body'] from_user = get_user_by_jid(event['from'].jid, session) text_len = len(text) if self.max_tweet_length and text_len > self.max_tweet_length: self.send_message( from_user.jid, 'Your message is longer than %s characters (%s chars). ' 'Your message has not been posted.' % (self.max_tweet_length, text_len), mfrom = self.jid, mtype = 'chat' ) return try: tweet = Tweet( username = from_user.username, text = text, ) session.add(tweet) except Exception: self.log.exception('can\'t save tweet') body = '@%s: %s' % (from_user.username, text) for subscriber in from_user.subscribers: self.send_message(subscriber.jid, body, mfrom=self.jid, mtype='chat', payload=event.payload) body = 'Mention by @%s: %s' % (from_user.username, text) for username in re.findall(r'\W@\w+', text): try: user = get_user_by_username(username[1:], session) except UserNotFound: continue if user not in from_user.subscribers: self.send_message(user.jid, body, mfrom=self.jid, mtype='chat', payload=event.payload) search.process_message(event)
def _whoami(self, event, session=None): user = get_user_by_jid(event['from'].jid, session) body = 'Username: %s\nJID: %s' % (user.username, user.jid) self.xmpp.sendMessage(user.jid, body, mfrom=self.jid, mtype='chat')