def test_is_registered(self): from functions import is_registered checked_nick = 'foobaz' s = Mock() s.send = Mock() s.recv.return_value = 'NickServ Last seen : now' self.assertTrue(is_registered(s, checked_nick)) s.recv.return_value = 'NickServ' self.assertFalse(is_registered(s, checked_nick)) response = ['baz', 'NickServ Information on:', 'NickServ', 'foo'] # side_effect will return the next item from the list which will be # assigned to is_registered.receive this way I'm able to test the # pass branch in the function s.recv.side_effect = response self.assertFalse(is_registered(s, checked_nick))
def channels(socket, components): # !channels 'Returns a string containing the channels the bot is connected to' response = '' if components['arguments'] == '!channels': # the user sent just the command, no garbage if components['sender'] in config.owner and \ is_registered(socket, components['sender']): # this command can be run only by the owners response = ', '.join(config.channels) response = config.current_nick + ' is connected to: ' + response else: response = 'This command can be run only by the owners!' return response
def channels(socket, components): # !channels '''Returns a string containing the channels the bot is connected to''' response = '' if components['arguments'] == '!channels': # the user sent just the command, no garbage if components['sender'] in config.owner and \ is_registered(socket, components['sender']): # this command can be run only by the owners response = ', '.join(config.channels) response = config.current_nick + ' is connected to: ' + response else: response = 'This command can be run only by the owners!' return response
def add(socket, components): if not components['sender'] in config.owner or not is_registered(socket, components['sender']): return 'This command can be run only by the owners!'.encode('utf8') arguments = components['arguments'].split('!add ')[1].split(':') phrase = arguments[0] lower_phrase = phrase.lower() value = arguments[1].lower() truth_value = False if value == 'honorable' or value == 'honourable': truth_value = True phrases = honor_functions.load_phrases() phrases[lower_phrase] = truth_value honor_functions.save_phrases(phrases) response = "'%s' added as '%s'" % (phrase, value) return response.encode('utf8')
def join(socket, components): # !join <#channel>+ """Returns a string for joining the given channel(s) Joins a list of channels, only if the sender is an owner """ response = "" join_command = components["arguments"].split("!join ") # notice the space if 2 == len(join_command): if components["sender"] in config.owner and is_registered(socket, components["sender"]): response = [] join_chans = [] response.append("JOIN ") arg_channels = join_command[1].lstrip().split(" ") for channel in arg_channels: channel = channel.strip("\r") if ( channel not in config.channels and len(channel) and "#" == channel[0] and -1 == channel.find(" ") ): # valid channel name join_chans.append(channel) config.channels.append(channel) if len(join_chans): response.append(",".join(join_chans)) response.append( "\r\nPRIVMSG " + components["action_args"][0] + " :Joined: {0}".format(", ".join(join_chans)) ) else: response = "Invalid channels names, usage: !join <#channel >+" else: response = "This command can be run only by the owners!" else: response = "Usage: !join <#channel >+" return response
def join(socket, components): # !join <#channel>+ '''Returns a string for joining the given channel(s) Joins a list of channels, only if the sender is an owner ''' response = '' join_command = components['arguments'].split('!join ') # notice the space if 2 == len(join_command): if components['sender'] in config.owner and is_registered( socket, components['sender']): response = [] join_chans = [] response.append('JOIN ') arg_channels = join_command[1].lstrip().split(' ') for channel in arg_channels: channel = channel.strip('\r') if channel not in config.channels and len(channel) and \ '#' == channel[0] \ and -1 == channel.find(' '): # valid channel name join_chans.append(channel) config.channels.append(channel) if len(join_chans): response.append(','.join(join_chans)) response.append('\r\nPRIVMSG ' + components['action_args'][0] + \ ' :Joined: {0}'.format(', '.join(join_chans))) else: response = 'Invalid channels names, usage: !join <#channel >+' else: response = 'This command can be run only by the owners!' else: response = 'Usage: !join <#channel >+' return response
def task(socket, components): response = '' registered = is_registered(socket, components['sender']) if components['sender'] in config.owner and registered: try: params = components['arguments'].split('!task ')[1] except IndexError: return 'Please specify a valid action: list, add, del' action_delimiter_position = params.find(' ') if -1 != action_delimiter_position: action = params[:action_delimiter_position] else: action = params if not is_valid_sqlite3(database_filename): return 'Invalid database!' conn = sqlite3.connect(database_filename) db = conn.cursor() is_user = user_exists(db, components['sender']) if is_user is None: return 'Error fetching the database!' if 'list' == action: if not is_user: return 'Could not retrieve any task list!' l = list_task(db, components['sender']) if not l: response = 'Could not retrieve any task list!' else: response = [] for t in l: response.append('PRIVMSG {0} :Task {1}: {2}\r\n'.format( components['action_args'][0], t[0], t[1])) elif 'add' == action: if not is_user and not create_user(db, components['sender']): return 'Error registering the user!' try: t = params.split(action)[1] except IndexError: return 'Please provide a task!' t = t.strip() if not t: return 'Please provide a task!' id = add_task(db, components['sender'], t) if id is False: return 'Error adding the task!' try: conn.commit() except sqlite3.Error: return 'Error adding the task!' response = 'Task with id {0} added!'.format(id) elif 'del' == action: if not is_user: return 'No tasks available!' try: id = params.split(action)[1] except IndexError: return 'Please provide a task id!' id = id.strip() if not id: return 'Please provide a task id!' no_deleted = del_task(db, components['sender'], id) if no_deleted is False: return 'An error occurred while deleting' try: conn.commit() except sqlite3.Error: return 'Error deleting the task!' if no_deleted == 1: response = 'Task with id {0} was deleted!'.format(id) else: response = 'No task with id {0} was found!'.format(id) else: response = 'Please specify a valid action: list, add, del' conn.close() else: response = 'This command can be run only by the owners!' return response