From 2e17cc22c9e8554c99fca1c6fb48dff8de6c9fe5 Mon Sep 17 00:00:00 2001 From: Tony Blyler Date: Thu, 9 Jun 2016 22:09:35 -0400 Subject: [PATCH] Initial commit --- README.md | 7 ++++++- pyshnotify.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 pyshnotify.py diff --git a/README.md b/README.md index 8cef412..850430c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # pyshnotify -Weechat shell executions from notifications +[Weechat](https://weechat.org) shell executions from notifications + +## Installation and Usage +1. `curl https://raw.githubusercontent.com/tblyler/pyshnotify/master/pyshnotify.py > "${HOME}/.weechat/python/autoload/pyshnotify.py` +2. If Weechat is running you can load the plugin with `/python reload` +3. Set the command to run with `/set plugins.var.python.pyshnotify.command "curl example.com/{{buffer_name}}/{{msg}}"` diff --git a/pyshnotify.py b/pyshnotify.py new file mode 100644 index 0000000..b86c53e --- /dev/null +++ b/pyshnotify.py @@ -0,0 +1,32 @@ +import weechat as w +import shlex, subprocess + +SCRIPT_NAME = "pyshnotify" +SCRIPT_AUTHOR = "Tony Blyler " +SCRIPT_VERSION = "1.0" +SCRIPT_LICENSE = "GPL3" +SCRIPT_DESC = "Execute command from highlights and private messages" + +def on_msg(data, buffer, timestamp, tags, displayed, highlight, sender, message): + if data == "private" or int(highlight) or w.buffer_get_string(buffer, "localvar_type") == "private": + buffer_name = w.buffer_get_string(buffer, "name") + cmd = w.config_get_plugin("command") + if not cmd: + return w.WEECHAT_RC_OK + + cmd = cmd.replace("{{buffer_name}}", buffer_name).replace("{{msg}}", message) + + proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = proc.communicate() + + if proc.returncode != 0: + w.prnt("", weechat.prefix("error") + " Unable to run command: '" + cmd + "' stdout: '" + stdout + "' stderr: '" + stderr + "'") + return w.WEECHAT_RC_ERROR + + return w.WEECHAT_RC_OK + +if __name__ == "__main__" and w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""): + w.config_set_desc_plugin("command", "Shell command to execute {{buffer_name}} and {{msg}} will be replaced per message") + if not w.config_get_plugin("command"): + w.config_set_plugin("command", "") + w.hook_print("", "irc_privmsg", "", 1, "on_msg", "")