jacob614

hexchat plugin template (old / bad version)

Mar 29th, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.59 KB | None | 0 0
  1. import xchat
  2. import re
  3. import random
  4.  
  5. __module_name__ = "Bot script"
  6. __module_version__ = "1.0"
  7. __module_description__ = "has bot commands"
  8.  
  9. # Timer
  10. def command_timer(userdata):
  11.     if type(userdata) is str:
  12.         xchat.command(userdata)
  13.     else:
  14.         if (len(userdata) > 2):
  15.             thechannel = xchat.find_context(server=userdata[1],channel=userdata[2])
  16.             if thechannel:
  17.                 thechannel.command(userdata[0])
  18.         else:
  19.             thechannel = xchat.find_context(channel=userdata[1])
  20.             if thechannel:
  21.                 thechannel.command(userdata[0])
  22.  
  23. #very bad rate limiter
  24. wait_timer = 100
  25.  
  26. def timer_reset(userdata):
  27.     global wait_timer
  28.     wait_timer -= 2000
  29.     if wait_timer < 100:
  30.         wait_timer = 100
  31.  
  32. def send(msg):
  33.     global wait_timer
  34.     timer = wait_timer
  35.     wait_timer += 2000
  36.     xchat.hook_timer(2000, timer_reset)
  37.     if wait_timer > 16000:
  38.         return
  39.     if timer < 5000:
  40.         timer = 100
  41.     xchat.hook_timer(timer, command_timer, msg)
  42.  
  43. # All the command functions
  44. command_list = [ ]
  45.  
  46. def command_add(command):
  47.     command_list.append(command)
  48.  
  49. echo_botlist = [ "stewiegriffin", "stewiegriffinsub", "t156", "crsbot", "oytaku", "pytaku", "nucbot", "mitch|hater"]
  50. def command_echo(text, user, server, channel):
  51.     echotext = ""
  52.     stripped = False
  53.     for i in text[1:]:
  54.         if not stripped and user.host != "~wolfy1339@unaffiliated/wolfy1339":
  55.             i = i.lstrip(";+!$@^#%'&.~") #remove bot prefixes and color codes
  56.             for j in echo_botlist: #remove bot names, so !echo can't do commands this way either
  57.                 if j in i.lower():
  58.                     i = ""
  59.         txt = color_strip(i.lower())
  60.         if len(i) > 0:
  61.             echotext = echotext + i + " "
  62.             stripped = True
  63.     if len(echotext) > 0:
  64.         send(["msg " + channel + " " + echotext, server, channel])
  65.  
  66. def command_potato(text, user, server, channel):
  67.     send(["action is a moldy potato", server, channel])
  68.  
  69. def command_list2(text, user, server, channel):
  70.     listtext = ""
  71.     for i in command_list:
  72.         if i[3] == "*":
  73.             Enabled = True
  74.             for j in i[4:]:
  75.                 if j == channel:
  76.                     Enabled = False
  77.             if Enabled:
  78.                 if len(listtext):
  79.                     listtext += ", "
  80.                 listtext += i[0]
  81.         else:
  82.             for j in i[3:]:
  83.                 if j == channel:
  84.                     if len(listtext):
  85.                         listtext += ", "
  86.                     listtext += i[0]
  87.     send(["msg " + channel + " " + user.nick + ": " + listtext, server, channel])
  88.  
  89. def command_help(text, user, server, channel):
  90.     if len(text) < 2:
  91.         text.append("help")
  92.     for i in command_list:
  93.         if text[1] == i[0].lstrip("*") or text[1] == i[0]:
  94.             send(["msg " + channel + " " + user.nick + ": " + i[2], server, channel])
  95.     return
  96.  
  97. def command_enable(text, user, server, channel):
  98.     if len(text) < 2:
  99.         return
  100.     command = text[1]
  101.     if len(text) > 2:
  102.         channel = text[2]
  103.     for i in command_list:
  104.         if command == i[0].lstrip("*") and len(i) > 3:
  105.             if i[3] == "*":
  106.                 for j in i[4:]:
  107.                     if j == channel:
  108.                         i.remove(channel)
  109.                         print "enabled " + command
  110.             else:
  111.                 for j in i[3:]:
  112.                     if j == channel:
  113.                         return
  114.                 i.append(channel)
  115.                 print "enabled " + command
  116.             return
  117.  
  118. def command_disable(text, user, server, channel):
  119.     if len(text) < 2:
  120.         return
  121.     command = text[1]
  122.     if len(text) > 2:
  123.         channel = text[2]
  124.     for i in command_list:
  125.         if command == i[0].lstrip("*") and len(i) > 3:
  126.             if i[3] == "*":
  127.                 for j in i[4:]:
  128.                     if j == channel:
  129.                         return
  130.                 i.append(channel)
  131.                 print "disabled " + command
  132.             else:
  133.                 for j in i[3:]:
  134.                     if j == channel:
  135.                         i.remove(channel)
  136.                         print "disabled " + command
  137.  
  138. ignore_list = [ "OphalBot", "StewieGriffin", "StewieGriffinSub", "t156", "CrsBot", "MitchBot", "Tribot200", "Bott", "Crackbot", "/bot/", "xsBot", "SexiestBrownie"]
  139. def command_ignore(text, user, server, channel):
  140.     if len(text) < 2 or "jac" in text[1] or "wolfy" in text[1]:
  141.         return
  142.     if "unignore" in text[0]:
  143.         for i in ignore_list:
  144.             if i == text[1]:
  145.                 ignore_list.remove(text[1])
  146.                 print "unignored " + text[1]
  147.                 return
  148.     else:
  149.         for i in ignore_list:
  150.             if i == text[1]:
  151.                 return
  152.         ignore_list.append(text[1])
  153.         print "ignored " + text[1]
  154.  
  155. def command_ping(text, user, server, channel):
  156.     send(["msg " + channel + " PONG", server, channel])
  157.  
  158. def color_strip(text):
  159.     regex = re.compile("\x03(?:\d{1,2}(?:,\d{1,2})?)?|\x01|\x02|\x09|\x0f|\x13|\x15|\x16|\x1f", re.UNICODE)
  160.     return regex.sub("", text)
  161.  
  162. class tempuser:
  163.     nick = ""
  164.     host = ""
  165.     prefix = ""
  166.  
  167. def get_user(username, server, channel):
  168.     try:
  169.         if channel is not None:
  170.             thechannel = xchat.find_context(server, channel)
  171.         else:
  172.             thechannel = xchat.get_context()
  173.     except:
  174.         thechannel = channel
  175.     if thechannel:
  176.         thechannel.command("")
  177.         users = thechannel.get_list("users")
  178.         for i in range(len(users)):
  179.             if users[i].nick == username:
  180.                 return users[i]
  181.  
  182. # Main Main Main Main
  183. # Main Main Main Main
  184. # Main Main Main Main
  185. # Main Main Main Main
  186. def main(word, word_eol, userdata):
  187.     if len(word) < 2:
  188.         return
  189.     server = xchat.get_info("server")
  190.     channel = xchat.get_info("channel")
  191.     text = word[1].split()
  192.     if len(text) < 1:
  193.         return
  194.    
  195.     tmpuser = color_strip(word[0])
  196.     user = get_user(tmpuser, server, channel)
  197.     if not user and "#" in channel:
  198.         return
  199.     elif not user:
  200.         user = tempuser()
  201.         user.nick = tmpuser
  202.         user.host = "unknown"
  203.         user.prefix = ""
  204.    
  205.     for i in ignore_list: #ignore both nicks and hosts
  206.         if user.nick == i or i in user.host:
  207.             return
  208.  
  209.     command = text[0].lower()
  210.     for i in command_list:
  211.         if command == i[0] and len(i) > 3:
  212.             if i[3] == "*":
  213.                 Enabled = True
  214.                 for j in i[3:]:
  215.                     if channel == j and not "wolfy" in user.nick and not "1" in user.nick:
  216.                         Enabled = False
  217.                 if Enabled:
  218.                     i[1](text, user, server, channel)
  219.             else:
  220.                 commandEnabled = False
  221.                 for j in i[3:]:
  222.                     if channel == j or j == "*" or "wolfy" in user.nick or "1" in user.nick:
  223.                         commandEnabled = True
  224.                 if commandEnabled:
  225.                     i[1](text, user, server, channel)
  226.  
  227. command_add(["*echo", command_echo, "Repeats text", "##powder-bots"])
  228. command_add(["*potato", command_potato, "I'm a moldy potato", "##powder-bots"])
  229. command_add(["*list", command_list2, "Lists all commands available in the channel", "*"])
  230. command_add(["*help", command_help, "(!help <command>) Shows help for a command", "*"])
  231. command_add(["*enable", command_enable, "Enables a command", ""])
  232. command_add(["*disable", command_disable, "Disables a command", ""])
  233. command_add(["*ignore", command_ignore, "Ignores a user", ""])
  234. command_add(["*unignore", command_ignore, "Unignores a user", ""])
  235. command_add(["*ping", command_ping, "PONG", "*"])
  236.  
  237. # Hooks
  238. def hook_unload(text, user, server, channel):
  239.     xchat.unhook(hooks[0])
  240.     xchat.unhook(hooks[1])
  241.     xchat.unhook(hooks[2])
  242.     xchat.unhook(hooks[3])
  243.     xchat.unhook(hooks[4])
  244.     xchat.unhook(hooks[5])
  245.     xchat.unhook(hooks[6])
  246.     print("*** Unloaded script")
  247.  
  248. def hook_unload2(userdata):
  249.     hook_unload("", "", "", "")
  250.  
  251. command_add(["*panic", hook_unload, "panic and disable all hooks", ""])
  252.  
  253. hooks = [ ]
  254. hooks.append(xchat.hook_print("Channel Message", main))
  255. hooks.append(xchat.hook_print("Channel Msg Hilight", main))
  256. hooks.append(xchat.hook_print("Channel Action", main))
  257. hooks.append(xchat.hook_print("Channel Action Hilight", main))
  258. hooks.append(xchat.hook_print("Your Message", main))
  259. hooks.append(xchat.hook_print("Your Action", main))
  260.    
  261. hooks.append(xchat.hook_unload(hook_unload2))
  262.  
  263. random.seed()
  264. print ("*** Loaded script")
Advertisement
Add Comment
Please, Sign In to add comment