def keydict_update(*args): '''Populate a python dictionary with relevant key=>buffer mappings.''' global keydict keylist = w.infolist_get('key', '', '') if w.config_get_plugin('use_keybindings') == 'on': while w.infolist_next(keylist): key = w.infolist_string(keylist, 'key') # we dont want jump sequences if 'j' in key: continue key = key.replace('meta-', '') key = key.replace('ctrl-', '^') if w.config_get_plugin('skip_number_binds') == 'on': # skip entries where buffer number = key, typically entries below 11 if key.isdigit(): continue command = w.infolist_string(keylist, 'command') # we only care about commands that leads to buffers if command.startswith('/buffer'): command = command.replace('/buffer ', '') buffer = command.lstrip('*') keydict[buffer] = key w.infolist_free(keylist) return w.WEECHAT_RC_OK
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 get_nicklist(server, channel): global options regex_flags = 0 if options['ignore_case']: regex_flags = re.IGNORECASE ignore_list = w.config_get_plugin('ignore_list') if ignore_list == '': ignore_match = lambda x: False else: ignore_match = re.compile('(%s)$' % ignore_list.replace(',', '|'), regex_flags).match server = w.buffer_get_string(w.current_buffer(), 'localvar_server') my_nick = w.info_get('irc_nick', server) nicklist = {} infolist_nicklist = w.infolist_get('nicklist', w.current_buffer(), '') while w.infolist_next(infolist_nicklist): nick = w.infolist_string(infolist_nicklist, 'name') prefix = w.infolist_string(infolist_nicklist, 'prefix') nick_type = w.infolist_string(infolist_nicklist, 'type') if nick_type != 'nick' or (options['ignore_self'] and nick == my_nick) or ignore_match(nick): pass else: if not nicklist.has_key(prefix): nicklist[prefix]=[] nicklist[prefix].append(nick) w.infolist_free(infolist_nicklist) return nicklist
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 find_channels(): """Return list of servers and channels""" 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(): 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: name = w.infolist_string(infolist, "name") key = w.infolist_string(infolist, "key") channels.append((name, key)) # sort by name, keyed channels first channels.sort(key=lambda c: (not c[1], c[0])) items[server] = ','.join(name for name, key in channels) keys = ','.join(key for name, key in channels if key) if keys: items[server] += ' ' + keys w.infolist_free(infolist) return items
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 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 buffer_opened_closed_cb(data, signal, signal_data): global OPTIONS # localvar not set in this moment? :-( # server = weechat.buffer_get_string(signal_data, 'localvar_server') # get internal servername infolist = weechat.infolist_get('buffer', signal_data, '') weechat.infolist_next(infolist) plugin_name = weechat.infolist_string(infolist, 'plugin_name') name = weechat.infolist_string(infolist, 'name') weechat.infolist_free(infolist) # TODO how about matrix script or other non-irc channel buffer? no idea! help is welcome if plugin_name != "irc": # for example /fset, /color etc.pp buffer return weechat.WEECHAT_RC_OK if OPTIONS['activity'].lower() == 'no' or OPTIONS['activity'].lower() == 'off' or OPTIONS['activity'].lower() == '0': weechat.command('','/allchan -exclude=%s /buffer hide' % OPTIONS['channel_exclude']) if not signal_data: # signal_data available? weechat.command(signal_data,'/allchan -current /buffer unhide') else: # signal_data empty! weechat.command('','/allchan /buffer hide') exclude_server('') single_channel_exclude() else: weechat.command('','/allchan /buffer hide') exclude_hotlist() return weechat.WEECHAT_RC_OK
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 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 channel_block(server, channel): fail = None config_cycle = lambda opt: weechat.config_string_to_boolean(weechat.config_get_plugin("cycle_%s" % opt)) channels = weechat.infolist_get("irc_channel", "", "%s,%s" % (server, channel)) if weechat.infolist_next(channels): modes = weechat.infolist_string(channels, "modes") if " " in modes: modes, modes_args = modes.split(" ", 1) if not config_cycle("key") and weechat.infolist_string(channels, "key") != "": fail = "cycle_key" elif not config_cycle("invite") and "i" in modes: fail = "cycle_invite" elif not config_cycle("detach"): fail = "cycle_detach" weechat.infolist_free(channels) if fail: weechat.prnt("", "%s: won't automatically cycle %s.%s: %s" % (SCRIPT_NAME, server, channel, fail)) else: servers[server]["channels"].append(channel) buffer = weechat.buffer_search("irc", server) weechat.command(buffer, "/part %s" % channel) weechat.command(buffer, "/nick %s" % servers[server]["nick"])
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 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_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 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 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 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 channel_block(server, channel): fail = None config_cycle = lambda opt: weechat.config_string_to_boolean( weechat.config_get_plugin("cycle_%s" % opt)) channels = weechat.infolist_get("irc_channel", "", "%s,%s" % (server, channel)) if weechat.infolist_next(channels): modes = weechat.infolist_string(channels, "modes") if " " in modes: modes, modes_args = modes.split(" ", 1) if not config_cycle("key") and weechat.infolist_string( channels, "key") != "": fail = "cycle_key" elif not config_cycle("invite") and "i" in modes: fail = "cycle_invite" elif not config_cycle("detach"): fail = "cycle_detach" weechat.infolist_free(channels) if fail: weechat.prnt( "", "%s: won't automatically cycle %s.%s: %s" % (SCRIPT_NAME, server, channel, fail)) else: servers[server]["channels"].append(channel) buffer = weechat.buffer_search("irc", server) weechat.command(buffer, "/part %s" % channel) weechat.command(buffer, "/nick %s" % servers[server]["nick"])
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 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 xfer_end_do(data, signal, signal_data): wc.infolist_next(signal_data) status = wc.infolist_string(signal_data, 'status_string') filename = wc.infolist_string(signal_data, 'filename') local = wc.infolist_string(signal_data, 'local_filename') if status == "done": os.popen('eiyfs "%s"' % local, 'r', 0) return wc.WEECHAT_RC_OK
def bufsave_cmd(data, buffer, args): ''' Callback for /bufsave command ''' filename_raw = args if not filename_raw: w.command('', '/help %s' % SCRIPT_COMMAND) return w.WEECHAT_RC_OK filename = w.string_eval_path_home(filename_raw, {}, {}, {}) if exists(filename): w.prnt('', 'Error: target file already exists!') return w.WEECHAT_RC_OK try: fp = open(filename, 'w') except: w.prnt('', 'Error writing to target file!') return w.WEECHAT_RC_OK version = w.info_get('version_number', '') or 0 if int(version) >= 0x00030600: # use hdata with WeeChat >= 0.3.6 (direct access to data, very fast) own_lines = w.hdata_pointer(w.hdata_get('buffer'), buffer, 'own_lines') if own_lines: line = w.hdata_pointer(w.hdata_get('lines'), own_lines, 'first_line') hdata_line = w.hdata_get('line') hdata_line_data = w.hdata_get('line_data') while line: data = w.hdata_pointer(hdata_line, line, 'data') if data: date = w.hdata_time(hdata_line_data, data, 'date') # since WeeChat 0.3.9, hdata_time returns long instead of string if not isinstance(date, str): date = time.strftime('%F %T', time.localtime(int(date))) fp.write('%s %s %s\n' %(\ date, cstrip(w.hdata_string(hdata_line_data, data, 'prefix')), cstrip(w.hdata_string(hdata_line_data, data, 'message')), )) line = w.hdata_move(hdata_line, line, 1) else: # use infolist with WeeChat <= 0.3.5 (full duplication of lines, slow and uses memory) infolist = w.infolist_get('buffer_lines', buffer, '') 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) fp.close() return w.WEECHAT_RC_OK
def get_hdata(): """Get list of hdata hooked by plugins in a dict with 3 indexes: plugin, name, xxx.""" hdata = defaultdict(lambda: defaultdict(defaultdict)) infolist = weechat.infolist_get('hook', '', 'hdata') while weechat.infolist_next(infolist): hdata_name = weechat.infolist_string(infolist, 'hdata_name') plugin = weechat.infolist_string(infolist, 'plugin_name') or 'weechat' hdata[plugin][hdata_name]['description'] = weechat.infolist_string(infolist, 'description') variables = '' variables_update = '' lists = '' ptr_hdata = weechat.hdata_get(hdata_name) if ptr_hdata: hdata2 = [] string = weechat.hdata_get_string(ptr_hdata, 'var_keys_values') if string: for item in string.split(','): key = item.split(':')[0] var_offset = weechat.hdata_get_var_offset(ptr_hdata, key) var_array_size = weechat.hdata_get_var_array_size_string(ptr_hdata, '', key) if var_array_size: var_array_size = ', array_size: "%s"' % var_array_size var_hdata = weechat.hdata_get_var_hdata(ptr_hdata, key) if var_hdata: var_hdata = ', hdata: "%s"' % var_hdata type_string = weechat.hdata_get_var_type_string(ptr_hdata, key) hdata2.append({'offset': var_offset, 'text': '\'%s\' (%s)' % (key, type_string), 'textlong': '\'%s\' (%s%s%s)' % (key, type_string, var_array_size, var_hdata), 'update': weechat.hdata_update(ptr_hdata, '', { '__update_allowed': key })}) hdata2 = sorted(hdata2, key=itemgetter('offset')) for item in hdata2: if variables: variables += ' +\n' variables += ' %s' % item['textlong'] if item['update']: if variables_update: variables_update += ' +\n' variables_update += ' %s' % item['text'] if weechat.hdata_update(ptr_hdata, '', { '__delete_allowed' : '' }): if variables_update: variables_update += ' +\n' variables_update += ' \'__delete\'' hdata[plugin][hdata_name]['vars'] = '\n%s' % variables hdata[plugin][hdata_name]['vars_update'] = '\n%s' % variables_update string = weechat.hdata_get_string(ptr_hdata, 'list_keys') if string: for item in sorted(string.split(',')): if lists: lists += ' +\n' lists += ' \'%s\'' % item lists = '\n%s' % lists else: lists = '\n -' hdata[plugin][hdata_name]['lists'] = lists weechat.infolist_free(infolist) return hdata
def bufsave_cmd(data, buffer, args): ''' Callback for /bufsave command ''' filename_raw = args if not filename_raw: w.command('', '/help %s' %SCRIPT_COMMAND) return w.WEECHAT_RC_OK filename = w.string_eval_path_home(filename_raw, {}, {}, {}) if exists(filename): w.prnt('', 'Error: target file already exists!') return w.WEECHAT_RC_OK try: fp = open(filename, 'w') except: w.prnt('', 'Error writing to target file!') return w.WEECHAT_RC_OK version = w.info_get('version_number', '') or 0 if int(version) >= 0x00030600: # use hdata with WeeChat >= 0.3.6 (direct access to data, very fast) own_lines = w.hdata_pointer(w.hdata_get('buffer'), buffer, 'own_lines') if own_lines: line = w.hdata_pointer(w.hdata_get('lines'), own_lines, 'first_line') hdata_line = w.hdata_get('line') hdata_line_data = w.hdata_get('line_data') while line: data = w.hdata_pointer(hdata_line, line, 'data') if data: date = w.hdata_time(hdata_line_data, data, 'date') # since WeeChat 0.3.9, hdata_time returns long instead of string if not isinstance(date, str): date = time.strftime('%F %T', time.localtime(int(date))) fp.write('%s %s %s\n' %(\ date, cstrip(w.hdata_string(hdata_line_data, data, 'prefix')), cstrip(w.hdata_string(hdata_line_data, data, 'message')), )) line = w.hdata_move(hdata_line, line, 1) else: # use infolist with WeeChat <= 0.3.5 (full duplication of lines, slow and uses memory) infolist = w.infolist_get('buffer_lines', buffer, '') 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) fp.close() return w.WEECHAT_RC_OK
def withoutOp(server, channel): L = [] infolist = weechat.infolist_get('irc_nick', '', '%s,%s' %(server, channel)) while weechat.infolist_next(infolist): if not '@' in weechat.infolist_string(infolist, 'prefix'): L.append(weechat.infolist_string(infolist, 'name')) weechat.infolist_free(infolist) return L
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 walk_nicklist(nicklist,word): weechat.infolist_reset_item_cursor(nicklist) ni = weechat.infolist_next(nicklist) while ni : type = weechat.infolist_string(nicklist,'type') if type == 'nick': nick = weechat.infolist_string(nicklist,'name') if nick.lower().startswith(word): return nick ni = weechat.infolist_next(nicklist) return ''
def withoutOp(server, channel): L = [] infolist = weechat.infolist_get('irc_nick', '', '%s,%s' % (server, channel)) while weechat.infolist_next(infolist): if not '@' in weechat.infolist_string(infolist, 'prefix'): L.append(weechat.infolist_string(infolist, 'name')) weechat.infolist_free(infolist) return L
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 get_infolists(): """Get list of infolists hooked by plugins in a dict with 3 indexes: plugin, name, xxx.""" infolists = defaultdict(lambda: defaultdict(defaultdict)) infolist = weechat.infolist_get('hook', '', 'infolist') while weechat.infolist_next(infolist): infolist_name = weechat.infolist_string(infolist, 'infolist_name') plugin = weechat.infolist_string(infolist, 'plugin_name') or 'weechat' for key in ('description', 'pointer_description', 'args_description'): infolists[plugin][infolist_name][key] = weechat.infolist_string(infolist, key) weechat.infolist_free(infolist) return infolists
def get_userhost(server, channel, nick): try: infolist = weechat.infolist_get("irc_nick", "", "%s,%s" % (server, channel)) while weechat.infolist_next(infolist): _nick = weechat.infolist_string(infolist, "name").lower() if _nick == nick: host = weechat.infolist_string(infolist, "host") userhost = "%s!%s" % (nick, host) return userhost.lower() finally: weechat.infolist_free(infolist)
def get_userhost(server, channel, nick): try: infolist = weechat.infolist_get('irc_nick', '', '%s,%s' %(server, channel)) while weechat.infolist_next(infolist): _nick = weechat.infolist_string(infolist, 'name').lower() if _nick == nick: host = weechat.infolist_string(infolist, 'host') userhost = '%s!%s' %(nick, host) return userhost.lower() finally: weechat.infolist_free(infolist)
def get_infolists(): """Get list of infolists hooked by plugins in a dict with 3 indexes: plugin, name, xxx.""" infolists = defaultdict(lambda: defaultdict(defaultdict)) infolist = weechat.infolist_get('hook', '', 'infolist') while weechat.infolist_next(infolist): infolist_name = weechat.infolist_string(infolist, 'infolist_name') plugin = weechat.infolist_string(infolist, 'plugin_name') or 'weechat' for key in ('description', 'pointer_description', 'args_description'): infolists[plugin][infolist_name][key] = weechat.infolist_string( infolist, key) weechat.infolist_free(infolist) return infolists
def get_completions(): """Get list of completions hooked by plugins in a dict with 3 indexes: plugin, item, xxx.""" global ignore_completions_items 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 find_matching_users(server, channel, pattern): # this is for check patterns when they are added infolist = weechat.infolist_get('irc_nick', '', '%s,%s' %(server, channel)) L = [] while weechat.infolist_next(infolist): nick = weechat.infolist_string(infolist, 'name') host = weechat.infolist_string(infolist, 'host') userhost = '%s!%s' %(nick, host) if fnmatch(userhost.lower(), pattern): L.append(nick) weechat.infolist_free(infolist) return L
def find_buffer(): """ Find the buffer when the plugin starts """ infolist = weechat.infolist_get("buffer", "", "") while weechat.infolist_next(infolist): topic = weechat.infolist_string(infolist, "title") if weechat.string_remove_color(topic, "") == TOPIC: name = weechat.infolist_string(infolist, "name") set_options(name) request_completion() break weechat.infolist_free(infolist)
def nicksColorsForBuffer(): ''' Returns a dictionary of nicks to match with their color. Thanks colorize_nicks.py for inspiration! ''' nicks = {} bufferNicks = weechat.infolist_get('nicklist', buff, '') while weechat.infolist_next(bufferNicks): if weechat.infolist_string(bufferNicks, 'type') == 'nick': nick = weechat.infolist_string(bufferNicks, 'name') nicks[nick] = nickColor(nick) weechat.infolist_free(bufferNicks) return nicks