[gaim-migrate @ 4676]
[pidgin-git.git] / src / core.c
blobcf5011ae62db3c1a231903d63b5f57e571a49caa
1 /*
2 * gaim
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <glib.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
31 #ifdef _WIN32
32 #include <winsock.h>
33 #include <io.h>
34 #else
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 #include <unistd.h>
38 #endif
40 #include <sys/stat.h>
41 #include <errno.h>
42 #include <signal.h>
43 #include <getopt.h>
44 #include <stdarg.h>
45 #include <string.h>
47 #include "gaim.h"
48 #include "gaim-socket.h"
50 #ifdef _WIN32
51 #include "win32dep.h"
52 #endif
54 #ifndef _WIN32
55 static gint UI_fd = -1;
56 #endif
57 int gaim_session = 0;
58 GSList *uis = NULL;
59 GSList *groups = NULL;
61 static guchar *UI_build(guint32 *len, guchar type, guchar subtype, va_list args)
63 guchar *buffer;
64 guint32 pos;
65 int size;
66 void *data;
68 *len = sizeof(guchar) * 2 + 4;
69 buffer = g_malloc(*len);
70 pos = 0;
72 memcpy(buffer + pos, &type, sizeof(type)); pos += sizeof(type);
73 memcpy(buffer + pos, &subtype, sizeof(subtype)); pos += sizeof(subtype);
75 /* we come back and do size last */
76 pos += 4;
78 size = va_arg(args, int);
79 while (size != -1) {
80 *len += size;
81 buffer = g_realloc(buffer, *len);
83 data = va_arg(args, void *);
84 memcpy(buffer + pos, data, size);
85 pos += size;
87 size = va_arg(args, int);
90 pos -= sizeof(guchar) * 2 + 4;
92 /* now we do size */
93 memcpy(buffer + sizeof(guchar) * 2, &pos, 4);
95 return buffer;
98 gint UI_write(struct UI *ui, guchar *data, gint len)
100 gint sent;
101 /* we'll let the write silently fail because the read will pick it up as dead */
102 g_io_channel_write(ui->channel, data, len, &sent);
103 return sent;
106 void UI_build_write(struct UI *ui, guchar type, guchar subtype, ...)
108 va_list ap;
109 gchar *data;
110 guint32 len;
112 va_start(ap, subtype);
113 data = UI_build(&len, type, subtype, ap);
114 va_end(ap);
116 UI_write(ui, data, len);
118 g_free(data);
121 void UI_broadcast(guchar *data, gint len)
123 GSList *u = uis;
124 while (u) {
125 struct UI *ui = u->data;
126 UI_write(ui, data, len);
127 u = u->next;
131 void UI_build_broadcast(guchar type, guchar subtype, ...)
133 va_list ap;
134 gchar *data;
135 guint32 len;
137 if (!uis)
138 return;
140 va_start(ap, subtype);
141 data = UI_build(&len, type, subtype, ap);
142 va_end(ap);
144 UI_broadcast(data, len);
146 g_free(data);
149 #ifndef _WIN32
150 static void meta_handler(struct UI *ui, guchar subtype, guchar *data)
152 struct gaim_cui_packet *p;
153 switch (subtype) {
154 case CUI_META_LIST:
155 break;
156 case CUI_META_QUIT:
157 while (uis) {
158 ui = uis->data;
159 uis = g_slist_remove(uis, ui);
160 g_io_channel_close(ui->channel);
161 g_source_remove(ui->inpa);
162 g_free(ui);
164 do_quit();
165 break;
166 case CUI_META_DETACH:
167 uis = g_slist_remove(uis, ui);
168 g_io_channel_close(ui->channel);
169 g_source_remove(ui->inpa);
170 g_free(ui);
171 break;
172 case CUI_META_PING:
173 p = cui_packet_new(CUI_TYPE_META, CUI_META_ACK);
174 cui_send_packet(g_io_channel_unix_get_fd(ui->channel), p);
175 cui_packet_free(p);
176 break;
177 default:
178 debug_printf("unhandled meta subtype %d\n", subtype);
179 break;
183 static void plugin_handler(struct UI *ui, guchar subtype, guchar *data)
185 #ifdef GAIM_PLUGINS
186 guint id;
187 struct gaim_plugin *p;
189 switch (subtype) {
191 case CUI_PLUGIN_LIST:
192 break;
194 case CUI_PLUGIN_LOAD:
195 p = load_plugin(data);
196 /* XXX need to broadcast to UIs that plugin has been loaded */
197 break;
198 case CUI_PLUGIN_UNLOAD:
199 memcpy(&id, data, sizeof(id));
200 p = g_list_nth_data(plugins, id);
201 if (p) {
202 unload_plugin(p);
203 /* XXX need to broadcast to UIs that plugin has been unloaded */
205 break;
206 default:
207 debug_printf("unhandled plugin subtype %d\n", subtype);
208 break;
210 #endif
213 static void user_handler(struct UI *ui, guchar subtype, guchar *data)
215 guint id;
216 struct aim_user *u;
218 switch (subtype) {
220 case CUI_USER_LIST:
221 break;
222 case CUI_USER_ADD:
223 break;
224 case CUI_USER_REMOVE:
225 break;
226 case CUI_USER_MODIFY:
227 break;
229 case CUI_USER_SIGNON:
230 if (!data)
231 return;
232 memcpy(&id, data, sizeof(id));
233 u = g_slist_nth_data(aim_users, id);
234 if (u)
235 serv_login(u);
236 /* don't need to do anything here because the UI will get updates from other handlers */
237 break;
238 default:
239 debug_printf("unhandled user subtype %d\n", subtype);
240 break;
244 static void message_handler(struct UI *ui, guchar subtype, guchar *data)
246 switch (subtype) {
247 case CUI_MESSAGE_LIST:
248 break;
249 case CUI_MESSAGE_SEND:
250 if (!data)
251 return;
253 guint id;
254 struct gaim_connection *gc;
255 guint len;
256 char *who, *msg;
257 gint flags;
258 int pos = 0;
260 memcpy(&id, data + pos, sizeof(id));
261 pos += sizeof(id);
262 gc = g_slist_nth_data(connections, id);
263 if (!gc)
264 return;
266 memcpy(&len, data + pos, sizeof(len));
267 pos += sizeof(len);
268 who = g_strndup(data + pos, len + 1);
269 pos += len;
271 memcpy(&len, data + pos, sizeof(len));
272 pos += sizeof(len);
273 msg = g_strndup(data + pos, len + 1);
274 pos += len;
276 memcpy(&flags, data + pos, sizeof(flags));
277 serv_send_im(gc, who, msg, -1, flags);
279 g_free(who);
280 g_free(msg);
282 break;
283 case CUI_MESSAGE_RECV:
284 break;
285 default:
286 debug_printf("unhandled message subtype %d\n", subtype);
287 break;
291 static gint gaim_recv(GIOChannel *source, guchar *buf, gint len)
293 gint total = 0;
294 gint cur;
296 while (total < len) {
297 if (g_io_channel_read(source, buf + total, len - total, &cur) != G_IO_ERROR_NONE)
298 return -1;
299 if (cur == 0)
300 return total;
301 total += cur;
304 return total;
307 static void remote_handler(struct UI *ui, guchar subtype, guchar *data, int len)
309 const char *resp;
310 char *send;
311 switch (subtype) {
312 case CUI_REMOTE_CONNECTIONS:
313 break;
314 case CUI_REMOTE_URI:
315 send = g_malloc(len + 1);
316 memcpy(send, data, len);
317 send[len] = 0;
318 resp = handle_uri(send);
319 g_free(send);
320 /* report error */
321 break;
322 default:
323 debug_printf("Unhandled remote subtype %d\n", subtype);
324 break;
328 static gboolean UI_readable(GIOChannel *source, GIOCondition cond, gpointer data)
330 struct UI *ui = data;
332 guchar type;
333 guchar subtype;
334 guint32 len;
336 guchar *in;
338 /* no byte order worries! this'll change if we go to TCP */
339 if (gaim_recv(source, &type, sizeof(type)) != sizeof(type)) {
340 debug_printf("UI has abandoned us!\n");
341 uis = g_slist_remove(uis, ui);
342 g_io_channel_close(ui->channel);
343 g_source_remove(ui->inpa);
344 g_free(ui);
345 return FALSE;
348 if (gaim_recv(source, &subtype, sizeof(subtype)) != sizeof(subtype)) {
349 debug_printf("UI has abandoned us!\n");
350 uis = g_slist_remove(uis, ui);
351 g_io_channel_close(ui->channel);
352 g_source_remove(ui->inpa);
353 g_free(ui);
354 return FALSE;
357 if (gaim_recv(source, (guchar *)&len, sizeof(len)) != sizeof(len)) {
358 debug_printf("UI has abandoned us!\n");
359 uis = g_slist_remove(uis, ui);
360 g_io_channel_close(ui->channel);
361 g_source_remove(ui->inpa);
362 g_free(ui);
363 return FALSE;
366 if (len) {
367 in = g_new0(guchar, len);
368 if (gaim_recv(source, in, len) != len) {
369 debug_printf("UI has abandoned us!\n");
370 uis = g_slist_remove(uis, ui);
371 g_io_channel_close(ui->channel);
372 g_source_remove(ui->inpa);
373 g_free(ui);
374 return FALSE;
376 } else
377 in = NULL;
379 switch (type) {
380 case CUI_TYPE_META:
381 meta_handler(ui, subtype, in);
382 break;
383 case CUI_TYPE_PLUGIN:
384 plugin_handler(ui, subtype, in);
385 break;
386 case CUI_TYPE_USER:
387 user_handler(ui, subtype, in);
388 break;
390 case CUI_TYPE_CONN:
391 conn_handler(ui, subtype, in);
392 break;
393 case CUI_TYPE_BUDDY:
394 buddy_handler(ui, subtype, in);
395 break;
397 case CUI_TYPE_MESSAGE:
398 message_handler(ui, subtype, in);
399 break;
401 case CUI_TYPE_CHAT:
402 chat_handler(ui, subtype, in);
403 break;
405 case CUI_TYPE_REMOTE:
406 remote_handler(ui, subtype, in, len);
407 break;
408 default:
409 debug_printf("unhandled type %d\n", type);
410 break;
413 if (in)
414 g_free(in);
415 return TRUE;
418 static gboolean socket_readable(GIOChannel *source, GIOCondition cond, gpointer data)
420 struct sockaddr_un saddr;
421 gint len = sizeof(saddr);
422 gint fd;
424 struct UI *ui;
426 if ((fd = accept(UI_fd, (struct sockaddr *)&saddr, &len)) == -1)
427 return FALSE;
429 ui = g_new0(struct UI, 1);
430 uis = g_slist_append(uis, ui);
432 ui->channel = g_io_channel_unix_new(fd);
433 ui->inpa = g_io_add_watch(ui->channel, G_IO_IN | G_IO_HUP | G_IO_ERR, UI_readable, ui);
434 g_io_channel_unref(ui->channel);
436 debug_printf("got one\n");
437 return TRUE;
440 static gint open_socket()
442 struct sockaddr_un saddr;
443 gint fd;
445 while (gaim_session_exists(gaim_session))
446 gaim_session++;
448 debug_printf("session: %d\n", gaim_session);
450 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) {
451 mode_t m = umask(0177);
452 saddr.sun_family = AF_UNIX;
454 g_snprintf(saddr.sun_path, sizeof(saddr.sun_path), "%s" G_DIR_SEPARATOR_S "gaim_%s.%d",
455 g_get_tmp_dir(), g_get_user_name(), gaim_session);
456 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) != -1)
457 listen(fd, 100);
458 else {
459 g_log(NULL, G_LOG_LEVEL_CRITICAL,
460 "Failed to assign %s to a socket (Error: %s)",
461 saddr.sun_path, strerror(errno));
462 return -1;
464 umask(m);
465 } else
466 g_log(NULL, G_LOG_LEVEL_CRITICAL, "Unable to open socket: %s", strerror(errno));
467 return fd;
469 #endif /*! _WIN32*/
471 int core_main()
474 GMainLoop *loop;
477 #ifndef _WIN32
478 GIOChannel *channel;
480 UI_fd = open_socket();
481 if (UI_fd < 0)
482 return 1;
484 channel = g_io_channel_unix_new(UI_fd);
485 g_io_add_watch(channel, G_IO_IN, socket_readable, NULL);
486 g_io_channel_unref(channel);
487 #endif
490 loop = g_main_new(TRUE);
491 g_main_run(loop);
494 gaim_blist_load();
496 return 0;
499 void core_quit()
501 /* don't save prefs after plugins are gone... */
502 #ifndef _WIN32
504 char buf[1024];
505 close(UI_fd);
506 sprintf(buf, "%s" G_DIR_SEPARATOR_S "gaim_%s.%d", g_get_tmp_dir(), g_get_user_name(), gaim_session);
507 unlink(buf);
508 debug_printf("Removed core\n");
510 #endif