def find_channels(): """Return list of servers and channels""" #@TODO: make it return a dict with more options like "nicks_count etc." items = {} infolist = w.infolist_get('irc_server', '', '') # populate servers while w.infolist_next(infolist): items[w.infolist_string(infolist, 'name')] = '' w.infolist_free(infolist) # populate channels per server for server in items.keys(): items[server] = '' #init if connected but no channels infolist = w.infolist_get('irc_channel', '', server) while w.infolist_next(infolist): if w.infolist_integer(infolist, 'nicks_count') == 0: #parted but still open in a buffer: bit hackish continue if w.infolist_integer(infolist, 'type') == 0: channel = w.infolist_string(infolist, "buffer_short_name") items[server] += '%s,' %channel w.infolist_free(infolist) return items
def find_channels(): """Return list of servers and channels""" #@TODO: make it return a dict with more options like "nicks_count etc." items = {} infolist = w.infolist_get('irc_server', '', '') # populate servers while w.infolist_next(infolist): items[w.infolist_string(infolist, 'name')] = '' w.infolist_free(infolist) # populate channels per server for server in items.keys(): keys = [] keyed_channels = [] unkeyed_channels = [] items[server] = '' #init if connected but no channels infolist = w.infolist_get('irc_channel', '', server) while w.infolist_next(infolist): if w.infolist_integer(infolist, 'nicks_count') == 0: #parted but still open in a buffer: bit hackish continue if w.infolist_integer(infolist, 'type') == 0: key = w.infolist_string(infolist, "key") if len(key) > 0: keys.append(key) keyed_channels.append(w.infolist_string(infolist, "name")) else : unkeyed_channels.append(w.infolist_string(infolist, "name")) items[server] = ','.join(keyed_channels + unkeyed_channels) if len(keys) > 0: items[server] += ' %s' % ','.join(keys) w.infolist_free(infolist) return items
def command_main(data, buffer, args): infolist = w.infolist_get("buffer", "", "") buffer_groups = {} results = [] buffer_count = 0 merge_count = 0 numbers = set() while w.infolist_next(infolist): bplugin = w.infolist_string(infolist, "plugin_name") bname = w.infolist_string(infolist, "name") bpointer = w.infolist_pointer(infolist, "pointer") bnumber = w.infolist_integer(infolist, "number") btype = w.buffer_get_string(bpointer, 'localvar_type') if not bnumber in numbers: numbers.add(bnumber) else: merge_count += 1 if btype == 'server': bdesc = 'servers' elif btype == 'channel': bdesc = 'channels' elif btype == 'private': bdesc = 'queries' else: bdesc = bplugin buffer_groups.setdefault(bdesc,[]).append({'name': bname, 'pointer': bpointer}) w.infolist_free(infolist) infolist = w.infolist_get("window", "", "") windows_v = set() windows_h = set() windows = set() while w.infolist_next(infolist): window = w.infolist_pointer(infolist, "pointer") window_w = w.infolist_integer(infolist, "width_pct") window_h = w.infolist_integer(infolist, "height_pct") windows.add(window) if window_h == 100 and window_w != 100: windows_v.add(window) elif window_w == 100 and window_h != 100: windows_h.add(window) #else: #both 100%, thus no splits w.infolist_free(infolist) window_count = len(windows) for desc, buffers in buffer_groups.iteritems(): buffer_count += len(buffers) results.append('%i %s' % (len(buffers), desc)) buffer_stats = ', '.join(sorted(results, key = lambda item: (int(item.partition(' ')[0]) if item[0].isdigit() else float('inf'), item),reverse=True)) # descending numerical sort of strings stats_string = '%i buffers (%i merged): %s; %i windows' % (buffer_count, merge_count, buffer_stats, window_count) if '-split' in args: stats_string += ": %i vertically / %i horizontally split" % (len(windows_v), len(windows_h)) w.command("", "/input insert %s" % stats_string) return w.WEECHAT_RC_OK
def command_main(data, buffer, args): infolist = w.infolist_get("buffer", "", "") buffer_groups = {} results = [] buffer_count = 0 merge_count = 0 numbers = set() while w.infolist_next(infolist): bplugin = w.infolist_string(infolist, "plugin_name") bname = w.infolist_string(infolist, "name") bpointer = w.infolist_pointer(infolist, "pointer") bnumber = w.infolist_integer(infolist, "number") if not bnumber in numbers: numbers.add(bnumber) else: merge_count += 1 btype = bplugin if bplugin == 'irc': if 'server.' in bname: btype = '%s servers' % btype elif '#' in bname: btype = '%s channels' % btype else: btype = '%s queries' % btype buffer_groups.setdefault(btype,[]).append({'name': bname, 'pointer': bpointer}) w.infolist_free(infolist) infolist = w.infolist_get("window", "", "") windows_v = set() windows_h = set() windows = set() while w.infolist_next(infolist): window = w.infolist_pointer(infolist, "pointer") window_w = w.infolist_integer(infolist, "width_pct") window_h = w.infolist_integer(infolist, "height_pct") windows.add(window) if window_h == 100 and window_w != 100: windows_v.add(window) elif window_w == 100 and window_h != 100: windows_h.add(window) #else: #both 100%, thus no splits w.infolist_free(infolist) window_count = len(windows) for bplugin, buffers in buffer_groups.iteritems(): buffer_count += len(buffers) results.append('%i %s' % (len(buffers), bplugin)) buffer_stats = ', '.join(sorted(results)) stats_string = '%i windows used (%i vertically / %i horizontally split). %i (of which %i merged) buffers open: %s' % (window_count, len(windows_v), len(windows_h), buffer_count, merge_count, buffer_stats) w.command("", "/input insert %s" % stats_string) return w.WEECHAT_RC_OK
def init_weechat(self): """ Initialize theme using current WeeChat options (aliases are replaced with their values from palette). """ # get palette options self.palette = {} infolist = weechat.infolist_get('option', '', 'weechat.palette.*') while weechat.infolist_next(infolist): option_name = weechat.infolist_string(infolist, 'option_name') value = weechat.infolist_string(infolist, 'value') self.palette[value] = option_name weechat.infolist_free(infolist) # get color options (replace aliases by values from palette) self.options = {} infolist = weechat.infolist_get('option', '', '') while weechat.infolist_next(infolist): full_name = weechat.infolist_string(infolist, 'full_name') if self._option_is_used(full_name): value = weechat.infolist_string(infolist, 'value') self.options[full_name] = self._get_color_without_alias(value) weechat.infolist_free(infolist) # replace aliases in chat_nick_colors option = 'weechat.color.chat_nick_colors' colors = [] for color in self.options.get(option, '').split(','): colors.append(self._get_color_without_alias(color)) if colors: self.options[option] = ','.join(colors) # replace aliases in buffer_time_format option = 'weechat.look.buffer_time_format' if option in self.options: value = re.compile(r'\$\{color:[^\}]+\}').sub( self._replace_color_alias, self.options[option]) if value: self.options[option] = value # build dict with nick prefixes (and replace alisases) prefixes = [] option = 'irc.color.nick_prefixes' for prefix in self.options.get(option, '').split(';'): values = prefix.split(':', 1) if len(values) == 2: prefixes.append(values[0] + ':' + self._get_color_without_alias(values[1])) if prefixes: self.options[option] = ';'.join(prefixes) # delete palette del self.palette
def get_list_commands(plugin, input_cmd, input_args): """Get list of commands (beginning with current input).""" global cmdhelp_settings infolist = weechat.infolist_get('hook', '', 'command,%s*' % input_cmd) commands = [] plugin_names = [] while weechat.infolist_next(infolist): commands.append(weechat.infolist_string(infolist, 'command')) plugin_names.append( weechat.infolist_string(infolist, 'plugin_name') or 'core') weechat.infolist_free(infolist) if commands: if len(commands) > 1 or commands[0].lower() != input_cmd.lower(): commands2 = [] for index, command in enumerate(commands): if commands.count(command) > 1: commands2.append('%s(%s)' % (command, plugin_names[index])) else: commands2.append(command) return '%s%d commands: %s%s' % ( weechat.color(cmdhelp_settings['color_list_count']), len(commands2), weechat.color(cmdhelp_settings['color_list']), ', '.join(commands2)) return None
def match_against_nicklist(server, channel, hostmask): """Compare the hostmask against all users in the channel""" infolist = w.infolist_get("irc_nick", "", "{},{}".format(server, channel)) if "$a:" in hostmask or "$~a" in hostmask: field = "account" hostmask = hostmask.replace("$a:", "") hostfield = False else: field = "host" hostfield = True extban_unreg = hostmask == "$~a" matches = [] while w.infolist_next(infolist): name = w.infolist_string(infolist, "name") if hostfield: host = name + "!" + w.infolist_string(infolist, field) else: host = w.infolist_string(infolist, field) if ((extban_unreg and host == "*") or (not extban_unreg and w.string_match(host, hostmask, 0))): matches.append(name) w.infolist_free(infolist) return matches
def save_history(): global history_list # get buffers ptr_infolist_buffer = weechat.infolist_get('buffer','','') while weechat.infolist_next(ptr_infolist_buffer): ptr_buffer = weechat.infolist_pointer(ptr_infolist_buffer,'pointer') # check for localvar_save_history if not weechat.buffer_get_string(ptr_buffer, 'localvar_save_history') and OPTIONS['save_buffer'].lower() == 'off': continue plugin = weechat.buffer_get_string(ptr_buffer, 'localvar_plugin') name = weechat.buffer_get_string(ptr_buffer, 'localvar_name') filename = get_filename_with_path('%s.%s' % (plugin,name)) get_buffer_history(ptr_buffer) if len(history_list): write_history(filename) weechat.infolist_free(ptr_infolist_buffer) if OPTIONS['save_global'].lower() != 'off': get_buffer_history('') # buffer pointer (if not set, return global history) if len(history_list): write_history(filename_global_history)
def update_user_count(server=None, channel=None): if isinstance(channel, str): channel = set((channel, )) elif channel: channel = set(channel) def update_channel(server, channel=None): channel_infolist = weechat.infolist_get('irc_channel', '', server) while weechat.infolist_next(channel_infolist): _channel = weechat.infolist_string(channel_infolist, 'name') if channel: _channel = caseInsensibleKey(_channel) if _channel not in channel: continue channel_stats[server, _channel] = weechat.infolist_integer(channel_infolist, 'nicks_count') weechat.infolist_free(channel_infolist) if not server: server_infolist = weechat.infolist_get('irc_server', '', '') while weechat.infolist_next(server_infolist): server = weechat.infolist_string(server_infolist, 'name') update_channel(server) weechat.infolist_free(server_infolist) else: update_channel(server, channel)
def get_help_command(plugin, input_cmd, input_args): """Get help for command in input.""" global cmdhelp_settings if input_cmd == 'set' and input_args: return get_help_option(input_args) infolist = weechat.infolist_get('hook', '', 'command,%s' % input_cmd) cmd_plugin_name = '' cmd_command = '' cmd_args = '' cmd_desc = '' while weechat.infolist_next(infolist): cmd_plugin_name = (weechat.infolist_string(infolist, 'plugin_name') or 'core') cmd_command = weechat.infolist_string(infolist, 'command') cmd_args = weechat.infolist_string(infolist, 'args_nls') cmd_desc = weechat.infolist_string(infolist, 'description') if weechat.infolist_pointer(infolist, 'plugin') == plugin: break weechat.infolist_free(infolist) if cmd_plugin_name == 'alias': return '%sAlias %s%s%s => %s%s' % ( weechat.color(cmdhelp_settings['color_alias']), weechat.color(cmdhelp_settings['color_alias_name']), cmd_command, weechat.color(cmdhelp_settings['color_alias']), weechat.color(cmdhelp_settings['color_alias_value']), cmd_desc, ) if input_args: cmd_args = get_command_arguments(input_args, cmd_args) if not cmd_args: return None return '%s%s' % (weechat.color(cmdhelp_settings['color_arguments']), cmd_args)
def bufsave_cmd(data, buffer, args): ''' Callback for /bufsave command ''' filename = args if not filename: w.command('', '/help %s' %SCRIPT_COMMAND) return w.WEECHAT_RC_OK if exists(filename): w.prnt('', "Error: target file already exists!") return w.WEECHAT_RC_OK infolist = w.infolist_get('buffer_lines', buffer, '') channel = w.buffer_get_string(buffer, 'name') try: fp = file(filename, 'w') except: w.prnt('', "Error writing to target file!") return w.WEECHAT_RC_OK while w.infolist_next(infolist): fp.write('%s %s %s\n' %(\ w.infolist_time(infolist, 'date'), cstrip(w.infolist_string(infolist, 'prefix')), cstrip(w.infolist_string(infolist, 'message')), )) w.infolist_free(infolist) return w.WEECHAT_RC_OK
def gen_infolist_get(infolist_name, arguments, pointer=""): """ Same as infolist_get(), but yields it's elements. Be sure to iterate through the whole list, to ensure weechat.infolist_free() is called. """ infolist = weechat.infolist_get(infolist_name, pointer, arguments) if infolist: while weechat.infolist_next(infolist): fields = weechat.infolist_fields(infolist).split(',') field_names = [] list_element = {} for field in fields: field_type, field_name = field.split(':') field_names.append(field_name) # decide which function to use info_func = { 'i': weechat.infolist_integer, 's': weechat.infolist_string, 'p': weechat.infolist_pointer, # 'b': weechat.infolist_buffer, 't': weechat.infolist_time, }[field_type] value = info_func(infolist, field_name) list_element[field_name] = value # create a temporary namedtuple type using field_names item_tpl = namedtuple('InfolistItem', field_names) yield item_tpl(**list_element) weechat.infolist_free(infolist)
def get_option_list_and_desc(option, displayname): """Get list of options and description for option(s).""" global cmdhelp_settings, cmdhelp_option_infolist global cmdhelp_option_infolist_fields options = [] description = '' cmdhelp_option_infolist = weechat.infolist_get('option', '', option) if cmdhelp_option_infolist: cmdhelp_option_infolist_fields = {} while weechat.infolist_next(cmdhelp_option_infolist): options.append(weechat.infolist_string(cmdhelp_option_infolist, 'full_name')) if not description: fields = weechat.infolist_fields(cmdhelp_option_infolist) for field in fields.split(','): items = field.split(':', 1) if len(items) == 2: cmdhelp_option_infolist_fields[items[1]] = items[0] description = re.compile(r'\$\{[^\}]+\}').sub( format_option, cmdhelp_settings['format_option']) if displayname: description = '%s%s%s: %s' % ( weechat.color(cmdhelp_settings['color_option_name']), weechat.infolist_string(cmdhelp_option_infolist, 'full_name'), weechat.color(cmdhelp_settings['color_option_help']), description) weechat.infolist_free(cmdhelp_option_infolist) cmdhelp_option_infolist = '' cmdhelp_option_infolist_fields = {} return options, description
def bas_config_option_cb(data, option, value): if not weechat.config_boolean(bas_options["look_instant"]): return weechat.WEECHAT_RC_OK if not weechat.config_get(option): # option was deleted return weechat.WEECHAT_RC_OK option = option[len("%s.buffer." % CONFIG_FILE_NAME):] pos = option.rfind(".") if pos > 0: buffer_mask = option[0:pos] property = option[pos+1:] if buffer_mask and property: buffers = weechat.infolist_get("buffer", "", buffer_mask) if not buffers: return weechat.WEECHAT_RC_OK while weechat.infolist_next(buffers): buffer = weechat.infolist_pointer(buffers, "pointer") weechat.buffer_set(buffer, property, value) weechat.infolist_free(buffers) return weechat.WEECHAT_RC_OK
def get_hank_home(): infolist = weechat.infolist_get("python_script", "", "hankbot") fname = "~/hank/hank.py" while weechat.infolist_next(infolist): fname = weechat.infolist_string(infolist, "filename") weechat.infolist_free(infolist) return os.path.dirname(os.path.realpath(fname))
def check_buffer_timer_cb(data, remaining_calls): global WEECHAT_VERSION,whitelist # search for buffers in hotlist ptr_infolist = weechat.infolist_get("hotlist", "", "") while weechat.infolist_next(ptr_infolist): ptr_buffer = weechat.infolist_pointer(ptr_infolist, "buffer_pointer") localvar_name = weechat.buffer_get_string(ptr_buffer, 'localvar_name') # buffer in whitelist? go to next buffer buf_type = weechat.buffer_get_string(ptr_buffer,'localvar_type') # buffer is a query buffer? if OPTIONS['ignore_query'].lower() == 'on' and buf_type == 'private': continue # buffer in whitelist? if localvar_name in whitelist: continue if ptr_buffer: if get_time_from_line(ptr_buffer): if OPTIONS['clear'].lower() == 'hotlist' or OPTIONS['clear'].lower() == 'all': weechat.buffer_set(ptr_buffer, "hotlist", '-1') if OPTIONS['clear'].lower() == 'unread' or OPTIONS['clear'].lower() == 'all': weechat.command(ptr_buffer,"/input set_unread_current_buffer") weechat.infolist_free(ptr_infolist) return weechat.WEECHAT_RC_OK
def allquery_command_cb(data, buffer, args): """ Callback for /allquery command """ args = args.strip() if args == "": weechat.command("", "/help %s" % SCRIPT_COMMAND) return weechat.WEECHAT_RC_OK argv = args.split(" ") exclude_nick = None if argv[0].startswith("-exclude="): exclude_nick = make_list(argv[0]) command = " ".join(argv[1::]) else: command = args if not command.startswith("/"): weechat.command("", "/help %s" % SCRIPT_COMMAND) return weechat.WEECHAT_RC_OK infolist = weechat.infolist_get("buffer", "", "") while weechat.infolist_next(infolist): if weechat.infolist_string(infolist, "plugin_name") == "irc": ptr = weechat.infolist_pointer(infolist, "pointer") server = weechat.buffer_get_string(ptr, "localvar_server") query = weechat.buffer_get_string(ptr, "localvar_channel") execute_command = re.sub(r'\b\$nick\b', query, command) if weechat.buffer_get_string(ptr, "localvar_type") == "private": if exclude_nick is not None: if not query in exclude_nick: weechat.command(ptr, execute_command) else: weechat.command(ptr, execute_command) weechat.infolist_free(infolist) return weechat.WEECHAT_RC_OK
def _find_first_buffer_number(): infolist = weechat.infolist_get('buffer', '', '') while weechat.infolist_next(infolist): short_name = weechat.infolist_string(infolist, 'short_name') if _newserv_match(short_name): number = weechat.infolist_integer(infolist, 'number') return number
def get_patterns_in_config(filter): d = {} infolist = weechat.infolist_get('option', '', 'plugins.var.python.%s.%s' %(SCRIPT_NAME, filter)) while weechat.infolist_next(infolist): name = weechat.infolist_string(infolist, 'option_name') name = name[len('python.%s.' %SCRIPT_NAME):] # channels might have dots in their names, so we'll strip type from right and server # from left. Lets hope that users doesn't use dots in server names. name, _, type = name.rpartition('.') if type not in ('op', 'halfop', 'voice'): # invalid option continue server, _, channel = name.partition('.') value = weechat.infolist_string(infolist, 'value') if not value: continue else: value = value.split(',') key = (server, channel) if key not in d: d[key] = {type:value} else: d[key][type] = value weechat.infolist_free(infolist) return d
def update_title(data, signal, signal_data): ''' The callback that adds title. ''' if w.config_get_plugin('short_name') == 'on': title = w.buffer_get_string(w.current_buffer(), 'short_name') else: title = w.buffer_get_string(w.current_buffer(), 'name') hotlist = w.infolist_get('hotlist', '', '') hot_text = '' while w.infolist_next(hotlist): priority = w.infolist_integer(hotlist, 'priority') if priority >= int(w.config_get_plugin('title_priority')): number = w.infolist_integer(hotlist, 'buffer_number') thebuffer = w.infolist_pointer(hotlist, 'buffer_pointer') name = w.buffer_get_string(thebuffer, 'short_name') hot_text += ' %s' % number if hot_text: title += ' [A:%s]' % hot_text w.infolist_free(hotlist) w.window_set_title(title) return w.WEECHAT_RC_OK
def hotlist_dict(): """Return the contents of the hotlist as a dictionary. The returned dictionary has the following structure: >>> hotlist = { ... "0x0": { # string representation of the buffer pointer ... "count_low": 0, ... "count_message": 0, ... "count_private": 0, ... "count_highlight": 0, ... } ... } """ hotlist = {} infolist = weechat.infolist_get("hotlist", "", "") while weechat.infolist_next(infolist): buffer_pointer = weechat.infolist_pointer(infolist, "buffer_pointer") hotlist[buffer_pointer] = {} hotlist[buffer_pointer]["count_low"] = weechat.infolist_integer( infolist, "count_00") hotlist[buffer_pointer]["count_message"] = weechat.infolist_integer( infolist, "count_01") hotlist[buffer_pointer]["count_private"] = weechat.infolist_integer( infolist, "count_02") hotlist[buffer_pointer]["count_highlight"] = weechat.infolist_integer( infolist, "count_03") weechat.infolist_free(infolist) return hotlist
def hotlist_cache_update_cb(data, remaining_calls): #this keeps the hotlist dupe up to date for the buffer switch, but is prob technically a race condition. (meh) global hotlist prev_hotlist = hotlist hotlist = w.infolist_get("hotlist", "", "") w.infolist_free(prev_hotlist) return w.WEECHAT_RC_OK
def buffer_count(): buffer_count = 0 buffer = weechat.infolist_get("buffer", "", "") while weechat.infolist_next(buffer): buffer_count += 1 weechat.infolist_free(buffer) return buffer_count
def get_clones_for_buffer(infolist_buffer_name, hostname_to_match=None): matches = {} infolist = weechat.infolist_get("irc_nick", "", infolist_buffer_name) while(weechat.infolist_next(infolist)): ident_hostname = weechat.infolist_string(infolist, "host") host_matchdata = re.match('([^@]+)@(\S+)', ident_hostname) if not host_matchdata: continue hostname = host_matchdata.group(2).lower() ident = host_matchdata.group(1).lower() if weechat.config_get_plugin("compare_idents") == "on": hostkey = ident_hostname.lower() else: hostkey = hostname if hostname_to_match and hostname_to_match.lower() != hostkey: continue nick = weechat.infolist_string(infolist, "name") matches.setdefault(hostkey,[]).append({ 'nick': nick, 'mask': "%s!%s" % ( format_from_config(nick, "colors.mask.nick"), format_from_config(ident_hostname, "colors.mask.identhost")), 'ident': ident, 'ident_hostname': ident_hostname, 'hostname': hostname, }) weechat.infolist_free(infolist) #Select only the results that have more than 1 match for a host return dict((k, v) for (k, v) in matches.iteritems() if len(v) > 1)
def reorder_buffers(): global buffers bufcopy = dict(buffers) priolist = [] while len(bufcopy): priolist.append(max(bufcopy, key=bufcopy.get)) bufcopy.pop(max(bufcopy, key=bufcopy.get)) pointerlist = {} infolist = wee.infolist_get("buffer", "", "") while wee.infolist_next(infolist): # go through the buffers and jot down relevant pointers for name in priolist: try: bufname = wee.infolist_string(infolist, "name").split('.', 1)[1] except IndexError: bufname = wee.infolist_string(infolist, "name") if name == bufname: if name in pointerlist: pointerlist[name].append( wee.infolist_pointer(infolist, "pointer")) else: pointerlist[name] = [wee.infolist_pointer( infolist, "pointer")] index = 1 if(maintop): index += 1 for name in priolist: if name in pointerlist: for pointer in pointerlist[name]: wee.buffer_set(pointer, "number", str(index)) index += 1 return
def get_matching_buffers(input): """ Return list with buffers matching user input """ global buffers_pos list = [] if len(input) == 0: buffers_pos = 0 input = input.lower() infolist = weechat.infolist_get("buffer", "", "") while weechat.infolist_next(infolist): if weechat.config_get_plugin("short_name") == "on": name = weechat.infolist_string(infolist, "short_name") else: name = weechat.infolist_string(infolist, "name") number = weechat.infolist_integer(infolist, "number") matching = name.lower().find(input) >= 0 if not matching and input[-1] == ' ': matching = name.lower().endswith(input.strip()) if not matching and input.isdigit(): matching = str(number).startswith(input) if len(input) == 0 or matching: list.append({"number": number, "name": name}) if len(input) == 0 and weechat.infolist_pointer(infolist, "pointer") == weechat.current_buffer(): buffers_pos = len(list) - 1 weechat.infolist_free(infolist) return list
def servernicks(servername): infolist = weechat.infolist_get('irc_server','',servername) weechat.infolist_next(infolist) nicks = weechat.infolist_string(infolist, 'nicks') weechat.infolist_free(infolist) servernicks = nicks.split(',') return servernicks
def get_irc_servers(): """ Returns a list of configured IRC servers in weechat""" serverptrlist = weechat.infolist_get('irc_server', '', '') serverlist = [] while weechat.infolist_next(serverptrlist): serverlist.append(weechat.infolist_string(serverptrlist, 'name')) return serverlist
def get_options(): """ Get list of config options in a dict with 4 indexes: config, section, option, xxx. """ global plugin_list, ignore_options options = \ defaultdict(lambda: defaultdict(lambda: defaultdict(defaultdict))) infolist = weechat.infolist_get('option', '', '') while weechat.infolist_next(infolist): full_name = weechat.infolist_string(infolist, 'full_name') if not re.search('|'.join(ignore_options), full_name): config = weechat.infolist_string(infolist, 'config_name') if config in plugin_list and 'o' in plugin_list[config]: section = weechat.infolist_string(infolist, 'section_name') option = weechat.infolist_string(infolist, 'option_name') for key in ('type', 'string_values', 'default_value', 'description'): options[config][section][option][key] = \ weechat.infolist_string(infolist, key) for key in ('min', 'max', 'null_value_allowed'): options[config][section][option][key] = \ weechat.infolist_integer(infolist, key) weechat.infolist_free(infolist) return options
def reorder_buffers(): """Reorders the buffers once the whitelist has changed """ count = 2 chan = "" ilist = weechat.infolist_get("buffer", "", "") for chan in whitelist: if -1 == chan.find(".#"): #network name is not set, matching by short_name weechat.infolist_reset_item_cursor(ilist) while weechat.infolist_next(ilist): if weechat.infolist_string(ilist, "short_name") == chan: chan = weechat.infolist_string(ilist, "name") break buff = weechat.buffer_search("irc", chan) if buff: weechat.buffer_set(buff, "number", str(count)) count += 1 weechat.infolist_free(ilist) return weechat.WEECHAT_RC_OK
def update_title(data, signal, signal_data): title = w.buffer_get_string(w.current_buffer(), 'name') num = w.buffer_get_integer(w.current_buffer(), 'number') title = w.string_remove_color(title, '') title = "[WeeChat] [" + str(num) + ":" + title + "]" hotlist = w.infolist_get('hotlist', '', '') while w.infolist_next(hotlist): number = w.infolist_integer(hotlist, 'buffer_number') thebuffer = w.infolist_pointer(hotlist, 'buffer_pointer') name = w.buffer_get_string(thebuffer, 'short_name') if not number == num: title += ' (%s:%s)' % (number, name) w.infolist_free(hotlist) w.window_set_title(title) return w.WEECHAT_RC_OK
def get_default_aliases(): """ Get list of default aliases as list of dictionaries. """ default_aliases = [] infolist = weechat.infolist_get('alias_default', '', '') while weechat.infolist_next(infolist): default_aliases.append({ 'name': '/' + weechat.infolist_string(infolist, 'name'), 'command': '/' + weechat.infolist_string(infolist, 'command'), 'completion': weechat.infolist_string(infolist, 'completion'), }) weechat.infolist_free(infolist) return default_aliases
def get_buffers(): ''' Get a list of all the buffers in weechat. ''' buffers = [] buffer_list = weechat.infolist_get('buffer', '', '') while weechat.infolist_next(buffer_list): name = weechat.infolist_string(buffer_list, 'full_name') number = weechat.infolist_integer(buffer_list, 'number') # Buffer is merged with one we already have in the list, skip it. if number <= len(buffers): continue buffers.append((name, number - 1)) weechat.infolist_free(buffer_list) return buffers
def get_commands(): """ Get list of commands in a dict with 3 indexes: plugin, command, xxx. """ global plugin_list commands = defaultdict(lambda: defaultdict(defaultdict)) infolist = weechat.infolist_get('hook', '', 'command') while weechat.infolist_next(infolist): plugin = weechat.infolist_string(infolist, 'plugin_name') or 'weechat' if plugin in plugin_list: command = weechat.infolist_string(infolist, 'command') if command == plugin or 'c' in plugin_list[plugin]: for key in ('description', 'args', 'args_description', 'completion'): commands[plugin][command][key] = \ weechat.infolist_string(infolist, key) weechat.infolist_free(infolist) return commands
def run_alert(srv, chn, from_nick): buffer = weechat.info_get("irc_buffer", srv + "," + chn) if not buffer: return nicks = [] nicklist = weechat.infolist_get("nicklist", buffer, "") is_op = False while weechat.infolist_next(nicklist): nick = weechat.infolist_string(nicklist, "name") nick_type = weechat.infolist_string(nicklist, "type") nick_prefix = weechat.infolist_string(nicklist, "prefix") if nick_type == "nick": nicks.append(nick) if nick == from_nick and (nick_prefix == "@" or nick_prefix == "&"): is_op = True weechat.infolist_free(nicklist) if is_op: say(srv, chn, "Alert: " + (", ".join(nicks)))
def get_url_options(): """ Get list of completions hooked by plugins in a dict with 3 indexes: plugin, item, xxx. """ url_options = [] infolist = weechat.infolist_get('url_options', '', '') while weechat.infolist_next(infolist): url_options.append( { 'name': weechat.infolist_string(infolist, 'name').lower(), 'option': weechat.infolist_integer(infolist, 'option'), 'type': weechat.infolist_string(infolist, 'type'), 'constants': weechat.infolist_string(infolist, 'constants') .lower().replace(',', ', ') }) weechat.infolist_free(infolist) return url_options
def hook_print_callback(data, buffer, date, tags, displayed, highlight, prefix, message): if weechat.config_get_plugin('short_own') == 'on': # get servername infolist = weechat.infolist_get('buffer', buffer, '') weechat.infolist_next(infolist) servername, undef = weechat.infolist_string(infolist, 'name').split('.', 1) weechat.infolist_free(infolist) # get own nick my_nick = weechat.info_get('irc_nick', servername) if my_nick in tags: return weechat.WEECHAT_RC_OK return match_url(message, buffer, False) return weechat.WEECHAT_RC_OK
def rlayout_bar_cb(data, item, window): infolist = weechat.infolist_get("layout", "", "") layout = None while weechat.infolist_next(infolist): if weechat.infolist_integer(infolist, "current_layout") == 1: layout = weechat.infolist_string(infolist, "name") break weechat.infolist_free(infolist) if layout is None: return "" else: term_height = int(weechat.info_get("term_height", "")) term_width = int(weechat.info_get("term_width", "")) return "%s (%sx%s)" % (layout, term_width, term_height)
def _read_api_completions(): """ Get list of WeeChat/plugins completions as dictionary with 3 indexes: plugin, item, xxx. """ completions = defaultdict(lambda: defaultdict(defaultdict)) infolist = weechat.infolist_get('hook', '', 'completion') while weechat.infolist_next(infolist): completion_item = weechat.infolist_string(infolist, 'completion_item') if not re.search('|'.join(IGNORE_COMPLETIONS_ITEMS), completion_item): plugin = (weechat.infolist_string(infolist, 'plugin_name') or 'weechat') completions[plugin][completion_item]['description'] = \ weechat.infolist_string(infolist, 'description') weechat.infolist_free(infolist) return completions
def get_commands(): """ Get list of WeeChat/plugins commands as dictionary with 3 indexes: plugin, command, xxx. """ commands = defaultdict(lambda: defaultdict(defaultdict)) infolist = weechat.infolist_get('hook', '', 'command') while weechat.infolist_next(infolist): plugin = weechat.infolist_string(infolist, 'plugin_name') or 'weechat' if plugin in PLUGIN_LIST: command = weechat.infolist_string(infolist, 'command') if command == plugin or 'c' in PLUGIN_LIST[plugin]: for key in ('description', 'args', 'args_description', 'completion'): commands[plugin][command][key] = \ weechat.infolist_string(infolist, key) weechat.infolist_free(infolist) return commands
def set_back(overridable_messages): """Removes away status for servers where one of the overridable_messages is set""" if (weechat.config_get_plugin('away') == '0'): return # No need to come back again serverlist = weechat.infolist_get('irc_server', '', '') if serverlist: buffers = [] while weechat.infolist_next(serverlist): if (weechat.infolist_string(serverlist, 'away_message') in overridable_messages): ptr = weechat.infolist_pointer(serverlist, 'buffer') if ptr: buffers.append(ptr) weechat.infolist_free(serverlist) for buffer in buffers: weechat.command(buffer, "/away") weechat.config_set_plugin('away', '0')
def check_nicks(data, remaining_calls): serverlist = OPTIONS['serverlist'].split(',') infolist = weechat.infolist_get('irc_server', '', '') while weechat.infolist_next(infolist): servername = weechat.infolist_string(infolist, 'name') ptr_buffer = weechat.infolist_pointer(infolist, 'buffer') nick = weechat.infolist_string(infolist, 'nick') ssl_connected = weechat.infolist_integer(infolist, 'ssl_connected') is_connected = weechat.infolist_integer(infolist, 'is_connected') server_matched = re.search(r"\b({})\b".format("|".join(serverlist)), servername) if servername in serverlist or server_matched and is_connected: if nick and ssl_connected + is_connected: ison(ptr_buffer, servername, nick, server_nicks(servername)) weechat.infolist_free(infolist) return weechat.WEECHAT_RC_OK
def is_logged(self): """Return True if conversations with this context's peer are currently being logged to disk.""" infolist = weechat.infolist_get('logger_buffer', '', '') buf = self.buffer() result = False while weechat.infolist_next(infolist): if weechat.infolist_pointer(infolist, 'buffer') == buf: result = bool(weechat.infolist_integer(infolist, 'log_enabled')) break weechat.infolist_free(infolist) return result
def get_servers(): '''Get the servers that are not away, or were set away by this script''' ignores = w.config_get_plugin('ignore').split(',') infolist = w.infolist_get('irc_server','','') buffers = [] while w.infolist_next(infolist): if not w.infolist_integer(infolist, 'is_connected') == 1 or \ w.infolist_string(infolist, 'name') in ignores: continue if not w.config_string_to_boolean(w.config_get_plugin('set_away')) or \ not w.infolist_integer(infolist, 'is_away') or \ w.infolist_string(infolist, 'away_message') == \ w.config_get_plugin('message'): buffers.append((w.infolist_pointer(infolist, 'buffer'), w.infolist_string(infolist, 'nick'))) w.infolist_free(infolist) return buffers
def retrieve_channels(server): """Get a list of the channels the user is in""" isupport_chantypes = w.info_get("irc_server_isupport_value", "{},CHANTYPES".format(server)) ischan_regex = re.compile("^{}\.[{}]".format( re.escape(server), re.escape(isupport_chantypes))) chans = [] infolist = w.infolist_get("buffer", "", "") while w.infolist_next(infolist): bname = w.infolist_string(infolist, "name") if ischan_regex.match(bname): chans.append(bname) w.infolist_free(infolist) return chans
def get_current_query_buffers(): stored_query_buffers_per_server = {} ptr_infolist_buffer = weechat.infolist_get('buffer', '', '') while weechat.infolist_next(ptr_infolist_buffer): ptr_buffer = weechat.infolist_pointer(ptr_infolist_buffer, 'pointer') buf_type = weechat.buffer_get_string(ptr_buffer, 'localvar_type') if buf_type == 'private': server_name = weechat.buffer_get_string(ptr_buffer, 'localvar_server') channel_name = weechat.buffer_get_string(ptr_buffer, 'localvar_channel') stored_query_buffers_per_server.setdefault(server_name, set([])) stored_query_buffers_per_server[server_name].add(channel_name) weechat.infolist_free(ptr_infolist_buffer) return stored_query_buffers_per_server
def get_fit_size(self): widths = [] heights = [] infolist = weechat.infolist_get("window", "", "") while weechat.infolist_next(infolist): buffer = weechat.infolist_pointer(infolist, "buffer") if buffer == self.buffer: width = weechat.infolist_integer(infolist, "chat_width") height = weechat.infolist_integer(infolist, "chat_height") widths.append(width) heights.append(height) weechat.infolist_free(infolist) if widths and heights: return (min(heights), min(widths)) else: return None
def bas_signal_buffer_opened_cb(data, signal, signal_data): buffer = signal_data name = "%s.%s" % (weechat.buffer_get_string(buffer, "plugin"), weechat.buffer_get_string(buffer, "name")) options = weechat.infolist_get("option", "", "%s.buffer.*" % CONFIG_FILE_NAME) if options: while weechat.infolist_next(options): option = weechat.infolist_string(options, "option_name") value = weechat.infolist_string(options, "value") if option: pos = option.rfind(".") if pos > 0: buffer_mask = option[0:pos] property = option[pos+1:] if buffer_mask and property: if weechat.string_match(name, buffer_mask, 1): weechat.buffer_set(buffer, property, value) weechat.infolist_free(options) return weechat.WEECHAT_RC_OK
def irc_channel_is_empty(channel, server): """Return True if we are the only nick in the channel.""" infolist = weechat.infolist_get('irc_nick', '', "%s,%s" % (server, channel)) n_members = 0 # Count users if infolist: while weechat.infolist_next(infolist): n_members += 1 weechat.infolist_free(infolist) # At least another person is in the channel. if n_members > 1: return False return True
def save_conf(args): """match options and save to config file""" try: f = open(get_config(args), 'w+') except Exception as e: w.prnt('', '%sError: %s' % (w.prefix('error'), e)) return w.WEECHAT_RC_ERROR header = [ '#', '# WeeChat %s (compiled on %s)' % (w.info_get('version', ''), w.info_get('date', '')), '#', '# Use /autoconf load or cat this file to the FIFO pipe.', '#', '# For more info, see https://weechat.org/scripts/source/autoconf.py.html', '#', '' ] for ln in header: f.write('%s\n' % ln) w.command('', '/buffer clear') w.command('', '/set diff') infolist = w.infolist_get('buffer_lines', '', '') while w.infolist_next(infolist): message = cstrip(w.infolist_string(infolist, 'message')) ignore = w.config_get_plugin('ignore').split(',') option = re.match(RE['option'], message) if option: if not any(fnmatch(option.group(1), p.strip()) for p in ignore): f.write('*/set %s %s\n' % (option.group(1), option.group(2))) f.close() w.infolist_free(infolist)
def bas_apply_options_for_buffer(buffer): full_name = weechat.buffer_get_string(buffer, "full_name") options = weechat.infolist_get("option", "", "%s.buffer.*" % CONFIG_FILE_NAME) if not options: return while weechat.infolist_next(options): option = weechat.infolist_string(options, "option_name") value = weechat.infolist_string(options, "value") if option: pos = option.rfind(".") if pos > 0: buffer_mask = option[0:pos] property = option[pos + 1:] if buffer_mask and property: if weechat.string_match(full_name, buffer_mask, 1): weechat.buffer_set(buffer, property, value) weechat.infolist_free(options)
def get_url_options(): """ Get list of URL options as list of dictionaries. """ url_options = [] infolist = weechat.infolist_get('url_options', '', '') while weechat.infolist_next(infolist): url_options.append({ 'name': weechat.infolist_string(infolist, 'name').lower(), 'option': weechat.infolist_integer(infolist, 'option'), 'type': weechat.infolist_string(infolist, 'type'), 'constants': weechat.infolist_string(infolist, 'constants').lower().replace(',', ', ') }) weechat.infolist_free(infolist) return url_options
def bar_item_update(signal, callback, callback_data): ptr_infolist_option = weechat.infolist_get( 'option', '', 'plugins.var.python.' + SCRIPT_NAME + '.*') if not ptr_infolist_option: return while weechat.infolist_next(ptr_infolist_option): option_full_name = weechat.infolist_string(ptr_infolist_option, 'full_name') option_name = option_full_name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname # check if item exists in a bar and if we have a hook for it if weechat.bar_item_search(option_name) and option_name in hooks: weechat.bar_item_update(option_name) weechat.infolist_free(ptr_infolist_option) return weechat.WEECHAT_RC_OK
def rlayouts_list(): """ Return a list of configured rlayouts. """ layouts = [] pattern = re.compile(r"^plugins\.var\.python\.%s\.layout\.(.+)\." % SCRIPT_NAME) infolist = weechat.infolist_get( "option", "", "plugins.var.python.%s.layout.*" % SCRIPT_NAME) while weechat.infolist_next(infolist): layout = re.search(pattern, weechat.infolist_string(infolist, "full_name")).groups() if layout[0] not in layouts: layouts.append(layout[0]) weechat.infolist_free(infolist) return layouts
def hotlist_changed(data, signal, signal_data): hotlist = weechat.infolist_get('hotlist', '', '') data = [] while weechat.infolist_next(hotlist): priority = weechat.infolist_integer(hotlist, 'priority') plugin_name = weechat.infolist_string(hotlist, 'plugin_name') buffer_name = weechat.infolist_string(hotlist, 'buffer_name') buffer_number = weechat.infolist_integer(hotlist, 'buffer_number') low_messages = weechat.infolist_integer(hotlist, 'count_00') channel_messages = weechat.infolist_integer(hotlist, 'count_01') private_messages = weechat.infolist_integer(hotlist, 'count_02') highlight_messages = weechat.infolist_integer(hotlist, 'count_03') buffer_pointer = weechat.infolist_pointer(hotlist, 'buffer_pointer') short_name = weechat.buffer_get_string(buffer_pointer, 'short_name') data.append({ 'priority': priority, 'plugin_name': plugin_name, 'buffer_name': buffer_name, 'buffer_number': buffer_number, 'low_messages': low_messages, 'channel_messages': channel_messages, 'private_messages': private_messages, 'highlight_messages': highlight_messages, 0: low_messages, 1: channel_messages, 2: private_messages, 3: highlight_messages, 'short_name': short_name }) weechat.infolist_free(hotlist) write_file({'hotlist': data}) return weechat.WEECHAT_RC_OK
def grep_buffer(buffer, head, tail, *args): """Return a list of lines that match 'regexp' in 'buffer', if no regexp returns all lines.""" lines = [] # Using /grep in grep's buffer can lead to some funny effects # We should take measures if that's the case grep_buffer = weechat.buffer_search('python', SCRIPT_NAME) grep_buffer = buffer == grep_buffer infolist = weechat.infolist_get('buffer_lines', buffer, '') if tail: # like with grep_file() if we need the last few matching lines, we move the cursor to # the end and search backwards infolist_next = weechat.infolist_prev else: infolist_next = weechat.infolist_next limit = head or tail append = lines.append check = check_string infolist_time = weechat.infolist_time while infolist_next(infolist): prefix = weechat.infolist_string(infolist, 'prefix') message = weechat.infolist_string(infolist, 'message') prefix = weechat.string_remove_color(prefix, '') message = weechat.string_remove_color(message, '') if grep_buffer: if script_nick == prefix: # ignore our lines continue date = prefix line = '%s\t%s' % (date, message.replace(' ', '\t', 1)) else: date = infolist_time(infolist, 'date') line = '%s\t%s\t%s' % (date, prefix, message) line = check(line, *args) if line: append(line) if limit and len(lines) >= limit: break weechat.infolist_free(infolist) if tail: lines.reverse() return lines
def create_bar_items(): ptr_infolist_option = weechat.infolist_get( 'option', '', 'plugins.var.python.' + SCRIPT_NAME + '.*') if not ptr_infolist_option: return while weechat.infolist_next(ptr_infolist_option): option_full_name = weechat.infolist_string(ptr_infolist_option, 'full_name') option_name = option_full_name[len('plugins.var.python.' + SCRIPT_NAME + '.'):] # get optionname if weechat.bar_item_search(option_name): weechat.bar_item_update(option_name) else: weechat.bar_item_new(option_name, 'update_item', option_name) weechat.bar_item_update(option_name) weechat.infolist_free(ptr_infolist_option)
def set_away(message, overridable_messages=[]): """Sets away status, but respectfully (so it doesn't change already set statuses""" if (weechat.config_get_plugin('away') == '1'): return # No need to go away again # (this prevents some repeated messages) serverlist = weechat.infolist_get('irc_server', '', '') if serverlist: buffers = [] while weechat.infolist_next(serverlist): if weechat.infolist_integer(serverlist, 'is_away') == 0: ptr = weechat.infolist_pointer(serverlist, 'buffer') if ptr: buffers.append(ptr) elif (weechat.infolist_string(serverlist, 'away_message') in overridable_messages): buffers.append(weechat.infolist_pointer(serverlist, 'buffer')) weechat.infolist_free(serverlist) for buffer in buffers: weechat.command(buffer, "/away %s" % message) weechat.config_set_plugin('away', '1')
def get_buffers(self): buffers = [] buffer_pnt = weechat.infolist_get("buffer", "", "") while weechat.infolist_next(buffer_pnt): buffer_plugin_name = weechat.infolist_string(buffer_pnt, "plugin_name") buffer_name = weechat.infolist_string(buffer_pnt, "name") buffer_number = weechat.infolist_integer(buffer_pnt, "number") buffer_number = int(buffer_number) if buffer_plugin_name == 'irc': (server, name) = tuple(buffer_name.split('.', 1)) buf = SortServers_Buffer(buffer_number, server, name) buffers.append(buf) weechat.infolist_free(buffer_pnt) return buffers
def idle_check(data, remain): try: timeout = int(w.config_get_plugin("timeout")) except ValueError: timeout = 0 message = w.config_get_plugin("message") inactivity = int(w.info_get("inactivity", "0")) / 60 last_action = (time.monotonic() - LAST_ACTION) / 60 inactivity = min(inactivity, last_action) if timeout > 0 and inactivity >= timeout: servers = w.infolist_get("irc_server", "", "") while w.infolist_next(servers): if (w.infolist_integer(servers, "is_connected") == 1 and w.infolist_integer(servers, "is_away") == 0): ptr = w.infolist_pointer(servers, "buffer") w.command(ptr, f"/away {message}") w.infolist_free(servers) return w.WEECHAT_RC_OK