def run(self, event: EventMessage) -> EventMessage: # Handy variables hallo_obj = event.server.hallo destination = event.user if event.channel is None else event.channel # Clean up input clean_input = event.command_args.strip().lower() # Acquire lock sub_repo = self.get_sub_repo(hallo_obj) with sub_repo.sub_lock: # Check whether input is asking to update all subscriptions if clean_input in self.NAMES_ALL or clean_input == "": matching_subs = sub_repo.sub_list else: # Otherwise see if a search subscription matches the specified one matching_subs = sub_repo.get_subs_by_name( clean_input, destination) if len(matching_subs) == 0: return event.create_response( "Error, no subscriptions match that name.") # Loop through matching search subscriptions, getting updates update = False for search_sub in matching_subs: try: update = search_sub.update() or update except Exception as e: error = SubscriptionCheckError(search_sub, e) logger.error(error.get_log_line(), exc_info=e) # Save list sub_repo.save_json() # Output response to user if not update: return event.create_response( "There were no updates for specified subscriptions.") return event.create_response(f"Subscription updates were found.")
def run(self, event: EventMessage) -> EventMessage: # Handy variables server = event.server hallo_obj = server.hallo function_dispatcher = hallo_obj.function_dispatcher sub_check_function = function_dispatcher.get_function_by_name( "check subscription" ) sub_check_obj = function_dispatcher.get_function_object( sub_check_function ) # type: hallo.modules.new_subscriptions.subscription_check.SubscriptionCheck sub_repo = sub_check_obj.get_sub_repo(hallo_obj) # Find list of feeds for current channel. with sub_repo.sub_lock: dest_searches: List[hallo.modules.subscriptions.subscription.Subscription] = \ sub_repo.get_subs_by_destination( event.user if event.channel is None else event.channel ) if len(dest_searches) == 0: return event.create_response( "There are no subscriptions posting to this destination." ) sub_names = [] for search_item in dest_searches: new_line = f"{search_item.source.type_name} - {search_item.source.title}" if search_item.last_update is not None: new_line += f" ({search_item.last_update.isoformat()})" sub_names.append(new_line) sub_names.sort() return event.create_response( "Subscriptions posting to this channel:\n" + "\n".join(sub_names) )
def run(self, event: EventMessage) -> EventMessage: # Handy variables server = event.server hallo_obj = server.hallo function_dispatcher = hallo_obj.function_dispatcher sub_check_function = function_dispatcher.get_function_by_name( "check subscription") sub_check_obj: hallo.modules.subscriptions.subscription_check.SubscriptionCheck = \ function_dispatcher.get_function_object( sub_check_function ) sub_repo = sub_check_obj.get_sub_repo(hallo_obj) # Clean up input clean_input = event.command_args.strip() # Acquire lock with sub_repo.sub_lock: # Find any feeds with specified title test_subs: List[hallo.modules.subscriptions.subscription. Subscription] = sub_repo.get_subs_by_name( clean_input.lower(), event.user if event.channel is None else event.channel, ) if len(test_subs) > 0: for del_sub in test_subs: sub_repo.remove_sub(del_sub) title_line = f"Removed {len(test_subs)} subscriptions:" if len(test_subs) == 1: title_line = "Removed subscription:" sub_lines = "\n".join([ f"{del_sub.source.type_name} - {del_sub.source.title}" for del_sub in test_subs ]) return event.create_response(f"{title_line}\n{sub_lines}") return event.create_response( "Error, there are no subscriptions in this channel matching that name." )
def run(self, event: EventMessage) -> EventMessage: # Construct type name sub_type_name = " ".join( [ w for w in event.command_name.lower().split() if w not in self.sub_words + self.add_words ] ).strip() # Get class from sub type name source_class = hallo.modules.subscriptions.subscription_factory.SubscriptionFactory.get_source_class_by_name( sub_type_name ) # Get subscription repo function_dispatcher = event.server.hallo.function_dispatcher # TODO: check if I need to do this? sub_check_class = function_dispatcher.get_function_by_name("check subscription") sub_check_obj = function_dispatcher.get_function_object( sub_check_class ) # type: hallo.modules.new_subscriptions.subscription_check.SubscriptionCheck sub_repo = sub_check_obj.get_sub_repo(event.server.hallo) # Create new subscription sub_obj = hallo.modules.subscriptions.subscription.Subscription.create_from_input( event, source_class, sub_repo ) # Test subscription sub_obj.update(send=False) # Acquire lock and save sub with sub_repo.sub_lock: # Add new subscription to list sub_repo.add_sub(sub_obj) # Save list sub_repo.save_json() # Send response return event.create_response( f"Created a new {source_class.type_name} subscription for {sub_obj.source.title}" )
def dispatch(self, event: EventMessage, flag_list: List[str] = None) -> None: """ Sends the function call out to whichever function, if one is found :param event: The message event which has triggered the function dispatch :param flag_list: List of flags to apply to function call """ if flag_list is None: flag_list = [] # Find the function name. Try joining each amount of words in the message until you find a valid function name function_message_split = event.command_text.split() if not function_message_split: function_message_split = [""] function_class_test = None function_name_test = "" function_args_test = "" for function_name_test in [ " ".join(function_message_split[:x + 1]) for x in range(len(function_message_split))[::-1] ]: function_class_test = self.get_function_by_name(function_name_test) function_args_test = " ".join( function_message_split)[len(function_name_test):].strip() if function_class_test is not None: break # If function isn't found, output a not found message if function_class_test is None: if EventMessage.FLAG_HIDE_ERRORS not in flag_list: event.reply( event.create_response( "Error, this is not a recognised function.")) error = FunctionNotFoundError(self, event) logger.error(error.get_log_line()) return function_class = function_class_test event.split_command_text(function_name_test, function_args_test) # Check function rights and permissions if not self.check_function_permissions(function_class, event.server, event.user, event.channel): event.reply( event.create_response( "You do not have permission to use this function.")) error = FunctionNotAllowedError(self, function_class, event) logger.error(error.get_log_line()) return # If persistent, get the object, otherwise make one function_obj = self.get_function_object(function_class) # Try running the function, if it fails, return an error message try: response = function_obj.run(event) if response is not None: event.reply(response) else: event.reply( event.create_response("The function returned no value.")) return except Exception as e: error = FunctionError(e, self, function_obj, event) e_str = (str(e)[:250] + "..") if len(str(e)) > 250 else str(e) event.reply( event.create_response( "Function failed with error message: {}".format(e_str))) logger.error(error.get_log_line()) return