comparison src/protocols/simple/simple.c @ 11181:e5bbe5070e04

[gaim-migrate @ 13292] first import sip/simple prpl files. still buggy and deactivated by default. committer: Tailor Script <tailor@pidgin.im>
author Thomas Butter <tbutter>
date Tue, 02 Aug 2005 20:24:51 +0000
parents
children 5f79dfde334c
comparison
equal deleted inserted replaced
11180:5d103f550f6a 11181:e5bbe5070e04
1 /**
2 * @file simple.c
3 *
4 * gaim
5 *
6 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include "internal.h"
24
25 #include "accountopt.h"
26 #include "blist.h"
27 #include "conversation.h"
28 #include "debug.h"
29 #include "notify.h"
30 #include "prpl.h"
31 #include "plugin.h"
32 #include "util.h"
33 #include "version.h"
34 #include "network.h"
35 #include "xmlnode.h"
36
37 #include "simple.h"
38 #include "sipmsg.h"
39 #include "srvresolve.h"
40
41 static char *gentag() {
42 return g_strdup_printf("%04d%04d", rand() & 0xFFFF, rand() & 0xFFFF);
43 }
44
45 static char *genbranch() {
46 return g_strdup_printf("z9hG4bK%04X%04X%04X%04X%04X",
47 rand() & 0xFFFF,
48 rand() & 0xFFFF,
49 rand() & 0xFFFF,
50 rand() & 0xFFFF,
51 rand() & 0xFFFF);
52 }
53
54 static char *gencallid() {
55 return g_strdup_printf("%04Xg%04Xa%04Xi%04Xm%04Xt%04Xb%04Xx%04Xx",
56 rand() & 0xFFFF,
57 rand() & 0xFFFF,
58 rand() & 0xFFFF,
59 rand() & 0xFFFF,
60 rand() & 0xFFFF,
61 rand() & 0xFFFF,
62 rand() & 0xFFFF,
63 rand() & 0xFFFF);
64 }
65
66 static const char *simple_list_icon(GaimAccount *a, GaimBuddy *b) {
67 return "simple";
68 }
69
70 static void simple_keep_alive(GaimConnection *gc) {
71 return; // need it?
72 }
73
74 static gboolean process_register_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc);
75 static void send_notify(struct simple_account_data *sip, struct simple_watcher *);
76
77 static void send_publish(struct simple_account_data *sip);
78
79 static void do_notifies(struct simple_account_data *sip) {
80 GSList *tmp = sip->watcher;
81 gaim_debug_info("simple", "do_notifies()\n");
82 if((sip->republish != -1) || sip->republish < time(NULL))
83 send_publish(sip);
84
85 while(tmp) {
86 gaim_debug_info("simple", "notifying %s\n", ((struct simple_watcher*)tmp->data)->name);
87 send_notify(sip, tmp->data);
88 tmp = tmp->next;
89 }
90 }
91
92 static void simple_set_status(GaimAccount *account, GaimStatus *status) {
93 GaimStatusPrimitive primitive = gaim_status_type_get_primitive(gaim_status_get_type(status));
94 struct simple_account_data *sip = NULL;
95 if (!gaim_status_is_active(status))
96 return;
97
98 if(account->gc) sip = account->gc->proto_data;
99 if(sip) {
100 if(sip->status) g_free(sip->status);
101 if(primitive == GAIM_STATUS_AVAILABLE) sip->status = g_strdup("available");
102 else sip->status = g_strdup("busy");
103
104 do_notifies(sip);
105 }
106 if ((primitive != GAIM_STATUS_OFFLINE)
107 && (!gaim_account_is_connected(account))) {
108 gaim_account_connect(account);
109 }
110 }
111
112 static struct sip_connection *connection_find(struct simple_account_data *sip, int fd) {
113 struct sip_connection *ret = NULL;
114 GSList *entry = sip->openconns;
115 while(entry) {
116 ret = entry->data;
117 if(ret->fd == fd) return ret;
118 entry = entry->next;
119 }
120 return NULL;
121 }
122
123 static struct simple_watcher *watcher_find(struct simple_account_data *sip, gchar *name) {
124 struct simple_watcher *watcher;
125 GSList *entry = sip->watcher;
126 while(entry) {
127 watcher = entry->data;
128 if(!strcmp(name, watcher->name)) return watcher;
129 entry = entry->next;
130 }
131 return NULL;
132 }
133
134 static struct simple_watcher *watcher_create(struct simple_account_data *sip, gchar *name, gchar *callid, gchar *ourtag, gchar *theirtag) {
135 struct simple_watcher *watcher = g_new0(struct simple_watcher,1);
136 watcher->name = g_strdup(name);
137 watcher->dialog.callid = g_strdup(callid);
138 watcher->dialog.ourtag = g_strdup(ourtag);
139 watcher->dialog.theirtag = g_strdup(theirtag);
140 sip->watcher = g_slist_append(sip->watcher, watcher);
141 return watcher;
142 }
143
144 static void watcher_remove(struct simple_account_data *sip, gchar *name) {
145 struct simple_watcher *watcher = watcher_find(sip, name);
146 sip->watcher = g_slist_remove(sip->watcher, watcher);
147 g_free(watcher->name);
148 g_free(watcher->dialog.callid);
149 g_free(watcher->dialog.ourtag);
150 g_free(watcher->dialog.theirtag);
151 g_free(watcher);
152 }
153
154 static struct sip_connection *connection_create(struct simple_account_data *sip, int fd) {
155 struct sip_connection *ret = g_new0(struct sip_connection,1);
156 ret->fd = fd;
157 sip->openconns = g_slist_append(sip->openconns, ret);
158 return ret;
159 }
160
161 static void connection_remove(struct simple_account_data *sip, int fd) {
162 struct sip_connection *conn = connection_find(sip, fd);
163 sip->openconns = g_slist_remove(sip->openconns, conn);
164 if(conn->inputhandler) gaim_input_remove(conn->inputhandler);
165 if(conn->inbuf) g_free(conn->inbuf);
166 g_free(conn);
167 }
168
169 static void simple_add_buddy(GaimConnection *gc, GaimBuddy *buddy, GaimGroup *group)
170 {
171 struct simple_account_data *sip = (struct simple_account_data *)gc->proto_data;
172 struct simple_buddy *b;
173 if(strncmp("sip:", buddy->name,4)) {
174 gchar *buf = g_strdup_printf(_("Could not add the buddy %s because every simple user has to start with 'sip:'."), buddy->name);
175 gaim_notify_error(gc, NULL, _("Unable To Add"), buf);
176 g_free(buf);
177 gaim_blist_remove_buddy(buddy);
178 return;
179 }
180 if(!g_hash_table_lookup(sip->buddies, buddy->name)) {
181 b = g_new0(struct simple_buddy, 1);
182 gaim_debug_info("simple","simple_add_buddy %s\n",buddy->name);
183 b->name = g_strdup(buddy->name);
184 g_hash_table_insert(sip->buddies, b->name, b);
185 } else {
186 gaim_debug_info("simple","buddy %s already in internal list\n", buddy->name);
187 }
188 }
189
190 static void simple_get_buddies(GaimConnection *gc) {
191 GaimBlistNode *gnode, *cnode, *bnode;
192
193 gaim_debug_info("simple","simple_get_buddies\n");
194
195 for(gnode = gaim_get_blist()->root; gnode; gnode = gnode->next) {
196 if(!GAIM_BLIST_NODE_IS_GROUP(gnode)) continue;
197 for(cnode = gnode->child; cnode; cnode = cnode->next) {
198 if(!GAIM_BLIST_NODE_IS_CONTACT(cnode)) continue;
199 for(bnode = cnode->child; bnode; bnode = bnode->next) {
200 if(!GAIM_BLIST_NODE_IS_BUDDY(bnode)) continue;
201 simple_add_buddy(gc, (GaimBuddy*)bnode, (GaimGroup *)gnode);
202 }
203 }
204 }
205 }
206
207 static void simple_remove_buddy(GaimConnection *gc, GaimBuddy *buddy, GaimGroup *group)
208 {
209 struct simple_account_data *sip = (struct simple_account_data *)gc->proto_data;
210 struct simple_buddy *b = g_hash_table_lookup(sip->buddies, buddy->name);
211 g_hash_table_remove(sip->buddies, buddy->name);
212 g_free(b->name);
213 g_free(b);
214 }
215
216 static GList *simple_status_types(GaimAccount *acc) {
217 GaimStatusType *type;
218 GList *types = NULL;
219 gaim_debug_info("simple","called simple_status_types\n");
220 type = gaim_status_type_new(GAIM_STATUS_OFFLINE, "offline", _("Offline"), FALSE);
221 types = g_list_append(types, type);
222
223 type = gaim_status_type_new(GAIM_STATUS_ONLINE, "online", _("Online"), FALSE);
224 types = g_list_append(types, type);
225
226 type = gaim_status_type_new_with_attrs(
227 GAIM_STATUS_AVAILABLE, "available", _("Available"),
228 TRUE, TRUE, FALSE,
229 "message", _("Message"), gaim_value_new(GAIM_TYPE_STRING), NULL);
230 types = g_list_append(types, type);
231
232 return types;
233 }
234
235 static void simple_input_cb(gpointer data, gint source, GaimInputCondition cond);
236
237 static void send_later_cb(gpointer data, gint source, GaimInputCondition cond) {
238 GaimConnection *gc = data;
239 struct simple_account_data *sip = gc->proto_data;
240 struct sip_connection *conn;
241
242 if( source < 0 ) {
243 gaim_connection_error(gc,"Could not connect");
244 return;
245 }
246
247 sip->fd = source;
248 sip->connecting = 0;
249 write(sip->fd, sip->sendlater, strlen(sip->sendlater));
250 conn = connection_create(sip, source);
251 conn->inputhandler = gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_input_cb, gc);
252 g_free(sip->sendlater);
253 sip->sendlater = 0;
254 }
255
256
257 static void sendlater(GaimConnection *gc, const char *buf) {
258 struct getserver_return *serveradr;
259 struct simple_account_data *sip = gc->proto_data;
260 int error = 0;
261 if(!sip->connecting) {
262 serveradr = getserver(sip->servername);
263 gaim_debug_info("simple","connecting to %s port %d", serveradr->name, serveradr->port);
264 error = gaim_proxy_connect(sip->account, serveradr->name, serveradr->port, send_later_cb, gc);
265 if(error) {
266 gaim_connection_error(gc, _("Couldn't create socket"));
267 }
268 sip->connecting = 1;
269 }
270 if(sip->sendlater) {
271 gchar *old = sip->sendlater;
272 sip->sendlater = g_strdup_printf("%s\r\n%s",old, buf);
273 } else {
274 sip->sendlater = g_strdup(buf);
275 }
276 }
277
278 static int sendout_pkt(GaimConnection *gc, const char *buf) {
279 struct simple_account_data *sip = gc->proto_data;
280 time_t currtime = time(NULL);
281 int ret;
282
283 gaim_debug(GAIM_DEBUG_MISC, "simple", "\n\nsending - %s\n######\n%s\n######\n\n", ctime(&currtime), buf);
284 if(sip->fd <0 ) {
285 sendlater(gc, buf);
286 return 0;
287 }
288 ret = write(sip->fd, buf, strlen(buf));
289 if(ret < 0) {
290 sendlater(gc,buf);
291 return 0;
292 }
293 return ret;
294 }
295
296 static void send_sip_response(GaimConnection *gc, struct sipmsg *msg, int code, char *text, char *body) {
297 GSList *tmp = msg->headers;
298 char *oldstr;
299 char *name;
300 char *value;
301 char *outstr = g_strdup_printf("SIP/2.0 %d %s\r\n",code, text);
302 while(tmp) {
303 oldstr = outstr;
304 name = ((struct siphdrelement*)(tmp->data))->name;
305 value = ((struct siphdrelement*)(tmp->data))->value;
306 outstr = g_strdup_printf("%s%s: %s\r\n",oldstr, name, value);
307 g_free(oldstr);
308 tmp = g_slist_next(tmp);
309 }
310 oldstr = outstr;
311 if(body) outstr = g_strdup_printf("%s\r\n%s",outstr,body);
312 else outstr = g_strdup_printf("%s\r\n",outstr);
313 g_free(oldstr);
314 sendout_pkt(gc, outstr);
315 g_free(outstr);
316 }
317
318 static void transactions_add_buf(struct simple_account_data *sip, gchar *buf, void *callback) {
319 struct transaction *trans = g_new0(struct transaction, 1);
320 trans->time = time(NULL);
321 trans->msg = sipmsg_parse_msg(buf);
322 trans->cseq = sipmsg_find_header(trans->msg, "CSeq");
323 trans->callback = callback;
324 sip->transactions = g_slist_append(sip->transactions, trans);
325 }
326
327 static struct transaction *transactions_find(struct simple_account_data *sip, struct sipmsg *msg) {
328 struct transaction *trans;
329 GSList *transactions = sip->transactions;
330 gchar *cseq = sipmsg_find_header(msg, "CSeq");
331
332 while(transactions) {
333 trans = transactions->data;
334 if(!strcmp(trans->cseq, cseq)) {
335 return trans;
336 }
337 transactions = transactions->next;
338 }
339
340 return (struct transaction *)NULL;
341 }
342
343 static void send_sip_request(GaimConnection *gc, gchar *method, gchar *url, gchar *to, gchar *addheaders, gchar *body, struct sip_dialog *dialog, TransCallback tc) {
344 struct simple_account_data *sip = gc->proto_data;
345 char *callid= dialog ? g_strdup(dialog->callid) : gencallid();
346 char *auth="";
347 char *addh="";
348 gchar *branch = genbranch();
349 char *buf;
350 HASHHEX response;
351 HASHHEX HA2;
352
353 if(addheaders) addh=addheaders;
354 if(sip->registrar.nonce && !strcmp(method,"REGISTER") && sip->registrar.fouroseven<4) {
355 gchar noncecount[90];
356 sprintf(noncecount,"%08d",sip->registrar.nc++);
357 DigestCalcResponse(sip->registrar.HA1, sip->registrar.nonce, noncecount, "", "", method, url, HA2, response);
358 gaim_debug(GAIM_DEBUG_MISC, "simple", "response %s", response);
359 auth = g_strdup_printf("Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=\"%s\", response=\"%s\"\r\n",sip->username, sip->registrar.realm, sip->registrar.nonce, url, noncecount, response);
360 gaim_debug(GAIM_DEBUG_MISC, "simple", "header %s", auth);
361 }
362
363 if(sip->proxy.nonce && strcmp(method,"REGISTER")) {
364 gchar noncecount[90];
365 sprintf(noncecount, "%08d", sip->proxy.nc++);
366 DigestCalcResponse(sip->proxy.HA1, sip->proxy.nonce, noncecount, "", "", method, url, HA2, response);
367 gaim_debug(GAIM_DEBUG_MISC, "simple", "response %s", response);
368 auth = g_strdup_printf("Proxy-Authorization: Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=\"%s\", response=\"%s\"\r\n",sip->username, sip->proxy.realm, sip->proxy.nonce, url, noncecount, response);
369 gaim_debug(GAIM_DEBUG_MISC, "simple", "header %s", auth);
370 }
371
372
373 buf = g_strdup_printf("%s %s SIP/2.0\r\n"
374 "Via: SIP/2.0/TCP %s:%d;branch=%s\r\n"
375 "From: <sip:%s@%s>;tag=%s\r\n"
376 "To: <%s>%s%s\r\n"
377 "Max-Forwards: 10\r\n"
378 "CSeq: %d %s\r\n"
379 "User-Agent: Gaim SIP/SIMPLE Plugin\r\n"
380 "Call-ID: %s\r\n"
381 "%s%s"
382 "Content-Length: %d\r\n\r\n%s",
383 method,
384 url,
385 sip->ip,
386 sip->listenport,
387 branch,
388 sip->username,
389 sip->servername,
390 dialog ? dialog->ourtag : gentag(),
391 to,
392 dialog ? ";tag=" : "",
393 dialog ? dialog->theirtag : "",
394 ++sip->cseq,
395 method,
396 callid,
397 auth,
398 addh,
399 strlen(body),
400 body);
401 g_free(branch);
402 g_free(callid);
403
404 // add to running transactions
405
406 transactions_add_buf(sip, buf, tc);
407
408 sendout_pkt(gc,buf);
409
410 g_free(buf);
411 }
412
413 static void do_register(GaimConnection *gc) {
414 struct simple_account_data *sip = gc->proto_data;
415 sip->registerstatus = 1;
416
417 char *uri = g_strdup_printf("sip:%s",sip->servername);
418 char *to = g_strdup_printf("sip:%s@%s",sip->username,sip->servername);
419 char *contact = g_strdup_printf("Contact: <sip:%s@%s:%d;transport=tcp>;methods=\"MESSAGE, SUBSCRIBE, NOTIFY\"\r\nExpires: 900\r\n", sip->username, sip->ip, sip->listenport);
420
421 // allow one auth try per register
422 sip->proxy.fouroseven = 0;
423 sip->registrar.fouroseven = 0;
424
425 sip->reregister = time(NULL) + 540;
426 send_sip_request(gc,"REGISTER",uri,to, contact, "", NULL, process_register_response);
427 g_free(uri);
428 g_free(to);
429 }
430
431 static gchar *parse_from(gchar *hdr) {
432 gchar *from = hdr;
433 gchar *tmp;
434
435 if(!from) return NULL;
436 gaim_debug_info("simple", "parsing address out of %s\n",from);
437 tmp = strchr(from, '<');
438
439 // i hate the different SIP UA behaviours...
440 if(tmp) { // sip address in <...>
441 from = tmp+1;
442 tmp = strchr(from,'>');
443 if(tmp) {
444 from = g_strndup(from,tmp-from);
445 } else {
446 gaim_debug_info("simple", "found < without > in From\n");
447 return NULL;
448 }
449 } else {
450 tmp = strchr(from, ';');
451 if(tmp) {
452 from = g_strndup(from,tmp-from);
453 }
454 }
455 gaim_debug_info("simple", "got %s\n",from);
456 return from;
457 }
458
459 static gboolean process_subscribe_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) {
460 gchar *to = parse_from(sipmsg_find_header(tc->msg,"To")); // cant be NULL since it is our own msg
461
462
463 if(msg->response==200 || msg->response==202) {
464 return TRUE;
465 }
466
467 // we can not subscribe -> user is offline (TODO unknown status?)
468
469 gaim_prpl_got_user_status(sip->account, to, "offline", NULL);
470 g_free(to);
471 return TRUE;
472 }
473
474 static void simple_subscribe(struct simple_account_data *sip, struct simple_buddy *buddy) {
475 gchar *contact = "Expires: 900\r\nAccept: application/pidf+xml\r\nEvent: presence\r\n";
476 gchar *to;
477 if(strstr(buddy->name,"sip:")) to = g_strdup(buddy->name);
478 else to = g_strdup_printf("sip:%s",buddy->name);
479 // subscribe to buddy presence
480 // we dont need to know the status so we do not need a callback
481
482 send_sip_request(sip->gc, "SUBSCRIBE",to, to, contact, "", NULL, process_subscribe_response);
483
484 g_free(to);
485
486 // resubscribe before of subscription expires
487 // add some jitter
488 buddy->resubscribe = time(NULL)+550+(rand()%50);
489 }
490
491 static void simple_buddy_resub(char *name, struct simple_buddy *buddy, struct simple_account_data *sip) {
492 time_t curtime = time(NULL);
493
494 if(buddy->resubscribe < curtime) {
495 gaim_debug(GAIM_DEBUG_MISC, "simple", "simple_buddy_resub %s\n",name);
496 simple_subscribe(sip, buddy);
497 }
498 }
499
500 static gboolean register_timeout(struct simple_account_data *sip) {
501 GSList *tmp;
502 time_t curtime = time(NULL);
503 // register again if first registration expires
504 if(sip->reregister < curtime) {
505 do_register(sip->gc);
506 }
507
508 // check for every subscription if we need to resubscribe
509 g_hash_table_foreach(sip->buddies, (GHFunc)simple_buddy_resub, (gpointer)sip);
510
511 // remove a timed out suscriber
512
513 tmp = sip->watcher;
514 while(tmp) {
515 struct simple_watcher *watcher = tmp->data;
516 if(watcher->expire < curtime) {
517 watcher_remove(sip, watcher->name);
518 tmp = sip->watcher;
519 }
520 if(tmp) tmp = tmp->next;
521 }
522
523 return TRUE;
524 }
525
526 static void simple_send_message(struct simple_account_data *sip, char *to, char *msg, char *type) {
527 gchar *hdr;
528 if(type) {
529 hdr = g_strdup_printf("Content-Type: %s\r\n",type);
530 } else {
531 hdr = g_strdup("Content-Type: text/plain\r\n");
532 }
533 send_sip_request(sip->gc, "MESSAGE", to, to, hdr, msg, NULL, NULL);
534 g_free(hdr);
535 }
536
537 static int simple_im_send(GaimConnection *gc, const char *who, const char *what, GaimConvImFlags flags) {
538 struct simple_account_data *sip = gc->proto_data;
539 char *to = g_strdup(who);
540 char *text = g_strdup(what);
541 simple_send_message(sip, to, text, NULL);
542 g_free(to);
543 g_free(text);
544 return 1;
545 }
546
547 static void process_incoming_message(struct simple_account_data *sip, struct sipmsg *msg) {
548 gchar *from;
549 gchar *contenttype;
550 gboolean found = FALSE;
551
552 from = parse_from(sipmsg_find_header(msg, "From"));
553
554 if(!from) return;
555
556 gaim_debug(GAIM_DEBUG_MISC, "simple", "got message from %s: %s\n", from, msg->body);
557
558 contenttype = sipmsg_find_header(msg, "Content-Type");
559 if(!contenttype || !strncmp(contenttype, "text/plain", 10) || !strncmp(contenttype, "text/html", 9)) {
560 serv_got_im(sip->gc, from, msg->body, 0, time(NULL));
561 send_sip_response(sip->gc, msg, 200, "OK", NULL);
562 found = TRUE;
563 }
564 if(!strncmp(contenttype, "application/im-iscomposing+xml",30)) {
565 xmlnode *isc = xmlnode_from_str(msg->body, msg->bodylen);
566 xmlnode *state;
567 gchar *statedata;
568
569 if(!isc) {
570 gaim_debug_info("simple","process_incoming_message: can not parse iscomposing\n");
571 return;
572 }
573
574 state = xmlnode_get_child(isc, "state");
575
576 if(!state) {
577 gaim_debug_info("simple","process_incoming_message: no state found\n");
578 return;
579 }
580
581 statedata = xmlnode_get_data(state);
582 if(statedata) {
583 if(strstr(statedata,"active")) serv_got_typing(sip->gc, from, 0, GAIM_TYPING);
584 else serv_got_typing_stopped(sip->gc, from);
585 }
586 xmlnode_free(isc);
587 send_sip_response(sip->gc, msg, 200, "OK", NULL);
588 found = TRUE;
589 }
590 if(!found) {
591 gaim_debug_info("simple", "got unknown mime-type");
592 send_sip_response(sip->gc, msg, 415, "Unsupported media type", NULL);
593 }
594 g_free(from);
595 }
596
597 static void fill_auth(struct simple_account_data *sip, gchar *hdr, struct sip_auth *auth) {
598 if(!hdr) {
599 gaim_debug_info("simple", "fill_auth: hdr==NULL\n");
600 return;
601 }
602 int i=0;
603 gchar **parts = g_strsplit(hdr, " ", 0);
604 while(parts[i]) {
605 if(!strncmp(parts[i],"nonce",5)) {
606 auth->nonce = g_strndup(parts[i]+7,strlen(parts[i]+7)-1);
607 }
608 if(!strncmp(parts[i],"realm",5)) {
609 auth->realm = g_strndup(parts[i]+7,strlen(parts[i]+7)-2);
610 }
611 i++;
612 }
613
614 gaim_debug(GAIM_DEBUG_MISC, "simple", "nonce: %s realm: %s ", auth->nonce, auth->realm);
615
616 DigestCalcHA1("md5", sip->username, auth->realm, sip->password, auth->nonce, "", auth->HA1);
617
618 auth->nc=1;
619 }
620
621
622 gboolean process_register_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) {
623 gchar *tmp;
624 gaim_debug(GAIM_DEBUG_MISC, "simple", "in process register response response: %d\n", msg->response);
625 switch (msg->response) {
626 case 200:
627 if(sip->registerstatus<3) { // registered
628 send_publish(sip);
629 }
630 sip->registerstatus=3;
631 gaim_connection_set_state(sip->gc, GAIM_CONNECTED);
632 register_timeout(sip);
633 break;
634 case 401:
635 if(sip->registerstatus!=2) {
636 tmp = sipmsg_find_header(msg, "WWW-Authenticate");
637 fill_auth(sip, tmp, &sip->registrar);
638 sip->registerstatus=2;
639 gaim_debug(GAIM_DEBUG_MISC, "simple", "HA1: %s\n",sip->registrar.HA1);
640 do_register(sip->gc);
641 }
642 break;
643 }
644 return TRUE;
645 }
646
647 static void process_incoming_notify(struct simple_account_data *sip, struct sipmsg *msg) {
648 gchar *from;
649 gchar *fromhdr;
650 gchar *tmp2;
651 xmlnode *pidf;
652 xmlnode *basicstatus;
653 gboolean isonline = FALSE;
654
655 fromhdr = sipmsg_find_header(msg,"From");
656 from = parse_from(fromhdr);
657 if(!from) return;
658
659 pidf = xmlnode_from_str(msg->body, msg->bodylen);
660
661 if(!pidf) {
662 gaim_debug_info("simple","process_incoming_notify: no parseable pidf\n");
663 return;
664 }
665
666
667 basicstatus = xmlnode_get_child(xmlnode_get_child(xmlnode_get_child(pidf,"tuple"),"status"), "basic");
668
669 if(!basicstatus) {
670 gaim_debug_info("simple","process_incoming_notify: no basic found\n");
671 return;
672 }
673
674 tmp2 = xmlnode_get_data(basicstatus);
675
676 if(!tmp2) {
677 gaim_debug_info("simple","process_incoming_notify: no basic data found\n");
678 return;
679 }
680
681 if(strstr(tmp2, "open")) {
682 isonline = TRUE;
683 }
684
685 if(isonline) gaim_prpl_got_user_status(sip->account, from, "available", NULL);
686 else gaim_prpl_got_user_status(sip->account, from, "offline", NULL);
687
688 xmlnode_free(pidf);
689
690 g_free(from);
691 send_sip_response(sip->gc, msg, 200, "OK", NULL);
692 }
693
694 static int simple_typing(GaimConnection *gc, const char *name, int typing) {
695 struct simple_account_data *sip = gc->proto_data;
696
697 gchar *xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
698 "<isComposing xmlns=\"urn:ietf:params:xml:ns:im-iscomposing\"\n"
699 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
700 "xsi:schemaLocation=\"urn:ietf:params:xml:ns:im-composing iscomposing.xsd\">\n"
701 "<state>%s</state>\n"
702 "<contenttype>text/plain</contenttype>\n"
703 "<refresh>60</refresh>\n"
704 "</isComposing>";
705 gchar *recv = g_strdup(name);
706 if(typing == GAIM_TYPING) {
707 gchar *msg = g_strdup_printf(xml, "active");
708 simple_send_message(sip, recv, msg, "application/im-iscomposing+xml");
709 g_free(msg);
710 } else {
711 gchar *msg = g_strdup_printf(xml, "idle");
712 simple_send_message(sip, recv, msg, "application/im-iscomposing+xml");
713 g_free(msg);
714 }
715 g_free(recv);
716 return 1;
717 }
718
719 static gchar *find_tag(gchar *hdr) {
720 gchar *tmp = strstr(hdr, ";tag=");
721 gchar *tmp2;
722 if(!tmp) return NULL;
723 tmp += 5;
724 if((tmp2 = strchr(tmp, ';'))) {
725 tmp2[0] = '\0';
726 tmp = g_strdup(tmp);
727 tmp2[0] = ';';
728 return tmp;
729 }
730 return g_strdup(tmp);
731 }
732
733 static gchar* gen_pidf(struct simple_account_data *sip) {
734 gchar *doc = g_strdup_printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
735 "<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n"
736 "xmlns:im=\"urn:ietf:params:xml:ns:pidf:im\"\n"
737 "entity=\"sip:%s@%s\">\n"
738 "<tuple id=\"bs35r9f\">\n"
739 "<status>\n"
740 "<basic>open</basic>\n"
741 "<im:im>%s</im:im>\n"
742 "</status>\n"
743 "</tuple>\n"
744 "</presence>",
745 sip->username,
746 sip->servername,
747 sip->status);
748 return doc;
749 }
750
751 static void send_notify(struct simple_account_data *sip, struct simple_watcher *watcher) {
752 gchar *doc = gen_pidf(sip);
753 send_sip_request(sip->gc, "NOTIFY", watcher->name, watcher->name, "Event: presence\r\nContent-Type: application/pidf+xml\r\n", doc, &watcher->dialog, NULL);
754 g_free(doc);
755 }
756
757 static gboolean process_publish_response(struct simple_account_data *sip, struct sipmsg *msg, struct transaction *tc) {
758 if(msg->response != 200) {
759 // never send again
760 sip->republish = -1;
761 }
762 return TRUE;
763 }
764
765 static void send_publish(struct simple_account_data *sip) {
766 gchar *uri = g_strdup_printf("sip:%s@%s", sip->username, sip->servername);
767 gchar *doc = gen_pidf(sip);
768 send_sip_request(sip->gc, "PUBLISH", uri, uri, "Expires: 600\r\nEvent: presence\r\nContent-Type: application/pidf+xml\r\nAccept: application/pidf+xml\r\n", doc, NULL, process_publish_response);
769 sip->republish = time(NULL) + 500;
770 g_free(doc);
771 }
772
773 static void process_incoming_subscribe(struct simple_account_data *sip, struct sipmsg *msg) {
774 gchar *from = parse_from(sipmsg_find_header(msg, "From"));
775 gchar *theirtag = find_tag(sipmsg_find_header(msg, "From"));
776 gchar *ourtag = find_tag(sipmsg_find_header(msg, "To"));
777 gboolean tagadded = FALSE;
778 gchar *callid = sipmsg_find_header(msg, "Call-ID");
779 gchar *expire = sipmsg_find_header(msg, "Expire");
780 gchar *tmp;
781 struct simple_watcher *watcher = watcher_find(sip, from);
782 if(!ourtag) {
783 tagadded = TRUE;
784 ourtag = gentag();
785 }
786 if(!watcher) { // new subscription
787 watcher = watcher_create(sip, from, callid, ourtag, theirtag);
788 }
789 if(tagadded) {
790 gchar *to = g_strdup_printf("%s;tag=%s", sipmsg_find_header(msg, "To"), ourtag);
791 sipmsg_remove_header(msg, "To");
792 sipmsg_add_header(msg, "To", to);
793 }
794 if(expire)
795 watcher->expire = time(NULL) + strtol(expire, NULL, 10);
796 else
797 watcher->expire = time(NULL) + 600;
798 sipmsg_remove_header(msg, "Contact");
799 tmp = g_strdup_printf("<%s@%s>",sip->username, sip->servername);
800 sipmsg_add_header(msg, "Contact", tmp);
801 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);
802 send_sip_response(sip->gc, msg, 200, "Ok", NULL);
803 g_free(tmp);
804 send_notify(sip, watcher);
805 }
806
807 static void process_input(struct simple_account_data *sip, struct sip_connection *conn)
808 {
809 char *cur;
810 char *dummy;
811 struct sipmsg *msg;
812 int restlen;
813 int found=0;
814
815 cur = conn->inbuf;
816
817 // according to the RFC remove CRLF at the beginning
818 while(*cur == '\r' || *cur == '\n') {
819 cur++;
820 }
821 if(cur != conn->inbuf) {
822 memmove(conn->inbuf, cur, conn->inbufused-(cur-conn->inbuf));
823 conn->inbufused=strlen(conn->inbuf);
824 }
825
826 // Received a full Header?
827 if((cur = strstr(conn->inbuf, "\r\n\r\n"))!=NULL) {
828 time_t currtime = time(NULL);
829 cur += 2;
830 cur[0] = '\0';
831 gaim_debug_info("simple","\n\nreceived - %s\n######\n%s\n#######\n\n",ctime(&currtime), conn->inbuf);
832 msg = sipmsg_parse_header(conn->inbuf);
833 cur[0] = '\r';
834 cur += 2;
835 restlen = conn->inbufused - (cur-conn->inbuf);
836 if(restlen>=msg->bodylen) {
837 dummy = g_malloc(msg->bodylen+1);
838 memcpy(dummy, cur, msg->bodylen);
839 dummy[msg->bodylen]='\0';
840 msg->body = dummy;
841 cur+=msg->bodylen;
842 memmove(conn->inbuf, cur, conn->inbuflen);
843 conn->inbufused=strlen(conn->inbuf);
844 } else {
845 sipmsg_free(msg);
846 return;
847 }
848 // sipmsg_print(msg);
849 gaim_debug(GAIM_DEBUG_MISC, "simple", "in process response response: %d\n", msg->response);
850 if( msg->response == 0 ) { // request
851 if(!strcmp(msg->method, "MESSAGE")) {
852 process_incoming_message(sip, msg);
853 found = 1;
854 }
855 if(!strcmp(msg->method, "NOTIFY")) {
856 process_incoming_notify(sip, msg);
857 found = 1;
858 }
859 if(!strcmp(msg->method, "SUBSCRIBE")) {
860 process_incoming_subscribe(sip, msg);
861 found = 1;
862 }
863 } else { // response
864 struct transaction *trans = transactions_find(sip, msg);
865 if(trans) {
866 if(msg->response == 407) {
867 if(sip->proxy.fouroseven>3) return;
868 sip->proxy.fouroseven++;
869 // do proxy authentication
870
871 gchar *ptmp = sipmsg_find_header(msg,"Proxy-Authenticate");
872 gchar *resend;
873 gchar *auth;
874
875 HASHHEX HA2;
876 HASHHEX response;
877 gchar noncecount[90];
878 fill_auth(sip, ptmp, &sip->proxy);
879 sprintf(noncecount, "%08d", sip->proxy.nc++);
880
881 DigestCalcResponse(sip->proxy.HA1, sip->proxy.nonce, noncecount, "", "", trans->msg->method, trans->msg->target, HA2, response);
882 gaim_debug(GAIM_DEBUG_MISC, "simple", "response %s\n", response);
883 auth = g_strdup_printf("Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", nc=\"%s\", response=\"%s\"\r\n",sip->username, sip->proxy.realm, sip->proxy.nonce, trans->msg->target, noncecount, response);
884 sipmsg_remove_header(msg, "Proxy-Authorization");
885 sipmsg_add_header(trans->msg, "Proxy-Authorization", auth);
886 g_free(auth);
887 resend = sipmsg_to_string(trans->msg);
888 // resend request
889 sendout_pkt(sip->gc, resend);
890 g_free(resend);
891 } else {
892 sip->proxy.fouroseven = 0;
893 if(msg->response == 401) sip->registrar.fouroseven++;
894 else sip->registrar.fouroseven = 0;
895 if(trans->callback) {
896 // call the callback to process response
897 (trans->callback)(sip, msg, trans);
898
899 sip->transactions = g_slist_remove(sip->transactions, trans);
900 } else {
901 // transaction has no callback - just remove it
902 sip->transactions = g_slist_remove(sip->transactions, trans);
903 }
904 }
905 found = 1;
906 } else {
907 gaim_debug(GAIM_DEBUG_MISC, "simple", "received response to unknown transaction");
908 }
909 }
910 if(!found) {
911 gaim_debug(GAIM_DEBUG_MISC, "simple", "received a unknown sip message with method %sand response %d\n",msg->method, msg->response);
912 }
913 } else {
914 gaim_debug(GAIM_DEBUG_MISC, "simple", "received a incomplete sip msg: %s\n", conn->inbuf);
915 }
916 }
917
918 static void simple_input_cb(gpointer data, gint source, GaimInputCondition cond)
919 {
920 GaimConnection *gc = data;
921 struct simple_account_data *sip = gc->proto_data;
922 int len;
923 struct sip_connection *conn = connection_find(sip, source);
924 if(!conn) {
925 gaim_debug_error("simple", "Connection not found!\n");
926 return;
927 }
928
929 if (conn->inbuflen < conn->inbufused + SIMPLE_BUF_INC) {
930 conn->inbuflen += SIMPLE_BUF_INC;
931 conn->inbuf = g_realloc(conn->inbuf, conn->inbuflen);
932 }
933
934 if ((len = read(source, conn->inbuf + conn->inbufused, SIMPLE_BUF_INC - 1)) <= 0) {
935 gaim_debug_info("simple","simple_input_cb: read error\n");
936 connection_remove(sip, source);
937 if(sip->fd == source) sip->fd = -1;
938 // gaim_connection_error(gc, _("Read error"));
939 return;
940 }
941 if(len == 0) {
942 // connection was closed
943 connection_remove(sip, source);
944 if(sip->fd == source) sip->fd = -1;
945 }
946
947 conn->inbufused += len;
948 conn->inbuf[conn->inbufused]='\0';
949
950 process_input(sip, conn);
951 }
952
953 /* Callback for new connections on incoming TCP port */
954 static void simple_newconn_cb(gpointer data, gint source, GaimInputCondition cond) {
955 GaimConnection *gc = data;
956 struct simple_account_data *sip = gc->proto_data;
957 struct sip_connection *conn;
958
959
960 int newfd = accept(source, NULL, NULL);
961
962 conn = connection_create(sip, newfd);
963
964 conn->inputhandler = gaim_input_add(newfd, GAIM_INPUT_READ, simple_input_cb, gc);
965 }
966
967 static void login_cb(gpointer data, gint source, GaimInputCondition cond) {
968 GaimConnection *gc = data;
969 struct simple_account_data *sip = gc->proto_data;
970 struct sip_connection *conn;
971
972 if( source < 0 ) {
973 gaim_connection_error(gc,"Could not connect");
974 return;
975 }
976
977 sip->fd = source;
978
979 // get buddies from blist
980 simple_get_buddies(gc);
981
982 conn = connection_create(sip, source);
983
984 // get the local ip
985 sip->ip = g_strdup(gaim_network_get_my_ip(source));
986
987 do_register(gc);
988
989 conn->inputhandler = gaim_input_add(sip->fd, GAIM_INPUT_READ, simple_input_cb, gc);
990 }
991
992 static guint simple_ht_hash_nick(const char *nick) {
993 char *lc = g_utf8_strdown(nick, -1);
994 guint bucket = g_str_hash(lc);
995 g_free(lc);
996
997 return bucket;
998 }
999
1000 static gboolean simple_ht_equals_nick(const char *nick1, const char *nick2) {
1001 return (gaim_utf8_strcasecmp(nick1, nick2) == 0);
1002 }
1003
1004 static void simple_login(GaimAccount *account, GaimStatus *status)
1005 {
1006 GaimConnection *gc;
1007 struct simple_account_data *sip;
1008 gchar **userserver;
1009 int error=0;
1010 struct getserver_return *serveradr;
1011
1012 const char *username = gaim_account_get_username(account);
1013
1014 gc = gaim_account_get_connection(account);
1015
1016 gc->proto_data = sip = g_new0(struct simple_account_data,1);
1017 sip->gc=gc;
1018 sip->account = account;
1019 if (strpbrk(username, " \t\v\r\n") != NULL) {
1020 gaim_connection_error(gc, _("SIP usernames may not contain whitespaces or @ symbols"));
1021 return;
1022 }
1023
1024 userserver = g_strsplit(username, "@", 2);
1025
1026 gaim_connection_set_display_name(gc,userserver[0]);
1027 sip->username = g_strdup(userserver[0]);
1028 sip->servername = g_strdup(userserver[1]);
1029 sip->password = g_strdup(gaim_connection_get_password(gc));
1030 g_strfreev(userserver);
1031
1032 sip->buddies = g_hash_table_new((GHashFunc)simple_ht_hash_nick, (GEqualFunc)simple_ht_equals_nick);
1033
1034 gaim_connection_update_progress(gc, _("Connecting"), 1, 2);
1035
1036 sip->status = g_strdup("available");
1037
1038 // search for SRV record
1039 serveradr = getserver(sip->servername);
1040 gaim_debug_info("simple","connecting to %s port %d", serveradr->name, serveradr->port);
1041
1042 // open tcp connection to the server
1043 error = gaim_proxy_connect(account, serveradr->name, serveradr->port, login_cb, gc);
1044 if(error) {
1045 gaim_connection_error(gc, _("Couldn't create socket"));
1046 }
1047
1048 // create socket for incoming connections
1049 sip->listenfd = gaim_network_listen_range(5060, 5080);
1050 if(sip->listenfd == -1) {
1051 gaim_connection_error(gc, _("Could not create listen socket"));
1052 return;
1053 }
1054 sip->listenport = gaim_network_get_port_from_fd(sip->listenfd);
1055 gaim_input_add(sip->listenfd, GAIM_INPUT_READ, simple_newconn_cb, gc);
1056
1057 // register timeout callback for register / subscribe renewal
1058 sip->registertimeout = gaim_timeout_add((rand()%10)+10*1000, (GSourceFunc)register_timeout, sip);
1059 }
1060
1061 static void simple_close(GaimConnection *gc)
1062 {
1063 struct simple_account_data *sip = gc->proto_data;
1064 // if(sip) {
1065 if(0) {
1066 if(sip->servername) g_free(sip->servername);
1067 if(sip->username) g_free(sip->username);
1068 if(sip->password) g_free(sip->password);
1069 if(sip->registrar.nonce) g_free(sip->registrar.nonce);
1070 if(sip->registrar.realm) g_free(sip->registrar.nonce);
1071 if(sip->proxy.nonce) g_free(sip->proxy.nonce);
1072 if(sip->proxy.realm) g_free(sip->proxy.realm);
1073 // if(sip->registertimeout) gaim_timeout_remove(sip->registertimeout);
1074 if(sip->sendlater) g_free(sip->sendlater);
1075 if(sip->ip) g_free(sip->ip);
1076 sip->servername = sip->username = sip->password = sip->registrar.nonce = sip->registrar.realm = sip->proxy.nonce = sip->proxy.realm = sip->sendlater = sip->ip = 0;
1077 }
1078 // if(gc->proto_data) g_free(gc->proto_data);
1079 // gc->proto_data = 0;
1080 // TODO free connections
1081 }
1082
1083 static GaimPluginProtocolInfo prpl_info =
1084 {
1085 0,
1086 NULL, /* user_splits */
1087 NULL, /* protocol_options */
1088 NO_BUDDY_ICONS, /* icon_spec */
1089 simple_list_icon, /* list_icon */
1090 NULL, /* list_emblems */
1091 NULL, /* status_text */
1092 NULL, /* tooltip_text */
1093 simple_status_types, /* away_states */
1094 NULL, /* blist_node_menu */
1095 NULL, /* chat_info */
1096 NULL, /* chat_info_defaults */
1097 simple_login, /* login */
1098 simple_close, /* close */
1099 simple_im_send, /* send_im */
1100 NULL, /* set_info */
1101 simple_typing, /* send_typing */
1102 NULL, /* get_info */
1103 simple_set_status, /* set_status */
1104 NULL, /* set_idle */
1105 NULL, /* change_passwd */
1106 simple_add_buddy, /* add_buddy */
1107 NULL, /* add_buddies */
1108 simple_remove_buddy, /* remove_buddy */
1109 NULL, /* remove_buddies */
1110 NULL, /* add_permit */
1111 NULL, /* add_deny */
1112 NULL, /* rem_permit */
1113 NULL, /* rem_deny */
1114 NULL, /* set_permit_deny */
1115 NULL, /* warn */
1116 NULL, /* join_chat */
1117 NULL, /* reject_chat */
1118 NULL, /* get_chat_name */
1119 NULL, /* chat_invite */
1120 NULL, /* chat_leave */
1121 NULL, /* chat_whisper */
1122 NULL, /* chat_send */
1123 simple_keep_alive, /* keepalive */
1124 NULL, /* register_user */
1125 NULL, /* get_cb_info */
1126 NULL, /* get_cb_away */
1127 NULL, /* alias_buddy */
1128 NULL, /* group_buddy */
1129 NULL, /* rename_group */
1130 NULL, /* buddy_free */
1131 NULL, /* convo_closed */
1132 NULL, /* normalize */
1133 NULL, /* set_buddy_icon */
1134 NULL, /* remove_group */
1135 NULL, /* get_cb_real_name */
1136 NULL, /* set_chat_topic */
1137 NULL, /* find_blist_chat */
1138 NULL, /* roomlist_get_list */
1139 NULL, /* roomlist_cancel */
1140 NULL, /* roomlist_expand_category */
1141 NULL, /* can_receive_file */
1142 NULL /* send_file */
1143 };
1144
1145
1146 static GaimPluginInfo info =
1147 {
1148 GAIM_PLUGIN_MAGIC,
1149 GAIM_MAJOR_VERSION,
1150 GAIM_MINOR_VERSION,
1151 GAIM_PLUGIN_PROTOCOL, /**< type */
1152 NULL, /**< ui_requirement */
1153 0, /**< flags */
1154 NULL, /**< dependencies */
1155 GAIM_PRIORITY_DEFAULT, /**< priority */
1156
1157 "prpl-simple", /**< id */
1158 "SIMPLE", /**< name */
1159 VERSION, /**< version */
1160 N_("SIP/SIMPLE Protocol Plugin"), /** summary */
1161 N_("The SIP/SIMPLE Protocol Plugin"), /** description */
1162 N_("Thomas Butter <butter@uni-mannheim.de>"), /**< author */
1163 GAIM_WEBSITE, /**< homepage */
1164
1165 NULL, /**< load */
1166 NULL, /**< unload */
1167 NULL, /**< destroy */
1168
1169 NULL, /**< ui_info */
1170 &prpl_info, /**< extra_info */
1171 NULL,
1172 NULL
1173 };
1174
1175 static void _init_plugin(GaimPlugin *plugin)
1176 {
1177 GaimAccountUserSplit *split;
1178
1179 gaim_debug_register_category("simple");
1180
1181 split = gaim_account_user_split_new(_("Server"), "blubb.com", '@');
1182 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split);
1183
1184 // _simple_plugin = plugin;
1185 }
1186
1187 GAIM_INIT_PLUGIN(simple, _init_plugin, info);