dreamer2908

!select inside outside v1.3 (addon for Hexchat)

Oct 1st, 2016
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.56 KB | None | 0 0
  1. #!/usr/bin/python
  2. # encoding: utf-8
  3.  
  4. __module_name__ = "Select"
  5. __module_author__ = "dreamer2908"
  6. __module_version__ = "1.3"
  7. __module_description__ = "!select inside, outside"
  8.  
  9. import sys, os, math, random
  10.  
  11. try:
  12.     import hexchat as xchat
  13. except:
  14.     import xchat as xchat
  15.  
  16. defaultTimer = None
  17. terminalSupportUnicode = False
  18. python2 = False
  19. win32 = False
  20. debug = False
  21.  
  22. hookList = []
  23. eventList1 = ["Channel Message", "Channel Msg Hilight"] # If someone HLs you, it's Channel Msg Hilight, not Channel Message
  24. eventList2 = ["Notice"]
  25. eventList3 = ["Private Message", "Private Message To Dialog"]
  26. eventList4 = ["Your Message"]
  27.  
  28. inProgress = False
  29. execDelay = 1 # milisecond
  30. unlockDelay = 1000 # must be larger than execDelay
  31.  
  32. trigger = '!select ' # or '.c ', 'rarely_decide: ', '.erabe ', or whatever. Make sure there's a space at the end
  33.  
  34. alwaysReap = False
  35.  
  36. def unlockMain(userdata):
  37.     global inProgress
  38.     inProgress = False
  39.     # timer-type callback will be called again and again every n miliseconds if it returns something
  40.     # otherwise it will be removed automatically
  41.  
  42. def delayExecute(command, time):
  43.     xchat.hook_timer(time, delayExecuteSub, command)
  44.  
  45. def delayExecuteSub(command):
  46.     xchat.command(command)
  47.  
  48. def delayInvoke(invokeMe, command, time):
  49.     xchat.hook_timer(time, delayInvokeSub, (invokeMe, command))
  50.  
  51. def delayInvokeSub(stuff):
  52.     invokeMe, command = stuff
  53.     invokeMe(command)
  54.  
  55. def selectMain(word, word_eol, userdata):
  56.     # prevent recursive events when capturing Your Message event
  57.     global inProgress
  58.     if inProgress:
  59.         return
  60.     inProgress = True
  61.     # userdata 1. channel message 2. notice 3. private msg 4. your message
  62.     # Event Channel Message, Channel Msg Hilight, Private Message all have [0] nickname, [1] the message
  63.     try:
  64.         text = xchat.strip(word[1]).strip()
  65.         if text.startswith(trigger):
  66.             contextCommand = xchat.get_context().command
  67.             selection = selectSub(word[1])
  68.             if userdata == 1 and selection != None:
  69.                 delayInvoke(contextCommand, 'say %s' % selection, execDelay) # use delay execution to fix disordered message <me>inside  <me>!select inside outside
  70.             elif userdata == 2 and selection != None: # delay length doesn't matter; even 1ms is enough
  71.                 delayInvoke(contextCommand, 'notice %s %s' % (word[0], selection), execDelay)
  72.             elif userdata == 3 and selection != None:
  73.                 delayInvoke(contextCommand, 'msg %s %s' % (word[0], selection), execDelay)
  74.             elif userdata == 4 and selection != None:
  75.                 # xchat.command('say %s' % word[1]) # nope. Hexchat will say this LATER
  76.                 delayInvoke(contextCommand, 'say %s' % selection, execDelay)
  77.     except Exception:
  78.         # unlock it even in case of exception
  79.         xchat.hook_timer(unlockDelay, unlockMain)
  80.         raise
  81.     # use delay unlock to eliminate potential issue when more than one request come in less than 1ms
  82.     # shouldn't happen in reality but whatever
  83.     # can also be use to set a minimal delay between 2 requests (useful for public bots)
  84.     xchat.hook_timer(unlockDelay, unlockMain)
  85.     return xchat.EAT_NONE
  86.  
  87. def selectSub(text):
  88.     import random, os
  89.  
  90.     if alwaysReap:
  91.         return 'The answer is always "Rape!"'
  92.  
  93.     text = xchat.strip(text)
  94.     text = text.strip()
  95.     if text.startswith(trigger):
  96.         text = text[(len(trigger)):]
  97.         # if the text contain ',' then consider ',' the separator (unless it's at the end or beginning)
  98.         # None = all white spaces
  99.         if ',' in text.strip(','):
  100.             separator = ','
  101.         else:
  102.             separator = None
  103.         # split and remove empty entries + trim
  104.         selections = []
  105.         for s in text.split(separator):
  106.             s = s.strip()
  107.             if len(s) > 0:
  108.                 selections.append(s)
  109.  
  110.         # if one of the option is 'rape' or 'rape something' (misc chars removed), the answer is always rape!
  111.         for tmp in selections:
  112.             tmp = tmp.lower().strip('0987654321`~!@#$%^&*()-_=+;:"\';[{}]\\| ,./<>?')
  113.             if tmp == 'rape' or tmp.startswith('rape '):
  114.                 return 'The answer is always "Rape!"'
  115.  
  116.         if len(selections) > 0:
  117.             # get a random number
  118.             random.seed()
  119.             selected = random.randint(0, len(selections)-1)
  120.             return selections[selected]
  121.         else:
  122.             return None
  123.  
  124. def hookStuff():
  125.     global hookList
  126.     for event in eventList1:
  127.         hookList.append(xchat.hook_print(event, selectMain, 1))
  128.     for event in eventList2:
  129.         hookList.append(xchat.hook_print(event, selectMain, 2))
  130.     for event in eventList3:
  131.         hookList.append(xchat.hook_print(event, selectMain, 3))
  132.     for event in eventList4:
  133.         hookList.append(xchat.hook_print(event, selectMain, 4))
  134.  
  135. def unhookStuff():
  136.     global hookList
  137.     for hook in hookList:
  138.         xchat.unhook(hook)
  139.     hookList = []
  140.  
  141. def controller(word, word_eol, userdata):
  142.     global alwaysReap
  143.     if len(word) > 1:
  144.         if word[1] == 'stop':
  145.             unhookStuff()
  146.             xchat.prnt('Trigger stopped.')
  147.         elif word[1] == 'start' or word[1] == 'restart':
  148.             unhookStuff()
  149.             hookStuff()
  150.             xchat.prnt('Trigger (re)started.')
  151.         elif word[1] == '!select':
  152.             selection = selectSub(word_eol[1])
  153.             if selection != None:
  154.                 xchat.prnt('%s' % word_eol[1])
  155.                 xchat.prnt('%s' % selection)
  156.         elif word[1] == 'reap':
  157.             if len(word) > 2 and word[2] == 'on':
  158.                 alwaysReap = True
  159.                 xchat.prnt('Reap mode on.')
  160.             else:
  161.                 alwaysReap = False
  162.                 xchat.prnt('Reap mode off.')
  163.         else:
  164.             showReadme()
  165.     else:
  166.         showReadme()
  167.     return xchat.EAT_ALL
  168.  
  169. def showReadme():
  170.     xchat.prnt(__module_name__ + ' v' + __module_version__ + ' by ' + __module_author__)
  171.     xchat.prnt('Usage:')
  172.     xchat.prnt('    !select selection a, selection b, selection c, etc')
  173.     xchat.prnt('Config:')
  174.     xchat.prnt('    /select stop|start|restart')
  175.     xchat.prnt('    /select reap on|off')
  176.  
  177. def initStuff():
  178.     import sys, time
  179.     global defaultTimer, terminalSupportUnicode, python2, win32, trigger, unlockDelay, execDelay
  180.  
  181.     # Stats setup
  182.     if sys.platform == 'win32':
  183.         # On Windows, the best timer is time.clock
  184.         defaultTimer = time.clock
  185.         win32 = True
  186.     else:
  187.         # On most other platforms the best timer is time.time
  188.         defaultTimer = time.time
  189.         win32 = False
  190.  
  191.     try:
  192.         text = u'「いなり、こんこん、恋いろは。」番宣PV'.encode(sys.stdout.encoding)
  193.         terminalSupportUnicode = True
  194.     except:
  195.         terminalSupportUnicode = False
  196.  
  197.     if sys.version_info[0] < 3:
  198.         python2 = True
  199.  
  200.     # check sanity
  201.     if unlockDelay <= execDelay:
  202.         unlockDelay = execDelay + 1
  203.  
  204.     if not trigger.endswith(' '):
  205.         trigger = trigger + ' '
  206.  
  207. def test(word, word_eol, userdata):
  208.     xchat.prnt('Yo')
  209.     return xchat.EAT_ALL
  210.  
  211. initStuff()
  212. hookStuff()
  213. # xchat.hook_command("selecttest", test, help="Select test")
  214. xchat.hook_command("select", controller, help="Select controller")
  215. xchat.prnt(u'%s v%s plugin loaded' % (__module_name__, __module_version__))
Advertisement
Add Comment
Please, Sign In to add comment