Mercurial > pidgin.yaz
comparison plugins/gaim-remote/remote.c @ 5859:022786c7ab53
[gaim-migrate @ 6290]
CUI is gone, long live gaim-remote! The old CUI functionality, which was
for remote-controlling gaim, is now a Core Plugin, so any future UI
(including the current, normal gaim gtk UI) can be remote-controlled.
Applications will soon be able to link against the library and header files
and provide their own remote-control of gaim, but why bother? :) If you
use gaim-remote, make sure to load the new plugin. It won't auto-load.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sat, 14 Jun 2003 06:06:40 +0000 |
parents | |
children | 059d95c67cda |
comparison
equal
deleted
inserted
replaced
5858:96e5b32e75ad | 5859:022786c7ab53 |
---|---|
1 /* | |
2 * Remote control plugin for Gaim | |
3 * | |
4 * Copyright (C) 2003 Christian Hammond. | |
5 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
6 * | |
7 * This program is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation; either version 2 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 * 02111-1307, USA. | |
21 */ | |
22 #include "config.h" | |
23 | |
24 #include "gaim.h" | |
25 | |
26 #ifdef _WIN32 | |
27 #include <winsock.h> | |
28 #include <io.h> | |
29 #else | |
30 #include <sys/socket.h> | |
31 #include <sys/un.h> | |
32 #include <unistd.h> | |
33 #endif | |
34 | |
35 #include <sys/stat.h> | |
36 #include <errno.h> | |
37 #include <signal.h> | |
38 #include <getopt.h> | |
39 #include <stdarg.h> | |
40 #include <string.h> | |
41 | |
42 #include "gaim.h" | |
43 #include "remote-socket.h" | |
44 | |
45 #ifdef _WIN32 | |
46 #include "win32dep.h" | |
47 #endif | |
48 | |
49 | |
50 #define REMOTE_PLUGIN_ID "core-remote" | |
51 | |
52 struct UI { | |
53 GIOChannel *channel; | |
54 guint inpa; | |
55 }; | |
56 | |
57 #ifndef _WIN32 | |
58 static gint UI_fd = -1; | |
59 #endif | |
60 int gaim_session = 0; | |
61 GSList *uis = NULL; | |
62 | |
63 #if 0 | |
64 static guchar * | |
65 UI_build(guint32 *len, guchar type, guchar subtype, va_list args) | |
66 { | |
67 guchar *buffer; | |
68 guint32 pos; | |
69 int size; | |
70 void *data; | |
71 | |
72 *len = sizeof(guchar) * 2 + 4; | |
73 buffer = g_malloc(*len); | |
74 pos = 0; | |
75 | |
76 memcpy(buffer + pos, &type, sizeof(type)); pos += sizeof(type); | |
77 memcpy(buffer + pos, &subtype, sizeof(subtype)); pos += sizeof(subtype); | |
78 | |
79 /* we come back and do size last */ | |
80 pos += 4; | |
81 | |
82 size = va_arg(args, int); | |
83 while (size != -1) { | |
84 *len += size; | |
85 buffer = g_realloc(buffer, *len); | |
86 | |
87 data = va_arg(args, void *); | |
88 memcpy(buffer + pos, data, size); | |
89 pos += size; | |
90 | |
91 size = va_arg(args, int); | |
92 } | |
93 | |
94 pos -= sizeof(guchar) * 2 + 4; | |
95 | |
96 /* now we do size */ | |
97 memcpy(buffer + sizeof(guchar) * 2, &pos, 4); | |
98 | |
99 return buffer; | |
100 } | |
101 | |
102 static gint | |
103 UI_write(struct UI *ui, guchar *data, gint len) | |
104 { | |
105 GError *error = NULL; | |
106 gint sent; | |
107 /* we'll let the write silently fail because the read will pick it up as dead */ | |
108 g_io_channel_write_chars(ui->channel, data, len, &sent, &error); | |
109 if (error) | |
110 g_error_free(error); | |
111 return sent; | |
112 } | |
113 | |
114 static void | |
115 UI_build_write(struct UI *ui, guchar type, guchar subtype, ...) | |
116 { | |
117 va_list ap; | |
118 gchar *data; | |
119 guint32 len; | |
120 | |
121 va_start(ap, subtype); | |
122 data = UI_build(&len, type, subtype, ap); | |
123 va_end(ap); | |
124 | |
125 UI_write(ui, data, len); | |
126 | |
127 g_free(data); | |
128 } | |
129 | |
130 static void | |
131 UI_broadcast(guchar *data, gint len) | |
132 { | |
133 GSList *u = uis; | |
134 while (u) { | |
135 struct UI *ui = u->data; | |
136 UI_write(ui, data, len); | |
137 u = u->next; | |
138 } | |
139 } | |
140 | |
141 static void | |
142 UI_build_broadcast(guchar type, guchar subtype, ...) | |
143 { | |
144 va_list ap; | |
145 gchar *data; | |
146 guint32 len; | |
147 | |
148 if (!uis) | |
149 return; | |
150 | |
151 va_start(ap, subtype); | |
152 data = UI_build(&len, type, subtype, ap); | |
153 va_end(ap); | |
154 | |
155 UI_broadcast(data, len); | |
156 | |
157 g_free(data); | |
158 } | |
159 #endif | |
160 | |
161 #ifndef _WIN32 | |
162 static void | |
163 meta_handler(struct UI *ui, guchar subtype, guchar *data) | |
164 { | |
165 GaimRemotePacket *p; | |
166 GError *error = NULL; | |
167 switch (subtype) { | |
168 case CUI_META_LIST: | |
169 break; | |
170 case CUI_META_QUIT: | |
171 while (uis) { | |
172 ui = uis->data; | |
173 uis = g_slist_remove(uis, ui); | |
174 g_io_channel_shutdown(ui->channel, TRUE, &error); | |
175 g_source_remove(ui->inpa); | |
176 g_free(ui); | |
177 } | |
178 do_quit(); | |
179 break; | |
180 case CUI_META_DETACH: | |
181 uis = g_slist_remove(uis, ui); | |
182 g_io_channel_shutdown(ui->channel, TRUE, &error); | |
183 g_source_remove(ui->inpa); | |
184 g_free(ui); | |
185 break; | |
186 case CUI_META_PING: | |
187 p = gaim_remote_packet_new(CUI_TYPE_META, CUI_META_ACK); | |
188 gaim_remote_session_send_packet(g_io_channel_unix_get_fd(ui->channel), | |
189 p); | |
190 gaim_remote_packet_free(p); | |
191 break; | |
192 default: | |
193 gaim_debug(GAIM_DEBUG_WARNING, "cui", | |
194 "Unhandled meta subtype %d\n", subtype); | |
195 break; | |
196 } | |
197 | |
198 if(error) | |
199 g_error_free(error); | |
200 } | |
201 | |
202 static void | |
203 plugin_handler(struct UI *ui, guchar subtype, guchar *data) | |
204 { | |
205 #ifdef GAIM_PLUGINS | |
206 guint id; | |
207 GaimPlugin *p; | |
208 | |
209 switch (subtype) { | |
210 /* | |
211 case CUI_PLUGIN_LIST: | |
212 break; | |
213 */ | |
214 case CUI_PLUGIN_LOAD: | |
215 gaim_plugin_load(gaim_plugin_probe(data)); | |
216 break; | |
217 case CUI_PLUGIN_UNLOAD: | |
218 memcpy(&id, data, sizeof(id)); | |
219 p = g_list_nth_data(gaim_plugins_get_loaded(), id); | |
220 if (p) { | |
221 gaim_plugin_unload(p); | |
222 } | |
223 break; | |
224 default: | |
225 gaim_debug(GAIM_DEBUG_WARNING, "cui", | |
226 "Unhandled plugin subtype %d\n", subtype); | |
227 break; | |
228 } | |
229 #endif | |
230 } | |
231 | |
232 static void | |
233 user_handler(struct UI *ui, guchar subtype, guchar *data) | |
234 { | |
235 guint id; | |
236 GaimAccount *account; | |
237 | |
238 switch (subtype) { | |
239 /* | |
240 case CUI_USER_LIST: | |
241 break; | |
242 case CUI_USER_ADD: | |
243 break; | |
244 case CUI_USER_REMOVE: | |
245 break; | |
246 case CUI_USER_MODIFY: | |
247 break; | |
248 */ | |
249 case CUI_USER_SIGNON: | |
250 if (!data) | |
251 return; | |
252 memcpy(&id, data, sizeof(id)); | |
253 account = g_list_nth_data(gaim_accounts_get_all(), id); | |
254 if (account) | |
255 serv_login(account); | |
256 /* don't need to do anything here because the UI will get updates from other handlers */ | |
257 break; | |
258 default: | |
259 gaim_debug(GAIM_DEBUG_WARNING, "cui", | |
260 "Unhandled user subtype %d\n", subtype); | |
261 break; | |
262 } | |
263 } | |
264 | |
265 static void | |
266 message_handler(struct UI *ui, guchar subtype, guchar *data) | |
267 { | |
268 switch (subtype) { | |
269 case CUI_MESSAGE_LIST: | |
270 break; | |
271 case CUI_MESSAGE_SEND: | |
272 if (!data) | |
273 return; | |
274 { | |
275 guint id; | |
276 GaimConnection *gc; | |
277 guint len; | |
278 char *who, *msg; | |
279 gint flags; | |
280 int pos = 0; | |
281 | |
282 memcpy(&id, data + pos, sizeof(id)); | |
283 pos += sizeof(id); | |
284 gc = g_list_nth_data(gaim_connections_get_all(), id); | |
285 if (!gc) | |
286 return; | |
287 | |
288 memcpy(&len, data + pos, sizeof(len)); | |
289 pos += sizeof(len); | |
290 who = g_strndup(data + pos, len + 1); | |
291 pos += len; | |
292 | |
293 memcpy(&len, data + pos, sizeof(len)); | |
294 pos += sizeof(len); | |
295 msg = g_strndup(data + pos, len + 1); | |
296 pos += len; | |
297 | |
298 memcpy(&flags, data + pos, sizeof(flags)); | |
299 serv_send_im(gc, who, msg, -1, flags); | |
300 | |
301 g_free(who); | |
302 g_free(msg); | |
303 } | |
304 break; | |
305 case CUI_MESSAGE_RECV: | |
306 break; | |
307 default: | |
308 gaim_debug(GAIM_DEBUG_WARNING, "cui", | |
309 "Unhandled message subtype %d\n", subtype); | |
310 break; | |
311 } | |
312 } | |
313 | |
314 static gint | |
315 gaim_recv(GIOChannel *source, guchar *buf, gint len) | |
316 { | |
317 gint total = 0; | |
318 gint cur; | |
319 | |
320 GError *error = NULL; | |
321 | |
322 while (total < len) { | |
323 if (g_io_channel_read_chars(source, buf + total, len - total, &cur, &error) != G_IO_STATUS_NORMAL) { | |
324 if (error) | |
325 g_error_free(error); | |
326 return -1; | |
327 } | |
328 if (cur == 0) | |
329 return total; | |
330 total += cur; | |
331 } | |
332 | |
333 return total; | |
334 } | |
335 | |
336 static void | |
337 remote_handler(struct UI *ui, guchar subtype, guchar *data, int len) | |
338 { | |
339 const char *resp; | |
340 char *send; | |
341 switch (subtype) { | |
342 case CUI_REMOTE_CONNECTIONS: | |
343 break; | |
344 case CUI_REMOTE_URI: | |
345 send = g_malloc(len + 1); | |
346 memcpy(send, data, len); | |
347 send[len] = 0; | |
348 resp = handle_uri(send); | |
349 g_free(send); | |
350 /* report error */ | |
351 break; | |
352 default: | |
353 gaim_debug(GAIM_DEBUG_WARNING, "cui", | |
354 "Unhandled remote subtype %d\n", subtype); | |
355 break; | |
356 } | |
357 } | |
358 | |
359 static gboolean | |
360 UI_readable(GIOChannel *source, GIOCondition cond, gpointer data) | |
361 { | |
362 struct UI *ui = data; | |
363 | |
364 guchar type; | |
365 guchar subtype; | |
366 guint32 len; | |
367 | |
368 GError *error = NULL; | |
369 | |
370 guchar *in; | |
371 | |
372 /* no byte order worries! this'll change if we go to TCP */ | |
373 if (gaim_recv(source, &type, sizeof(type)) != sizeof(type)) { | |
374 gaim_debug(GAIM_DEBUG_ERROR, "cui", "UI has abandoned us!\n"); | |
375 uis = g_slist_remove(uis, ui); | |
376 g_io_channel_shutdown(ui->channel, TRUE, &error); | |
377 if(error) { | |
378 g_error_free(error); | |
379 error = NULL; | |
380 } | |
381 g_source_remove(ui->inpa); | |
382 g_free(ui); | |
383 return FALSE; | |
384 } | |
385 | |
386 if (gaim_recv(source, &subtype, sizeof(subtype)) != sizeof(subtype)) { | |
387 gaim_debug(GAIM_DEBUG_ERROR, "cui", "UI has abandoned us!\n"); | |
388 uis = g_slist_remove(uis, ui); | |
389 g_io_channel_shutdown(ui->channel, TRUE, &error); | |
390 if(error) { | |
391 g_error_free(error); | |
392 error = NULL; | |
393 } | |
394 g_source_remove(ui->inpa); | |
395 g_free(ui); | |
396 return FALSE; | |
397 } | |
398 | |
399 if (gaim_recv(source, (guchar *)&len, sizeof(len)) != sizeof(len)) { | |
400 gaim_debug(GAIM_DEBUG_ERROR, "cui", "UI has abandoned us!\n"); | |
401 uis = g_slist_remove(uis, ui); | |
402 g_io_channel_shutdown(ui->channel, TRUE, &error); | |
403 if(error) { | |
404 g_error_free(error); | |
405 error = NULL; | |
406 } | |
407 g_source_remove(ui->inpa); | |
408 g_free(ui); | |
409 return FALSE; | |
410 } | |
411 | |
412 if (len) { | |
413 in = g_new0(guchar, len); | |
414 if (gaim_recv(source, in, len) != len) { | |
415 gaim_debug(GAIM_DEBUG_ERROR, "cui", "UI has abandoned us!\n"); | |
416 uis = g_slist_remove(uis, ui); | |
417 g_io_channel_shutdown(ui->channel, TRUE, &error); | |
418 if(error) { | |
419 g_error_free(error); | |
420 error = NULL; | |
421 } | |
422 g_source_remove(ui->inpa); | |
423 g_free(ui); | |
424 return FALSE; | |
425 } | |
426 } else | |
427 in = NULL; | |
428 | |
429 switch (type) { | |
430 case CUI_TYPE_META: | |
431 meta_handler(ui, subtype, in); | |
432 break; | |
433 case CUI_TYPE_PLUGIN: | |
434 plugin_handler(ui, subtype, in); | |
435 break; | |
436 case CUI_TYPE_USER: | |
437 user_handler(ui, subtype, in); | |
438 break; | |
439 /* | |
440 case CUI_TYPE_CONN: | |
441 conn_handler(ui, subtype, in); | |
442 break; | |
443 case CUI_TYPE_BUDDY: | |
444 buddy_handler(ui, subtype, in); | |
445 break; | |
446 */ | |
447 case CUI_TYPE_MESSAGE: | |
448 message_handler(ui, subtype, in); | |
449 break; | |
450 /* | |
451 case CUI_TYPE_CHAT: | |
452 chat_handler(ui, subtype, in); | |
453 break; | |
454 */ | |
455 case CUI_TYPE_REMOTE: | |
456 remote_handler(ui, subtype, in, len); | |
457 break; | |
458 default: | |
459 gaim_debug(GAIM_DEBUG_WARNING, "cui", | |
460 "Unhandled type %d\n", type); | |
461 break; | |
462 } | |
463 | |
464 if (in) | |
465 g_free(in); | |
466 return TRUE; | |
467 } | |
468 | |
469 static gboolean | |
470 socket_readable(GIOChannel *source, GIOCondition cond, gpointer data) | |
471 { | |
472 struct sockaddr_un saddr; | |
473 gint len = sizeof(saddr); | |
474 gint fd; | |
475 | |
476 struct UI *ui; | |
477 | |
478 if ((fd = accept(UI_fd, (struct sockaddr *)&saddr, &len)) == -1) | |
479 return FALSE; | |
480 | |
481 ui = g_new0(struct UI, 1); | |
482 uis = g_slist_append(uis, ui); | |
483 | |
484 ui->channel = g_io_channel_unix_new(fd); | |
485 ui->inpa = g_io_add_watch(ui->channel, G_IO_IN | G_IO_HUP | G_IO_ERR, UI_readable, ui); | |
486 g_io_channel_unref(ui->channel); | |
487 | |
488 gaim_debug(GAIM_DEBUG_MISC, "cui", "Got one\n"); | |
489 return TRUE; | |
490 } | |
491 | |
492 static gint | |
493 open_socket() | |
494 { | |
495 struct sockaddr_un saddr; | |
496 gint fd; | |
497 | |
498 while (gaim_remote_session_exists(gaim_session)) | |
499 gaim_session++; | |
500 | |
501 gaim_debug(GAIM_DEBUG_MISC, "cui", "Session: %d\n", gaim_session); | |
502 | |
503 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) { | |
504 mode_t m = umask(0177); | |
505 saddr.sun_family = AF_UNIX; | |
506 | |
507 g_snprintf(saddr.sun_path, sizeof(saddr.sun_path), "%s" G_DIR_SEPARATOR_S "gaim_%s.%d", | |
508 g_get_tmp_dir(), g_get_user_name(), gaim_session); | |
509 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) != -1) | |
510 listen(fd, 100); | |
511 else { | |
512 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
513 "Failed to assign %s to a socket (Error: %s)", | |
514 saddr.sun_path, strerror(errno)); | |
515 return -1; | |
516 } | |
517 umask(m); | |
518 } else | |
519 g_log(NULL, G_LOG_LEVEL_CRITICAL, "Unable to open socket: %s", strerror(errno)); | |
520 return fd; | |
521 } | |
522 #endif /*! _WIN32*/ | |
523 | |
524 static int | |
525 core_main() | |
526 { | |
527 /* | |
528 GMainLoop *loop; | |
529 */ | |
530 #ifndef _WIN32 | |
531 GIOChannel *channel; | |
532 #endif | |
533 | |
534 gaim_set_blist(gaim_blist_new()); | |
535 gaim_blist_load(); | |
536 | |
537 #ifndef _WIN32 | |
538 | |
539 UI_fd = open_socket(); | |
540 if (UI_fd < 0) | |
541 return 1; | |
542 | |
543 channel = g_io_channel_unix_new(UI_fd); | |
544 g_io_add_watch(channel, G_IO_IN, socket_readable, NULL); | |
545 g_io_channel_unref(channel); | |
546 #endif | |
547 | |
548 /* | |
549 loop = g_main_new(TRUE); | |
550 g_main_run(loop); | |
551 */ | |
552 | |
553 return 0; | |
554 } | |
555 | |
556 static void | |
557 core_quit() | |
558 { | |
559 /* don't save prefs after plugins are gone... */ | |
560 #ifndef _WIN32 | |
561 char buf[1024]; | |
562 close(UI_fd); | |
563 snprintf(buf, 1024, "%s" G_DIR_SEPARATOR_S "gaim_%s.%d", | |
564 g_get_tmp_dir(), g_get_user_name(), gaim_session); | |
565 | |
566 unlink(buf); | |
567 | |
568 gaim_debug(GAIM_DEBUG_MISC, "core", "Removed core\n"); | |
569 #endif | |
570 } | |
571 | |
572 static gboolean | |
573 plugin_load(GaimPlugin *plugin) | |
574 { | |
575 core_main(); | |
576 } | |
577 | |
578 static gboolean | |
579 plugin_unload(GaimPlugin *plugin) | |
580 { | |
581 core_quit(); | |
582 } | |
583 | |
584 static GaimPluginInfo info = | |
585 { | |
586 2, /**< api_version */ | |
587 GAIM_PLUGIN_STANDARD, /**< type */ | |
588 NULL, /**< ui_requirement */ | |
589 0, /**< flags */ | |
590 NULL, /**< dependencies */ | |
591 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
592 | |
593 REMOTE_PLUGIN_ID, /**< id */ | |
594 N_("Remote Control"), /**< name */ | |
595 VERSION, /**< version */ | |
596 /** summary */ | |
597 N_("Provides remote control for gaim applications."), | |
598 /** description */ | |
599 N_("Gives Gaim the ability to be remote-controlled through third-party " | |
600 "applications or through the gaim-remote tool."), | |
601 "Christian Hammond <chipx86@gnupdate.org>", /**< author */ | |
602 WEBSITE, /**< homepage */ | |
603 | |
604 plugin_load, /**< load */ | |
605 plugin_unload, /**< unload */ | |
606 NULL, /**< destroy */ | |
607 | |
608 NULL, /**< ui_info */ | |
609 NULL /**< extra_info */ | |
610 }; | |
611 | |
612 static void | |
613 __init_plugin(GaimPlugin *plugin) | |
614 { | |
615 } | |
616 | |
617 GAIM_INIT_PLUGIN(remote, __init_plugin, info); |