8487
|
1 /*
|
|
2 * gaim - Rendezvous Protocol Plugin
|
|
3 *
|
|
4 * Gaim is the legal property of its developers, whose names are too numerous
|
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
6 * source distribution.
|
|
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 #include "internal.h"
|
|
23
|
|
24 #include "account.h"
|
|
25 #include "accountopt.h"
|
|
26 #include "blist.h"
|
|
27 #include "conversation.h"
|
|
28 #include "debug.h"
|
|
29 #include "prpl.h"
|
|
30
|
|
31 #include "mdns.h"
|
|
32 #include "util.h"
|
|
33
|
|
34 #define RENDEZVOUS_CONNECT_STEPS 2
|
|
35
|
|
36 typedef struct _RendezvousData {
|
|
37 int fd;
|
|
38 GHashTable *buddies;
|
|
39 } RendezvousData;
|
|
40
|
|
41 typedef struct _RendezvousBuddy {
|
|
42 gchar *firstandlast;
|
|
43 gchar *aim;
|
|
44 int p2pjport;
|
|
45 int status;
|
|
46 int idle;
|
|
47 gchar *msg;
|
|
48 } RendezvousBuddy;
|
|
49
|
|
50 #define UC_IDLE 2
|
|
51
|
|
52 /****************************/
|
|
53 /* Utility Functions */
|
|
54 /****************************/
|
|
55 static void rendezvous_buddy_free(gpointer data)
|
|
56 {
|
|
57 RendezvousBuddy *rb = data;
|
|
58
|
|
59 g_free(rb->firstandlast);
|
|
60 g_free(rb->msg);
|
|
61 g_free(rb);
|
|
62 }
|
|
63
|
8546
|
64 /**
|
8487
|
65 * Extract the "user@host" name from a full presence domain
|
|
66 * of the form "user@host._presence._tcp.local"
|
|
67 *
|
|
68 * @return If the domain is NOT a _presence._tcp.local domain
|
|
69 * then return NULL. Otherwise return a newly allocated
|
|
70 * null-terminated string containing the "user@host" for
|
|
71 * the given domain. This string should be g_free'd
|
|
72 * when no longer needed.
|
|
73 */
|
|
74 static gchar *rendezvous_extract_name(gchar *domain)
|
|
75 {
|
|
76 gchar *ret, *suffix;
|
|
77
|
|
78 if (!g_str_has_suffix(domain, "._presence._tcp.local"))
|
|
79 return NULL;
|
|
80
|
|
81 suffix = strstr(domain, "._presence._tcp.local");
|
|
82 ret = g_strndup(domain, suffix - domain);
|
|
83
|
|
84 return ret;
|
|
85 }
|
|
86
|
|
87 /****************************/
|
|
88 /* Buddy List Functions */
|
|
89 /****************************/
|
|
90 static void rendezvous_addtolocal(GaimConnection *gc, const char *name, const char *domain)
|
|
91 {
|
|
92 GaimAccount *account = gaim_connection_get_account(gc);
|
|
93 GaimBuddy *b;
|
|
94 GaimGroup *g;
|
|
95
|
|
96 g = gaim_find_group(domain);
|
|
97 if (g == NULL) {
|
|
98 g = gaim_group_new(domain);
|
|
99 gaim_blist_add_group(g, NULL);
|
|
100 }
|
|
101
|
|
102 b = gaim_find_buddy_in_group(account, name, g);
|
|
103 if (b != NULL)
|
|
104 return;
|
|
105
|
|
106 b = gaim_buddy_new(account, name, NULL);
|
|
107 gaim_blist_add_buddy(b, NULL, g, NULL);
|
|
108 serv_got_update(gc, b->name, 1, 0, 0, 0, 0);
|
|
109 }
|
|
110
|
|
111 static void rendezvous_removefromlocal(GaimConnection *gc, const char *name, const char *domain)
|
|
112 {
|
|
113 GaimAccount *account = gaim_connection_get_account(gc);
|
|
114 GaimBuddy *b;
|
|
115 GaimGroup *g;
|
|
116
|
|
117 g = gaim_find_group(domain);
|
|
118 if (g == NULL)
|
|
119 return;
|
|
120
|
|
121 b = gaim_find_buddy_in_group(account, name, g);
|
|
122 if (b == NULL)
|
|
123 return;
|
|
124
|
|
125 serv_got_update(gc, b->name, 0, 0, 0, 0, 0);
|
|
126 gaim_blist_remove_buddy(b);
|
8546
|
127 /* XXX - This results in incorrect group counts--needs to be fixed in the core */
|
8487
|
128 }
|
|
129
|
|
130 static void rendezvous_removeallfromlocal(GaimConnection *gc)
|
|
131 {
|
|
132 GaimAccount *account = gaim_connection_get_account(gc);
|
|
133 GaimBuddyList *blist;
|
|
134 GaimBlistNode *gnode, *cnode, *bnode;
|
|
135 GaimBuddy *b;
|
|
136
|
|
137 /* Go through and remove all buddies that belong to this account */
|
|
138 if ((blist = gaim_get_blist()) != NULL) {
|
|
139 for (gnode = blist->root; gnode; gnode = gnode->next) {
|
|
140 if (!GAIM_BLIST_NODE_IS_GROUP(gnode))
|
|
141 continue;
|
|
142 for (cnode = gnode->child; cnode; cnode = cnode->next) {
|
|
143 if (!GAIM_BLIST_NODE_IS_CONTACT(cnode))
|
|
144 continue;
|
|
145 for (bnode = cnode->child; bnode; bnode = bnode->next) {
|
|
146 if (!GAIM_BLIST_NODE_IS_BUDDY(bnode))
|
|
147 continue;
|
|
148 b = (GaimBuddy *)bnode;
|
|
149 if (b->account != account)
|
|
150 continue;
|
|
151 serv_got_update(gc, b->name, 0, 0, 0, 0, 0);
|
|
152 gaim_blist_remove_buddy(b);
|
|
153 }
|
|
154 }
|
|
155 }
|
|
156 }
|
|
157 }
|
|
158
|
|
159 static void rendezvous_handle_rr_txt(GaimConnection *gc, ResourceRecord *rr, const gchar *name)
|
|
160 {
|
|
161 RendezvousData *rd = gc->proto_data;
|
|
162 RendezvousBuddy *rb;
|
|
163 GHashTable *rdata;
|
|
164 gchar *tmp1, *tmp2;
|
|
165
|
8594
|
166 rdata = rr->rdata;
|
|
167
|
|
168 /* Don't do a damn thing if the version is greater than 1 */
|
|
169 tmp1 = g_hash_table_lookup(rdata, "version");
|
|
170 if ((tmp1 == NULL) || (strcmp(tmp1, "1")))
|
|
171 return;
|
|
172
|
8487
|
173 rb = g_hash_table_lookup(rd->buddies, name);
|
|
174 if (rb == NULL) {
|
|
175 rb = g_malloc0(sizeof(RendezvousBuddy));
|
|
176 g_hash_table_insert(rd->buddies, g_strdup(name), rb);
|
|
177 }
|
|
178
|
|
179 tmp1 = g_hash_table_lookup(rdata, "1st");
|
|
180 tmp2 = g_hash_table_lookup(rdata, "last");
|
|
181 g_free(rb->firstandlast);
|
|
182 rb->firstandlast = g_strdup_printf("%s%s%s",
|
|
183 (tmp1 ? tmp1 : ""),
|
|
184 (tmp1 || tmp2 ? " " : ""),
|
|
185 (tmp2 ? tmp2 : ""));
|
|
186 serv_got_alias(gc, name, rb->firstandlast);
|
|
187
|
|
188 tmp1 = g_hash_table_lookup(rdata, "aim");
|
|
189 if (tmp1 != NULL) {
|
|
190 g_free(rb->aim);
|
|
191 rb->aim = g_strdup(tmp1);
|
|
192 }
|
|
193
|
8594
|
194 /*
|
|
195 * We only want to use this port as a back-up. Ideally the port
|
|
196 * is specified in a separate, SRV resource record.
|
|
197 */
|
|
198 if (rb->p2pjport == 0) {
|
|
199 tmp1 = g_hash_table_lookup(rdata, "port.p2pj");
|
|
200 rb->p2pjport = atoi(tmp1);
|
|
201 }
|
8487
|
202
|
|
203 tmp1 = g_hash_table_lookup(rdata, "status");
|
|
204 if (tmp1 != NULL) {
|
|
205 if (!strcmp(tmp1, "dnd")) {
|
|
206 /* Available */
|
|
207 rb->status = 0;
|
|
208 } else if (!strcmp(tmp1, "away")) {
|
|
209 /* Idle */
|
|
210 tmp2 = g_hash_table_lookup(rdata, "away");
|
|
211 rb->idle = atoi(tmp2);
|
|
212 gaim_debug_error("XXX", "User has been idle for %d\n", rb->idle);
|
|
213 rb->status = UC_IDLE;
|
|
214 } else if (!strcmp(tmp1, "avail")) {
|
|
215 /* Away */
|
|
216 rb->status = UC_UNAVAILABLE;
|
|
217 }
|
|
218 serv_got_update(gc, name, 1, 0, 0, 0, rb->status);
|
|
219 }
|
|
220
|
|
221 tmp1 = g_hash_table_lookup(rdata, "msg");
|
|
222 if (tmp1 != NULL) {
|
|
223 g_free(rb->msg);
|
|
224 rb->msg = g_strdup(tmp1);
|
|
225 }
|
8594
|
226 }
|
8546
|
227
|
8594
|
228 static void rendezvous_handle_rr_srv(GaimConnection *gc, ResourceRecord *rr, const gchar *name)
|
|
229 {
|
|
230 RendezvousData *rd = gc->proto_data;
|
|
231 RendezvousBuddy *rb;
|
|
232 ResourceRecordSRV *rdata;
|
|
233
|
|
234 rdata = rr->rdata;
|
|
235
|
|
236 rb = g_hash_table_lookup(rd->buddies, name);
|
|
237 if (rb == NULL) {
|
|
238 rb = g_malloc0(sizeof(RendezvousBuddy));
|
|
239 g_hash_table_insert(rd->buddies, g_strdup(name), rb);
|
|
240 }
|
|
241
|
|
242 rb->p2pjport = rdata->port;
|
8487
|
243 }
|
|
244
|
|
245 /*
|
|
246 * Parse a resource record and do stuff if we need to.
|
|
247 */
|
|
248 static void rendezvous_handle_rr(GaimConnection *gc, ResourceRecord *rr)
|
|
249 {
|
|
250 gchar *name;
|
|
251
|
8594
|
252 gaim_debug_misc("rendezvous", "Parsing resource record with domain name %s\n", rr->name);
|
|
253
|
|
254 /*
|
|
255 * XXX - Cache resource records from this function, if needed.
|
|
256 * Use the TTL value of the rr to cause this data to expire, but let
|
|
257 * the mdns_cache stuff handle that as much as possible.
|
|
258 */
|
8487
|
259
|
|
260 switch (rr->type) {
|
|
261 case RENDEZVOUS_RRTYPE_NULL: {
|
|
262 if ((name = rendezvous_extract_name(rr->name)) != NULL) {
|
|
263 if (rr->rdlength > 0) {
|
|
264 /* Data is a buddy icon */
|
|
265 gaim_buddy_icons_set_for_user(gaim_connection_get_account(gc), name, rr->rdata, rr->rdlength);
|
|
266 }
|
|
267
|
|
268 g_free(name);
|
|
269 }
|
|
270 } break;
|
|
271
|
|
272 case RENDEZVOUS_RRTYPE_PTR: {
|
|
273 gchar *rdata = rr->rdata;
|
|
274 if ((name = rendezvous_extract_name(rdata)) != NULL) {
|
|
275 if (rr->ttl > 0)
|
|
276 rendezvous_addtolocal(gc, name, "Rendezvous");
|
|
277 else
|
|
278 rendezvous_removefromlocal(gc, name, "Rendezvous");
|
|
279
|
|
280 g_free(name);
|
|
281 }
|
|
282 } break;
|
|
283
|
|
284 case RENDEZVOUS_RRTYPE_TXT: {
|
|
285 if ((name = rendezvous_extract_name(rr->name)) != NULL) {
|
|
286 rendezvous_handle_rr_txt(gc, rr, name);
|
|
287 g_free(name);
|
|
288 }
|
|
289 } break;
|
8594
|
290
|
|
291 case RENDEZVOUS_RRTYPE_SRV: {
|
|
292 if ((name = rendezvous_extract_name(rr->name)) != NULL) {
|
|
293 rendezvous_handle_rr_srv(gc, rr, name);
|
|
294 g_free(name);
|
|
295 }
|
|
296 } break;
|
8487
|
297 }
|
|
298 }
|
|
299
|
|
300 /****************************/
|
|
301 /* Icon and Emblem Funtions */
|
|
302 /****************************/
|
|
303 static const char* rendezvous_prpl_list_icon(GaimAccount *a, GaimBuddy *b)
|
|
304 {
|
|
305 return "rendezvous";
|
|
306 }
|
|
307
|
|
308 static void rendezvous_prpl_list_emblems(GaimBuddy *b, char **se, char **sw, char **nw, char **ne)
|
|
309 {
|
|
310 if (GAIM_BUDDY_IS_ONLINE(b)) {
|
|
311 if (b->uc & UC_UNAVAILABLE)
|
|
312 *se = "away";
|
|
313 } else {
|
|
314 *se = "offline";
|
|
315 }
|
|
316 }
|
|
317
|
|
318 static gchar *rendezvous_prpl_status_text(GaimBuddy *b)
|
|
319 {
|
|
320 GaimConnection *gc = b->account->gc;
|
|
321 RendezvousData *rd = gc->proto_data;
|
|
322 RendezvousBuddy *rb;
|
|
323 gchar *ret;
|
|
324
|
|
325 rb = g_hash_table_lookup(rd->buddies, b->name);
|
|
326 if ((rb == NULL) || (rb->msg == NULL))
|
|
327 return NULL;
|
|
328
|
|
329 ret = g_strdup(rb->msg);
|
|
330
|
|
331 return ret;
|
|
332 }
|
|
333
|
|
334 static gchar *rendezvous_prpl_tooltip_text(GaimBuddy *b)
|
|
335 {
|
|
336 GaimConnection *gc = b->account->gc;
|
|
337 RendezvousData *rd = gc->proto_data;
|
|
338 RendezvousBuddy *rb;
|
|
339 GString *ret;
|
|
340
|
|
341 rb = g_hash_table_lookup(rd->buddies, b->name);
|
|
342 if (rb == NULL)
|
|
343 return NULL;
|
|
344
|
|
345 ret = g_string_new("");
|
|
346
|
|
347 if (rb->aim != NULL)
|
8591
|
348 g_string_append_printf(ret, "\n<b>%s</b>: %s", _("AIM Screen name"), rb->aim);
|
8487
|
349
|
|
350 if (rb->msg != NULL) {
|
|
351 if (rb->status == UC_UNAVAILABLE)
|
8591
|
352 g_string_append_printf(ret, "\n<b>%s</b>: %s", _("Away"), rb->msg);
|
8487
|
353 else
|
8591
|
354 g_string_append_printf(ret, "\n<b>%s</b>: %s", _("Available"), rb->msg);
|
8487
|
355 }
|
|
356
|
|
357 return g_string_free(ret, FALSE);
|
|
358 }
|
|
359
|
|
360 /****************************/
|
|
361 /* Connection Funtions */
|
|
362 /****************************/
|
|
363 static void rendezvous_callback(gpointer data, gint source, GaimInputCondition condition)
|
|
364 {
|
|
365 GaimConnection *gc = data;
|
|
366 RendezvousData *rd = gc->proto_data;
|
|
367 DNSPacket *dns;
|
|
368 int i;
|
|
369
|
|
370 gaim_debug_misc("rendezvous", "Received rendezvous datagram\n");
|
|
371
|
|
372 dns = mdns_read(rd->fd);
|
|
373 if (dns == NULL)
|
|
374 return;
|
|
375
|
|
376 /* Handle the DNS packet */
|
|
377 for (i = 0; i < dns->header.numanswers; i++)
|
|
378 rendezvous_handle_rr(gc, &dns->answers[i]);
|
|
379 for (i = 0; i < dns->header.numauthority; i++)
|
|
380 rendezvous_handle_rr(gc, &dns->authority[i]);
|
|
381 for (i = 0; i < dns->header.numadditional; i++)
|
|
382 rendezvous_handle_rr(gc, &dns->additional[i]);
|
|
383
|
|
384 mdns_free(dns);
|
|
385 }
|
|
386
|
|
387 static void rendezvous_prpl_login(GaimAccount *account)
|
|
388 {
|
|
389 GaimConnection *gc = gaim_account_get_connection(account);
|
|
390 RendezvousData *rd;
|
|
391
|
|
392 rd = g_new0(RendezvousData, 1);
|
|
393 rd->buddies = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, rendezvous_buddy_free);
|
|
394 gc->proto_data = rd;
|
|
395
|
|
396 gaim_connection_update_progress(gc, _("Preparing Buddy List"), 0, RENDEZVOUS_CONNECT_STEPS);
|
|
397 rendezvous_removeallfromlocal(gc);
|
|
398
|
|
399 gaim_connection_update_progress(gc, _("Connecting"), 1, RENDEZVOUS_CONNECT_STEPS);
|
|
400 rd->fd = mdns_establish_socket();
|
|
401 if (rd->fd == -1) {
|
|
402 gaim_connection_error(gc, _("Unable to login to rendezvous"));
|
|
403 return;
|
|
404 }
|
|
405
|
|
406 gc->inpa = gaim_input_add(rd->fd, GAIM_INPUT_READ, rendezvous_callback, gc);
|
|
407 gaim_connection_set_state(gc, GAIM_CONNECTED);
|
|
408
|
|
409 mdns_query(rd->fd, "_presence._tcp.local");
|
|
410
|
|
411 #if 0
|
|
412 text_record_add("txtvers", "1");
|
|
413 text_record_add("status", "avail");
|
|
414 text_record_add("1st", gaim_account_get_string(account, "first", "Gaim"));
|
|
415 text_record_add("AIM", "markdoliner");
|
|
416 text_record_add("version", "1");
|
|
417 text_record_add("port.p2pj", "5298");
|
|
418 text_record_add("last", gaim_account_get_string(account, "last", _("User")));
|
|
419
|
|
420 publish(account->username, "_presence._tcp", 5298);
|
|
421 #endif
|
|
422 }
|
|
423
|
|
424 static void rendezvous_prpl_close(GaimConnection *gc)
|
|
425 {
|
|
426 RendezvousData *rd = (RendezvousData *)gc->proto_data;
|
|
427
|
|
428 if (gc->inpa)
|
|
429 gaim_input_remove(gc->inpa);
|
|
430
|
|
431 rendezvous_removeallfromlocal(gc);
|
|
432
|
|
433 if (!rd)
|
|
434 return;
|
|
435
|
|
436 if (rd->fd >= 0)
|
|
437 close(rd->fd);
|
|
438
|
|
439 g_hash_table_destroy(rd->buddies);
|
|
440
|
|
441 g_free(rd);
|
|
442 }
|
|
443
|
|
444 static int rendezvous_prpl_send_im(GaimConnection *gc, const char *who, const char *message, GaimConvImFlags flags)
|
|
445 {
|
|
446 gaim_debug_info("rendezvous", "Sending IM\n");
|
|
447
|
|
448 return 1;
|
|
449 }
|
|
450
|
8589
|
451 static void rendezvous_prpl_set_away(GaimConnection *gc, const char *state, const char *text)
|
8487
|
452 {
|
|
453 gaim_debug_error("rendezvous", "Set away, state=%s, text=%s\n", state, text);
|
|
454 }
|
|
455
|
|
456 static GaimPlugin *my_protocol = NULL;
|
|
457
|
|
458 static GaimPluginProtocolInfo prpl_info =
|
|
459 {
|
|
460 OPT_PROTO_NO_PASSWORD,
|
|
461 NULL,
|
|
462 NULL,
|
|
463 NULL,
|
|
464 rendezvous_prpl_list_icon,
|
|
465 rendezvous_prpl_list_emblems,
|
|
466 rendezvous_prpl_status_text,
|
|
467 rendezvous_prpl_tooltip_text,
|
|
468 NULL,
|
|
469 NULL,
|
|
470 NULL,
|
|
471 NULL,
|
|
472 rendezvous_prpl_login,
|
|
473 rendezvous_prpl_close,
|
|
474 rendezvous_prpl_send_im,
|
|
475 NULL,
|
|
476 NULL,
|
|
477 NULL,
|
8589
|
478 rendezvous_prpl_set_away,
|
8487
|
479 NULL,
|
|
480 NULL,
|
|
481 NULL,
|
|
482 NULL,
|
|
483 NULL,
|
|
484 NULL,
|
|
485 NULL,
|
|
486 NULL,
|
|
487 NULL,
|
|
488 NULL,
|
|
489 NULL,
|
|
490 NULL,
|
|
491 NULL,
|
|
492 NULL,
|
|
493 NULL,
|
|
494 NULL,
|
|
495 NULL,
|
|
496 NULL,
|
|
497 NULL,
|
|
498 NULL,
|
|
499 NULL,
|
|
500 NULL,
|
|
501 NULL,
|
|
502 NULL,
|
|
503 NULL,
|
|
504 NULL,
|
|
505 NULL,
|
|
506 NULL,
|
|
507 NULL,
|
|
508 NULL,
|
8586
|
509 NULL,
|
8589
|
510 NULL
|
8487
|
511 };
|
|
512
|
|
513 static GaimPluginInfo info =
|
|
514 {
|
|
515 2, /**< api_version */
|
|
516 GAIM_PLUGIN_PROTOCOL, /**< type */
|
|
517 NULL, /**< ui_requirement */
|
|
518 0, /**< flags */
|
|
519 NULL, /**< dependencies */
|
|
520 GAIM_PRIORITY_DEFAULT, /**< priority */
|
|
521
|
|
522 "prpl-rendezvous", /**< id */
|
|
523 "Rendezvous", /**< name */
|
|
524 VERSION, /**< version */
|
|
525 /** summary */
|
|
526 N_("Rendezvous Protocol Plugin"),
|
|
527 /** description */
|
|
528 N_("Rendezvous Protocol Plugin"),
|
|
529 NULL, /**< author */
|
|
530 GAIM_WEBSITE, /**< homepage */
|
|
531
|
|
532 NULL, /**< load */
|
|
533 NULL, /**< unload */
|
|
534 NULL, /**< destroy */
|
|
535
|
|
536 NULL, /**< ui_info */
|
|
537 &prpl_info /**< extra_info */
|
|
538 };
|
|
539
|
|
540 static void init_plugin(GaimPlugin *plugin)
|
|
541 {
|
|
542 GaimAccountUserSplit *split;
|
|
543 GaimAccountOption *option;
|
|
544
|
|
545 /* Try to avoid making this configurable... */
|
|
546 split = gaim_account_user_split_new(_("Host Name"), "localhost", '@');
|
|
547 prpl_info.user_splits = g_list_append(prpl_info.user_splits, split);
|
|
548
|
|
549 option = gaim_account_option_string_new(_("First Name"), "first", "Gaim");
|
|
550 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options,
|
|
551 option);
|
|
552
|
|
553 option = gaim_account_option_string_new(_("Last Name"), "last", _("User"));
|
|
554 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options,
|
|
555 option);
|
|
556
|
|
557 option = gaim_account_option_bool_new(_("Share AIM screen name"), "shareaim", TRUE);
|
|
558 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options,
|
|
559 option);
|
|
560
|
|
561 my_protocol = plugin;
|
|
562 }
|
|
563
|
|
564 GAIM_INIT_PLUGIN(rendezvous, init_plugin, info);
|