Home

Script: bitlbee_completion.py

Add tab completion to bitlbee commands. (for WeeChat ≥ 0.4.3)
Author: Roger Duran, version 0.2, GPL3 — added: 2015-03-22, updated: 2016-01-13
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
# Add tab completion to bitlbee commands
# based on http://scripts.irssi.org/scripts/bitlbee_tab_completion.pl
#
# History:
#
# 2015-11-02, Mickaël Thomas <mickael9@gmail.com>:
#     version 0.2: strip color attributes for topic detection
# 2015-03-22, Roger Duran <rogerduran@gmail.com>:
#     version 0.1: initial version

import weechat

SCRIPT_NAME = "bitlbee_completion"
SCRIPT_AUTHOR = "Roger Duran <rogerduran@gmail.com>"
SCRIPT_VERSION = "0.2"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "Add tab completion to bitlbee commands"

OPTS = {
    "server": None,
    "channel": None
    }

TOPIC = "Welcome to the control channel. "\
    "Type help for help information."

commands = []


def request_completion():
    """
    Request the completion to the bitlbee server and wait for response
    """
    server = OPTS["server"]
    weechat.command(server, "/quote -server %s COMPLETIONS" % server)


def modifier_cb(data, modifier, modifier_data, string):
    """
    When the server returns the completion, update the commands list
    """

    if ":COMPLETIONS" not in string:
        return string
    command = string.split(":COMPLETIONS ")[1]
    if command not in ("OK", "END"):
        commands.append(command)
    return ""


def bitlbee_completion(data, completion_item, buffer, completion):
    """
    Complete bitlbee commands only in the bitlbee buffer
    """

    server = OPTS["server"]
    channel = OPTS["channel"]
    if not server or not channel:
        return weechat.WEECHAT_RC_OK

    buff_name = weechat.buffer_get_string(buffer, "name")
    if buff_name == "%s.%s" % (server, channel):
        for command in commands:
            weechat.hook_completion_list_add(completion, command, 0,
                                             weechat.WEECHAT_LIST_POS_SORT)
    return weechat.WEECHAT_RC_OK


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 set_options(name):
    server, channel = name.split(".")
    OPTS["server"] = server
    OPTS["channel"] = channel


def print_332(data, buffer, time, tags, displayed, highlight, prefix, message):
    """
    Find the buffer when a new one is open
    """
    if weechat.string_remove_color(message, "") == TOPIC:
        name = weechat.buffer_get_string(buffer, "name")
        set_options(name)
        request_completion()
    return weechat.WEECHAT_RC_OK


def main():
    weechat.hook_modifier("irc_in_notice", "modifier_cb", "")
    weechat.hook_completion("bitlbee", "bitlbee completion",
                            "bitlbee_completion", "")

    weechat.hook_print('', 'irc_332', '', 1, 'print_332', '')
    weechat.hook_print('', 'irc_topic', '', 1, 'print_332', '')
    find_buffer()

if __name__ == "__main__":
    if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
                        SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
        main()