dreamer2908

!select inside outside

Aug 11th, 2014
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.53 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.1"
  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. delayHook = None
  30. unlockHook = None
  31. execDelay = 1 # milisecond
  32. unlockDelay = 2 # must be larger than execDelay
  33.  
  34. trigger = '!select ' # or '.c ', 'rarely_decide: ', '.erabe ', or whatever. Make sure there's a space at the end
  35.  
  36. def unlockMain(userdata):
  37.     global inProgress, unlockHook
  38.     inProgress = False
  39.     # The document says timer-type callback will be called again and again every n miliseconds, but in my computer, it is called only ONCE.
  40.     # Unhook it in case it suddenly works as what the document says
  41.     xchat.unhook(unlockHook)
  42.  
  43. def delayExecute(command, time):
  44.     global delayHook
  45.     delayHook = xchat.hook_timer(time, delayExecuteSub, command)
  46.  
  47. def delayExecuteSub(command):
  48.     global delayHook
  49.     xchat.command(command)
  50.     xchat.unhook(delayHook)
  51.  
  52. def selectMain(word, word_eol, userdata):
  53.     # solution to recursive events when capturing Your Message event
  54.     global inProgress, unlockHook, execDelay, unlockDelay
  55.     if inProgress:
  56.         return
  57.     inProgress = True
  58.     # userdata 1. channel message 2. notice 3. private msg 4. your message
  59.     # Event Channel Message, Channel Msg Hilight, Private Message all have [0] nickname, [1] the message
  60.     try:
  61.         text = xchat.strip(word[1]).strip()
  62.         if text.startswith(trigger):
  63.             selection = selectSub(word[1])
  64.             if userdata == 1 and selection != None:
  65.                 delayExecute('say %s' % selection, execDelay) # use delay execution to fix disordered message <me>inside  <me>!select inside outside
  66.             elif userdata == 2 and selection != None: # delay length doesn't matter; even 1ms is enough
  67.                 delayExecute('notice %s %s' % (word[0], selection), execDelay)
  68.             elif userdata == 3 and selection != None:
  69.                 delayExecute('msg %s %s' % (word[0], selection), execDelay)
  70.             elif userdata == 4 and selection != None:
  71.                 # xchat.command('say %s' % word[1]) # nope. Hexchat will say this LATER
  72.                 delayExecute('say %s' % selection, execDelay)
  73.     except Exception:
  74.         # unlock it even in case of exception :v
  75.         unlockHook = xchat.hook_timer(unlockDelay, unlockMain)
  76.         raise
  77.     # use delay unlock to eliminate potential issue when more than one request come in less than 1ms
  78.     # shouldn't happen in reality but whatever
  79.     # can also be use to set a minimal delay between 2 requests (useful for public bots)
  80.     unlockHook = xchat.hook_timer(unlockDelay, unlockMain)
  81.     return xchat.EAT_NONE
  82.  
  83. def selectSub(text):
  84.     import random, os
  85.     text = xchat.strip(text)
  86.     text = text.strip()
  87.     if text.startswith(trigger):
  88.         text = text[(len(trigger)):]
  89.         # if the text contain ',' then consider ',' the separator (unless it's at the end or beginning)
  90.         # None = all white spaces
  91.         if ',' in text.strip(','):
  92.             separator = ','
  93.         else:
  94.             separator = None
  95.         # split and remove empty entries + trim
  96.         selections = []
  97.         for s in text.split(separator):
  98.             s = s.strip()
  99.             if len(s) > 0:
  100.                 selections.append(s)
  101.         if len(selections) > 0:
  102.             # get a random number
  103.             random.seed()
  104.             selected = random.randint(0, len(selections)-1)
  105.             return selections[selected]
  106.         else:
  107.             return None
  108.  
  109. def hookStuff():
  110.     global hookList, eventList
  111.     for event in eventList1:
  112.         hookList.append(xchat.hook_print(event, selectMain, 1))
  113.     for event in eventList2:
  114.         hookList.append(xchat.hook_print(event, selectMain, 2))
  115.     for event in eventList3:
  116.         hookList.append(xchat.hook_print(event, selectMain, 3))
  117.     for event in eventList4:
  118.         hookList.append(xchat.hook_print(event, selectMain, 4))
  119.  
  120. def unhookStuff():
  121.     global hookList
  122.     for hook in hookList:
  123.         xchat.unhook(hook)
  124.     hookList = []
  125.  
  126. def controller(word, word_eol, userdata):
  127.     if len(word) > 0:
  128.         if word[1] == 'stop':
  129.             unhookStuff()
  130.         elif word[1] == 'start' or word[1] == 'restart':
  131.             unhookStuff()
  132.             hookStuff()
  133.         elif word[1] == '!select':
  134.             selection = selectSub(word_eol[1])
  135.             if selection != None:
  136.                 xchat.prnt('%s' % word_eol[1])
  137.                 xchat.prnt('%s' % selection)
  138.     return xchat.EAT_ALL
  139.  
  140. def initStuff():
  141.     import sys, time
  142.     global defaultTimer, terminalSupportUnicode, python2, win32, trigger, unlockDelay, execDelay
  143.  
  144.     # Stats setup
  145.     if sys.platform == 'win32':
  146.         # On Windows, the best timer is time.clock
  147.         defaultTimer = time.clock
  148.         win32 = True
  149.     else:
  150.         # On most other platforms the best timer is time.time
  151.         defaultTimer = time.time
  152.         win32 = False
  153.  
  154.     terminalSupportUnicode = True
  155.     try:
  156.         text = u'「いなり、こんこん、恋いろは。」番宣PV'.encode(sys.stdout.encoding)
  157.     except:
  158.         terminalSupportUnicode = False
  159.  
  160.     if sys.version_info[0] < 3:
  161.         python2 = True
  162.  
  163.     # check sanity
  164.     if unlockDelay <= execDelay:
  165.         unlockDelay = execDelay + 1
  166.  
  167.     if not trigger.endswith(' '):
  168.         trigger = trigger + ' '
  169.  
  170. def test(word, word_eol, userdata):
  171.     print('Yo')
  172.     return xchat.EAT_ALL
  173.  
  174. initStuff()
  175. hookStuff()
  176. # xchat.hook_command("selecttest", test, help="Select test")
  177. xchat.hook_command("select", controller, help="Select controller")
  178. xchat.prnt(u'%s v%s plugin loaded' % (__module_name__, __module_version__))
Advertisement
Add Comment
Please, Sign In to add comment