# vim: set fileencoding=utf-8 tabstop=8 expandtab shiftwidth=4 softtabstop=4 : # Copyright (C) 2012 Florian "The Compiler" Bruhin # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the try: import weechat except ImportError: print("Error: this script is a plugin for weechat and needs to be run " "inside weechat.") raise import shlex, subprocess, string weechat.register("highlight_command", "The Compiler", "0.1", "GPL3", "Call external commands on highlights/messages/dcc", "", "") settings = { "highlight_command": ("/bin/true '%DATE% <%PREFIX%/%BUFFER%> %MESSAGE%'", "Set the command which will be called on a " "highlight. %DATE%, %PREFIX%, %BUFFER% and " "%MESSAGE% will be substituted by the Unixear" "timestamp, Prefix (nick), buffer and the message text " "respectively."), "privmsg_command": ("/bin/true '%DATE% <%PREFIX%> %MESSAGE%'", "Set the command which will be called on a query. " "%DATE%, %PREFIX%, %BUFFER% and %MESSAGE% will be " "substituted by the Unix timestamp, Prefix (nick), " "buffer and the message text, respectively."), } for (option, (default_value, desc)) in settings.items(): if weechat.config_get_plugin(option) == "": weechat.config_set_plugin(option, default_value) weechat.config_set_desc_plugin(option, '%s (default: "%s")' % (desc, default_value)) weechat.hook_print("", # buffer "irc_privmsg", # tags "", # message 1, # strip_colors "signal_callback", # callback "") # callback_data def call_command(msgtype, bufname, prefix, message, date): if msgtype == 'privmsg': cmdstr = weechat.config_get_plugin('privmsg_command') elif msgtype == 'highlight': cmdstr = weechat.config_get_plugin('highlight_command') replace = { '%BUFFER%': bufname, '%PREFIX%': prefix, '%MESSAGE%': message, '%DATE%': date } for (old, new) in replace.items(): cmdstr = cmdstr.replace(old, new) cmd = shlex.split(cmdstr) with open('/dev/null', 'w') as FNULL: subprocess.call(cmd, stdout=FNULL, stderr=FNULL) def signal_callback(data, bufferp, date, tags, displayed, highlight, prefix, message): bufname = (weechat.buffer_get_string(bufferp, "short_name") or weechat.buffer_get_string(bufferp, "name")) if (weechat.buffer_get_string(bufferp, "localvar_type") == "private" and bufname == prefix): # Query msgtype='privmsg' elif highlight == "1": # Highlight msgtype='highlight' else: # Normal message return(weechat.WEECHAT_RC_OK) call_command(msgtype, bufname, prefix, message, date) return(weechat.WEECHAT_RC_OK)