Mercurial > pidgin
annotate src/protocols/msn/servconn.c @ 7254:bb0160033e50
[gaim-migrate @ 7831]
Oh, I can feel it.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Tue, 14 Oct 2003 03:12:28 +0000 |
parents | bd433a45a463 |
children | ff9127038a5a |
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> | |
6701
b7e113a59b51
[gaim-migrate @ 7227]
Christian Hammond <chipx86@chipx86.com>
parents:
6122
diff
changeset
|
7 * |
5309 | 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; | |
64 | |
65 command = str; | |
66 | |
67 /** | |
68 * See how many spaces we have in this. | |
69 */ | |
70 param_start = strchr(command, ' '); | |
71 | |
72 if (param_start != NULL) { | |
73 params = g_strsplit(param_start + 1, " ", 0); | |
74 | |
75 for (param_count = 0; params[param_count] != NULL; param_count++) | |
76 ; | |
77 | |
78 *param_start = '\0'; | |
79 } | |
80 | |
81 cb = g_hash_table_lookup(servconn->commands, command); | |
82 | |
83 if (cb == NULL) { | |
5317
2b437c18719e
[gaim-migrate @ 5689]
Christian Hammond <chipx86@chipx86.com>
parents:
5309
diff
changeset
|
84 cb = g_hash_table_lookup(servconn->commands, "_UNKNOWN_"); |
5309 | 85 |
86 if (cb == NULL) { | |
87 gaim_debug(GAIM_DEBUG_WARNING, "msn", | |
88 "Unhandled command '%s'\n", str); | |
89 | |
90 if (params != NULL) | |
91 g_strfreev(params); | |
92 | |
93 return TRUE; | |
94 } | |
95 } | |
96 | |
97 result = cb(servconn, command, (const char **)params, param_count); | |
98 | |
99 if (params != NULL) | |
100 g_strfreev(params); | |
101 | |
5898
5baeb89ee2d4
[gaim-migrate @ 6330]
Christian Hammond <chipx86@chipx86.com>
parents:
5897
diff
changeset
|
102 if (g_list_find(session->servconns, servconn) == NULL) |
5897
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
103 return result; |
e3733414ca08
[gaim-migrate @ 6329]
Christian Hammond <chipx86@chipx86.com>
parents:
5793
diff
changeset
|
104 |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
105 /* Process all queued messages that are waiting on this command. */ |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
106 for (l = servconn->msg_queue; l != NULL; l = l_next) { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
107 MsnQueueEntry *entry = l->data; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
108 MsnMessage *msg; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
109 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
110 l_next = l->next; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
111 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
112 if (entry->command == NULL || |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
113 !g_ascii_strcasecmp(entry->command, command)) { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
114 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
115 MsnUser *sender; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
116 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
117 msg = entry->msg; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
118 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
119 msn_message_ref(msg); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
120 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
121 sender = msn_message_get_sender(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 servconn->msg_passport = g_strdup(msn_user_get_passport(sender)); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
124 servconn->msg_friendly = g_strdup(msn_user_get_name(sender)); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
125 |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
126 process_message(servconn, msg); |
5506
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 g_free(servconn->msg_passport); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
129 g_free(servconn->msg_friendly); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
130 |
6122 | 131 msn_servconn_unqueue_message(servconn, entry->msg); |
132 | |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
133 msn_message_destroy(msg); |
5744
6b87c127fe7b
[gaim-migrate @ 6168]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
134 entry->msg = NULL; |
5506
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 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
137 |
5309 | 138 return result; |
139 } | |
140 | |
141 static gboolean | |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
142 process_multi_line(MsnServConn *servconn, char *buffer) |
5309 | 143 { |
144 char msg_str[MSN_BUF_LEN]; | |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
145 gboolean result = TRUE; |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
146 |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
147 if (servconn->multiline_type == MSN_MULTILINE_MSG) { |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
148 MsnMessage *msg; |
5309 | 149 |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
150 g_snprintf(msg_str, sizeof(msg_str), |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
151 "MSG %s %s %d\r\n%s", |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
152 servconn->msg_passport, servconn->msg_friendly, |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
153 servconn->multiline_len, buffer); |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
154 |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
155 gaim_debug(GAIM_DEBUG_MISC, "msn", |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
156 "Message: {%s}\n", buffer); |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
157 |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
158 msg = msn_message_new_from_str(servconn->session, msg_str); |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
159 |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
160 result = process_message(servconn, msg); |
5309 | 161 |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
162 msn_message_destroy(msg); |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
163 } |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
164 else if (servconn->multiline_type == MSN_MULTILINE_IPG) { |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
165 g_snprintf(msg_str, sizeof(msg_str), |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
166 "IPG %d\r\n%s", |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
167 servconn->multiline_len, buffer); |
5436
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5317
diff
changeset
|
168 |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
169 gaim_debug(GAIM_DEBUG_MISC, "msn", |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
170 "Incoming Page: {%s}\n", buffer); |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
171 } |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
172 else if (servconn->multiline_type == MSN_MULTILINE_NOT) { |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
173 g_snprintf(msg_str, sizeof(msg_str), |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
174 "NOT %d\r\n%s", |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
175 servconn->multiline_len, buffer); |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
176 |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
177 gaim_debug(GAIM_DEBUG_MISC, "msn", |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
178 "Notification: {%s}\n", buffer); |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
179 } |
5309 | 180 |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
181 return result; |
5309 | 182 } |
183 | |
184 static void | |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
185 connect_cb(gpointer data, gint source, GaimInputCondition cond) |
5309 | 186 { |
187 MsnServConn *servconn = data; | |
188 | |
189 if (servconn->connect_cb(data, source, cond)) | |
190 servconn->inpa = gaim_input_add(servconn->fd, GAIM_INPUT_READ, | |
191 servconn->login_cb, data); | |
192 } | |
193 | |
194 MsnServConn * | |
195 msn_servconn_new(MsnSession *session) | |
196 { | |
197 MsnServConn *servconn; | |
198 | |
199 g_return_val_if_fail(session != NULL, NULL); | |
200 | |
201 servconn = g_new0(MsnServConn, 1); | |
202 | |
203 servconn->login_cb = msn_servconn_parse_data; | |
204 servconn->session = session; | |
205 | |
206 servconn->commands = g_hash_table_new_full(g_str_hash, g_str_equal, | |
207 g_free, NULL); | |
208 | |
209 servconn->msg_types = g_hash_table_new_full(g_str_hash, g_str_equal, | |
210 g_free, NULL); | |
211 | |
5898
5baeb89ee2d4
[gaim-migrate @ 6330]
Christian Hammond <chipx86@chipx86.com>
parents:
5897
diff
changeset
|
212 session->servconns = g_list_append(session->servconns, servconn); |
5baeb89ee2d4
[gaim-migrate @ 6330]
Christian Hammond <chipx86@chipx86.com>
parents:
5897
diff
changeset
|
213 |
5309 | 214 return servconn; |
215 } | |
216 | |
217 gboolean | |
218 msn_servconn_connect(MsnServConn *servconn) | |
219 { | |
220 int i; | |
221 | |
222 g_return_val_if_fail(servconn != NULL, FALSE); | |
223 g_return_val_if_fail(servconn->server != NULL, FALSE); | |
224 g_return_val_if_fail(!servconn->connected, TRUE); | |
225 | |
5681
46d7ad0dfa26
[gaim-migrate @ 6100]
Christian Hammond <chipx86@chipx86.com>
parents:
5506
diff
changeset
|
226 i = gaim_proxy_connect(servconn->session->account, servconn->server, |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
227 servconn->port, connect_cb, servconn); |
5309 | 228 |
229 if (i == 0) | |
230 servconn->connected = TRUE; | |
231 | |
232 return servconn->connected; | |
233 } | |
234 | |
235 void | |
236 msn_servconn_disconnect(MsnServConn *servconn) | |
237 { | |
238 g_return_if_fail(servconn != NULL); | |
239 g_return_if_fail(servconn->connected); | |
240 | |
241 if (servconn->inpa) | |
242 gaim_input_remove(servconn->inpa); | |
243 | |
5744
6b87c127fe7b
[gaim-migrate @ 6168]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
244 close(servconn->fd); |
6b87c127fe7b
[gaim-migrate @ 6168]
Christian Hammond <chipx86@chipx86.com>
parents:
5681
diff
changeset
|
245 |
6827
1cfbb731aa1f
[gaim-migrate @ 7372]
Christian Hammond <chipx86@chipx86.com>
parents:
6826
diff
changeset
|
246 if (servconn->rxqueue != NULL) |
1cfbb731aa1f
[gaim-migrate @ 7372]
Christian Hammond <chipx86@chipx86.com>
parents:
6826
diff
changeset
|
247 g_free(servconn->rxqueue); |
5309 | 248 |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
249 while (servconn->txqueue != NULL) { |
5309 | 250 g_free(servconn->txqueue->data); |
251 | |
252 servconn->txqueue = g_slist_remove(servconn->txqueue, | |
253 servconn->txqueue->data); | |
254 } | |
255 | |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
256 while (servconn->msg_queue != NULL) { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
257 MsnQueueEntry *entry = servconn->msg_queue->data; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
258 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
259 msn_servconn_unqueue_message(servconn, entry->msg); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
260 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
261 |
5309 | 262 servconn->connected = FALSE; |
263 } | |
264 | |
265 void | |
266 msn_servconn_destroy(MsnServConn *servconn) | |
267 { | |
5898
5baeb89ee2d4
[gaim-migrate @ 6330]
Christian Hammond <chipx86@chipx86.com>
parents:
5897
diff
changeset
|
268 MsnSession *session; |
5baeb89ee2d4
[gaim-migrate @ 6330]
Christian Hammond <chipx86@chipx86.com>
parents:
5897
diff
changeset
|
269 |
5309 | 270 g_return_if_fail(servconn != NULL); |
271 | |
5898
5baeb89ee2d4
[gaim-migrate @ 6330]
Christian Hammond <chipx86@chipx86.com>
parents:
5897
diff
changeset
|
272 session = servconn->session; |
5baeb89ee2d4
[gaim-migrate @ 6330]
Christian Hammond <chipx86@chipx86.com>
parents:
5897
diff
changeset
|
273 |
5baeb89ee2d4
[gaim-migrate @ 6330]
Christian Hammond <chipx86@chipx86.com>
parents:
5897
diff
changeset
|
274 session->servconns = g_list_remove(session->servconns, servconn); |
5baeb89ee2d4
[gaim-migrate @ 6330]
Christian Hammond <chipx86@chipx86.com>
parents:
5897
diff
changeset
|
275 |
5309 | 276 if (servconn->connected) |
277 msn_servconn_disconnect(servconn); | |
278 | |
279 if (servconn->server != NULL) | |
280 g_free(servconn->server); | |
281 | |
282 g_free(servconn); | |
283 } | |
284 | |
285 void | |
286 msn_servconn_set_server(MsnServConn *servconn, const char *server, int port) | |
287 { | |
288 g_return_if_fail(servconn != NULL); | |
289 g_return_if_fail(server != NULL); | |
290 g_return_if_fail(port > 0); | |
291 | |
292 if (servconn->server != NULL) | |
293 g_free(servconn->server); | |
294 | |
295 servconn->server = g_strdup(server); | |
296 servconn->port = port; | |
297 } | |
298 | |
299 const char * | |
300 msn_servconn_get_server(const MsnServConn *servconn) | |
301 { | |
302 g_return_val_if_fail(servconn != NULL, NULL); | |
303 | |
304 return servconn->server; | |
305 } | |
306 | |
307 int | |
308 msn_servconn_get_port(const MsnServConn *servconn) | |
309 { | |
310 g_return_val_if_fail(servconn != NULL, 0); | |
311 | |
312 return servconn->port; | |
313 } | |
314 | |
315 void | |
316 msn_servconn_set_connect_cb(MsnServConn *servconn, | |
317 gboolean (*connect_cb)(gpointer, gint, | |
318 GaimInputCondition)) | |
319 { | |
320 g_return_if_fail(servconn != NULL); | |
321 | |
322 servconn->connect_cb = connect_cb; | |
323 } | |
324 | |
325 void | |
326 msn_servconn_set_failed_read_cb(MsnServConn *servconn, | |
327 void (*failed_read_cb)(gpointer, gint, | |
328 GaimInputCondition)) | |
329 { | |
330 g_return_if_fail(servconn != NULL); | |
331 | |
332 servconn->failed_read_cb = failed_read_cb; | |
333 } | |
334 | |
335 size_t | |
336 msn_servconn_write(MsnServConn *servconn, const char *buf, size_t size) | |
337 { | |
338 g_return_val_if_fail(servconn != NULL, 0); | |
339 | |
340 gaim_debug(GAIM_DEBUG_MISC, "msn", "C: %s%s", buf, | |
341 (*(buf + size - 1) == '\n' ? "" : "\n")); | |
342 | |
343 return write(servconn->fd, buf, size); | |
344 } | |
345 | |
346 gboolean | |
347 msn_servconn_send_command(MsnServConn *servconn, const char *command, | |
348 const char *params) | |
349 { | |
350 char buf[MSN_BUF_LEN]; | |
351 | |
352 g_return_val_if_fail(servconn != NULL, FALSE); | |
353 g_return_val_if_fail(command != NULL, FALSE); | |
354 | |
355 if (params == NULL) | |
356 g_snprintf(buf, sizeof(buf), "%s %u\r\n", command, | |
357 servconn->session->trId++); | |
358 else | |
359 g_snprintf(buf, sizeof(buf), "%s %u %s\r\n", | |
360 command, servconn->session->trId++, params); | |
361 | |
362 return (msn_servconn_write(servconn, buf, strlen(buf)) > 0); | |
363 } | |
364 | |
365 void | |
5506
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
366 msn_servconn_queue_message(MsnServConn *servconn, const char *command, |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
367 MsnMessage *msg) |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
368 { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
369 MsnQueueEntry *entry; |
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 g_return_if_fail(servconn != NULL); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
372 g_return_if_fail(msg != NULL); |
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 entry = g_new0(MsnQueueEntry, 1); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
375 entry->msg = msg; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
376 entry->command = (command == NULL ? NULL : g_strdup(command)); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
377 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
378 servconn->msg_queue = g_slist_append(servconn->msg_queue, entry); |
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 msn_message_ref(msg); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
381 } |
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 void |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
384 msn_servconn_unqueue_message(MsnServConn *servconn, MsnMessage *msg) |
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 MsnQueueEntry *entry = NULL; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
387 GSList *l; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
388 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
389 g_return_if_fail(servconn != NULL); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
390 g_return_if_fail(msg != NULL); |
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 for (l = servconn->msg_queue; l != NULL; l = l->next) { |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
393 entry = l->data; |
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 if (entry->msg == msg) |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
396 break; |
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 entry = NULL; |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
399 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
400 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
401 g_return_if_fail(entry != NULL); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
402 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
403 msn_message_unref(msg); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
404 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
405 servconn->msg_queue = g_slist_remove(servconn->msg_queue, entry); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
406 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
407 if (entry->command != NULL) |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
408 g_free(entry->command); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
409 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
410 g_free(entry); |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
411 } |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
412 |
4f72b611f0ee
[gaim-migrate @ 5905]
Christian Hammond <chipx86@chipx86.com>
parents:
5454
diff
changeset
|
413 void |
5309 | 414 msn_servconn_register_command(MsnServConn *servconn, const char *command, |
415 MsnServConnCommandCb cb) | |
416 { | |
417 char *command_up; | |
418 | |
419 g_return_if_fail(servconn != NULL); | |
420 g_return_if_fail(command != NULL); | |
421 g_return_if_fail(cb != NULL); | |
422 | |
423 command_up = g_ascii_strup(command, -1); | |
424 | |
425 g_hash_table_insert(servconn->commands, command_up, cb); | |
426 } | |
427 | |
428 void | |
429 msn_servconn_register_msg_type(MsnServConn *servconn, | |
430 const char *content_type, | |
431 MsnServConnMsgCb cb) | |
432 { | |
433 g_return_if_fail(servconn != NULL); | |
434 g_return_if_fail(content_type != NULL); | |
435 g_return_if_fail(cb != NULL); | |
436 | |
437 g_hash_table_insert(servconn->msg_types, g_strdup(content_type), cb); | |
438 } | |
439 | |
440 void | |
441 msn_servconn_parse_data(gpointer data, gint source, GaimInputCondition cond) | |
442 { | |
443 MsnServConn *servconn = (MsnServConn *)data; | |
5899
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
444 MsnSession *session = servconn->session; |
5309 | 445 char buf[MSN_BUF_LEN]; |
446 gboolean cont = TRUE; | |
447 int len; | |
448 | |
449 len = read(servconn->fd, buf, sizeof(buf)); | |
450 | |
451 if (len <= 0) { | |
452 if (servconn->failed_read_cb != NULL) | |
453 servconn->failed_read_cb(data, source, cond); | |
454 | |
455 return; | |
456 } | |
457 | |
458 servconn->rxqueue = g_realloc(servconn->rxqueue, len + servconn->rxlen); | |
459 memcpy(servconn->rxqueue + servconn->rxlen, buf, len); | |
460 servconn->rxlen += len; | |
461 | |
462 while (cont) { | |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
463 if (servconn->parsing_multiline) { |
5309 | 464 char *msg; |
465 | |
466 if (servconn->rxlen == 0) | |
467 break; | |
468 | |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
469 if (servconn->multiline_len > servconn->rxlen) |
5309 | 470 break; |
471 | |
472 msg = servconn->rxqueue; | |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
473 servconn->rxlen -= servconn->multiline_len; |
5309 | 474 |
475 if (servconn->rxlen) { | |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
476 servconn->rxqueue = g_memdup(msg + servconn->multiline_len, |
5309 | 477 servconn->rxlen); |
478 } | |
479 else { | |
480 servconn->rxqueue = NULL; | |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
481 msg = g_realloc(msg, servconn->multiline_len + 1); |
5309 | 482 } |
483 | |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
484 msg[servconn->multiline_len] = '\0'; |
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
485 servconn->parsing_multiline = FALSE; |
5309 | 486 |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
487 process_multi_line(servconn, msg); |
5309 | 488 |
5899
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
489 if (g_list_find(session->servconns, servconn) != NULL) { |
5962
115769a4c6fc
[gaim-migrate @ 6409]
Christian Hammond <chipx86@chipx86.com>
parents:
5899
diff
changeset
|
490 servconn->multiline_len = 0; |
5899
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
491 |
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
492 if (servconn->msg_passport != NULL) |
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
493 g_free(servconn->msg_passport); |
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
494 |
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
495 if (servconn->msg_friendly != NULL) |
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
496 g_free(servconn->msg_friendly); |
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
497 } |
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
498 else |
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
499 cont = 0; |
349204c9a709
[gaim-migrate @ 6331]
Christian Hammond <chipx86@chipx86.com>
parents:
5898
diff
changeset
|
500 |
5309 | 501 g_free(msg); |
502 } | |
503 else { | |
504 char *end = servconn->rxqueue; | |
505 char *cmd; | |
506 int cmdlen, i; | |
507 | |
508 if (!servconn->rxlen) | |
509 return; | |
510 | |
511 for (i = 0; i < servconn->rxlen - 1; end++, i++) { | |
512 if (*end == '\r' && end[1] == '\n') | |
513 break; | |
514 } | |
515 | |
516 if (i == servconn->rxlen - 1) | |
517 return; | |
518 | |
519 cmdlen = end - servconn->rxqueue + 2; | |
520 cmd = servconn->rxqueue; | |
521 servconn->rxlen -= cmdlen; | |
522 | |
523 if (servconn->rxlen) | |
524 servconn->rxqueue = g_memdup(cmd + cmdlen, servconn->rxlen); | |
525 else { | |
526 servconn->rxqueue = NULL; | |
527 cmd = g_realloc(cmd, cmdlen + 1); | |
528 } | |
529 | |
530 cmd[cmdlen] = '\0'; | |
531 | |
532 gaim_debug(GAIM_DEBUG_MISC, "msn", "S: %s", cmd); | |
533 | |
534 g_strchomp(cmd); | |
535 | |
5793
952710ac6635
[gaim-migrate @ 6218]
Christian Hammond <chipx86@chipx86.com>
parents:
5785
diff
changeset
|
536 cont = process_single_line(servconn, cmd); |
5309 | 537 |
538 g_free(cmd); | |
539 } | |
540 } | |
541 } | |
542 |