Mercurial > pidgin
annotate src/core.c @ 2438:5cbe86a444d9
[gaim-migrate @ 2451]
more updates to core.
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Sat, 06 Oct 2001 00:10:29 +0000 |
parents | b4f2a53c0ee5 |
children | 932adc1ac9ed |
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 |
2438
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
70 static gint gaim_recv(GIOChannel *source, void *buf, gint len) |
2416 | 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 | |
2438
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
90 guchar type; |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
91 guchar subtype; |
2416 | 92 guint32 len; |
93 | |
94 guchar *in; | |
95 | |
96 /* no byte order worries! this'll change if we go to TCP */ | |
2438
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
97 if (gaim_recv(source, &type, sizeof(type)) != sizeof(type)) { |
2416 | 98 debug_printf("UI has abandoned us!\n"); |
99 uis = g_slist_remove(uis, ui); | |
100 g_io_channel_close(ui->channel); | |
101 g_source_remove(ui->inpa); | |
102 g_free(ui); | |
103 return FALSE; | |
104 } | |
105 | |
2438
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
106 if (gaim_recv(source, &subtype, sizeof(subtype)) != sizeof(subtype)) { |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
107 debug_printf("UI has abandoned us!\n"); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
108 uis = g_slist_remove(uis, ui); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
109 g_io_channel_close(ui->channel); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
110 g_source_remove(ui->inpa); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
111 g_free(ui); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
112 return FALSE; |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
113 } |
2416 | 114 |
2438
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
115 if (gaim_recv(source, &len, sizeof(len)) != sizeof(len)) { |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
116 debug_printf("UI has abandoned us!\n"); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
117 uis = g_slist_remove(uis, ui); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
118 g_io_channel_close(ui->channel); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
119 g_source_remove(ui->inpa); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
120 g_free(ui); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
121 return FALSE; |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
122 } |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
123 |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
124 in = g_new0(guchar, len); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
125 if (gaim_recv(source, in, len) != len) { |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
126 debug_printf("UI has abandoned us!\n"); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
127 uis = g_slist_remove(uis, ui); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
128 g_io_channel_close(ui->channel); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
129 g_source_remove(ui->inpa); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
130 g_free(ui); |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
131 return FALSE; |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
132 } |
5cbe86a444d9
[gaim-migrate @ 2451]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2435
diff
changeset
|
133 |
2416 | 134 switch (type) { |
135 /* | |
136 case CUI_TYPE_META: | |
137 meta_handler(ui, in); | |
138 break; | |
139 case CUI_TYPE_PLUGIN: | |
140 plugin_handler(ui, in); | |
141 break; | |
142 case CUI_TYPE_USER: | |
143 user_handler(ui, in); | |
144 break; | |
145 case CUI_TYPE_CONN: | |
146 conn_handler(ui, in); | |
147 break; | |
148 case CUI_TYPE_BUDDY: | |
149 buddy_handler(ui, in); | |
150 break; | |
151 case CUI_TYPE_MESSAGE: | |
152 message_handler(ui, in); | |
153 break; | |
154 case CUI_TYPE_CHAT: | |
155 chat_handler(ui, in); | |
156 break; | |
157 */ | |
158 default: | |
159 debug_printf("unhandled type %d\n", type); | |
160 break; | |
161 } | |
162 | |
163 g_free(in); | |
164 return TRUE; | |
165 } | |
166 | |
167 static gboolean socket_readable(GIOChannel *source, GIOCondition cond, gpointer data) | |
168 { | |
169 struct sockaddr_un saddr; | |
170 gint len; | |
171 gint fd; | |
172 | |
173 struct UI *ui; | |
174 | |
175 if ((fd = accept(UI_fd, (struct sockaddr *)&saddr, &len)) == -1) | |
176 return FALSE; | |
177 | |
178 ui = g_new0(struct UI, 1); | |
179 uis = g_slist_append(uis, ui); | |
180 | |
181 ui->channel = g_io_channel_unix_new(fd); | |
182 ui->inpa = g_io_add_watch(ui->channel, G_IO_IN | G_IO_HUP | G_IO_ERR, UI_readable, ui); | |
183 g_io_channel_unref(ui->channel); | |
184 | |
185 debug_printf("got one\n"); | |
186 return TRUE; | |
187 } | |
188 | |
189 static gint open_socket() | |
190 { | |
191 struct sockaddr_un saddr; | |
192 gint fd; | |
193 | |
194 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) { | |
195 umask(0177); | |
196 saddr.sun_family = AF_UNIX; | |
197 g_snprintf(saddr.sun_path, 108, "%s/gaim_%s.%d", | |
198 g_get_tmp_dir(), g_get_user_name(), getpid()); | |
199 if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) != -1) | |
200 listen(fd, 100); | |
201 else | |
202 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
203 "Failed to assign %s to a socket (Error: %s)", | |
204 saddr.sun_path, strerror(errno)); | |
205 } else | |
206 g_log(NULL, G_LOG_LEVEL_CRITICAL, "Unable to open socket: %s", strerror(errno)); | |
207 return fd; | |
208 } | |
209 | |
210 int core_main() | |
211 { | |
212 /* | |
213 GMainLoop *loop; | |
214 */ | |
215 | |
216 GIOChannel *channel; | |
217 | |
218 UI_fd = open_socket(); | |
219 if (UI_fd < 0) | |
220 return 1; | |
221 | |
222 channel = g_io_channel_unix_new(UI_fd); | |
223 g_io_add_watch(channel, G_IO_IN, socket_readable, NULL); | |
224 g_io_channel_unref(channel); | |
225 | |
226 /* | |
227 loop = g_main_new(TRUE); | |
228 g_main_run(loop); | |
229 */ | |
230 | |
231 return 0; | |
232 } | |
233 | |
234 void core_quit() | |
235 { | |
236 char buf[1024]; | |
237 close(UI_fd); | |
238 sprintf(buf, "%s/gaim_%s.%d", g_get_tmp_dir(), g_get_user_name(), getpid()); | |
239 unlink(buf); | |
240 } |