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
|
14542
|
524 static void simple_send_raw(GaimConnection *gc, const char *buf, int len)
|
|
525 {
|
|
526 sendout_pkt(gc, buf);
|
|
527 return len;
|
|
528 }
|
|
529
|
14192
|
530 static void sendout_sipmsg(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
531 GSList *tmp = msg->headers;
|
|
532 gchar *name;
|
|
533 gchar *value;
|
|
534 GString *outstr = g_string_new("");
|
|
535 g_string_append_printf(outstr, "%s %s SIP/2.0\r\n", msg->method, msg->target);
|
|
536 while(tmp) {
|
|
537 name = ((struct siphdrelement*) (tmp->data))->name;
|
|
538 value = ((struct siphdrelement*) (tmp->data))->value;
|
|
539 g_string_append_printf(outstr, "%s: %s\r\n", name, value);
|
|
540 tmp = g_slist_next(tmp);
|
|
541 }
|
|
542 g_string_append_printf(outstr, "\r\n%s", msg->body ? msg->body : "");
|
|
543 sendout_pkt(sip->gc, outstr->str);
|
|
544 g_string_free(outstr, TRUE);
|
|
545 }
|
|
546
|
|
547 static void send_sip_response(GaimConnection *gc, struct sipmsg *msg, int code,
|
|
548 const char *text, const char *body) {
|
|
549 GSList *tmp = msg->headers;
|
|
550 gchar *name;
|
|
551 gchar *value;
|
|
552 GString *outstr = g_string_new("");
|
|
553
|
|
554 /* When sending the acknowlegements and errors, the content length from the original
|
|
555 message is still here, but there is no body; we need to make sure we're sending the
|
|
556 correct content length */
|
|
557 sipmsg_remove_header(msg, "Content-Length");
|
|
558 if(body) {
|
|
559 gchar len[12];
|
|
560 sprintf(len, "%" G_GSIZE_FORMAT , strlen(body));
|
|
561 sipmsg_add_header(msg, "Content-Length", len);
|
|
562 }
|
|
563 else
|
|
564 sipmsg_add_header(msg, "Content-Length", "0");
|
|
565 g_string_append_printf(outstr, "SIP/2.0 %d %s\r\n", code, text);
|
|
566 while(tmp) {
|
|
567 name = ((struct siphdrelement*) (tmp->data))->name;
|
|
568 value = ((struct siphdrelement*) (tmp->data))->value;
|
|
569
|
|
570 g_string_append_printf(outstr, "%s: %s\r\n", name, value);
|
|
571 tmp = g_slist_next(tmp);
|
|
572 }
|
|
573 g_string_append_printf(outstr, "\r\n%s", body ? body : "");
|
|
574 sendout_pkt(gc, outstr->str);
|
|
575 g_string_free(outstr, TRUE);
|
|
576 }
|
|
577
|
|
578 static void transactions_remove(struct simple_account_data *sip, struct transaction *trans) {
|
|
579 if(trans->msg) sipmsg_free(trans->msg);
|
|
580 sip->transactions = g_slist_remove(sip->transactions, trans);
|
|
581 g_free(trans);
|
|
582 }
|
|
583
|
|
584 static void transactions_add_buf(struct simple_account_data *sip, const gchar *buf, void *callback) {
|
|
585 struct transaction *trans = g_new0(struct transaction, 1);
|
|
586 trans->time = time(NULL);
|
|
587 trans->msg = sipmsg_parse_msg(buf);
|
|
588 trans->cseq = sipmsg_find_header(trans->msg, "CSeq");
|
|
589 trans->callback = callback;
|
|
590 sip->transactions = g_slist_append(sip->transactions, trans);
|
|
591 }
|
|
592
|
|
593 static struct transaction *transactions_find(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
594 struct transaction *trans;
|
|
595 GSList *transactions = sip->transactions;
|
|
596 gchar *cseq = sipmsg_find_header(msg, "CSeq");
|
|
597
|
|
598 while(transactions) {
|
|
599 trans = transactions->data;
|
|
600 if(!strcmp(trans->cseq, cseq)) {
|
|
601 return trans;
|
|
602 }
|
|
603 transactions = transactions->next;
|
|
604 }
|
|
605
|
|
606 return NULL;
|
|
607 }
|
|
608
|
|
609 static void send_sip_request(GaimConnection *gc, const gchar *method,
|
|
610 const gchar *url, const gchar *to, const gchar *addheaders,
|
|
611 const gchar *body, struct sip_dialog *dialog, TransCallback tc) {
|
|
612 struct simple_account_data *sip = gc->proto_data;
|
|
613 char *callid = dialog ? g_strdup(dialog->callid) : gencallid();
|
|
614 char *auth = NULL;
|
|
615 const char *addh = "";
|
|
616 gchar *branch = genbranch();
|
|
617 gchar *tag = NULL;
|
|
618 char *buf;
|
|
619
|
|
620 if(!strcmp(method, "REGISTER")) {
|
|
621 if(sip->regcallid) {
|
|
622 g_free(callid);
|
|
623 callid = g_strdup(sip->regcallid);
|
|
624 }
|
|
625 else sip->regcallid = g_strdup(callid);
|
|
626 }
|
|
627
|
|
628 if(addheaders) addh = addheaders;
|
|
629 if(sip->registrar.type && !strcmp(method, "REGISTER")) {
|
|
630 buf = auth_header(sip, &sip->registrar, method, url);
|
|
631 auth = g_strdup_printf("Authorization: %s", buf);
|
|
632 g_free(buf);
|
|
633 gaim_debug(GAIM_DEBUG_MISC, "simple", "header %s", auth);
|
|
634 }
|
|
635
|
|
636 if(sip->proxy.type && strcmp(method, "REGISTER")) {
|
|
637 buf = auth_header(sip, &sip->proxy, method, url);
|
|
638 auth = g_strdup_printf("Proxy-Authorization: %s", buf);
|
|
639 g_free(buf);
|
|
640 gaim_debug(GAIM_DEBUG_MISC, "simple", "header %s", auth);
|
|
641 }
|
|
642
|
|
643 if (!dialog)
|
|
644 tag = gentag();
|
|
645
|
|
646 buf = g_strdup_printf("%s %s SIP/2.0\r\n"
|
|
647 "Via: SIP/2.0/%s %s:%d;branch=%s\r\n"
|
|
648 /* Don't know what epid is, but LCS wants it */
|
|
649 "From: <sip:%s@%s>;tag=%s;epid=1234567890\r\n"
|
|
650 "To: <%s>%s%s\r\n"
|
|
651 "Max-Forwards: 10\r\n"
|
|
652 "CSeq: %d %s\r\n"
|
|
653 "User-Agent: Gaim/" VERSION "\r\n"
|
|
654 "Call-ID: %s\r\n"
|
|
655 "%s%s"
|
|
656 "Content-Length: %" G_GSIZE_FORMAT "\r\n\r\n%s",
|
|
657 method,
|
|
658 url,
|
|
659 sip->udp ? "UDP" : "TCP",
|
|
660 gaim_network_get_my_ip(-1),
|
|
661 sip->listenport,
|
|
662 branch,
|
|
663 sip->username,
|
|
664 sip->servername,
|
|
665 dialog ? dialog->ourtag : tag,
|
|
666 to,
|
|
667 dialog ? ";tag=" : "",
|
|
668 dialog ? dialog->theirtag : "",
|
|
669 ++sip->cseq,
|
|
670 method,
|
|
671 callid,
|
|
672 auth ? auth : "",
|
|
673 addh,
|
|
674 strlen(body),
|
|
675 body);
|
|
676
|
|
677 g_free(tag);
|
|
678 g_free(auth);
|
|
679 g_free(branch);
|
|
680 g_free(callid);
|
|
681
|
|
682 /* add to ongoing transactions */
|
|
683
|
|
684 transactions_add_buf(sip, buf, tc);
|
|
685
|
|
686 sendout_pkt(gc, buf);
|
|
687
|
|
688 g_free(buf);
|
|
689 }
|
|
690
|
|
691 static char *get_contact(struct simple_account_data *sip) {
|
|
692 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");
|
|
693 }
|
|
694
|
|
695 static void do_register_exp(struct simple_account_data *sip, int expire) {
|
|
696 char *uri = g_strdup_printf("sip:%s", sip->servername);
|
|
697 char *to = g_strdup_printf("sip:%s@%s", sip->username, sip->servername);
|
|
698 char *contact = get_contact(sip);
|
|
699 char *hdr = g_strdup_printf("Contact: %s\r\nExpires: %d\r\n", contact, expire);
|
|
700 g_free(contact);
|
|
701
|
|
702 sip->registerstatus = 1;
|
|
703
|
|
704 if(expire) {
|
|
705 sip->reregister = time(NULL) + expire - 50;
|
|
706 } else {
|
|
707 sip->reregister = time(NULL) + 600;
|
|
708 }
|
|
709
|
|
710 send_sip_request(sip->gc, "REGISTER", uri, to, hdr, "", NULL,
|
|
711 process_register_response);
|
|
712
|
|
713 g_free(hdr);
|
|
714 g_free(uri);
|
|
715 g_free(to);
|
|
716 }
|
|
717
|
|
718 static void do_register(struct simple_account_data *sip) {
|
|
719 do_register_exp(sip, sip->registerexpire);
|
|
720 }
|
|
721
|
|
722 static gchar *parse_from(const gchar *hdr) {
|
|
723 gchar *from;
|
|
724 const gchar *tmp, *tmp2 = hdr;
|
|
725
|
|
726 if(!hdr) return NULL;
|
|
727 gaim_debug_info("simple", "parsing address out of %s\n", hdr);
|
|
728 tmp = strchr(hdr, '<');
|
|
729
|
|
730 /* i hate the different SIP UA behaviours... */
|
|
731 if(tmp) { /* sip address in <...> */
|
|
732 tmp2 = tmp + 1;
|
|
733 tmp = strchr(tmp2, '>');
|
|
734 if(tmp) {
|
|
735 from = g_strndup(tmp2, tmp - tmp2);
|
|
736 } else {
|
|
737 gaim_debug_info("simple", "found < without > in From\n");
|
|
738 return NULL;
|
|
739 }
|
|
740 } else {
|
|
741 tmp = strchr(tmp2, ';');
|
|
742 if(tmp) {
|
|
743 from = g_strndup(tmp2, tmp - tmp2);
|
|
744 } else {
|
|
745 from = g_strdup(tmp2);
|
|
746 }
|
|
747 }
|
|
748 gaim_debug_info("simple", "got %s\n", from);
|
|
749 return from;
|
|
750 }
|
|
751
|
|
752 static gboolean process_subscribe_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) {
|
|
753 gchar *to;
|
|
754
|
|
755 if(msg->response == 200 || msg->response == 202) {
|
|
756 return TRUE;
|
|
757 }
|
|
758
|
|
759 to = parse_from(sipmsg_find_header(tc->msg, "To")); /* cant be NULL since it is our own msg */
|
|
760
|
|
761 /* we can not subscribe -> user is offline (TODO unknown status?) */
|
|
762
|
|
763 gaim_prpl_got_user_status(sip->account, to, "offline", NULL);
|
|
764 g_free(to);
|
|
765 return TRUE;
|
|
766 }
|
|
767
|
|
768 static void simple_subscribe(struct simple_account_data *sip, struct simple_buddy *buddy) {
|
|
769 gchar *contact = "Expires: 1200\r\nAccept: application/pidf+xml, application/xpidf+xml\r\nEvent: presence\r\n";
|
|
770 gchar *to;
|
|
771 gchar *tmp;
|
|
772
|
|
773 if(strstr(buddy->name, "sip:"))
|
|
774 to = g_strdup(buddy->name);
|
|
775 else
|
|
776 to = g_strdup_printf("sip:%s", buddy->name);
|
|
777
|
|
778 tmp = get_contact(sip);
|
|
779 contact = g_strdup_printf("%sContact: %s\r\n", contact, tmp);
|
|
780 g_free(tmp);
|
|
781
|
|
782 /* subscribe to buddy presence
|
|
783 * we dont need to know the status so we do not need a callback */
|
|
784
|
|
785 send_sip_request(sip->gc, "SUBSCRIBE", to, to, contact, "", NULL,
|
|
786 process_subscribe_response);
|
|
787
|
|
788 g_free(to);
|
|
789 g_free(contact);
|
|
790
|
|
791 /* resubscribe before subscription expires */
|
|
792 /* add some jitter */
|
|
793 buddy->resubscribe = time(NULL)+1140+(rand()%50);
|
|
794 }
|
|
795
|
|
796 static gboolean simple_add_lcs_contacts(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) {
|
|
797 gchar *tmp;
|
|
798 xmlnode *item, *group, *isc;
|
|
799 const char *name_group;
|
|
800 GaimBuddy *b;
|
|
801 GaimGroup *g = NULL;
|
|
802 struct simple_buddy *bs;
|
|
803 int len = msg->bodylen;
|
|
804
|
|
805
|
|
806 tmp = sipmsg_find_header(msg, "Event");
|
|
807 if(tmp && !strncmp(tmp, "vnd-microsoft-roaming-contacts", 30)){
|
|
808
|
|
809 gaim_debug_info("simple", "simple_add_lcs_contacts->%s-%d\n", msg->body, len);
|
|
810 /*Convert the contact from XML to Gaim Buddies*/
|
|
811 isc = xmlnode_from_str(msg->body, len);
|
|
812
|
|
813 /* ToDo. Find for all groups */
|
|
814 if ((group = xmlnode_get_child(isc, "group"))) {
|
|
815 name_group = xmlnode_get_attrib(group, "name");
|
|
816 gaim_debug_info("simple", "name_group->%s\n", name_group);
|
|
817 g = gaim_find_group(name_group);
|
|
818 if(!g)
|
|
819 g = gaim_group_new(name_group);
|
|
820 }
|
|
821
|
|
822 if (!g) {
|
|
823 g = gaim_find_group("Buddies");
|
|
824 if(!g)
|
|
825 g = gaim_group_new("Buddies");
|
|
826 }
|
|
827
|
|
828 for(item = xmlnode_get_child(isc, "contact"); item; item = xmlnode_get_next_twin(item))
|
|
829 {
|
|
830 const char *uri, *name, *groups;
|
|
831 char *buddy_name;
|
|
832 uri = xmlnode_get_attrib(item, "uri");
|
|
833 name = xmlnode_get_attrib(item, "name");
|
|
834 groups = xmlnode_get_attrib(item, "groups");
|
|
835 gaim_debug_info("simple", "URI->%s\n", uri);
|
|
836
|
|
837 buddy_name = g_strdup_printf("sip:%s", uri);
|
|
838
|
|
839 b = gaim_find_buddy(sip->account, buddy_name);
|
|
840 if(!b){
|
|
841 b = gaim_buddy_new(sip->account, buddy_name, uri);
|
|
842 }
|
|
843 g_free(buddy_name);
|
|
844
|
|
845 gaim_blist_add_buddy(b, NULL, g, NULL);
|
|
846 gaim_blist_alias_buddy(b, uri);
|
|
847 bs = g_new0(struct simple_buddy, 1);
|
|
848 bs->name = g_strdup(b->name);
|
|
849 g_hash_table_insert(sip->buddies, bs->name, bs);
|
|
850 }
|
|
851 xmlnode_free(isc);
|
|
852 }
|
|
853 return 0;
|
|
854 }
|
|
855
|
|
856 static void simple_subscribe_buddylist(struct simple_account_data *sip) {
|
|
857 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";
|
|
858 gchar *to;
|
|
859 gchar *tmp;
|
|
860 to = g_strdup_printf("sip:%s@%s", sip->username, sip->servername);
|
|
861
|
|
862 tmp = get_contact(sip);
|
|
863
|
|
864 contact = g_strdup_printf("%sContact: %s\r\n", contact, tmp);
|
|
865 g_free(tmp);
|
|
866
|
|
867 send_sip_request(sip->gc, "SUBSCRIBE", to, to, contact, "", NULL, simple_add_lcs_contacts);
|
|
868
|
|
869 g_free(to);
|
|
870 g_free(contact);
|
|
871 }
|
|
872
|
|
873
|
|
874 static void simple_buddy_resub(char *name, struct simple_buddy *buddy, struct simple_account_data *sip) {
|
|
875 time_t curtime = time(NULL);
|
|
876 gaim_debug_info("simple", "buddy resub\n");
|
|
877 if(buddy->resubscribe < curtime) {
|
|
878 gaim_debug(GAIM_DEBUG_MISC, "simple", "simple_buddy_resub %s\n", name);
|
|
879 simple_subscribe(sip, buddy);
|
|
880 }
|
|
881 }
|
|
882
|
|
883 static gboolean resend_timeout(struct simple_account_data *sip) {
|
|
884 GSList *tmp = sip->transactions;
|
|
885 time_t currtime = time(NULL);
|
|
886 while(tmp) {
|
|
887 struct transaction *trans = tmp->data;
|
|
888 tmp = tmp->next;
|
|
889 gaim_debug_info("simple", "have open transaction age: %d\n", currtime- trans->time);
|
|
890 if((currtime - trans->time > 5) && trans->retries >= 1) {
|
|
891 /* TODO 408 */
|
|
892 } else {
|
|
893 if((currtime - trans->time > 2) && trans->retries == 0) {
|
|
894 trans->retries++;
|
|
895 sendout_sipmsg(sip, trans->msg);
|
|
896 }
|
|
897 }
|
|
898 }
|
|
899 return TRUE;
|
|
900 }
|
|
901
|
|
902 static gboolean subscribe_timeout(struct simple_account_data *sip) {
|
|
903 GSList *tmp;
|
|
904 time_t curtime = time(NULL);
|
|
905 /* register again if first registration expires */
|
|
906 if(sip->reregister < curtime) {
|
|
907 do_register(sip);
|
|
908 }
|
|
909 /* check for every subscription if we need to resubscribe */
|
|
910 g_hash_table_foreach(sip->buddies, (GHFunc)simple_buddy_resub, (gpointer)sip);
|
|
911
|
|
912 /* remove a timed out suscriber */
|
|
913
|
|
914 tmp = sip->watcher;
|
|
915 while(tmp) {
|
|
916 struct simple_watcher *watcher = tmp->data;
|
|
917 if(watcher->expire < curtime) {
|
|
918 watcher_remove(sip, watcher->name);
|
|
919 tmp = sip->watcher;
|
|
920 }
|
|
921 if(tmp) tmp = tmp->next;
|
|
922 }
|
|
923
|
|
924 return TRUE;
|
|
925 }
|
|
926
|
|
927 static void simple_send_message(struct simple_account_data *sip, const char *to, const char *msg, const char *type) {
|
|
928 gchar *hdr;
|
|
929 gchar *fullto;
|
|
930 if(strncmp("sip:", to, 4)) {
|
|
931 fullto = g_strdup_printf("sip:%s", to);
|
|
932 } else {
|
|
933 fullto = g_strdup(to);
|
|
934 }
|
|
935 if(type) {
|
|
936 hdr = g_strdup_printf("Content-Type: %s\r\n", type);
|
|
937 } else {
|
|
938 hdr = g_strdup("Content-Type: text/plain\r\n");
|
|
939 }
|
|
940 send_sip_request(sip->gc, "MESSAGE", fullto, fullto, hdr, msg, NULL, NULL);
|
|
941 g_free(hdr);
|
|
942 g_free(fullto);
|
|
943 }
|
|
944
|
|
945 static int simple_im_send(GaimConnection *gc, const char *who, const char *what, GaimMessageFlags flags) {
|
|
946 struct simple_account_data *sip = gc->proto_data;
|
|
947 char *to = g_strdup(who);
|
|
948 char *text = gaim_unescape_html(what);
|
|
949 simple_send_message(sip, to, text, NULL);
|
|
950 g_free(to);
|
|
951 g_free(text);
|
|
952 return 1;
|
|
953 }
|
|
954
|
|
955 static void process_incoming_message(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
956 gchar *from;
|
|
957 gchar *contenttype;
|
|
958 gboolean found = FALSE;
|
|
959
|
|
960 from = parse_from(sipmsg_find_header(msg, "From"));
|
|
961
|
|
962 if(!from) return;
|
|
963
|
|
964 gaim_debug(GAIM_DEBUG_MISC, "simple", "got message from %s: %s\n", from, msg->body);
|
|
965
|
|
966 contenttype = sipmsg_find_header(msg, "Content-Type");
|
|
967 if(!contenttype || !strncmp(contenttype, "text/plain", 10) || !strncmp(contenttype, "text/html", 9)) {
|
|
968 serv_got_im(sip->gc, from, msg->body, 0, time(NULL));
|
|
969 send_sip_response(sip->gc, msg, 200, "OK", NULL);
|
|
970 found = TRUE;
|
|
971 }
|
|
972 if(!strncmp(contenttype, "application/im-iscomposing+xml", 30)) {
|
|
973 xmlnode *isc = xmlnode_from_str(msg->body, msg->bodylen);
|
|
974 xmlnode *state;
|
|
975 gchar *statedata;
|
|
976
|
|
977 if(!isc) {
|
|
978 gaim_debug_info("simple", "process_incoming_message: can not parse iscomposing\n");
|
|
979 return;
|
|
980 }
|
|
981
|
|
982 state = xmlnode_get_child(isc, "state");
|
|
983
|
|
984 if(!state) {
|
|
985 gaim_debug_info("simple", "process_incoming_message: no state found\n");
|
|
986 xmlnode_free(isc);
|
|
987 return;
|
|
988 }
|
|
989
|
|
990 statedata = xmlnode_get_data(state);
|
|
991 if(statedata) {
|
|
992 if(strstr(statedata, "active")) serv_got_typing(sip->gc, from, 0, GAIM_TYPING);
|
|
993 else serv_got_typing_stopped(sip->gc, from);
|
|
994
|
|
995 g_free(statedata);
|
|
996 }
|
|
997 xmlnode_free(isc);
|
|
998 send_sip_response(sip->gc, msg, 200, "OK", NULL);
|
|
999 found = TRUE;
|
|
1000 }
|
|
1001 if(!found) {
|
|
1002 gaim_debug_info("simple", "got unknown mime-type");
|
|
1003 send_sip_response(sip->gc, msg, 415, "Unsupported media type", NULL);
|
|
1004 }
|
|
1005 g_free(from);
|
|
1006 }
|
|
1007
|
|
1008
|
|
1009 gboolean process_register_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) {
|
|
1010 gchar *tmp;
|
|
1011 gaim_debug(GAIM_DEBUG_MISC, "simple", "in process register response response: %d\n", msg->response);
|
|
1012 switch (msg->response) {
|
|
1013 case 200:
|
|
1014 if(sip->registerstatus < 3) { /* registered */
|
|
1015 if(gaim_account_get_bool(sip->account, "dopublish", TRUE)) {
|
|
1016 send_publish(sip);
|
|
1017 }
|
|
1018 }
|
|
1019 sip->registerstatus = 3;
|
|
1020 gaim_connection_set_state(sip->gc, GAIM_CONNECTED);
|
|
1021
|
|
1022 /* get buddies from blist */
|
|
1023 simple_get_buddies(sip->gc);
|
|
1024
|
|
1025 subscribe_timeout(sip);
|
|
1026 tmp = sipmsg_find_header(msg, "Allow-Events");
|
|
1027 if(tmp && strstr(tmp, "vnd-microsoft-provisioning")){
|
|
1028 simple_subscribe_buddylist(sip);
|
|
1029 }
|
|
1030
|
|
1031 break;
|
|
1032 case 401:
|
|
1033 if(sip->registerstatus != 2) {
|
|
1034 gaim_debug_info("simple", "REGISTER retries %d\n", sip->registrar.retries);
|
|
1035 if(sip->registrar.retries > 3) {
|
|
1036 sip->gc->wants_to_die = TRUE;
|
|
1037 gaim_connection_error(sip->gc, _("Wrong Password"));
|
|
1038 return TRUE;
|
|
1039 }
|
|
1040 tmp = sipmsg_find_header(msg, "WWW-Authenticate");
|
|
1041 fill_auth(sip, tmp, &sip->registrar);
|
|
1042 sip->registerstatus = 2;
|
|
1043 do_register(sip);
|
|
1044 }
|
|
1045 break;
|
|
1046 }
|
|
1047 return TRUE;
|
|
1048 }
|
|
1049
|
|
1050 static void process_incoming_notify(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
1051 gchar *from;
|
|
1052 gchar *fromhdr;
|
|
1053 gchar *tmp2;
|
|
1054 xmlnode *pidf;
|
|
1055 xmlnode *basicstatus = NULL, *tuple, *status;
|
|
1056 gboolean isonline = FALSE;
|
|
1057
|
|
1058 fromhdr = sipmsg_find_header(msg, "From");
|
|
1059 from = parse_from(fromhdr);
|
|
1060 if(!from) return;
|
|
1061
|
|
1062 pidf = xmlnode_from_str(msg->body, msg->bodylen);
|
|
1063
|
|
1064 if(!pidf) {
|
|
1065 gaim_debug_info("simple", "process_incoming_notify: no parseable pidf\n");
|
|
1066 return;
|
|
1067 }
|
|
1068
|
|
1069 if ((tuple = xmlnode_get_child(pidf, "tuple")))
|
|
1070 if ((status = xmlnode_get_child(tuple, "status")))
|
|
1071 basicstatus = xmlnode_get_child(status, "basic");
|
|
1072
|
|
1073 if(!basicstatus) {
|
|
1074 gaim_debug_info("simple", "process_incoming_notify: no basic found\n");
|
|
1075 xmlnode_free(pidf);
|
|
1076 return;
|
|
1077 }
|
|
1078
|
|
1079 tmp2 = xmlnode_get_data(basicstatus);
|
|
1080
|
|
1081 if(!tmp2) {
|
|
1082 gaim_debug_info("simple", "process_incoming_notify: no basic data found\n");
|
|
1083 xmlnode_free(pidf);
|
|
1084 return;
|
|
1085 }
|
|
1086
|
|
1087 if(strstr(tmp2, "open")) {
|
|
1088 isonline = TRUE;
|
|
1089 }
|
|
1090
|
|
1091 g_free(tmp2);
|
|
1092
|
|
1093 if(isonline) gaim_prpl_got_user_status(sip->account, from, "available", NULL);
|
|
1094 else gaim_prpl_got_user_status(sip->account, from, "offline", NULL);
|
|
1095
|
|
1096 xmlnode_free(pidf);
|
|
1097
|
|
1098 g_free(from);
|
|
1099 send_sip_response(sip->gc, msg, 200, "OK", NULL);
|
|
1100 }
|
|
1101
|
|
1102 static unsigned int simple_typing(GaimConnection *gc, const char *name, GaimTypingState state) {
|
|
1103 struct simple_account_data *sip = gc->proto_data;
|
|
1104
|
|
1105 gchar *xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
|
1106 "<isComposing xmlns=\"urn:ietf:params:xml:ns:im-iscomposing\"\n"
|
|
1107 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
|
|
1108 "xsi:schemaLocation=\"urn:ietf:params:xml:ns:im-composing iscomposing.xsd\">\n"
|
|
1109 "<state>%s</state>\n"
|
|
1110 "<contenttype>text/plain</contenttype>\n"
|
|
1111 "<refresh>60</refresh>\n"
|
|
1112 "</isComposing>";
|
|
1113 gchar *recv = g_strdup(name);
|
|
1114 if(state == GAIM_TYPING) {
|
|
1115 gchar *msg = g_strdup_printf(xml, "active");
|
|
1116 simple_send_message(sip, recv, msg, "application/im-iscomposing+xml");
|
|
1117 g_free(msg);
|
|
1118 } else /* TODO: Only if (state == GAIM_TYPED) ? */ {
|
|
1119 gchar *msg = g_strdup_printf(xml, "idle");
|
|
1120 simple_send_message(sip, recv, msg, "application/im-iscomposing+xml");
|
|
1121 g_free(msg);
|
|
1122 }
|
|
1123 g_free(recv);
|
|
1124 /*
|
|
1125 * TODO: Is this right? It will cause the core to call
|
|
1126 * serv_send_typing(gc, who, GAIM_TYPING) once every second
|
|
1127 * until the user stops typing. If that's not desired,
|
|
1128 * then return 0 instead.
|
|
1129 */
|
|
1130 return 1;
|
|
1131 }
|
|
1132
|
|
1133 static gchar *find_tag(const gchar *hdr) {
|
|
1134 const gchar *tmp = strstr(hdr, ";tag="), *tmp2;
|
|
1135
|
|
1136 if(!tmp) return NULL;
|
|
1137 tmp += 5;
|
|
1138 if((tmp2 = strchr(tmp, ';'))) {
|
|
1139 return g_strndup(tmp, tmp2 - tmp);
|
|
1140 }
|
|
1141 return g_strdup(tmp);
|
|
1142 }
|
|
1143
|
|
1144 static gchar* gen_xpidf(struct simple_account_data *sip) {
|
|
1145 gchar *doc = g_strdup_printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
|
1146 "<presence>\n"
|
|
1147 "<presentity uri=\"sip:%s@%s;method=SUBSCRIBE\"/>\n"
|
|
1148 "<display name=\"sip:%s@%s\"/>\n"
|
|
1149 "<atom id=\"1234\">\n"
|
|
1150 "<address uri=\"sip:%s@%s\">\n"
|
|
1151 "<status status=\"%s\"/>\n"
|
|
1152 "</address>\n"
|
|
1153 "</atom>\n"
|
|
1154 "</presence>\n",
|
|
1155 sip->username,
|
|
1156 sip->servername,
|
|
1157 sip->username,
|
|
1158 sip->servername,
|
|
1159 sip->username,
|
|
1160 sip->servername,
|
|
1161 sip->status);
|
|
1162 return doc;
|
|
1163 }
|
|
1164
|
|
1165
|
|
1166
|
|
1167 static gchar* gen_pidf(struct simple_account_data *sip) {
|
|
1168 gchar *doc = g_strdup_printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
|
1169 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n"
|
|
1170 "xmlns:im=\"urn:ietf:params:xml:ns:pidf:im\"\n"
|
|
1171 "entity=\"sip:%s@%s\">\n"
|
|
1172 "<tuple id=\"bs35r9f\">\n"
|
|
1173 "<status>\n"
|
|
1174 "<basic>open</basic>\n"
|
|
1175 "</status>\n"
|
|
1176 "<note>%s</note>\n"
|
|
1177 "</tuple>\n"
|
|
1178 "</presence>",
|
|
1179 sip->username,
|
|
1180 sip->servername,
|
|
1181 sip->status);
|
|
1182 return doc;
|
|
1183 }
|
|
1184
|
|
1185 static void send_notify(struct simple_account_data *sip, struct simple_watcher *watcher) {
|
|
1186 gchar *doc = watcher->needsxpidf ? gen_xpidf(sip) : gen_pidf(sip);
|
|
1187 gchar *hdr = watcher->needsxpidf ? "Event: presence\r\nContent-Type: application/xpidf+xml\r\n" : "Event: presence\r\nContent-Type: application/pidf+xml\r\n";
|
|
1188 send_sip_request(sip->gc, "NOTIFY", watcher->name, watcher->name, hdr, doc, &watcher->dialog, NULL);
|
|
1189 g_free(doc);
|
|
1190 }
|
|
1191
|
|
1192 static gboolean process_publish_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) {
|
|
1193 if(msg->response != 200 && msg->response != 408) {
|
|
1194 /* never send again */
|
|
1195 sip->republish = -1;
|
|
1196 }
|
|
1197 return TRUE;
|
|
1198 }
|
|
1199
|
|
1200 static void send_publish(struct simple_account_data *sip) {
|
|
1201 gchar *uri = g_strdup_printf("sip:%s@%s", sip->username, sip->servername);
|
|
1202 gchar *doc = gen_pidf(sip);
|
|
1203 send_sip_request(sip->gc, "PUBLISH", uri, uri,
|
|
1204 "Expires: 600\r\nEvent: presence\r\n"
|
|
1205 "Content-Type: application/pidf+xml\r\n",
|
|
1206 doc, NULL, process_publish_response);
|
|
1207 sip->republish = time(NULL) + 500;
|
|
1208 g_free(uri);
|
|
1209 g_free(doc);
|
|
1210 }
|
|
1211
|
|
1212 static void process_incoming_subscribe(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
1213 const char *from_hdr = sipmsg_find_header(msg, "From");
|
|
1214 gchar *from = parse_from(from_hdr);
|
|
1215 gchar *theirtag = find_tag(from_hdr);
|
|
1216 gchar *ourtag = find_tag(sipmsg_find_header(msg, "To"));
|
|
1217 gboolean tagadded = FALSE;
|
|
1218 gchar *callid = sipmsg_find_header(msg, "Call-ID");
|
|
1219 gchar *expire = sipmsg_find_header(msg, "Expire");
|
|
1220 gchar *tmp;
|
|
1221 struct simple_watcher *watcher = watcher_find(sip, from);
|
|
1222 if(!ourtag) {
|
|
1223 tagadded = TRUE;
|
|
1224 ourtag = gentag();
|
|
1225 }
|
|
1226 if(!watcher) { /* new subscription */
|
|
1227 gchar *acceptheader = sipmsg_find_header(msg, "Accept");
|
|
1228 gboolean needsxpidf = FALSE;
|
|
1229 if(!gaim_privacy_check(sip->account, from)) {
|
|
1230 send_sip_response(sip->gc, msg, 202, "Ok", NULL);
|
|
1231 goto privend;
|
|
1232 }
|
|
1233 if(acceptheader) {
|
|
1234 gchar *tmp = acceptheader;
|
|
1235 gboolean foundpidf = FALSE;
|
|
1236 gboolean foundxpidf = FALSE;
|
|
1237 while(tmp && tmp < acceptheader + strlen(acceptheader)) {
|
|
1238 gchar *tmp2 = strchr(tmp, ',');
|
|
1239 if(tmp2) *tmp2 = '\0';
|
|
1240 if(!strcmp("application/pidf+xml", tmp))
|
|
1241 foundpidf = TRUE;
|
|
1242 if(!strcmp("application/xpidf+xml", tmp))
|
|
1243 foundxpidf = TRUE;
|
|
1244 if(tmp2) {
|
|
1245 *tmp2 = ',';
|
|
1246 tmp = tmp2;
|
|
1247 while(*tmp == ' ') tmp++;
|
|
1248 } else
|
|
1249 tmp = 0;
|
|
1250 }
|
|
1251 if(!foundpidf && foundxpidf) needsxpidf = TRUE;
|
|
1252 g_free(acceptheader);
|
|
1253 }
|
|
1254 watcher = watcher_create(sip, from, callid, ourtag, theirtag, needsxpidf);
|
|
1255 }
|
|
1256 if(tagadded) {
|
|
1257 gchar *to = g_strdup_printf("%s;tag=%s", sipmsg_find_header(msg, "To"), ourtag);
|
|
1258 sipmsg_remove_header(msg, "To");
|
|
1259 sipmsg_add_header(msg, "To", to);
|
|
1260 g_free(to);
|
|
1261 }
|
|
1262 if(expire)
|
|
1263 watcher->expire = time(NULL) + strtol(expire, NULL, 10);
|
|
1264 else
|
|
1265 watcher->expire = time(NULL) + 600;
|
|
1266 sipmsg_remove_header(msg, "Contact");
|
|
1267 tmp = get_contact(sip);
|
|
1268 sipmsg_add_header(msg, "Contact", tmp);
|
|
1269 g_free(tmp);
|
|
1270 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);
|
|
1271 send_sip_response(sip->gc, msg, 200, "Ok", NULL);
|
|
1272 send_notify(sip, watcher);
|
|
1273 privend:
|
|
1274 g_free(from);
|
|
1275 g_free(theirtag);
|
|
1276 g_free(ourtag);
|
|
1277 g_free(callid);
|
|
1278 g_free(expire);
|
|
1279 }
|
|
1280
|
|
1281 static void process_input_message(struct simple_account_data *sip, struct sipmsg *msg) {
|
|
1282 gboolean found = FALSE;
|
|
1283 if(msg->response == 0) { /* request */
|
|
1284 if(!strcmp(msg->method, "MESSAGE")) {
|
|
1285 process_incoming_message(sip, msg);
|
|
1286 found = TRUE;
|
|
1287 } else if(!strcmp(msg->method, "NOTIFY")) {
|
|
1288 process_incoming_notify(sip, msg);
|
|
1289 found = TRUE;
|
|
1290 } else if(!strcmp(msg->method, "SUBSCRIBE")) {
|
|
1291 process_incoming_subscribe(sip, msg);
|
|
1292 found = TRUE;
|
|
1293 } else {
|
|
1294 send_sip_response(sip->gc, msg, 501, "Not implemented", NULL);
|
|
1295 }
|
|
1296 } else { /* response */
|
|
1297 struct transaction *trans = transactions_find(sip, msg);
|
|
1298 if(trans) {
|
|
1299 if(msg->response == 407) {
|
|
1300 gchar *resend, *auth, *ptmp;
|
|
1301
|
|
1302 if(sip->proxy.retries > 3) return;
|
|
1303 sip->proxy.retries++;
|
|
1304 /* do proxy authentication */
|
|
1305
|
|
1306 ptmp = sipmsg_find_header(msg, "Proxy-Authenticate");
|
|
1307
|
|
1308 fill_auth(sip, ptmp, &sip->proxy);
|
|
1309 auth = auth_header(sip, &sip->proxy, trans->msg->method, trans->msg->target);
|
|
1310 sipmsg_remove_header(trans->msg, "Proxy-Authorization");
|
|
1311 sipmsg_add_header(trans->msg, "Proxy-Authorization", auth);
|
|
1312 g_free(auth);
|
|
1313 resend = sipmsg_to_string(trans->msg);
|
|
1314 /* resend request */
|
|
1315 sendout_pkt(sip->gc, resend);
|
|
1316 g_free(resend);
|
|
1317 } else {
|
|
1318 if(msg->response == 100) {
|
|
1319 /* ignore provisional response */
|
|
1320 gaim_debug_info("simple", "got trying response\n");
|
|
1321 } else {
|
|
1322 sip->proxy.retries = 0;
|
|
1323 if(!strcmp(trans->msg->method, "REGISTER")) {
|
|
1324 if(msg->response == 401) sip->registrar.retries++;
|
|
1325 else sip->registrar.retries = 0;
|
|
1326 } else {
|
|
1327 if(msg->response == 401) {
|
|
1328 gchar *resend, *auth, *ptmp;
|
|
1329
|
|
1330 if(sip->registrar.retries > 4) return;
|
|
1331 sip->registrar.retries++;
|
|
1332
|
|
1333 ptmp = sipmsg_find_header(msg, "WWW-Authenticate");
|
|
1334
|
|
1335 fill_auth(sip, ptmp, &sip->registrar);
|
|
1336 auth = auth_header(sip, &sip->registrar, trans->msg->method, trans->msg->target);
|
|
1337 sipmsg_remove_header(trans->msg, "Authorization");
|
|
1338 sipmsg_add_header(trans->msg, "Authorization", auth);
|
|
1339 g_free(auth);
|
|
1340 resend = sipmsg_to_string(trans->msg);
|
|
1341 /* resend request */
|
|
1342 sendout_pkt(sip->gc, resend);
|
|
1343 g_free(resend);
|
|
1344 }
|
|
1345 }
|
|
1346 if(trans->callback) {
|
|
1347 /* call the callback to process response*/
|
|
1348 (trans->callback)(sip, msg, trans);
|
|
1349 }
|
|
1350 transactions_remove(sip, trans);
|
|
1351 }
|
|
1352 }
|
|
1353 found = TRUE;
|
|
1354 } else {
|
|
1355 gaim_debug(GAIM_DEBUG_MISC, "simple", "received response to unknown transaction");
|
|
1356 }
|
|
1357 }
|
|
1358 if(!found) {
|
|
1359 gaim_debug(GAIM_DEBUG_MISC, "simple", "received a unknown sip message with method %s and response %d\n", msg->method, msg->response);
|
|
1360 }
|
|
1361 }
|
|
1362
|
|
1363 static void process_input(struct simple_account_data *sip, struct sip_connection *conn)
|
|
1364 {
|
|
1365 char *cur;
|
|
1366 char *dummy;
|
|
1367 struct sipmsg *msg;
|
|
1368 int restlen;
|
|
1369 cur = conn->inbuf;
|
|
1370
|
|
1371 /* according to the RFC remove CRLF at the beginning */
|
|
1372 while(*cur == '\r' || *cur == '\n') {
|
|
1373 cur++;
|
|
1374 }
|
|
1375 if(cur != conn->inbuf) {
|
|
1376 memmove(conn->inbuf, cur, conn->inbufused - (cur - conn->inbuf));
|
|
1377 conn->inbufused = strlen(conn->inbuf);
|
|
1378 }
|
|
1379
|
|
1380 /* Received a full Header? */
|
|
1381 if((cur = strstr(conn->inbuf, "\r\n\r\n")) != NULL) {
|
|
1382 time_t currtime = time(NULL);
|
|
1383 cur += 2;
|
|
1384 cur[0] = '\0';
|
|
1385 gaim_debug_info("simple", "\n\nreceived - %s\n######\n%s\n#######\n\n", ctime(&currtime), conn->inbuf);
|
|
1386 msg = sipmsg_parse_header(conn->inbuf);
|
|
1387 cur[0] = '\r';
|
|
1388 cur += 2;
|
|
1389 restlen = conn->inbufused - (cur - conn->inbuf);
|
|
1390 if(restlen >= msg->bodylen) {
|
|
1391 dummy = g_malloc(msg->bodylen + 1);
|
|
1392 memcpy(dummy, cur, msg->bodylen);
|
|
1393 dummy[msg->bodylen] = '\0';
|
|
1394 msg->body = dummy;
|
|
1395 cur += msg->bodylen;
|
|
1396 memmove(conn->inbuf, cur, conn->inbuflen - (cur - conn->inbuf));
|
|
1397 conn->inbufused = strlen(conn->inbuf);
|
|
1398 } else {
|
|
1399 sipmsg_free(msg);
|
|
1400 return;
|
|
1401 }
|
|
1402 gaim_debug(GAIM_DEBUG_MISC, "simple", "in process response response: %d\n", msg->response);
|
|
1403 process_input_message(sip, msg);
|
|
1404 } else {
|
|
1405 gaim_debug(GAIM_DEBUG_MISC, "simple", "received a incomplete sip msg: %s\n", conn->inbuf);
|
|
1406 }
|
|
1407 }
|
|
1408
|
|
1409 static void simple_udp_process(gpointer data, gint source, GaimInputCondition con) {
|
|
1410 GaimConnection *gc = data;
|
|
1411 struct simple_account_data *sip = gc->proto_data;
|
|
1412 struct sipmsg *msg;
|
|
1413 int len;
|
|
1414 time_t currtime;
|
|
1415
|
|
1416 static char buffer[65536];
|
|
1417 if((len = recv(source, buffer, sizeof(buffer) - 1, 0)) > 0) {
|
|
1418 buffer[len] = '\0';
|
|
1419 gaim_debug_info("simple", "\n\nreceived - %s\n######\n%s\n#######\n\n", ctime(&currtime), buffer);
|
|
1420 msg = sipmsg_parse_msg(buffer);
|
|
1421 if(msg) process_input_message(sip, msg);
|
|
1422 }
|
|
1423 }
|
|
1424
|
|
1425 static void simple_input_cb(gpointer data, gint source, GaimInputCondition cond)
|
|
1426 {
|
|
1427 GaimConnection *gc = data;
|
|
1428 struct simple_account_data *sip = gc->proto_data;
|
|
1429 int len;
|
|
1430 struct sip_connection *conn = connection_find(sip, source);
|
|
1431 if(!conn) {
|
|
1432 gaim_debug_error("simple", "Connection not found!\n");
|
|
1433 return;
|
|
1434 }
|
|
1435
|
|
1436 if(conn->inbuflen < conn->inbufused + SIMPLE_BUF_INC) {
|
|
1437 conn->inbuflen += SIMPLE_BUF_INC;
|
|
1438 conn->inbuf = g_realloc(conn->inbuf, conn->inbuflen);
|
|
1439 }
|
|
1440
|
|
1441 len = read(source, conn->inbuf + conn->inbufused, SIMPLE_BUF_INC - 1);
|
|
1442
|
|
1443 if(len < 0 && errno == EAGAIN)
|
|
1444 return;
|
|
1445 else if(len <= 0) {
|
|
1446 gaim_debug_info("simple", "simple_input_cb: read error\n");
|
|
1447 connection_remove(sip, source);
|
|
1448 if(sip->fd == source) sip->fd = -1;
|
|
1449 return;
|
|
1450 }
|
|
1451
|
|
1452 conn->inbufused += len;
|
|
1453 conn->inbuf[conn->inbufused] = '\0';
|
|
1454
|
|
1455 process_input(sip, conn);
|
|
1456 }
|
|
1457
|
|
1458 /* Callback for new connections on incoming TCP port */
|
|
1459 static void simple_newconn_cb(gpointer data, gint source, GaimInputCondition cond) {
|
|
1460 GaimConnection *gc = data;
|
|
1461 struct simple_account_data *sip = gc->proto_data;
|
|
1462 struct sip_connection *conn;
|
|
1463
|
|
1464 int newfd = accept(source, NULL, NULL);
|
|
1465
|
|
1466 conn = connection_create(sip, newfd);
|
|
1467
|
|
1468 conn->inputhandler = gaim_input_add(newfd, GAIM_INPUT_READ, simple_input_cb, gc);
|
|
1469 }
|
|
1470
|
|
1471 static void login_cb(gpointer data, gint source, const gchar *error_message) {
|
|
1472 GaimConnection *gc = data;
|
|
1473 struct simple_account_data *sip;
|
|
1474 struct sip_connection *conn;
|
|
1475
|
|
1476 if (!GAIM_CONNECTION_IS_VALID(gc))
|
|
1477 {
|
|
1478 if (source >= 0)
|
|
1479 close(source);
|
|
1480 return;
|
|
1481 }
|
|
1482
|
|
1483 if(source < 0) {
|
|
1484 gaim_connection_error(gc, _("Could not connect"));
|
|
1485 return;
|
|
1486 }
|
|
1487
|
|
1488 sip = gc->proto_data;
|
|
1489 sip->fd = source;
|
|
1490
|
|
1491 conn = connection_create(sip, source);
|
|
1492
|
|
1493 sip->registertimeout = gaim_timeout_add((rand()%100)+10*1000, (GSourceFunc)subscribe_timeout, sip);
|
|
1494
|
|
1495 do_register(sip);
|
|
1496
|
|
1497 conn->inputhandler = gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_input_cb, gc);
|
|
1498 }
|
|
1499
|
|
1500 static guint simple_ht_hash_nick(const char *nick) {
|
|
1501 char *lc = g_utf8_strdown(nick, -1);
|
|
1502 guint bucket = g_str_hash(lc);
|
|
1503 g_free(lc);
|
|
1504
|
|
1505 return bucket;
|
|
1506 }
|
|
1507
|
|
1508 static gboolean simple_ht_equals_nick(const char *nick1, const char *nick2) {
|
|
1509 return (gaim_utf8_strcasecmp(nick1, nick2) == 0);
|
|
1510 }
|
|
1511
|
|
1512 static void simple_udp_host_resolved_listen_cb(int listenfd, gpointer data) {
|
|
1513 struct simple_account_data *sip = (struct simple_account_data*) data;
|
|
1514
|
14267
|
1515 sip->listen_data = NULL;
|
|
1516
|
14192
|
1517 if(listenfd == -1) {
|
|
1518 gaim_connection_error(sip->gc, _("Could not create listen socket"));
|
|
1519 return;
|
|
1520 }
|
|
1521
|
|
1522 sip->fd = listenfd;
|
|
1523
|
|
1524 sip->listenport = gaim_network_get_port_from_fd(sip->fd);
|
|
1525 sip->listenfd = sip->fd;
|
|
1526
|
|
1527 sip->listenpa = gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_udp_process, sip->gc);
|
|
1528
|
|
1529 sip->resendtimeout = gaim_timeout_add(2500, (GSourceFunc) resend_timeout, sip);
|
|
1530 sip->registertimeout = gaim_timeout_add((rand()%100)+10*1000, (GSourceFunc)subscribe_timeout, sip);
|
|
1531 do_register(sip);
|
|
1532 }
|
|
1533
|
|
1534 static void simple_udp_host_resolved(GSList *hosts, gpointer data, const char *error_message) {
|
|
1535 struct simple_account_data *sip = (struct simple_account_data*) data;
|
|
1536 int addr_size;
|
|
1537
|
14238
|
1538 sip->query_data = NULL;
|
|
1539
|
14192
|
1540 if (!hosts || !hosts->data) {
|
|
1541 gaim_connection_error(sip->gc, _("Couldn't resolve host"));
|
|
1542 return;
|
|
1543 }
|
|
1544
|
|
1545 addr_size = GPOINTER_TO_INT(hosts->data);
|
|
1546 hosts = g_slist_remove(hosts, hosts->data);
|
|
1547 memcpy(&(sip->serveraddr), hosts->data, addr_size);
|
|
1548 g_free(hosts->data);
|
|
1549 hosts = g_slist_remove(hosts, hosts->data);
|
|
1550 while(hosts) {
|
|
1551 hosts = g_slist_remove(hosts, hosts->data);
|
|
1552 g_free(hosts->data);
|
|
1553 hosts = g_slist_remove(hosts, hosts->data);
|
|
1554 }
|
|
1555
|
|
1556 /* create socket for incoming connections */
|
14267
|
1557 sip->listen_data = gaim_network_listen_range(5060, 5160, SOCK_DGRAM,
|
|
1558 simple_udp_host_resolved_listen_cb, sip);
|
|
1559 if (sip->listen_data == NULL) {
|
14192
|
1560 gaim_connection_error(sip->gc, _("Could not create listen socket"));
|
|
1561 return;
|
|
1562 }
|
|
1563 }
|
|
1564
|
|
1565 static void
|
|
1566 simple_tcp_connect_listen_cb(int listenfd, gpointer data) {
|
|
1567 struct simple_account_data *sip = (struct simple_account_data*) data;
|
14262
|
1568 GaimProxyConnectData *connect_data;
|
14192
|
1569
|
14267
|
1570 sip->listen_data = NULL;
|
|
1571
|
14192
|
1572 sip->listenfd = listenfd;
|
|
1573 if(sip->listenfd == -1) {
|
|
1574 gaim_connection_error(sip->gc, _("Could not create listen socket"));
|
|
1575 return;
|
|
1576 }
|
|
1577
|
|
1578 gaim_debug_info("simple", "listenfd: %d\n", sip->listenfd);
|
|
1579 sip->listenport = gaim_network_get_port_from_fd(sip->listenfd);
|
|
1580 sip->listenpa = gaim_input_add(sip->listenfd, GAIM_INPUT_READ,
|
|
1581 simple_newconn_cb, sip->gc);
|
|
1582 gaim_debug_info("simple", "connecting to %s port %d\n",
|
|
1583 sip->realhostname, sip->realport);
|
|
1584 /* open tcp connection to the server */
|
14262
|
1585 connect_data = gaim_proxy_connect(sip->account, sip->realhostname,
|
14192
|
1586 sip->realport, login_cb, sip->gc);
|
14262
|
1587 if(connect_data == NULL) {
|
14192
|
1588 gaim_connection_error(sip->gc, _("Couldn't create socket"));
|
|
1589 }
|
|
1590 }
|
|
1591
|
|
1592 static void srvresolved(GaimSrvResponse *resp, int results, gpointer data) {
|
|
1593 struct simple_account_data *sip;
|
|
1594 gchar *hostname;
|
|
1595 int port;
|
|
1596
|
14308
|
1597 sip = data;
|
|
1598 sip->srv_query_data = NULL;
|
14192
|
1599
|
|
1600 port = gaim_account_get_int(sip->account, "port", 0);
|
|
1601
|
|
1602 /* find the host to connect to */
|
|
1603 if(results) {
|
|
1604 hostname = g_strdup(resp->hostname);
|
|
1605 if(!port)
|
|
1606 port = resp->port;
|
|
1607 g_free(resp);
|
|
1608 } else {
|
|
1609 if(!gaim_account_get_bool(sip->account, "useproxy", FALSE)) {
|
|
1610 hostname = g_strdup(sip->servername);
|
|
1611 } else {
|
|
1612 hostname = g_strdup(gaim_account_get_string(sip->account, "proxy", sip->servername));
|
|
1613 }
|
|
1614 }
|
|
1615
|
|
1616 sip->realhostname = hostname;
|
|
1617 sip->realport = port;
|
|
1618 if(!sip->realport) sip->realport = 5060;
|
|
1619
|
|
1620 /* TCP case */
|
|
1621 if(!sip->udp) {
|
|
1622 /* create socket for incoming connections */
|
14267
|
1623 sip->listen_data = gaim_network_listen_range(5060, 5160, SOCK_STREAM,
|
|
1624 simple_tcp_connect_listen_cb, sip);
|
|
1625 if (sip->listen_data == NULL) {
|
14192
|
1626 gaim_connection_error(sip->gc, _("Could not create listen socket"));
|
|
1627 return;
|
|
1628 }
|
|
1629 } else { /* UDP */
|
|
1630 gaim_debug_info("simple", "using udp with server %s and port %d\n", hostname, port);
|
|
1631
|
14238
|
1632 sip->query_data = gaim_dnsquery_a(hostname, port, simple_udp_host_resolved, sip);
|
|
1633 if (sip->query_data == NULL) {
|
|
1634 gaim_connection_error(sip->gc, _("Could not resolve hostname"));
|
|
1635 }
|
14192
|
1636 }
|
|
1637 }
|
|
1638
|
|
1639 static void simple_login(GaimAccount *account)
|
|
1640 {
|
|
1641 GaimConnection *gc;
|
|
1642 struct simple_account_data *sip;
|
|
1643 gchar **userserver;
|
|
1644 gchar *hosttoconnect;
|
|
1645
|
|
1646 const char *username = gaim_account_get_username(account);
|
|
1647 gc = gaim_account_get_connection(account);
|
|
1648
|
|
1649 if (strpbrk(username, " \t\v\r\n") != NULL) {
|
|
1650 gc->wants_to_die = TRUE;
|
|
1651 gaim_connection_error(gc, _("SIP usernames may not contain whitespaces or @ symbols"));
|
|
1652 return;
|
|
1653 }
|
|
1654
|
|
1655 gc->proto_data = sip = g_new0(struct simple_account_data, 1);
|
|
1656 sip->gc = gc;
|
|
1657 sip->account = account;
|
|
1658 sip->registerexpire = 900;
|
|
1659 sip->udp = gaim_account_get_bool(account, "udp", FALSE);
|
|
1660 /* TODO: is there a good default grow size? */
|
|
1661 if(!sip->udp)
|
|
1662 sip->txbuf = gaim_circ_buffer_new(0);
|
|
1663
|
|
1664 userserver = g_strsplit(username, "@", 2);
|
|
1665 gaim_connection_set_display_name(gc, userserver[0]);
|
|
1666 sip->username = g_strdup(userserver[0]);
|
|
1667 sip->servername = g_strdup(userserver[1]);
|
|
1668 sip->password = g_strdup(gaim_connection_get_password(gc));
|
|
1669 g_strfreev(userserver);
|
|
1670
|
|
1671 sip->buddies = g_hash_table_new((GHashFunc)simple_ht_hash_nick, (GEqualFunc)simple_ht_equals_nick);
|
|
1672
|
|
1673 gaim_connection_update_progress(gc, _("Connecting"), 1, 2);
|
|
1674
|
|
1675 /* TODO: Set the status correctly. */
|
|
1676 sip->status = g_strdup("available");
|
|
1677
|
|
1678 if(!gaim_account_get_bool(account, "useproxy", FALSE)) {
|
|
1679 hosttoconnect = g_strdup(sip->servername);
|
|
1680 } else {
|
|
1681 hosttoconnect = g_strdup(gaim_account_get_string(account, "proxy", sip->servername));
|
|
1682 }
|
|
1683
|
14308
|
1684 sip->srv_query_data = gaim_srv_resolve("sip",
|
|
1685 sip->udp ? "udp" : "tcp", hosttoconnect, srvresolved, sip);
|
14192
|
1686 g_free(hosttoconnect);
|
|
1687 }
|
|
1688
|
|
1689 static void simple_close(GaimConnection *gc)
|
|
1690 {
|
|
1691 struct simple_account_data *sip = gc->proto_data;
|
|
1692
|
|
1693 if(sip) {
|
|
1694 /* unregister */
|
|
1695 do_register_exp(sip, 0);
|
|
1696 connection_free_all(sip);
|
|
1697
|
14238
|
1698 if (sip->query_data != NULL)
|
|
1699 gaim_dnsquery_destroy(sip->query_data);
|
|
1700
|
14308
|
1701 if (sip->srv_query_data != NULL)
|
|
1702 gaim_srv_cancel(sip->srv_query_data);
|
|
1703
|
14267
|
1704 if (sip->listen_data != NULL)
|
|
1705 gaim_network_listen_cancel(sip->listen_data);
|
|
1706
|
14192
|
1707 g_free(sip->servername);
|
|
1708 g_free(sip->username);
|
|
1709 g_free(sip->password);
|
|
1710 g_free(sip->registrar.nonce);
|
|
1711 g_free(sip->registrar.opaque);
|
|
1712 g_free(sip->registrar.target);
|
|
1713 g_free(sip->registrar.realm);
|
|
1714 g_free(sip->registrar.digest_session_key);
|
|
1715 g_free(sip->proxy.nonce);
|
|
1716 g_free(sip->proxy.opaque);
|
|
1717 g_free(sip->proxy.target);
|
|
1718 g_free(sip->proxy.realm);
|
|
1719 g_free(sip->proxy.digest_session_key);
|
|
1720 if(sip->txbuf)
|
|
1721 gaim_circ_buffer_destroy(sip->txbuf);
|
|
1722 g_free(sip->realhostname);
|
|
1723 if(sip->listenpa) gaim_input_remove(sip->listenpa);
|
|
1724 if(sip->tx_handler) gaim_input_remove(sip->tx_handler);
|
|
1725 if(sip->resendtimeout) gaim_timeout_remove(sip->resendtimeout);
|
|
1726 if(sip->registertimeout) gaim_timeout_remove(sip->registertimeout);
|
|
1727 }
|
|
1728 g_free(gc->proto_data);
|
|
1729 gc->proto_data = NULL;
|
|
1730 }
|
|
1731
|
|
1732 /* not needed since privacy is checked for every subscribe */
|
|
1733 static void dummy_add_deny(GaimConnection *gc, const char *name) {
|
|
1734 }
|
|
1735
|
|
1736 static void dummy_permit_deny(GaimConnection *gc) {
|
|
1737 }
|
|
1738
|
|
1739 static GaimPluginProtocolInfo prpl_info =
|
|
1740 {
|
|
1741 0,
|
|
1742 NULL, /* user_splits */
|
|
1743 NULL, /* protocol_options */
|
|
1744 NO_BUDDY_ICONS, /* icon_spec */
|
|
1745 simple_list_icon, /* list_icon */
|
|
1746 NULL, /* list_emblems */
|
|
1747 NULL, /* status_text */
|
|
1748 NULL, /* tooltip_text */
|
|
1749 simple_status_types, /* away_states */
|
|
1750 NULL, /* blist_node_menu */
|
|
1751 NULL, /* chat_info */
|
|
1752 NULL, /* chat_info_defaults */
|
|
1753 simple_login, /* login */
|
|
1754 simple_close, /* close */
|
|
1755 simple_im_send, /* send_im */
|
|
1756 NULL, /* set_info */
|
|
1757 simple_typing, /* send_typing */
|
|
1758 NULL, /* get_info */
|
|
1759 simple_set_status, /* set_status */
|
|
1760 NULL, /* set_idle */
|
|
1761 NULL, /* change_passwd */
|
|
1762 simple_add_buddy, /* add_buddy */
|
|
1763 NULL, /* add_buddies */
|
|
1764 simple_remove_buddy, /* remove_buddy */
|
|
1765 NULL, /* remove_buddies */
|
|
1766 dummy_add_deny, /* add_permit */
|
|
1767 dummy_add_deny, /* add_deny */
|
|
1768 dummy_add_deny, /* rem_permit */
|
|
1769 dummy_add_deny, /* rem_deny */
|
|
1770 dummy_permit_deny, /* set_permit_deny */
|
|
1771 NULL, /* join_chat */
|
|
1772 NULL, /* reject_chat */
|
|
1773 NULL, /* get_chat_name */
|
|
1774 NULL, /* chat_invite */
|
|
1775 NULL, /* chat_leave */
|
|
1776 NULL, /* chat_whisper */
|
|
1777 NULL, /* chat_send */
|
|
1778 simple_keep_alive, /* keepalive */
|
|
1779 NULL, /* register_user */
|
|
1780 NULL, /* get_cb_info */
|
|
1781 NULL, /* get_cb_away */
|
|
1782 NULL, /* alias_buddy */
|
|
1783 NULL, /* group_buddy */
|
|
1784 NULL, /* rename_group */
|
|
1785 NULL, /* buddy_free */
|
|
1786 NULL, /* convo_closed */
|
|
1787 NULL, /* normalize */
|
|
1788 NULL, /* set_buddy_icon */
|
|
1789 NULL, /* remove_group */
|
|
1790 NULL, /* get_cb_real_name */
|
|
1791 NULL, /* set_chat_topic */
|
|
1792 NULL, /* find_blist_chat */
|
|
1793 NULL, /* roomlist_get_list */
|
|
1794 NULL, /* roomlist_cancel */
|
|
1795 NULL, /* roomlist_expand_category */
|
|
1796 NULL, /* can_receive_file */
|
|
1797 NULL, /* send_file */
|
|
1798 NULL, /* new_xfer */
|
|
1799 NULL, /* offline_message */
|
|
1800 NULL, /* whiteboard_prpl_ops */
|
14542
|
1801 simple_send_raw, /* send_raw */
|
14192
|
1802 };
|
|
1803
|
|
1804
|
|
1805 static GaimPluginInfo info =
|
|
1806 {
|
|
1807 GAIM_PLUGIN_MAGIC,
|
|
1808 GAIM_MAJOR_VERSION,
|
|
1809 GAIM_MINOR_VERSION,
|
|
1810 GAIM_PLUGIN_PROTOCOL, /**< type */
|
|
1811 NULL, /**< ui_requirement */
|
|
1812 0, /**< flags */
|
|
1813 NULL, /**< dependencies */
|
|
1814 GAIM_PRIORITY_DEFAULT, /**< priority */
|
|
1815
|
|
1816 "prpl-simple", /**< id */
|
|
1817 "SIMPLE", /**< name */
|
|
1818 VERSION, /**< version */
|
|
1819 N_("SIP/SIMPLE Protocol Plugin"), /** summary */
|
|
1820 N_("The SIP/SIMPLE Protocol Plugin"), /** description */
|
|
1821 "Thomas Butter <butter@uni-mannheim.de>", /**< author */
|
|
1822 GAIM_WEBSITE, /**< homepage */
|
|
1823
|
|
1824 NULL, /**< load */
|
|
1825 NULL, /**< unload */
|
|
1826 NULL, /**< destroy */
|
|
1827
|
|
1828 NULL, /**< ui_info */
|
|
1829 &prpl_info, /**< extra_info */
|
|
1830 NULL,
|
|
1831 NULL
|
|
1832 };
|
|
1833
|
|
1834 static void _init_plugin(GaimPlugin *plugin)
|
|
1835 {
|
|
1836 GaimAccountUserSplit *split;
|
|
1837 GaimAccountOption *option;
|
|
1838
|
|
1839 split = gaim_account_user_split_new(_("Server"), "", '@');
|
|
1840 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split);
|
|
1841
|
|
1842 option = gaim_account_option_bool_new(_("Publish status (note: everyone may watch you)"), "dopublish", TRUE);
|
|
1843 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1844
|
|
1845 option = gaim_account_option_int_new(_("Connect port"), "port", 0);
|
|
1846 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1847
|
|
1848 option = gaim_account_option_bool_new(_("Use UDP"), "udp", FALSE);
|
|
1849 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1850 option = gaim_account_option_bool_new(_("Use proxy"), "useproxy", FALSE);
|
|
1851 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1852 option = gaim_account_option_string_new(_("Proxy"), "proxy", "");
|
|
1853 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1854 option = gaim_account_option_string_new(_("Auth User"), "authuser", "");
|
|
1855 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1856 option = gaim_account_option_string_new(_("Auth Domain"), "authdomain", "");
|
|
1857 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
|
|
1858 }
|
|
1859
|
|
1860 GAIM_INIT_PLUGIN(simple, _init_plugin, info);
|