aboutsummaryrefslogtreecommitdiff
path: root/weechat/python/queryman.py
blob: c979f93fa4d3c5985eaaeb31463cf54fd1ac6b42 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013-2016 by nils_2 <weechatter@arcor.de>
#
# save and restore query buffers after /quit
#
# 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/>.
#
# idea by lasers@freenode.#weechat
#
# 2016-02-27: nils_2, (freenode.#weechat)
#       0.3 : make script consistent with "buffer_switch_autojoin" option (idea haasn)
#
# 2013-11-07: nils_2, (freenode.#weechat)
#       0.2 : fix file not found error (reported by calcifea)
#           : make script compatible with Python 3.x
#
# 2013-07-26: nils_2, (freenode.#weechat)
#       0.1 : initial release
#
# script will create a config file (~./weechat/queryman.txt)
# format: "servername nickname" (without "")
#
# Development is currently hosted at
# https://github.com/weechatter/weechat-scripts

try:
    import weechat,re,os

except Exception:
    print("This script must be run under WeeChat.")
    print("Get WeeChat now at: http://www.weechat.org/")
    quit()

SCRIPT_NAME     = 'queryman'
SCRIPT_AUTHOR   = 'nils_2 <weechatter@arcor.de>'
SCRIPT_VERSION  = '0.3'
SCRIPT_LICENSE  = 'GPL'
SCRIPT_DESC     = 'save and restore query buffers after /quit'

query_buffer_list = []
queryman_filename = 'queryman.txt'

# ================================[ callback ]===============================
def quit_signal_cb(data, signal, signal_data):
    save_query_buffer_to_file()
    return weechat.WEECHAT_RC_OK

# signal_data contains servername
def irc_server_connected_signal_cb(data, signal, signal_data):
    load_query_buffer_irc_server_opened(signal_data)
    return weechat.WEECHAT_RC_OK

# ================================[ file ]===============================
def get_filename_with_path():
    global queryman_filename
    path = weechat.info_get("weechat_dir", "")
    return os.path.join(path,queryman_filename)

def load_query_buffer_irc_server_opened(server_connected):
    global query_buffer_list

    filename = get_filename_with_path()

    if os.path.isfile(filename):
        f = open(filename, 'rb')
        for line in f:
            servername,nick = line.split(' ')
            if servername == server_connected:
                noswitch = ""
                switch_autojoin = weechat.config_get("irc.look.buffer_switch_autojoin")
                if not weechat.config_boolean(switch_autojoin):
                    noswitch = "-noswitch"
                weechat.command('','/query %s -server %s %s' % ( noswitch, servername, nick ))
        f.close()
    else:
        weechat.prnt('','%s%s: Error loading query buffer from "%s"' % (weechat.prefix('error'), SCRIPT_NAME, filename))

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

# ================================[ main ]===============================
if __name__ == '__main__':
    if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, '', ''):
        version = weechat.info_get('version_number', '') or 0
        if int(version) >= 0x00030700:
            weechat.hook_signal('quit', 'quit_signal_cb', '')
            weechat.hook_signal('irc_server_connected', 'irc_server_connected_signal_cb', '')