postpone: add match_prefix setting
[shutils.git] / weechat / postpone.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2010 by Alexander Schremmer <alex@alexanderweb.de>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 #
20 # (this script requires WeeChat 0.3.0 or newer)
21 #
22 # History:
23 # 2013-11-08, Stefan Huber <shuber@sthu.org>
24 # version 0.2.2: add match_prefix setting
25 # 2012-12-29, Stefan Huber <shuber@sthu.org>
26 # version 0.2.1: fix channel determination in join_cb
27 # 2010-05-20, Alexander Schremmer <alex@alexanderweb.de>
28 # version 0.2: removed InfoList code
29 # 2010-05-15, Alexander Schremmer <alex@alexanderweb.de>
30 # version 0.1: initial release
31
32 import weechat as w
33 import re
34
35 SCRIPT_NAME = "postpone"
36 SCRIPT_AUTHOR = "Alexander Schremmer <alex@alexanderweb.de>"
37 SCRIPT_VERSION = "0.2.2"
38 SCRIPT_LICENSE = "GPL3"
39 SCRIPT_DESC = "Postpones written messages for later dispatching if target nick is not on channel"
40
41 postpone_data = {}
42
43 settings = {
44 'match_prefix': ('', 'Postpone message if prefix before "nick:" is matched.')
45 }
46
47
48 def join_cb(data, signal, signal_data):
49 server = signal.split(',')[0] # EFNet,irc_in_JOIN
50 channel = re.match('.* JOIN :?(?P<channel>.+)$', signal_data).groups()[0]
51 nick = re.match(':(?P<nick>.+)!', signal_data).groups()[0].lower()
52 buffer = w.buffer_search("", "%s.%s" % (server, channel))
53 if server in postpone_data and channel in postpone_data[server] and\
54 nick in postpone_data[server][channel]:
55 messages = postpone_data[server][channel][nick]
56 for msg in messages:
57 w.command(buffer, msg + " (This message has been postponed.)")
58 messages[:] = []
59 return w.WEECHAT_RC_OK
60
61 def channel_has_nick(server, channel, nick):
62 buffer = w.buffer_search("", "%s.%s" % (server, channel))
63 return bool(w.nicklist_search_nick(buffer, "", nick))
64
65 def command_run_input(data, buffer, command):
66 """ Function called when a command "/input xxxx" is run """
67 if command == "/input return": # As in enter was pressed.
68 input_s = w.buffer_get_string(buffer, 'input')
69 server = w.buffer_get_string(buffer, 'localvar_server')
70 channel = w.buffer_get_string(buffer, 'localvar_channel')
71 match_prefix = w.config_get_plugin('match_prefix')
72
73 match = re.match(match_prefix + r'([\w-]+?): (.*)$', input_s)
74 if match:
75 nick, message = match.groups()
76 if not channel_has_nick(server, channel, nick):
77 w.prnt(buffer, "| Enqueued message for %s: %s" % (nick, message))
78 save = nick + ": " + message
79 postpone_data.setdefault(server, {}).setdefault(channel,
80 {}).setdefault(nick.lower(), []).append(save)
81 w.buffer_set(buffer, 'input', "")
82 # XXX why doesn't this work? i want to have the typed text
83 # in the history
84 #history_list = w.infolist_get("history", buffer, "")
85 #history_item = w.infolist_new_item(history_list)
86 #w.infolist_new_var_string(history_item, "text", input_s)
87 return w.WEECHAT_RC_OK
88
89
90 if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
91 SCRIPT_DESC, "", ""):
92
93 version = w.info_get('version_number', '') or 0
94 for option, default_desc in settings.iteritems():
95 if not w.config_is_set_plugin(option):
96 w.config_set_plugin(option, default_desc[0])
97 if int(version) >= 0x00030500:
98 w.config_set_desc_plugin(option, default_desc[1])
99
100 w.hook_command_run("/input return", "command_run_input", "")
101 w.hook_signal('*,irc_in2_join', 'join_cb', '')
102