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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# -*- coding: iso-8859-1 -*-
# =============================================================================
# autoauth.py (c) October 2005 by kolter <kolter+dev@openics.org>
# Python script for WeeChat.
#
# Licence : GPL v2
# Description : Permits to auto-authenticate when changing nick
# Syntax : try /auth help to get help on this script
#
#
# ### changelog ###
#
# * version 0.5
# - fix bug when script script is run for first time
# - rewrite half script to improve access to settings
# - add a feature to permit to run command(s) when identified
# - add completion for commands
# * version 0.4
# - use set_plugin_config and get_plugin_config to read ans save settings
# - remove deprecated import
# * version 0.3
# - add return codes
# * version 0.2
# - correct weechatdir with weechat_dir while using weechat.get_info
# * version 0.1 :
# - first release
#
# =============================================================================
VERSION="0.5"
NAME="autoauth"
import weechat
weechat.register (NAME, VERSION, "", "Auto authentification while changing nick")
weechat.add_message_handler("NOTICE", "auth_notice_check")
weechat.add_command_handler(
"auth",
"auth_command",
"Auto authentification while changing nick",
"{ add $nick $pass [$server=current] | del $nick [$server=current] | list | cmd [$command [$server=current]] }",
" add : add authorization for $nick with password $pass for $server\n"
" del : del authorization for $nick for $server\n"
" list : list all authorization settings\n"
" cmd : command(s) (separated by '|') to run when identified for $server\n"
" %n will be replaced by current nick in each command",
"add|del|list|cmd %- %S %S"
)
def auth_cmdlist():
cmd = ''
cmds = weechat.get_plugin_config("commands")
if cmds == '':
weechat.prnt("[%s] commands (empty)" % (NAME))
else:
weechat.prnt("[%s] commands (list)" % (NAME))
for c in cmds.split("####"):
weechat.prnt(" --> %s : '%s' " % (c.split(":::")[0], c.split(":::")[1]))
def auth_cmdget(server):
cmd = ''
cmds = weechat.get_plugin_config("commands")
if cmds != '':
for c in cmds.split("####"):
if c.find(":::") != -1:
if c.split(":::")[0] == server:
cmd = ":::".join(c.split(":::")[1:])
break
return cmd
def auth_cmdset(server, command):
cmds = weechat.get_plugin_config("commands")
found = False
conf = []
if cmds != '':
for c in cmds.split("####"):
if c.find(":::") != -1:
if c.split(":::")[0] == server:
found = True
conf.append("%s:::%s" % (server, command))
else:
conf.append(c)
if not found:
conf.append("%s:::%s" % (server, command))
weechat.set_plugin_config("commands", "####".join(conf))
weechat.prnt("[%s] command '%s' successfully added for server %s" % (NAME, command, server))
def auth_cmdunset(server):
cmds = weechat.get_plugin_config("commands")
found = False
conf = []
if cmds != '':
for c in cmds.split("####"):
if c.find(":::") != -1:
if c.split(":::")[0] != server:
conf.append(c)
else:
found = True
if found:
weechat.prnt("[%s] command for server '%s' successfully removed" % (NAME, server))
weechat.set_plugin_config("commands", "####".join(conf))
def auth_cmd(args, server):
if server == '':
if args == '':
auth_cmdlist()
else:
weechat.prnt("[%s] error while setting command, can't find a server" % (NAME))
else:
if args == '':
auth_cmdunset(server)
else:
auth_cmdset(server, args)
def auth_list():
data = weechat.get_plugin_config("data")
if data == "":
weechat.prnt("[%s] accounts (empty)" % (NAME))
else:
weechat.prnt("[%s] accounts (list)" % (NAME))
for e in data.split(","):
if e.find("=") == -1:
continue
(serv_nick, passwd) = e.split("=")
(server, nick) = serv_nick.split(".")
weechat.prnt(" --> %s@%s " % (nick, server))
def auth_notice_check(server, args):
if args.find("If this is your nickname, type /msg NickServ") != -1 or args.find("This nickname is registered and protected.") != -1 :
passwd = auth_get(weechat.get_info("nick"), server)
if passwd != None:
weechat.command("/quote nickserv identify %s" % (passwd), "", server)
commands = auth_cmdget(server)
if commands != '':
for c in commands.split("|"):
weechat.command(c.strip().replace("%n", weechat.get_info('nick')))
return weechat.PLUGIN_RC_OK
def auth_del(the_nick, the_server):
data = weechat.get_plugin_config("data")
found = False
conf = []
for e in data.split(","):
if e.find("=") == -1:
continue
(serv_nick, passwd) = e.split("=")
(server, nick) = serv_nick.split(".")
if the_nick == nick and the_server == server:
found = True
else:
conf.append("%s.%s=%s" % (server, nick, passwd))
if found:
weechat.set_plugin_config("data", ",".join(conf))
weechat.prnt("[%s] nick '%s@%s' successfully remove" % (NAME, the_nick, the_server))
else:
weechat.prnt("[%s] an error occured while removing nick '%s@%s' (not found)" % (NAME, the_nick, the_server))
def auth_add(the_nick, the_passwd, the_server):
data = weechat.get_plugin_config("data")
found = False
conf = []
for e in data.split(","):
if e.find("=") == -1:
continue
(serv_nick, passwd) = e.split("=")
(server, nick) = serv_nick.split(".")
if the_nick == nick and the_server == server:
passwd = the_passwd
found = True
conf.append("%s.%s=%s" % (server, nick, passwd))
if not found:
conf.append("%s.%s=%s" % (the_server, the_nick, the_passwd))
weechat.set_plugin_config("data", ",".join(conf))
weechat.prnt("[%s] nick '%s@%s' successfully added" % (NAME, the_nick, the_server))
def auth_get(the_nick, the_server):
data = weechat.get_plugin_config("data")
for e in data.split(","):
if e.find("=") == -1:
continue
(serv_nick, passwd) = e.split("=")
(server, nick) = serv_nick.split(".")
if the_nick == nick and the_server == server:
return passwd
return None
def auth_command(server, args):
list_args = args.split(" ")
#strip spaces
while '' in list_args:
list_args.remove('')
while ' ' in list_args:
list_args.remove(' ')
if len(list_args) == 0:
weechat.command("/help auth")
elif list_args[0] not in ["add", "del", "list", "cmd"]:
weechat.prnt("[%s] bad option while using /auth command, try '/help auth' for more info" % (NAME))
elif list_args[0] == "cmd":
if len(list_args[1:]) == 1 and list_args[1] in weechat.get_server_info().keys():
auth_cmd("", list_args[1])
elif len(list_args[1:]) == 1:
auth_cmd(list_args[1], weechat.get_info('server'))
elif len(list_args[1:]) >= 2:
if list_args[-1] in weechat.get_server_info().keys():
auth_cmd(" ".join(list_args[1:-1]), list_args[-1])
else:
auth_cmd(" ".join(list_args[1:]), weechat.get_info('server'))
else:
auth_cmd(" ".join(list_args[1:]), weechat.get_info(server))
elif list_args[0] == "list":
auth_list()
elif list_args[0] == "add":
if len(list_args) < 3 or (len(list_args) == 3 and weechat.get_info("server") == ''):
weechat.prnt("[%s] bad option while using /auth command, try '/help auth' for more info" % (NAME))
else:
if len(list_args) == 3:
auth_add(list_args[1], list_args[2], weechat.get_info("server"))
else:
auth_add(list_args[1], list_args[2], list_args[3])
elif list_args[0] == "del":
if len(list_args) < 2:
weechat.prnt("[%s] bad option while using /auth command, try '/help auth' for more info" % (NAME))
else:
if len(list_args) == 2:
auth_del(list_args[1], weechat.get_info("server"))
else:
auth_del(list_args[1], list_args[2])
else:
pass
return weechat.PLUGIN_RC_OK
|