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 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 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 server_nicks(servername): infolist = weechat.infolist_get('irc_server', '', servername) weechat.infolist_next(infolist) nicks = string_eval_expression(weechat.infolist_string( infolist, 'nicks')) # nicks in config weechat.infolist_free(infolist) return nicks.split(',')
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 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 save_query_buffer_to_file(): global query_buffer_list ptr_infolist_buffer = weechat.infolist_get("buffer", "", "") while weechat.infolist_next(ptr_infolist_buffer): ptr_buffer = weechat.infolist_pointer(ptr_infolist_buffer, "pointer") type = weechat.buffer_get_string(ptr_buffer, "localvar_type") if type == "private": server = weechat.buffer_get_string(ptr_buffer, "localvar_server") channel = weechat.buffer_get_string(ptr_buffer, "localvar_channel") query_buffer_list.insert(0, "%s %s" % (server, channel)) weechat.infolist_free(ptr_infolist_buffer) filename = get_filename_with_path() if len(query_buffer_list): try: f = open(filename, "w") i = 0 while i < len(query_buffer_list): f.write("%s\n" % query_buffer_list[i]) i = i + 1 f.close() except: weechat.prnt( "", '%s%s: Error writing query buffer to "%s"' % (weechat.prefix("error"), SCRIPT_NAME, filename) ) raise else: # no query buffer(s). remove file if os.path.isfile(filename): os.remove(filename) return
def ns_ban_cb(data, buffer, args): args = args.split() oper = args[0] nick = args[1] channel = weechat.buffer_get_string(buffer, 'localvar_channel') server = weechat.buffer_get_string(buffer, 'localvar_server') if oper == 'ban': found = False infolist = weechat.infolist_get("irc_nick", "", "{},{},{}".format(server, channel, nick)) while weechat.infolist_next(infolist): found = True account = weechat.infolist_string(infolist, "account") if account: weechat.command("", '/mode +b $a:{}'.format(account)) weechat.infolist_free(infolist) if not found: # TODO: Handle numeric 315 too, ‘End of /WHO list’ hooks['who'] = weechat.hook_modifier("irc_in_354", "who_mod_cb", "") weechat.command("", "/who %s n%%an" % args[1]) operation[nick] = oper else: # TODO: Handle numeric 315 too, ‘End of /WHO list’ hooks['who'] = weechat.hook_modifier("irc_in_354", "who_mod_cb", "") weechat.command("", "/who %s n%%an" % args[1]) operation[nick] = oper return weechat.WEECHAT_RC_OK
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 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 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 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 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_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 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 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 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 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 disable_logging(self): """Return the previous logger level and set the buffer logger level to 0. If it was already 0, return None.""" # If previous_log_level has not been previously set, return the level # we detect now. if not hasattr(self, 'previous_log_level'): infolist = weechat.infolist_get('logger_buffer', '', '') buf = self.buffer() previous_log_level = 0 while weechat.infolist_next(infolist): if weechat.infolist_pointer(infolist, 'buffer') == buf: previous_log_level = weechat.infolist_integer( infolist, 'log_level') if self.is_logged(): weechat.command(buf, '/mute logger disable') self.print_buffer( 'Logs have been temporarily disabled for the session. They will be restored upon finishing the OTR session.') break weechat.infolist_free(infolist) return previous_log_level # If previous_log_level was already set, it means we already altered it # and that we just detected an already modified logging level. # Return the pre-existing value so it doesn't get lost, and we can # restore it later. else: return self.previous_log_level
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 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 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 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 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 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 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 cb_command_bufferlist(data, buf, list_args): """Print a list of all buffer names.""" infolist = weechat.infolist_get("buffer", "", "") weechat.prnt('', "[" + NAME + "] buffer list") while weechat.infolist_next(infolist): weechat.prnt('', ' ' + weechat.infolist_string(infolist, "full_name")) weechat.infolist_free(infolist) return weechat.WEECHAT_RC_OK
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')) weechat.infolist_free(serverptrlist) return serverlist
def get_all_buffers(): """Returns list with pointers of all open buffers.""" buffers = [] infolist = weechat.infolist_get('buffer', '', '') while weechat.infolist_next(infolist): buffers.append(weechat.infolist_pointer(infolist, 'pointer')) weechat.infolist_free(infolist) return buffers
def cb_command_apply(data, buf, list_args): """Apply the rules the all existing buffers; useful when testing a new rule.""" infolist = weechat.infolist_get("buffer", "", "") while weechat.infolist_next(infolist): buf2 = weechat.infolist_pointer(infolist, "pointer") cb_signal_apply_rules(data, None, buf2) weechat.infolist_free(infolist) return weechat.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 should_send(buffer, tags, nick, highlighted): if not nick: # a nick is required to form a correct message, bail return False if highlighted: if weechat.config_get_plugin('notify_on_highlight') != 'on': # notifying on highlights is disabled, bail return False elif weechat.buffer_get_string(buffer, 'localvar_type') == 'private': if weechat.config_get_plugin('notify_on_privmsg') != 'on': # notifying on private messages is disabled, bail return False else: # not a highlight or private message, bail return False notify_when = weechat.config_get_plugin('notify_when') if notify_when == 'never': # user has opted to not be notified, bail return False elif notify_when == 'away': # user has opted to only be notified when away infolist_args = ( weechat.buffer_get_string(buffer, 'localvar_channel'), weechat.buffer_get_string(buffer, 'localvar_server'), weechat.buffer_get_string(buffer, 'localvar_nick') ) if not None in infolist_args: infolist = weechat.infolist_get('irc_nick', '', ','.join(infolist_args)) if infolist: away_status = weechat.infolist_integer(infolist, 'away') weechat.infolist_free(infolist) if not away_status: # user is not away, bail return False elif notify_when == 'detached': # user has opted to only be notified when detached (relays) num_relays = weechat.info_get('relay_client_count', 'connected') if num_relays == 0: # no relays connected, bail return False if nick == weechat.buffer_get_string(buffer, 'localvar_nick'): # the sender was the current user, bail return False if nick in weechat.config_get_plugin('ignore_nicks').split(','): # the sender was on the ignore list, bail return False for buffer_name in get_buffer_names(buffer): if buffer_name in weechat.config_get_plugin('ignore_buffers').split(','): # the buffer was on the ignore list, bail return False return True
def infolist_get_first_entry_from_hotlist(): infolist = weechat.infolist_get('hotlist', '', '') if infolist: weechat.infolist_next(infolist) # go to first entry in hotlist buffer_name = weechat.infolist_string(infolist, 'buffer_name') buffer_number = weechat.infolist_integer(infolist, 'buffer_number') ptr_buffer = weechat.infolist_pointer(infolist, 'buffer_pointer') weechat.infolist_free(infolist) return buffer_name, ptr_buffer, buffer_number
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") if weechat.config_get_plugin( "use_core_instead_weechat") == "on" and name == "weechat": name = "core" number = weechat.infolist_integer(infolist, "number") full_name = weechat.infolist_string(infolist, "full_name") if not full_name: full_name = "%s.%s" % (weechat.infolist_string( infolist, "plugin_name"), weechat.infolist_string(infolist, "name")) pointer = weechat.infolist_pointer(infolist, "pointer") 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, "full_name": full_name, "pointer": pointer }) if len(input) == 0 and pointer == weechat.current_buffer(): buffers_pos = len(list) - 1 weechat.infolist_free(infolist) if not weechat.config_string_to_boolean( weechat.config_get_plugin('sort_by_activity')): return list # sort buffers like in hotlist. hotlist = [] infolist = weechat.infolist_get("hotlist", "", "") while weechat.infolist_next(infolist): hotlist.append(weechat.infolist_pointer(infolist, "buffer_pointer")) weechat.infolist_free(infolist) last_index = len(hotlist) def priority(b): try: return hotlist.index(b["pointer"]) except ValueError: # not in hotlist, always last. return last_index return sorted(list, key=priority)
def chanact_cb(*args): ''' Callback ran on hotlist changes ''' global keydict hotlist = w.infolist_get('hotlist', '', '') activity = [] while w.infolist_next(hotlist): priority = w.infolist_integer(hotlist, 'priority') if priority < int(w.config_get_plugin('lowest_priority')): continue int_number = w.infolist_integer(hotlist, 'buffer_number') number = str(int_number) thebuffer = w.infolist_pointer(hotlist, 'buffer_pointer') name = w.buffer_get_string(thebuffer, 'short_name') color = w.config_get_plugin('color_default') if priority > 0: color = w.config_get_plugin('color_%s' %priority) if number in keydict: number = keydict[number] entry = '%s%s%s' % (w.color(color), number, w.color('reset')) elif name in keydict: name = keydict[name] entry = '%s%s%s' % (w.color(color), name, w.color('reset')) elif name: entry = '%s%s%s:%s%s%s' % ( w.color('default'), number, w.color('reset'), w.color(color), name[:int(w.config_get_plugin('item_length'))], w.color('reset')) else: entry = '%s%s%s' % ( w.color(color), number, w.color('reset')) activity.append((entry, thebuffer, sort_rank(thebuffer, priority), int_number)) if w.config_get_plugin('sort_by_number') == "on": activity.sort(key=lambda t: int(t[3])) else: activity.sort(key=lambda t: int(t[2]), reverse=True) w.infolist_free(hotlist) if activity: message = w.config_get_plugin('message') delim = w.config_get_plugin('delimiter') return message + delim.join(a[0] for a in activity) else: return ''
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)
def term_window_resize_cb(data, signal, signal_data): infolist = weechat.infolist_get("window", signal_data, "") if weechat.infolist_next(infolist): buffer = weechat.infolist_pointer(infolist, "buffer") if buffer in terms: term = terms[buffer] term.resized() weechat.infolist_free(infolist) return weechat.WEECHAT_RC_OK
def bas_completion_options_cb(data, completion_item, buffer, completion): """ Complete with config options, for command '/autosetbuffer'. """ options = weechat.infolist_get("option", "", "%s.buffer.*" % CONFIG_FILE_NAME) if options: while weechat.infolist_next(options): weechat.hook_completion_list_add(completion, weechat.infolist_string(options, "option_name"), 0, weechat.WEECHAT_LIST_POS_SORT) weechat.infolist_free(options) return weechat.WEECHAT_RC_OK
def get_all_buffers(): '''Returns list with pointers of all open buffers.''' buffers = [] infolist = w.infolist_get('buffer', '', '') while w.infolist_next(infolist): buffer_type = w.buffer_get_string(w.infolist_pointer(infolist, 'pointer'), 'localvar_type') if buffer_type == 'private': # we only close private message buffers for now buffers.append(w.infolist_pointer(infolist, 'pointer')) w.infolist_free(infolist) return buffers
def check_hotlist(check_pointer): ptr_buffer = 0 infolist = weechat.infolist_get('hotlist', '', '') while weechat.infolist_next(infolist): pointer = weechat.infolist_pointer(infolist, 'buffer_pointer') if pointer == check_pointer: ptr_buffer = pointer break weechat.infolist_free(infolist) return ptr_buffer
def get_connected_servers() -> Generator[str, None, None]: """ return a list of connected servers that are not AWAY """ infolist = w.infolist_get("irc_server", "", "") if infolist: while w.infolist_next(infolist): if (w.infolist_integer(infolist, "is_connected") and not w.infolist_integer(infolist, "is_away")): yield w.infolist_string(infolist, "name") w.infolist_free(infolist)
def hotlist(): hotlist_items = [] hotlist = weechat.infolist_get("hotlist", "", "") while weechat.infolist_next(hotlist): buffer_number = weechat.infolist_integer(hotlist, "buffer_number") buffer = weechat.infolist_pointer(hotlist, "buffer_pointer") short_name = weechat.buffer_get_string(buffer, "short_name") hotlist_items.append("%s:%s" % (buffer_number, short_name)) weechat.infolist_free(hotlist) return ",".join(hotlist_items)
def infolist_get_buffer_name_and_ptr_by_name(str_buffer_name): infolist = weechat.infolist_get('buffer', '', '*%s*' % str_buffer_name) full_name = '' ptr_buffer = '' if infolist: while weechat.infolist_next(infolist): full_name = weechat.infolist_string(infolist, 'full_name') ptr_buffer = weechat.infolist_pointer(infolist, 'pointer') break weechat.infolist_free(infolist) return full_name, ptr_buffer
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_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 layout_exist(layout): infolist = weechat.infolist_get("layout", "", "") found = False while weechat.infolist_next(infolist): if layout == weechat.infolist_string(infolist, "name"): found = True break weechat.infolist_free(infolist) return found
def get_open_buffers(): buffers = [] infolist = w.infolist_get("buffer", "", "") if infolist: while w.infolist_next(infolist): name = w.infolist_string(infolist, "name") number = w.infolist_integer(infolist, "number") _ = "{0}:{1}".format(number, name) buffers.append(_) w.infolist_free(infolist) return buffers
def relay_get_server(relay_ptr: str) -> Dict: """ get the server name for a relay client pointer """ infolist = w.infolist_get("relay", relay_ptr, "") server = {} # type: Dict[str, str] if infolist and w.infolist_next(infolist): match = RELAY_REGEX.search(w.infolist_string(infolist, "desc")) if match: server = match.groupdict() w.infolist_free(infolist) return server
def irc_servers(): ''' Disconnected IRC servers, workaround for /away -all bug in v. < 0.3.2 ''' serverlist = w.infolist_get('irc_server', '', '') buffers = [] if serverlist: while w.infolist_next(serverlist): if w.infolist_integer(serverlist, 'is_connected') == 0: buffers.append((w.infolist_pointer(serverlist, 'buffer'))) w.infolist_free(serverlist) return buffers
def get_last_line_date(buffer): date = '1970-01-01 01:00:00' infolist = w.infolist_get('buffer_lines', buffer, '') while w.infolist_prev(infolist): date = w.infolist_time(infolist, 'date') if date != '1970-01-01 01:00:00': # Some lines like "Day changed to" message doesn't have date # set so loop until we find a message that does break w.infolist_free(infolist) return date
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 current_server = None if '-current' in argv: current_server = weechat.buffer_get_string(weechat.current_buffer(), "localvar_server") # remove "-current" + whitespace from argumentlist args = args.replace("-current", "") args = args.lstrip() argv.remove("-current") # search for "-exclude" in arguments i = 0 for entry in argv[0:]: if entry.startswith("-exclude="): exclude_nick = make_list(argv[i]) command = " ".join(argv[i + 1::]) break i += 1 else: command = args # no command found. if not command: return weechat.WEECHAT_RC_OK if not command.startswith("/"): command = "/%s" % command 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'\$nick', query, command) if weechat.buffer_get_string(ptr, "localvar_type") == "private": if current_server is not None: if server == current_server: exclude_nick_and_server(ptr, query, server, exclude_nick, execute_command) else: exclude_nick_and_server(ptr, query, server, exclude_nick, execute_command) weechat.infolist_free(infolist) return weechat.WEECHAT_RC_OK
def infolist_buffer_set_title(buffer): # get list of infolists available list = "" infolist = weechat.infolist_get("hook", "", "infolist") while weechat.infolist_next(infolist): list += " %s" % weechat.infolist_string(infolist, "infolist_name") weechat.infolist_free(infolist) # set buffer title weechat.buffer_set(buffer, "title", "%s %s | Infolists:%s" % (SCRIPT_NAME, SCRIPT_VERSION, list))
def exclude_hotlist(): if OPTIONS['hotlist'] == '0' or OPTIONS['hotlist'] == '': return weechat.WEECHAT_RC_OK infolist = weechat.infolist_get('hotlist', '', '') while weechat.infolist_next(infolist): buffer_number = weechat.infolist_integer(infolist, 'buffer_number') priority = weechat.infolist_integer(infolist, 'priority') if int(OPTIONS['hotlist']) == priority or OPTIONS['hotlist'] == '4': weechat.command('', '/buffer unhide %s' % buffer_number) weechat.infolist_free(infolist) return weechat.WEECHAT_RC_OK
def ugCheckLineOutsideWindow(): global urlGrab , urlGrabSettings, urlgrab_buffer, current_line, max_buffer_length if (urlgrab_buffer): infolist = weechat.infolist_get("window", "", "current") if (weechat.infolist_next(infolist)): start_line_y = weechat.infolist_integer(infolist, "start_line_y") chat_height = weechat.infolist_integer(infolist, "chat_height") if(start_line_y > current_line): weechat.command(urlgrab_buffer, "/window scroll -%i" %(start_line_y - current_line)) elif(start_line_y <= current_line - chat_height): weechat.command(urlgrab_buffer, "/window scroll +%i"%(current_line - start_line_y - chat_height + 1)) weechat.infolist_free(infolist)