Files
cproof-plugins/tests/python-test.py
2016-03-26 01:04:46 +00:00

357 lines
13 KiB
Python

import prof
import threading
import time
plugin_win = "Python Test"
count = 0
ping_id = 1
def _inc_counter():
global count
while True:
time.sleep(5)
count = count + 1
def _handle_win_input(win, line):
prof.win_show(win, "Input received: " + line)
def _create_win():
if prof.win_exists(plugin_win) == False:
prof.win_create(plugin_win, _handle_win_input)
def _consalert():
_create_win()
prof.win_focus(plugin_win)
prof.cons_alert()
prof.win_show(plugin_win, "called -> prof.cons_alert")
def _consshow(msg):
if not msg:
prof.cons_bad_cmd_usage("/python-test")
return
_create_win()
prof.win_focus(plugin_win)
prof.cons_show(msg)
prof.win_show(plugin_win, "called -> prof.cons_show: " + msg)
def _consshow_t(group, key, dflt, msg):
if not group or not key or not dflt or not msg:
prof.cons_bad_cmd_usage("/python-test")
return
_create_win()
prof.win_focus(plugin_win)
groupval = None if group == "none" else group
keyval = None if key == "none" else key
dfltval = None if dflt == "none" else dflt
prof.cons_show_themed(groupval, keyval, dfltval, msg)
prof.win_show(plugin_win, "called -> prof.cons_show_themed: " + group + ", " + key + ", " + dflt + ", " + msg)
def _constest():
res = prof.current_win_is_console()
_create_win()
prof.win_focus(plugin_win)
if res:
prof.win_show(plugin_win, "called -> prof.current_win_is_console: true")
else:
prof.win_show(plugin_win, "called -> prof.current_win_is_console: false")
def _winshow(msg):
if not msg:
prof.cons_bad_cmd_usage("/python-test")
return
_create_win()
prof.win_focus(plugin_win)
prof.win_show(plugin_win, msg)
prof.win_show(plugin_win, "called -> prof.win_show: " + msg)
def _winshow_t(group, key, dflt, msg):
if not group or not key or not dflt or not msg:
prof.cons_bad_cmd_usage("/python-test")
return
_create_win()
prof.win_focus(plugin_win)
groupval = None if group == "none" else group
keyval = None if key == "none" else key
dfltval = None if dflt == "none" else dflt
prof.win_show_themed(plugin_win, groupval, keyval, dfltval, msg)
prof.win_show(plugin_win, "called -> prof_win_show_themed: " + group + ", " + key + ", " + dflt + ", " + msg)
def _sendline(line):
if not line:
prof.cons_bad_cmd_usage("/python-test")
return
_create_win()
prof.win_focus(plugin_win)
prof.send_line(line)
prof.win_show(plugin_win, "called -> prof.send_line: " + line)
def _notify(msg):
if not msg:
prof.cons_bad_cmd_usage("/python-test")
return
_create_win()
prof.win_focus(plugin_win)
prof.notify(msg, 5000, "python-test plugin")
prof.win_show(plugin_win, "called -> prof.notify: " + msg)
def _get(subject):
if subject == "recipient":
_create_win()
recipient = prof.get_current_recipient();
if recipient:
prof.win_focus(plugin_win)
prof.win_show(plugin_win, "called -> prof.get_current_recipient: " + recipient)
else:
prof.win_focus(plugin_win)
prof.win_show(plugin_win, "called -> prof_get_current_recipient: <none>")
elif subject == "room":
_create_win()
room = prof.get_current_muc()
if room:
prof.win_focus(plugin_win)
prof.win_show(plugin_win, "called -> prof_get_current_muc: " + room)
else:
prof.win_focus(plugin_win)
prof.win_show(plugin_win, "called -> prof_get_current_muc: <none>")
else:
prof.cons_bad_cmd_usage("/python-test")
def _log(level, msg):
if not level or not msg:
prof.cons_bad_cmd_usage("/python-test")
return
if level == "debug":
_create_win()
prof.win_focus(plugin_win)
prof.log_debug(msg)
prof.win_show(plugin_win, "called -> prof.log_debug: " + msg)
elif level == "info":
_create_win()
prof.win_focus(plugin_win)
prof.log_info(msg)
prof.win_show(plugin_win, "called -> prof.log_info: " + msg)
elif level == "warning":
_create_win()
prof.win_focus(plugin_win)
prof.log_warning(msg)
prof.win_show(plugin_win, "called -> prof.log_warning: " + msg)
elif level == "error":
_create_win()
prof.win_focus(plugin_win)
prof.log_error(msg)
prof.win_show(plugin_win, "called -> prof.log_error: " + msg)
else:
prof.cons_bad_cmd_usage("/python-test")
def _count():
_create_win()
prof.win_focus(plugin_win)
prof.win_show(plugin_win, "Count: " + str(count))
def _ping(jid):
global ping_id
if not jid:
prof.cons_bad_cmd_usage("/python-test")
return
_create_win()
prof.win_focus(plugin_win)
res = prof.send_stanza("<iq to='" + jid + "' id='pythonplugin-" + str(ping_id) + "' type='get'><ping xmlns='urn:xmpp:ping'/></iq>")
ping_id = ping_id + 1
if res:
prof.win_show(plugin_win, "Ping sent successfully")
else:
prof.win_show(plugin_win, "Error sending ping")
def _boolean(op, group, key, value_str):
if op != "get" and op != "set":
prof.cons_bad_cmd_usage("/python-test")
return
if group == None or key == None:
prof.cons_bad_cmd_usage("/python-test")
return
if op == "set" and value_str != "true" and value_str != "false":
prof.cons_bad_cmd_usage("/python-test")
return
if op == "get":
dflt = False
_create_win()
prof.win_focus(plugin_win)
res = prof.settings_get_boolean(group, key, dflt)
if res:
prof.win_show(plugin_win, "Boolean setting: TRUE")
else:
prof.win_show(plugin_win, "Boolean setting: FALSE")
elif op == "set":
value = False
if value_str == "true":
value = True
_create_win()
prof.win_focus(plugin_win)
prof.settings_set_boolean(group, key, value)
prof.win_show(plugin_win, "Set [" + group + "] " + key + " to " + str(value))
def _cmd_pythontest(subcmd=None, arg1=None, arg2=None, arg3=None, arg4=None):
if subcmd == "consalert": _consalert()
elif subcmd == "consshow": _consshow(arg1)
elif subcmd == "consshow_t": _consshow_t(arg1, arg2, arg3, arg4)
elif subcmd == "constest": _constest()
elif subcmd == "winshow": _winshow(arg1)
elif subcmd == "winshow_t": _winshow_t(arg1, arg2, arg3, arg4)
elif subcmd == "sendline": _sendline(arg1)
elif subcmd == "notify": _notify(arg1)
elif subcmd == "get": _get(arg1)
elif subcmd == "log": _log(arg1, arg2)
elif subcmd == "count": _count()
elif subcmd == "ping": _ping(arg1)
elif subcmd == "boolean": _boolean(arg1, arg2, arg3, arg4)
else: prof.cons_bad_cmd_usage("/python-test")
def timed_callback():
_create_win()
prof.win_show(plugin_win, "timed -> timed_callback called")
def prof_init(version, status):
t = threading.Thread(target=_inc_counter)
t.daemon = True
t.start()
prof.win_create(plugin_win, _handle_win_input)
synopsis = [
"/python-test consalert",
"/python-test consshow <message>",
"/python-test consshow_t <group> <key> <default> <message>",
"/python-test constest",
"/python-test winshow <message>",
"/python-test winshow_t <group> <key> <default> <message>",
"/python-test notify <message>",
"/python-test sendline <line>",
"/python-test get recipient|room",
"/python-test log debug|info|warning|error <message>",
"/python-test count",
"/python-test ping <jid>",
"/python-test boolean get <group> <key>",
"/python-test boolean set <group> <key> <value>"
]
description = "Python test plugins. All commands focus the plugin window."
args = [
[ "consalert", "Highlight the console window in the status bar" ],
[ "consshow <message>", "Show the message in the console window" ],
[ "consshow_t <group> <key> <default> <message>", "Show the themed message in the console window. " ],
[ "constest", "Show whether the command was run in the console." ],
[ "winshow <message>", "Show the message in the plugin window" ],
[ "winshow_t <group> <key> <default> <message>", "Show the themed message in the plugin window. " ],
[ "notify <message>", "Send a desktop notification with message" ],
[ "sendline <line>", "Pass line to profanity to process" ],
[ "get recipient", "Show the current chat recipient, if in a chat window" ],
[ "get room", "Show the current room JID, if ina a chat room"],
[ "log debug|info|warning|error <message>", "Log a message at the specified level" ],
[ "count", "Show the counter, incremented every 5 seconds by a worker thread" ],
[ "ping <jid>", "Send an XMPP ping to the specified Jabber ID" ],
[ "boolean get <group> <key>", "Get a boolean setting" ],
[ "boolean set <group> <key> <value>", "Set a boolean setting" ]
]
examples = [
"/python-test sendline /about",
"/python-test log debug \"Test debug message\"",
"/python-test consshow_t c-test cons.show none \"This is themed\"",
"/python-test consshow_t none none bold_cyan \"This is bold_cyan\"",
"/python-test ping buddy@server.org"
]
prof.register_command("/python-test", 1, 5, synopsis, description, args, examples, _cmd_pythontest)
prof.register_ac("/python-test",
[ "consalert", "consshow", "consshow_t", "constest", "winshow", "winshow_t", "notify", "sendline", "get", "log", "count", "ping", "boolean" ]
)
prof.register_ac("/python-test get",
[ "recipient", "room" ]
)
prof.register_ac("/python-test log",
[ "debug", "info", "warning", "error" ]
)
prof.register_ac("/python-test boolean",
[ "get", "set" ]
)
prof.register_timed(timed_callback, 30)
def prof_on_start():
_create_win()
prof.win_show(plugin_win, "fired -> prof_on_start")
def prof_on_shutdown():
_create_win()
prof.win_show(plugin_win, "fired -> prof_on_shutdown")
def prof_on_connect(account_name, fulljid):
_create_win()
prof.win_show(plugin_win, "fired -> prof_on_connect: " + account_name + ", " + fulljid)
def prof_on_disconnect(account_name, fulljid):
_create_win()
prof.win_show(plugin_win, "fired -> prof_on_disconnect: " + account_name + ", " + fulljid)
def prof_pre_chat_message_display(jid, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_pre_chat_message_display: " + jid + ", " + message)
def prof_post_chat_message_display(jid, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_post_chat_message_display: " + jid + ", " + message)
def prof_pre_chat_message_send(jid, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_pre_chat_message_send: " + jid + ", " + message)
def prof_post_chat_message_send(jid, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_post_chat_message_send: " + jid + ", " + message)
def prof_pre_room_message_display(room, nick, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_pre_room_message_display: " + room + ", " + nick + ", " + message)
def prof_post_room_message_display(room, nick, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_post_room_message_display: " + room + ", " + nick + ", " + message)
def prof_pre_room_message_send(room, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_pre_room_message_send: " + room + ", " + message)
def prof_post_room_message_send(room, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_post_room_message_send: " + room + ", " + message)
def prof_pre_priv_message_display(room, nick, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_pre_priv_message_display: " + room + ", " + nick + ", " + message)
def prof_post_priv_message_display(room, nick, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_post_priv_message_display: " + room + ", " + nick + ", " + message)
def prof_pre_priv_message_send(room, nick, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_pre_priv_message_send: " + room + ", " + nick + ", " + message)
def prof_post_priv_message_send(room, nick, message):
_create_win()
prof.win_show(plugin_win, "fired -> prof_post_priv_message_send: " + room + ", " + nick + ", " + message)