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