diff --git a/plugins/connect.py b/plugins/connect.py index ced28d44..e8368712 100644 --- a/plugins/connect.py +++ b/plugins/connect.py @@ -1,7 +1,8 @@ import prof -user = "jabber.org" +user = "prof1@panesar" +# hooks def prof_on_start(): global user prof.cons_show("") diff --git a/plugins/cricket-score.py b/plugins/cricket-score.py index 54047a89..6a0c41d0 100644 --- a/plugins/cricket-score.py +++ b/plugins/cricket-score.py @@ -9,7 +9,6 @@ score_url = None summary = None # hooks - def prof_init(version, status): if score_url: prof.register_timed(get_scores, 60) @@ -18,6 +17,7 @@ def prof_on_start(): if score_url: get_scores() +# local functions def get_scores(): global score_url global summary diff --git a/plugins/goodbyeworld.py b/plugins/goodbyeworld.py index b4609897..68a6d2cb 100644 --- a/plugins/goodbyeworld.py +++ b/plugins/goodbyeworld.py @@ -4,18 +4,16 @@ package_version = None package_status = None # hooks - def prof_init(version, status): global package_version global package_status package_version = version package_status = status -def prof_on_start(): +def prof_on_connect(): goodbyeworld() # local functions - def goodbyeworld(): global package_version global package_status diff --git a/plugins/helloworld.py b/plugins/helloworld.py index db039708..0cc1e724 100644 --- a/plugins/helloworld.py +++ b/plugins/helloworld.py @@ -4,7 +4,6 @@ package_version = None package_status = None # hooks - def prof_init(version, status): global package_version global package_status @@ -14,11 +13,7 @@ def prof_init(version, status): def prof_on_start(): helloworld() -def prof_on_connect(): - prof.cons_show("Connected hello world plugin") - # local functions - def helloworld(): global package_version global package_status diff --git a/plugins/platform-info.py b/plugins/platform-info.py index 94550f19..aac169d8 100644 --- a/plugins/platform-info.py +++ b/plugins/platform-info.py @@ -2,12 +2,10 @@ import prof import platform # hooks - def prof_init(version, status): prof.register_command("/platform", 0, 0, "/platform", "Output system information.", "Output system information", cmd_platform) # commands - def cmd_platform(): result_summary = platform.platform() prof.cons_show(result_summary) diff --git a/plugins/whoami.py b/plugins/whoami.py index d48d1c40..d6342970 100644 --- a/plugins/whoami.py +++ b/plugins/whoami.py @@ -2,12 +2,10 @@ import prof import getpass # hooks - def prof_init(version, status): prof.register_command("/whoami", 0, 0, "/whoami", "Call shell whoami command.", "Call shell whoami command.", cmd_whoami) -# local functions - +# commands def cmd_whoami(): me = getpass.getuser() prof.cons_show(me) diff --git a/plugins/www-view.py b/plugins/www-view.py new file mode 100644 index 00000000..3434d88a --- /dev/null +++ b/plugins/www-view.py @@ -0,0 +1,21 @@ +import prof +import os +import webbrowser + + +# hooks +def prof_init(version, status): + prof.register_command("/browser", 1, 1, "/browser url", "View a URL in the browser.", "View a URL in the browser", cmd_browser) + +# commands +def cmd_browser(url): + savout = os.dup(1) + saverr = os.dup(2) + os.close(1) + os.close(2) + os.open(os.devnull, os.O_RDWR) + try: + webbrowser.open(url, new=2) + finally: + os.dup2(savout, 1) + os.dup2(saverr, 2) diff --git a/src/command/command.c b/src/command/command.c index 6f647716..5448182c 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -1107,7 +1107,7 @@ cmd_execute(const char * const command, const char * const inp) g_strfreev(args); return result; } - } else if (plugins_command_run(command)) { + } else if (plugins_command_run(inp)) { return TRUE; } else { return cmd_execute_default(inp); diff --git a/src/plugins/callbacks.c b/src/plugins/callbacks.c index e228c498..388892af 100644 --- a/src/plugins/callbacks.c +++ b/src/plugins/callbacks.c @@ -24,6 +24,7 @@ #include "plugins/callbacks.h" #include "plugins/plugins.h" #include "tools/autocomplete.h" +#include "tools/parser.h" #include "ui/ui.h" @@ -44,18 +45,63 @@ callbacks_add_timed(PluginTimedFunction *timed_function) } gboolean -plugins_command_run(const char * const cmd) +plugins_command_run(const char * const input) { - GSList *p_command = p_commands; + gchar **split = g_strsplit(input, " ", -1); + GSList *p_command = p_commands; while (p_command != NULL) { PluginCommand *command = p_command->data; - if (strcmp(command->command_name, cmd) == 0) { - PyObject_CallObject(command->p_callback, NULL); - return TRUE; + if (g_strcmp0(split[0], command->command_name) == 0) { + gchar **args = parse_args(input, command->min_args, command->max_args); + if (args == NULL) { + cons_show(""); + cons_show("Usage: %s", command->usage); + if (ui_current_win_type() == WIN_CHAT) { + char usage[strlen(command->usage) + 8]; + sprintf(usage, "Usage: %s", command->usage); + ui_current_print_line(usage); + } + return TRUE; + } else { + int num_args = g_strv_length(args); + PyObject *p_args = NULL; + if (num_args == 0) { + PyObject_CallObject(command->p_callback, p_args); + } else if (num_args == 1) { + p_args = Py_BuildValue("(s)", args[0]); + PyObject_CallObject(command->p_callback, p_args); + Py_XDECREF(p_args); + } else if (num_args == 2) { + p_args = Py_BuildValue("ss", args[0], args[1]); + PyObject_CallObject(command->p_callback, p_args); + Py_XDECREF(p_args); + } else if (num_args == 3) { + p_args = Py_BuildValue("sss", args[0], args[1], args[2]); + PyObject_CallObject(command->p_callback, p_args); + Py_XDECREF(p_args); + } else if (num_args == 4) { + p_args = Py_BuildValue("ssss", args[0], args[1], args[2], args[3]); + PyObject_CallObject(command->p_callback, p_args); + Py_XDECREF(p_args); + } else if (num_args == 5) { + p_args = Py_BuildValue("sssss", args[0], args[1], args[2], args[3], args[4]); + PyObject_CallObject(command->p_callback, p_args); + Py_XDECREF(p_args); + } + + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + } + g_strfreev(split); + return TRUE; + } + g_strfreev(args); } p_command = g_slist_next(p_command); } + g_strfreev(split); return FALSE; }