]>
git.sthu.org Git - shutils.git/blob - weechat/postpone.py
1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2010 by Alexander Schremmer <alex@alexanderweb.de>
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.
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.
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/>.
20 # (this script requires WeeChat 0.3.0 or newer)
23 # 2013-11-08, Stefan Huber <shuber@sthu.org>
24 # version 0.2.2: add match_prefix setting, recall timestamp of message
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
34 from datetime
import datetime
35 from time
import strftime
37 SCRIPT_NAME
= "postpone"
38 SCRIPT_AUTHOR
= "Alexander Schremmer <alex@alexanderweb.de>"
39 SCRIPT_VERSION
= "0.2.2"
40 SCRIPT_LICENSE
= "GPL3"
41 SCRIPT_DESC
= "Postpones written messages for later dispatching if target nick is not on channel"
46 'match_prefix': ('', 'Postpone message if prefix before "nick:" is matched.')
50 def join_cb(data
, signal
, signal_data
):
51 server
= signal
.split(',')[0] # EFNet,irc_in_JOIN
52 channel
= re
.match('.* JOIN :?(?P<channel>.+)$', signal_data
).groups()[0]
53 nick
= re
.match(':(?P<nick>.+)!', signal_data
).groups()[0].lower()
54 buffer = w
.buffer_search("", "%s.%s" % (server
, channel
))
55 if server
in postpone_data
and channel
in postpone_data
[server
] and\
56 nick
in postpone_data
[server
][channel
]:
57 messages
= postpone_data
[server
][channel
][nick
]
58 for time
, msg
in messages
:
59 tstr
= strftime("%Y-%m-%d %H:%M:%S", time
.timetuple())
60 w
.command(buffer, msg
+ " (This message has been postponed on " + tstr
+ ".)")
62 return w
.WEECHAT_RC_OK
64 def channel_has_nick(server
, channel
, nick
):
65 buffer = w
.buffer_search("", "%s.%s" % (server
, channel
))
66 return bool(w
.nicklist_search_nick(buffer, "", nick
))
68 def command_run_input(data
, buffer, command
):
69 """ Function called when a command "/input xxxx" is run """
70 if command
== "/input return": # As in enter was pressed.
71 input_s
= w
.buffer_get_string(buffer, 'input')
72 server
= w
.buffer_get_string(buffer, 'localvar_server')
73 channel
= w
.buffer_get_string(buffer, 'localvar_channel')
74 match_prefix
= w
.config_get_plugin('match_prefix')
76 match
= re
.match(match_prefix
+ r
'([\w-]+?): (.*)$', input_s
)
78 nick
, message
= match
.groups()
79 if not channel_has_nick(server
, channel
, nick
):
80 w
.prnt(buffer, "| Enqueued message for %s: %s" % (nick
, message
))
81 save
= datetime
.now(), nick
+ ": " + message
82 postpone_data
.setdefault(server
, {}).setdefault(channel
,
83 {}).setdefault(nick
.lower(), []).append(save
)
84 w
.buffer_set(buffer, 'input', "")
85 # XXX why doesn't this work? i want to have the typed text
87 #history_list = w.infolist_get("history", buffer, "")
88 #history_item = w.infolist_new_item(history_list)
89 #w.infolist_new_var_string(history_item, "text", input_s)
90 return w
.WEECHAT_RC_OK
93 if w
.register(SCRIPT_NAME
, SCRIPT_AUTHOR
, SCRIPT_VERSION
, SCRIPT_LICENSE
,
96 version
= w
.info_get('version_number', '') or 0
97 for option
, default_desc
in settings
.iteritems():
98 if not w
.config_is_set_plugin(option
):
99 w
.config_set_plugin(option
, default_desc
[0])
100 if int(version
) >= 0x00030500:
101 w
.config_set_desc_plugin(option
, default_desc
[1])
103 w
.hook_command_run("/input return", "command_run_input", "")
104 w
.hook_signal('*,irc_in2_join', 'join_cb', '')