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