Permits to auto-authenticate when changing nick.
Author: kolter
— Version: 1.3
— License: GPL-2.0-or-later
For WeeChat ≥ 0.3.0.
Tags: irc, py2, py3
Added: 2011-10-04
— Updated: 2021-11-07
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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | # -*- coding: utf-8 -*- # ============================================================================= # autoauth.py (c) October 2005 by kolter <kolter@openics.org> # Python script for WeeChat. # # Licence : GPL v2 # Description : Permits to auto-authenticate when changing nick # Syntax : try /autoauth help to get help on this script # # # ### changelog ### # # * version 1.3 (Sébastien Helleu <flashcode@flashtux.org>) # - make script compatible with WeeChat >= 3.4 # (new parameters in function hdata_search) # * version 1.2 (Sébastien Helleu <flashcode@flashtux.org>) # - make script compatible with Python 3 # * version 1.1 (CrazyCat <crazycat@c-p-f.org>) # - add a way to manage NickServ nick and host # * version 1.0 (Simmo Saan <simmo.saan@gmail.com>) # - rename command /auth to /autoauth # * version 0.10 (Felix Eckhofer <felix@tribut.de>) # - fix "/auth cmd" commandline parsing # * version 0.9 (Felix Eckhofer <felix@tribut.de>) # - fix commands execution # - force correct server for /quote # * version 0.8 (excalibr@freenode) # - respond only to notice message from NickServ # * version 0.7 (Adam Spiers <weechat@adamspiers.org>) # - allow commas in passwords # - fix for FreeNode # * version 0.6 (CrazyCat <crazycat@c-p-f.org>) # - adaptation for weechat 0.3.0 # * 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 # # ============================================================================= from __future__ import print_function VERSION = "1.3" NAME = "autoauth" AUTHOR = "Kolter" DELIMITER = "|@|" import_ok = True try: import weechat except ImportError: print("Script must be run under weechat. https://weechat.org/") import_ok = False import re weechat.register( NAME, AUTHOR, VERSION, "GPL2", "Auto authentification while changing nick", "", "", ) weechat.hook_signal("*,irc_in2_notice", "auth_notice_check", "") weechat.hook_command( "autoauth", "Auto authentification while changing nick", "{ add $nick $pass [$server=current] | del $nick [$server=current] | list " "| cmd [$command [$server=current]] " "| ns [$Nick[!username[@host]]] [$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\n" " ns : set NickServ mask (or part of mask) for $server, the NickServ " "nick is mandatory", "add|del|list|cmd %- %S %S", "auth_command", "", ) def auth_cmdlist(): cmd = "" cmds = weechat.config_get_plugin("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.config_get_plugin("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.config_get_plugin("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.config_set_plugin("commands", "####".join(conf)) weechat.prnt( "", "[%s] command '%s' successfully added for server %s" % (NAME, command, server), ) def auth_cmdunset(server): cmds = weechat.config_get_plugin("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.config_set_plugin("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.config_get_plugin("data") if data == "": weechat.prnt("", "[%s] accounts (empty)" % (NAME)) else: weechat.prnt("", "[%s] accounts (list)" % (NAME)) for e in data.split(DELIMITER): 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(data, buffer, args): server = buffer.split(",")[0] nickserv = auth_nsget(server) (nnick, nhost) = nickserv.split("!") if ( args.startswith(":" + nickserv) and args.find("/msg NickServ IDENTIFY") != -1 ): # args.find("If this is your nickname, type /msg NickServ") != -1 or args.find("This nickname is registered") != -1 : passwd = auth_get(weechat.info_get("irc_nick", server), server) if passwd != None: weechat.command( server, "/quote -server %s %s identify %s" % (server, nnick, passwd), ) commands = auth_cmdget(server) if commands != "": for c in commands.split("|"): weechat.command( server, c.strip().replace( "%n", weechat.info_get("irc_nick", server) ), ) return weechat.WEECHAT_RC_OK def auth_del(the_nick, the_server): data = weechat.config_get_plugin("data") found = False conf = [] for e in data.split(DELIMITER): 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.config_set_plugin("data", DELIMITER.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.config_get_plugin("data") found = False conf = [] for e in data.split(DELIMITER): 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.config_set_plugin("data", DELIMITER.join(conf)) weechat.prnt( "", "[%s] nick '%s@%s' successfully added" % (NAME, the_nick, the_server), ) def auth_get(the_nick, the_server): data = weechat.config_get_plugin("data") for e in data.split(DELIMITER): 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 get_channel_from_buffer_args(buffer, args): server_name = weechat.buffer_get_string(buffer, "localvar_server") channel_name = args if not channel_name: channel_name = weechat.buffer_get_string(buffer, "localvar_channel") match_data = re.match("\A(irc.)?([^.]+)\.(#\S+)\Z", channel_name) if match_data: channel_name = match_data.group(3) server_name = match_data.group(2) return server_name, channel_name def search_server(hdata, servers, server_name): """Search the IRC server using hdata_search function.""" weechat_version = int(weechat.info_get("version_number", "") or 0) if weechat_version >= 0x03040000: return weechat.hdata_search( hdata, servers, "${irc_server.name} == ${server_name}", {}, {"server_name": server_name}, {}, 1, ) return weechat.hdata_search( hdata, servers, "${irc_server.name} == " + server_name, 1 ) def auth_command(data, buffer, args): list_args = args.split(" ") server, channel = get_channel_from_buffer_args(buffer, args) # strip spaces while "" in list_args: list_args.remove("") while " " in list_args: list_args.remove(" ") h_servers = weechat.hdata_get("irc_server") l_servers = weechat.hdata_get_list(h_servers, "irc_servers") if len(list_args) == 0: weechat.command(buffer, "/help autoauth") elif list_args[0] not in ["add", "del", "list", "cmd", "ns"]: weechat.prnt( buffer, "[%s] bad option while using /autoauth command, " "try '/help autoauth' for more info" % (NAME), ) elif list_args[0] == "cmd": if len(list_args[1:]) == 1 and search_server(h_servers, l_servers, list_args[1]): auth_cmd("", list_args[1]) elif len(list_args[1:]) == 1: auth_cmd(list_args[1], server) elif len(list_args[1:]) >= 2: if search_server(h_servers, l_servers, list_args[-1]): auth_cmd(" ".join(list_args[1:-1]), list_args[-1]) else: auth_cmd(" ".join(list_args[1:]), server) else: auth_cmd(" ".join(list_args[1:]), server) elif list_args[0] == "ns": if len(list_args[1:]) == 1 and search_server(h_servers, l_servers, list_args[1]): auth_ns("", list_args[1]) elif len(list_args[1:]) == 1: auth_ns(list_args[1], server) elif len(list_args[1:]) == 2: if search_server(h_servers, l_servers, list_args[-1]): auth_ns(" ".join(list_args[1:-1]), list_args[-1]) else: auth_ns(" ".join(list_args[1:]), server) else: auth_ns(" ".join(list_args[1:]), server) elif list_args[0] == "list": auth_list() elif list_args[0] == "add": if len(list_args) < 3 or (len(list_args) == 3 and server == ""): weechat.prnt( buffer, "[%s] bad option while using /autoauth command, try '/help autoauth' for more info" % (NAME), ) else: if len(list_args) == 3: auth_add(list_args[1], list_args[2], 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( buffer, "[%s] bad option while using /autoauth command, try '/help autoauth' for more info" % (NAME), ) else: if len(list_args) == 2: auth_del(list_args[1], server) else: auth_del(list_args[1], list_args[2]) else: pass return weechat.WEECHAT_RC_OK def auth_nslist(): ns = "NickServ!services@services" nss = weechat.config_get_plugin("nickservs") if nss == "": weechat.prnt( "", "[%s] NickServ : NickServ!services@services" % (NAME) ) else: weechat.prnt("", "[%s] NickServ (list)" % (NAME)) for n in nss.split("####"): weechat.prnt( "", " --> %s : '%s' " % (n.split(":::")[0], n.split(":::")[1]), ) def auth_nsget(server): ns = "NickServ!services@services" nss = weechat.config_get_plugin("nickservs") if nss != "": for n in nss.split("####"): if n.find(":::") != -1: if n.split(":::")[0] == server: ns = ":::".join(n.split(":::")[1:]) break return ns def auth_nsset(server, nickserv): nss = weechat.config_get_plugin("nickservs") found = False conf = [] if nss != "": for n in nss.split("####"): if n.find(":::") != -1: if n.split(":::")[0] == server: found = True conf.append("%s:::%s" % (server, nickserv)) else: conf.append(n) if not found: conf.append("%s:::%s" % (server, nickserv)) weechat.config_set_plugin("nickservs", "####".join(conf)) weechat.prnt( "", "[%s] NickServ '%s' successfully added for server %s" % (NAME, nickserv, server), ) def auth_nsunset(server): nss = weechat.config_get_plugin("nickservs") found = False conf = [] if nss != "": for n in nss.split("####"): if n.find(":::") != -1: if n.split(":::")[0] != server: conf.append(n) else: found = True if found: weechat.prnt( "", "[%s] NickServ for server '%s' successfully removed" % (NAME, server), ) weechat.config_set_plugin("nickservs", "####".join(conf)) def auth_ns(args, server): if server == "": if args == "": auth_nslist() else: weechat.prnt( "", "[%s] error while setting NickServ, can't find a server" % (NAME), ) else: if args == "": auth_nsunset(server) else: auth_nsset(server, args) |