File: autojoin.py

package info (click to toggle)
weechat-scripts 20100422-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,184 kB
  • ctags: 1,634
  • sloc: python: 8,545; perl: 8,472; ruby: 147; tcl: 137; sh: 8; makefile: 2
file content (115 lines) | stat: -rw-r--r-- 3,921 bytes parent folder | download
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
115
# -*- coding: utf-8 -*-
#
# Copyright (c) 2009 by xt <xt@bash.no>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# (this script requires WeeChat 0.3.0 or newer)
#
# History:
# 2009-06-18, xt <xt@bash.no>
#     version 0.1: initial release
#
# 2009-06-18, LBo <leon@tim-online.nl>
#     version 0.2: added autosaving of join channels
#     /set plugins.var.python.autojoin.autosave 'on'
#
# @TODO: find_channels() also returns channels which are already /part'ed but
#        are still in a buffer
# @TODO: plugin responds to all part messages, not only from self

import weechat as w

SCRIPT_NAME    = "autojoin"
SCRIPT_AUTHOR  = "xt <xt@bash.no>"
SCRIPT_VERSION = "0.2"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC    = "Configure autojoin for all servers according to currently joined channels"
SCRIPT_COMMAND = "autojoin"

# script options
settings = {
    "autosave": "off",
}

if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
    # Init everything
    for option, default_value in settings.items():
        if w.config_get_plugin(option) == "":
            w.config_set_plugin(option, default_value)
    
    w.hook_command(SCRIPT_COMMAND,
                   SCRIPT_DESC,
                   "[--run]",
                   "   --run: actually run the commands instead of displaying\n",
                   "--run",
                   "autojoin_cb",
                   "")
    
    w.hook_signal('*,irc_in2_join', 'autosave_autojoin_channels', '')
    w.hook_signal('*,irc_in2_part', 'autosave_autojoin_channels', '')
    w.hook_signal('*,irc_in2_quit', 'autosave_autojoin_channels', '')
    w.hook_signal('quit',           'autosave_autojoin_channels', '')

def autosave_autojoin_channels(data, buffer, args):
    ''' Autojoin current channels '''
    if w.config_get_plugin("autosave") != "on":
        return w.WEECHAT_RC_OK
    items = find_channels()
    
    # print/execute commands
    for server, channels in items.iteritems():
        channels = channels.rstrip(',')
        command = "/set irc.server.%s.autojoin '%s'" % (server, channels)
        w.command('', command)
    
    return w.WEECHAT_RC_OK

def autojoin_cb(data, buffer, args):
    """Old behaviour: doesn't save empty channel list"""
    items = find_channels()
    
    # print/execute commands
    for server, channels in items.iteritems():
        channels = channels.rstrip(',')
        if not channels: # empty channel list
            continue
        command = '/set irc.server.%s.autojoin %s' % (server, channels)
        if args == '--run':
            w.command('', command)
        else:
            w.prnt('', command)
    
    return w.WEECHAT_RC_OK

def find_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():
        infolist = w.infolist_get('irc_channel', '',  server)
        while w.infolist_next(infolist):
            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