def _new_completion_args(self, word_list: List[str], argument_position: int = -1, add_after: str = '', quoted: bool = True, override: bool = False) -> None: """ 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_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) #pylint: disable=no-member current = c.currentsong() #pylint: disable=no-member 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']) #pylint: disable=no-member 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 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 False if len(args) == 1: args.append('') jid = safeJID(args[1]) if jid.server and (jid.resource or jid.full.endswith('/')): tab = self.core.tabs.by_name_and_class(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 default not in nicks: nicks.append(default) else: if nick not in nicks: nicks.append(nick) jids_list = ['%s/%s' % (jid.bare, nick) for nick in nicks] return Completion( the_input.new_completion, jids_list, 1, quotify=True) muc_list = [tab.name for tab in self.core.get_tabs(tabs.MucTab)] muc_list.append('*') return Completion(the_input.new_completion, muc_list, 1, quotify=True)
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_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 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.core.pending_invites.keys())) bookmarks = [(str(elem.jid) if not elem.nick else '%s/%s' % (elem.jid, elem.nick)) for elem in self.core.bookmarks] to_suggest = [] for bookmark in bookmarks: tab = self.core.tabs.by_name_and_class(bookmark, tabs.MucTab) if not tab or (tab and not tab.joined): to_suggest.append(bookmark) relevant_rooms.extend(sorted(to_suggest)) if the_input.last_completion: return Completion(the_input.new_completion, [], 1, quotify=True) if jid.user: # we are writing the server: complete the server serv_list = [] for tab in self.core.get_tabs(tabs.MucTab): if tab.joined: serv_list.append( '%s@%s' % (jid.user, safeJID(tab.name).host)) serv_list.extend(relevant_rooms) return Completion( the_input.new_completion, serv_list, 1, quotify=True) elif args[1].startswith('/'): # we completing only a resource return Completion( the_input.new_completion, ['/%s' % self.core.own_nick], 1, quotify=True) else: return Completion( the_input.new_completion, relevant_rooms, 1, quotify=True)
def 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 Completion(self.set, the_input) return False
def completion_remind(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 Completion(the_input.auto_completion, ["60", "5m", "15m", "30m", "1h", "10h", "1d"], '')
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 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 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 Completion(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 Completion(the_input.new_completion, l, n, quotify=True)
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 asyncio.ensure_future(self.async_exec(command, arg))
def 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 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 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 plugin_name not in self.core.plugin_manager.plugins: return the_input.new_completion([], n, quotify=True) plugin = self.core.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 plugin_name not in self.core.plugin_manager.plugins: return the_input.new_completion([''], n, quotify=True) plugin = self.core.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 plugin_name not in self.core.plugin_manager.plugins: return the_input.new_completion([''], n, quotify=True) plugin = self.core.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 False return the_input.new_completion(end_list, n, quotify=True)
def 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 plugin_name not in self.core.plugin_manager.plugins: return Completion(the_input.new_completion, [], n, quotify=True) plugin = self.core.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 plugin_name not in self.core.plugin_manager.plugins: return Completion(the_input.new_completion, [''], n, quotify=True) plugin = self.core.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 plugin_name not in self.core.plugin_manager.plugins: return Completion(the_input.new_completion, [''], n, quotify=True) plugin = self.core.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 False return Completion(the_input.new_completion, end_list, n, quotify=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 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_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 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.core.pending_invites.keys())) bookmarks = [(str(elem.jid) if not elem.nick else '%s/%s' % (elem.jid, elem.nick)) for elem in self.core.bookmarks] to_suggest = [] for bookmark in bookmarks: tab = self.core.get_tab_by_name(bookmark, tabs.MucTab) if not tab or (tab and not tab.joined): to_suggest.append(bookmark) relevant_rooms.extend(sorted(to_suggest)) if the_input.last_completion: return Completion(the_input.new_completion, [], 1, quotify=True) if jid.user: # we are writing the server: complete the server serv_list = [] for tab in self.core.get_tabs(tabs.MucTab): if tab.joined: serv_list.append('%s@%s' % (jid.user, safeJID(tab.name).host)) serv_list.extend(relevant_rooms) return Completion(the_input.new_completion, serv_list, 1, quotify=True) elif args[1].startswith('/'): # we completing only a resource return Completion(the_input.new_completion, ['/%s' % self.core.own_nick], 1, quotify=True) else: return Completion(the_input.new_completion, relevant_rooms, 1, quotify=True)
def impromptu(self, args: str) -> None: """/impromptu <jid> [<jid> ...]""" if args is None: return self.help('impromptu') jids = set() current_tab = self.core.tabs.current_tab if isinstance(current_tab, tabs.ConversationTab): jids.add(current_tab.general_jid) for jid in common.shell_split(' '.join(args)): jids.add(safeJID(jid).bare) asyncio.ensure_future(self.core.impromptu(jids)) self.core.information('Invited %s to a random room' % (', '.join(jids)), '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 Completion(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 Completion(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 Completion(the_input.new_completion, groups, n, '', quotify=True) return False
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 impromptu(self, args: str) -> None: """/impromptu <jid> [<jid> ...]""" if args is None: return self.help('impromptu') jids = set() current_tab = self.core.tabs.current_tab if isinstance(current_tab, tabs.ConversationTab): jids.add(current_tab.general_jid) for jid in common.shell_split(' '.join(args)): jids.add(safeJID(jid).bare) asyncio.ensure_future(self.core.impromptu(jids)) self.core.information( 'Invited %s to a random room' % (', '.join(jids)), '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 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 Completion(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 Completion(the_input.new_completion, groups, n, '', quotify=True) elif n == 3: groups = sorted(group for group in roster.groups) return Completion(the_input.new_completion, groups, n, '', quotify=True) return False
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 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 = 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', default='/') return Completion(the_input.new_completion, [home, '/tmp'], n, quotify=True) path_ = Path(args[n]) if path_.is_dir(): dir_ = path_ base = '' else: dir_ = path_.parent base = path_.name try: names = list(dir_.iterdir()) except OSError: names = [] names_filtered = [ name for name in names if str(name).startswith(base) ] if names_filtered: names = names_filtered if not names: names = [path_] end_list = [] for name in names: if not str(name).startswith('.'): value = dir_ / name end_list.append(str(value)) return Completion(the_input.new_completion, end_list, n, quotify=True)
def completion_groupremove(self, the_input): args = shell_split(the_input.text) n = the_input.get_argument_position() if n == 1: jids = sorted(jid for jid in roster.jids()) return Completion( 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 Completion( the_input.new_completion, groups, n, '', quotify=True) return False
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 completion_groupmove(self, the_input): args = shell_split(the_input.text) n = the_input.get_argument_position() if n == 1: jids = sorted(jid for jid in roster.jids()) return Completion( 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 Completion( the_input.new_completion, groups, n, '', quotify=True) elif n == 3: groups = sorted(group for group in roster.groups) return Completion( the_input.new_completion, groups, n, '', quotify=True) return False
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 = 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', default='/') return Completion( the_input.new_completion, [home, '/tmp'], n, quotify=True) path_ = Path(args[n]) if path_.is_dir(): dir_ = path_ base = '' else: dir_ = path_.parent base = path_.name try: names = list(dir_.iterdir()) except OSError: names = [] names_filtered = [ name for name in names if str(name).startswith(base) ] if names_filtered: names = names_filtered if not names: names = [path_] end_list = [] for name in names: if not str(name).startswith('.'): value = dir_ / name end_list.append(str(value)) return Completion( the_input.new_completion, end_list, n, quotify=True)
def second(self, args, *a, **kw): default_args = defaults if args and args.strip(): args = common.shell_split(args) else: 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_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: Any, args: str, *a: Any, **kw: Any) -> Any: default_args = default_args_outer if args and args.strip(): split_args = common.shell_split(args) else: split_args = [] if len(split_args) < mandatory: return func(self, None, *a, **kw) res, split_args = split_args[:mandatory], split_args[ mandatory:] if optional == -1: opt_args = split_args[:] else: opt_args = split_args[:optional] if opt_args: res += opt_args split_args = split_args[len(opt_args):] default_args = default_args[len(opt_args):] res += default_args if split_args and res and not ignore_trailing_arguments: res[-1] += " " + " ".join(split_args) return func(self, res, *a, **kw)
def second(self, args: str, *a, **kw): default_args = default_args_outer if args and args.strip(): split_args = common.shell_split(args) else: split_args = [] if len(split_args) < mandatory: return func(self, None, *a, **kw) res, split_args = split_args[:mandatory], split_args[ mandatory:] if optional == -1: opt_args = split_args[:] else: opt_args = split_args[:optional] if opt_args: res += opt_args split_args = split_args[len(opt_args):] default_args = default_args[len(opt_args):] res += default_args if split_args and res and not ignore_trailing_arguments: res[-1] += " " + " ".join(split_args) return func(self, res, *a, **kw)
def 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 Completion(the_input.new_completion, ['true', 'false'], 2, quotify=True) if n >= 3: return False if len(args) == 1: args.append('') jid = safeJID(args[1]) if jid.server and (jid.resource or jid.full.endswith('/')): tab = self.core.tabs.by_name_and_class(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 default not in nicks: nicks.append(default) else: if nick not in nicks: nicks.append(nick) jids_list = ['%s/%s' % (jid.bare, nick) for nick in nicks] return Completion(the_input.new_completion, jids_list, 1, quotify=True) muc_list = [tab.name for tab in self.core.get_tabs(tabs.MucTab)] muc_list.sort() muc_list.append('*') return Completion(the_input.new_completion, muc_list, 1, quotify=True)
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_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) #pylint: disable=no-member current = c.currentsong() #pylint: disable=no-member 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']) #pylint: disable=no-member 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 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 Completion(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 Completion(the_input.new_completion, end_list, n, quotify=True)
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 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() 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()