Advertisement
lraven

Untitled

Jul 2nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.14 KB | None | 0 0
  1. ## Python !tell script for Hexchat
  2. ## by Cycloneblaze
  3.  
  4. __module_name__ = "tellMessage"
  5. __module_version__ = "4.3"
  6. __module_description__ = "Store messages for users and deliver them later. Use !tell"
  7.  
  8. ##
  9. ## version history:
  10. ## 0.1 - first version
  11. ## - just writing the thing
  12. ## - additions listed like so
  13. ## 1.0 - first working version :P
  14. ## - tells work with !tell
  15. ## 1.1 - now only catches !tell at the start of the messages, rather than anywhere
  16. ## - proper load / unload text
  17. ## - uses /say rather than /botserv say
  18. ## 2.0 - tells are split into public tells (using !tell) and private tells (using /notice botname @tell)
  19. ## - tells are stored seperately for public / private
  20. ## 3.0 - new preference to define whether the user running the script or botserv repeats the stored
  21. ## messages
  22. ## - now catches all OSErrors, which should include PermissionErrors, FileNotFoundErrors and any
  23. ## other error which would pop up and continually send, with try-except clauses
  24. ## - banned nicknames consolidated into a set of variables for easier modifications
  25. ## - plugin preferences can now be set and modified with commands (cuurently only 1 exists)
  26. ## - deprecated private tells (the @tell function), they don't work very well and aren't very private
  27. ## 3.1 - changed the reply message
  28. ## 3.2 - fixed a bug caused by not capturing all text events (hilights)
  29. ## - removed private tells (almost) entirely
  30. ## 4.0 - added a confirmation message when a tell is sent
  31. ## - added a message telling the time since the message was stored, given when the message is delivered
  32. ## - added prefs for both of these so they can be turned off
  33. ## - consolidated pref settings into one function
  34. ## - grammar: repeat -> deliver, tell -> messaage
  35. ## - added a command to list the nicknames messages awaiting delivery
  36. ## 4.1 - now option 2 for /confirm uses /notice to deliver the confirmation privately
  37. ## - removed zeroes
  38. ## - new format for files: old messages will crash the script (but I won't fix it cause there aren't any 7:^])
  39. ## 4.2 - removed zeroes again, in less lines
  40. ## - now case-insensitive for all commands and names
  41. ## 4.3 - consolidated some functions for less duplication of stuff
  42. ## - fixed seconds(s)s(s)ss(s)
  43. ##
  44.  
  45. import hexchat
  46. import os
  47.  
  48. hexchat.prnt("tellMessage script v{} loaded. python code by Cycloneblaze".format(__module_version__))
  49.  
  50. directory = 'C:/tells/'
  51. sub1 = 'C:/tells/public/'
  52. sub2 = 'C:/tells/private/'
  53. if not os.path.exists(directory):
  54. try:
  55. os.makedirs(directory)
  56. os.makedirs(sub1)
  57. os.makedirs(sub2)
  58. except(OSError):
  59. pass
  60.  
  61. if hexchat.get_pluginpref('tellMessage_botserv') == None:
  62. hexchat.set_pluginpref('tellMessage_botserv', 1)
  63. if hexchat.get_pluginpref('tellMessage_confirm') == None:
  64. hexchat.set_pluginpref('tellMessage_confirm', 1)
  65. if hexchat.get_pluginpref('tellMessage_time') == None:
  66. hexchat.set_pluginpref('tellMessage_time', 1)
  67. # initialise script preferences
  68.  
  69. services = ('Global', 'OperServ', 'BotServ', 'ChanServ', 'HostServ', 'MemoServ', 'NickServ')
  70. # don't change, unless I missed one
  71. bots = ('Q', 'X', 'Porygon2', 'ChanStat', 'ChanStat-2')
  72. # add the channel bots if they aren't here already
  73. banned_users = ('tellbot', 'tellbot2')
  74. # add nicknames who cannot use !tell. Must have at least 2 names :v
  75. banned = (services + bots + banned_users)
  76.  
  77. def timesince(seconds):
  78. m, s = divmod(seconds, 60)
  79. h, m = divmod(m, 60)
  80. d, h = divmod(h, 24)
  81. global since
  82. if d < 0 or h < 0 or m < 0 or s < 0:
  83. since = ['E', 'E', 'E', 'E']
  84. else:
  85. since = [d, h, m ,s]
  86.  
  87. def public_cb_tell_store(word, word_eol, userdata):
  88. mesg = [0, 1]
  89. mesg[0] = hexchat.strip(word[0], -1, 3)
  90. mesg[1] = hexchat.strip(word[1], -1, 3)
  91. nick = hexchat.strip(mesg[0], -1, 3)
  92. chan = hexchat.get_info("channel")
  93. userlist = hexchat.get_list('users')
  94. for i in userlist:
  95. if i.nick == nick:
  96. storetime = int(i.lasttalk)
  97. if not nick in banned and mesg[1].find("!") == 0 and mesg[1].lower().find('tell') == 1:
  98. message = mesg[1].split()
  99. tonick = message[1]
  100. fromnick = hexchat.strip(mesg[0])
  101. store_msg = ' '.join(message[2:])
  102. store_msg = fromnick + ' ' + str(storetime) + ' ' + store_msg
  103. store_msg = store_msg + '\n'
  104. filename = str(sub1 + tonick.lower() + '.txt')
  105. try:
  106. with open(filename, 'a') as tells:
  107. tells.write(store_msg)
  108. if hexchat.get_pluginpref('tellMessage_confirm') == 1:
  109. if hexchat.get_pluginpref('tellMessage_botserv') == 1:
  110. 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))
  111. elif hexchat.get_pluginpref('tellMessage_botserv') == 0:
  112. 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))
  113. elif hexchat.get_pluginpref('tellMessage_confirm') == 2:
  114. 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))
  115.  
  116. except(OSError):
  117. pass
  118.  
  119. return hexchat.EAT_NONE
  120.  
  121. trying = 0
  122. since = 0
  123.  
  124. def public_cb_tell_return(word, word_eol, userdata):
  125. nick = hexchat.strip(word[0])
  126. chan = hexchat.get_info("channel")
  127. path = (sub1 + nick.lower() + '.txt')
  128. receivedtime = 0
  129. userlist = hexchat.get_list('users')
  130. for i in userlist:
  131. if i.nick == nick:
  132. receivedtime = int(i.lasttalk)
  133. global trying
  134. if not nick in banned and os.path.exists(path) and trying == 0:
  135. try:
  136. tells = open(path, 'r+')
  137. trying = 1
  138. for line in tells:
  139. linein = line.replace('\n', '')
  140. seq = linein.split(' ')
  141. sentby = seq[0]
  142. storetime = int(seq[1])
  143. diff = (receivedtime - storetime)
  144. timesince(diff)
  145. msg = ' '.join(seq[2:])
  146. if hexchat.get_pluginpref('tellMessage_botserv') == 1:
  147. hexchat.command("botserv say {0} \002{1}\017 left a message for {2}: \'{3}\'".format(chan, sentby, nick, msg))
  148. elif hexchat.get_pluginpref('tellMessage_botserv') == 0:
  149. hexchat.command("say \002{0}\017 left a message for {1}: \'{2}\'".format(sentby, nick, msg))
  150. if hexchat.get_pluginpref('tellMessage_time') == 1:
  151. d, h, m, s = since[0], since[1], since[2], since[3]
  152. first = "This message was left "
  153. second_d, second_ds = "\002" + str(d) + "\017 day ", "\002" + str(d) + "\017 days "
  154. second_h, second_hs = "\002" + str(h) + "\017 hour ", "\002" + str(h) + "\017 hours "
  155. second_m, second_ms = "\002" + str(m) + "\017 minute ", "\002" + str(m) + "\017 minutes "
  156. second_s, second_ss = "\002" + str(s) + "\017 second ", "\002" + str(s) + "\017 seconds "
  157. third = "ago."
  158. final = first
  159. if d != 0 and d != 1:
  160. final = final + second_ds
  161. elif d != 0 and d == 1:
  162. final = final + second_d
  163. if h != 0 and h != 1:
  164. final = final + second_hs
  165. elif h != 0 and h == 1:
  166. final = final + second_h
  167. if m != 0 and m != 1:
  168. final = final + second_ms
  169. elif m != 0 and m == 1:
  170. final = final + second_m
  171. if s != 0 and s != 1:
  172. final = final + second_ss
  173. elif s != 0 and s == 1:
  174. final = final + second_s
  175. final = final + third
  176. if d == 0 and h == 0 and m == 0 and s == 0:
  177. final = first + "just moments " + third
  178. if hexchat.get_pluginpref('tellMessage_botserv') == 1:
  179. hexchat.command("botserv say {} {}".format(chan, final))
  180. elif hexchat.get_pluginpref('tellMessage_botserv') == 0:
  181. hexchat.command("say {}".format(final))
  182. if d == 'E' or h == 'E' or m == 'E' or s == 'E':
  183. print("Something went wrong creating the since (probably your system clock changed)")
  184. tells.close()
  185. if tells.closed == True:
  186. try:
  187. os.remove(path)
  188. except(OSError):
  189. pass
  190. trying = 0
  191. except(OSError):
  192. pass
  193.  
  194. return hexchat.EAT_NONE
  195.  
  196. def prefs_cb(word, word_eol, userdata):
  197. if word[0].upper() == 'USEBOT':
  198. if hexchat.get_pluginpref('tellMessage_botserv') == None:
  199. print('The value did not exist, initialising as 1...')
  200. hexchat.set_pluginpref('tellMessage_botserv', 1)
  201. elif len(word) == 1:
  202. print('Setting is', hexchat.get_pluginpref('tellMessage_botserv'))
  203. elif word[1] == '1' or word[1] == '0':
  204. value = hexchat.strip(word[1], -1, 3)
  205. hexchat.set_pluginpref('tellMessage_botserv', value)
  206. if hexchat.get_pluginpref('tellMessage_botserv') == int(value):
  207. print('Setting set to', value)
  208. else:
  209. print('The plugin value was somehow not set to what you tried to set it to!')
  210. else:
  211. print('Usage: /usebot <value>\nValid settings are 0 (you deliver the messages) and 1 (the bot delivers the messages)')
  212. elif word[0].upper() == 'SINCE':
  213. if hexchat.get_pluginpref('tellMessage_time') == None:
  214. print('The value did not exist, initialising as 1...')
  215. hexchat.set_pluginpref('tellMessage_time', 1)
  216. elif len(word) == 1:
  217. print('Setting is', hexchat.get_pluginpref('tellMessage_time'))
  218. elif word[1] == '1' or word[1] == '0':
  219. value = hexchat.strip(word[1], -1, 3)
  220. hexchat.set_pluginpref('tellMessage_time', value)
  221. if hexchat.get_pluginpref('tellMessage_time') == int(value):
  222. print('Setting set to', value)
  223. else:
  224. print('The plugin value was somehow not set to what you tried to set it to!')
  225. else:
  226. print('Usage: /usebot <value>\nValid settings are 0 (nothing is given) and 1 (time since storage is given)')
  227. elif word[0].upper() == 'CONFIRM':
  228. if hexchat.get_pluginpref('tellMessage_confirm') == None:
  229. print('The value did not exist, initialising as 1...')
  230. hexchat.set_pluginpref('tellMessage_confirm', 1)
  231. elif len(word) == 1:
  232. print('Setting is', hexchat.get_pluginpref('tellMessage_confirm'))
  233. elif word[1] == '2' or word[1] == '1' or word[1] == '0':
  234. value = hexchat.strip(word[1], -1, 3)
  235. hexchat.set_pluginpref('tellMessage_confirm', value)
  236. if hexchat.get_pluginpref('tellMessage_confirm') == int(value):
  237. print('Setting set to', value)
  238. else:
  239. print('The plugin value was somehow not set to what you tried to set it to!')
  240. else:
  241. print('Usage: /usebot <value>\nValid settings are 0 (no confirmation) and 1 (public confirmation of sending) and 2 (private confirmation of sending)')
  242. elif word[0].upper() == 'LISTPREFS':
  243. 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')
  244. for i in hexchat.list_pluginpref():
  245. hexchat.prnt(str(i))
  246. hexchat.prnt('\nEnd of list.')
  247.  
  248. def listmsgs_cb(word, word_eol, userdata):
  249. print('Nicknames with messages in C:/tells/public/ awaiting delivery:')
  250. for i in os.listdir(sub1):
  251. print(str(i).replace('.txt', ''))
  252. if os.listdir(sub1) == []:
  253. print(None)
  254. print('Nicknames with messages in C:/tells/private/ awaiting delivery:')
  255. for i in os.listdir(sub2):
  256. print(str(i).replace('.txt', ''))
  257. if os.listdir(sub2) == []:
  258. print(None)
  259. print('The next time these nicknames speak in a channel you are in they will receive their messages.')
  260.  
  261. def unload_cb(userdata):
  262. hexchat.prnt("tellMessage script v{} unloaded".format(__module_version__))
  263.  
  264. EVENTS = [("Channel Message"),("Your Message"),("Your Action"),("Channel Action"),("Channel Msg Hilight"),("Channel Action Hilight")]
  265. for event in EVENTS:
  266. hexchat.hook_print(event, public_cb_tell_store)
  267. hexchat.hook_print(event, public_cb_tell_return)
  268. hexchat.hook_unload(unload_cb)
  269. 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")
  270. 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")
  271. 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")
  272. hexchat.hook_command("LISTMSGS", listmsgs_cb, help="/LISTMSGS \nGives a list of the nicknames with messages awaiting delivery\nUsed by tellMessage.py")
  273. hexchat.hook_command("LISTPREFS", prefs_cb, help="/LISTPREFS")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement