usr bin python encoding utf-8 The script is in the public domain Leoni

 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
#!/usr/bin/python
# -*- encoding: utf-8 -*-
"""
The script is in the public domain.
Leonid Evdokimov (weechat at darkk dot net dot ru)
http://darkk.net.ru/weechat/gentoo-ru.py
0.01
"""
import weechat
import re
import locale
from time import time as NOW
def parse_privmsg(server, command):
# :nick!ident@host PRIVMSG dest :foobarbaz
l = command.split(' ', 3)
mask = l[0][1:]
nick = mask.split("!")[0]
dest = l[2]
message = l[3][1:]
###########################################
#nothing, info, message = command.split(":", 2)
#info = info.split(' ')
if dest == weechat.get_info('nick', server):
buffer = nick
else:
buffer = dest
return (nick, buffer, message)
def strip_irc_colors(message):
# look at src/plugins/irc/irc-color.c to get proper color parser
# modifiers = ( # one-byte modifiers
# ur'\x02', # IRC_COLOR_BOLD_CHAR
# ur'\x03', # IRC_COLOR_COLOR_CHAR, color defenition follows
# ur'\x0F', # IRC_COLOR_RESET_CHAR
# ur'\x11', # IRC_COLOR_FIXED_CHAR
# ur'\x12', # IRC_COLOR_REVERSE_CHAR
# ur'\x16', # IRC_COLOR_REVERSE2_CHAR
# ur'\x1d', # IRC_COLOR_ITALIC_CHAR
# ur'\x1f') # IRC_COLOR_UNDERLINE_CHAR
# hope, python regexps are character-aware, not byte-aware
return re.sub(ur'(?:\x02|\x03(?:\d{1,2})?(?:,\d{1,2})?|\x0F|\x11|\x12|\x16|\x1d|\x1f)', '', message)
channel_name = '#gentoo-ru'
server_name = 'freenode'
send_queue = []
badnicks = dict()
def flush_queue():
global send_queue, badnicks
local_queue, send_queue = send_queue, []
for msg, buffer, server in local_queue:
weechat.command(msg.encode(local_charset), channel_name, server_name)
hour_ago = NOW() - 60*60
badnicks = dict(filter(lambda pair: hour_ago <= pair[1], badnicks.iteritems()))
return weechat.PLUGIN_RC_OK
def on_msg(server, args):
nick, buffer, message = [unicode(s, local_charset) for s in parse_privmsg(server, args)]
message = strip_irc_colors(message)
if buffer == channel_name and server == server_name:
latin = sum(1 for char in message if u'\u0041' <= char <= u'\u0080')
ascii8 = sum(1 for char in message if u'\u0080' <= char <= u'\u00FF')
cyrillic = sum(1 for char in message if u'\u0400' <= char <= u'\u04FF')
if cyrillic == 0 and ascii8:
if ascii8 >= 0.25 * len(message):
global send_queue, badnicks
if not nick in badnicks:
send_queue.append((u"%s, please, speak English or Russian in UTF-8" % nick, buffer, server))
badnicks[nick] = NOW()
return weechat.PLUGIN_RC_OK
def main():
global local_charset
if weechat.register("gentoo-ru", "0.1", "", "Notify users about bad encoding"):
# locale.getpreferredencoding has side-effects
local_charset = locale.getpreferredencoding(True)
weechat.add_message_handler("PRIVMSG", "on_msg")
weechat.add_timer_handler(1, "flush_queue")
main()
# vim:set tabstop=4 softtabstop=4 shiftwidth=4:
# vim:set foldmethod=marker foldlevel=32 foldmarker={{{,}}}:
# vim:set expandtab: