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