weechat: add postpone 2013-01-03
[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 # 2012-12-29, Stefan Huber <shuber@sthu.org>
24 # version 0.2.1: fix channel determination in join_cb
25 # 2010-05-20, Alexander Schremmer <alex@alexanderweb.de>
26 # version 0.2: removed InfoList code
27 # 2010-05-15, Alexander Schremmer <alex@alexanderweb.de>
28 # version 0.1: initial release
29
30 import weechat as w
31 import re
32
33 SCRIPT_NAME = "postpone"
34 SCRIPT_AUTHOR = "Alexander Schremmer <alex@alexanderweb.de>"
35 SCRIPT_VERSION = "0.2.1"
36 SCRIPT_LICENSE = "GPL3"
37 SCRIPT_DESC = "Postpones written messages for later dispatching if target nick is not on channel"
38
39
40 postpone_data = {}
41
42
43 def join_cb(data, signal, signal_data):
44 server = signal.split(',')[0] # EFNet,irc_in_JOIN
45 channel = re.match('.* JOIN :?(?P<channel>.+)$', signal_data).groups()[0]
46 nick = re.match(':(?P<nick>.+)!', signal_data).groups()[0].lower()
47 buffer = w.buffer_search("", "%s.%s" % (server, channel))
48 if server in postpone_data and channel in postpone_data[server] and\
49 nick in postpone_data[server][channel]:
50 messages = postpone_data[server][channel][nick]
51 for msg in messages:
52 w.command(buffer, msg + " (This message has been postponed.)")
53 messages[:] = []
54 return w.WEECHAT_RC_OK
55
56 def channel_has_nick(server, channel, nick):
57 buffer = w.buffer_search("", "%s.%s" % (server, channel))
58 return bool(w.nicklist_search_nick(buffer, "", nick))
59
60 def command_run_input(data, buffer, command):
61 """ Function called when a command "/input xxxx" is run """
62 if command == "/input return": # As in enter was pressed.
63 input_s = w.buffer_get_string(buffer, 'input')
64 server = w.buffer_get_string(buffer, 'localvar_server')
65 channel = w.buffer_get_string(buffer, 'localvar_channel')
66
67 match = re.match(r'([\w-]+?): (.*)$', input_s)
68 if match:
69 nick, message = match.groups()
70 if not channel_has_nick(server, channel, nick):
71 w.prnt(buffer, "| Enqueued message for %s: %s" % (nick, message))
72 postpone_data.setdefault(server, {}).setdefault(channel,
73 {}).setdefault(nick.lower(), []).append(input_s)
74 w.buffer_set(buffer, 'input', "")
75 # XXX why doesn't this work? i want to have the typed text
76 # in the history
77 #history_list = w.infolist_get("history", buffer, "")
78 #history_item = w.infolist_new_item(history_list)
79 #w.infolist_new_var_string(history_item, "text", input_s)
80 return w.WEECHAT_RC_OK
81
82
83 if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE,
84 SCRIPT_DESC, "", ""):
85 w.hook_command_run("/input return", "command_run_input", "")
86 w.hook_signal('*,irc_in2_join', 'join_cb', '')
87