def test_string_to_bool(self): true_list = ['1', 'true', 't', 'yes', 'y', 'on', 'enabled', 'enable'] for true_str in true_list: assert Commons.string_to_bool(true_str) false_list = ['0', 'false', 'f', 'no', 'n', 'off', 'disabled', 'disable'] for false_str in false_list: assert not Commons.string_to_bool(false_str) invalid_list = ["hello", "example", "test"] for invalid_str in invalid_list: assert Commons.string_to_bool(invalid_str) is None
def run(self, line, user_obj, destination_obj=None): line_split = line.split() if len(line_split) < 3: return "Error, you need to specify a location, a right and the value" bool_input = line_split[-1] right_input = line_split[-2] location_input = line_split[:-2] # Search for the permission_mask they want. try: permission_mask = self.find_permission_mask(location_input, user_obj, destination_obj if destination_obj.is_channel() else None) # If it comes back with an error message, return that error except PermissionControlException as e: return str(e) # If it comes back unspecified, generic error message if permission_mask is None: return "Error, I can't find that permission mask. Specify which you wish to modify as user={username}, " \ "or similarly for usergroup, channel, server or hallo." # Turn bool_input into a boolean bool_bool = Commons.string_to_bool(bool_input) # Check if boolean input is valid if bool_bool is None: return "Error, I don't understand your boolean value. Please use true or false." # Set the right permission_mask.set_right(right_input, bool_bool) return "Set "+right_input+" to "+{True: "true", False: "false"}[bool_bool]+"."
def run(self, event): line_split = event.command_args.split() if len(line_split) < 3: return event.create_response("Error, you need to specify a location, a right and the value") bool_input = line_split[-1] right_input = line_split[-2] location_input = line_split[:-2] # Search for the permission_mask they want. try: permission_mask = self.find_permission_mask(location_input, event.user, event.channel) # If it comes back with an error message, return that error except PermissionControlException as e: return event.create_response(str(e)) # If it comes back unspecified, generic error message if permission_mask is None: return event.create_response("Error, I can't find that permission mask. " + "Specify which you wish to modify as user={username}, " + "or similarly for usergroup, channel, server or hallo.") # Turn bool_input into a boolean bool_bool = Commons.string_to_bool(bool_input) # Check if boolean input is valid if bool_bool is None: return event.create_response("Error, I don't understand your boolean value. Please use true or false.") # Set the right permission_mask.set_right(right_input, bool_bool) return event.create_response("Set {} to {}.".format(right_input, "true" if bool_bool else "false"))
def edit_server_irc(self, line, server_obj, user_obj, destination_obj): """Processes arguments in order to edit an IRC server""" # Set all variables to none as default server_address, server_port = None, None # Find the URL, if specified url_regex = re.compile("(^|\s)(irc://)?(([a-z.]+\.[a-z]+)(:([0-9]+))?)(\s|$)", re.IGNORECASE) url_search = url_regex.search(line) if url_search is not None: line = line.replace(url_search.group(0), " ") server_address = url_search.group(4).lower() server_port = int(url_search.group(6)) # Find the server_address, if specified with equals notation server_address = Commons.find_parameter("server_address", line) or server_address # Find the server_port, if specified with equals notation server_port_param = Commons.find_parameter("server_port", line) if server_port_param is not None: try: server_port = int(server_port_param) except ValueError: return "Invalid port number." # If server_address or server_port are set, edit those and reconnect. if server_address is not None: server_obj.server_address = server_address if server_port is not None: server_obj.server_port = server_port # Get other parameters, if set. defaulting to whatever server defaults. auto_connect = Commons.string_to_bool(Commons.find_parameter("auto_connect", line)) or server_obj.get_auto_connect() server_nick = Commons.find_parameter("server_nick", line) or Commons.find_parameter("nick", line) or server_obj.get_nick() server_prefix = Commons.find_parameter("server_prefix", line) or Commons.find_parameter("prefix", line) or server_obj.get_prefix() full_name = Commons.find_parameter("full_name", line) or server_obj.get_full_name() nickserv_nick = Commons.find_parameter("nickserv_nick", line) or server_obj.get_nickserv_nick() nickserv_identity_command = Commons.find_parameter("nickserv_identity_command", line) or server_obj.get_nickserv_ident_command() nickserv_identity_response = Commons.find_parameter("nickserv_identity_response", line) or server_obj.get_nickserv_ident_response() nickserv_password = Commons.find_parameter("nickserv_password", line) or server_obj.get_nickserv_pass() # Set all the new variables server_obj.set_auto_connect(auto_connect) server_obj.set_nick(server_nick) server_obj.set_prefix(server_prefix) server_obj.set_full_name(full_name) server_obj.set_nickserv_nick(nickserv_nick) server_obj.set_nickserv_ident_command(nickserv_identity_command) server_obj.set_nickserv_ident_response(nickserv_identity_response) server_obj.get_nickserv_pass(nickserv_password) # If server address or server port was changed, reconnect. if server_port is not None or server_address is not None: server_obj.reconnect() return "Modified the IRC server: " + server_obj.get_name() + "."
def run(self, line, user_obj, destination_obj=None): # Get server object server_obj = user_obj.server # If no arguments given, toggle passive functions in current destination line_clean = line.strip() if line_clean == '': destination_obj.passive_enabled = not destination_obj.passive_enabled return "Passive functions toggled." # If line has 1 argument, line_split = line_clean.split() if len(line_split) == 1: # Check if a boolean was specified input_bool = Commons.string_to_bool(line_split[0]) if input_bool is not None: destination_obj.passive_enabled = input_bool return "Passive functions set " + {False: 'disabled', True: 'enabled'}[input_bool] + "." # Check if a channel was specified target_channel = server_obj.get_channel_by_name(line_split[0]) if target_channel.in_channel: target_channel.passive_enabled = not target_channel.passive_enabled return "Passive functions toggled in " + target_channel.get_name() + "." # Otherwise input unknown return "Error, I don't understand your input, please specify a channel and whether to turn passive " \ "functions on or off." # Otherwise line has 2 or more arguments. # Check if first argument is boolean input_bool = Commons.string_to_bool(line_split[0]) target_channel_name = line_split[1] if input_bool is None: input_bool = Commons.string_to_bool(line_split[1]) target_channel_name = line_split[0] if input_bool is None: return "Error, I don't understand your input, please specify a channel and whether to turn passive " \ "functions on or off." target_channel = server_obj.get_channel_by_name(target_channel_name) if not target_channel.in_channel: return "Error, I'm not in that channel." target_channel.passive_enabled = input_bool return "Passive functions set " + {False: 'disabled', True: 'enabled'}[ input_bool] + " in " + target_channel.name + "."
def run(self, line, user_obj, destination_obj=None): # Get server object server_obj = user_obj.get_server() # If no arguments given, toggle caps lock in current destination line_clean = line.strip() if line_clean == '': destination_obj.set_upper_case(not destination_obj.is_upper_case()) return "Caps lock toggled." # If line has 1 argument, line_split = line_clean.split() if len(line_split) == 1: # Check if a boolean was specified input_bool = Commons.string_to_bool(line_split[0]) if input_bool is not None: destination_obj.set_upper_case(input_bool) return "Caps lock set " + {False: 'off', True: 'on'}[input_bool] + "." # Check if a channel was specified target_channel = server_obj.get_channel_by_name(line_split[0]) if target_channel.is_in_channel(): target_channel.set_upper_case(not target_channel.is_upper_case()) return "Caps lock toggled in " + target_channel.get_name() + "." # Otherwise input unknown return "Error, I don't understand your input, please specify a channel and whether to turn caps lock " \ "on or off." # Otherwise line has 2 or more arguments. # Check if first argument is boolean input_bool = Commons.string_to_bool(line_split[0]) target_channel_name = line_split[1] if input_bool is None: input_bool = Commons.string_to_bool(line_split[1]) target_channel_name = line_split[0] if input_bool is None: return "Error, I don't understand your input, please specify a channel and whether to turn " \ "caps lock on or off." target_channel = server_obj.get_channel_by_name(target_channel_name) if target_channel is None or not target_channel.is_in_channel(): return "Error, I'm not in that channel." target_channel.set_upper_case(input_bool) return "Caps lock set " + {False: 'off', True: 'on'}[input_bool] + " in " + target_channel.get_name() + "."
def run(self, line, user_obj, destination_obj=None): # Load XML doc = minidom.parse("store/pony/pony_episodes.xml") pony_episodes_list_elem = doc.getElementsByTagName("pony_episodes")[0] episode_list = [] song_list = [] # Loop through pony episodes, adding to episode_list (or song_list, if there is a song) for pony_episode_elem in pony_episodes_list_elem.getElementsByTagName("pony_episode"): episode_dict = {'name': pony_episode_elem.getElementsByTagName("name")[0].firstChild.data, 'full_code': pony_episode_elem.getElementsByTagName("full_code")[0].firstChild.data} if Commons.string_to_bool(pony_episode_elem.getElementsByTagName("song")[0].firstChild.data): song_list.append(episode_dict) episode_list.append(episode_dict) # If song, get episode from song list, otherwise get one from episode list if line.strip().lower() != "song": episode = Commons.get_random_choice(episode_list)[0] else: episode = Commons.get_random_choice(song_list)[0] # Return output return "You should choose: " + episode['full_code'] + " - " + episode['name'] + "."
def connect_to_new_server_irc(self, line, user_obj, destination_obj): """ Processes arguments in order to connect to a new IRC server :type line: str :type user_obj: Destination.User :type destination_obj: Destination.Destination """ # Get some handy objects current_server = user_obj.get_server() hallo_obj = current_server.get_hallo() # Set all variables to none as default server_address, server_port = None, None server_name = None # Find the URL, if specified url_regex = re.compile("(^|\s)(irc://)?(([a-z.]+\.[a-z]+)(:([0-9]+))?)(\s|$)", re.IGNORECASE) url_search = url_regex.search(line) if url_search is not None: line = line.replace(url_search.group(0), " ") server_address = url_search.group(4).lower() try: server_port = int(url_search.group(6)) except (ValueError, TypeError): server_port = None # Find the server_address, if specified with equals notation server_address = Commons.find_parameter("server_address", line) or server_address # Find the server_port, if specified with equals notation server_port_param = Commons.find_parameter("server_port", line) if server_port_param is not None: try: server_port = int(server_port_param) except (ValueError, TypeError): return "Error, invalid port number." # Check server_address and server_port are set if server_address is None: return "Error, No server address specified." if server_port is None and isinstance(current_server, ServerIRC): server_port = current_server.get_server_port() if server_port is None: return "Error, No server port specified." # Get server name server_name = Commons.find_any_parameter(["server_name", "name"], line) or server_name # if server name is null, get it from server_address if server_name is None: server_name = Commons.get_domain_name(server_address) # Get other parameters, if set. auto_connect_str = Commons.find_parameter("auto_connect", line) auto_connect = True if auto_connect_str is None else Commons.string_to_bool(auto_connect_str) server_nick = Commons.find_any_parameter(["server_nick", "nick"], line) or current_server.get_nick() server_prefix_arg = Commons.find_any_parameter(["server_prefix", "prefix"], line) if not server_prefix_arg: server_prefix = current_server.prefix else: server_prefix = None if Commons.is_string_null(server_prefix_arg) else server_prefix_arg full_name = Commons.find_parameter("full_name", line) or current_server.get_full_name() nickserv_nick = "nickserv" nickserv_identity_command = "status" nickserv_identity_resp = "^status [^ ]+ 3$" nickserv_password = None if isinstance(current_server, ServerIRC): nickserv_nick = current_server.get_nickserv_nick() nickserv_identity_command = current_server.get_nickserv_ident_command() nickserv_identity_resp = current_server.get_nickserv_ident_response() nickserv_password = current_server.get_nickserv_pass() nickserv_nick = Commons.find_parameter("nickserv_nick", line) or nickserv_nick nickserv_identity_command = Commons.find_parameter("nickserv_identity_command", line) or nickserv_identity_command nickserv_identity_resp = Commons.find_parameter("nickserv_identity_resp", line) or nickserv_identity_resp nickserv_password = Commons.find_parameter("nickserv_password", line) or nickserv_password # Create this serverIRC object new_server_obj = ServerIRC(hallo_obj, server_name, server_address, server_port) new_server_obj.set_auto_connect(auto_connect) new_server_obj.set_nick(server_nick) new_server_obj.set_prefix(server_prefix) new_server_obj.set_full_name(full_name) new_server_obj.set_nickserv_nick(nickserv_nick) new_server_obj.set_nickserv_ident_command(nickserv_identity_command) new_server_obj.set_nickserv_ident_response(nickserv_identity_resp) new_server_obj.set_nickserv_pass(nickserv_password) # Add user with same name on new server to all the same groups as current user new_user_nick = Commons.find_any_parameter(["user", "god"], line) or user_obj.get_name() new_user = new_server_obj.get_user_by_name(new_user_nick) for group in user_obj.user_group_list: new_user.add_user_group(group) # Add the new object to Hallo's list hallo_obj.add_server(new_server_obj) # Connect to the new server object. Thread(target=new_server_obj.run).start() return "Connected to new IRC server: " + new_server_obj.get_name() + "."