14192
|
1 /**
|
|
2 * @file simple.c
|
|
3 *
|
|
4 * gaim
|
|
5 *
|
|
6 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
|
|
7 *
|
|
8 * ***
|
|
9 * Thanks to Google's Summer of Code Program and the helpful mentors
|
|
10 * ***
|
|
11 *
|
|
12 * This program is free software; you can redistribute it and/or modify
|
|
13 * it under the terms of the GNU General Public License as published by
|
|
14 * the Free Software Foundation; either version 2 of the License, or
|
|
15 * (at your option) any later version.
|
|
16 *
|
|
17 * This program is distributed in the hope that it will be useful,
|
|
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 * GNU General Public License for more details.
|
|
21 *
|
|
22 * You should have received a copy of the GNU General Public License
|
|
23 * along with this program; if not, write to the Free Software
|
|
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
25 */
|
|
26
|
|
27 #include "internal.h"
|
|
28
|
|
29 #include "accountopt.h"
|
|
30 #include "blist.h"
|
|
31 #include "conversation.h"
|
|
32 #include "dnsquery.h"
|
|
33 #include "debug.h"
|
|
34 #include "notify.h"
|
|
35 #include "privacy.h"
|
|
36 #include "prpl.h"
|
|
37 #include "plugin.h"
|
|
38 #include "util.h"
|
|
39 #include "version.h"
|
|
40 #include "network.h"
|
|
41 #include "xmlnode.h"
|
|
42
|
|
43 #include "simple.h"
|
|
44 #include "sipmsg.h"
|
|
45 #include "dnssrv.h"
|
|
46 #include "ntlm.h"
|
|
47
|
|
48 static char *gentag() {
|
|
49 return g_strdup_printf("%04d%04d", rand() & 0xFFFF, rand() & 0xFFFF);
|
|
50 }
|
|
51
|
|
52 static char *genbranch() {
|
|
53 return g_strdup_printf("z9hG4bK%04X%04X%04X%04X%04X",
|
|
54 rand() & 0xFFFF, rand() & 0xFFFF, rand() & 0xFFFF,
|
|
55 rand() & 0xFFFF, rand() & 0xFFFF);
|
|
56 }
|
|
57
|
|
58 static char *gencallid() {
|
|
59 return g_strdup_printf("%04Xg%04Xa%04Xi%04Xm%04Xt%04Xb%04Xx%04Xx",
|
|
60 rand() & 0xFFFF, rand() & 0xFFFF, rand() & 0xFFFF,
|
|
61 rand() & 0xFFFF, rand() & 0xFFFF, rand() & 0xFFFF,
|
|
62 rand() & 0xFFFF, rand() & 0xFFFF);
|
|
63 }
|
|
64
|
|
65 static const char *simple_list_icon(GaimAccount *a, GaimBuddy *b) {
|
|
66 return "simple";
|
|
67 }
|
|
68
|
|
69 static void simple_keep_alive(GaimConnection *gc) {
|
|
70 struct simple_account_data *sip = gc->proto_data;
|
|
71 if(sip->udp) { /* in case of UDP send a packet only with a 0 byte to
|
|
72 remain in the NAT table */
|
|
73 gchar buf[2] = {0, 0};
|
|
74 gaim_debug_info("simple", "sending keep alive\n");
|
|
75 sendto(sip->fd, buf, 1, 0, (struct sockaddr*)&sip->serveraddr, sizeof(struct sockaddr_in));
|
|
76 }
|
|
77 return;
|
|
78 }
|
|
79
|
|
80 static gboolean process_register_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc);
|
|
81 static void send_notify(struct simple_account_data *sip, struct simple_watcher *);
|
|
82
|
|
83 static void send_publish(struct simple_account_data *sip);
|
|
84
|
|
85 static void do_notifies(struct simple_account_data *sip) {
|
|
86 GSList *tmp = sip->watcher;
|
|
87 gaim_debug_info("simple", "do_notifies()\n");
|
|
88 if((sip->republish != -1) || sip->republish < time(NULL)) {
|
|
89 if(gaim_account_get_bool(sip->account, "dopublish", TRUE)) {
|
|
90 send_publish(sip);
|
|
91 }
|
|
92 }
|
|
93
|
|
94 while(tmp) {
|
|
95 gaim_debug_info("simple", "notifying %s\n", ((struct simple_watcher*)tmp->data)->name);
|
|
96 send_notify(sip, tmp->data);
|
|
97 tmp = tmp->next;
|
|
98 }
|
|
99 }
|
|
100
|
|
101 static void simple_set_status(GaimAccount *account, GaimStatus *status) {
|
|
102 GaimStatusPrimitive primitive = gaim_status_type_get_primitive(gaim_status_get_type(status));
|
|
103 struct simple_account_data *sip = NULL;
|
|
104
|
|
105 if (!gaim_status_is_active(status))
|
|
106 return;
|
|
107
|
|
108 if (account->gc)
|
|
109 sip = account->gc->proto_data;
|
|
110
|
|
111 if (sip)
|
|
112 {
|
|
113 g_free(sip->status);
|
|
114 if (primitive == GAIM_STATUS_AVAILABLE)
|
|
115 sip->status = g_strdup("available");
|
|
116 else
|
|
117 sip->status = g_strdup("busy");
|
|
118
|
|
119 do_notifies(sip);
|
|
120 }
|
|
121 }
|
|
122
|
|
123 static struct sip_connection *connection_find(struct simple_account_data *sip, int fd) {
|
|
124 struct sip_connection *ret = NULL;
|
|
125 GSList *entry = sip->openconns;
|
|
126 while(entry) {
|
|
127 ret = entry->data;
|
|
128 if(ret->fd == fd) return ret;
|
|
129 entry = entry->next;
|
|
130 }
|
|
131 return NULL;
|
|
132 }
|
|
133
|
|
134 static struct simple_watcher *watcher_find(struct simple_account_data *sip,
|
|
135 const gchar *name) {
|
|
136 struct simple_watcher *watcher;
|
|
137 GSList *entry = sip->watcher;
|
|
138 while(entry) {
|
|
139 watcher = entry->data;
|
|
140 if(!strcmp(name, watcher->name)) return watcher;
|
|
141 entry = entry->next;
|
|
142 }
|
|
143 return NULL;
|
|
144 }
|
|
145
|
|
146 static struct simple_watcher *watcher_create(struct simple_account_data *sip,
|
|
147 const gchar *name, const gchar *callid, const gchar *ourtag,
|
|
148 const gchar *theirtag, gboolean needsxpidf) {
|
|
149 struct simple_watcher *watcher = g_new0(struct simple_watcher, 1);
|
|
150 watcher->name = g_strdup(name);
|
|
151 watcher->dialog.callid = g_strdup(callid);
|
|
152 watcher->dialog.ourtag = g_strdup(ourtag);
|
|
153 watcher->dialog.theirtag = g_strdup(theirtag);
|
|
154 watcher->needsxpidf = needsxpidf;
|
|
155 sip->watcher = g_slist_append(sip->watcher, watcher);
|
|
156 return watcher;
|
|
157 }
|
|
158
|
|
159 static void watcher_remove(struct simple_account_data *sip, const gchar *name) {
|
|
160 struct simple_watcher *watcher = watcher_find(sip, name);
|
|
161 sip->watcher = g_slist_remove(sip->watcher, watcher);
|
|
162 g_free(watcher->name);
|
|
163 g_free(watcher->dialog.callid);
|
|
164 g_free(watcher->dialog.ourtag);
|
|
165 g_free(watcher->dialog.theirtag);
|
|
166 g_free(watcher);
|
|
167 }
|
|
168
|
|
169 static struct sip_connection *connection_create(struct simple_account_data *sip, int fd) {
|
|
170 struct sip_connection *ret = g_new0(struct sip_connection, 1);
|
|
171 ret->fd = fd;
|
|
172 sip->openconns = g_slist_append(sip->openconns, ret);
|
|
173 return ret;
|
|
174 }
|
|
175
|
|
176 static void connection_remove(struct simple_account_data *sip, int fd) {
|
|
177 struct sip_connection *conn = connection_find(sip, fd);
|
|
178 sip->openconns = g_slist_remove(sip->openconns, conn);
|
|
179 if(conn->inputhandler) gaim_input_remove(conn->inputhandler);
|
|
180 g_free(conn->inbuf);
|
|
181 g_free(conn);
|
|
182 }
|
|
183
|
|
184 static void connection_free_all(struct simple_account_data *sip) {
|
|
185 struct sip_connection *ret = NULL;
|
|
186 GSList *entry = sip->openconns;
|
|
187 while(entry) {
|
|
188 ret = entry->data;
|
|
189 connection_remove(sip, ret->fd);
|
|
190 entry = sip->openconns;
|
|
191 }
|
|
192 }
|
|
193
|
|
194 static void simple_add_buddy(GaimConnection *gc, GaimBuddy *buddy, GaimGroup *group)
|
|
195 {
|
|
196 struct simple_account_data *sip = (struct simple_account_data *)gc->proto_data;
|
|
197 struct simple_buddy *b;
|
|
198 if(strncmp("sip:", buddy->name, 4)) {
|
|
199 gchar *buf = g_strdup_printf("sip:%s", buddy->name);
|
|
200 gaim_blist_rename_buddy(buddy, buf);
|
|
201 g_free(buf);
|
|
202 }
|
|
203 if(!g_hash_table_lookup(sip->buddies, buddy->name)) {
|
|
204 b = g_new0(struct simple_buddy, 1);
|
|
205 gaim_debug_info("simple", "simple_add_buddy %s\n", buddy->name);
|
|
206 b->name = g_strdup(buddy->name);
|
|
207 g_hash_table_insert(sip->buddies, b->name, b);
|
|
208 } else {
|
|
209 gaim_debug_info("simple", "buddy %s already in internal list\n", buddy->name);
|
|
210 }
|
|
211 }
|
|
212
|
|
213 static void simple_get_buddies(GaimConnection *gc) {
|
|
214 GaimBlistNode *gnode, *cnode, *bnode;
|
|
215
|
|
216 gaim_debug_info("simple", "simple_get_buddies\n");
|
|
217
|
|
218 for(gnode = gaim_get_blist()->root; gnode; gnode = gnode->next) {
|
|
219 if(!GAIM_BLIST_NODE_IS_GROUP(gnode)) continue;
|
|
220 for(cnode = gnode->child; cnode; cnode = cnode->next) {
|
|
221 if(!GAIM_BLIST_NODE_IS_CONTACT(cnode)) continue;
|
|
222 for(bnode = cnode->child; bnode; bnode = bnode->next) {
|
|
223 if(!GAIM_BLIST_NODE_IS_BUDDY(bnode)) continue;
|
|
224 if(((GaimBuddy*)bnode)->account == gc->account)
|
|
225 simple_add_buddy(gc, (GaimBuddy*)bnode, (GaimGroup *)gnode);
|
|
226 }
|
|
227 }
|
|
228 }
|
|
229 }
|
|
230
|
|
231 static void simple_remove_buddy(GaimConnection *gc, GaimBuddy *buddy, GaimGroup *group)
|
|
232 {
|
|
233 struct simple_account_data *sip = (struct simple_account_data *)gc->proto_data;
|
|
234 struct simple_buddy *b = g_hash_table_lookup(sip->buddies, buddy->name);
|
|
235 g_hash_table_remove(sip->buddies, buddy->name);
|
|
236 g_free(b->name);
|
|
237 g_free(b);
|
|
238 }
|
|
239
|
|
240 static GList *simple_status_types(GaimAccount *acc) {
|
|
241 GaimStatusType *type;
|
|
242 GList *types = NULL;
|
|
243
|
|
244 type = gaim_status_type_new_with_attrs(
|
|
245 GAIM_STATUS_AVAILABLE, NULL, NULL, TRUE, TRUE, FALSE,
|
|
246 "message", _("Message"), gaim_value_new(GAIM_TYPE_STRING),
|
|
247 NULL);
|
|
248 types = g_list_append(types, type);
|
|
249
|
|
250 type = gaim_status_type_new_full(
|
|
251 GAIM_STATUS_OFFLINE, NULL, NULL, TRUE, TRUE, FALSE);
|
|
252 types = g_list_append(types, type);
|
|
253
|
|
254 return types;
|
|
255 }
|
|
256
|
|
257 static gchar *auth_header(struct simple_account_data *sip,
|
|
258 struct sip_auth *auth, const gchar *method, const gchar *target) {
|
|
259 gchar noncecount[9];
|
|
260 gchar *response;
|
|
261 gchar *ret;
|
|
262 gchar *tmp;
|
|
263 const char *authdomain;
|
|
264 const char *authuser;
|
|
265
|
|
266 authdomain = gaim_account_get_string(sip->account, "authdomain", "");
|
|
267 authuser = gaim_account_get_string(sip->account, "authuser", sip->username);
|
|
268
|
|
269 if(!authuser || strlen(authuser) < 1) {
|
|
270 authuser = sip->username;
|
|
271 }
|
|
272
|
|
273 if(auth->type == 1) { /* Digest */
|
|
274 sprintf(noncecount, "%08d", auth->nc++);
|
|
275 response = gaim_cipher_http_digest_calculate_response(
|
|
276 "md5", method, target, NULL, NULL,
|
|
277 auth->nonce, noncecount, NULL, auth->digest_session_key);
|
|
278 gaim_debug(GAIM_DEBUG_MISC, "simple", "response %s\n", response);
|
|
279
|
|
280 ret = g_strdup_printf("Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=\"%s\", response=\"%s\"\r\n", authuser, auth->realm, auth->nonce, target, noncecount, response);
|
|
281 g_free(response);
|
|
282 return ret;
|
|
283 } else if(auth->type == 2) { /* NTLM */
|
|
284 if(auth->nc == 3 && auth->nonce) {
|
|
285 /* TODO: Don't hardcode "gaim" as the hostname */
|
|
286 ret = gaim_ntlm_gen_type3(authuser, sip->password, "gaim", authdomain, (const guint8 *)auth->nonce, &auth->flags);
|
|
287 tmp = g_strdup_printf("NTLM qop=\"auth\", opaque=\"%s\", realm=\"%s\", targetname=\"%s\", gssapi-data=\"%s\"\r\n", auth->opaque, auth->realm, auth->target, ret);
|
|
288 g_free(ret);
|
|
289 return tmp;
|
|
290 }
|
|
291 tmp = g_strdup_printf("NTLM qop=\"auth\", realm=\"%s\", targetname=\"%s\", gssapi-data=\"\"\r\n", auth->realm, auth->target);
|
|
292 return tmp;
|
|
293 }
|
|
294
|
|
295 sprintf(noncecount, "%08d", auth->nc++);
|
|
296 response = gaim_cipher_http_digest_calculate_response(
|
|
297 "md5", method, target, NULL, NULL,
|
|
298 auth->nonce, noncecount, NULL, auth->digest_session_key);
|
|
299 gaim_debug(GAIM_DEBUG_MISC, "simple", "response %s\n", response);
|
|
300
|
|
301 ret = g_strdup_printf("Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=\"%s\", response=\"%s\"\r\n", authuser, auth->realm, auth->nonce, target, noncecount, response);
|
|
302 g_free(response);
|
|
303 return ret;
|
|
304 }
|
|
305
|
|
306 static char *parse_attribute(const char *attrname, const char *source) {
|
|
307 const char *tmp, *tmp2;
|
|
308 char *retval = NULL;
|
|
309 int len = strlen(attrname);
|
|
310
|
|
311 if(!strncmp(source, attrname, len)) {
|
|
312 tmp = source + len;
|
|
313 tmp2 = g_strstr_len(tmp, strlen(tmp), "\"");
|
|
314 if(tmp2)
|
|
315 retval = g_strndup(tmp, tmp2 - tmp);
|
|
316 else
|
|
317 retval = g_strdup(tmp);
|
|
318 }
|
|
319
|
|
320 return retval;
|
|
321 }
|
|
322
|
|
323 static void fill_auth(struct simple_account_data *sip, gchar *hdr, struct sip_auth *auth) {
|
|
324 int i = 0;
|
|
325 const char *authuser;
|
|
326 char *tmp;
|
|
327 gchar **parts;
|
|
328
|
|
329 authuser = gaim_account_get_string(sip->account, "authuser", sip->username);
|
|
330
|
|
331 if(!authuser || strlen(authuser) < 1) {
|
|
332 authuser = sip->username;
|
|
333 }
|
|
334
|
|
335 if(!hdr) {
|
|
336 gaim_debug_error("simple", "fill_auth: hdr==NULL\n");
|
|
337 return;
|
|
338 }
|
|
339
|
|
340 if(!g_strncasecmp(hdr, "NTLM", 4)) {
|
|
341 gaim_debug_info("simple", "found NTLM\n");
|
|
342 auth->type = 2;
|
|
343 parts = g_strsplit(hdr+5, "\", ", 0);
|
|
344 i = 0;
|
|
345 while(parts[i]) {
|
|
346 gaim_debug_info("simple", "parts[i] %s\n", parts[i]);
|
|
347 if((tmp = parse_attribute("gssapi-data=\"", parts[i]))) {
|
|
348 auth->nonce = g_memdup(gaim_ntlm_parse_type2(tmp, &auth->flags), 8);
|
|
349 g_free(tmp);
|
|
350 }
|
|
351 if((tmp = parse_attribute("targetname=\"",
|
|
352 parts[i]))) {
|
|
353 auth->target = tmp;
|
|
354 }
|
|
355 else if((tmp = parse_attribute("realm=\"",
|
|
356 parts[i]))) {
|
|
357 auth->realm = tmp;
|
|
358 }
|
|
359 else if((tmp = parse_attribute("opaque=\"", parts[i]))) {
|
|
360 auth->opaque = tmp;
|
|
361 }
|
|
362 i++;
|
|
363 }
|
|
364 g_strfreev(parts);
|
|
365 auth->nc = 1;
|
|
366 if(!strstr(hdr, "gssapi-data")) {
|
|
367 auth->nc = 1;
|
|
368 } else {
|
|
369 auth->nc = 3;
|
|
370 }
|
|
371 return;
|
|
372 }
|
|
373
|
|
374 auth->type = 1;
|
|
375 parts = g_strsplit(hdr, " ", 0);
|
|
376 while(parts[i]) {
|
|
377 if((tmp = parse_attribute("nonce=\"", parts[i]))) {
|
|
378 auth->nonce = tmp;
|
|
379 }
|
|
380 else if((tmp = parse_attribute("realm=\"", parts[i]))) {
|
|
381 auth->realm = tmp;
|
|
382 }
|
|
383 i++;
|
|
384 }
|
|
385 g_strfreev(parts);
|
|
386
|
|
387 gaim_debug(GAIM_DEBUG_MISC, "simple", "nonce: %s realm: %s\n", auth->nonce ? auth->nonce : "(null)", auth->realm ? auth->realm : "(null)");
|
|
388 if(auth->realm) {
|
|
389 auth->digest_session_key = gaim_cipher_http_digest_calculate_session_key(
|
|
390 "md5", authuser, auth->realm, sip->password, auth->nonce, NULL);
|
|
391
|
|
392 auth->nc = 1;
|
|
393 }
|
|
394 }
|
|
395
|
|
396 static void simple_canwrite_cb(gpointer data, gint source, GaimInputCondition cond) {
|
|
397 GaimConnection *gc = data;
|
|
398 struct simple_account_data *sip = gc->proto_data;
|
|
399 gsize max_write;
|
|
400 gssize written;
|
|
401
|
|
402 max_write = gaim_circ_buffer_get_max_read(sip->txbuf);
|
|
403
|
|
404 if(max_write == 0) {
|
|
405 gaim_input_remove(sip->tx_handler);
|
|
406 sip->tx_handler = 0;
|
|
407 return;
|
|
408 }
|
|
409
|
|
410 written = write(sip->fd, sip->txbuf->outptr, max_write);
|
|
411
|
|
412 if(written < 0 && errno == EAGAIN)
|
|
413 written = 0;
|
|
414 else if(written <= 0) {
|
|
415 /*TODO: do we really want to disconnect on a failure to write?*/
|
|
416 gaim_connection_error(gc, _("Could not write"));
|
|
417 return;
|
|
418 }
|
|
419
|
|
420 gaim_circ_buffer_mark_read(sip->txbuf, written);
|
|
421 }
|
|
422
|
|
423 static void simple_input_cb(gpointer data, gint source, GaimInputCondition cond);
|
|
424
|
|
425 static void send_later_cb(gpointer data, gint source, const gchar *error) {
|
|
426 GaimConnection *gc = data;
|
|
427 struct simple_account_data *sip;
|
|
428 struct sip_connection *conn;
|
|
429
|
|
430 if (!GAIM_CONNECTION_IS_VALID(gc))
|
|
431 {
|
|
432 if (source >= 0)
|
|
433 close(source);
|
|
434 return;
|
|
435 }
|
|
436
|
|
437 if(source < 0) {
|
|
438 gaim_connection_error(gc, _("Could not connect"));
|
|
439 return;
|
|
440 }
|
|
441
|
|
442 sip = gc->proto_data;
|
|
443 sip->fd = source;
|
|
444 sip->connecting = FALSE;
|
|
445
|
|
446 simple_canwrite_cb(gc, sip->fd, GAIM_INPUT_WRITE);
|
|
447
|
|
448 /* If there is more to write now, we need to register a handler */
|
|
449 if(sip->txbuf->bufused > 0)
|
|
450 sip->tx_handler = gaim_input_add(sip->fd, GAIM_INPUT_WRITE,
|
|
451 simple_canwrite_cb, gc);
|
|
452
|
|
453 conn = connection_create(sip, source);
|
|
454 conn->inputhandler = gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_input_cb, gc);
|
|
455 }
|
|
456
|
|
457
|
|
458 static void sendlater(GaimConnection *gc, const char *buf) {
|
|
459 struct simple_account_data *sip = gc->proto_data;
|
14262
|
460 GaimProxyConnectData *connect_data;
|
14192
|
461
|
|
462 if(!sip->connecting) {
|
|
463 gaim_debug_info("simple", "connecting to %s port %d\n", sip->realhostname ? sip->realhostname : "{NULL}", sip->realport);
|
14262
|
464 connect_data = gaim_proxy_connect(sip->account, sip->realhostname, sip->realport, send_later_cb, gc);
|
|
465 if(connect_data == NULL) {
|
14192
|
466 gaim_connection_error(gc, _("Couldn't create socket"));
|
|
467 }
|
|
468 sip->connecting = TRUE;
|
|
469 }
|
|
470
|
|
471 if(gaim_circ_buffer_get_max_read(sip->txbuf) > 0)
|
|
472 gaim_circ_buffer_append(sip->txbuf, "\r\n", 2);
|
|
473
|
|
474 gaim_circ_buffer_append(sip->txbuf, buf, strlen(buf));
|
|
475 }
|
|
476
|
|
477 static void sendout_pkt(GaimConnection *gc, const char *buf) {
|
|
478 struct simple_account_data *sip = gc->proto_data;
|
|
479 time_t currtime = time(NULL);
|
|
480 int writelen = strlen(buf);
|
|
481
|
|
482 gaim_debug(GAIM_DEBUG_MISC, "simple", "\n\nsending - %s\n######\n%s\n######\n\n", ctime(&currtime), buf);
|
|
483 if(sip->udp) {
|
|
484 if(sendto(sip->fd, buf, writelen, 0, (struct sockaddr*)&sip->serveraddr, sizeof(struct sockaddr_in)) < writelen) {
|
|
485 gaim_debug_info("simple", "could not send packet\n");
|
|
486 }
|
|
487 } else {
|
|
488 int ret;
|
|
489 if(sip->fd < 0) {
|
|
490 sendlater(gc, buf);
|
|
491 return;
|
|
492 }
|
|
493
|
|
494 if(sip->tx_handler) {
|
|
495 ret = -1;
|
|
496 errno = EAGAIN;
|
|
497 } else
|
|
498 ret = write(sip->fd, buf, writelen);
|
|
499
|
|
500 if (ret < 0 && errno == EAGAIN)
|
|
501 ret = 0;
|
|
502 else if(ret <= 0) { /* XXX: When does this happen legitimately? */
|
|
503 sendlater(gc, buf);
|
|
504 return;
|
|
505 }
|
|
506
|
|
507 if (ret < writelen) {
|
|
508 if(!sip->tx_handler)
|
|
509 sip->tx_handler = gaim_input_add(sip->fd,
|
|
510 GAIM_INPUT_WRITE, simple_canwrite_cb,
|
|
511 gc);
|
|
512
|
|
513 /* XXX: is it OK to do this? You might get part of a request sent
|
|
514 with part of another. */
|
|
515 if(sip->txbuf->bufused > 0)
|
|
516 gaim_circ_buffer_append(sip->txbuf, "\r\n", 2);
|
|
517
|
|
518 gaim_circ_buffer_append(sip->txbuf, buf + ret,
|
|
519 writelen - ret);
|
|
520 }
|
|
521 }
|
|
522 }
|
|
523
|
|
524 static void sendout_sipmsg(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
525 GSList *tmp = msg->headers;
|
|
526 gchar *name;
|
|
527 gchar *value;
|
|
528 GString *outstr = g_string_new("");
|
|
529 g_string_append_printf(outstr, "%s %s SIP/2.0\r\n", msg->method, msg->target);
|
|
530 while(tmp) {
|
|
531 name = ((struct siphdrelement*) (tmp->data))->name;
|
|
532 value = ((struct siphdrelement*) (tmp->data))->value;
|
|
533 g_string_append_printf(outstr, "%s: %s\r\n", name, value);
|
|
534 tmp = g_slist_next(tmp);
|
|
535 }
|
|
536 g_string_append_printf(outstr, "\r\n%s", msg->body ? msg->body : "");
|
|
537 sendout_pkt(sip->gc, outstr->str);
|
|
538 g_string_free(outstr, TRUE);
|
|
539 }
|
|
540
|
|
541 static void send_sip_response(GaimConnection *gc, struct sipmsg *msg, int code,
|
|
542 const char *text, const char *body) {
|
|
543 GSList *tmp = msg->headers;
|
|
544 gchar *name;
|
|
545 gchar *value;
|
|
546 GString *outstr = g_string_new("");
|
|
547
|
|
548 /* When sending the acknowlegements and errors, the content length from the original
|
|
549 message is still here, but there is no body; we need to make sure we're sending the
|
|
550 correct content length */
|
|
551 sipmsg_remove_header(msg, "Content-Length");
|
|
552 if(body) {
|
|
553 gchar len[12];
|
|
554 sprintf(len, "%" G_GSIZE_FORMAT , strlen(body));
|
|
555 sipmsg_add_header(msg, "Content-Length", len);
|
|
556 }
|
|
557 else
|
|
558 sipmsg_add_header(msg, "Content-Length", "0");
|
|
559 g_string_append_printf(outstr, "SIP/2.0 %d %s\r\n", code, text);
|
|
560 while(tmp) {
|
|
561 name = ((struct siphdrelement*) (tmp->data))->name;
|
|
562 value = ((struct siphdrelement*) (tmp->data))->value;
|
|
563
|
|
564 g_string_append_printf(outstr, "%s: %s\r\n", name, value);
|
|
565 tmp = g_slist_next(tmp);
|
|
566 }
|
|
567 g_string_append_printf(outstr, "\r\n%s", body ? body : "");
|
|
568 sendout_pkt(gc, outstr->str);
|
|
569 g_string_free(outstr, TRUE);
|
|
570 }
|
|
571
|
|
572 static void transactions_remove(struct simple_account_data *sip, struct transaction *trans) {
|
|
573 if(trans->msg) sipmsg_free(trans->msg);
|
|
574 sip->transactions = g_slist_remove(sip->transactions, trans);
|
|
575 g_free(trans);
|
|
576 }
|
|
577
|
|
578 static void transactions_add_buf(struct simple_account_data *sip, const gchar *buf, void *callback) {
|
|
579 struct transaction *trans = g_new0(struct transaction, 1);
|
|
580 trans->time = time(NULL);
|
|
581 trans->msg = sipmsg_parse_msg(buf);
|
|
582 trans->cseq = sipmsg_find_header(trans->msg, "CSeq");
|
|
583 trans->callback = callback;
|
|
584 sip->transactions = g_slist_append(sip->transactions, trans);
|
|
585 }
|
|
586
|
|
587 static struct transaction *transactions_find(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
588 struct transaction *trans;
|
|
589 GSList *transactions = sip->transactions;
|
|
590 gchar *cseq = sipmsg_find_header(msg, "CSeq");
|
|
591
|
|
592 while(transactions) {
|
|
593 trans = transactions->data;
|
|
594 if(!strcmp(trans->cseq, cseq)) {
|
|
595 return trans;
|
|
596 }
|
|
597 transactions = transactions->next;
|
|
598 }
|
|
599
|
|
600 return NULL;
|
|
601 }
|
|
602
|
|
603 static void send_sip_request(GaimConnection *gc, const gchar *method,
|
|
604 const gchar *url, const gchar *to, const gchar *addheaders,
|
|
605 const gchar *body, struct sip_dialog *dialog, TransCallback tc) {
|
|
606 struct simple_account_data *sip = gc->proto_data;
|
|
607 char *callid = dialog ? g_strdup(dialog->callid) : gencallid();
|
|
608 char *auth = NULL;
|
|
609 const char *addh = "";
|
|
610 gchar *branch = genbranch();
|
|
611 gchar *tag = NULL;
|
|
612 char *buf;
|
|
613
|
|
614 if(!strcmp(method, "REGISTER")) {
|
|
615 if(sip->regcallid) {
|
|
616 g_free(callid);
|
|
617 callid = g_strdup(sip->regcallid);
|
|
618 }
|
|
619 else sip->regcallid = g_strdup(callid);
|
|
620 }
|
|
621
|
|
622 if(addheaders) addh = addheaders;
|
|
623 if(sip->registrar.type && !strcmp(method, "REGISTER")) {
|
|
624 buf = auth_header(sip, &sip->registrar, method, url);
|
|
625 auth = g_strdup_printf("Authorization: %s", buf);
|
|
626 g_free(buf);
|
|
627 gaim_debug(GAIM_DEBUG_MISC, "simple", "header %s", auth);
|
|
628 }
|
|
629
|
|
630 if(sip->proxy.type && strcmp(method, "REGISTER")) {
|
|
631 buf = auth_header(sip, &sip->proxy, method, url);
|
|
632 auth = g_strdup_printf("Proxy-Authorization: %s", buf);
|
|
633 g_free(buf);
|
|
634 gaim_debug(GAIM_DEBUG_MISC, "simple", "header %s", auth);
|
|
635 }
|
|
636
|
|
637 if (!dialog)
|
|
638 tag = gentag();
|
|
639
|
|
640 buf = g_strdup_printf("%s %s SIP/2.0\r\n"
|
|
641 "Via: SIP/2.0/%s %s:%d;branch=%s\r\n"
|
|
642 /* Don't know what epid is, but LCS wants it */
|
|
643 "From: <sip:%s@%s>;tag=%s;epid=1234567890\r\n"
|
|
644 "To: <%s>%s%s\r\n"
|
|
645 "Max-Forwards: 10\r\n"
|
|
646 "CSeq: %d %s\r\n"
|
|
647 "User-Agent: Gaim/" VERSION "\r\n"
|
|
648 "Call-ID: %s\r\n"
|
|
649 "%s%s"
|
|
650 "Content-Length: %" G_GSIZE_FORMAT "\r\n\r\n%s",
|
|
651 method,
|
|
652 url,
|
|
653 sip->udp ? "UDP" : "TCP",
|
|
654 gaim_network_get_my_ip(-1),
|
|
655 sip->listenport,
|
|
656 branch,
|
|
657 sip->username,
|
|
658 sip->servername,
|
|
659 dialog ? dialog->ourtag : tag,
|
|
660 to,
|
|
661 dialog ? ";tag=" : "",
|
|
662 dialog ? dialog->theirtag : "",
|
|
663 ++sip->cseq,
|
|
664 method,
|
|
665 callid,
|
|
666 auth ? auth : "",
|
|
667 addh,
|
|
668 strlen(body),
|
|
669 body);
|
|
670
|
|
671 g_free(tag);
|
|
672 g_free(auth);
|
|
673 g_free(branch);
|
|
674 g_free(callid);
|
|
675
|
|
676 /* add to ongoing transactions */
|
|
677
|
|
678 transactions_add_buf(sip, buf, tc);
|
|
679
|
|
680 sendout_pkt(gc, buf);
|
|
681
|
|
682 g_free(buf);
|
|
683 }
|
|
684
|
|
685 static char *get_contact(struct simple_account_data *sip) {
|
|
686 return g_strdup_printf("<sip:%s@%s:%d;transport=%s>;methods=\"MESSAGE, SUBSCRIBE, NOTIFY\"", sip->username, gaim_network_get_my_ip(-1), sip->listenport, sip->udp ? "udp" : "tcp");
|
|
687 }
|
|
688
|
|
689 static void do_register_exp(struct simple_account_data *sip, int expire) {
|
|
690 char *uri = g_strdup_printf("sip:%s", sip->servername);
|
|
691 char *to = g_strdup_printf("sip:%s@%s", sip->username, sip->servername);
|
|
692 char *contact = get_contact(sip);
|
|
693 char *hdr = g_strdup_printf("Contact: %s\r\nExpires: %d\r\n", contact, expire);
|
|
694 g_free(contact);
|
|
695
|
|
696 sip->registerstatus = 1;
|
|
697
|
|
698 if(expire) {
|
|
699 sip->reregister = time(NULL) + expire - 50;
|
|
700 } else {
|
|
701 sip->reregister = time(NULL) + 600;
|
|
702 }
|
|
703
|
|
704 send_sip_request(sip->gc, "REGISTER", uri, to, hdr, "", NULL,
|
|
705 process_register_response);
|
|
706
|
|
707 g_free(hdr);
|
|
708 g_free(uri);
|
|
709 g_free(to);
|
|
710 }
|
|
711
|
|
712 static void do_register(struct simple_account_data *sip) {
|
|
713 do_register_exp(sip, sip->registerexpire);
|
|
714 }
|
|
715
|
|
716 static gchar *parse_from(const gchar *hdr) {
|
|
717 gchar *from;
|
|
718 const gchar *tmp, *tmp2 = hdr;
|
|
719
|
|
720 if(!hdr) return NULL;
|
|
721 gaim_debug_info("simple", "parsing address out of %s\n", hdr);
|
|
722 tmp = strchr(hdr, '<');
|
|
723
|
|
724 /* i hate the different SIP UA behaviours... */
|
|
725 if(tmp) { /* sip address in <...> */
|
|
726 tmp2 = tmp + 1;
|
|
727 tmp = strchr(tmp2, '>');
|
|
728 if(tmp) {
|
|
729 from = g_strndup(tmp2, tmp - tmp2);
|
|
730 } else {
|
|
731 gaim_debug_info("simple", "found < without > in From\n");
|
|
732 return NULL;
|
|
733 }
|
|
734 } else {
|
|
735 tmp = strchr(tmp2, ';');
|
|
736 if(tmp) {
|
|
737 from = g_strndup(tmp2, tmp - tmp2);
|
|
738 } else {
|
|
739 from = g_strdup(tmp2);
|
|
740 }
|
|
741 }
|
|
742 gaim_debug_info("simple", "got %s\n", from);
|
|
743 return from;
|
|
744 }
|
|
745
|
|
746 static gboolean process_subscribe_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) {
|
|
747 gchar *to;
|
|
748
|
|
749 if(msg->response == 200 || msg->response == 202) {
|
|
750 return TRUE;
|
|
751 }
|
|
752
|
|
753 to = parse_from(sipmsg_find_header(tc->msg, "To")); /* cant be NULL since it is our own msg */
|
|
754
|
|
755 /* we can not subscribe -> user is offline (TODO unknown status?) */
|
|
756
|
|
757 gaim_prpl_got_user_status(sip->account, to, "offline", NULL);
|
|
758 g_free(to);
|
|
759 return TRUE;
|
|
760 }
|
|
761
|
|
762 static void simple_subscribe(struct simple_account_data *sip, struct simple_buddy *buddy) {
|
|
763 gchar *contact = "Expires: 1200\r\nAccept: application/pidf+xml, application/xpidf+xml\r\nEvent: presence\r\n";
|
|
764 gchar *to;
|
|
765 gchar *tmp;
|
|
766
|
|
767 if(strstr(buddy->name, "sip:"))
|
|
768 to = g_strdup(buddy->name);
|
|
769 else
|
|
770 to = g_strdup_printf("sip:%s", buddy->name);
|
|
771
|
|
772 tmp = get_contact(sip);
|
|
773 contact = g_strdup_printf("%sContact: %s\r\n", contact, tmp);
|
|
774 g_free(tmp);
|
|
775
|
|
776 /* subscribe to buddy presence
|
|
777 * we dont need to know the status so we do not need a callback */
|
|
778
|
|
779 send_sip_request(sip->gc, "SUBSCRIBE", to, to, contact, "", NULL,
|
|
780 process_subscribe_response);
|
|
781
|
|
782 g_free(to);
|
|
783 g_free(contact);
|
|
784
|
|
785 /* resubscribe before subscription expires */
|
|
786 /* add some jitter */
|
|
787 buddy->resubscribe = time(NULL)+1140+(rand()%50);
|
|
788 }
|
|
789
|
|
790 static gboolean simple_add_lcs_contacts(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) {
|
|
791 gchar *tmp;
|
|
792 xmlnode *item, *group, *isc;
|
|
793 const char *name_group;
|
|
794 GaimBuddy *b;
|
|
795 GaimGroup *g = NULL;
|
|
796 struct simple_buddy *bs;
|
|
797 int len = msg->bodylen;
|
|
798
|
|
799
|
|
800 tmp = sipmsg_find_header(msg, "Event");
|
|
801 if(tmp && !strncmp(tmp, "vnd-microsoft-roaming-contacts", 30)){
|
|
802
|
|
803 gaim_debug_info("simple", "simple_add_lcs_contacts->%s-%d\n", msg->body, len);
|
|
804 /*Convert the contact from XML to Gaim Buddies*/
|
|
805 isc = xmlnode_from_str(msg->body, len);
|
|
806
|
|
807 /* ToDo. Find for all groups */
|
|
808 if ((group = xmlnode_get_child(isc, "group"))) {
|
|
809 name_group = xmlnode_get_attrib(group, "name");
|
|
810 gaim_debug_info("simple", "name_group->%s\n", name_group);
|
|
811 g = gaim_find_group(name_group);
|
|
812 if(!g)
|
|
813 g = gaim_group_new(name_group);
|
|
814 }
|
|
815
|
|
816 if (!g) {
|
|
817 g = gaim_find_group("Buddies");
|
|
818 if(!g)
|
|
819 g = gaim_group_new("Buddies");
|
|
820 }
|
|
821
|
|
822 for(item = xmlnode_get_child(isc, "contact"); item; item = xmlnode_get_next_twin(item))
|
|
823 {
|
|
824 const char *uri, *name, *groups;
|
|
825 char *buddy_name;
|
|
826 uri = xmlnode_get_attrib(item, "uri");
|
|
827 name = xmlnode_get_attrib(item, "name");
|
|
828 groups = xmlnode_get_attrib(item, "groups");
|
|
829 gaim_debug_info("simple", "URI->%s\n", uri);
|
|
830
|
|
831 buddy_name = g_strdup_printf("sip:%s", uri);
|
|
832
|
|
833 b = gaim_find_buddy(sip->account, buddy_name);
|
|
834 if(!b){
|
|
835 b = gaim_buddy_new(sip->account, buddy_name, uri);
|
|
836 }
|
|
837 g_free(buddy_name);
|
|
838
|
|
839 gaim_blist_add_buddy(b, NULL, g, NULL);
|
|
840 gaim_blist_alias_buddy(b, uri);
|
|
841 bs = g_new0(struct simple_buddy, 1);
|
|
842 bs->name = g_strdup(b->name);
|
|
843 g_hash_table_insert(sip->buddies, bs->name, bs);
|
|
844 }
|
|
845 xmlnode_free(isc);
|
|
846 }
|
|
847 return 0;
|
|
848 }
|
|
849
|
|
850 static void simple_subscribe_buddylist(struct simple_account_data *sip) {
|
|
851 gchar *contact = "Event: vnd-microsoft-roaming-contacts\r\nAccept: application/vnd-microsoft-roaming-contacts+xml\r\nSupported: com.microsoft.autoextend\r\nSupported: ms-benotify\r\nProxy-Require: ms-benotify\r\nSupported: ms-piggyback-first-notify\r\n";
|
|
852 gchar *to;
|
|
853 gchar *tmp;
|
|
854 to = g_strdup_printf("sip:%s@%s", sip->username, sip->servername);
|
|
855
|
|
856 tmp = get_contact(sip);
|
|
857
|
|
858 contact = g_strdup_printf("%sContact: %s\r\n", contact, tmp);
|
|
859 g_free(tmp);
|
|
860
|
|
861 send_sip_request(sip->gc, "SUBSCRIBE", to, to, contact, "", NULL, simple_add_lcs_contacts);
|
|
862
|
|
863 g_free(to);
|
|
864 g_free(contact);
|
|
865 }
|
|
866
|
|
867
|
|
868 static void simple_buddy_resub(char *name, struct simple_buddy *buddy, struct simple_account_data *sip) {
|
|
869 time_t curtime = time(NULL);
|
|
870 gaim_debug_info("simple", "buddy resub\n");
|
|
871 if(buddy->resubscribe < curtime) {
|
|
872 gaim_debug(GAIM_DEBUG_MISC, "simple", "simple_buddy_resub %s\n", name);
|
|
873 simple_subscribe(sip, buddy);
|
|
874 }
|
|
875 }
|
|
876
|
|
877 static gboolean resend_timeout(struct simple_account_data *sip) {
|
|
878 GSList *tmp = sip->transactions;
|
|
879 time_t currtime = time(NULL);
|
|
880 while(tmp) {
|
|
881 struct transaction *trans = tmp->data;
|
|
882 tmp = tmp->next;
|
|
883 gaim_debug_info("simple", "have open transaction age: %d\n", currtime- trans->time);
|
|
884 if((currtime - trans->time > 5) && trans->retries >= 1) {
|
|
885 /* TODO 408 */
|
|
886 } else {
|
|
887 if((currtime - trans->time > 2) && trans->retries == 0) {
|
|
888 trans->retries++;
|
|
889 sendout_sipmsg(sip, trans->msg);
|
|
890 }
|
|
891 }
|
|
892 }
|
|
893 return TRUE;
|
|
894 }
|
|
895
|
|
896 static gboolean subscribe_timeout(struct simple_account_data *sip) {
|
|
897 GSList *tmp;
|
|
898 time_t curtime = time(NULL);
|
|
899 /* register again if first registration expires */
|
|
900 if(sip->reregister < curtime) {
|
|
901 do_register(sip);
|
|
902 }
|
|
903 /* check for every subscription if we need to resubscribe */
|
|
904 g_hash_table_foreach(sip->buddies, (GHFunc)simple_buddy_resub, (gpointer)sip);
|
|
905
|
|
906 /* remove a timed out suscriber */
|
|
907
|
|
908 tmp = sip->watcher;
|
|
909 while(tmp) {
|
|
910 struct simple_watcher *watcher = tmp->data;
|
|
911 if(watcher->expire < curtime) {
|
|
912 watcher_remove(sip, watcher->name);
|
|
913 tmp = sip->watcher;
|
|
914 }
|
|
915 if(tmp) tmp = tmp->next;
|
|
916 }
|
|
917
|
|
918 return TRUE;
|
|
919 }
|
|
920
|
|
921 static void simple_send_message(struct simple_account_data *sip, const char *to, const char *msg, const char *type) {
|
|
922 gchar *hdr;
|
|
923 gchar *fullto;
|
|
924 if(strncmp("sip:", to, 4)) {
|
|
925 fullto = g_strdup_printf("sip:%s", to);
|
|
926 } else {
|
|
927 fullto = g_strdup(to);
|
|
928 }
|
|
929 if(type) {
|
|
930 hdr = g_strdup_printf("Content-Type: %s\r\n", type);
|
|
931 } else {
|
|
932 hdr = g_strdup("Content-Type: text/plain\r\n");
|
|
933 }
|
|
934 send_sip_request(sip->gc, "MESSAGE", fullto, fullto, hdr, msg, NULL, NULL);
|
|
935 g_free(hdr);
|
|
936 g_free(fullto);
|
|
937 }
|
|
938
|
|
939 static int simple_im_send(GaimConnection *gc, const char *who, const char *what, GaimMessageFlags flags) {
|
|
940 struct simple_account_data *sip = gc->proto_data;
|
|
941 char *to = g_strdup(who);
|
|
942 char *text = gaim_unescape_html(what);
|
|
943 simple_send_message(sip, to, text, NULL);
|
|
944 g_free(to);
|
|
945 g_free(text);
|
|
946 return 1;
|
|
947 }
|
|
948
|
|
949 static void process_incoming_message(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
950 gchar *from;
|
|
951 gchar *contenttype;
|
|
952 gboolean found = FALSE;
|
|
953
|
|
954 from = parse_from(sipmsg_find_header(msg, "From"));
|
|
955
|
|
956 if(!from) return;
|
|
957
|
|
958 gaim_debug(GAIM_DEBUG_MISC, "simple", "got message from %s: %s\n", from, msg->body);
|
|
959
|
|
960 contenttype = sipmsg_find_header(msg, "Content-Type");
|
|
961 if(!contenttype || !strncmp(contenttype, "text/plain", 10) || !strncmp(contenttype, "text/html", 9)) {
|
|
962 serv_got_im(sip->gc, from, msg->body, 0, time(NULL));
|
|
963 send_sip_response(sip->gc, msg, 200, "OK", NULL);
|
|
964 found = TRUE;
|
|
965 }
|
|
966 if(!strncmp(contenttype, "application/im-iscomposing+xml", 30)) {
|
|
967 xmlnode *isc = xmlnode_from_str(msg->body, msg->bodylen);
|
|
968 xmlnode *state;
|
|
969 gchar *statedata;
|
|
970
|
|
971 if(!isc) {
|
|
972 gaim_debug_info("simple", "process_incoming_message: can not parse iscomposing\n");
|
|
973 return;
|
|
974 }
|
|
975
|
|
976 state = xmlnode_get_child(isc, "state");
|
|
977
|
|
978 if(!state) {
|
|
979 gaim_debug_info("simple", "process_incoming_message: no state found\n");
|
|
980 xmlnode_free(isc);
|
|
981 return;
|
|
982 }
|
|
983
|
|
984 statedata = xmlnode_get_data(state);
|
|
985 if(statedata) {
|
|
986 if(strstr(statedata, "active")) serv_got_typing(sip->gc, from, 0, GAIM_TYPING);
|
|
987 else serv_got_typing_stopped(sip->gc, from);
|
|
988
|
|
989 g_free(statedata);
|
|
990 }
|
|
991 xmlnode_free(isc);
|
|
992 send_sip_response(sip->gc, msg, 200, "OK", NULL);
|
|
993 found = TRUE;
|
|
994 }
|
|
995 if(!found) {
|
|
996 gaim_debug_info("simple", "got unknown mime-type");
|
|
997 send_sip_response(sip->gc, msg, 415, "Unsupported media type", NULL);
|
|
998 }
|
|
999 g_free(from);
|
|
1000 }
|
|
1001
|
|
1002
|
|
1003 gboolean process_register_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) {
|
|
1004 gchar *tmp;
|
|
1005 gaim_debug(GAIM_DEBUG_MISC, "simple", "in process register response response: %d\n", msg->response);
|
|
1006 switch (msg->response) {
|
|
1007 case 200:
|
|
1008 if(sip->registerstatus < 3) { /* registered */
|
|
1009 if(gaim_account_get_bool(sip->account, "dopublish", TRUE)) {
|
|
1010 send_publish(sip);
|
|
1011 }
|
|
1012 }
|
|
1013 sip->registerstatus = 3;
|
|
1014 gaim_connection_set_state(sip->gc, GAIM_CONNECTED);
|
|
1015
|
|
1016 /* get buddies from blist */
|
|
1017 simple_get_buddies(sip->gc);
|
|
1018
|
|
1019 subscribe_timeout(sip);
|
|
1020 tmp = sipmsg_find_header(msg, "Allow-Events");
|
|
1021 if(tmp && strstr(tmp, "vnd-microsoft-provisioning")){
|
|
1022 simple_subscribe_buddylist(sip);
|
|
1023 }
|
|
1024
|
|
1025 break;
|
|
1026 case 401:
|
|
1027 if(sip->registerstatus != 2) {
|
|
1028 gaim_debug_info("simple", "REGISTER retries %d\n", sip->registrar.retries);
|
|
1029 if(sip->registrar.retries > 3) {
|
|
1030 sip->gc->wants_to_die = TRUE;
|
|
1031 gaim_connection_error(sip->gc, _("Wrong Password"));
|
|
1032 return TRUE;
|
|
1033 }
|
|
1034 tmp = sipmsg_find_header(msg, "WWW-Authenticate");
|
|
1035 fill_auth(sip, tmp, &sip->registrar);
|
|
1036 sip->registerstatus = 2;
|
|
1037 do_register(sip);
|
|
1038 }
|
|
1039 break;
|
|
1040 }
|
|
1041 return TRUE;
|
|
1042 }
|
|
1043
|
|
1044 static void process_incoming_notify(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
1045 gchar *from;
|
|
1046 gchar *fromhdr;
|
|
1047 gchar *tmp2;
|
|
1048 xmlnode *pidf;
|
|
1049 xmlnode *basicstatus = NULL, *tuple, *status;
|
|
1050 gboolean isonline = FALSE;
|
|
1051
|
|
1052 fromhdr = sipmsg_find_header(msg, "From");
|
|
1053 from = parse_from(fromhdr);
|
|
1054 if(!from) return;
|
|
1055
|
|
1056 pidf = xmlnode_from_str(msg->body, msg->bodylen);
|
|
1057
|
|
1058 if(!pidf) {
|
|
1059 gaim_debug_info("simple", "process_incoming_notify: no parseable pidf\n");
|
|
1060 return;
|
|
1061 }
|
|
1062
|
|
1063 if ((tuple = xmlnode_get_child(pidf, "tuple")))
|
|
1064 if ((status = xmlnode_get_child(tuple, "status")))
|
|
1065 basicstatus = xmlnode_get_child(status, "basic");
|
|
1066
|
|
1067 if(!basicstatus) {
|
|
1068 gaim_debug_info("simple", "process_incoming_notify: no basic found\n");
|
|
1069 xmlnode_free(pidf);
|
|
1070 return;
|
|
1071 }
|
|
1072
|
|
1073 tmp2 = xmlnode_get_data(basicstatus);
|
|
1074
|
|
1075 if(!tmp2) {
|
|
1076 gaim_debug_info("simple", "process_incoming_notify: no basic data found\n");
|
|
1077 xmlnode_free(pidf);
|
|
1078 return;
|
|
1079 }
|
|
1080
|
|
1081 if(strstr(tmp2, "open")) {
|
|
1082 isonline = TRUE;
|
|
1083 }
|
|
1084
|
|
1085 g_free(tmp2);
|
|
1086
|
|
1087 if(isonline) gaim_prpl_got_user_status(sip->account, from, "available", NULL);
|
|
1088 else gaim_prpl_got_user_status(sip->account, from, "offline", NULL);
|
|
1089
|
|
1090 xmlnode_free(pidf);
|
|
1091
|
|
1092 g_free(from);
|
|
1093 send_sip_response(sip->gc, msg, 200, "OK", NULL);
|
|
1094 }
|
|
1095
|
|
1096 static unsigned int simple_typing(GaimConnection *gc, const char *name, GaimTypingState state) {
|
|
1097 struct simple_account_data *sip = gc->proto_data;
|
|
1098
|
|
1099 gchar *xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
|
1100 "<isComposing xmlns=\"urn:ietf:params:xml:ns:im-iscomposing\"\n"
|
|
1101 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
|
|
1102 "xsi:schemaLocation=\"urn:ietf:params:xml:ns:im-composing iscomposing.xsd\">\n"
|
|
1103 "<state>%s</state>\n"
|
|
1104 "<contenttype>text/plain</contenttype>\n"
|
|
1105 "<refresh>60</refresh>\n"
|
|
1106 "</isComposing>";
|
|
1107 gchar *recv = g_strdup(name);
|
|
1108 if(state == GAIM_TYPING) {
|
|
1109 gchar *msg = g_strdup_printf(xml, "active");
|
|
1110 simple_send_message(sip, recv, msg, "application/im-iscomposing+xml");
|
|
1111 g_free(msg);
|
|
1112 } else /* TODO: Only if (state == GAIM_TYPED) ? */ {
|
|
1113 gchar *msg = g_strdup_printf(xml, "idle");
|
|
1114 simple_send_message(sip, recv, msg, "application/im-iscomposing+xml");
|
|
1115 g_free(msg);
|
|
1116 }
|
|
1117 g_free(recv);
|
|
1118 /*
|
|
1119 * TODO: Is this right? It will cause the core to call
|
|
1120 * serv_send_typing(gc, who, GAIM_TYPING) once every second
|
|
1121 * until the user stops typing. If that's not desired,
|
|
1122 * then return 0 instead.
|
|
1123 */
|
|
1124 return 1;
|
|
1125 }
|
|
1126
|
|
1127 static gchar *find_tag(const gchar *hdr) {
|
|
1128 const gchar *tmp = strstr(hdr, ";tag="), *tmp2;
|
|
1129
|
|
1130 if(!tmp) return NULL;
|
|
1131 tmp += 5;
|
|
1132 if((tmp2 = strchr(tmp, ';'))) {
|
|
1133 return g_strndup(tmp, tmp2 - tmp);
|
|
1134 }
|
|
1135 return g_strdup(tmp);
|
|
1136 }
|
|
1137
|
|
1138 static gchar* gen_xpidf(struct simple_account_data *sip) {
|
|
1139 gchar *doc = g_strdup_printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
|
1140 "<presence>\n"
|
|
1141 "<presentity uri=\"sip:%s@%s;method=SUBSCRIBE\"/>\n"
|
|
1142 "<display name=\"sip:%s@%s\"/>\n"
|
|
1143 "<atom id=\"1234\">\n"
|
|
1144 "<address uri=\"sip:%s@%s\">\n"
|
|
1145 "<status status=\"%s\"/>\n"
|
|
1146 "</address>\n"
|
|
1147 "</atom>\n"
|
|
1148 "</presence>\n",
|
|
1149 sip->username,
|
|
1150 sip->servername,
|
|
1151 sip->username,
|
|
1152 sip->servername,
|
|
1153 sip->username,
|
|
1154 sip->servername,
|
|
1155 sip->status);
|
|
1156 return doc;
|
|
1157 }
|
|
1158
|
|
1159
|
|
1160
|
|
1161 static gchar* gen_pidf(struct simple_account_data *sip) {
|
|
1162 gchar *doc = g_strdup_printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
|
1163 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n"
|
|
1164 "xmlns:im=\"urn:ietf:params:xml:ns:pidf:im\"\n"
|
|
1165 "entity=\"sip:%s@%s\">\n"
|
|
1166 "<tuple id=\"bs35r9f\">\n"
|
|
1167 "<status>\n"
|
|
1168 "<basic>open</basic>\n"
|
|
1169 "</status>\n"
|
|
1170 "<note>%s</note>\n"
|
|
1171 "</tuple>\n"
|
|
1172 "</presence>",
|
|
1173 sip->username,
|
|
1174 sip->servername,
|
|
1175 sip->status);
|
|
1176 return doc;
|
|
1177 }
|
|
1178
|
|
1179 static void send_notify(struct simple_account_data *sip, struct simple_watcher *watcher) {
|
|
1180 gchar *doc = watcher->needsxpidf ? gen_xpidf(sip) : gen_pidf(sip);
|
|
1181 gchar *hdr = watcher->needsxpidf ? "Event: presence\r\nContent-Type: application/xpidf+xml\r\n" : "Event: presence\r\nContent-Type: application/pidf+xml\r\n";
|
|
1182 send_sip_request(sip->gc, "NOTIFY", watcher->name, watcher->name, hdr, doc, &watcher->dialog, NULL);
|
|
1183 g_free(doc);
|
|
1184 }
|
|
1185
|
|
1186 static gboolean process_publish_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) {
|
|
1187 if(msg->response != 200 && msg->response != 408) {
|
|
1188 /* never send again */
|
|
1189 sip->republish = -1;
|
|
1190 }
|
|
1191 return TRUE;
|
|
1192 }
|
|
1193
|
|
1194 static void send_publish(struct simple_account_data *sip) {
|
|
1195 gchar *uri = g_strdup_printf("sip:%s@%s", sip->username, sip->servername);
|
|
1196 gchar *doc = gen_pidf(sip);
|
|
1197 send_sip_request(sip->gc, "PUBLISH", uri, uri,
|
|
1198 "Expires: 600\r\nEvent: presence\r\n"
|
|
1199 "Content-Type: application/pidf+xml\r\n",
|
|
1200 doc, NULL, process_publish_response);
|
|
1201 sip->republish = time(NULL) + 500;
|
|
1202 g_free(uri);
|
|
1203 g_free(doc);
|
|
1204 }
|
|
1205
|
|
1206 static void process_incoming_subscribe(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
1207 const char *from_hdr = sipmsg_find_header(msg, "From");
|
|
1208 gchar *from = parse_from(from_hdr);
|
|
1209 gchar *theirtag = find_tag(from_hdr);
|
|
1210 gchar *ourtag = find_tag(sipmsg_find_header(msg, "To"));
|
|
1211 gboolean tagadded = FALSE;
|
|
1212 gchar *callid = sipmsg_find_header(msg, "Call-ID");
|
|
1213 gchar *expire = sipmsg_find_header(msg, "Expire");
|
|
1214 gchar *tmp;
|
|
1215 struct simple_watcher *watcher = watcher_find(sip, from);
|
|
1216 if(!ourtag) {
|
|
1217 tagadded = TRUE;
|
|
1218 ourtag = gentag();
|
|
1219 }
|
|
1220 if(!watcher) { /* new subscription */
|
|
1221 gchar *acceptheader = sipmsg_find_header(msg, "Accept");
|
|
1222 gboolean needsxpidf = FALSE;
|
|
1223 if(!gaim_privacy_check(sip->account, from)) {
|
|
1224 send_sip_response(sip->gc, msg, 202, "Ok", NULL);
|
|
1225 goto privend;
|
|
1226 }
|
|
1227 if(acceptheader) {
|
|
1228 gchar *tmp = acceptheader;
|
|
1229 gboolean foundpidf = FALSE;
|
|
1230 gboolean foundxpidf = FALSE;
|
|
1231 while(tmp && tmp < acceptheader + strlen(acceptheader)) {
|
|
1232 gchar *tmp2 = strchr(tmp, ',');
|
|
1233 if(tmp2) *tmp2 = '\0';
|
|
1234 if(!strcmp("application/pidf+xml", tmp))
|
|
1235 foundpidf = TRUE;
|
|
1236 if(!strcmp("application/xpidf+xml", tmp))
|
|
1237 foundxpidf = TRUE;
|
|
1238 if(tmp2) {
|
|
1239 *tmp2 = ',';
|
|
1240 tmp = tmp2;
|
|
1241 while(*tmp == ' ') tmp++;
|
|
1242 } else
|
|
1243 tmp = 0;
|
|
1244 }
|
|
1245 if(!foundpidf && foundxpidf) needsxpidf = TRUE;
|
|
1246 g_free(acceptheader);
|
|
1247 }
|
|
1248 watcher = watcher_create(sip, from, callid, ourtag, theirtag, needsxpidf);
|
|
1249 }
|
|
1250 if(tagadded) {
|
|
1251 gchar *to = g_strdup_printf("%s;tag=%s", sipmsg_find_header(msg, "To"), ourtag);
|
|
1252 sipmsg_remove_header(msg, "To");
|
|
1253 sipmsg_add_header(msg, "To", to);
|
|
1254 g_free(to);
|
|
1255 }
|
|
1256 if(expire)
|
|
1257 watcher->expire = time(NULL) + strtol(expire, NULL, 10);
|
|
1258 else
|
|
1259 watcher->expire = time(NULL) + 600;
|
|
1260 sipmsg_remove_header(msg, "Contact");
|
|
1261 tmp = get_contact(sip);
|
|
1262 sipmsg_add_header(msg, "Contact", tmp);
|
|
1263 g_free(tmp);
|
|
1264 gaim_debug_info("simple", "got subscribe: name %s ourtag %s theirtag %s callid %s\n", watcher->name, watcher->dialog.ourtag, watcher->dialog.theirtag, watcher->dialog.callid);
|
|
1265 send_sip_response(sip->gc, msg, 200, "Ok", NULL);
|
|
1266 send_notify(sip, watcher);
|
|
1267 privend:
|
|
1268 g_free(from);
|
|
1269 g_free(theirtag);
|
|
1270 g_free(ourtag);
|
|
1271 g_free(callid);
|
|
1272 g_free(expire);
|
|
1273 }
|
|
1274
|
|
1275 static void process_input_message(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
1276 gboolean found = FALSE;
|
|
1277 if(msg->response == 0) { /* request */
|
|
1278 if(!strcmp(msg->method, "MESSAGE")) {
|
|
1279 process_incoming_message(sip, msg);
|
|
1280 found = TRUE;
|
|
1281 } else if(!strcmp(msg->method, "NOTIFY")) {
|
|
1282 process_incoming_notify(sip, msg);
|
|
1283 found = TRUE;
|
|
1284 } else if(!strcmp(msg->method, "SUBSCRIBE")) {
|
|
1285 process_incoming_subscribe(sip, msg);
|
|
1286 found = TRUE;
|
|
1287 } else {
|
|
1288 send_sip_response(sip->gc, msg, 501, "Not implemented", NULL);
|
|
1289 }
|
|
1290 } else { /* response */
|
|
1291 struct transaction *trans = transactions_find(sip, msg);
|
|
1292 if(trans) {
|
|
1293 if(msg->response == 407) {
|
|
1294 gchar *resend, *auth, *ptmp;
|
|
1295
|
|
1296 if(sip->proxy.retries > 3) return;
|
|
1297 sip->proxy.retries++;
|
|
1298 /* do proxy authentication */
|
|
1299
|
|
1300 ptmp = sipmsg_find_header(msg, "Proxy-Authenticate");
|
|
1301
|
|
1302 fill_auth(sip, ptmp, &sip->proxy);
|
|
1303 auth = auth_header(sip, &sip->proxy, trans->msg->method, trans->msg->target);
|
|
1304 sipmsg_remove_header(trans->msg, "Proxy-Authorization");
|
|
1305 sipmsg_add_header(trans->msg, "Proxy-Authorization", auth);
|
|
1306 g_free(auth);
|
|
1307 resend = sipmsg_to_string(trans->msg);
|
|
1308 /* resend request */
|
|
1309 sendout_pkt(sip->gc, resend);
|
|
1310 g_free(resend);
|
|
1311 } else {
|
|
1312 if(msg->response == 100) {
|
|
1313 /* ignore provisional response */
|
|
1314 gaim_debug_info("simple", "got trying response\n");
|
|
1315 } else {
|
|
1316 sip->proxy.retries = 0;
|
|
1317 if(!strcmp(trans->msg->method, "REGISTER")) {
|
|
1318 if(msg->response == 401) sip->registrar.retries++;
|
|
1319 else sip->registrar.retries = 0;
|
|
1320 } else {
|
|
1321 if(msg->response == 401) {
|
|
1322 gchar *resend, *auth, *ptmp;
|
|
1323
|
|
1324 if(sip->registrar.retries > 4) return;
|
|
1325 sip->registrar.retries++;
|
|
1326
|
|
1327 ptmp = sipmsg_find_header(msg, "WWW-Authenticate");
|
|
1328
|
|
1329 fill_auth(sip, ptmp, &sip->registrar);
|
|
1330 auth = auth_header(sip, &sip->registrar, trans->msg->method, trans->msg->target);
|
|
1331 sipmsg_remove_header(trans->msg, "Authorization");
|
|
1332 sipmsg_add_header(trans->msg, "Authorization", auth);
|
|
1333 g_free(auth);
|
|
1334 resend = sipmsg_to_string(trans->msg);
|
|
1335 /* resend request */
|
|
1336 sendout_pkt(sip->gc, resend);
|
|
1337 g_free(resend);
|
|
1338 }
|
|
1339 }
|
|
1340 if(trans->callback) {
|
|
1341 /* call the callback to process response*/
|
|
1342 (trans->callback)(sip, msg, trans);
|
|
1343 }
|
|
1344 transactions_remove(sip, trans);
|
|
1345 }
|
|
1346 }
|
|
1347 found = TRUE;
|
|
1348 } else {
|
|
1349 gaim_debug(GAIM_DEBUG_MISC, "simple", "received response to unknown transaction");
|
|
1350 }
|
|
1351 }
|
|
1352 if(!found) {
|
|
1353 gaim_debug(GAIM_DEBUG_MISC, "simple", "received a unknown sip message with method %s and response %d\n", msg->method, msg->response);
|
|
1354 }
|
|
1355 }
|
|
1356
|
|
1357 static void process_input(struct simple_account_data *sip, struct sip_connection *conn)
|
|
1358 {
|
|
1359 char *cur;
|
|
1360 char *dummy;
|
|
1361 struct sipmsg *msg;
|
|
1362 int restlen;
|
|
1363 cur = conn->inbuf;
|
|
1364
|
|
1365 /* according to the RFC remove CRLF at the beginning */
|
|
1366 while(*cur == '\r' || *cur == '\n') {
|
|
1367 cur++;
|
|
1368 }
|
|
1369 if(cur != conn->inbuf) {
|
|
1370 memmove(conn->inbuf, cur, conn->inbufused - (cur - conn->inbuf));
|
|
1371 conn->inbufused = strlen(conn->inbuf);
|
|
1372 }
|
|
1373
|
|
1374 /* Received a full Header? */
|
|
1375 if((cur = strstr(conn->inbuf, "\r\n\r\n")) != NULL) {
|
|
1376 time_t currtime = time(NULL);
|
|
1377 cur += 2;
|
|
1378 cur[0] = '\0';
|
|
1379 gaim_debug_info("simple", "\n\nreceived - %s\n######\n%s\n#######\n\n", ctime(&currtime), conn->inbuf);
|
|
1380 msg = sipmsg_parse_header(conn->inbuf);
|
|
1381 cur[0] = '\r';
|
|
1382 cur += 2;
|
|
1383 restlen = conn->inbufused - (cur - conn->inbuf);
|
|
1384 if(restlen >= msg->bodylen) {
|
|
1385 dummy = g_malloc(msg->bodylen + 1);
|
|
1386 memcpy(dummy, cur, msg->bodylen);
|
|
1387 dummy[msg->bodylen] = '\0';
|
|
1388 msg->body = dummy;
|
|
1389 cur += msg->bodylen;
|
|
1390 memmove(conn->inbuf, cur, conn->inbuflen - (cur - conn->inbuf));
|
|
1391 conn->inbufused = strlen(conn->inbuf);
|
|
1392 } else {
|
|
1393 sipmsg_free(msg);
|
|
1394 return;
|
|
1395 }
|
|
1396 gaim_debug(GAIM_DEBUG_MISC, "simple", "in process response response: %d\n", msg->response);
|
|
1397 process_input_message(sip, msg);
|
|
1398 } else {
|
|
1399 gaim_debug(GAIM_DEBUG_MISC, "simple", "received a incomplete sip msg: %s\n", conn->inbuf);
|
|
1400 }
|
|
1401 }
|
|
1402
|
|
1403 static void simple_udp_process(gpointer data, gint source, GaimInputCondition con) {
|
|
1404 GaimConnection *gc = data;
|
|
1405 struct simple_account_data *sip = gc->proto_data;
|
|
1406 struct sipmsg *msg;
|
|
1407 int len;
|
|
1408 time_t currtime;
|
|
1409
|
|
1410 static char buffer[65536];
|
|
1411 if((len = recv(source, buffer, sizeof(buffer) - 1, 0)) > 0) {
|
|
1412 buffer[len] = '\0';
|
|
1413 gaim_debug_info("simple", "\n\nreceived - %s\n######\n%s\n#######\n\n", ctime(&currtime), buffer);
|
|
1414 msg = sipmsg_parse_msg(buffer);
|
|
1415 if(msg) process_input_message(sip, msg);
|
|
1416 }
|
|
1417 }
|
|
1418
|
|
1419 static void simple_input_cb(gpointer data, gint source, GaimInputCondition cond)
|
|
1420 {
|
|
1421 GaimConnection *gc = data;
|
|
1422 struct simple_account_data *sip = gc->proto_data;
|
|
1423 int len;
|
|
1424 struct sip_connection *conn = connection_find(sip, source);
|
|
1425 if(!conn) {
|
|
1426 gaim_debug_error("simple", "Connection not found!\n");
|
|
1427 return;
|
|
1428 }
|
|
1429
|
|
1430 if(conn->inbuflen < conn->inbufused + SIMPLE_BUF_INC) {
|
|
1431 conn->inbuflen += SIMPLE_BUF_INC;
|
|
1432 conn->inbuf = g_realloc(conn->inbuf, conn->inbuflen);
|
|
1433 }
|
|
1434
|
|
1435 len = read(source, conn->inbuf + conn->inbufused, SIMPLE_BUF_INC - 1);
|
|
1436
|
|
1437 if(len < 0 && errno == EAGAIN)
|
|
1438 return;
|
|
1439 else if(len <= 0) {
|
|
1440 gaim_debug_info("simple", "simple_input_cb: read error\n");
|
|
1441 connection_remove(sip, source);
|
|
1442 if(sip->fd == source) sip->fd = -1;
|
|
1443 return;
|
|
1444 }
|
|
1445
|
|
1446 conn->inbufused += len;
|
|
1447 conn->inbuf[conn->inbufused] = '\0';
|
|
1448
|
|
1449 process_input(sip, conn);
|
|
1450 }
|
|
1451
|
|
1452 /* Callback for new connections on incoming TCP port */
|
|
1453 static void simple_newconn_cb(gpointer data, gint source, GaimInputCondition cond) {
|
|
1454 GaimConnection *gc = data;
|
|
1455 struct simple_account_data *sip = gc->proto_data;
|
|
1456 struct sip_connection *conn;
|
|
1457
|
|
1458 int newfd = accept(source, NULL, NULL);
|
|
1459
|
|
1460 conn = connection_create(sip, newfd);
|
|
1461
|
|
1462 conn->inputhandler = gaim_input_add(newfd, GAIM_INPUT_READ, simple_input_cb, gc);
|
|
1463 }
|
|
1464
|
|
1465 static void login_cb(gpointer data, gint source, const gchar *error_message) {
|
|
1466 GaimConnection *gc = data;
|
|
1467 struct simple_account_data *sip;
|
|
1468 struct sip_connection *conn;
|
|
1469
|
|
1470 if (!GAIM_CONNECTION_IS_VALID(gc))
|
|
1471 {
|
|
1472 if (source >= 0)
|
|
1473 close(source);
|
|
1474 return;
|
|
1475 }
|
|
1476
|
|
1477 if(source < 0) {
|
|
1478 gaim_connection_error(gc, _("Could not connect"));
|
|
1479 return;
|
|
1480 }
|
|
1481
|
|
1482 sip = gc->proto_data;
|
|
1483 sip->fd = source;
|
|
1484
|
|
1485 conn = connection_create(sip, source);
|
|
1486
|
|
1487 sip->registertimeout = gaim_timeout_add((rand()%100)+10*1000, (GSourceFunc)subscribe_timeout, sip);
|
|
1488
|
|
1489 do_register(sip);
|
|
1490
|
|
1491 conn->inputhandler = gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_input_cb, gc);
|
|
1492 }
|
|
1493
|
|
1494 static guint simple_ht_hash_nick(const char *nick) {
|
|
1495 char *lc = g_utf8_strdown(nick, -1);
|
|
1496 guint bucket = g_str_hash(lc);
|
|
1497 g_free(lc);
|
|
1498
|
|
1499 return bucket;
|
|
1500 }
|
|
1501
|
|
1502 static gboolean simple_ht_equals_nick(const char *nick1, const char *nick2) {
|
|
1503 return (gaim_utf8_strcasecmp(nick1, nick2) == 0);
|
|
1504 }
|
|
1505
|
|
1506 static void simple_udp_host_resolved_listen_cb(int listenfd, gpointer data) {
|
|
1507 struct simple_account_data *sip = (struct simple_account_data*) data;
|
|
1508
|
|
1509 if(listenfd == -1) {
|
|
1510 gaim_connection_error(sip->gc, _("Could not create listen socket"));
|
|
1511 return;
|
|
1512 }
|
|
1513
|
|
1514 sip->fd = listenfd;
|
|
1515
|
|
1516 sip->listenport = gaim_network_get_port_from_fd(sip->fd);
|
|
1517 sip->listenfd = sip->fd;
|
|
1518
|
|
1519 sip->listenpa = gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_udp_process, sip->gc);
|
|
1520
|
|
1521 sip->resendtimeout = gaim_timeout_add(2500, (GSourceFunc) resend_timeout, sip);
|
|
1522 sip->registertimeout = gaim_timeout_add((rand()%100)+10*1000, (GSourceFunc)subscribe_timeout, sip);
|
|
1523 do_register(sip);
|
|
1524 }
|
|
1525
|
|
1526 static void simple_udp_host_resolved(GSList *hosts, gpointer data, const char *error_message) {
|
|
1527 struct simple_account_data *sip = (struct simple_account_data*) data;
|
|
1528 int addr_size;
|
|
1529
|
14238
|
1530 sip->query_data = NULL;
|
|
1531
|
14192
|
1532 if (!hosts || !hosts->data) {
|
|
1533 gaim_connection_error(sip->gc, _("Couldn't resolve host"));
|
|
1534 return;
|
|
1535 }
|
|
1536
|
|
1537 addr_size = GPOINTER_TO_INT(hosts->data);
|
|
1538 hosts = g_slist_remove(hosts, hosts->data);
|
|
1539 memcpy(&(sip->serveraddr), hosts->data, addr_size);
|
|
1540 g_free(hosts->data);
|
|
1541 hosts = g_slist_remove(hosts, hosts->data);
|
|
1542 while(hosts) {
|
|
1543 hosts = g_slist_remove(hosts, hosts->data);
|
|
1544 g_free(hosts->data);
|
|
1545 hosts = g_slist_remove(hosts, hosts->data);
|
|
1546 }
|
|
1547
|
|
1548 /* create socket for incoming connections */
|
|
1549 if(!gaim_network_listen_range(5060, 5160, SOCK_DGRAM,
|
|
1550 simple_udp_host_resolved_listen_cb, sip)) {
|
|
1551 gaim_connection_error(sip->gc, _("Could not create listen socket"));
|
|
1552 return;
|
|
1553 }
|
|
1554 }
|
|
1555
|
|
1556 static void
|
|
1557 simple_tcp_connect_listen_cb(int listenfd, gpointer data) {
|
|
1558 struct simple_account_data *sip = (struct simple_account_data*) data;
|
14262
|
1559 GaimProxyConnectData *connect_data;
|
14192
|
1560
|
|
1561 sip->listenfd = listenfd;
|
|
1562 if(sip->listenfd == -1) {
|
|
1563 gaim_connection_error(sip->gc, _("Could not create listen socket"));
|
|
1564 return;
|
|
1565 }
|
|
1566
|
|
1567 gaim_debug_info("simple", "listenfd: %d\n", sip->listenfd);
|
|
1568 sip->listenport = gaim_network_get_port_from_fd(sip->listenfd);
|
|
1569 sip->listenpa = gaim_input_add(sip->listenfd, GAIM_INPUT_READ,
|
|
1570 simple_newconn_cb, sip->gc);
|
|
1571 gaim_debug_info("simple", "connecting to %s port %d\n",
|
|
1572 sip->realhostname, sip->realport);
|
|
1573 /* open tcp connection to the server */
|
14262
|
1574 connect_data = gaim_proxy_connect(sip->account, sip->realhostname,
|
14192
|
1575 sip->realport, login_cb, sip->gc);
|
14262
|
1576 if(connect_data == NULL) {
|
14192
|
1577 gaim_connection_error(sip->gc, _("Couldn't create socket"));
|
|
1578 }
|
|
1579 }
|
|
1580
|
|
1581 static void srvresolved(GaimSrvResponse *resp, int results, gpointer data) {
|
|
1582 GaimConnection *gc;
|
|
1583 struct simple_account_data *sip;
|
|
1584 gchar *hostname;
|
|
1585 int port;
|
|
1586
|
|
1587 gc = data;
|
|
1588 if (!GAIM_CONNECTION_IS_VALID(gc))
|
|
1589 {
|
|
1590 /* This connection has been closed */
|
|
1591 g_free(resp);
|
|
1592 return;
|
|
1593 }
|
|
1594
|
|
1595 sip = gc->proto_data;
|
|
1596 port = gaim_account_get_int(sip->account, "port", 0);
|
|
1597
|
|
1598 /* find the host to connect to */
|
|
1599 if(results) {
|
|
1600 hostname = g_strdup(resp->hostname);
|
|
1601 if(!port)
|
|
1602 port = resp->port;
|
|
1603 g_free(resp);
|
|
1604 } else {
|
|
1605 if(!gaim_account_get_bool(sip->account, "useproxy", FALSE)) {
|
|
1606 hostname = g_strdup(sip->servername);
|
|
1607 } else {
|
|
1608 hostname = g_strdup(gaim_account_get_string(sip->account, "proxy", sip->servername));
|
|
1609 }
|
|
1610 }
|
|
1611
|
|
1612 sip->realhostname = hostname;
|
|
1613 sip->realport = port;
|
|
1614 if(!sip->realport) sip->realport = 5060;
|
|
1615
|
|
1616 /* TCP case */
|
|
1617 if(!sip->udp) {
|
|
1618 /* create socket for incoming connections */
|
|
1619 if(!gaim_network_listen_range(5060, 5160, SOCK_STREAM,
|
|
1620 simple_tcp_connect_listen_cb, sip)) {
|
|
1621 gaim_connection_error(sip->gc, _("Could not create listen socket"));
|
|
1622 return;
|
|
1623 }
|
|
1624 } else { /* UDP */
|
|
1625 gaim_debug_info("simple", "using udp with server %s and port %d\n", hostname, port);
|
|
1626
|
14238
|
1627 sip->query_data = gaim_dnsquery_a(hostname, port, simple_udp_host_resolved, sip);
|
|
1628 if (sip->query_data == NULL) {
|
|
1629 gaim_connection_error(sip->gc, _("Could not resolve hostname"));
|
|
1630 }
|
14192
|
1631 }
|
|
1632 }
|
|
1633
|
|
1634 static void simple_login(GaimAccount *account)
|
|
1635 {
|
|
1636 GaimConnection *gc;
|
|
1637 struct simple_account_data *sip;
|
|
1638 gchar **userserver;
|
|
1639 gchar *hosttoconnect;
|
|
1640
|
|
1641 const char *username = gaim_account_get_username(account);
|
|
1642 gc = gaim_account_get_connection(account);
|
|
1643
|
|
1644 if (strpbrk(username, " \t\v\r\n") != NULL) {
|
|
1645 gc->wants_to_die = TRUE;
|
|
1646 gaim_connection_error(gc, _("SIP usernames may not contain whitespaces or @ symbols"));
|
|
1647 return;
|
|
1648 }
|
|
1649
|
|
1650 gc->proto_data = sip = g_new0(struct simple_account_data, 1);
|
|
1651 sip->gc = gc;
|
|
1652 sip->account = account;
|
|
1653 sip->registerexpire = 900;
|
|
1654 sip->udp = gaim_account_get_bool(account, "udp", FALSE);
|
|
1655 /* TODO: is there a good default grow size? */
|
|
1656 if(!sip->udp)
|
|
1657 sip->txbuf = gaim_circ_buffer_new(0);
|
|
1658
|
|
1659 userserver = g_strsplit(username, "@", 2);
|
|
1660 gaim_connection_set_display_name(gc, userserver[0]);
|
|
1661 sip->username = g_strdup(userserver[0]);
|
|
1662 sip->servername = g_strdup(userserver[1]);
|
|
1663 sip->password = g_strdup(gaim_connection_get_password(gc));
|
|
1664 g_strfreev(userserver);
|
|
1665
|
|
1666 sip->buddies = g_hash_table_new((GHashFunc)simple_ht_hash_nick, (GEqualFunc)simple_ht_equals_nick);
|
|
1667
|
|
1668 gaim_connection_update_progress(gc, _("Connecting"), 1, 2);
|
|
1669
|
|
1670 /* TODO: Set the status correctly. */
|
|
1671 sip->status = g_strdup("available");
|
|
1672
|
|
1673 if(!gaim_account_get_bool(account, "useproxy", FALSE)) {
|
|
1674 hosttoconnect = g_strdup(sip->servername);
|
|
1675 } else {
|
|
1676 hosttoconnect = g_strdup(gaim_account_get_string(account, "proxy", sip->servername));
|
|
1677 }
|
|
1678
|
|
1679 /* TCP case */
|
|
1680 if(!sip->udp) {
|
|
1681 gaim_srv_resolve("sip", "tcp", hosttoconnect, srvresolved, gc);
|
|
1682 } else { /* UDP */
|
|
1683 gaim_srv_resolve("sip", "udp", hosttoconnect, srvresolved, gc);
|
|
1684 }
|
|
1685 g_free(hosttoconnect);
|
|
1686 }
|
|
1687
|
|
1688 static void simple_close(GaimConnection *gc)
|
|
1689 {
|
|
1690 struct simple_account_data *sip = gc->proto_data;
|
|
1691
|
|
1692 if(sip) {
|
|
1693 /* unregister */
|
|
1694 do_register_exp(sip, 0);
|
|
1695 connection_free_all(sip);
|
|
1696
|
14238
|
1697 if (sip->query_data != NULL)
|
|
1698 gaim_dnsquery_destroy(sip->query_data);
|
|
1699
|
14192
|
1700 g_free(sip->servername);
|
|
1701 g_free(sip->username);
|
|
1702 g_free(sip->password);
|
|
1703 g_free(sip->registrar.nonce);
|
|
1704 g_free(sip->registrar.opaque);
|
|
1705 g_free(sip->registrar.target);
|
|
1706 g_free(sip->registrar.realm);
|
|
1707 g_free(sip->registrar.digest_session_key);
|
|
1708 g_free(sip->proxy.nonce);
|
|
1709 g_free(sip->proxy.opaque);
|
|
1710 g_free(sip->proxy.target);
|
|
1711 g_free(sip->proxy.realm);
|
|
1712 g_free(sip->proxy.digest_session_key);
|
|
1713 if(sip->txbuf)
|
|
1714 gaim_circ_buffer_destroy(sip->txbuf);
|
|
1715 g_free(sip->realhostname);
|
|
1716 if(sip->listenpa) gaim_input_remove(sip->listenpa);
|
|
1717 if(sip->tx_handler) gaim_input_remove(sip->tx_handler);
|
|
1718 if(sip->resendtimeout) gaim_timeout_remove(sip->resendtimeout);
|
|
1719 if(sip->registertimeout) gaim_timeout_remove(sip->registertimeout);
|
|
1720 }
|
|
1721 g_free(gc->proto_data);
|
|
1722 gc->proto_data = NULL;
|
|
1723 }
|
|
1724
|
|
1725 /* not needed since privacy is checked for every subscribe */
|
|
1726 static void dummy_add_deny(GaimConnection *gc, const char *name) {
|
|
1727 }
|
|
1728
|
|
1729 static void dummy_permit_deny(GaimConnection *gc) {
|
|
1730 }
|
|
1731
|
|
1732 static GaimPluginProtocolInfo prpl_info =
|
|
1733 {
|
|
1734 0,
|
|
1735 NULL, /* user_splits */
|
|
1736 NULL, /* protocol_options */
|
|
1737 NO_BUDDY_ICONS, /* icon_spec */
|
|
1738 simple_list_icon, /* list_icon */
|
|
1739 NULL, /* list_emblems */
|
|
1740 NULL, /* status_text */
|
|
1741 NULL, /* tooltip_text */
|
|
1742 simple_status_types, /* away_states */
|
|
1743 NULL, /* blist_node_menu */
|
|
1744 NULL, /* chat_info */
|
|
1745 NULL, /* chat_info_defaults */
|
|
1746 simple_login, /* login */
|
|
1747 simple_close, /* close */
|
|
1748 simple_im_send, /* send_im */
|
|
1749 NULL, /* set_info */
|
|
1750 simple_typing, /* send_typing */
|
|
1751 NULL, /* get_info */
|
|
1752 simple_set_status, /* set_status */
|
|
1753 NULL, /* set_idle */
|
|
1754 NULL, /* change_passwd */
|
|
1755 simple_add_buddy, /* add_buddy */
|
|
1756 NULL, /* add_buddies */
|
|
1757 simple_remove_buddy, /* remove_buddy */
|
|
1758 NULL, /* remove_buddies */
|
|
1759 dummy_add_deny, /* add_permit */
|
|
1760 dummy_add_deny, /* add_deny */
|
|
1761 dummy_add_deny, /* rem_permit */
|
|
1762 dummy_add_deny, /* rem_deny */
|
|
1763 dummy_permit_deny, /* set_permit_deny */
|
|
1764 NULL, /* join_chat */
|
|
1765 NULL, /* reject_chat */
|
|
1766 NULL, /* get_chat_name */
|
|
1767 NULL, /* chat_invite */
|
|
1768 NULL, /* chat_leave */
|
|
1769 NULL, /* chat_whisper */
|
|
1770 NULL, /* chat_send */
|
|
1771 simple_keep_alive, /* keepalive */
|
|
1772 NULL, /* register_user */
|
|
1773 NULL, /* get_cb_info */
|
|
1774 NULL, /* get_cb_away */
|
|
1775 NULL, /* alias_buddy */
|
|
1776 NULL, /* group_buddy */
|
|
1777 NULL, /* rename_group */
|
|
1778 NULL, /* buddy_free */
|
|
1779 NULL, /* convo_closed */
|
|
1780 NULL, /* normalize */
|
|
1781 NULL, /* set_buddy_icon */
|
|
1782 NULL, /* remove_group */
|
|
1783 NULL, /* get_cb_real_name */
|
|
1784 NULL, /* set_chat_topic */
|
|
1785 NULL, /* find_blist_chat */
|
|
1786 NULL, /* roomlist_get_list */
|
|
1787 NULL, /* roomlist_cancel */
|
|
1788 NULL, /* roomlist_expand_category */
|
|
1789 NULL, /* can_receive_file */
|
|
1790 NULL, /* send_file */
|
|
1791 NULL, /* new_xfer */
|
|
1792 NULL, /* offline_message */
|
|
1793 NULL, /* whiteboard_prpl_ops */
|
|
1794 };
|
|
1795
|
|
1796
|
|
1797 static GaimPluginInfo info =
|
|
1798 {
|
|
1799 GAIM_PLUGIN_MAGIC,
|
|
1800 GAIM_MAJOR_VERSION,
|
|
1801 GAIM_MINOR_VERSION,
|
|
1802 GAIM_PLUGIN_PROTOCOL, /**< type */
|
|
1803 NULL, /**< ui_requirement */
|
|
1804 0, /**< flags */
|
|
1805 NULL, /**< dependencies */
|
|
1806 GAIM_PRIORITY_DEFAULT, /**< priority */
|
|
1807
|
|
1808 "prpl-simple", /**< id */
|
|
1809 "SIMPLE", /**< name */
|
|
1810 VERSION, /**< version */
|
|
1811 N_("SIP/SIMPLE Protocol Plugin"), /** summary */
|
|
1812 N_("The SIP/SIMPLE Protocol Plugin"), /** description */
|
|
1813 "Thomas Butter <butter@uni-mannheim.de>", /**< author */
|
|
1814 GAIM_WEBSITE, /**< homepage */
|
|
1815
|
|
1816 NULL, /**< load */
|
|
1817 NULL, /**< unload */
|
|
1818 NULL, /**< destroy */
|
|
1819
|
|
1820 NULL, /**< ui_info */
|
|
1821 &prpl_info, /**< extra_info */
|
|
1822 NULL,
|
|
1823 NULL
|
|
1824 };
|
|
1825
|
|
1826 static void _init_plugin(GaimPlugin *plugin)
|
|
1827 {
|
|
1828 GaimAccountUserSplit *split;
|
|
1829 GaimAccountOption *option;
|
|
1830
|
|
1831 split = gaim_account_user_split_new(_("Server"), "", '@');
|
|
1832 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split);
|
|
1833
|
|
1834 option = gaim_account_option_bool_new(_("Publish status (note: everyone may watch you)"), "dopublish", TRUE);
|
|
1835 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1836
|
|
1837 option = gaim_account_option_int_new(_("Connect port"), "port", 0);
|
|
1838 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1839
|
|
1840 option = gaim_account_option_bool_new(_("Use UDP"), "udp", FALSE);
|
|
1841 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1842 option = gaim_account_option_bool_new(_("Use proxy"), "useproxy", FALSE);
|
|
1843 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1844 option = gaim_account_option_string_new(_("Proxy"), "proxy", "");
|
|
1845 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1846 option = gaim_account_option_string_new(_("Auth User"), "authuser", "");
|
|
1847 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1848 option = gaim_account_option_string_new(_("Auth Domain"), "authdomain", "");
|
|
1849 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1850 }
|
|
1851
|
|
1852 GAIM_INIT_PLUGIN(simple, _init_plugin, info);
|