Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # encoding: utf-8
- __module_name__ = "Select"
- __module_author__ = "dreamer2908"
- __module_version__ = "1.1"
- __module_description__ = "!select inside outside"
- import sys, os, math, random
- try:
- import hexchat as xchat
- except:
- import xchat as xchat
- defaultTimer = None
- terminalSupportUnicode = False
- python2 = False
- win32 = False
- debug = False
- hookList = []
- eventList1 = ["Channel Message", "Channel Msg Hilight"] # If someone HLs you, it's Channel Msg Hilight, not Channel Message
- eventList2 = ["Notice"]
- eventList3 = ["Private Message", "Private Message To Dialog"]
- eventList4 = ["Your Message"]
- inProgress = False
- delayHook = None
- unlockHook = None
- execDelay = 1 # milisecond
- unlockDelay = 2 # must be larger than execDelay
- trigger = '!select ' # or '.c ', 'rarely_decide: ', '.erabe ', or whatever. Make sure there's a space at the end
- def unlockMain(userdata):
- global inProgress, unlockHook
- inProgress = False
- # The document says timer-type callback will be called again and again every n miliseconds, but in my computer, it is called only ONCE.
- # Unhook it in case it suddenly works as what the document says
- xchat.unhook(unlockHook)
- def delayExecute(command, time):
- global delayHook
- delayHook = xchat.hook_timer(time, delayExecuteSub, command)
- def delayExecuteSub(command):
- global delayHook
- xchat.command(command)
- xchat.unhook(delayHook)
- def selectMain(word, word_eol, userdata):
- # solution to recursive events when capturing Your Message event
- global inProgress, unlockHook, execDelay, unlockDelay
- if inProgress:
- return
- inProgress = True
- # userdata 1. channel message 2. notice 3. private msg 4. your message
- # Event Channel Message, Channel Msg Hilight, Private Message all have [0] nickname, [1] the message
- try:
- text = xchat.strip(word[1]).strip()
- if text.startswith(trigger):
- selection = selectSub(word[1])
- if userdata == 1 and selection != None:
- delayExecute('say %s' % selection, execDelay) # use delay execution to fix disordered message <me>inside <me>!select inside outside
- elif userdata == 2 and selection != None: # delay length doesn't matter; even 1ms is enough
- delayExecute('notice %s %s' % (word[0], selection), execDelay)
- elif userdata == 3 and selection != None:
- delayExecute('msg %s %s' % (word[0], selection), execDelay)
- elif userdata == 4 and selection != None:
- # xchat.command('say %s' % word[1]) # nope. Hexchat will say this LATER
- delayExecute('say %s' % selection, execDelay)
- except Exception:
- # unlock it even in case of exception :v
- unlockHook = xchat.hook_timer(unlockDelay, unlockMain)
- raise
- # use delay unlock to eliminate potential issue when more than one request come in less than 1ms
- # shouldn't happen in reality but whatever
- # can also be use to set a minimal delay between 2 requests (useful for public bots)
- unlockHook = xchat.hook_timer(unlockDelay, unlockMain)
- return xchat.EAT_NONE
- def selectSub(text):
- import random, os
- text = xchat.strip(text)
- text = text.strip()
- if text.startswith(trigger):
- text = text[(len(trigger)):]
- # if the text contain ',' then consider ',' the separator (unless it's at the end or beginning)
- # None = all white spaces
- if ',' in text.strip(','):
- separator = ','
- else:
- separator = None
- # split and remove empty entries + trim
- selections = []
- for s in text.split(separator):
- s = s.strip()
- if len(s) > 0:
- selections.append(s)
- if len(selections) > 0:
- # get a random number
- random.seed()
- selected = random.randint(0, len(selections)-1)
- return selections[selected]
- else:
- return None
- def hookStuff():
- global hookList, eventList
- for event in eventList1:
- hookList.append(xchat.hook_print(event, selectMain, 1))
- for event in eventList2:
- hookList.append(xchat.hook_print(event, selectMain, 2))
- for event in eventList3:
- hookList.append(xchat.hook_print(event, selectMain, 3))
- for event in eventList4:
- hookList.append(xchat.hook_print(event, selectMain, 4))
- def unhookStuff():
- global hookList
- for hook in hookList:
- xchat.unhook(hook)
- hookList = []
- def controller(word, word_eol, userdata):
- if len(word) > 0:
- if word[1] == 'stop':
- unhookStuff()
- elif word[1] == 'start' or word[1] == 'restart':
- unhookStuff()
- hookStuff()
- elif word[1] == '!select':
- selection = selectSub(word_eol[1])
- if selection != None:
- xchat.prnt('%s' % word_eol[1])
- xchat.prnt('%s' % selection)
- return xchat.EAT_ALL
- def initStuff():
- import sys, time
- global defaultTimer, terminalSupportUnicode, python2, win32, trigger, unlockDelay, execDelay
- # Stats setup
- if sys.platform == 'win32':
- # On Windows, the best timer is time.clock
- defaultTimer = time.clock
- win32 = True
- else:
- # On most other platforms the best timer is time.time
- defaultTimer = time.time
- win32 = False
- terminalSupportUnicode = True
- try:
- text = u'「いなり、こんこん、恋いろは。」番宣PV'.encode(sys.stdout.encoding)
- except:
- terminalSupportUnicode = False
- if sys.version_info[0] < 3:
- python2 = True
- # check sanity
- if unlockDelay <= execDelay:
- unlockDelay = execDelay + 1
- if not trigger.endswith(' '):
- trigger = trigger + ' '
- def test(word, word_eol, userdata):
- print('Yo')
- return xchat.EAT_ALL
- initStuff()
- hookStuff()
- # xchat.hook_command("selecttest", test, help="Select test")
- xchat.hook_command("select", controller, help="Select controller")
- xchat.prnt(u'%s v%s plugin loaded' % (__module_name__, __module_version__))
Advertisement
Add Comment
Please, Sign In to add comment