1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Added browser plugin, allow up to 5 args for command callbacks

This commit is contained in:
James Booth 2013-08-08 01:40:03 +01:00
parent ab45239033
commit 9f76a5e610
9 changed files with 78 additions and 21 deletions

View File

@ -1,7 +1,8 @@
import prof
user = "jabber.org"
user = "prof1@panesar"
# hooks
def prof_on_start():
global user
prof.cons_show("")

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

21
plugins/www-view.py Normal file
View File

@ -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)

View File

@ -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);

View File

@ -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;
}