changeset 13845:d0ff520f87da

[gaim-migrate @ 16301] This is tcl /cmd support. It doesn't currently let tcl plugins return error message (I couldn't ever get that to work). But other than that it works. Ethan please look over this when you get a chance. committer: Tailor Script <tailor@pidgin.im>
author Etan Reisner <pidgin@unreliablesource.net>
date Wed, 21 Jun 2006 04:57:27 +0000
parents 597beb5c9de1
children 5af4422eb3f8
files plugins/tcl/Makefile.am plugins/tcl/tcl.c plugins/tcl/tcl_cmd.c plugins/tcl/tcl_cmds.c plugins/tcl/tcl_gaim.h plugins/tcl/tcl_signals.c
diffstat 6 files changed, 285 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/tcl/Makefile.am	Wed Jun 21 04:10:47 2006 +0000
+++ b/plugins/tcl/Makefile.am	Wed Jun 21 04:57:27 2006 +0000
@@ -5,7 +5,7 @@
 plugin_LTLIBRARIES = tcl.la
 
 tcl_la_SOURCES = tcl.c tcl_glib.c tcl_glib.h tcl_cmds.c tcl_signals.c tcl_gaim.h \
-                 tcl_ref.c
+                 tcl_ref.c tcl_cmd.c
 
 EXTRA_DIST = signal-test.tcl Makefile.mingw
 
--- a/plugins/tcl/tcl.c	Wed Jun 21 04:10:47 2006 +0000
+++ b/plugins/tcl/tcl.c	Wed Jun 21 04:57:27 2006 +0000
@@ -126,6 +126,7 @@
 
 	Tcl_CreateObjCommand(interp, "::gaim::account", tcl_cmd_account, (ClientData)NULL, NULL);
 	Tcl_CreateObjCommand(interp, "::gaim::buddy", tcl_cmd_buddy, (ClientData)NULL, NULL);
+	Tcl_CreateObjCommand(interp, "::gaim::cmd", tcl_cmd_cmd, (ClientData)NULL, NULL);
 	Tcl_CreateObjCommand(interp, "::gaim::connection", tcl_cmd_connection, (ClientData)NULL, NULL);
 	Tcl_CreateObjCommand(interp, "::gaim::conversation", tcl_cmd_conversation, (ClientData)NULL, NULL);
 	Tcl_CreateObjCommand(interp, "::gaim::core", tcl_cmd_core, (ClientData)NULL, NULL);
@@ -284,7 +285,9 @@
 
 	if (Tcl_EvalFile(interp, plugin->path) != TCL_OK) {
 		result = Tcl_GetObjResult(interp);
-		gaim_debug(GAIM_DEBUG_ERROR, "tcl", "Error evaluating %s: %s\n", plugin->path, Tcl_GetString(result));
+		gaim_debug(GAIM_DEBUG_ERROR, "tcl",
+		           "Error evaluating %s: %s\n", plugin->path,
+		           Tcl_GetString(result));
 		Tcl_DeleteInterp(interp);
 		return FALSE;
 	}
@@ -313,6 +316,7 @@
 	if (data != NULL) {
 		g_hash_table_remove(tcl_plugins, (gpointer)(data->interp));
 		gaim_signals_disconnect_by_handle(data->interp);
+		tcl_cmd_cleanup(data->interp);
 		tcl_signal_cleanup(data->interp);
 		Tcl_Release((ClientData)data->interp);
 		Tcl_DeleteInterp(data->interp);
@@ -341,6 +345,7 @@
 	if(!tcl_loaded)
 		return FALSE;
 	tcl_glib_init();
+	tcl_cmd_init();
 	tcl_signal_init();
 	gaim_tcl_ref_init();
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/tcl/tcl_cmd.c	Wed Jun 21 04:57:27 2006 +0000
@@ -0,0 +1,189 @@
+/**
+ * @file tcl_cmd.c Gaim Tcl cmd API
+ *
+ * gaim
+ *
+ * Copyright (C) 2006 Etan Reisner <deryni@gmail.com>
+ *
+ * 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 2 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
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include <tcl.h>
+
+#include "tcl_gaim.h"
+
+#include "internal.h"
+#include "cmds.h"
+#include "debug.h"
+
+static GList *tcl_cmd_callbacks;
+
+static GaimCmdRet tcl_cmd_callback(GaimConversation *conv, const gchar *cmd,
+                                   gchar **args, gchar **errors,
+                                   struct tcl_cmd_handler *handler);
+static Tcl_Obj *new_cmd_cb_namespace(void);
+
+void tcl_cmd_init()
+{
+    tcl_cmd_callbacks = NULL;
+}
+
+void tcl_cmd_handler_free(struct tcl_cmd_handler *handler)
+{
+	if (handler == NULL)
+		return;
+
+	Tcl_DecrRefCount(handler->namespace);
+	g_free(handler);
+}
+
+void tcl_cmd_cleanup(Tcl_Interp *interp)
+{
+	GList *cur;
+	struct tcl_cmd_handler *handler;
+
+	for (cur = tcl_cmd_callbacks; cur != NULL; cur = g_list_next(cur)) {
+		handler = cur->data;
+		if (handler->interp == interp) {
+			gaim_cmd_unregister(handler->id);
+			tcl_cmd_handler_free(handler);
+			cur->data = NULL;
+		}
+	}
+	tcl_cmd_callbacks = g_list_remove_all(tcl_cmd_callbacks, NULL);
+}
+
+GaimCmdId tcl_cmd_register(struct tcl_cmd_handler *handler)
+{
+	int id;
+	GString *proc;
+
+	if ((id = gaim_cmd_register(Tcl_GetString(handler->cmd),
+				    handler->args, handler->priority,
+				    handler->flags, handler->prpl_id,
+				    GAIM_CMD_FUNC(tcl_cmd_callback),
+				    handler->helpstr, (void *)handler)) == 0)
+		return 0;
+
+	handler->namespace = new_cmd_cb_namespace ();
+	Tcl_IncrRefCount(handler->namespace);
+	proc = g_string_new("");
+	g_string_append_printf(proc, "namespace eval %s { proc cb { conv cmd arglist } { %s } }",
+	                       Tcl_GetString(handler->namespace),
+	                       Tcl_GetString(handler->proc));
+	if (Tcl_Eval(handler->interp, proc->str) != TCL_OK) {
+		Tcl_DecrRefCount(handler->namespace);
+		g_string_free(proc, TRUE);
+		return 0;
+	}
+	g_string_free(proc, TRUE);
+
+	tcl_cmd_callbacks = g_list_append(tcl_cmd_callbacks, (gpointer)handler);
+
+	return id;
+}
+
+void tcl_cmd_unregister(GaimCmdId id, Tcl_Interp *interp)
+{
+	GList *cur;
+	GString *cmd;
+	gboolean found = FALSE;
+	struct tcl_cmd_handler *handler;
+
+	for (cur = tcl_cmd_callbacks; cur != NULL; cur = g_list_next(cur)) {
+		handler = cur->data;
+		if (handler->interp == interp && handler->id == id) {
+			gaim_cmd_unregister(id);
+			cmd = g_string_sized_new(64);
+			g_string_printf(cmd, "namespace delete %s",
+			                Tcl_GetString(handler->namespace));
+			Tcl_EvalEx(interp, cmd->str, -1, TCL_EVAL_GLOBAL);
+			tcl_cmd_handler_free(handler);
+			g_string_free(cmd, TRUE);
+			cur->data = NULL;
+			found = TRUE;
+			break;
+		}
+	}
+
+	if (found)
+		tcl_cmd_callbacks = g_list_remove_all(tcl_cmd_callbacks, NULL);
+}
+
+static GaimCmdRet tcl_cmd_callback(GaimConversation *conv, const gchar *cmd,
+                                   gchar **args, gchar **errors,
+                                   struct tcl_cmd_handler *handler)
+{
+	int retval, error, i;
+	Tcl_Obj *command, *arg, *tclargs, *result;
+
+	command = Tcl_NewListObj(0, NULL);
+	Tcl_IncrRefCount(command);
+
+	/* The callback */
+	arg = Tcl_DuplicateObj(handler->namespace);
+	Tcl_AppendStringsToObj(arg, "::cb", NULL);
+	Tcl_ListObjAppendElement(handler->interp, command, arg);
+
+	/* The conversation */
+	arg = Tcl_NewIntObj((int)conv);
+	Tcl_ListObjAppendElement(handler->interp, command, arg);
+
+	/* The command */
+	arg = Tcl_NewStringObj(cmd, -1);
+	Tcl_ListObjAppendElement(handler->interp, command, arg);
+
+	/* The args list */
+	tclargs = Tcl_NewListObj(0, NULL);
+	for (i = 0; i < handler->nargs; i++) {
+		arg = Tcl_NewStringObj(args[i], -1);
+
+		Tcl_ListObjAppendElement(handler->interp, tclargs, arg);
+	}
+	Tcl_ListObjAppendElement(handler->interp, command, tclargs);
+
+	if ((error = Tcl_EvalObjEx(handler->interp, command,
+	                           TCL_EVAL_GLOBAL)) != TCL_OK) {
+		gchar *errorstr;
+
+		errorstr = g_strdup_printf("error evaluating callback: %s\n",
+		                           Tcl_GetString(Tcl_GetObjResult(handler->interp)));
+		gaim_debug(GAIM_DEBUG_ERROR, "tcl", errorstr);
+		*errors = errorstr;
+		retval = GAIM_CMD_RET_FAILED;
+	} else {
+		result = Tcl_GetObjResult(handler->interp);
+		if ((error = Tcl_GetIntFromObj(handler->interp, result,
+		                               &retval)) != TCL_OK) {
+			gchar *errorstr;
+
+			errorstr = g_strdup_printf("Error retreiving procedure result: %s\n",
+			                           Tcl_GetString(Tcl_GetObjResult(handler->interp)));
+			gaim_debug(GAIM_DEBUG_ERROR, "tcl", errorstr);
+			*errors = errorstr;
+			retval = GAIM_CMD_RET_FAILED;
+		}
+	}
+
+	return retval;
+}
+
+static Tcl_Obj *new_cmd_cb_namespace()
+{
+	char name[32];
+	static int cbnum;
+
+	g_snprintf(name, sizeof(name), "::gaim::_callback::cb_%d", cbnum++);
+	return Tcl_NewStringObj(name, -1);
+}
--- a/plugins/tcl/tcl_cmds.c	Wed Jun 21 04:10:47 2006 +0000
+++ b/plugins/tcl/tcl_cmds.c	Wed Jun 21 04:57:27 2006 +0000
@@ -541,6 +541,70 @@
 	return TCL_OK;
 }
 
+int tcl_cmd_cmd(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
+{
+	const char *cmds[] = { "register", "unregister", NULL };
+	enum { CMD_CMD_REGISTER, CMD_CMD_UNREGISTER } cmd;
+	struct tcl_cmd_handler *handler;
+	Tcl_Obj *result = Tcl_GetObjResult(interp);
+	GaimCmdId id;
+	int error;
+
+	if (objc < 2) {
+		Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?args?");
+		return TCL_ERROR;
+	}
+
+	if ((error = Tcl_GetIndexFromObj(interp, objv[1], cmds, "subcommand", 0, (int *)&cmd)) != TCL_OK)
+		return error;
+
+	switch (cmd) {
+	case CMD_CMD_REGISTER:
+		if (objc != 9) {
+			Tcl_WrongNumArgs(interp, 2, objv, "cmd arglist priority flags prpl_id proc helpstr");
+			return TCL_ERROR;
+		}
+		handler = g_new0(struct tcl_cmd_handler, 1);
+		handler->cmd = objv[2];
+		handler->args = Tcl_GetString(objv[3]);
+		handler->nargs = strlen(handler->args);
+		if ((error = Tcl_GetIntFromObj(interp, objv[4],
+		                               &handler->priority)) != TCL_OK) {
+			g_free(handler);
+			return error;
+		}
+		if ((error = Tcl_GetIntFromObj(interp, objv[5],
+		                               &handler->flags)) != TCL_OK) {
+			g_free(handler);
+			return error;
+		}
+		handler->prpl_id = Tcl_GetString(objv[6]);
+		handler->proc = objv[7];
+		handler->helpstr = Tcl_GetString(objv[8]);
+		handler->interp = interp;
+		if ((id = tcl_cmd_register(handler)) == 0) {
+			tcl_cmd_handler_free(handler);
+			Tcl_SetIntObj(result, 0);
+		} else {
+			handler->id = id;
+			Tcl_SetIntObj(result, id);
+		}
+		break;
+	case CMD_CMD_UNREGISTER:
+		if (objc != 3) {
+			Tcl_WrongNumArgs(interp, 2, objv, "id");
+			return TCL_ERROR;
+		}
+		if ((error = Tcl_GetIntFromObj(interp, objv[2],
+		                               (int *)&id)) != TCL_OK)
+			return error;
+		tcl_cmd_unregister(id, interp);
+		break;
+	}
+
+	return TCL_OK;
+}
+
 int tcl_cmd_connection(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
 {
 	Tcl_Obj *result = Tcl_GetObjResult(interp), *list, *elem;
--- a/plugins/tcl/tcl_gaim.h	Wed Jun 21 04:10:47 2006 +0000
+++ b/plugins/tcl/tcl_gaim.h	Wed Jun 21 04:57:27 2006 +0000
@@ -26,6 +26,7 @@
 #include <tcl.h>
 
 #include "internal.h"
+#include "cmds.h"
 #include "plugin.h"
 #include "value.h"
 #include "stringref.h"
@@ -45,6 +46,23 @@
 	GaimValue **argtypes;
 };
 
+struct tcl_cmd_handler {
+	int id;
+	Tcl_Obj *cmd;
+	Tcl_Interp *interp;
+
+	Tcl_Obj *namespace;
+	/* These are temporary during setup */
+	const char *args;
+	int priority;
+	int flags;
+	const char *prpl_id;
+	Tcl_Obj *proc;
+	const char *helpstr;
+
+	int nargs;
+};
+
 extern GaimPlugin *_tcl_plugin;
 
 /* Capitalized this way because these are "types" */
@@ -65,6 +83,12 @@
 gboolean tcl_signal_connect(struct tcl_signal_handler *handler);
 void tcl_signal_disconnect(void *instance, const char *signal, Tcl_Interp *interp);
 
+void tcl_cmd_init(void);
+void tcl_cmd_handler_free(struct tcl_cmd_handler *handler);
+void tcl_cmd_cleanup(Tcl_Interp *interp);
+GaimCmdId tcl_cmd_register(struct tcl_cmd_handler *handler);
+void tcl_cmd_unregister(GaimCmdId id, Tcl_Interp *interp);
+
 void gaim_tcl_ref_init(void);
 void *gaim_tcl_ref_get(Tcl_Interp *interp, Tcl_Obj *obj, GaimStringref *type);
 Tcl_Obj *gaim_tcl_ref_new(GaimStringref *type, void *value);
@@ -72,6 +96,7 @@
 Tcl_ObjCmdProc tcl_cmd_account;
 Tcl_ObjCmdProc tcl_cmd_signal_connect;
 Tcl_ObjCmdProc tcl_cmd_buddy;
+Tcl_ObjCmdProc tcl_cmd_cmd;
 Tcl_ObjCmdProc tcl_cmd_connection;
 Tcl_ObjCmdProc tcl_cmd_conversation;
 Tcl_ObjCmdProc tcl_cmd_core;
--- a/plugins/tcl/tcl_signals.c	Wed Jun 21 04:10:47 2006 +0000
+++ b/plugins/tcl/tcl_signals.c	Wed Jun 21 04:57:27 2006 +0000
@@ -387,7 +387,6 @@
 	g_string_free(val, TRUE);
 	g_free(vals);
 	g_free(strs);
-	
 
 	return retval;
 }