archive

- Random tools & helpful resources for IRC
git clone git://git.acid.vegas/archive.git
Log | Files | Refs | Archive

masshl.py (3398B)

      1 # Released into the Public Domain
      2 
      3 import random
      4 #from threading import Thread
      5 #from time import sleep
      6 import weechat
      7 
      8 SCRIPT_NAME    = "masshl"
      9 SCRIPT_AUTHOR  = "The Krusty Krab <wowaname@volatile.ch>"
     10 SCRIPT_VERSION = "1.0"
     11 SCRIPT_LICENSE = "Public domain"
     12 SCRIPT_DESC    = "Provides nicklist hooks."
     13 
     14 if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
     15 	SCRIPT_LICENSE, SCRIPT_DESC, "", ""):
     16 	weechat.hook_command("masshl",
     17 		SCRIPT_DESC,
     18 		"[-do <delay>] text (broken, currently no-op)",
     19 		"-d  Specify a delay at the beginning (e.g. -d 1 for\n"
     20 		"    one second) to insert a delay between messages.\n"
     21 		"    %n - replace with next nick\n"
     22 		"    %N - replace with as many nicks as possible per line\n"
     23 		"    %r - replace with random hex value to thwart antispam\n"
     24 		"-o  Include your own nick in output",
     25 		"-od", "masshl_cmd_cb", "")
     26 
     27 class Loop():
     28 	def __init__(self, buffer, nicks, input, input_method, N_param, delay, opts):
     29 		self.buffer = buffer
     30 		self.nicks = nicks
     31 		self.input = input
     32 		self.input_method = input_method
     33 		self.N_param = N_param
     34 		self.delay = delay
     35 		self.opts = opts
     36 
     37 	def run(self):
     38 		i = -('o' not in self.opts)
     39 		if i == -1: self.nicks.pop(0)
     40 		N_nicks = ""
     41 		output = self.input
     42 		for nick in self.nicks:
     43 			i += 1
     44 			if self.N_param:
     45 				N_nicks += " %s" % nick
     46 				if (nick != self.nicks[-1] and
     47 				 len(output) + len(N_nicks) + len(self.nicks[i]) < 300):
     48 					continue
     49 			else: output = self.input.replace("%n",nick)
     50 			N_nicks = N_nicks.lstrip()
     51 			output = output.replace("%N",N_nicks)
     52 			output = output.replace("%r","%08x" % random.randint(0,0xffffffff))
     53 			if self.input_method == "keybinding":
     54 				weechat.buffer_set(self.buffer, "input", output)
     55 			else:
     56 				weechat.command(self.buffer, output)
     57 #			sleep(self.delay)
     58 			output = self.input
     59 			N_nicks = ""
     60 
     61 def masshl_cmd_cb(data, buffer, args):
     62 	input = args
     63 
     64 	input_method = "command"
     65 	server = weechat.buffer_get_string(buffer, 'localvar_server')
     66 	channel = weechat.buffer_get_string(buffer, 'localvar_channel')
     67 
     68 	if not input or (input[0] == '-' and input.find(' ') == -1):
     69 		input = (input + ' ' if input else '') + weechat.buffer_get_string(buffer, "input")
     70 		input_method = "keybinding"
     71 
     72 	N_param = "%N" in input
     73 	if not N_param and "%n" not in input and "%r" not in input:
     74 		# if we bind this to Enter key, we don't want useless flooding on
     75 		# normal messages
     76 		return weechat.WEECHAT_RC_OK
     77 
     78 	optstop = input and input[0] == '-' and input.find(' ')
     79 	opts = input[1:optstop] if optstop else ''
     80 	cmdstop = 'd' in opts and input.find(' ', optstop+1)
     81 	delay = 0
     82 	if 'd' in opts:
     83 		find = input[optstop+1:cmdstop]
     84 		where = input.find(find, cmdstop+1)
     85 		try: delay = float(find)
     86 		except ValueError:
     87 			weechat.prnt(buffer, "delay must be a float value!")
     88 			return weechat.WEECHAT_RC_ERROR
     89 		input = input[where+len(find):]
     90 	else: input = input[optstop+bool(optstop):]
     91 
     92 	nicklist = weechat.infolist_get("irc_nick", "", "%s,%s" % (server,channel))
     93 
     94 	# dealing with the cursor can get a little tricky. let's use a dict
     95 	# instead, that way we can manipulate just what we need and we can
     96 	# do that with builtins
     97 	nicks = []
     98 	while weechat.infolist_next(nicklist):
     99 		nicks.append(weechat.infolist_string(nicklist, "name"))
    100 
    101 	weechat.infolist_free(nicklist)
    102 
    103 	workhorse = Loop(buffer, nicks, input, input_method, N_param, delay, opts)
    104 	workhorse.run()
    105 
    106 	return weechat.WEECHAT_RC_OK