comparison src/protocols/msn/servconn.c @ 5309:e2e53316a21d

[gaim-migrate @ 5681] Announcing the new MSN prpl! It probably has some bugs, and for the time being, there is no file transfer. That's good though, because the current MSN file transfer is a little broken. I've had many corrupted files. I'll commit new file transfer code when it's written. I want this heavily tested before 0.63! If you use MSN, please talk to people on it. Let me know of any oddities, crashes, bugs, whatever. I'll fix things as I find them. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Tue, 06 May 2003 02:06:56 +0000
parents
children 2b437c18719e
comparison
equal deleted inserted replaced
5308:6aa785e55d0f 5309:e2e53316a21d
1 /**
2 * @file servconn.c Server connection functions
3 *
4 * gaim
5 *
6 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 #include "msn.h"
23 #include "servconn.h"
24
25 static gboolean
26 __process_single_line(MsnServConn *servconn, char *str)
27 {
28 MsnServConnCommandCb cb;
29 gboolean result;
30 size_t param_count = 0;
31 char *command, *param_start;
32 char **params = NULL;
33
34 command = str;
35
36 /**
37 * See how many spaces we have in this.
38 */
39 param_start = strchr(command, ' ');
40
41 if (param_start != NULL) {
42 params = g_strsplit(param_start + 1, " ", 0);
43
44 for (param_count = 0; params[param_count] != NULL; param_count++)
45 ;
46
47 *param_start = '\0';
48 }
49
50 cb = g_hash_table_lookup(servconn->commands, command);
51
52 if (cb == NULL) {
53 cb = g_hash_table_lookup(servconn->commands, "_unknown_");
54
55 if (cb == NULL) {
56 gaim_debug(GAIM_DEBUG_WARNING, "msn",
57 "Unhandled command '%s'\n", str);
58
59 if (params != NULL)
60 g_strfreev(params);
61
62 return TRUE;
63 }
64 }
65
66 result = cb(servconn, command, (const char **)params, param_count);
67
68 if (params != NULL)
69 g_strfreev(params);
70
71 return result;
72 }
73
74 static gboolean
75 __process_multi_line(MsnServConn *servconn, char *buffer)
76 {
77 MsnServConnMsgCb cb;
78 MsnMessage *msg;
79 char msg_str[MSN_BUF_LEN];
80
81 g_snprintf(msg_str, sizeof(msg_str),
82 "MSG %s %s %d\r\n%s",
83 servconn->msg_passport, servconn->msg_friendly,
84 servconn->msg_len, buffer);
85
86 msg = msn_message_new_from_str(servconn->session, msg_str);
87
88 cb = g_hash_table_lookup(servconn->msg_types,
89 msn_message_get_content_type(msg));
90
91 if (cb == NULL) {
92 gaim_debug(GAIM_DEBUG_WARNING, "msn",
93 "Unhandled content-type '%s': %s\n",
94 msn_message_get_content_type(msg),
95 msn_message_get_body(msg));
96
97 msn_message_destroy(msg);
98
99 return FALSE;
100 }
101
102 cb(servconn, msg);
103
104 msn_message_destroy(msg);
105
106 return TRUE;
107 }
108
109 static void
110 __connect_cb(gpointer data, gint source, GaimInputCondition cond)
111 {
112 MsnServConn *servconn = data;
113
114 if (servconn->connect_cb(data, source, cond))
115 servconn->inpa = gaim_input_add(servconn->fd, GAIM_INPUT_READ,
116 servconn->login_cb, data);
117 }
118
119 MsnServConn *
120 msn_servconn_new(MsnSession *session)
121 {
122 MsnServConn *servconn;
123
124 g_return_val_if_fail(session != NULL, NULL);
125
126 servconn = g_new0(MsnServConn, 1);
127
128 servconn->login_cb = msn_servconn_parse_data;
129 servconn->session = session;
130
131 servconn->commands = g_hash_table_new_full(g_str_hash, g_str_equal,
132 g_free, NULL);
133
134 servconn->msg_types = g_hash_table_new_full(g_str_hash, g_str_equal,
135 g_free, NULL);
136
137 return servconn;
138 }
139
140 gboolean
141 msn_servconn_connect(MsnServConn *servconn)
142 {
143 int i;
144
145 g_return_val_if_fail(servconn != NULL, FALSE);
146 g_return_val_if_fail(servconn->server != NULL, FALSE);
147 g_return_val_if_fail(!servconn->connected, TRUE);
148
149 i = proxy_connect(servconn->session->account, servconn->server,
150 servconn->port, __connect_cb, servconn);
151
152 if (i == 0)
153 servconn->connected = TRUE;
154
155 return servconn->connected;
156 }
157
158 void
159 msn_servconn_disconnect(MsnServConn *servconn)
160 {
161 g_return_if_fail(servconn != NULL);
162 g_return_if_fail(servconn->connected);
163
164 close(servconn->fd);
165
166 if (servconn->inpa)
167 gaim_input_remove(servconn->inpa);
168
169 g_free(servconn->rxqueue);
170
171 while (servconn->txqueue) {
172 g_free(servconn->txqueue->data);
173
174 servconn->txqueue = g_slist_remove(servconn->txqueue,
175 servconn->txqueue->data);
176 }
177
178 servconn->connected = FALSE;
179 }
180
181 void
182 msn_servconn_destroy(MsnServConn *servconn)
183 {
184 g_return_if_fail(servconn != NULL);
185
186 if (servconn->connected)
187 msn_servconn_disconnect(servconn);
188
189 if (servconn->server != NULL)
190 g_free(servconn->server);
191
192 g_free(servconn);
193 }
194
195 void
196 msn_servconn_set_server(MsnServConn *servconn, const char *server, int port)
197 {
198 g_return_if_fail(servconn != NULL);
199 g_return_if_fail(server != NULL);
200 g_return_if_fail(port > 0);
201
202 if (servconn->server != NULL)
203 g_free(servconn->server);
204
205 servconn->server = g_strdup(server);
206 servconn->port = port;
207 }
208
209 const char *
210 msn_servconn_get_server(const MsnServConn *servconn)
211 {
212 g_return_val_if_fail(servconn != NULL, NULL);
213
214 return servconn->server;
215 }
216
217 int
218 msn_servconn_get_port(const MsnServConn *servconn)
219 {
220 g_return_val_if_fail(servconn != NULL, 0);
221
222 return servconn->port;
223 }
224
225 void
226 msn_servconn_set_connect_cb(MsnServConn *servconn,
227 gboolean (*connect_cb)(gpointer, gint,
228 GaimInputCondition))
229 {
230 g_return_if_fail(servconn != NULL);
231
232 servconn->connect_cb = connect_cb;
233 }
234
235 void
236 msn_servconn_set_failed_read_cb(MsnServConn *servconn,
237 void (*failed_read_cb)(gpointer, gint,
238 GaimInputCondition))
239 {
240 g_return_if_fail(servconn != NULL);
241
242 servconn->failed_read_cb = failed_read_cb;
243 }
244
245 size_t
246 msn_servconn_write(MsnServConn *servconn, const char *buf, size_t size)
247 {
248 g_return_val_if_fail(servconn != NULL, 0);
249
250 gaim_debug(GAIM_DEBUG_MISC, "msn", "C: %s%s", buf,
251 (*(buf + size - 1) == '\n' ? "" : "\n"));
252
253 return write(servconn->fd, buf, size);
254 }
255
256 gboolean
257 msn_servconn_send_command(MsnServConn *servconn, const char *command,
258 const char *params)
259 {
260 char buf[MSN_BUF_LEN];
261
262 g_return_val_if_fail(servconn != NULL, FALSE);
263 g_return_val_if_fail(command != NULL, FALSE);
264
265 if (params == NULL)
266 g_snprintf(buf, sizeof(buf), "%s %u\r\n", command,
267 servconn->session->trId++);
268 else
269 g_snprintf(buf, sizeof(buf), "%s %u %s\r\n",
270 command, servconn->session->trId++, params);
271
272 return (msn_servconn_write(servconn, buf, strlen(buf)) > 0);
273 }
274
275 void
276 msn_servconn_register_command(MsnServConn *servconn, const char *command,
277 MsnServConnCommandCb cb)
278 {
279 char *command_up;
280
281 g_return_if_fail(servconn != NULL);
282 g_return_if_fail(command != NULL);
283 g_return_if_fail(cb != NULL);
284
285 command_up = g_ascii_strup(command, -1);
286
287 g_hash_table_insert(servconn->commands, command_up, cb);
288 }
289
290 void
291 msn_servconn_register_msg_type(MsnServConn *servconn,
292 const char *content_type,
293 MsnServConnMsgCb cb)
294 {
295 g_return_if_fail(servconn != NULL);
296 g_return_if_fail(content_type != NULL);
297 g_return_if_fail(cb != NULL);
298
299 g_hash_table_insert(servconn->msg_types, g_strdup(content_type), cb);
300 }
301
302 void
303 msn_servconn_parse_data(gpointer data, gint source, GaimInputCondition cond)
304 {
305 MsnServConn *servconn = (MsnServConn *)data;
306 char buf[MSN_BUF_LEN];
307 gboolean cont = TRUE;
308 int len;
309
310 len = read(servconn->fd, buf, sizeof(buf));
311
312 if (len <= 0) {
313 if (servconn->failed_read_cb != NULL)
314 servconn->failed_read_cb(data, source, cond);
315
316 return;
317 }
318
319 servconn->rxqueue = g_realloc(servconn->rxqueue, len + servconn->rxlen);
320 memcpy(servconn->rxqueue + servconn->rxlen, buf, len);
321 servconn->rxlen += len;
322
323 while (cont) {
324 if (servconn->parsing_msg) {
325 char *msg;
326
327 if (servconn->rxlen == 0)
328 break;
329
330 if (servconn->msg_len > servconn->rxlen)
331 break;
332
333 msg = servconn->rxqueue;
334 servconn->rxlen -= servconn->msg_len;
335
336 if (servconn->rxlen) {
337 servconn->rxqueue = g_memdup(msg + servconn->msg_len,
338 servconn->rxlen);
339 }
340 else {
341 servconn->rxqueue = NULL;
342 msg = g_realloc(msg, servconn->msg_len + 1);
343 }
344
345 msg[servconn->msg_len] = '\0';
346 servconn->parsing_msg = FALSE;
347
348 __process_multi_line(servconn, msg);
349
350 servconn->msg_len = 0;
351 g_free(servconn->msg_passport);
352 g_free(servconn->msg_friendly);
353 g_free(msg);
354 }
355 else {
356 char *end = servconn->rxqueue;
357 char *cmd;
358 int cmdlen, i;
359
360 if (!servconn->rxlen)
361 return;
362
363 for (i = 0; i < servconn->rxlen - 1; end++, i++) {
364 if (*end == '\r' && end[1] == '\n')
365 break;
366 }
367
368 if (i == servconn->rxlen - 1)
369 return;
370
371 cmdlen = end - servconn->rxqueue + 2;
372 cmd = servconn->rxqueue;
373 servconn->rxlen -= cmdlen;
374
375 if (servconn->rxlen)
376 servconn->rxqueue = g_memdup(cmd + cmdlen, servconn->rxlen);
377 else {
378 servconn->rxqueue = NULL;
379 cmd = g_realloc(cmd, cmdlen + 1);
380 }
381
382 cmd[cmdlen] = '\0';
383
384 gaim_debug(GAIM_DEBUG_MISC, "msn", "S: %s", cmd);
385
386 g_strchomp(cmd);
387
388 cont = __process_single_line(servconn, cmd);
389
390 g_free(cmd);
391 }
392 }
393 }
394