Mercurial > pidgin.yaz
annotate src/protocols/msn/servconn.c @ 5897:e3733414ca08
[gaim-migrate @ 6329]
Fixed the MSN segfault on XFR, where we were accessing a servconn that was
destroyed, due to it being the old notification server that was replaced
during XFR. Did that make sense to anybody?
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sun, 15 Jun 2003 19:23:59 +0000 |
parents | 952710ac6635 |
children | 5baeb89ee2d4 |
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 | |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
25 typedef struct |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
26 { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
27 char *command; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
28 MsnMessage *msg; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
29 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
30 } MsnQueueEntry; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
31 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
32 static gboolean |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
33 process_message(MsnServConn *servconn, MsnMessage *msg) |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
34 { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
35 MsnServConnMsgCb cb; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
36 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
37 cb = g_hash_table_lookup(servconn->msg_types, |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
38 msn_message_get_content_type(msg)); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
39 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
40 if (cb == NULL) { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
41 gaim_debug(GAIM_DEBUG_WARNING, "msn", |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
42 "Unhandled content-type '%s': %s\n", |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
43 msn_message_get_content_type(msg), |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
44 msn_message_get_body(msg)); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
45 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
46 return FALSE; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
47 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
48 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
49 cb(servconn, msg); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
50 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
51 return TRUE; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
52 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
53 |
5309 | 54 static gboolean |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
55 process_single_line(MsnServConn *servconn, char *str) |
5309 | 56 { |
5897
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
57 MsnSession *session = servconn->session; |
5309 | 58 MsnServConnCommandCb cb; |
5897
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
59 GSList *l, *l_next = NULL; |
5309 | 60 gboolean result; |
61 size_t param_count = 0; | |
62 char *command, *param_start; | |
63 char **params = NULL; | |
5897
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
64 MsnServConn *old_notification_conn; |
5309 | 65 |
66 command = str; | |
67 | |
5897
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
68 old_notification_conn = session->notification_conn; |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
69 |
5309 | 70 /** |
71 * See how many spaces we have in this. | |
72 */ | |
73 param_start = strchr(command, ' '); | |
74 | |
75 if (param_start != NULL) { | |
76 params = g_strsplit(param_start + 1, " ", 0); | |
77 | |
78 for (param_count = 0; params[param_count] != NULL; param_count++) | |
79 ; | |
80 | |
81 *param_start = '\0'; | |
82 } | |
83 | |
84 cb = g_hash_table_lookup(servconn->commands, command); | |
85 | |
86 if (cb == NULL) { | |
5317
2b437c18719e
[gaim-migrate @ 5689]
Christian Hammond <chipx86@chipx86.com>
parents:
5309
diff
changeset
|
87 cb = g_hash_table_lookup(servconn->commands, "_UNKNOWN_"); |
5309 | 88 |
89 if (cb == NULL) { | |
90 gaim_debug(GAIM_DEBUG_WARNING, "msn", | |
91 "Unhandled command '%s'\n", str); | |
92 | |
93 if (params != NULL) | |
94 g_strfreev(params); | |
95 | |
96 return TRUE; | |
97 } | |
98 } | |
99 | |
100 result = cb(servconn, command, (const char **)params, param_count); | |
101 | |
102 if (params != NULL) | |
103 g_strfreev(params); | |
104 | |
5897
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
105 /* |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
106 * We're checking here if the old notification server was replaced |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
107 * with a new one, and if the current servconn here is the old |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
108 * notification server. If so, we're going to have a bit of trouble |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
109 * in the upcoming loop, as servconn will be a freed variable, so |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
110 * we'll just return early. |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
111 */ |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
112 if (servconn == old_notification_conn && |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
113 old_notification_conn != session->notification_conn) { |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
114 |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
115 return result; |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
116 } |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
117 |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
118 /* Process all queued messages that are waiting on this command. */ |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
119 for (l = servconn->msg_queue; l != NULL; l = l_next) { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
120 MsnQueueEntry *entry = l->data; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
121 MsnMessage *msg; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
122 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
123 l_next = l->next; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
124 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
125 if (entry->command == NULL || |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
126 !g_ascii_strcasecmp(entry->command, command)) { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
127 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
128 MsnUser *sender; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
129 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
130 msg = entry->msg; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
131 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
132 msn_message_ref(msg); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
133 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
134 sender = msn_message_get_sender(msg); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
135 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
136 servconn->msg_passport = g_strdup(msn_user_get_passport(sender)); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
137 servconn->msg_friendly = g_strdup(msn_user_get_name(sender)); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
138 |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
139 process_message(servconn, msg); |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
140 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
141 g_free(servconn->msg_passport); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
142 g_free(servconn->msg_friendly); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
143 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
144 msn_message_destroy(msg); |
5744
6b87c127fe7b
[gaim-migrate @ 6168]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
145 entry->msg = NULL; |
5785
5419c37a6b0c
[gaim-migrate @ 6210]
Christian Hammond <chipx86@chipx86.com>
parents:
5744
diff
changeset
|
146 |
5419c37a6b0c
[gaim-migrate @ 6210]
Christian Hammond <chipx86@chipx86.com>
parents:
5744
diff
changeset
|
147 msn_servconn_unqueue_message(servconn, entry->msg); |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
148 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
149 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
150 |
5309 | 151 return result; |
152 } | |
153 | |
154 static gboolean | |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
155 process_multi_line(MsnServConn *servconn, char *buffer) |
5309 | 156 { |
157 MsnMessage *msg; | |
158 char msg_str[MSN_BUF_LEN]; | |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
159 gboolean result; |
5309 | 160 |
161 g_snprintf(msg_str, sizeof(msg_str), | |
162 "MSG %s %s %d\r\n%s", | |
163 servconn->msg_passport, servconn->msg_friendly, | |
164 servconn->msg_len, buffer); | |
165 | |
5436
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5317
diff
changeset
|
166 gaim_debug(GAIM_DEBUG_MISC, "msn", |
5454
c6efac0e28ff
[gaim-migrate @ 5842]
Christian Hammond <chipx86@chipx86.com>
parents:
5436
diff
changeset
|
167 "Message: {%s}\n", buffer); |
5436
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5317
diff
changeset
|
168 |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
169 msg = msn_message_new_from_str(servconn->session, msg_str); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
170 |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
171 result = process_message(servconn, msg); |
5309 | 172 |
173 msn_message_destroy(msg); | |
174 | |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
175 return result; |
5309 | 176 } |
177 | |
178 static void | |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
179 connect_cb(gpointer data, gint source, GaimInputCondition cond) |
5309 | 180 { |
181 MsnServConn *servconn = data; | |
182 | |
183 if (servconn->connect_cb(data, source, cond)) | |
184 servconn->inpa = gaim_input_add(servconn->fd, GAIM_INPUT_READ, | |
185 servconn->login_cb, data); | |
186 } | |
187 | |
188 MsnServConn * | |
189 msn_servconn_new(MsnSession *session) | |
190 { | |
191 MsnServConn *servconn; | |
192 | |
193 g_return_val_if_fail(session != NULL, NULL); | |
194 | |
195 servconn = g_new0(MsnServConn, 1); | |
196 | |
197 servconn->login_cb = msn_servconn_parse_data; | |
198 servconn->session = session; | |
199 | |
200 servconn->commands = g_hash_table_new_full(g_str_hash, g_str_equal, | |
201 g_free, NULL); | |
202 | |
203 servconn->msg_types = g_hash_table_new_full(g_str_hash, g_str_equal, | |
204 g_free, NULL); | |
205 | |
206 return servconn; | |
207 } | |
208 | |
209 gboolean | |
210 msn_servconn_connect(MsnServConn *servconn) | |
211 { | |
212 int i; | |
213 | |
214 g_return_val_if_fail(servconn != NULL, FALSE); | |
215 g_return_val_if_fail(servconn->server != NULL, FALSE); | |
216 g_return_val_if_fail(!servconn->connected, TRUE); | |
217 | |
5681
46d7ad0dfa26
[gaim-migrate @ 6100]
Christian Hammond <chipx86@chipx86.com>
parents:
5506
diff
changeset
|
218 i = gaim_proxy_connect(servconn->session->account, servconn->server, |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
219 servconn->port, connect_cb, servconn); |
5309 | 220 |
221 if (i == 0) | |
222 servconn->connected = TRUE; | |
223 | |
224 return servconn->connected; | |
225 } | |
226 | |
227 void | |
228 msn_servconn_disconnect(MsnServConn *servconn) | |
229 { | |
230 g_return_if_fail(servconn != NULL); | |
231 g_return_if_fail(servconn->connected); | |
232 | |
233 if (servconn->inpa) | |
234 gaim_input_remove(servconn->inpa); | |
235 | |
5744
6b87c127fe7b
[gaim-migrate @ 6168]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
236 close(servconn->fd); |
6b87c127fe7b
[gaim-migrate @ 6168]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
237 |
5309 | 238 g_free(servconn->rxqueue); |
239 | |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
240 while (servconn->txqueue != NULL) { |
5309 | 241 g_free(servconn->txqueue->data); |
242 | |
243 servconn->txqueue = g_slist_remove(servconn->txqueue, | |
244 servconn->txqueue->data); | |
245 } | |
246 | |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
247 while (servconn->msg_queue != NULL) { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
248 MsnQueueEntry *entry = servconn->msg_queue->data; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
249 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
250 msn_servconn_unqueue_message(servconn, entry->msg); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
251 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
252 |
5309 | 253 servconn->connected = FALSE; |
254 } | |
255 | |
256 void | |
257 msn_servconn_destroy(MsnServConn *servconn) | |
258 { | |
259 g_return_if_fail(servconn != NULL); | |
260 | |
261 if (servconn->connected) | |
262 msn_servconn_disconnect(servconn); | |
263 | |
264 if (servconn->server != NULL) | |
265 g_free(servconn->server); | |
266 | |
267 g_free(servconn); | |
268 } | |
269 | |
270 void | |
271 msn_servconn_set_server(MsnServConn *servconn, const char *server, int port) | |
272 { | |
273 g_return_if_fail(servconn != NULL); | |
274 g_return_if_fail(server != NULL); | |
275 g_return_if_fail(port > 0); | |
276 | |
277 if (servconn->server != NULL) | |
278 g_free(servconn->server); | |
279 | |
280 servconn->server = g_strdup(server); | |
281 servconn->port = port; | |
282 } | |
283 | |
284 const char * | |
285 msn_servconn_get_server(const MsnServConn *servconn) | |
286 { | |
287 g_return_val_if_fail(servconn != NULL, NULL); | |
288 | |
289 return servconn->server; | |
290 } | |
291 | |
292 int | |
293 msn_servconn_get_port(const MsnServConn *servconn) | |
294 { | |
295 g_return_val_if_fail(servconn != NULL, 0); | |
296 | |
297 return servconn->port; | |
298 } | |
299 | |
300 void | |
301 msn_servconn_set_connect_cb(MsnServConn *servconn, | |
302 gboolean (*connect_cb)(gpointer, gint, | |
303 GaimInputCondition)) | |
304 { | |
305 g_return_if_fail(servconn != NULL); | |
306 | |
307 servconn->connect_cb = connect_cb; | |
308 } | |
309 | |
310 void | |
311 msn_servconn_set_failed_read_cb(MsnServConn *servconn, | |
312 void (*failed_read_cb)(gpointer, gint, | |
313 GaimInputCondition)) | |
314 { | |
315 g_return_if_fail(servconn != NULL); | |
316 | |
317 servconn->failed_read_cb = failed_read_cb; | |
318 } | |
319 | |
320 size_t | |
321 msn_servconn_write(MsnServConn *servconn, const char *buf, size_t size) | |
322 { | |
323 g_return_val_if_fail(servconn != NULL, 0); | |
324 | |
325 gaim_debug(GAIM_DEBUG_MISC, "msn", "C: %s%s", buf, | |
326 (*(buf + size - 1) == '\n' ? "" : "\n")); | |
327 | |
328 return write(servconn->fd, buf, size); | |
329 } | |
330 | |
331 gboolean | |
332 msn_servconn_send_command(MsnServConn *servconn, const char *command, | |
333 const char *params) | |
334 { | |
335 char buf[MSN_BUF_LEN]; | |
336 | |
337 g_return_val_if_fail(servconn != NULL, FALSE); | |
338 g_return_val_if_fail(command != NULL, FALSE); | |
339 | |
340 if (params == NULL) | |
341 g_snprintf(buf, sizeof(buf), "%s %u\r\n", command, | |
342 servconn->session->trId++); | |
343 else | |
344 g_snprintf(buf, sizeof(buf), "%s %u %s\r\n", | |
345 command, servconn->session->trId++, params); | |
346 | |
347 return (msn_servconn_write(servconn, buf, strlen(buf)) > 0); | |
348 } | |
349 | |
350 void | |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
351 msn_servconn_queue_message(MsnServConn *servconn, const char *command, |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
352 MsnMessage *msg) |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
353 { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
354 MsnQueueEntry *entry; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
355 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
356 g_return_if_fail(servconn != NULL); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
357 g_return_if_fail(msg != NULL); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
358 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
359 entry = g_new0(MsnQueueEntry, 1); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
360 entry->msg = msg; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
361 entry->command = (command == NULL ? NULL : g_strdup(command)); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
362 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
363 servconn->msg_queue = g_slist_append(servconn->msg_queue, entry); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
364 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
365 msn_message_ref(msg); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
366 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
367 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
368 void |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
369 msn_servconn_unqueue_message(MsnServConn *servconn, MsnMessage *msg) |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
370 { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
371 MsnQueueEntry *entry = NULL; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
372 GSList *l; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
373 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
374 g_return_if_fail(servconn != NULL); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
375 g_return_if_fail(msg != NULL); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
376 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
377 for (l = servconn->msg_queue; l != NULL; l = l->next) { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
378 entry = l->data; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
379 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
380 if (entry->msg == msg) |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
381 break; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
382 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
383 entry = NULL; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
384 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
385 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
386 g_return_if_fail(entry != NULL); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
387 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
388 msn_message_unref(msg); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
389 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
390 servconn->msg_queue = g_slist_remove(servconn->msg_queue, entry); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
391 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
392 if (entry->command != NULL) |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
393 g_free(entry->command); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
394 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
395 g_free(entry); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
396 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
397 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
398 void |
5309 | 399 msn_servconn_register_command(MsnServConn *servconn, const char *command, |
400 MsnServConnCommandCb cb) | |
401 { | |
402 char *command_up; | |
403 | |
404 g_return_if_fail(servconn != NULL); | |
405 g_return_if_fail(command != NULL); | |
406 g_return_if_fail(cb != NULL); | |
407 | |
408 command_up = g_ascii_strup(command, -1); | |
409 | |
410 g_hash_table_insert(servconn->commands, command_up, cb); | |
411 } | |
412 | |
413 void | |
414 msn_servconn_register_msg_type(MsnServConn *servconn, | |
415 const char *content_type, | |
416 MsnServConnMsgCb cb) | |
417 { | |
418 g_return_if_fail(servconn != NULL); | |
419 g_return_if_fail(content_type != NULL); | |
420 g_return_if_fail(cb != NULL); | |
421 | |
422 g_hash_table_insert(servconn->msg_types, g_strdup(content_type), cb); | |
423 } | |
424 | |
425 void | |
426 msn_servconn_parse_data(gpointer data, gint source, GaimInputCondition cond) | |
427 { | |
428 MsnServConn *servconn = (MsnServConn *)data; | |
429 char buf[MSN_BUF_LEN]; | |
430 gboolean cont = TRUE; | |
431 int len; | |
432 | |
433 len = read(servconn->fd, buf, sizeof(buf)); | |
434 | |
435 if (len <= 0) { | |
436 if (servconn->failed_read_cb != NULL) | |
437 servconn->failed_read_cb(data, source, cond); | |
438 | |
439 return; | |
440 } | |
441 | |
442 servconn->rxqueue = g_realloc(servconn->rxqueue, len + servconn->rxlen); | |
443 memcpy(servconn->rxqueue + servconn->rxlen, buf, len); | |
444 servconn->rxlen += len; | |
445 | |
446 while (cont) { | |
447 if (servconn->parsing_msg) { | |
448 char *msg; | |
449 | |
450 if (servconn->rxlen == 0) | |
451 break; | |
452 | |
453 if (servconn->msg_len > servconn->rxlen) | |
454 break; | |
455 | |
456 msg = servconn->rxqueue; | |
457 servconn->rxlen -= servconn->msg_len; | |
458 | |
459 if (servconn->rxlen) { | |
460 servconn->rxqueue = g_memdup(msg + servconn->msg_len, | |
461 servconn->rxlen); | |
462 } | |
463 else { | |
464 servconn->rxqueue = NULL; | |
465 msg = g_realloc(msg, servconn->msg_len + 1); | |
466 } | |
467 | |
468 msg[servconn->msg_len] = '\0'; | |
469 servconn->parsing_msg = FALSE; | |
470 | |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
471 process_multi_line(servconn, msg); |
5309 | 472 |
473 servconn->msg_len = 0; | |
474 g_free(servconn->msg_passport); | |
475 g_free(servconn->msg_friendly); | |
476 g_free(msg); | |
477 } | |
478 else { | |
479 char *end = servconn->rxqueue; | |
480 char *cmd; | |
481 int cmdlen, i; | |
482 | |
483 if (!servconn->rxlen) | |
484 return; | |
485 | |
486 for (i = 0; i < servconn->rxlen - 1; end++, i++) { | |
487 if (*end == '\r' && end[1] == '\n') | |
488 break; | |
489 } | |
490 | |
491 if (i == servconn->rxlen - 1) | |
492 return; | |
493 | |
494 cmdlen = end - servconn->rxqueue + 2; | |
495 cmd = servconn->rxqueue; | |
496 servconn->rxlen -= cmdlen; | |
497 | |
498 if (servconn->rxlen) | |
499 servconn->rxqueue = g_memdup(cmd + cmdlen, servconn->rxlen); | |
500 else { | |
501 servconn->rxqueue = NULL; | |
502 cmd = g_realloc(cmd, cmdlen + 1); | |
503 } | |
504 | |
505 cmd[cmdlen] = '\0'; | |
506 | |
507 gaim_debug(GAIM_DEBUG_MISC, "msn", "S: %s", cmd); | |
508 | |
509 g_strchomp(cmd); | |
510 | |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
511 cont = process_single_line(servconn, cmd); |
5309 | 512 |
513 g_free(cmd); | |
514 } | |
515 } | |
516 } | |
517 |