[gaim-migrate @ 3063]
[pidgin-git.git] / src / core.c
blob4443a6ab2a76dc894118d30fd2d785e5523c765f
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>
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <sys/un.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <signal.h>
36 #include <getopt.h>
37 #include <stdarg.h>
38 #include <string.h>
40 #include "gaim.h"
42 static gint UI_fd = -1;
43 GSList *uis = NULL;
45 static guchar *UI_build(guint32 *len, guchar type, guchar subtype, va_list args)
47 guchar *buffer;
48 guint32 pos;
49 int size;
50 void *data;
52 *len = sizeof(guchar) * 2 + 4;
53 buffer = g_malloc(*len);
54 pos = 0;
56 memcpy(buffer + pos, &type, sizeof(type)); pos += sizeof(type);
57 memcpy(buffer + pos, &subtype, sizeof(subtype)); pos += sizeof(subtype);
59 /* we come back and do size last */
60 pos += 4;
62 size = va_arg(args, int);
63 while (size != -1) {
64 *len += size;
65 buffer = g_realloc(buffer, *len);
67 data = va_arg(args, void *);
68 memcpy(buffer + pos, data, size);
69 pos += size;
71 size = va_arg(args, int);
74 pos -= sizeof(guchar) * 2 + 4;
76 /* now we do size */
77 memcpy(buffer + sizeof(guchar) * 2, &pos, 4);
79 return buffer;
82 gint UI_write(struct UI *ui, guchar *data, gint len)
84 gint sent;
85 /* we'll let the write silently fail because the read will pick it up as dead */
86 g_io_channel_write(ui->channel, data, len, &sent);
87 return sent;
90 void UI_build_write(struct UI *ui, guchar type, guchar subtype, ...)
92 va_list ap;
93 gchar *data;
94 guint32 len;
96 va_start(ap, subtype);
97 data = UI_build(&len, type, subtype, ap);
98 va_end(ap);
100 UI_write(ui, data, len);
102 g_free(data);
105 void UI_broadcast(guchar *data, gint len)
107 GSList *u = uis;
108 while (u) {
109 struct UI *ui = u->data;
110 UI_write(ui, data, len);
111 u = u->next;
115 void UI_build_broadcast(guchar type, guchar subtype, ...)
117 va_list ap;
118 gchar *data;
119 guint32 len;
121 if (!uis)
122 return;
124 va_start(ap, subtype);
125 data = UI_build(&len, type, subtype, ap);
126 va_end(ap);
128 UI_broadcast(data, len);
130 g_free(data);
133 static void meta_handler(struct UI *ui, guchar subtype, guchar *data)
135 switch (subtype) {
136 case CUI_META_LIST:
137 break;
138 case CUI_META_QUIT:
139 while (uis) {
140 ui = uis->data;
141 uis = g_slist_remove(uis, ui);
142 g_io_channel_close(ui->channel);
143 g_source_remove(ui->inpa);
144 g_free(ui);
146 do_quit();
147 break;
148 case CUI_META_DETACH:
149 uis = g_slist_remove(uis, ui);
150 g_io_channel_close(ui->channel);
151 g_source_remove(ui->inpa);
152 g_free(ui);
153 break;
154 default:
155 debug_printf("unhandled meta subtype %d\n", subtype);
156 break;
160 static void plugin_handler(struct UI *ui, guchar subtype, guchar *data)
162 #ifdef GAIM_PLUGINS
163 guint id;
164 struct gaim_plugin *p;
166 switch (subtype) {
168 case CUI_PLUGIN_LIST:
169 break;
171 case CUI_PLUGIN_LOAD:
172 p = load_plugin(data);
173 /* XXX need to broadcast to UIs that plugin has been loaded */
174 break;
175 case CUI_PLUGIN_UNLOAD:
176 memcpy(&id, data, sizeof(id));
177 p = g_list_nth_data(plugins, id);
178 if (p) {
179 unload_plugin(p);
180 /* XXX need to broadcast to UIs that plugin has been unloaded */
182 break;
183 case CUI_PLUGIN_RELOAD:
184 memcpy(&id, data, sizeof(id));
185 p = g_list_nth_data(plugins, id);
186 if (p) {
187 p = reload_plugin(p);
188 /* XXX need to broadcast to UIs that plugin has been reloaded */
190 break;
191 default:
192 debug_printf("unhandled plugin subtype %d\n", subtype);
193 break;
195 #endif
198 static void user_handler(struct UI *ui, guchar subtype, guchar *data)
200 guint id;
201 struct aim_user *u;
203 switch (subtype) {
205 case CUI_USER_LIST:
206 break;
207 case CUI_USER_ADD:
208 break;
209 case CUI_USER_REMOVE:
210 break;
211 case CUI_USER_MODIFY:
212 break;
214 case CUI_USER_SIGNON:
215 if (!data)
216 return;
217 memcpy(&id, data, sizeof(id));
218 u = g_slist_nth_data(aim_users, id);
219 if (u)
220 serv_login(u);
221 /* don't need to do anything here because the UI will get updates from other handlers */
222 break;
223 default:
224 debug_printf("unhandled user subtype %d\n", subtype);
225 break;
229 static void message_handler(struct UI *ui, guchar subtype, guchar *data)
231 switch (subtype) {
232 case CUI_MESSAGE_LIST:
233 break;
234 case CUI_MESSAGE_SEND:
235 if (!data)
236 return;
238 guint id;
239 struct gaim_connection *gc;
240 guint len;
241 char *who, *msg;
242 gint flags;
243 int pos = 0;
245 memcpy(&id, data + pos, sizeof(id));
246 pos += sizeof(id);
247 gc = g_slist_nth_data(connections, id);
248 if (!gc)
249 return;
251 memcpy(&len, data + pos, sizeof(len));
252 pos += sizeof(len);
253 who = g_strndup(data + pos, len + 1);
254 pos += len;
256 memcpy(&len, data + pos, sizeof(len));
257 pos += sizeof(len);
258 msg = g_strndup(data + pos, len + 1);
259 pos += len;
261 memcpy(&flags, data + pos, sizeof(flags));
262 serv_send_im(gc, who, msg, -1, flags);
264 g_free(who);
265 g_free(msg);
267 break;
268 case CUI_MESSAGE_RECV:
269 break;
270 default:
271 debug_printf("unhandled message subtype %d\n", subtype);
272 break;
276 static gint gaim_recv(GIOChannel *source, guchar *buf, gint len)
278 gint total = 0;
279 gint cur;
281 while (total < len) {
282 if (g_io_channel_read(source, buf + total, len - total, &cur) != G_IO_ERROR_NONE)
283 return -1;
284 if (cur == 0)
285 return total;
286 total += cur;
289 return total;
292 static gboolean UI_readable(GIOChannel *source, GIOCondition cond, gpointer data)
294 struct UI *ui = data;
296 guchar type;
297 guchar subtype;
298 guint32 len;
300 guchar *in;
302 /* no byte order worries! this'll change if we go to TCP */
303 if (gaim_recv(source, &type, sizeof(type)) != sizeof(type)) {
304 debug_printf("UI has abandoned us!\n");
305 uis = g_slist_remove(uis, ui);
306 g_io_channel_close(ui->channel);
307 g_source_remove(ui->inpa);
308 g_free(ui);
309 return FALSE;
312 if (gaim_recv(source, &subtype, sizeof(subtype)) != sizeof(subtype)) {
313 debug_printf("UI has abandoned us!\n");
314 uis = g_slist_remove(uis, ui);
315 g_io_channel_close(ui->channel);
316 g_source_remove(ui->inpa);
317 g_free(ui);
318 return FALSE;
321 if (gaim_recv(source, (guchar *)&len, sizeof(len)) != sizeof(len)) {
322 debug_printf("UI has abandoned us!\n");
323 uis = g_slist_remove(uis, ui);
324 g_io_channel_close(ui->channel);
325 g_source_remove(ui->inpa);
326 g_free(ui);
327 return FALSE;
330 if (len) {
331 in = g_new0(guchar, len);
332 if (gaim_recv(source, in, len) != len) {
333 debug_printf("UI has abandoned us!\n");
334 uis = g_slist_remove(uis, ui);
335 g_io_channel_close(ui->channel);
336 g_source_remove(ui->inpa);
337 g_free(ui);
338 return FALSE;
340 } else
341 in = NULL;
343 switch (type) {
344 case CUI_TYPE_META:
345 meta_handler(ui, subtype, in);
346 break;
347 case CUI_TYPE_PLUGIN:
348 plugin_handler(ui, subtype, in);
349 break;
350 case CUI_TYPE_USER:
351 user_handler(ui, subtype, in);
352 break;
354 case CUI_TYPE_CONN:
355 conn_handler(ui, subtype, in);
356 break;
357 case CUI_TYPE_BUDDY:
358 buddy_handler(ui, subtype, in);
359 break;
361 case CUI_TYPE_MESSAGE:
362 message_handler(ui, subtype, in);
363 break;
365 case CUI_TYPE_CHAT:
366 chat_handler(ui, subtype, in);
367 break;
369 default:
370 debug_printf("unhandled type %d\n", type);
371 break;
374 if (in)
375 g_free(in);
376 return TRUE;
379 static gboolean socket_readable(GIOChannel *source, GIOCondition cond, gpointer data)
381 struct sockaddr_un saddr;
382 gint len;
383 gint fd;
385 struct UI *ui;
387 if ((fd = accept(UI_fd, (struct sockaddr *)&saddr, &len)) == -1)
388 return FALSE;
390 ui = g_new0(struct UI, 1);
391 uis = g_slist_append(uis, ui);
393 ui->channel = g_io_channel_unix_new(fd);
394 ui->inpa = g_io_add_watch(ui->channel, G_IO_IN | G_IO_HUP | G_IO_ERR, UI_readable, ui);
395 g_io_channel_unref(ui->channel);
397 debug_printf("got one\n");
398 return TRUE;
401 static gint open_socket()
403 struct sockaddr_un saddr;
404 gint fd;
406 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) {
407 mode_t m = umask(0177);
408 saddr.sun_family = AF_UNIX;
409 g_snprintf(saddr.sun_path, 108, "%s/gaim_%s.%d",
410 g_get_tmp_dir(), g_get_user_name(), getpid());
411 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) != -1)
412 listen(fd, 100);
413 else
414 g_log(NULL, G_LOG_LEVEL_CRITICAL,
415 "Failed to assign %s to a socket (Error: %s)",
416 saddr.sun_path, strerror(errno));
417 umask(m);
418 } else
419 g_log(NULL, G_LOG_LEVEL_CRITICAL, "Unable to open socket: %s", strerror(errno));
420 return fd;
423 int core_main()
426 GMainLoop *loop;
429 GIOChannel *channel;
431 UI_fd = open_socket();
432 if (UI_fd < 0)
433 return 1;
435 channel = g_io_channel_unix_new(UI_fd);
436 g_io_add_watch(channel, G_IO_IN, socket_readable, NULL);
437 g_io_channel_unref(channel);
440 loop = g_main_new(TRUE);
441 g_main_run(loop);
444 return 0;
447 void core_quit()
449 char buf[1024];
450 close(UI_fd);
451 sprintf(buf, "%s/gaim_%s.%d", g_get_tmp_dir(), g_get_user_name(), getpid());
452 unlink(buf);
453 debug_printf("Removed core\n");