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