def _new_completion_args(self, word_list, argument_position=-1, add_after='', quoted=True, override=False): """ Case for completing arguments with position ≠ 0 """ if quoted: words = common.shell_split(self.text) else: words = self.text.split() if argument_position >= len(words): current = '' else: current = words[argument_position] if quoted: split_words = words[1:] words = [words[0]] for word in split_words: if ' ' in word or '\\' in word: words.append('"' + word + '"') else: words.append(word) current_l = current.lower() if self.last_completion is not None: self.hit_list.append(self.hit_list.pop(0)) else: if override: hit_list = word_list else: hit_list = [] for word in word_list: if word.lower().startswith(current_l): hit_list.append(word) if not hit_list: return self.hit_list = hit_list if argument_position >= len(words): if quoted and ' ' in self.hit_list[0]: words.append('"' + self.hit_list[0] + '"') else: words.append(self.hit_list[0]) else: if quoted and ' ' in self.hit_list[0]: words[argument_position] = '"' + self.hit_list[0] + '"' else: words[argument_position] = self.hit_list[0] new_pos = -1 for i, word in enumerate(words): if argument_position >= i: new_pos += len(word) + 1 self.last_completion = self.hit_list[0] self.text = words[0] + ' ' + ' '.join(words[1:]) self.pos = new_pos
def command_exec(self, args): args = common.shell_split(args) if len(args) == 1: command = args[0] arg = None elif len(args) == 2: command = args[1] arg = args[0] else: self.api.run_command('/help exec') return try: process = subprocess.Popen(['sh', '-c', command], stdout=subprocess.PIPE) except OSError as e: self.api.information('Failed to execute command: %s' % (e, ), 'Error') return result = process.communicate()[0].decode('utf-8') if arg and arg == '-o': if not self.api.send_message('%s' % (result, )): self.api.information( 'Cannot send result (%s), this is not a conversation tab' % result) elif arg and arg == '-O': if not self.api.send_message('%s:\n%s' % (command, result)): self.api.information( 'Cannot send result (%s), this is not a conversation tab' % result) else: self.api.information('%s:\n%s' % (command, result), 'Info') return
def completion_bookmark_local(self, the_input): """Completion for /bookmark_local""" n = the_input.get_argument_position(quoted=True) args = common.shell_split(the_input.text) if n >= 2: return if len(args) == 1: args.append('') jid = safeJID(args[1]) if jid.server and (jid.resource or jid.full.endswith('/')): tab = self.get_tab_by_name(jid.bare, tabs.MucTab) nicks = [tab.own_nick] if tab else [] default = os.environ.get('USER') if os.environ.get( 'USER') else 'poezio' nick = config.get('default_nick') if not nick: if not default in nicks: nicks.append(default) else: if not nick in nicks: nicks.append(nick) jids_list = ['%s/%s' % (jid.bare, nick) for nick in nicks] return the_input.new_completion(jids_list, 1, quotify=True) muc_list = [tab.name for tab in self.get_tabs(tabs.MucTab)] muc_list.append('*') return the_input.new_completion(muc_list, 1, quotify=True)
def completion_bookmark_local(self, the_input): """Completion for /bookmark_local""" n = the_input.get_argument_position(quoted=True) args = common.shell_split(the_input.text) if n >= 2: return if len(args) == 1: args.append("") jid = safeJID(args[1]) if jid.server and (jid.resource or jid.full.endswith("/")): tab = self.get_tab_by_name(jid.bare, tabs.MucTab) nicks = [tab.own_nick] if tab else [] default = os.environ.get("USER") if os.environ.get("USER") else "poezio" nick = config.get("default_nick") if not nick: if not default in nicks: nicks.append(default) else: if not nick in nicks: nicks.append(nick) jids_list = ["%s/%s" % (jid.bare, nick) for nick in nicks] return the_input.new_completion(jids_list, 1, quotify=True) muc_list = [tab.name for tab in self.get_tabs(tabs.MucTab)] muc_list.append("*") return the_input.new_completion(muc_list, 1, quotify=True)
def completion_quote(self, the_input): def nick_match(msg): if not msg.nickname: return nick == '' return msg.nickname.lower().startswith(nick.lower()) def time_match(msg): return msg.str_time.endswith(time) messages = self.core.get_conversation_messages() if not messages: return text = the_input.get_text() args = common.shell_split(text) n = len(args) if text.endswith(' '): n += 1 time = args[-1] if re.match(seconds_re, time) is not None: messages = list(filter(time_match, messages)) for i in range(3): the_input.key_backspace(False) elif n == 2: try: if args[1][0] not in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0'): return False except: pass nick = '' if n == 3: nick = args[1] messages = list(filter(nick_match, messages)) return the_input.auto_completion([msg.str_time for msg in messages[::-1]], '')
def command_exec(self, args): args = common.shell_split(args) if len(args) == 1: command = args[0] arg = None elif len(args) == 2: command = args[1] arg = args[0] else: self.api.run_command('/help exec') return try: process = subprocess.Popen(['sh', '-c', command], stdout=subprocess.PIPE) except OSError as e: self.api.information('Failed to execute command: %s' % (e,), 'Error') return result = process.communicate()[0].decode('utf-8') if arg and arg == '-o': if not self.api.send_message('%s' % (result,)): self.api.information('Cannot send result (%s), this is not a conversation tab' % result) elif arg and arg == '-O': if not self.api.send_message('%s:\n%s' % (command, result)): self.api.information('Cannot send result (%s), this is not a conversation tab' % result) else: self.api.information('%s:\n%s' % (command, result), 'Info') return
def completion_bookmark(self, the_input): """Completion for /bookmark""" args = common.shell_split(the_input.text) n = the_input.get_argument_position(quoted=True) if n == 2: return the_input.new_completion(['true', 'false'], 2, quotify=True) if n >= 3: return if len(args) == 1: args.append('') jid = safeJID(args[1]) if jid.server and (jid.resource or jid.full.endswith('/')): tab = self.get_tab_by_name(jid.bare, tabs.MucTab) nicks = [tab.own_nick] if tab else [] default = os.environ.get('USER') if os.environ.get('USER') else 'poezio' nick = config.get('default_nick') if not nick: if not default in nicks: nicks.append(default) else: if not nick in nicks: nicks.append(nick) jids_list = ['%s/%s' % (jid.bare, nick) for nick in nicks] return the_input.new_completion(jids_list, 1, quotify=True) muc_list = [tab.name for tab in self.get_tabs(tabs.MucTab)] muc_list.sort() muc_list.append('*') return the_input.new_completion(muc_list, 1, quotify=True)
def completion_delay(self, the_input): txt = the_input.get_text() args = common.shell_split(txt) n = len(args) if txt.endswith(' '): n += 1 if n == 2: return the_input.auto_completion(["60", "5m", "15m", "30m", "1h", "10h", "1d"], '')
def completion_set(self, the_input): """Completion for /set""" args = common.shell_split(the_input.text) n = the_input.get_argument_position(quoted=True) if n >= len(args): args.append('') if n == 1: if '|' in args[1]: plugin_name, section = args[1].split('|')[:2] if not plugin_name in self.plugin_manager.plugins: return the_input.new_completion([], n, quotify=True) plugin = self.plugin_manager.plugins[plugin_name] end_list = [ '%s|%s' % (plugin_name, section) for section in plugin.config.sections() ] else: end_list = set(config.options('Poezio')) end_list.update(config.default.get('Poezio', {})) end_list = list(end_list) end_list.sort() elif n == 2: if '|' in args[1]: plugin_name, section = args[1].split('|')[:2] if not plugin_name in self.plugin_manager.plugins: return the_input.new_completion([''], n, quotify=True) plugin = self.plugin_manager.plugins[plugin_name] end_list = set(plugin.config.options(section or plugin_name)) if plugin.config.default: end_list.update( plugin.config.default.get(section or plugin_name, {})) end_list = list(end_list) end_list.sort() elif not config.has_option('Poezio', args[1]): if config.has_section(args[1]): end_list = config.options(args[1]) end_list.append('') else: end_list = [] else: end_list = [str(config.get(args[1], '')), ''] elif n == 3: if '|' in args[1]: plugin_name, section = args[1].split('|')[:2] if not plugin_name in self.plugin_manager.plugins: return the_input.new_completion([''], n, quotify=True) plugin = self.plugin_manager.plugins[plugin_name] end_list = [ str(plugin.config.get(args[2], '', section or plugin_name)), '' ] else: if not config.has_section(args[1]): end_list = [''] else: end_list = [str(config.get(args[2], '', args[1])), ''] else: return return the_input.new_completion(end_list, n, quotify=True)
def completion_delay(self, the_input): txt = the_input.get_text() args = common.shell_split(txt) n = len(args) if txt.endswith(' '): n += 1 if n == 2: return the_input.auto_completion( ["60", "5m", "15m", "30m", "1h", "10h", "1d"], '')
def completion_set_default(self, the_input): """ Completion for /set_default """ args = common.shell_split(the_input.text) n = the_input.get_argument_position(quoted=True) if n >= len(args): args.append("") if n == 1 or (n == 2 and config.has_section(args[1])): return self.completion_set(the_input) return []
def completion_set_default(self, the_input): """ Completion for /set_default """ args = common.shell_split(the_input.text) n = the_input.get_argument_position(quoted=True) if n >= len(args): args.append('') if n == 1 or (n == 2 and config.has_section(args[1])): return self.completion_set(the_input) return []
def _new_completion_args(self, word_list, argument_position=-1, add_after='', quoted=True, override=False): """ Case for completing arguments with position ≠ 0 """ if quoted: words = common.shell_split(self.text) else: words = self.text.split() if argument_position >= len(words): current = '' else: current = words[argument_position] if quoted: split_words = words[1:] words = [words[0]] for word in split_words: if ' ' in word or '\\' in word: words.append('"' + word + '"') else: words.append(word) current_l = current.lower() if self.last_completion is not None: self.hit_list.append(self.hit_list.pop(0)) else: if override: hit_list = word_list else: hit_list = [] for word in word_list: if word.lower().startswith(current_l): hit_list.append(word) if not hit_list: return self.hit_list = hit_list if argument_position >= len(words): if quoted and ' ' in self.hit_list[0]: words.append('"'+self.hit_list[0]+'"') else: words.append(self.hit_list[0]) else: if quoted and ' ' in self.hit_list[0]: words[argument_position] = '"'+self.hit_list[0]+'"' else: words[argument_position] = self.hit_list[0] new_pos = -1 for i, word in enumerate(words): if argument_position >= i: new_pos += len(word) + 1 self.last_completion = self.hit_list[0] self.text = words[0] + ' ' + ' '.join(words[1:]) self.pos = new_pos
def command_delayed(self, arg): args = common.shell_split(arg) if len(args) != 2: return delay = common.parse_str_to_secs(args[0]) if not delay: return tab = self.core.current_tab() timed_event = timed_events.DelayedEvent(delay, self.say, (tab, args[1])) self.core.add_timed_event(timed_event)
def command_muc_ping(self, arg): args = common.shell_split(arg) if not args: return user = self.core.current_tab().get_user_by_name(args[0]) if user: jid = JID(self.core.current_tab().get_name()) jid.resource = user.nick else: jid = JID(args[0]) self.command_ping(jid.full)
def generic_command(command, extra_args, args): """ Function that will execute the command and set the relevant parameters (format string, etc). """ args = shell_split(args) new_extra_args = extra_args.format(*args) count = extra_args.count('{}') args = args[count:] new_extra_args += ' '.join(args) return command()(new_extra_args)
def completion_activity(self, the_input): """Completion for /activity""" n = the_input.get_argument_position(quoted=True) args = common.shell_split(the_input.text) if n == 1: return the_input.new_completion(sorted(pep.ACTIVITIES.keys()), n, quotify=True) elif n == 2: if args[1] in pep.ACTIVITIES: l = list(pep.ACTIVITIES[args[1]]) l.remove("category") l.sort() return the_input.new_completion(l, n, quotify=True)
def command_delayed(self, arg): args = common.shell_split(arg) if len(args) != 2: return delay = common.parse_str_to_secs(args[0]) if not delay: return tab = self.api.current_tab() timed_event = timed_events.DelayedEvent(delay, self.say, (tab, args[1])) self.api.add_timed_event(timed_event)
def completion_cert_fetch(self, the_input): """ completion for /cert_fetch <name> <path> """ text = the_input.get_text() args = common.shell_split(text) n = the_input.get_argument_position() log.debug('%s %s %s', the_input.text, n, the_input.pos) if n == 1: return elif n == 2: return self.completion_file(2, the_input)
def test_shell_split(): assert shell_split('"sdf 1" "toto 2"') == ["sdf 1", "toto 2"] assert shell_split('toto "titi"') == ["toto", "titi"] assert shell_split('toto ""') == ["toto", ""] assert shell_split('to"to titi "a" b') == ['to"to', "titi", "a", "b"] assert shell_split('"toto titi" toto ""') == ["toto titi", "toto", ""] assert shell_split('toto "titi') == ["toto", "titi"]
def test_shell_split(): assert shell_split('"sdf 1" "toto 2"') == ['sdf 1', 'toto 2'] assert shell_split('toto "titi"') == ['toto', 'titi'] assert shell_split('toto ""') == ['toto', ''] assert shell_split('to"to titi "a" b') == ['to"to', 'titi', 'a', 'b'] assert shell_split('"toto titi" toto ""') == ['toto titi', 'toto', ''] assert shell_split('toto "titi') == ['toto', 'titi']
def completion_set(self, the_input): """Completion for /set""" args = common.shell_split(the_input.text) n = the_input.get_argument_position(quoted=True) if n >= len(args): args.append("") if n == 1: if "|" in args[1]: plugin_name, section = args[1].split("|")[:2] if not plugin_name in self.plugin_manager.plugins: return the_input.new_completion([], n, quotify=True) plugin = self.plugin_manager.plugins[plugin_name] end_list = ["%s|%s" % (plugin_name, section) for section in plugin.config.sections()] else: end_list = set(config.options("Poezio")) end_list.update(config.default.get("Poezio", {})) end_list = list(end_list) end_list.sort() elif n == 2: if "|" in args[1]: plugin_name, section = args[1].split("|")[:2] if not plugin_name in self.plugin_manager.plugins: return the_input.new_completion([""], n, quotify=True) plugin = self.plugin_manager.plugins[plugin_name] end_list = set(plugin.config.options(section or plugin_name)) if plugin.config.default: end_list.update(plugin.config.default.get(section or plugin_name, {})) end_list = list(end_list) end_list.sort() elif not config.has_option("Poezio", args[1]): if config.has_section(args[1]): end_list = config.options(args[1]) end_list.append("") else: end_list = [] else: end_list = [str(config.get(args[1], "")), ""] elif n == 3: if "|" in args[1]: plugin_name, section = args[1].split("|")[:2] if not plugin_name in self.plugin_manager.plugins: return the_input.new_completion([""], n, quotify=True) plugin = self.plugin_manager.plugins[plugin_name] end_list = [str(plugin.config.get(args[2], "", section or plugin_name)), ""] else: if not config.has_section(args[1]): end_list = [""] else: end_list = [str(config.get(args[2], "", args[1])), ""] else: return return the_input.new_completion(end_list, n, quotify=True)
def completion_join(self, the_input): """ Completion for /join Try to complete the MUC JID: if only a resource is provided, complete with the default nick if only a server is provided, complete with the rooms from the disco#items of that server if only a nodepart is provided, complete with the servers of the current joined rooms """ n = the_input.get_argument_position(quoted=True) args = common.shell_split(the_input.text) if n != 1: # we are not on the 1st argument of the command line return False if len(args) == 1: args.append('') jid = safeJID(args[1]) if args[1].endswith('@') and not jid.user and not jid.server: jid.user = args[1][:-1] relevant_rooms = [] relevant_rooms.extend(sorted(self.pending_invites.keys())) bookmarks = {str(elem.jid): False for elem in bookmark.bookmarks} for tab in self.get_tabs(tabs.MucTab): name = tab.name if name in bookmarks and not tab.joined: bookmarks[name] = True relevant_rooms.extend( sorted(room[0] for room in bookmarks.items() if room[1])) if the_input.last_completion: return the_input.new_completion([], 1, quotify=True) if jid.user: # we are writing the server: complete the server serv_list = [] for tab in self.get_tabs(tabs.MucTab): if tab.joined: serv_list.append('%s@%s' % (jid.user, safeJID(tab.name).host)) serv_list.extend(relevant_rooms) return the_input.new_completion(serv_list, 1, quotify=True) elif args[1].startswith('/'): # we completing only a resource return the_input.new_completion(['/%s' % self.own_nick], 1, quotify=True) else: return the_input.new_completion(relevant_rooms, 1, quotify=True) return True
def command_link(self, args): args = common.shell_split(args) if len(args) == 1: try: nb = int(args[0]) except: return self.core.command_help('link') else: nb = 1 link = self.find_link(nb) if link: self.core.exec_command([self.config.get('browser', 'firefox'), link]) else: self.core.information('No URL found.', 'Warning')
def completion_activity(self, the_input): """Completion for /activity""" n = the_input.get_argument_position(quoted=True) args = common.shell_split(the_input.text) if n == 1: return the_input.new_completion(sorted(pep.ACTIVITIES.keys()), n, quotify=True) elif n == 2: if args[1] in pep.ACTIVITIES: l = list(pep.ACTIVITIES[args[1]]) l.remove('category') l.sort() return the_input.new_completion(l, n, quotify=True)
def completion_cert_add(self, the_input): """ completion for /cert_add <name> <path> [management] """ text = the_input.get_text() args = common.shell_split(text) n = the_input.get_argument_position() log.debug('%s %s %s', the_input.text, n, the_input.pos) if n == 1: return elif n == 2: return self.completion_file(2, the_input) elif n == 3: return the_input.new_completion(['true', 'false'], n)
def command_remind(self, arg): args = common.shell_split(arg) if len(args) < 2: return time = common.parse_str_to_secs(args[0]) if not time: return self.tasks[self.count] = (time, args[1]) timed_event = timed_events.DelayedEvent(time, self.remind, self.count) self.api.add_timed_event(timed_event) self.api.information('Task %s added: %s every %s.' % (self.count, args[1], common.parse_secs_to_str(time)), 'Info') self.count += 1
def command_tell(self, args): """/tell <nick> <message>""" arg = common.shell_split(args) if len(arg) != 2: self.core.command_help('tell') return nick, msg = arg tab = self.api.current_tab() if not tab in self.tabs: self.tabs[tab] = {} if not nick in self.tabs[tab]: self.tabs[tab][nick] = [] self.tabs[tab][nick].append(msg) self.api.information('Message for %s queued' % nick, 'Info')
def completion_quote(self, the_input): def message_match(msg): return input_message.lower() in clean_text(msg.txt).lower() messages = self.api.get_conversation_messages() if not messages: return text = the_input.get_text() args = common.shell_split(text) if not text.endswith(" "): input_message = args[-1] messages = list(filter(message_match, messages)) elif len(args) > 1: return False return the_input.auto_completion([clean_text(msg.txt) for msg in messages[::-1]], "")
def command_link(self, args): args = common.shell_split(args) if len(args) == 1: try: nb = int(args[0]) except: return self.core.command_help("link") else: nb = 1 link = self.find_link(nb) if link: link = pipes.quote(link) self.core.exec_command("%s %s" % (self.config.get("browser", "firefox"), link)) else: self.core.information("No URL found.", "Warning")
def command_remind(self, arg): args = common.shell_split(arg) if len(args) < 2: return time = common.parse_str_to_secs(args[0]) if not time: return self.tasks[self.count] = (time, args[1]) timed_event = timed_events.DelayedEvent(time, self.remind, self.count) self.api.add_timed_event(timed_event) self.api.information( 'Task %s added: %s every %s.' % (self.count, args[1], common.parse_secs_to_str(time)), 'Info') self.count += 1
def completion_join(self, the_input): """ Completion for /join Try to complete the MUC JID: if only a resource is provided, complete with the default nick if only a server is provided, complete with the rooms from the disco#items of that server if only a nodepart is provided, complete with the servers of the current joined rooms """ n = the_input.get_argument_position(quoted=True) args = common.shell_split(the_input.text) if n != 1: # we are not on the 1st argument of the command line return False if len(args) == 1: args.append('') jid = safeJID(args[1]) if args[1].endswith('@') and not jid.user and not jid.server: jid.user = args[1][:-1] relevant_rooms = [] relevant_rooms.extend(sorted(self.pending_invites.keys())) bookmarks = {str(elem.jid): False for elem in bookmark.bookmarks} for tab in self.get_tabs(tabs.MucTab): name = tab.name if name in bookmarks and not tab.joined: bookmarks[name] = True relevant_rooms.extend(sorted(room[0] for room in bookmarks.items() if room[1])) if the_input.last_completion: return the_input.new_completion([], 1, quotify=True) if jid.user: # we are writing the server: complete the server serv_list = [] for tab in self.get_tabs(tabs.MucTab): if tab.joined: serv_list.append('%s@%s'% (jid.user, safeJID(tab.name).host)) serv_list.extend(relevant_rooms) return the_input.new_completion(serv_list, 1, quotify=True) elif args[1].startswith('/'): # we completing only a resource return the_input.new_completion(['/%s' % self.own_nick], 1, quotify=True) else: return the_input.new_completion(relevant_rooms, 1, quotify=True) return True
def completion_quote(self, the_input): def message_match(msg): return input_message.lower() in clean_text(msg.txt).lower() messages = self.api.get_conversation_messages() if not messages: return text = the_input.get_text() args = common.shell_split(text) if not text.endswith(' '): input_message = args[-1] messages = list(filter(message_match, messages)) elif len(args) > 1: return False return the_input.auto_completion( [clean_text(msg.txt) for msg in messages[::-1]], '')
def completion_groupremove(self, the_input): args = common.shell_split(the_input.text) n = the_input.get_argument_position() if n == 1: jids = sorted(jid for jid in roster.jids()) return the_input.new_completion(jids, n, '', quotify=True) elif n == 2: contact = roster[args[1]] if contact is None: return False groups = sorted(contact.groups) try: groups.remove('none') except ValueError: pass return the_input.new_completion(groups, n, '', quotify=True) return False
def completion_groupmove(self, the_input): args = common.shell_split(the_input.text) n = the_input.get_argument_position() if n == 1: jids = sorted(jid for jid in roster.jids()) return the_input.new_completion(jids, n, '', quotify=True) elif n == 2: contact = roster[args[1]] if not contact: return False groups = list(contact.groups) if 'none' in groups: groups.remove('none') return the_input.new_completion(groups, n, '', quotify=True) elif n == 3: groups = sorted(group for group in roster.groups) return the_input.new_completion(groups, n, '', quotify=True) return False
def command_alias(self, line): """ /alias <alias> <command> [args] """ arg = common.shell_split(line) if len(arg) < 2: self.core.information("Alias: Not enough parameters", "Error") return alias = arg[0] command = arg[1] tmp_args = arg[2] if len(arg) > 2 else "" if alias in self.core.commands or alias in self.commands: self.core.information("Alias: command already exists", "Error") return self.commands[alias] = lambda arg: self.get_command(command)(parse(arg, tmp_args)) self.add_command(alias, self.commands[alias], "This command is an alias for /%s %s" % (command, tmp_args)) self.core.information("Alias /%s successfuly created" % alias, "Info")
def command_display_corrections(self, args): args = shell_split(args) if len(args) == 1: try: nb = int(args[0]) except: return self.api.run_command('/help display_corrections') else: nb = 1 message = self.find_corrected(nb) if message: display = [] while message: display.append('%s %s%s%s %s' % (message.str_time, '* ' if message.me else '', message.nickname, '' if message.me else '>', message.txt)) message = message.old_message self.api.information('Older versions:\n' + '\n'.join(display[::-1]), 'Info') else: self.api.information('No corrected message found.', 'Warning')
def second(self, args, *a, **kw): default_args = defaults args = common.shell_split(args) if len(args) < mandatory: return func(self, None, *a, **kw) res, args = args[:mandatory], args[mandatory:] if optional == -1: opt_args = args[:] else: opt_args = args[:optional] if opt_args: res += opt_args args = args[len(opt_args):] default_args = default_args[len(opt_args):] res += default_args if args and res and not ignore_trailing_arguments: res[-1] += " " + " ".join(args) return func(self, res, *a, **kw)
def command_quote(self, args): args = common.shell_split(args) if len(args) in (1, 2): timestamp = args[-1] else: return self.core.command_help('quote') if re.match(timestamp_re, timestamp) is None: return self.core.information('Timestamp has a wrong format.', 'Warning') message = self.find_message_with_timestamp(timestamp) if message: before = self.config.get('before_quote', '') % {'nick': message.nickname or '', 'time': message.str_time} after = self.config.get('after_quote', '') % {'nick': message.nickname or '', 'time': message.str_time} self.core.insert_input_text('%(before)s%(quote)s%(after)s' % {'before': before.replace('\\n', '\n').replace('[SP]', ' '), 'quote': clean_text(message.txt), 'after': after.replace('\\n', '\n').replace('[SP]', ' ')}) else: self.core.information('No message found for timestamp %s.' % timestamp, 'Warning')
def command_mpd(self, args): args = shell_split(args) c = mpd.MPDClient() c.connect(host=self.config.get('host', 'localhost'), port=self.config.get('port', '6600')) password = self.config.get('password', '') if password: c.password(password) current = c.currentsong() artist = current.get('artist', 'Unknown artist') album = current.get('album', 'Unknown album') title = current.get('title', base(current.get('file', 'Unknown title'))) s = '%s - %s (%s)' % (artist, title, album) if 'full' in args: if 'elapsed' in current and 'time' in current: current_time = float(c.status()['elapsed']) percents = int(current_time / float(current['time']) * 10) s += ' \x192}[\x191}' + '-'*(percents-1) + '\x193}+' + '\x191}' + '-' * (10-percents-1) + '\x192}]\x19o' if not self.api.send_message('%s' % (s,)): self.api.information('Cannot send result (%s)' % s, 'Error')
def command_quote(self, args): args = common.shell_split(args) if len(args) == 1: message = args[-1] else: return self.api.run_command("/help quote") message = self.find_message(message) if message: before = self.config.get("before_quote", "") % {"nick": message.nickname or "", "time": message.str_time} after = self.config.get("after_quote", "") % {"nick": message.nickname or "", "time": message.str_time} self.core.insert_input_text( "%(before)s%(quote)s%(after)s" % { "before": before.replace("\\n", "\n").replace("[SP]", " "), "quote": clean_text(message.txt), "after": after.replace("\\n", "\n").replace("[SP]", " "), } ) else: self.api.information("No message found", "Warning")
def command_link(self, args): args = common.shell_split(args) start = 1 end = 1 # With two arguments, the first is the range, the second is the command # With only one argument, it is a range if it starts with a number # or :, otherwise it is a command if len(args) == 2 or\ len(args) == 1 and (args[0][0].isnumeric() or args[0][0] == ":"): if args[0].find(':') == -1: try: start = int(args[0]) end = start except ValueError: return self.api.run_command('/help link') else: start, end = args[0].split(':', 1) if start == '': start = 1 try: start = int(start) end = int(end) except ValueError: return self.api.information( 'Invalid range: %s' % (args[0]), 'Error') command = None if len(args) == 2: command = args[1] if len(args) == 1 and (not args[0][0].isnumeric() and args[0][0] != ":"): command = args[0] for nb in range(start, end + 1): link = self.find_link(nb) if not link: return self.api.information('No URL found.', 'Warning') default = app_mapping.get(platform.system(), 'firefox') if command is None: self.core.exec_command( [self.config.get('browser', default), link]) else: self.core.exec_command([command, link])
def command_display_corrections(self, args): args = shell_split(args) if len(args) == 1: try: nb = int(args[0]) except: return self.api.run_command('/help display_corrections') else: nb = 1 message = self.find_corrected(nb) if message: display = [] while message: display.append( '%s %s%s%s %s' % (message.str_time, '* ' if message.me else '', message.nickname, '' if message.me else '>', message.txt)) message = message.old_message self.api.information( 'Older versions:\n' + '\n'.join(display[::-1]), 'Info') else: self.api.information('No corrected message found.', 'Warning')
def completion_file(self, complete_number, the_input): """ Generic quoted completion for files/paths (use functools.partial to use directly as a completion for a command) """ text = the_input.get_text() args = common.shell_split(text) n = the_input.get_argument_position() if n == complete_number: if args[n - 1] == '' or len(args) < n + 1: home = os.getenv('HOME') or '/' return the_input.new_completion([home, '/tmp'], n, quotify=True) path_ = args[n] if path.isdir(path_): dir_ = path_ base = '' else: dir_ = path.dirname(path_) base = path.basename(path_) try: names = os.listdir(dir_) except OSError: names = [] names_filtered = [name for name in names if name.startswith(base)] if names_filtered: names = names_filtered if not names: names = [path_] end_list = [] for name in names: value = os.path.join(dir_, name) if not name.startswith('.'): end_list.append(value) return the_input.new_completion(end_list, n, quotify=True)
def command_mpd(self, args): args = shell_split(args) c = mpd.MPDClient() c.connect(host=self.config.get('host', 'localhost'), port=self.config.get('port', '6600')) password = self.config.get('password', '') if password: c.password(password) current = c.currentsong() artist = current.get('artist', 'Unknown artist') album = current.get('album', 'Unknown album') title = current.get('title', base(current.get('file', 'Unknown title'))) s = '%s - %s (%s)' % (artist, title, album) if 'full' in args: if 'elapsed' in current and 'time' in current: current_time = float(c.status()['elapsed']) percents = int(current_time / float(current['time']) * 10) s += ' \x192}[\x191}' + '-' * ( percents - 1) + '\x193}+' + '\x191}' + '-' * ( 10 - percents - 1) + '\x192}]\x19o' if not self.api.send_message('%s' % (s, )): self.api.information('Cannot send result (%s)' % s, 'Error')
def command_quote(self, args): args = common.shell_split(args) if len(args) == 1: message = args[-1] else: return self.api.run_command('/help quote') message = self.find_message(message) if message: before = self.config.get('before_quote', '') % { 'nick': message.nickname or '', 'time': message.str_time } after = self.config.get('after_quote', '') % { 'nick': message.nickname or '', 'time': message.str_time } self.core.insert_input_text( '%(before)s%(quote)s%(after)s' % { 'before': before.replace('\\n', '\n').replace('[SP]', ' '), 'quote': clean_text(message.txt), 'after': after.replace('\\n', '\n').replace('[SP]', ' ') }) else: self.api.information('No message found', 'Warning')
def command_otr(self, arg): """ /otr [start|refresh|end|fpr|ourfpr] """ args = common.shell_split(arg) if not args: return self.core.command_help('otr') action = args.pop(0) tab = self.api.current_tab() name = tab.name if isinstance(tab, DynamicConversationTab) and tab.locked_resource: name = safeJID(tab.name) name.resource = tab.locked_resource name = name.full format_dict = { 'jid_c': '\x19%s}' % dump_tuple(get_theme().COLOR_MUC_JID), 'info': '\x19%s}' % dump_tuple(get_theme().COLOR_INFORMATION_TEXT), 'normal': '\x19%s}' % dump_tuple(get_theme().COLOR_NORMAL_TEXT), 'jid': name, 'bare_jid': safeJID(name).bare } if action == 'end': # close the session context = self.get_context(name) context.disconnect() if isinstance(tab, DynamicConversationTab): ctx = self.find_encrypted_context_with_matching(safeJID(name).bare) while ctx is not None: ctx.disconnect() ctx = self.find_encrypted_context_with_matching(safeJID(name).bare) elif action == 'start' or action == 'refresh': self.otr_start(tab, name, format_dict) elif action == 'ourfpr': format_dict['fpr'] = self.account.getPrivkey() tab.add_message(OTR_OWN_FPR % format_dict, typ=0) elif action == 'fpr': if name in self.contexts: ctx = self.contexts[name] if ctx.getCurrentKey() is not None: format_dict['fpr'] = ctx.getCurrentKey() tab.add_message(OTR_REMOTE_FPR % format_dict, typ=0) else: tab.add_message(OTR_NO_FPR % format_dict, typ=0) elif action == 'drop': # drop the privkey (and obviously, end the current conversations before that) for context in self.contexts.values(): if context.state not in (STATE_FINISHED, STATE_PLAINTEXT): context.disconnect() self.account.drop_privkey() tab.add_message(KEY_DROPPED % format_dict, typ=0) elif action == 'trust': ctx = self.get_context(name) key = ctx.getCurrentKey() if key: fpr = key.cfingerprint() else: return if not ctx.getCurrentTrust(): format_dict['key'] = key ctx.setTrust(fpr, 'verified') self.account.saveTrusts() tab.add_message(TRUST_ADDED % format_dict, typ=0) elif action == 'untrust': ctx = self.get_context(name) key = ctx.getCurrentKey() if key: fpr = key.cfingerprint() else: return if ctx.getCurrentTrust(): format_dict['key'] = key ctx.setTrust(fpr, '') self.account.saveTrusts() tab.add_message(TRUST_REMOVED % format_dict, typ=0) self.core.refresh_window()