Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Python !tell script for Hexchat
- ## by Cycloneblaze
- __module_name__ = "tellMessage"
- __module_version__ = "4.3"
- __module_description__ = "Store messages for users and deliver them later. Use !tell"
- ##
- ## version history:
- ## 0.1 - first version
- ## - just writing the thing
- ## - additions listed like so
- ## 1.0 - first working version :P
- ## - tells work with !tell
- ## 1.1 - now only catches !tell at the start of the messages, rather than anywhere
- ## - proper load / unload text
- ## - uses /say rather than /botserv say
- ## 2.0 - tells are split into public tells (using !tell) and private tells (using /notice botname @tell)
- ## - tells are stored seperately for public / private
- ## 3.0 - new preference to define whether the user running the script or botserv repeats the stored
- ## messages
- ## - now catches all OSErrors, which should include PermissionErrors, FileNotFoundErrors and any
- ## other error which would pop up and continually send, with try-except clauses
- ## - banned nicknames consolidated into a set of variables for easier modifications
- ## - plugin preferences can now be set and modified with commands (cuurently only 1 exists)
- ## - deprecated private tells (the @tell function), they don't work very well and aren't very private
- ## 3.1 - changed the reply message
- ## 3.2 - fixed a bug caused by not capturing all text events (hilights)
- ## - removed private tells (almost) entirely
- ## 4.0 - added a confirmation message when a tell is sent
- ## - added a message telling the time since the message was stored, given when the message is delivered
- ## - added prefs for both of these so they can be turned off
- ## - consolidated pref settings into one function
- ## - grammar: repeat -> deliver, tell -> messaage
- ## - added a command to list the nicknames messages awaiting delivery
- ## 4.1 - now option 2 for /confirm uses /notice to deliver the confirmation privately
- ## - removed zeroes
- ## - new format for files: old messages will crash the script (but I won't fix it cause there aren't any 7:^])
- ## 4.2 - removed zeroes again, in less lines
- ## - now case-insensitive for all commands and names
- ## 4.3 - consolidated some functions for less duplication of stuff
- ## - fixed seconds(s)s(s)ss(s)
- ##
- import hexchat
- import os
- hexchat.prnt("tellMessage script v{} loaded. python code by Cycloneblaze".format(__module_version__))
- directory = 'C:/tells/'
- sub1 = 'C:/tells/public/'
- sub2 = 'C:/tells/private/'
- if not os.path.exists(directory):
- try:
- os.makedirs(directory)
- os.makedirs(sub1)
- os.makedirs(sub2)
- except(OSError):
- pass
- if hexchat.get_pluginpref('tellMessage_botserv') == None:
- hexchat.set_pluginpref('tellMessage_botserv', 1)
- if hexchat.get_pluginpref('tellMessage_confirm') == None:
- hexchat.set_pluginpref('tellMessage_confirm', 1)
- if hexchat.get_pluginpref('tellMessage_time') == None:
- hexchat.set_pluginpref('tellMessage_time', 1)
- # initialise script preferences
- services = ('Global', 'OperServ', 'BotServ', 'ChanServ', 'HostServ', 'MemoServ', 'NickServ')
- # don't change, unless I missed one
- bots = ('Q', 'X', 'Porygon2', 'ChanStat', 'ChanStat-2')
- # add the channel bots if they aren't here already
- banned_users = ('tellbot', 'tellbot2')
- # add nicknames who cannot use !tell. Must have at least 2 names :v
- banned = (services + bots + banned_users)
- def timesince(seconds):
- m, s = divmod(seconds, 60)
- h, m = divmod(m, 60)
- d, h = divmod(h, 24)
- global since
- if d < 0 or h < 0 or m < 0 or s < 0:
- since = ['E', 'E', 'E', 'E']
- else:
- since = [d, h, m ,s]
- def public_cb_tell_store(word, word_eol, userdata):
- mesg = [0, 1]
- mesg[0] = hexchat.strip(word[0], -1, 3)
- mesg[1] = hexchat.strip(word[1], -1, 3)
- nick = hexchat.strip(mesg[0], -1, 3)
- chan = hexchat.get_info("channel")
- userlist = hexchat.get_list('users')
- for i in userlist:
- if i.nick == nick:
- storetime = int(i.lasttalk)
- if not nick in banned and mesg[1].find("!") == 0 and mesg[1].lower().find('tell') == 1:
- message = mesg[1].split()
- tonick = message[1]
- fromnick = hexchat.strip(mesg[0])
- store_msg = ' '.join(message[2:])
- store_msg = fromnick + ' ' + str(storetime) + ' ' + store_msg
- store_msg = store_msg + '\n'
- filename = str(sub1 + tonick.lower() + '.txt')
- try:
- with open(filename, 'a') as tells:
- tells.write(store_msg)
- if hexchat.get_pluginpref('tellMessage_confirm') == 1:
- if hexchat.get_pluginpref('tellMessage_botserv') == 1:
- hexchat.command("botserv say {0} Your message to \002{1}\017 was stored successfully. They will receive it the next time they speak under that nickname (case insensitive).".format(chan, tonick))
- elif hexchat.get_pluginpref('tellMessage_botserv') == 0:
- hexchat.command("say Your message to \002{0}\017 was stored successfully. They will receive it the next time they speak under that nickname (case insensitive).".format(tonick))
- elif hexchat.get_pluginpref('tellMessage_confirm') == 2:
- hexchat.command("notice {0} Your message to \002{1}\017 was stored successfully. They will receive it the next time they speak under that nickname (case insensitive).".format(fromnick, tonick))
- except(OSError):
- pass
- return hexchat.EAT_NONE
- trying = 0
- since = 0
- def public_cb_tell_return(word, word_eol, userdata):
- nick = hexchat.strip(word[0])
- chan = hexchat.get_info("channel")
- path = (sub1 + nick.lower() + '.txt')
- receivedtime = 0
- userlist = hexchat.get_list('users')
- for i in userlist:
- if i.nick == nick:
- receivedtime = int(i.lasttalk)
- global trying
- if not nick in banned and os.path.exists(path) and trying == 0:
- try:
- tells = open(path, 'r+')
- trying = 1
- for line in tells:
- linein = line.replace('\n', '')
- seq = linein.split(' ')
- sentby = seq[0]
- storetime = int(seq[1])
- diff = (receivedtime - storetime)
- timesince(diff)
- msg = ' '.join(seq[2:])
- if hexchat.get_pluginpref('tellMessage_botserv') == 1:
- hexchat.command("botserv say {0} \002{1}\017 left a message for {2}: \'{3}\'".format(chan, sentby, nick, msg))
- elif hexchat.get_pluginpref('tellMessage_botserv') == 0:
- hexchat.command("say \002{0}\017 left a message for {1}: \'{2}\'".format(sentby, nick, msg))
- if hexchat.get_pluginpref('tellMessage_time') == 1:
- d, h, m, s = since[0], since[1], since[2], since[3]
- first = "This message was left "
- second_d, second_ds = "\002" + str(d) + "\017 day ", "\002" + str(d) + "\017 days "
- second_h, second_hs = "\002" + str(h) + "\017 hour ", "\002" + str(h) + "\017 hours "
- second_m, second_ms = "\002" + str(m) + "\017 minute ", "\002" + str(m) + "\017 minutes "
- second_s, second_ss = "\002" + str(s) + "\017 second ", "\002" + str(s) + "\017 seconds "
- third = "ago."
- final = first
- if d != 0 and d != 1:
- final = final + second_ds
- elif d != 0 and d == 1:
- final = final + second_d
- if h != 0 and h != 1:
- final = final + second_hs
- elif h != 0 and h == 1:
- final = final + second_h
- if m != 0 and m != 1:
- final = final + second_ms
- elif m != 0 and m == 1:
- final = final + second_m
- if s != 0 and s != 1:
- final = final + second_ss
- elif s != 0 and s == 1:
- final = final + second_s
- final = final + third
- if d == 0 and h == 0 and m == 0 and s == 0:
- final = first + "just moments " + third
- if hexchat.get_pluginpref('tellMessage_botserv') == 1:
- hexchat.command("botserv say {} {}".format(chan, final))
- elif hexchat.get_pluginpref('tellMessage_botserv') == 0:
- hexchat.command("say {}".format(final))
- if d == 'E' or h == 'E' or m == 'E' or s == 'E':
- print("Something went wrong creating the since (probably your system clock changed)")
- tells.close()
- if tells.closed == True:
- try:
- os.remove(path)
- except(OSError):
- pass
- trying = 0
- except(OSError):
- pass
- return hexchat.EAT_NONE
- def prefs_cb(word, word_eol, userdata):
- if word[0].upper() == 'USEBOT':
- if hexchat.get_pluginpref('tellMessage_botserv') == None:
- print('The value did not exist, initialising as 1...')
- hexchat.set_pluginpref('tellMessage_botserv', 1)
- elif len(word) == 1:
- print('Setting is', hexchat.get_pluginpref('tellMessage_botserv'))
- elif word[1] == '1' or word[1] == '0':
- value = hexchat.strip(word[1], -1, 3)
- hexchat.set_pluginpref('tellMessage_botserv', value)
- if hexchat.get_pluginpref('tellMessage_botserv') == int(value):
- print('Setting set to', value)
- else:
- print('The plugin value was somehow not set to what you tried to set it to!')
- else:
- print('Usage: /usebot <value>\nValid settings are 0 (you deliver the messages) and 1 (the bot delivers the messages)')
- elif word[0].upper() == 'SINCE':
- if hexchat.get_pluginpref('tellMessage_time') == None:
- print('The value did not exist, initialising as 1...')
- hexchat.set_pluginpref('tellMessage_time', 1)
- elif len(word) == 1:
- print('Setting is', hexchat.get_pluginpref('tellMessage_time'))
- elif word[1] == '1' or word[1] == '0':
- value = hexchat.strip(word[1], -1, 3)
- hexchat.set_pluginpref('tellMessage_time', value)
- if hexchat.get_pluginpref('tellMessage_time') == int(value):
- print('Setting set to', value)
- else:
- print('The plugin value was somehow not set to what you tried to set it to!')
- else:
- print('Usage: /usebot <value>\nValid settings are 0 (nothing is given) and 1 (time since storage is given)')
- elif word[0].upper() == 'CONFIRM':
- if hexchat.get_pluginpref('tellMessage_confirm') == None:
- print('The value did not exist, initialising as 1...')
- hexchat.set_pluginpref('tellMessage_confirm', 1)
- elif len(word) == 1:
- print('Setting is', hexchat.get_pluginpref('tellMessage_confirm'))
- elif word[1] == '2' or word[1] == '1' or word[1] == '0':
- value = hexchat.strip(word[1], -1, 3)
- hexchat.set_pluginpref('tellMessage_confirm', value)
- if hexchat.get_pluginpref('tellMessage_confirm') == int(value):
- print('Setting set to', value)
- else:
- print('The plugin value was somehow not set to what you tried to set it to!')
- else:
- print('Usage: /usebot <value>\nValid settings are 0 (no confirmation) and 1 (public confirmation of sending) and 2 (private confirmation of sending)')
- elif word[0].upper() == 'LISTPREFS':
- hexchat.prnt('\nThis is a list of all the plugin preferences.\nIt will include preferences from other plugins if they exist.\nAny which start with \'tellMessage_\' are for this plugin.\n\n')
- for i in hexchat.list_pluginpref():
- hexchat.prnt(str(i))
- hexchat.prnt('\nEnd of list.')
- def listmsgs_cb(word, word_eol, userdata):
- print('Nicknames with messages in C:/tells/public/ awaiting delivery:')
- for i in os.listdir(sub1):
- print(str(i).replace('.txt', ''))
- if os.listdir(sub1) == []:
- print(None)
- print('Nicknames with messages in C:/tells/private/ awaiting delivery:')
- for i in os.listdir(sub2):
- print(str(i).replace('.txt', ''))
- if os.listdir(sub2) == []:
- print(None)
- print('The next time these nicknames speak in a channel you are in they will receive their messages.')
- def unload_cb(userdata):
- hexchat.prnt("tellMessage script v{} unloaded".format(__module_version__))
- EVENTS = [("Channel Message"),("Your Message"),("Your Action"),("Channel Action"),("Channel Msg Hilight"),("Channel Action Hilight")]
- for event in EVENTS:
- hexchat.hook_print(event, public_cb_tell_store)
- hexchat.hook_print(event, public_cb_tell_return)
- hexchat.hook_unload(unload_cb)
- hexchat.hook_command("USEBOT", prefs_cb, help="/USEBOT <value>\nValue is either 0 or 1: if 0, stored messages are delivered by you, if 1, they are delivered by the channel bot\nUsed by tellMessage.py")
- hexchat.hook_command("CONFIRM", prefs_cb, help="/CONFIRM <value>\nValue is either 0 or 1: if 0, there is no confirmation message, if 1, a confirmation message is given in public chat by the channel bot\nIf 2, the message is given by you privately with /notice\nUsed by tellMessage.py")
- hexchat.hook_command("SINCE", prefs_cb, help="/SINCE <value>\nValue is either 0 or 1: if 0, no time is given, if 1, the time since the message was stored is given along with the message itself\nUsed by tellMessage.py")
- hexchat.hook_command("LISTMSGS", listmsgs_cb, help="/LISTMSGS \nGives a list of the nicknames with messages awaiting delivery\nUsed by tellMessage.py")
- hexchat.hook_command("LISTPREFS", prefs_cb, help="/LISTPREFS")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement