Mercurial > pidgin
annotate src/core.c @ 2435:b4f2a53c0ee5
[gaim-migrate @ 2448]
more splits
committer: Tailor Script <tailor@pidgin.im>
| author | Eric Warmenhoven <eric@warmenhoven.org> |
|---|---|
| date | Fri, 05 Oct 2001 22:06:27 +0000 |
| parents | 0ba75351a01b |
| children | 5cbe86a444d9 |
| rev | line source |
|---|---|
| 2416 | 1 /* |
| 2 * gaim | |
| 3 * | |
| 4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
| 5 * | |
| 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. | |
| 10 * | |
| 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. | |
| 15 * | |
| 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 | |
| 19 * | |
| 20 */ | |
| 21 | |
| 22 #ifdef HAVE_CONFIG_H | |
| 23 #include "config.h" | |
| 24 #endif | |
| 25 | |
| 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 | |
| 38 #include "gaim.h" | |
| 39 | |
| 40 static gint UI_fd = -1; | |
| 41 struct UI { | |
| 42 GIOChannel *channel; | |
| 43 guint inpa; | |
| 44 }; | |
| 45 GSList *uis = NULL; | |
| 46 | |
|
2435
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
47 gint UI_write(struct UI *ui, guchar *data, gint len) |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
48 { |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
49 guchar *send = g_new0(guchar, len + 6); |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
50 gint sent; |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
51 send[0] = 'f'; |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
52 send[1] = 1; |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
53 memcpy(send + 2, &len, sizeof(len)); |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
54 memcpy(send + 6, data, len); |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
55 /* we'll let the write silently fail because the read will pick it up as dead */ |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
56 g_io_channel_write(ui->channel, send, len + 6, &sent); |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
57 return sent; |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
58 } |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
59 |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
60 void UI_broadcast(guchar *data, gint len) |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
61 { |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
62 GSList *u = uis; |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
63 while (u) { |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
64 struct UI *ui = u->data; |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
65 UI_write(ui, data, len); |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
66 u = u->next; |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
67 } |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
68 } |
|
b4f2a53c0ee5
[gaim-migrate @ 2448]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2430
diff
changeset
|
69 |
| 2416 | 70 static gint gaim_recv(GIOChannel *source, guchar *buf, gint len) |
| 71 { | |
| 72 gint total = 0; | |
| 73 gint cur; | |
| 74 | |
| 75 while (total < len) { | |
| 76 if (g_io_channel_read(source, buf + total, len - total, &cur) != G_IO_ERROR_NONE) | |
| 77 return -1; | |
| 78 if (cur == 0) | |
| 79 return total; | |
| 80 total += cur; | |
| 81 } | |
| 82 | |
| 83 return total; | |
| 84 } | |
| 85 | |
| 86 static gboolean UI_readable(GIOChannel *source, GIOCondition cond, gpointer data) | |
| 87 { | |
| 88 struct UI *ui = data; | |
| 89 | |
| 90 guchar buf[2] = {0, 0}; | |
| 91 guint32 len; | |
| 92 | |
| 93 guchar *in; | |
| 94 | |
| 95 gushort type; | |
| 96 | |
| 97 /* buf[0] is to specify gaim, buf[1] is for protocol version */ | |
| 98 if ((gaim_recv(source, buf, 2) != 2) || (buf[0] != 102) || (buf[1] != 1)) { | |
| 99 debug_printf("UI has abandoned us! (%d %d)\n", buf[0], buf[1]); | |
| 100 uis = g_slist_remove(uis, ui); | |
| 101 g_io_channel_close(ui->channel); | |
| 102 g_source_remove(ui->inpa); | |
| 103 g_free(ui); | |
| 104 return FALSE; | |
| 105 } | |
| 106 | |
| 107 /* no byte order worries! this'll change if we go to TCP */ | |
| 108 if (gaim_recv(source, (guchar *)&len, sizeof(len)) != sizeof(len)) { | |
| 109 debug_printf("UI has abandoned us!\n"); | |
| 110 uis = g_slist_remove(uis, ui); | |
| 111 g_io_channel_close(ui->channel); | |
| 112 g_source_remove(ui->inpa); | |
| 113 g_free(ui); | |
| 114 return FALSE; | |
| 115 } | |
| 116 | |
| 117 in = g_new0(guchar, len + 1); | |
| 118 gaim_recv(source, in, len); | |
| 119 | |
| 120 memcpy(&type, in, sizeof(type)); | |
| 121 switch (type) { | |
| 122 /* | |
| 123 case CUI_TYPE_META: | |
| 124 meta_handler(ui, in); | |
| 125 break; | |
| 126 case CUI_TYPE_PLUGIN: | |
| 127 plugin_handler(ui, in); | |
| 128 break; | |
| 129 case CUI_TYPE_USER: | |
| 130 user_handler(ui, in); | |
| 131 break; | |
| 132 case CUI_TYPE_CONN: | |
| 133 conn_handler(ui, in); | |
| 134 break; | |
| 135 case CUI_TYPE_BUDDY: | |
| 136 buddy_handler(ui, in); | |
| 137 break; | |
| 138 case CUI_TYPE_MESSAGE: | |
| 139 message_handler(ui, in); | |
| 140 break; | |
| 141 case CUI_TYPE_CHAT: | |
| 142 chat_handler(ui, in); | |
| 143 break; | |
| 144 */ | |
| 145 default: | |
| 146 debug_printf("unhandled type %d\n", type); | |
| 147 break; | |
| 148 } | |
| 149 | |
| 150 g_free(in); | |
| 151 return TRUE; | |
| 152 } | |
| 153 | |
| 154 static gboolean socket_readable(GIOChannel *source, GIOCondition cond, gpointer data) | |
| 155 { | |
| 156 struct sockaddr_un saddr; | |
| 157 gint len; | |
| 158 gint fd; | |
| 159 | |
| 160 struct UI *ui; | |
| 161 | |
| 162 if ((fd = accept(UI_fd, (struct sockaddr *)&saddr, &len)) == -1) | |
| 163 return FALSE; | |
| 164 | |
| 165 ui = g_new0(struct UI, 1); | |
| 166 uis = g_slist_append(uis, ui); | |
| 167 | |
| 168 ui->channel = g_io_channel_unix_new(fd); | |
| 169 ui->inpa = g_io_add_watch(ui->channel, G_IO_IN | G_IO_HUP | G_IO_ERR, UI_readable, ui); | |
| 170 g_io_channel_unref(ui->channel); | |
| 171 | |
| 172 debug_printf("got one\n"); | |
| 173 return TRUE; | |
| 174 } | |
| 175 | |
| 176 static gint open_socket() | |
| 177 { | |
| 178 struct sockaddr_un saddr; | |
| 179 gint fd; | |
| 180 | |
| 181 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) { | |
| 182 umask(0177); | |
| 183 saddr.sun_family = AF_UNIX; | |
| 184 g_snprintf(saddr.sun_path, 108, "%s/gaim_%s.%d", | |
| 185 g_get_tmp_dir(), g_get_user_name(), getpid()); | |
| 186 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) != -1) | |
| 187 listen(fd, 100); | |
| 188 else | |
| 189 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
| 190 "Failed to assign %s to a socket (Error: %s)", | |
| 191 saddr.sun_path, strerror(errno)); | |
| 192 } else | |
| 193 g_log(NULL, G_LOG_LEVEL_CRITICAL, "Unable to open socket: %s", strerror(errno)); | |
| 194 return fd; | |
| 195 } | |
| 196 | |
| 197 int core_main() | |
| 198 { | |
| 199 /* | |
| 200 GMainLoop *loop; | |
| 201 */ | |
| 202 | |
| 203 GIOChannel *channel; | |
| 204 | |
| 205 UI_fd = open_socket(); | |
| 206 if (UI_fd < 0) | |
| 207 return 1; | |
| 208 | |
| 209 channel = g_io_channel_unix_new(UI_fd); | |
| 210 g_io_add_watch(channel, G_IO_IN, socket_readable, NULL); | |
| 211 g_io_channel_unref(channel); | |
| 212 | |
| 213 /* | |
| 214 loop = g_main_new(TRUE); | |
| 215 g_main_run(loop); | |
| 216 */ | |
| 217 | |
| 218 return 0; | |
| 219 } | |
| 220 | |
| 221 void core_quit() | |
| 222 { | |
| 223 char buf[1024]; | |
| 224 close(UI_fd); | |
| 225 sprintf(buf, "%s/gaim_%s.%d", g_get_tmp_dir(), g_get_user_name(), getpid()); | |
| 226 unlink(buf); | |
| 227 } |
