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