#!/usr/bin/env python
# -*- coding: utf-8 -*- 
"""
Executes a command when a message is received
"""

import prof
from subprocess import Popen
import time
from sys import platform

def secure(string):
    string=string.replace("'","\'")
    string=string.replace('\\','\\\\')
    return string

def notifycmd(sender,message):
    command = prof.settings_string_get("notifycmd", "command", "")
    # replace markers with complex strings first to avoid "%%mittens"=>"(%%)mittens"=>"(%m)ittens"=>"<message>ittens" but rather do "(%%)mittens"=>"%mittens"
    command = command.replace("%%","{percentreplace}")
    command = command.replace("%s","${senderreplace}")
    command = command.replace("%m","${messagereplace}")


    command = command.replace("{percentreplace}","%")
    command = "senderreplace='{}';messagereplace='{}';{}".format(secure(sender),secure(message),command)

    print(command)
    p = Popen(['sh', '-c', command])

def prof_post_chat_message_display(barejid, resource, message):
    enabled = prof.settings_string_get("notifycmd", "enabled", "on")
    current_recipient = prof.get_current_recipient()
    if enabled == "on" or (enabled == "active" and current_recipient == barejid):
        notifycmd(barejid, message)
    return message


def prof_post_room_message_display(barejid, nick, message):
    enabled = prof.settings_string_get("notifycmd", "enabled", "on")
    rooms = prof.settings_string_get("notifycmd", "rooms", "off")
    current_muc = prof.get_current_muc()
    if rooms == "on":
        if enabled == "on":
            notifycmd(nick + " in " + barejid, message)
        elif enabled == "active" and current_muc == barejid:
            notifycmd(nick, message)

    return message


def prof_post_priv_message_display(barejid, nick, message):
    if enabled:
        notifycmd(nick,message)

    return message


def _cmd_notifycmd(arg1=None, arg2=None):
    if arg1 == "on":
        prof.settings_string_set("notifycmd", "enabled", "on")
        prof.cons_show("Notifycmd plugin enabled")
    elif arg1 == "off":
        prof.settings_string_set("notifycmd", "enabled", "off")
        prof.cons_show("Notifycmd plugin disabled")
    elif arg1 == "active":
        prof.settings_string_set("notifycmd", "enabled", "active")
        prof.cons_show("Notifycmd plugin enabled for active window only")
    elif arg1 == "command":
        if arg2 == None:
            prof.cons_bad_cmd_usage("/notifycmd")
        else:
            prof.settings_string_set("notifycmd", "command", arg2)
            prof.cons_show("notifycmd plugin command set to: " + arg2)
    elif arg1 == "rooms":
        if arg2 == None:
            prof.cons_bad_cmd_usage("/notifycmd")
        else:
            prof.settings_string_set("notifycmd", "rooms", arg2)
            prof.cons_show("notifycmd plugin notifications for rooms set to: " + arg2)
    else:
        enabled = prof.settings_string_get("notifycmd", "enabled", "on")
        rooms = prof.settings_string_get("notifycmd", "rooms", "off")
        command = prof.settings_string_get("notifycmd", "command", "")
        prof.cons_show("Notifycmd plugin settings:")
        prof.cons_show("enabled : " + enabled)
        prof.cons_show("command : " + command)
        prof.cons_show("rooms : " + rooms)

def prof_init(version, status, account_name, fulljid):
    synopsis = [
        "/notifycmd on|off|active",
        "/notifycmd command <command>",
        "/notifycmd rooms on|off"
    ]
    description = "Executes a command when a message is received"
    args = [
        [ "on|off",      "Enable/disable notifycmd for all windows" ],
        [ "active",      "Enable notifycmd for active window only" ],
        [ "command <args>",    "Set command to execute. Replaces %s with sender, %m with message and %% with literal %." ],
        [ "rooms <args>",    "Turn notifycmd for rooms on or off" ]
    ]
    examples = []

    prof.register_command("/notifycmd", 0, 2, synopsis, description, args, examples, _cmd_notifycmd)
    prof.completer_add("/notifycmd", [ "on", "off","active","command","rooms" ])
    prof.completer_add("/notifycmd rooms", [ "on", "off" ])

@@ -224,16 +224,19 @@
 ace(
-%22'%22,%22
+'%5C%5C','%5C%5C%5C
 %5C'
-%22
 )%0A  
@@ -255,35 +255,107 @@
 ing.replace(
-'%5C%5C','%5C%5C%5C%5C'
+%22%5C%22%22,%22%5C%5C%5C%22%22)%0A    string=string.replace(%22$%22,%22%5C$%22)%0A    string=string.replace(%22%60%22,%22%5C%60%22
 )%0A    return
@@ -870,20 +870,22 @@
 replace=
-'%7B%7D'
+%5C%22%7B%7D%5C%22
 ;message
@@ -896,12 +896,14 @@
 ace=
-'%7B%7D'
+%5C%22%7B%7D%5C%22
 ;%7B%7D%22

Remove debug output. Fix code execution