Mercurial > pidgin.yaz
annotate plugins/tcl/tcl_cmds.c @ 7780:7ab3d01d2c50
[gaim-migrate @ 8425]
This should obey the logging preferences.
committer: Tailor Script <tailor@pidgin.im>
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Sat, 06 Dec 2003 21:58:46 +0000 |
parents | 1adc71ed6d45 |
children | beb7be215db3 |
rev | line source |
---|---|
6694 | 1 /** |
2 * @file tcl_cmds.c Commands for the 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 <tcl.h> | |
24 | |
25 #include "internal.h" | |
26 #include "conversation.h" | |
27 #include "connection.h" | |
28 #include "account.h" | |
29 #include "server.h" | |
30 #include "notify.h" | |
6700 | 31 #include "blist.h" |
6694 | 32 #include "debug.h" |
33 #include "prefs.h" | |
34 #include "core.h" | |
35 | |
36 #include "tcl_gaim.h" | |
37 | |
38 static gboolean tcl_validate_account(GaimAccount *account, Tcl_Interp *interp); | |
39 static gboolean tcl_validate_gc(GaimConnection *gc); | |
40 | |
41 static gboolean tcl_validate_account(GaimAccount *account, Tcl_Interp *interp) | |
42 { | |
43 GList *cur; | |
44 for (cur = gaim_accounts_get_all(); cur != NULL; cur = g_list_next(cur)) { | |
45 if (account == cur->data) | |
46 return TRUE; | |
47 } | |
48 if (interp != NULL) | |
49 Tcl_SetStringObj(Tcl_GetObjResult(interp), "invalid account", -1); | |
50 return FALSE; | |
51 } | |
52 | |
53 static gboolean tcl_validate_conversation(GaimConversation *convo, Tcl_Interp *interp) | |
54 { | |
55 GList *cur; | |
56 | |
57 for (cur = gaim_get_conversations(); cur != NULL; cur = g_list_next(cur)) { | |
58 if (convo == cur->data) | |
59 return TRUE; | |
60 } | |
61 if (interp != NULL) | |
62 Tcl_SetStringObj(Tcl_GetObjResult(interp), "invalid account", -1); | |
63 return FALSE; | |
64 } | |
65 | |
66 static gboolean tcl_validate_gc(GaimConnection *gc) | |
67 { | |
68 GList *cur; | |
69 for (cur = gaim_connections_get_all(); cur != NULL; cur = g_list_next(cur)) { | |
70 if (gc == cur->data) | |
71 return TRUE; | |
72 } | |
73 return FALSE; | |
74 } | |
75 | |
76 int tcl_cmd_account(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) | |
77 { | |
78 Tcl_Obj *result = Tcl_GetObjResult(interp), *list, *elem; | |
6737 | 79 char *cmds[] = { "alias", "connect", "connection", "disconnect", "find", |
80 "handle", "isconnected", "list", "public_ip", | |
81 "protocol", "username", NULL }; | |
6694 | 82 enum { CMD_ACCOUNT_ALIAS, CMD_ACCOUNT_CONNECT, CMD_ACCOUNT_CONNECTION, |
83 CMD_ACCOUNT_DISCONNECT, CMD_ACCOUNT_FIND, CMD_ACCOUNT_HANDLE, | |
6737 | 84 CMD_ACCOUNT_ISCONNECTED, CMD_ACCOUNT_LIST, CMD_ACCOUNT_PUBLIC_IP, |
85 CMD_ACCOUNT_PROTOCOL, CMD_ACCOUNT_USERNAME } cmd; | |
6694 | 86 char *listopts[] = { "-all", "-online", NULL }; |
87 enum { CMD_ACCOUNTLIST_ALL, CMD_ACCOUNTLIST_ONLINE } listopt; | |
6737 | 88 const char *alias, *ip; |
6694 | 89 GList *cur; |
90 GaimAccount *account; | |
91 int error; | |
92 | |
93 if (objc < 2) { | |
94 Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?args?"); | |
95 return TCL_ERROR; | |
96 } | |
97 | |
98 if ((error = Tcl_GetIndexFromObj(interp, objv[1], cmds, "subcommand", 0, (int *)&cmd)) != TCL_OK) | |
99 return error; | |
100 | |
101 switch (cmd) { | |
102 case CMD_ACCOUNT_ALIAS: | |
103 if (objc != 3) { | |
104 Tcl_WrongNumArgs(interp, 2, objv, "account"); | |
105 return TCL_ERROR; | |
106 } | |
107 error = Tcl_GetIntFromObj(interp, objv[2], (int *)&account); | |
108 if (error || !tcl_validate_account(account, interp)) | |
109 return TCL_ERROR; | |
110 alias = gaim_account_get_alias(account); | |
111 Tcl_SetStringObj(result, alias ? (char *)alias : "", -1); | |
112 break; | |
113 case CMD_ACCOUNT_CONNECT: | |
114 if (objc != 3) { | |
115 Tcl_WrongNumArgs(interp, 2, objv, "account"); | |
116 return TCL_ERROR; | |
117 } | |
118 error = Tcl_GetIntFromObj(interp, objv[2], (int *)&account); | |
119 if (error || !tcl_validate_account(account, interp)) | |
120 return TCL_ERROR; | |
121 if (gaim_account_is_connected(account)) | |
122 Tcl_SetIntObj(result, (int)gaim_account_get_connection(account)); | |
123 else | |
124 Tcl_SetIntObj(result, (int)gaim_account_connect(account)); | |
125 break; | |
126 case CMD_ACCOUNT_CONNECTION: | |
127 if (objc != 3) { | |
128 Tcl_WrongNumArgs(interp, 2, objv, "account"); | |
129 return TCL_ERROR; | |
130 } | |
131 error = Tcl_GetIntFromObj(interp, objv[2], (int *)&account); | |
132 if (error || !tcl_validate_account(account, interp)) | |
133 return TCL_ERROR; | |
134 Tcl_SetIntObj(result, (int)gaim_account_get_connection(account)); | |
135 break; | |
136 case CMD_ACCOUNT_DISCONNECT: | |
137 if (objc != 3) { | |
138 Tcl_WrongNumArgs(interp, 2, objv, "account"); | |
139 return TCL_ERROR; | |
140 } | |
141 error = Tcl_GetIntFromObj(interp, objv[2], (int *)&account); | |
142 if (error || !tcl_validate_account(account, interp)) | |
143 return TCL_ERROR; | |
144 gaim_account_disconnect(account); | |
145 break; | |
146 case CMD_ACCOUNT_FIND: | |
147 if (objc != 4) { | |
148 Tcl_WrongNumArgs(interp, 2, objv, "username protocol"); | |
149 return TCL_ERROR; | |
150 } | |
7156 | 151 Tcl_SetIntObj(result, (int)gaim_accounts_find(Tcl_GetString(objv[2]), |
6694 | 152 Tcl_GetString(objv[3]))); |
153 break; | |
154 case CMD_ACCOUNT_HANDLE: | |
155 if (objc != 2) { | |
156 Tcl_WrongNumArgs(interp, 2, objv, ""); | |
157 return TCL_ERROR; | |
158 } | |
159 Tcl_SetIntObj(result, (int)gaim_accounts_get_handle()); | |
160 break; | |
161 case CMD_ACCOUNT_ISCONNECTED: | |
162 if (objc != 3) { | |
163 Tcl_WrongNumArgs(interp, 2, objv, "account"); | |
164 return TCL_ERROR; | |
165 } | |
166 error = Tcl_GetIntFromObj(interp, objv[2], (int *)&account); | |
167 if (error || !tcl_validate_account(account, interp)) | |
168 return TCL_ERROR; | |
169 Tcl_SetBooleanObj(result, gaim_account_is_connected(account)); | |
170 break; | |
171 case CMD_ACCOUNT_LIST: | |
172 listopt = CMD_ACCOUNTLIST_ALL; | |
173 if (objc > 3) { | |
174 Tcl_WrongNumArgs(interp, 2, objv, "?option?"); | |
175 return TCL_ERROR; | |
176 } | |
177 if (objc == 3) { | |
178 if ((error = Tcl_GetIndexFromObj(interp, objv[2], listopts, "option", 0, (int *)&listopt)) != TCL_OK) | |
179 return error; | |
180 } | |
181 list = Tcl_NewListObj(0, NULL); | |
182 for (cur = gaim_accounts_get_all(); cur != NULL; cur = g_list_next(cur)) { | |
183 account = cur->data; | |
184 if (listopt == CMD_ACCOUNTLIST_ONLINE && !gaim_account_is_connected(account)) | |
185 continue; | |
186 elem = Tcl_NewIntObj((int)account); | |
187 Tcl_ListObjAppendElement(interp, list, elem); | |
188 } | |
189 Tcl_SetObjResult(interp, list); | |
190 break; | |
6737 | 191 case CMD_ACCOUNT_PUBLIC_IP: |
192 if (objc != 3) { | |
193 Tcl_WrongNumArgs(interp, 2, objv, "account"); | |
194 return TCL_ERROR; | |
195 } | |
196 error = Tcl_GetIntFromObj(interp, objv[2], (int *)&account); | |
197 if (error || !tcl_validate_account(account, interp)) | |
198 return TCL_ERROR; | |
199 ip = gaim_account_get_public_ip(account); | |
200 Tcl_SetStringObj(result, ip != NULL ? (char *)ip : "", -1); | |
201 break; | |
6694 | 202 case CMD_ACCOUNT_PROTOCOL: |
203 if (objc != 3) { | |
204 Tcl_WrongNumArgs(interp, 2, objv, "account"); | |
205 return TCL_ERROR; | |
206 } | |
207 error = Tcl_GetIntFromObj(interp, objv[2], (int *)&account); | |
208 if (error || !tcl_validate_account(account, interp)) | |
209 return TCL_ERROR; | |
210 Tcl_SetStringObj(result, (char *)gaim_account_get_protocol_id(account), -1); | |
211 break; | |
212 case CMD_ACCOUNT_USERNAME: | |
213 if (objc != 3) { | |
214 Tcl_WrongNumArgs(interp, 2, objv, "account"); | |
215 return TCL_ERROR; | |
216 } | |
217 error = Tcl_GetIntFromObj(interp, objv[2], (int *)&account); | |
218 if (error || !tcl_validate_account(account, interp)) | |
219 return TCL_ERROR; | |
220 Tcl_SetStringObj(result, (char *)gaim_account_get_username(account), -1); | |
221 break; | |
222 } | |
223 | |
224 return TCL_OK; | |
225 } | |
226 | |
227 static GaimBlistNode *tcl_list_to_buddy(Tcl_Interp *interp, int count, Tcl_Obj **elems) | |
228 { | |
229 GaimBlistNode *node = NULL; | |
230 GaimAccount *account; | |
231 char *name; | |
232 char *type; | |
233 | |
234 if (count < 3) { | |
235 Tcl_SetStringObj(Tcl_GetObjResult(interp), "list too short", -1); | |
236 return NULL; | |
237 } | |
238 | |
239 type = Tcl_GetString(elems[0]); | |
240 name = Tcl_GetString(elems[1]); | |
241 if (Tcl_GetIntFromObj(interp, elems[2], (int *)&account) != TCL_OK) | |
242 return NULL; | |
243 if (!tcl_validate_account(account, interp)) | |
244 return NULL; | |
245 | |
246 if (!strcmp(type, "buddy")) { | |
247 node = (GaimBlistNode *)gaim_find_buddy(account, name); | |
248 } else if (!strcmp(type, "group")) { | |
249 node = (GaimBlistNode *)gaim_blist_find_chat(account, name); | |
250 } | |
251 | |
252 return node; | |
253 } | |
254 | |
255 int tcl_cmd_buddy(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) | |
256 { | |
6746 | 257 Tcl_Obj *list, *tclgroup, *tclgrouplist, *tclcontact, *tclcontactlist, *tclbud, **elems, *result; |
6694 | 258 char *cmds[] = { "alias", "handle", "info", "list", NULL }; |
259 enum { CMD_BUDDY_ALIAS, CMD_BUDDY_HANDLE, CMD_BUDDY_INFO, CMD_BUDDY_LIST } cmd; | |
7100
abdc55ffadba
[gaim-migrate @ 7665]
Christian Hammond <chipx86@chipx86.com>
parents:
6982
diff
changeset
|
260 GaimBuddyList *blist; |
6746 | 261 GaimBlistNode *node, *gnode, *bnode; |
6694 | 262 GaimAccount *account; |
6746 | 263 GaimBuddy *bud; |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7100
diff
changeset
|
264 GaimChat *cnode; |
6694 | 265 int error, all = 0, count; |
266 | |
267 if (objc < 2) { | |
268 Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?args?"); | |
269 return TCL_ERROR; | |
270 } | |
271 if ((error = Tcl_GetIndexFromObj(interp, objv[1], cmds, "subcommand", 0, (int *)&cmd)) != TCL_OK) | |
272 return error; | |
273 | |
274 result = Tcl_GetObjResult(interp); | |
275 | |
276 switch (cmd) { | |
277 case CMD_BUDDY_ALIAS: | |
278 if (objc != 3) { | |
279 Tcl_WrongNumArgs(interp, 2, objv, "buddy"); | |
280 return TCL_ERROR; | |
281 } | |
282 if ((error = Tcl_ListObjGetElements(interp, objv[2], &count, &elems)) != TCL_OK) | |
283 return error; | |
284 if ((node = tcl_list_to_buddy(interp, count, elems)) == NULL) | |
285 return TCL_ERROR; | |
286 if (node->type == GAIM_BLIST_CHAT_NODE) | |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7100
diff
changeset
|
287 Tcl_SetStringObj(result, ((GaimChat *)node)->alias, -1); |
6694 | 288 else if (node->type == GAIM_BLIST_BUDDY_NODE) |
6700 | 289 Tcl_SetStringObj(result, (char *)gaim_get_buddy_alias((GaimBuddy *)node), -1); |
6694 | 290 return TCL_OK; |
291 break; | |
292 case CMD_BUDDY_HANDLE: | |
293 if (objc != 2) { | |
294 Tcl_WrongNumArgs(interp, 2, objv, ""); | |
295 return TCL_ERROR; | |
296 } | |
297 Tcl_SetIntObj(result, (int)gaim_blist_get_handle()); | |
298 break; | |
299 case CMD_BUDDY_INFO: | |
300 if (objc != 3 && objc != 4) { | |
301 Tcl_WrongNumArgs(interp, 2, objv, "( buddy | account username )"); | |
302 return TCL_ERROR; | |
303 } | |
304 if (objc == 3) { | |
305 if ((error = Tcl_ListObjGetElements(interp, objv[2], &count, &elems)) != TCL_OK) | |
306 return error; | |
307 if (count < 3) { | |
308 Tcl_SetStringObj(result, "buddy too short", -1); | |
309 return TCL_ERROR; | |
310 } | |
311 if (strcmp("buddy", Tcl_GetString(elems[0]))) { | |
312 Tcl_SetStringObj(result, "invalid buddy", -1); | |
313 return TCL_ERROR; | |
314 } | |
315 if ((error = Tcl_GetIntFromObj(interp, elems[2], (int *)&account)) != TCL_OK) | |
316 return TCL_ERROR; | |
317 if (!tcl_validate_account(account, interp)) | |
318 return TCL_ERROR; | |
319 serv_get_info(gaim_account_get_connection(account), Tcl_GetString(elems[1])); | |
320 } else { | |
321 if ((error = Tcl_GetIntFromObj(interp, objv[2], (int *)&account)) != TCL_OK) | |
322 return error; | |
323 if (!tcl_validate_account(account, interp)) | |
324 return TCL_ERROR; | |
325 serv_get_info(gaim_account_get_connection(account), Tcl_GetString(objv[3])); | |
326 } | |
327 break; | |
328 case CMD_BUDDY_LIST: | |
329 if (objc == 3) { | |
330 if (!strcmp("-all", Tcl_GetString(objv[2]))) { | |
331 all = 1; | |
332 } else { | |
333 Tcl_SetStringObj(result, "", -1); | |
334 Tcl_AppendStringsToObj(result, "unknown option: ", Tcl_GetString(objv[2]), NULL); | |
335 return TCL_ERROR; | |
336 } | |
337 } | |
338 list = Tcl_NewListObj(0, NULL); | |
339 blist = gaim_get_blist(); | |
340 for (gnode = blist->root; gnode != NULL; gnode = gnode->next) { | |
341 tclgroup = Tcl_NewListObj(0, NULL); | |
342 Tcl_ListObjAppendElement(interp, tclgroup, Tcl_NewStringObj("group", -1)); | |
343 Tcl_ListObjAppendElement(interp, tclgroup, | |
6700 | 344 Tcl_NewStringObj(((GaimGroup *)gnode)->name, -1)); |
6694 | 345 tclgrouplist = Tcl_NewListObj(0, NULL); |
346 for (node = gnode->child; node != NULL; node = node->next) { | |
347 switch (node->type) { | |
6746 | 348 case GAIM_BLIST_CONTACT_NODE: |
349 tclcontact = Tcl_NewListObj(0, NULL); | |
350 Tcl_IncrRefCount(tclcontact); | |
351 Tcl_ListObjAppendElement(interp, tclcontact, Tcl_NewStringObj("contact", -1)); | |
352 tclcontactlist = Tcl_NewListObj(0, NULL); | |
353 Tcl_IncrRefCount(tclcontactlist); | |
354 count = 0; | |
355 for (bnode = node->child; bnode != NULL; bnode = bnode ->next) { | |
356 if (bnode->type != GAIM_BLIST_BUDDY_NODE) | |
357 continue; | |
358 bud = (GaimBuddy *)bnode; | |
359 if (!all && !gaim_account_is_connected(bud->account)) | |
360 continue; | |
361 count++; | |
362 tclbud = Tcl_NewListObj(0, NULL); | |
363 Tcl_ListObjAppendElement(interp, tclbud, Tcl_NewStringObj("buddy", -1)); | |
364 Tcl_ListObjAppendElement(interp, tclbud, Tcl_NewStringObj(bud->name, -1)); | |
365 Tcl_ListObjAppendElement(interp, tclbud, Tcl_NewIntObj((int)bud->account)); | |
366 Tcl_ListObjAppendElement(interp, tclcontactlist, tclbud); | |
367 } | |
368 if (count) { | |
369 Tcl_ListObjAppendElement(interp, tclcontact, tclcontactlist); | |
370 Tcl_ListObjAppendElement(interp, tclgrouplist, tclcontact); | |
371 } | |
372 Tcl_DecrRefCount(tclcontact); | |
373 Tcl_DecrRefCount(tclcontactlist); | |
6694 | 374 break; |
375 case GAIM_BLIST_CHAT_NODE: | |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7100
diff
changeset
|
376 cnode = (GaimChat *)node; |
6694 | 377 if (!all && !gaim_account_is_connected(cnode->account)) |
378 continue; | |
379 tclbud = Tcl_NewListObj(0, NULL); | |
380 Tcl_ListObjAppendElement(interp, tclbud, Tcl_NewStringObj("chat", -1)); | |
381 Tcl_ListObjAppendElement(interp, tclbud, Tcl_NewStringObj(cnode->alias, -1)); | |
382 Tcl_ListObjAppendElement(interp, tclbud, Tcl_NewIntObj((int)cnode->account)); | |
6746 | 383 Tcl_ListObjAppendElement(interp, tclgrouplist, tclbud); |
6694 | 384 break; |
385 default: | |
6746 | 386 gaim_debug(GAIM_DEBUG_WARNING, "tcl", "Unexpected buddy type %d", node->type); |
6694 | 387 continue; |
388 } | |
389 } | |
390 Tcl_ListObjAppendElement(interp, tclgroup, tclgrouplist); | |
391 Tcl_ListObjAppendElement(interp, list, tclgroup); | |
392 } | |
393 Tcl_SetObjResult(interp, list); | |
394 break; | |
395 } | |
396 | |
397 return TCL_OK; | |
398 } | |
399 | |
400 int tcl_cmd_connection(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) | |
401 { | |
402 Tcl_Obj *result = Tcl_GetObjResult(interp), *list, *elem; | |
7713 | 403 char *cmds[] = { "account", "displayname", "handle", "list", NULL }; |
404 enum { CMD_CONN_ACCOUNT, CMD_CONN_DISPLAYNAME, CMD_CONN_HANDLE, CMD_CONN_LIST } cmd; | |
6694 | 405 int error; |
406 GList *cur; | |
407 GaimConnection *gc; | |
408 | |
409 if (objc < 2) { | |
410 Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?args?"); | |
411 return TCL_ERROR; | |
412 } | |
413 | |
414 if ((error = Tcl_GetIndexFromObj(interp, objv[1], cmds, "subcommand", 0, (int *)&cmd)) != TCL_OK) | |
415 return error; | |
416 | |
417 switch (cmd) { | |
418 case CMD_CONN_ACCOUNT: | |
419 if (objc != 3) { | |
6864 | 420 Tcl_WrongNumArgs(interp, 2, objv, "gc"); |
6694 | 421 return TCL_ERROR; |
422 } | |
423 error = Tcl_GetIntFromObj(interp, objv[2], (int *)&gc); | |
424 if (error || !tcl_validate_gc(gc)) { | |
425 Tcl_SetStringObj(result, "invalid gc", -1); | |
426 return TCL_ERROR; | |
427 } | |
428 Tcl_SetIntObj(result, (int)gaim_connection_get_account(gc)); | |
429 break; | |
7713 | 430 case CMD_CONN_DISPLAYNAME: |
431 if (objc != 3) { | |
432 Tcl_WrongNumArgs(interp, 2, objv, "gc"); | |
433 return TCL_ERROR; | |
434 } | |
435 error = Tcl_GetIntFromObj(interp, objv[2], (int *)&gc); | |
436 if (error || !tcl_validate_gc(gc)) { | |
437 Tcl_SetStringObj(result, "invalid gc", -1); | |
438 return TCL_ERROR; | |
439 } | |
440 Tcl_SetStringObj(result, (char *)gaim_connection_get_display_name(gc), -1); | |
441 break; | |
6694 | 442 case CMD_CONN_HANDLE: |
443 if (objc != 2) { | |
444 Tcl_WrongNumArgs(interp, 2, objv, ""); | |
445 return TCL_ERROR; | |
446 } | |
447 Tcl_SetIntObj(result, (int)gaim_connections_get_handle()); | |
448 break; | |
449 case CMD_CONN_LIST: | |
450 if (objc != 2) { | |
451 Tcl_WrongNumArgs(interp, 2, objv, ""); | |
452 return TCL_ERROR; | |
453 } | |
454 list = Tcl_NewListObj(0, NULL); | |
455 for (cur = gaim_connections_get_all(); cur != NULL; cur = g_list_next(cur)) { | |
456 elem = Tcl_NewIntObj((int)cur->data); | |
457 Tcl_ListObjAppendElement(interp, list, elem); | |
458 } | |
459 Tcl_SetObjResult(interp, list); | |
460 break; | |
461 } | |
462 | |
463 return TCL_OK; | |
464 } | |
465 | |
466 int tcl_cmd_conversation(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) | |
467 { | |
468 Tcl_Obj *list, *elem, *result = Tcl_GetObjResult(interp); | |
469 char *cmds[] = { "find", "handle", "list", "new", "write", NULL }; | |
470 enum { CMD_CONV_FIND, CMD_CONV_HANDLE, CMD_CONV_LIST, CMD_CONV_NEW, CMD_CONV_WRITE } cmd; | |
471 char *styles[] = { "send", "recv", "system", NULL }; | |
472 enum { CMD_CONV_WRITE_SEND, CMD_CONV_WRITE_RECV, CMD_CONV_WRITE_SYSTEM } style; | |
473 char *findopts[] = { "-account", NULL }; | |
474 enum { CMD_CONV_FIND_ACCOUNT } findopt; | |
475 char *newopts[] = { "-chat", "-im" }; | |
476 enum { CMD_CONV_NEW_CHAT, CMD_CONV_NEW_IM } newopt; | |
477 GaimConversation *convo; | |
478 GaimAccount *account; | |
479 GaimConversationType type; | |
480 GList *cur; | |
481 char *opt, *from, *what; | |
7156 | 482 int error, argsused, flags = 0; |
6694 | 483 |
484 if (objc < 2) { | |
485 Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?args?"); | |
486 return TCL_ERROR; | |
487 } | |
488 | |
489 if ((error = Tcl_GetIndexFromObj(interp, objv[1], cmds, "subcommand", 0, (int *)&cmd)) != TCL_OK) | |
490 return error; | |
491 | |
492 switch (cmd) { | |
493 case CMD_CONV_FIND: | |
494 if (objc < 3) { | |
495 Tcl_WrongNumArgs(interp, 2, objv, "?options? name"); | |
496 return TCL_ERROR; | |
497 } | |
498 argsused = 2; | |
499 account = NULL; | |
500 while (argsused < objc) { | |
501 opt = Tcl_GetString(objv[argsused]); | |
502 if (*opt == '-') { | |
503 if ((error = Tcl_GetIndexFromObj(interp, objv[argsused], findopts, | |
504 "option", 0, (int *)&findopt)) != TCL_OK) | |
505 return error; | |
506 argsused++; | |
507 switch (findopt) { | |
508 case CMD_CONV_FIND_ACCOUNT: | |
509 if (argsused == objc - 1) { | |
510 Tcl_SetStringObj(result, "-account requires an argument", -1); | |
511 return TCL_ERROR; | |
512 } | |
513 if ((error = Tcl_GetIntFromObj(interp, objv[argsused], | |
514 (int *)&account)) != TCL_OK) | |
515 return error; | |
516 if (!tcl_validate_account(account, interp)) | |
517 return TCL_ERROR; | |
518 argsused++; | |
519 break; | |
520 } | |
521 } else { | |
522 break; | |
523 } | |
524 } | |
525 if (objc - argsused != 1) { | |
526 Tcl_WrongNumArgs(interp, 2, objv, "?options? name"); | |
527 return error; | |
528 } | |
529 if (account != NULL) { | |
530 convo = gaim_find_conversation_with_account(Tcl_GetString(objv[argsused]), account); | |
531 } else { | |
532 convo = gaim_find_conversation(Tcl_GetString(objv[argsused])); | |
533 } | |
534 Tcl_SetIntObj(result, (int)convo); | |
535 break; | |
536 case CMD_CONV_HANDLE: | |
537 if (objc != 2) { | |
538 Tcl_WrongNumArgs(interp, 2, objv, ""); | |
539 return TCL_ERROR; | |
540 } | |
541 Tcl_SetIntObj(result, (int)gaim_conversations_get_handle()); | |
542 break; | |
543 case CMD_CONV_LIST: | |
544 list = Tcl_NewListObj(0, NULL); | |
545 for (cur = gaim_get_conversations(); cur != NULL; cur = g_list_next(cur)) { | |
546 elem = Tcl_NewIntObj((int)cur->data); | |
547 Tcl_ListObjAppendElement(interp, list, elem); | |
548 } | |
549 Tcl_SetObjResult(interp, list); | |
550 break; | |
551 case CMD_CONV_NEW: | |
552 if (objc < 4) { | |
553 Tcl_WrongNumArgs(interp, 2, objv, "?options? account name"); | |
554 return TCL_ERROR; | |
555 } | |
556 argsused = 2; | |
557 type = GAIM_CONV_IM; | |
558 while (argsused < objc) { | |
559 opt = Tcl_GetString(objv[argsused]); | |
560 if (*opt == '-') { | |
561 if ((error = Tcl_GetIndexFromObj(interp, objv[argsused], newopts, | |
562 "option", 0, (int *)&newopt)) != TCL_OK) | |
563 return error; | |
564 argsused++; | |
565 switch (newopt) { | |
566 case CMD_CONV_NEW_CHAT: | |
567 type = GAIM_CONV_CHAT; | |
568 break; | |
569 case CMD_CONV_NEW_IM: | |
570 type = GAIM_CONV_IM; | |
571 break; | |
572 } | |
573 } else { | |
574 break; | |
575 } | |
576 } | |
577 if (objc - argsused != 2) { | |
578 Tcl_WrongNumArgs(interp, 2, objv, "?options? account name"); | |
579 return TCL_ERROR; | |
580 } | |
581 if ((error = Tcl_GetIntFromObj(interp, objv[argsused++], (int *)&account)) != TCL_OK) | |
582 return error; | |
583 if (!tcl_validate_account(account, interp)) | |
584 return TCL_ERROR; | |
585 convo = gaim_conversation_new(type, account, Tcl_GetString(objv[argsused])); | |
586 Tcl_SetIntObj(result, (int)convo); | |
587 break; | |
588 case CMD_CONV_WRITE: | |
589 if (objc != 6) { | |
590 Tcl_WrongNumArgs(interp, 2, objv, "conversation style from what"); | |
591 return TCL_ERROR; | |
592 } | |
593 if ((error = Tcl_GetIntFromObj(interp, objv[2], (int *)&convo)) != TCL_OK) | |
594 return error; | |
595 if ((error = Tcl_GetIndexFromObj(interp, objv[3], styles, "style", 0, (int *)&style)) != TCL_OK) | |
596 return error; | |
597 if (!tcl_validate_conversation(convo, interp)) | |
598 return TCL_ERROR; | |
599 from = Tcl_GetString(objv[4]); | |
600 what = Tcl_GetString(objv[5]); | |
601 | |
602 switch (style) { | |
603 case CMD_CONV_WRITE_SEND: | |
604 flags = GAIM_MESSAGE_SEND; | |
605 break; | |
606 case CMD_CONV_WRITE_RECV: | |
607 flags = GAIM_MESSAGE_RECV; | |
608 break; | |
609 case CMD_CONV_WRITE_SYSTEM: | |
610 flags = GAIM_MESSAGE_SYSTEM; | |
611 break; | |
612 } | |
613 if (gaim_conversation_get_type(convo) == GAIM_CONV_CHAT) | |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7100
diff
changeset
|
614 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), from, what, flags, time(NULL)); |
6694 | 615 else |
7118
bf630f7dfdcd
[gaim-migrate @ 7685]
Christian Hammond <chipx86@chipx86.com>
parents:
7100
diff
changeset
|
616 gaim_conv_im_write(GAIM_CONV_IM(convo), from, what, flags, time(NULL)); |
6694 | 617 break; |
618 } | |
619 | |
620 return TCL_OK; | |
621 } | |
622 | |
623 int tcl_cmd_core(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) | |
624 { | |
625 Tcl_Obj *result = Tcl_GetObjResult(interp); | |
626 char *cmds[] = { "handle", "quit", NULL }; | |
627 enum { CMD_CORE_HANDLE, CMD_CORE_QUIT } cmd; | |
628 int error; | |
629 | |
630 if (objc < 2) { | |
631 Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?args?"); | |
632 return TCL_ERROR; | |
633 } | |
634 | |
635 if ((error = Tcl_GetIndexFromObj(interp, objv[1], cmds, "subcommand", 0, (int *)&cmd)) != TCL_OK) | |
636 return error; | |
637 | |
638 switch (cmd) { | |
639 case CMD_CORE_HANDLE: | |
640 if (objc != 2) { | |
641 Tcl_WrongNumArgs(interp, 2, objv, ""); | |
642 return TCL_ERROR; | |
643 } | |
644 Tcl_SetIntObj(result, (int)gaim_get_core()); | |
645 break; | |
646 case CMD_CORE_QUIT: | |
647 if (objc != 2) { | |
648 Tcl_WrongNumArgs(interp, 2, objv, ""); | |
649 return TCL_ERROR; | |
650 } | |
651 gaim_core_quit(); | |
652 break; | |
653 } | |
654 | |
655 return TCL_OK; | |
656 } | |
657 | |
658 int tcl_cmd_debug(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) | |
659 { | |
660 char *category, *message; | |
661 int lev; | |
662 char *levels[] = { "-misc", "-info", "-warning", "-error", NULL }; | |
663 GaimDebugLevel levelind[] = { GAIM_DEBUG_MISC, GAIM_DEBUG_INFO, GAIM_DEBUG_WARNING, GAIM_DEBUG_ERROR }; | |
664 int error; | |
665 | |
666 if (objc != 4) { | |
667 Tcl_WrongNumArgs(interp, 1, objv, "level category message"); | |
668 return TCL_ERROR; | |
669 } | |
670 | |
671 error = Tcl_GetIndexFromObj(interp, objv[1], levels, "debug level", 0, &lev); | |
672 if (error != TCL_OK) | |
673 return error; | |
674 | |
675 category = Tcl_GetString(objv[2]); | |
676 message = Tcl_GetString(objv[3]); | |
677 | |
678 gaim_debug(levelind[lev], category, "%s\n", message); | |
679 | |
680 return TCL_OK; | |
681 } | |
682 | |
683 int tcl_cmd_notify(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) | |
684 { | |
685 int error, type; | |
686 char *opts[] = { "-error", "-warning", "-info", NULL }; | |
687 GaimNotifyMsgType optind[] = { GAIM_NOTIFY_MSG_ERROR, GAIM_NOTIFY_MSG_WARNING, GAIM_NOTIFY_MSG_INFO }; | |
688 char *title, *msg1, *msg2; | |
689 | |
690 if (objc < 4 || objc > 5) { | |
691 Tcl_WrongNumArgs(interp, 1, objv, "?type? title primary secondary"); | |
692 return TCL_ERROR; | |
693 } | |
694 | |
695 if (objc == 4) { | |
696 title = Tcl_GetString(objv[1]); | |
697 msg1 = Tcl_GetString(objv[2]); | |
698 msg2 = Tcl_GetString(objv[3]); | |
699 } else { | |
700 error = Tcl_GetIndexFromObj(interp, objv[1], opts, "message type", 0, &type); | |
701 if (error != TCL_OK) | |
702 return error; | |
703 title = Tcl_GetString(objv[2]); | |
704 msg1 = Tcl_GetString(objv[3]); | |
705 msg2 = Tcl_GetString(objv[4]); | |
706 } | |
707 | |
708 gaim_notify_message(_tcl_plugin, optind[type], title, msg1, msg2, NULL, NULL); | |
709 | |
710 return TCL_OK; | |
711 } | |
712 | |
713 int tcl_cmd_prefs(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) | |
714 { | |
715 Tcl_Obj *result, *list, *elem, **elems; | |
716 char *cmds[] = { "get", "set", "type", NULL }; | |
717 enum { CMD_PREFS_GET, CMD_PREFS_SET, CMD_PREFS_TYPE } cmd; | |
718 /* char *types[] = { "none", "boolean", "int", "string", "stringlist", NULL }; */ | |
719 /* enum { TCL_PREFS_NONE, TCL_PREFS_BOOL, TCL_PREFS_INT, TCL_PREFS_STRING, TCL_PREFS_STRINGLIST } type; */ | |
720 GaimPrefType preftype; | |
721 GList *cur; | |
722 int error, intval, nelem, i; | |
723 | |
724 if (objc < 2) { | |
725 Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?args?"); | |
726 return TCL_ERROR; | |
727 } | |
728 | |
729 if ((error = Tcl_GetIndexFromObj(interp, objv[1], cmds, "subcommand", 0, (int *)&cmd)) != TCL_OK) | |
730 return error; | |
731 | |
732 result = Tcl_GetObjResult(interp); | |
733 switch (cmd) { | |
734 case CMD_PREFS_GET: | |
735 if (objc != 3) { | |
736 Tcl_WrongNumArgs(interp, 1, objv, "path"); | |
737 return TCL_ERROR; | |
738 } | |
739 preftype = gaim_prefs_get_type(Tcl_GetString(objv[2])); | |
740 switch (preftype) { | |
741 case GAIM_PREF_NONE: | |
742 Tcl_SetStringObj(result, "pref type none", -1); | |
743 return TCL_ERROR; | |
744 break; | |
745 case GAIM_PREF_BOOLEAN: | |
746 Tcl_SetBooleanObj(result, gaim_prefs_get_bool(Tcl_GetString(objv[2]))); | |
747 break; | |
748 case GAIM_PREF_INT: | |
749 Tcl_SetIntObj(result, gaim_prefs_get_int(Tcl_GetString(objv[2]))); | |
750 break; | |
751 case GAIM_PREF_STRING: | |
752 Tcl_SetStringObj(result, (char *)gaim_prefs_get_string(Tcl_GetString(objv[2])), -1); | |
753 break; | |
754 case GAIM_PREF_STRING_LIST: | |
755 cur = gaim_prefs_get_string_list(Tcl_GetString(objv[2])); | |
756 list = Tcl_NewListObj(0, NULL); | |
757 while (cur != NULL) { | |
758 elem = Tcl_NewStringObj((char *)cur->data, -1); | |
759 Tcl_ListObjAppendElement(interp, list, elem); | |
760 cur = g_list_next(cur); | |
761 } | |
762 Tcl_SetObjResult(interp, list); | |
763 break; | |
764 default: | |
765 gaim_debug(GAIM_DEBUG_ERROR, "tcl", "tcl does not know about pref type %d\n", preftype); | |
766 Tcl_SetStringObj(result, "unknown pref type", -1); | |
767 return TCL_ERROR; | |
768 } | |
769 break; | |
770 case CMD_PREFS_SET: | |
771 if (objc != 4) { | |
772 Tcl_WrongNumArgs(interp, 1, objv, "path value"); | |
773 return TCL_ERROR; | |
774 } | |
775 preftype = gaim_prefs_get_type(Tcl_GetString(objv[2])); | |
776 switch (preftype) { | |
777 case GAIM_PREF_NONE: | |
778 Tcl_SetStringObj(result, "bad path or pref type none", -1); | |
779 return TCL_ERROR; | |
780 break; | |
781 case GAIM_PREF_BOOLEAN: | |
782 if ((error = Tcl_GetBooleanFromObj(interp, objv[3], &intval)) != TCL_OK) | |
783 return error; | |
784 gaim_prefs_set_bool(Tcl_GetString(objv[2]), intval); | |
785 break; | |
786 case GAIM_PREF_INT: | |
787 if ((error = Tcl_GetIntFromObj(interp, objv[3], &intval)) != TCL_OK) | |
788 return error; | |
789 gaim_prefs_set_int(Tcl_GetString(objv[2]), intval); | |
790 break; | |
791 case GAIM_PREF_STRING: | |
792 gaim_prefs_set_string(Tcl_GetString(objv[2]), Tcl_GetString(objv[3])); | |
793 break; | |
794 case GAIM_PREF_STRING_LIST: | |
795 if ((error = Tcl_ListObjGetElements(interp, objv[3], &nelem, &elems)) != TCL_OK) | |
796 return error; | |
797 cur = NULL; | |
798 for (i = 0; i < nelem; i++) { | |
799 cur = g_list_append(cur, (gpointer)Tcl_GetString(elems[i])); | |
800 } | |
801 gaim_prefs_set_string_list(Tcl_GetString(objv[2]), cur); | |
802 g_list_free(cur); | |
803 break; | |
804 default: | |
805 gaim_debug(GAIM_DEBUG_ERROR, "tcl", "tcl does not know about pref type %d\n", preftype); | |
806 return TCL_ERROR; | |
807 } | |
808 break; | |
809 case CMD_PREFS_TYPE: | |
810 if (objc != 3) { | |
811 Tcl_WrongNumArgs(interp, 1, objv, "path"); | |
812 return TCL_ERROR; | |
813 } | |
814 preftype = gaim_prefs_get_type(Tcl_GetString(objv[2])); | |
815 switch (preftype) { | |
816 case GAIM_PREF_NONE: | |
817 Tcl_SetStringObj(result, "none", -1); | |
818 break; | |
819 case GAIM_PREF_BOOLEAN: | |
820 Tcl_SetStringObj(result, "boolean", -1); | |
821 break; | |
822 case GAIM_PREF_INT: | |
823 Tcl_SetStringObj(result, "int", -1); | |
824 break; | |
825 case GAIM_PREF_STRING: | |
826 Tcl_SetStringObj(result, "string", -1); | |
827 break; | |
828 case GAIM_PREF_STRING_LIST: | |
829 Tcl_SetStringObj(result, "stringlist", -1); | |
830 break; | |
831 default: | |
832 gaim_debug(GAIM_DEBUG_ERROR, "tcl", "tcl does not know about pref type %d\n", preftype); | |
833 Tcl_SetStringObj(result, "unknown", -1); | |
834 } | |
835 break; | |
836 } | |
837 | |
838 return TCL_OK; | |
839 } | |
840 | |
841 int tcl_cmd_send_im(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) | |
842 { | |
843 GaimConnection *gc; | |
844 char *who, *text; | |
845 int error; | |
846 Tcl_Obj *result; | |
847 | |
848 if (objc != 4) { | |
849 Tcl_WrongNumArgs(interp, 1, objv, "gc who text"); | |
850 return TCL_ERROR; | |
851 } | |
852 | |
853 if ((error = Tcl_GetIntFromObj(interp, objv[1], (int *)&gc)) != TCL_OK) | |
854 return error; | |
855 if (!tcl_validate_gc(gc)) { | |
856 result = Tcl_GetObjResult(interp); | |
857 Tcl_SetStringObj(result, "invalid gc", -1); | |
858 return TCL_ERROR; | |
859 } | |
860 | |
861 who = Tcl_GetString(objv[2]); | |
862 text = Tcl_GetString(objv[3]); | |
863 | |
6982 | 864 serv_send_im(gc, who, text, 0); |
6694 | 865 |
866 return TCL_OK; | |
867 } | |
868 | |
869 int tcl_cmd_signal(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) | |
870 { | |
871 char *cmds[] = { "connect", "disconnect", NULL }; | |
872 enum { CMD_SIGNAL_CONNECT, CMD_SIGNAL_DISCONNECT } cmd; | |
873 struct tcl_signal_handler *handler; | |
874 Tcl_Obj **elems, *result = Tcl_GetObjResult(interp); | |
875 void *instance; | |
876 int error, nelems, i; | |
877 | |
878 if (objc < 2) { | |
879 Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?args?"); | |
880 return TCL_ERROR; | |
881 } | |
882 | |
883 if ((error = Tcl_GetIndexFromObj(interp, objv[1], cmds, "subcommand", 0, (int *)&cmd)) != TCL_OK) | |
884 return error; | |
885 | |
886 switch (cmd) { | |
887 case CMD_SIGNAL_CONNECT: | |
888 if (objc != 6) { | |
889 Tcl_WrongNumArgs(interp, 2, objv, "instance signal args proc"); | |
890 return TCL_ERROR; | |
891 } | |
892 if ((error = Tcl_ListObjGetElements(interp, objv[4], &nelems, &elems)) != TCL_OK) | |
893 return error; | |
894 handler = g_new0(struct tcl_signal_handler, 1); | |
895 if ((error = Tcl_GetIntFromObj(interp, objv[2], (int *)&handler->instance)) != TCL_OK) { | |
896 g_free(handler); | |
897 return error; | |
898 } | |
899 handler->signal = g_strdup(Tcl_GetString(objv[3])); | |
900 if (nelems) { | |
901 handler->argnames = g_new0(char *, nelems); | |
902 for (i = 0; i < nelems; i++) { | |
903 handler->argnames[i] = g_strdup(Tcl_GetString(elems[i])); | |
904 } | |
905 } | |
906 handler->nnames = nelems; | |
907 handler->proc = Tcl_NewStringObj("namespace eval ::gaim::_callback { ", -1); | |
908 Tcl_AppendStringsToObj(handler->proc, Tcl_GetString(objv[5]), " }", NULL); | |
909 Tcl_IncrRefCount(handler->proc); | |
910 handler->interp = interp; | |
911 if (!tcl_signal_connect(handler)) { | |
912 tcl_signal_handler_free(handler); | |
913 Tcl_SetIntObj(result, 1); | |
914 } else { | |
915 Tcl_SetIntObj(result, 0); | |
916 } | |
917 break; | |
918 case CMD_SIGNAL_DISCONNECT: | |
919 if (objc != 4) { | |
920 Tcl_WrongNumArgs(interp, 2, objv, "signal"); | |
921 return TCL_ERROR; | |
922 } | |
923 if ((error = Tcl_GetIntFromObj(interp, objv[2], (int *)&instance)) != TCL_OK) | |
924 return error; | |
925 tcl_signal_disconnect(instance, Tcl_GetString(objv[3]), interp); | |
926 break; | |
927 } | |
928 | |
929 return TCL_OK; | |
930 } | |
931 | |
932 static gboolean unload_self(gpointer data) | |
933 { | |
934 GaimPlugin *plugin = data; | |
935 gaim_plugin_unload(plugin); | |
936 return FALSE; | |
937 } | |
938 | |
939 int tcl_cmd_unload(ClientData unused, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) | |
940 { | |
941 GaimPlugin *plugin; | |
942 if (objc != 1) { | |
943 Tcl_WrongNumArgs(interp, 1, objv, ""); | |
944 return TCL_ERROR; | |
945 } | |
946 | |
947 if ((plugin = tcl_interp_get_plugin(interp)) == NULL) { | |
948 /* This isn't exactly OK, but heh. What do you do? */ | |
949 return TCL_OK; | |
950 } | |
951 /* We can't unload immediately, but we can unload at the first | |
952 * known safe opportunity. */ | |
953 g_idle_add(unload_self, (gpointer)plugin); | |
954 | |
955 return TCL_OK; | |
956 } |