Tue, 02 Sep 2003 03:34:37 +0000
[gaim-migrate @ 7220]
Sean probably won't think this is contact support. This is in fact a
Tcl script plugin loader. That's probably what he'll think it is.
| 6694 | 1 | /** |
| 2 | * @file tcl.c Gaim Tcl plugin bindings | |
| 3 | * | |
| 4 | * gaim | |
| 5 | * | |
| 6 | * Copyright (C) 2003 Ethan Blanton <eblanton@cs.purdue.edu> | |
| 7 | * | |
| 8 | * This program is free software; you can redistribute it and/or modify | |
| 9 | * it under the terms of the GNU General Public License as published by | |
| 10 | * the Free Software Foundation; either version 2 of the License, or | |
| 11 | * (at your option) any later version. | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 | * GNU General Public License for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU General Public License | |
| 19 | * along with this program; if not, write to the Free Software | |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 21 | */ | |
| 22 | ||
| 23 | #include "config.h" | |
| 24 | ||
| 25 | #include <tcl.h> | |
| 26 | ||
| 27 | #ifdef HAVE_TK | |
| 28 | #include <tk.h> | |
| 29 | #endif | |
| 30 | ||
| 31 | #include <stdio.h> | |
| 32 | #include <sys/types.h> | |
| 33 | #include <sys/stat.h> | |
| 34 | #include <unistd.h> | |
| 35 | #include <string.h> | |
| 36 | ||
| 37 | #include "tcl_glib.h" | |
| 38 | #include "tcl_gaim.h" | |
| 39 | ||
| 40 | #include "internal.h" | |
| 41 | #include "connection.h" | |
| 42 | #include "plugin.h" | |
| 43 | #include "signals.h" | |
| 44 | #include "debug.h" | |
| 45 | #include "util.h" | |
| 46 | ||
| 47 | struct tcl_plugin_data { | |
| 48 | GaimPlugin *plugin; | |
| 49 | Tcl_Interp *interp; | |
| 50 | }; | |
| 51 | ||
| 52 | static GHashTable *tcl_plugins = NULL; | |
| 53 | ||
| 54 | GaimPlugin *_tcl_plugin; | |
| 55 | ||
| 56 | GaimPlugin *tcl_interp_get_plugin(Tcl_Interp *interp) | |
| 57 | { | |
| 58 | struct tcl_plugin_data *data; | |
| 59 | ||
| 60 | if (tcl_plugins == NULL) | |
| 61 | return NULL; | |
| 62 | ||
| 63 | data = g_hash_table_lookup(tcl_plugins, (gpointer)interp); | |
| 64 | return data != NULL ? data->plugin : NULL; | |
| 65 | } | |
| 66 | ||
| 67 | static int tcl_init_interp(Tcl_Interp *interp) | |
| 68 | { | |
| 69 | char *rcfile; | |
| 70 | char init[] = | |
| 71 | "namespace eval ::gaim {\n" | |
| 72 | " namespace export account buddy connection conversation\n" | |
| 73 | " namespace export core debug notify prefs send_im\n" | |
| 74 | " namespace export signal unload\n" | |
| 75 | " namespace eval _callback { }\n" | |
| 76 | "\n" | |
| 77 | " proc conv_send { account who text } {\n" | |
| 78 | " set gc [gaim::account connection $account]\n" | |
| 79 | " set convo [gaim::conversation new $account $who]\n" | |
| 80 | " set myalias [gaim::account alias $account]\n" | |
| 81 | "\n" | |
| 82 | " if {![string length $myalias]} {\n" | |
| 83 | " set myalias [gaim::account username $account]\n" | |
| 84 | " }\n" | |
| 85 | "\n" | |
| 86 | " gaim::send_im $gc $who $text\n" | |
| 87 | " gaim::conversation write $convo send $myalias $text\n" | |
| 88 | " }\n" | |
| 89 | "}\n" | |
| 90 | "\n" | |
| 91 | "proc bgerror { message } {\n" | |
| 92 | " global errorInfo\n" | |
| 93 | " gaim::notify -error \"Tcl Error\" \"Tcl Error: $message\" \"$errorInfo\"\n" | |
| 94 | "}\n"; | |
| 95 | ||
| 96 | if (Tcl_EvalEx(interp, init, -1, TCL_EVAL_GLOBAL) != TCL_OK) { | |
| 97 | return 1; | |
| 98 | } | |
| 99 | ||
| 100 | Tcl_SetVar(interp, "argc", "0", TCL_GLOBAL_ONLY); | |
| 101 | Tcl_SetVar(interp, "argv0", "gaim", TCL_GLOBAL_ONLY); | |
| 102 | Tcl_SetVar(interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY); | |
| 103 | rcfile = g_strdup_printf("%s" G_DIR_SEPARATOR_S "tclrc", gaim_user_dir()); | |
| 104 | Tcl_SetVar(interp, "tcl_rcFileName", rcfile, TCL_GLOBAL_ONLY); | |
| 105 | g_free(rcfile); | |
| 106 | ||
| 107 | Tcl_SetVar(interp, "::gaim::version", VERSION, TCL_GLOBAL_ONLY); | |
| 108 | Tcl_SetVar(interp, "::gaim::user_dir", gaim_user_dir(), TCL_GLOBAL_ONLY); | |
| 109 | #ifdef HAVE_TK | |
| 110 | Tcl_SetVar(interp, "::gaim::tk_available", "1", TCL_GLOBAL_ONLY); | |
| 111 | #else | |
| 112 | Tcl_SetVar(interp, "::gaim::tk_available", "0", TCL_GLOBAL_ONLY); | |
| 113 | #endif /* HAVE_TK */ | |
| 114 | ||
| 115 | Tcl_CreateObjCommand(interp, "::gaim::account", tcl_cmd_account, (ClientData)NULL, NULL); | |
| 116 | Tcl_CreateObjCommand(interp, "::gaim::buddy", tcl_cmd_buddy, (ClientData)NULL, NULL); | |
| 117 | Tcl_CreateObjCommand(interp, "::gaim::connection", tcl_cmd_connection, (ClientData)NULL, NULL); | |
| 118 | Tcl_CreateObjCommand(interp, "::gaim::conversation", tcl_cmd_conversation, (ClientData)NULL, NULL); | |
| 119 | Tcl_CreateObjCommand(interp, "::gaim::core", tcl_cmd_core, (ClientData)NULL, NULL); | |
| 120 | Tcl_CreateObjCommand(interp, "::gaim::debug", tcl_cmd_debug, (ClientData)NULL, NULL); | |
| 121 | Tcl_CreateObjCommand(interp, "::gaim::notify", tcl_cmd_notify, (ClientData)NULL, NULL); | |
| 122 | Tcl_CreateObjCommand(interp, "::gaim::prefs", tcl_cmd_prefs, (ClientData)NULL, NULL); | |
| 123 | Tcl_CreateObjCommand(interp, "::gaim::send_im", tcl_cmd_send_im, (ClientData)NULL, NULL); | |
| 124 | Tcl_CreateObjCommand(interp, "::gaim::signal", tcl_cmd_signal, (ClientData)NULL, NULL); | |
| 125 | Tcl_CreateObjCommand(interp, "::gaim::unload", tcl_cmd_unload, (ClientData)NULL, NULL); | |
| 126 | ||
| 127 | return 0; | |
| 128 | } | |
| 129 | ||
| 130 | static Tcl_Interp *tcl_create_interp() | |
| 131 | { | |
| 132 | Tcl_Interp *interp; | |
| 133 | ||
| 134 | interp = Tcl_CreateInterp(); | |
| 135 | if (Tcl_Init(interp) == TCL_ERROR) { | |
| 136 | Tcl_DeleteInterp(interp); | |
| 137 | return NULL; | |
| 138 | } | |
| 139 | ||
| 140 | #ifdef HAVE_TK | |
| 141 | Tcl_StaticPackage(interp, "Tk", Tk_Init, Tk_SafeInit); | |
| 142 | #endif /* HAVE_TK */ | |
| 143 | ||
| 144 | if (tcl_init_interp(interp)) { | |
| 145 | Tcl_DeleteInterp(interp); | |
| 146 | return NULL; | |
| 147 | } | |
| 148 | Tcl_StaticPackage(interp, "gaim", tcl_init_interp, NULL); | |
| 149 | ||
| 150 | return interp; | |
| 151 | } | |
| 152 | ||
| 153 | static gboolean tcl_probe_plugin(GaimPlugin *plugin) | |
| 154 | { | |
| 155 | GaimPluginInfo *info; | |
| 156 | Tcl_Interp *interp; | |
| 157 | Tcl_Parse parse; | |
| 158 | Tcl_Obj *result, **listitems; | |
| 159 | struct stat st; | |
| 160 | FILE *fp; | |
| 161 | char *buf, *cur; | |
| 162 | int len, found = 0, err = 0, nelems; | |
| 163 | gboolean status = FALSE; | |
| 164 | ||
| 165 | if ((fp = fopen(plugin->path, "r")) == NULL) | |
| 166 | return FALSE; | |
| 167 | if (fstat(fileno(fp), &st)) { | |
| 168 | fclose(fp); | |
| 169 | return FALSE; | |
| 170 | } | |
| 171 | len = st.st_size; | |
| 172 | ||
| 173 | buf = g_malloc(len + 1); | |
| 174 | if ((fread(buf, len, 1, fp)) != 1) { | |
| 175 | g_free(buf); | |
| 176 | fclose(fp); | |
| 177 | return FALSE; | |
| 178 | } | |
| 179 | fclose(fp); | |
| 180 | buf[len] = '\0'; | |
| 181 | ||
| 182 | if ((interp = tcl_create_interp()) == NULL) { | |
| 183 | return FALSE; | |
| 184 | } | |
| 185 | ||
| 186 | cur = buf; | |
| 187 | do { | |
| 188 | if (Tcl_ParseCommand(interp, cur, len, 0, &parse) == TCL_ERROR) { | |
| 189 | gaim_debug(GAIM_DEBUG_ERROR, "tcl", "parse error in %s: %s\n", plugin->path, | |
| 190 | Tcl_GetString(Tcl_GetObjResult(interp))); | |
| 191 | err = 1; | |
| 192 | break; | |
| 193 | } | |
| 194 | if (parse.tokenPtr[0].type == TCL_TOKEN_SIMPLE_WORD | |
| 195 | && !strncmp(parse.tokenPtr[0].start, "proc", parse.tokenPtr[0].size)) { | |
| 196 | if (!strncmp(parse.tokenPtr[2].start, "plugin_init", parse.tokenPtr[2].size)) { | |
| 197 | if (Tcl_EvalEx(interp, parse.commandStart, parse.commandSize, TCL_EVAL_GLOBAL) != TCL_OK) { | |
| 198 | Tcl_FreeParse(&parse); | |
| 199 | break; | |
| 200 | } | |
| 201 | found = 1; | |
| 202 | /* We'll continue parsing the file, just in case */ | |
| 203 | } | |
| 204 | } | |
| 205 | len -= (parse.commandStart + parse.commandSize) - cur; | |
| 206 | cur = parse.commandStart + parse.commandSize; | |
| 207 | Tcl_FreeParse(&parse); | |
| 208 | } while (len); | |
| 209 | ||
| 210 | if (found && !err) { | |
| 211 | if (Tcl_EvalEx(interp, "plugin_init", -1, TCL_EVAL_GLOBAL) == TCL_OK) { | |
| 212 | result = Tcl_GetObjResult(interp); | |
| 213 | if (Tcl_ListObjGetElements(interp, result, &nelems, &listitems) == TCL_OK) { | |
| 214 | if (nelems == 5) { | |
| 215 | info = g_new0(GaimPluginInfo, 1); | |
| 216 | ||
| 217 | info->api_version = 2; | |
| 218 | info->type = GAIM_PLUGIN_STANDARD; | |
| 219 | info->dependencies = g_list_append(info->dependencies, "core-tcl"); | |
| 220 | ||
| 221 | info->name = g_strdup(Tcl_GetString(listitems[0])); | |
| 222 | info->version = g_strdup(Tcl_GetString(listitems[1])); | |
| 223 | info->description = g_strdup(Tcl_GetString(listitems[2]));; | |
| 224 | info->author = g_strdup(Tcl_GetString(listitems[3])); | |
| 225 | info->homepage = g_strdup(Tcl_GetString(listitems[4])); | |
| 226 | ||
| 227 | plugin->info = info; | |
| 228 | ||
| 229 | if (gaim_plugin_register(plugin)) | |
| 230 | status = TRUE; | |
| 231 | } | |
| 232 | } | |
| 233 | } | |
| 234 | } | |
| 235 | ||
| 236 | Tcl_DeleteInterp(interp); | |
| 237 | g_free(buf); | |
| 238 | return status; | |
| 239 | } | |
| 240 | ||
| 241 | static gboolean tcl_load_plugin(GaimPlugin *plugin) | |
| 242 | { | |
| 243 | struct tcl_plugin_data *data; | |
| 244 | Tcl_Interp *interp; | |
| 245 | Tcl_Obj *result; | |
| 246 | ||
| 247 | plugin->extra = NULL; | |
| 248 | ||
| 249 | if ((interp = tcl_create_interp()) == NULL) { | |
| 250 | gaim_debug(GAIM_DEBUG_ERROR, "tcl", "Could not initialize Tcl interpreter\n"); | |
| 251 | return FALSE; | |
| 252 | } | |
| 253 | ||
| 254 | Tcl_SourceRCFile(interp); | |
| 255 | ||
| 256 | if (Tcl_EvalFile(interp, plugin->path) != TCL_OK) { | |
| 257 | result = Tcl_GetObjResult(interp); | |
| 258 | gaim_debug(GAIM_DEBUG_ERROR, "tcl", "Error evaluating %s: %s\n", plugin->path, Tcl_GetString(result)); | |
| 259 | Tcl_DeleteInterp(interp); | |
| 260 | return FALSE; | |
| 261 | } | |
| 262 | ||
| 263 | Tcl_Preserve((ClientData)interp); | |
| 264 | ||
| 265 | data = g_new0(struct tcl_plugin_data, 1); | |
| 266 | data->plugin = plugin; | |
| 267 | data->interp = interp; | |
| 268 | plugin->extra = data; | |
| 269 | ||
| 270 | g_hash_table_insert(tcl_plugins, (gpointer)interp, (gpointer)data); | |
| 271 | ||
| 272 | return TRUE; | |
| 273 | } | |
| 274 | ||
| 275 | static gboolean tcl_unload_plugin(GaimPlugin *plugin) | |
| 276 | { | |
| 277 | struct tcl_plugin_data *data; | |
| 278 | ||
| 279 | if (plugin == NULL) | |
| 280 | return TRUE; | |
| 281 | ||
| 282 | data = plugin->extra; | |
| 283 | ||
| 284 | g_hash_table_remove(tcl_plugins, (gpointer)data); | |
| 285 | if (data != NULL) { | |
| 286 | gaim_signals_disconnect_by_handle(data->interp); | |
| 287 | tcl_signal_cleanup(data->interp); | |
| 288 | Tcl_Release((ClientData)data->interp); | |
| 289 | Tcl_DeleteInterp(data->interp); | |
| 290 | g_free(data); | |
| 291 | } | |
| 292 | ||
| 293 | return TRUE; | |
| 294 | } | |
| 295 | ||
| 296 | static void tcl_destroy_plugin(GaimPlugin *plugin) | |
| 297 | { | |
| 298 | if (plugin->info != NULL) { | |
| 299 | g_free(plugin->info->name); | |
| 300 | g_free(plugin->info->version); | |
| 301 | g_free(plugin->info->description); | |
| 302 | g_free(plugin->info->author); | |
| 303 | g_free(plugin->info->homepage); | |
| 304 | } | |
| 305 | ||
| 306 | return; | |
| 307 | } | |
| 308 | ||
| 309 | static gboolean tcl_load(GaimPlugin *plugin) | |
| 310 | { | |
| 311 | tcl_glib_init(); | |
| 312 | tcl_signal_init(); | |
| 313 | tcl_plugins = g_hash_table_new(g_direct_hash, g_direct_equal); | |
| 314 | ||
| 315 | return TRUE; | |
| 316 | } | |
| 317 | ||
| 318 | static gboolean tcl_unload(GaimPlugin *plugin) | |
| 319 | { | |
| 320 | g_hash_table_destroy(tcl_plugins); | |
| 321 | tcl_plugins = NULL; | |
| 322 | ||
| 323 | return TRUE; | |
| 324 | } | |
| 325 | ||
| 326 | static GaimPluginLoaderInfo tcl_loader_info = | |
| 327 | { | |
| 328 | NULL, | |
| 329 | tcl_probe_plugin, | |
| 330 | tcl_load_plugin, | |
| 331 | tcl_unload_plugin, | |
| 332 | tcl_destroy_plugin, | |
| 333 | }; | |
| 334 | ||
| 335 | static GaimPluginInfo tcl_info = | |
| 336 | { | |
| 337 | 2, | |
| 338 | GAIM_PLUGIN_LOADER, | |
| 339 | NULL, | |
| 340 | 0, | |
| 341 | NULL, | |
| 342 | GAIM_PRIORITY_DEFAULT, | |
| 343 | "core-tcl", | |
| 344 | N_("Tcl Plugin Loader"), | |
| 345 | VERSION, | |
| 346 | N_("Provides support for loading Tcl plugins"), | |
| 347 | N_("Provides support for loading Tcl plugins"), | |
| 348 | "Ethan Blanton <eblanton@cs.purdue.edu>", | |
| 349 | GAIM_WEBSITE, | |
| 350 | tcl_load, | |
| 351 | tcl_unload, | |
| 352 | NULL, | |
| 353 | NULL, | |
| 354 | &tcl_loader_info | |
| 355 | }; | |
| 356 | ||
| 357 | static void tcl_init_plugin(GaimPlugin *plugin) | |
| 358 | { | |
| 359 | _tcl_plugin = plugin; | |
| 360 | ||
| 361 | Tcl_FindExecutable("gaim"); | |
| 362 | ||
| 363 | tcl_loader_info.exts = g_list_append(tcl_loader_info.exts, "tcl"); | |
| 364 | } | |
| 365 | ||
| 366 | GAIM_INIT_PLUGIN(tcl, tcl_init_plugin, tcl_info); |