Mercurial > pidgin
annotate src/protocols/msn/servconn.c @ 5440:7e8524b5ff98
[gaim-migrate @ 5822]
Here's the core of the new prefs system. Nothing actually uses this yet,
but that will come in time.
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sun, 18 May 2003 21:53:41 +0000 |
parents | ad445074d239 |
children | c6efac0e28ff |
rev | line source |
---|---|
5309 | 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) { | |
5317
2b437c18719e
[gaim-migrate @ 5689]
Christian Hammond <chipx86@chipx86.com>
parents:
5309
diff
changeset
|
53 cb = g_hash_table_lookup(servconn->commands, "_UNKNOWN_"); |
5309 | 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 | |
5436
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5317
diff
changeset
|
102 gaim_debug(GAIM_DEBUG_MISC, "msn", |
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5317
diff
changeset
|
103 "Message: {%s}\n", msn_message_get_body(msg)); |
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5317
diff
changeset
|
104 |
5309 | 105 cb(servconn, msg); |
106 | |
107 msn_message_destroy(msg); | |
108 | |
109 return TRUE; | |
110 } | |
111 | |
112 static void | |
113 __connect_cb(gpointer data, gint source, GaimInputCondition cond) | |
114 { | |
115 MsnServConn *servconn = data; | |
116 | |
117 if (servconn->connect_cb(data, source, cond)) | |
118 servconn->inpa = gaim_input_add(servconn->fd, GAIM_INPUT_READ, | |
119 servconn->login_cb, data); | |
120 } | |
121 | |
122 MsnServConn * | |
123 msn_servconn_new(MsnSession *session) | |
124 { | |
125 MsnServConn *servconn; | |
126 | |
127 g_return_val_if_fail(session != NULL, NULL); | |
128 | |
129 servconn = g_new0(MsnServConn, 1); | |
130 | |
131 servconn->login_cb = msn_servconn_parse_data; | |
132 servconn->session = session; | |
133 | |
134 servconn->commands = g_hash_table_new_full(g_str_hash, g_str_equal, | |
135 g_free, NULL); | |
136 | |
137 servconn->msg_types = g_hash_table_new_full(g_str_hash, g_str_equal, | |
138 g_free, NULL); | |
139 | |
140 return servconn; | |
141 } | |
142 | |
143 gboolean | |
144 msn_servconn_connect(MsnServConn *servconn) | |
145 { | |
146 int i; | |
147 | |
148 g_return_val_if_fail(servconn != NULL, FALSE); | |
149 g_return_val_if_fail(servconn->server != NULL, FALSE); | |
150 g_return_val_if_fail(!servconn->connected, TRUE); | |
151 | |
152 i = proxy_connect(servconn->session->account, servconn->server, | |
153 servconn->port, __connect_cb, servconn); | |
154 | |
155 if (i == 0) | |
156 servconn->connected = TRUE; | |
157 | |
158 return servconn->connected; | |
159 } | |
160 | |
161 void | |
162 msn_servconn_disconnect(MsnServConn *servconn) | |
163 { | |
164 g_return_if_fail(servconn != NULL); | |
165 g_return_if_fail(servconn->connected); | |
166 | |
167 close(servconn->fd); | |
168 | |
169 if (servconn->inpa) | |
170 gaim_input_remove(servconn->inpa); | |
171 | |
172 g_free(servconn->rxqueue); | |
173 | |
174 while (servconn->txqueue) { | |
175 g_free(servconn->txqueue->data); | |
176 | |
177 servconn->txqueue = g_slist_remove(servconn->txqueue, | |
178 servconn->txqueue->data); | |
179 } | |
180 | |
181 servconn->connected = FALSE; | |
182 } | |
183 | |
184 void | |
185 msn_servconn_destroy(MsnServConn *servconn) | |
186 { | |
187 g_return_if_fail(servconn != NULL); | |
188 | |
189 if (servconn->connected) | |
190 msn_servconn_disconnect(servconn); | |
191 | |
192 if (servconn->server != NULL) | |
193 g_free(servconn->server); | |
194 | |
195 g_free(servconn); | |
196 } | |
197 | |
198 void | |
199 msn_servconn_set_server(MsnServConn *servconn, const char *server, int port) | |
200 { | |
201 g_return_if_fail(servconn != NULL); | |
202 g_return_if_fail(server != NULL); | |
203 g_return_if_fail(port > 0); | |
204 | |
205 if (servconn->server != NULL) | |
206 g_free(servconn->server); | |
207 | |
208 servconn->server = g_strdup(server); | |
209 servconn->port = port; | |
210 } | |
211 | |
212 const char * | |
213 msn_servconn_get_server(const MsnServConn *servconn) | |
214 { | |
215 g_return_val_if_fail(servconn != NULL, NULL); | |
216 | |
217 return servconn->server; | |
218 } | |
219 | |
220 int | |
221 msn_servconn_get_port(const MsnServConn *servconn) | |
222 { | |
223 g_return_val_if_fail(servconn != NULL, 0); | |
224 | |
225 return servconn->port; | |
226 } | |
227 | |
228 void | |
229 msn_servconn_set_connect_cb(MsnServConn *servconn, | |
230 gboolean (*connect_cb)(gpointer, gint, | |
231 GaimInputCondition)) | |
232 { | |
233 g_return_if_fail(servconn != NULL); | |
234 | |
235 servconn->connect_cb = connect_cb; | |
236 } | |
237 | |
238 void | |
239 msn_servconn_set_failed_read_cb(MsnServConn *servconn, | |
240 void (*failed_read_cb)(gpointer, gint, | |
241 GaimInputCondition)) | |
242 { | |
243 g_return_if_fail(servconn != NULL); | |
244 | |
245 servconn->failed_read_cb = failed_read_cb; | |
246 } | |
247 | |
248 size_t | |
249 msn_servconn_write(MsnServConn *servconn, const char *buf, size_t size) | |
250 { | |
251 g_return_val_if_fail(servconn != NULL, 0); | |
252 | |
253 gaim_debug(GAIM_DEBUG_MISC, "msn", "C: %s%s", buf, | |
254 (*(buf + size - 1) == '\n' ? "" : "\n")); | |
255 | |
256 return write(servconn->fd, buf, size); | |
257 } | |
258 | |
259 gboolean | |
260 msn_servconn_send_command(MsnServConn *servconn, const char *command, | |
261 const char *params) | |
262 { | |
263 char buf[MSN_BUF_LEN]; | |
264 | |
265 g_return_val_if_fail(servconn != NULL, FALSE); | |
266 g_return_val_if_fail(command != NULL, FALSE); | |
267 | |
268 if (params == NULL) | |
269 g_snprintf(buf, sizeof(buf), "%s %u\r\n", command, | |
270 servconn->session->trId++); | |
271 else | |
272 g_snprintf(buf, sizeof(buf), "%s %u %s\r\n", | |
273 command, servconn->session->trId++, params); | |
274 | |
275 return (msn_servconn_write(servconn, buf, strlen(buf)) > 0); | |
276 } | |
277 | |
278 void | |
279 msn_servconn_register_command(MsnServConn *servconn, const char *command, | |
280 MsnServConnCommandCb cb) | |
281 { | |
282 char *command_up; | |
283 | |
284 g_return_if_fail(servconn != NULL); | |
285 g_return_if_fail(command != NULL); | |
286 g_return_if_fail(cb != NULL); | |
287 | |
288 command_up = g_ascii_strup(command, -1); | |
289 | |
290 g_hash_table_insert(servconn->commands, command_up, cb); | |
291 } | |
292 | |
293 void | |
294 msn_servconn_register_msg_type(MsnServConn *servconn, | |
295 const char *content_type, | |
296 MsnServConnMsgCb cb) | |
297 { | |
298 g_return_if_fail(servconn != NULL); | |
299 g_return_if_fail(content_type != NULL); | |
300 g_return_if_fail(cb != NULL); | |
301 | |
302 g_hash_table_insert(servconn->msg_types, g_strdup(content_type), cb); | |
303 } | |
304 | |
305 void | |
306 msn_servconn_parse_data(gpointer data, gint source, GaimInputCondition cond) | |
307 { | |
308 MsnServConn *servconn = (MsnServConn *)data; | |
309 char buf[MSN_BUF_LEN]; | |
310 gboolean cont = TRUE; | |
311 int len; | |
312 | |
313 len = read(servconn->fd, buf, sizeof(buf)); | |
314 | |
315 if (len <= 0) { | |
316 if (servconn->failed_read_cb != NULL) | |
317 servconn->failed_read_cb(data, source, cond); | |
318 | |
319 return; | |
320 } | |
321 | |
322 servconn->rxqueue = g_realloc(servconn->rxqueue, len + servconn->rxlen); | |
323 memcpy(servconn->rxqueue + servconn->rxlen, buf, len); | |
324 servconn->rxlen += len; | |
325 | |
326 while (cont) { | |
327 if (servconn->parsing_msg) { | |
328 char *msg; | |
329 | |
330 if (servconn->rxlen == 0) | |
331 break; | |
332 | |
333 if (servconn->msg_len > servconn->rxlen) | |
334 break; | |
335 | |
336 msg = servconn->rxqueue; | |
337 servconn->rxlen -= servconn->msg_len; | |
338 | |
339 if (servconn->rxlen) { | |
340 servconn->rxqueue = g_memdup(msg + servconn->msg_len, | |
341 servconn->rxlen); | |
342 } | |
343 else { | |
344 servconn->rxqueue = NULL; | |
345 msg = g_realloc(msg, servconn->msg_len + 1); | |
346 } | |
347 | |
348 msg[servconn->msg_len] = '\0'; | |
349 servconn->parsing_msg = FALSE; | |
350 | |
351 __process_multi_line(servconn, msg); | |
352 | |
353 servconn->msg_len = 0; | |
354 g_free(servconn->msg_passport); | |
355 g_free(servconn->msg_friendly); | |
356 g_free(msg); | |
357 } | |
358 else { | |
359 char *end = servconn->rxqueue; | |
360 char *cmd; | |
361 int cmdlen, i; | |
362 | |
363 if (!servconn->rxlen) | |
364 return; | |
365 | |
366 for (i = 0; i < servconn->rxlen - 1; end++, i++) { | |
367 if (*end == '\r' && end[1] == '\n') | |
368 break; | |
369 } | |
370 | |
371 if (i == servconn->rxlen - 1) | |
372 return; | |
373 | |
374 cmdlen = end - servconn->rxqueue + 2; | |
375 cmd = servconn->rxqueue; | |
376 servconn->rxlen -= cmdlen; | |
377 | |
378 if (servconn->rxlen) | |
379 servconn->rxqueue = g_memdup(cmd + cmdlen, servconn->rxlen); | |
380 else { | |
381 servconn->rxqueue = NULL; | |
382 cmd = g_realloc(cmd, cmdlen + 1); | |
383 } | |
384 | |
385 cmd[cmdlen] = '\0'; | |
386 | |
387 gaim_debug(GAIM_DEBUG_MISC, "msn", "S: %s", cmd); | |
388 | |
389 g_strchomp(cmd); | |
390 | |
391 cont = __process_single_line(servconn, cmd); | |
392 | |
393 g_free(cmd); | |
394 } | |
395 } | |
396 } | |
397 |