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