def handle_membership_change(bot, event): """Handle conversation membership change""" # Test if watching for membership changes is enabled if not bot.get_config_suboption(event.conv_id, 'membership_watching_enabled'): return # Generate list of added or removed users event_users = [event.conv.get_user(user_id) for user_id in event.conv_event.participant_ids] names = ', '.join([user.full_name for user in event_users]) # JOIN if event.conv_event.type_ == hangups.MEMBERSHIP_CHANGE_TYPE_JOIN: # Test if user who added new participants is admin admins_list = bot.get_config_suboption(event.conv_id, 'admins') if event.user_id.chat_id in admins_list: yield from event.conv.send_message( text_to_segments(_('{}: Welcome!').format(names)) ) else: text = _( '**!!! WARNING !!!**\n' '{} invited user {} without authorization!\n' '\n' '{}: Please leave this conversation immediately!' ).format(event.user.full_name, names, names) bot.send_message(event.conv, text) # LEAVE else: bot.send_message(event.conv, ('{}, I never liked you anyway!').format(names))
def conv_send(bot, event, conv_name, *args): """Send message to conversation as bot (use . for current conversation) Usage: /bot conv_send conversation_name text""" conv_name = strip_quotes(conv_name) convs = [event.conv] if conv_name == '.' else bot.find_conversations(conv_name) for c in convs: yield from c.send_message(text_to_segments(' '.join(args)))
def spoof(bot, event, *args): """Spoof IngressBot on specified GPS coordinates Usage: /bot spoof latitude,longitude [hack|fire|deploy|mod] [level] [count]""" link = "https://plus.google.com/u/0/{}/about".format(event.user.id_.chat_id) text = _("**!!! WARNING !!!**\n" "Agent {} ({}) has been reported to Niantic for attempted spoofing!").format( event.user.full_name, link ) yield from event.conv.send_message(text_to_segments(text))
def quit(bot, event, *args): """Oh my God! They killed Kenny! You bastards!""" print(_('Kevbot killed by user {} from conversation {}').format( event.user.full_name, get_conv_name(event.conv, truncate=True) )) yield from event.conv.send_message(text_to_segments(_('Et tu, Brute?'))) yield from bot._client.disconnect()
def conv_leave(bot, event, conv_name='', *args): """Leave current (or specified) conversation Usage: /bot conv_leave [conversation_name]""" conv_name = strip_quotes(conv_name) convs = [event.conv] if not conv_name or conv_name == '.' else bot.find_conversations(conv_name) for c in convs: yield from c.send_message(text_to_segments(_('I\'ll be back!'))) yield from c.leave()
def handle_command(bot, event): """Handle command messages""" # Test if message is not empty if not event.text: return # Get list of bot aliases aliases_list = bot.get_config_suboption(event.conv_id, 'commands_aliases') if not aliases_list: aliases_list = [default_bot_alias] # Test if message starts with bot alias if not find_bot_alias(aliases_list, event.text): return # Test if command handling is enabled if not bot.get_config_suboption(event.conv_id, 'commands_enabled'): raise StopEventHandling # Parse message line_args = shlex.split(event.text, posix=False) # Test if command length is sufficient if len(line_args) < 2: yield from event.conv.send_message( text_to_segments(_('{}: How may I serve you?').format(event.user.full_name)) ) raise StopEventHandling # Test if user has permissions for running command commands_admin_list = command.get_admin_commands(bot, event.conv_id) if commands_admin_list and line_args[1].lower() in commands_admin_list: admins_list = bot.get_config_suboption(event.conv_id, 'admins') if event.user_id.chat_id not in admins_list: yield from event.conv.send_message( text_to_segments(_('{}: I\'m sorry, I\'m afraid I can\'t do that.').format(event.user.full_name)) ) raise StopEventHandling # Run command yield from command.run(bot, event, *line_args[1:]) # Prevent other handlers from processing event raise StopEventHandling
def conv_refresh(bot, event, conv_name, *args): """Create new conversation with same users as in old one except kicked users (use . for current conversation) Usage: /bot conv_refresh conversation_name [kicked_user_name_1] [kicked_user_name_2] [...]""" conv_name = strip_quotes(conv_name) convs = [event.conv] if conv_name == '.' else bot.find_conversations(conv_name) kicked_chat_ids = get_unique_users(bot, args) for c in convs: new_chat_ids = {u for u in bot.find_users('', conv=c) if u.id_.chat_id not in set(kicked_chat_ids)} invitee_ids = [InviteeID( gaia_id=u.id_.gaia_id, fallback_name=u.full_name ) for u in new_chat_ids] # Create new conversation request = hangouts_pb2.CreateConversationRequest( request_header=bot._client.get_request_header(), type=hangouts_pb2.CONVERSATION_TYPE_GROUP, client_generated_id=bot._client.get_client_generated_id(), name=c.name, invitee_id=invitee_ids ) res = yield from bot._client.create_conversation(request) conv_id = res.conversation.conversation_id.id bot._conv_list.add_conversation(res.conversation) conv = bot._conv_list.get(conv_id) yield from conv.rename(c.name) yield from conv.send_message( text_to_segments(('**Welcome!**\n' 'This is the new refreshed conversation. Old conversation has been ' 'terminated, but you are one of the lucky ones who survived cleansing! ' 'If you are still in old conversation, please leave it.')) ) # Destroy old one and leave it yield from c.rename(('[TERMINATED] {}').format(c.name)) yield from c.send_message( text_to_segments(('**!!! WARNING !!!**\n' 'This conversation has been terminated! Please leave immediately!')) ) yield from c.leave()
def conv_list(bot, event, conv_name='', *args): """List all conversations where bot is wreaking havoc Usage: /bot conv_list [conversation_name] Legend: c ... commands, f ... forwarding, a ... autoreplies""" conv_name = strip_quotes(conv_name) convs = bot.list_conversations() if not conv_name else bot.find_conversations(conv_name) convs_text = [] for c in convs: s = '{} [c: {:d}, f: {:d}, a: {:d}]'.format( get_conv_name(c, truncate=True), bot.get_config_suboption(c.id_, 'commands_enabled'), bot.get_config_suboption(c.id_, 'forwarding_enabled'), bot.get_config_suboption(c.id_, 'autoreplies_enabled') ) convs_text.append(s) text = _('**Active conversations:**\n' '{}').format('\n'.join(convs_text)) yield from event.conv.send_message(text_to_segments(text))
def help(bot, event, cmd=None, *args): """Help me, Obi-Wan Kenobi. You're my only hope. Usage: /bot help [command]""" cmd = cmd if cmd else 'help' try: command_fn = command.commands[cmd] except KeyError: yield from command.unknown_command(bot, event) return text = _('**{}:**\n' '{}').format(cmd, _(command_fn.__doc__)) if cmd == 'help': text += _('\n\n' '**Supported commands:**\n' '{}').format(', '.join(sorted(command.commands.keys()))) yield from event.conv.send_message(text_to_segments(text))
def handle_forward(bot, event): """Handle message forwarding""" # Test if message is not empty if not event.text: return # Test if message forwarding is enabled if not bot.get_config_suboption(event.conv_id, 'forwarding_enabled'): return # Test if there are actually any forwarding destinations forward_to_list = bot.get_config_suboption(event.conv_id, 'forward_to') if not forward_to_list: return # Prepare attachments image_id_list = yield from bot.upload_images(event.conv_event.attachments) # Forward message to all destinations for dst in forward_to_list: try: conv = bot._conv_list.get(dst) except KeyError: continue # Prepend forwarded message with name of sender link = 'https://plus.google.com/u/0/{}/about'.format(event.user_id.chat_id) segments = text_to_segments('**[{}]({}):** '.format(event.user.full_name, link)) # Copy original message segments segments.extend(event.conv_event.segments) # Send text message first (without attachments) yield from conv.send_message(segments) # If there are attachments, send them separately for image_id in image_id_list: yield from conv.send_message([], image_id=image_id)
def conv_create(bot, event, conv_name, *args): """Create new conversation and invite users to it Usage: /bot conv_create conversation_name [user_name_1] [user_name_2] [...]""" conv_name = strip_quotes(conv_name) unique_user_objects = get_unique_user_objects(bot, args) if not unique_user_objects: yield from command.unknown_command(bot, event) return invitee_ids = [InviteeID( gaia_id=u.id_.gaia_id, fallback_name=u.full_name ) for u in unique_user_objects] request = hangouts_pb2.CreateConversationRequest( request_header=bot._client.get_request_header(), type=hangouts_pb2.CONVERSATION_TYPE_GROUP, client_generated_id=bot._client.get_client_generated_id(), name=conv_name, invitee_id=invitee_ids ) res = yield from bot._client.create_conversation(request) conv = bot._conv_list.add_conversation(res.conversation) yield from conv.rename(conv_name) yield from conv.send_message(text_to_segments(('Welcome!')))
def send_message(self, conversation, text): """Send simple chat message""" self.send_message_segments(conversation, text_to_segments(text))
def unknown_command(bot, event, *args): """Unknown command handler""" yield from event.conv.send_message( text_to_segments(_('{}: Unknown command!').format(event.user.full_name)) )
def echo(bot, event, *args): """Monkey see, monkey do! Usage: /bot echo text""" yield from event.conv.send_message(text_to_segments(' '.join(args)))
def ping(bot, event, *args): """Let's play ping pong!""" yield from event.conv.send_message(text_to_segments('pong'))