Skip to content

Instantly share code, notes, and snippets.

@aji
Last active March 19, 2018 07:36
Show Gist options
  • Save aji/4652918 to your computer and use it in GitHub Desktop.
Save aji/4652918 to your computer and use it in GitHub Desktop.
WeeChat script to run private messages through a text to speech program, though theoretically other uses are possible
import weechat
hist = {}
histlen = 32
# [head, ..., tail]
queue = []
def enqueue(msg):
global queue
if msg is None:
return
queue[:0] = [msg]
def dequeue():
global queue
if len(queue) == 0:
return None
queue, s = queue[:-1], queue[-1:]
return s[0]
def log(msg):
weechat.prnt('', 'MSGSAY:' + msg)
def hist_record(bufname, msg):
if not bufname in hist:
hist[bufname] = []
bhist = hist[bufname]
bhist[:0] = [msg]
if len(bhist) > histlen:
bhist = bhist[:histlen]
def sieve(bufname, displayed, highlight, msg):
curname = weechat.buffer_get_string(weechat.current_buffer(), 'name')
if not int(displayed):
return None
if int(highlight):
return msg
if curname == bufname:
return msg
return None
speaking = False
def escape(msg):
for c in '\\\"':
msg = msg.replace(c, '\\'+c)
return '"' + msg + '"'
def send(msg):
if msg is None:
return
cmd = 'espeak '+escape(msg)
#weechat.prnt('', cmd)
weechat.hook_process(cmd, 0, 'send_cb', '')
def tryqueue():
global speaking
if speaking:
return
msg = dequeue()
weechat.prnt('', str(msg))
if msg is None:
return
weechat.prnt('', ' saying')
speaking = True
send(msg)
def send_cb(data, cmd, ret, out, err):
global speaking
speaking = False
tryqueue()
return weechat.WEECHAT_RC_OK
def on_privmsg(data, buf, date, tags, displayed, highlight, prefix, msg):
bufname = weechat.buffer_get_string(buf, 'name')
enqueue(sieve(bufname, displayed, highlight, msg))
tryqueue()
hist_record(bufname, msg)
return weechat.WEECHAT_RC_OK
weechat.register('msgsay', 'Alex Iadicicco', '1.0', 'MIT',
'Run messages through a command', '', '')
weechat.hook_print('', 'irc_privmsg', '', 1, 'on_privmsg', '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment