11477
|
1 /*
|
|
2 * This program is free software; you can redistribute it and/or modify
|
|
3 * it under the terms of the GNU General Public License as published by
|
|
4 * the Free Software Foundation; either version 2 of the License, or
|
|
5 * (at your option) any later version.
|
|
6 *
|
|
7 * This program is distributed in the hope that it will be useful,
|
|
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 * GNU Library General Public License for more details.
|
|
11 *
|
|
12 * You should have received a copy of the GNU General Public License
|
|
13 * along with this program; if not, write to the Free Software
|
|
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
15 */
|
|
16
|
|
17
|
|
18 #include <glib.h>
|
|
19 #include <stdlib.h>
|
|
20
|
|
21 #include "buddy.h"
|
|
22 #include "account.h"
|
|
23 #include "blist.h"
|
|
24 #include "bonjour.h"
|
|
25 #include "debug.h"
|
|
26
|
|
27 /**
|
|
28 * Creates a new buddy.
|
|
29 */
|
|
30 BonjourBuddy* bonjour_buddy_new(gchar* name, gchar* first, gint port_p2pj,
|
|
31 gchar* phsh, gchar* status, gchar* email, gchar* last, gchar* jid, gchar* AIM,
|
|
32 gchar* vc, gchar* ip, gchar* msg)
|
|
33 {
|
|
34 BonjourBuddy* buddy = malloc(sizeof(BonjourBuddy));
|
|
35
|
|
36 buddy->name = g_strdup(name);
|
|
37 buddy->first = g_strdup(first);
|
|
38 buddy->port_p2pj = port_p2pj;
|
|
39 buddy->phsh = g_strdup(phsh);
|
|
40 buddy->status = g_strdup(status);
|
|
41 buddy->email = g_strdup(email);
|
|
42 buddy->last = g_strdup(last);
|
|
43 buddy->jid = g_strdup(jid);
|
|
44 buddy->AIM = g_strdup(AIM);
|
|
45 buddy->vc = g_strdup(vc);
|
|
46 buddy->ip = g_strdup(ip);
|
|
47 buddy->msg = g_strdup(msg);
|
|
48 buddy->conversation = NULL;
|
|
49
|
|
50 return buddy;
|
|
51 }
|
|
52
|
|
53 /**
|
|
54 * Check if all the compulsory buddy data is present.
|
|
55 */
|
|
56 gboolean bonjour_buddy_check(BonjourBuddy* buddy)
|
|
57 {
|
|
58 if(buddy->name == NULL){
|
|
59 return FALSE;
|
|
60 }
|
|
61
|
|
62 if(buddy->first == NULL){
|
|
63 return FALSE;
|
|
64 }
|
|
65
|
|
66 if(buddy->last == NULL){
|
|
67 return FALSE;
|
|
68 }
|
|
69
|
|
70 if(buddy->port_p2pj == -1){
|
|
71 return FALSE;
|
|
72 }
|
|
73
|
|
74 if(buddy->status == NULL){
|
|
75 return FALSE;
|
|
76 }
|
|
77
|
|
78 return TRUE;
|
|
79 }
|
|
80
|
|
81 /**
|
11496
|
82 * If the buddy does not yet exist, then create it and add it to
|
|
83 * our buddy list. In either case we set the correct status for
|
|
84 * the buddy.
|
11477
|
85 */
|
11496
|
86 void
|
|
87 bonjour_buddy_add_to_gaim(GaimAccount *account, BonjourBuddy *bonjour_buddy)
|
11477
|
88 {
|
11496
|
89 GaimBuddy *buddy;
|
|
90 GaimGroup *group;
|
|
91 const char *status_id, *first, *last;
|
|
92 char *alias;
|
11477
|
93
|
11496
|
94 /* Translate between the Bonjour status and the Gaim status */
|
|
95 if (g_ascii_strcasecmp("dnd", bonjour_buddy->status) == 0)
|
|
96 status_id = BONJOUR_STATUS_ID_AWAY;
|
|
97 else
|
|
98 status_id = BONJOUR_STATUS_ID_AVAILABLE;
|
|
99
|
|
100 /*
|
|
101 * TODO: Figure out the idle time by getting the "away"
|
|
102 * field from the DNS SD.
|
|
103 */
|
11477
|
104
|
11496
|
105 /* Create the alias for the buddy using the first and the last name */
|
|
106 first = bonjour_buddy->first;
|
|
107 last = bonjour_buddy->last;
|
|
108 alias = g_strdup_printf("%s%s%s",
|
|
109 (first && *first ? first : ""),
|
|
110 (first && *first && last && *last ? " " : ""),
|
|
111 (last && *last ? last : ""));
|
|
112
|
|
113 /* Make sure the Bonjour group exists in our buddy list */
|
|
114 group = gaim_find_group(BONJOUR_GROUP_NAME); /* Use the buddy's domain, instead? */
|
|
115 if (group == NULL)
|
|
116 {
|
|
117 group = gaim_group_new(BONJOUR_GROUP_NAME);
|
|
118 gaim_blist_add_group(group, NULL);
|
11477
|
119 }
|
11496
|
120
|
|
121 /* Make sure the buddy exists in our buddy list */
|
|
122 buddy = gaim_find_buddy(account, bonjour_buddy->name);
|
|
123 if (buddy == NULL)
|
|
124 {
|
|
125 buddy = gaim_buddy_new(account, bonjour_buddy->name, alias);
|
|
126 gaim_blist_node_set_flags((GaimBlistNode *)buddy, GAIM_BLIST_NODE_FLAG_NO_SAVE);
|
|
127 gaim_blist_add_buddy(buddy, NULL, group, NULL);
|
11477
|
128 }
|
11496
|
129
|
|
130 /* Set the user's status */
|
|
131 gaim_prpl_got_user_status(account, buddy->name, status_id, NULL);
|
|
132 gaim_prpl_got_user_idle(account, buddy->name, FALSE, 0);
|
|
133
|
|
134 g_free(alias);
|
11477
|
135 }
|
|
136
|
|
137 /**
|
|
138 * Deletes a buddy from memory.
|
|
139 */
|
|
140 void bonjour_buddy_delete(BonjourBuddy* buddy)
|
|
141 {
|
|
142 g_free(buddy->name);
|
|
143 g_free(buddy->first);
|
|
144 g_free(buddy->phsh);
|
|
145 g_free(buddy->status);
|
|
146 g_free(buddy->email);
|
|
147 g_free(buddy->last);
|
|
148 g_free(buddy->jid);
|
|
149 g_free(buddy->AIM);
|
|
150 g_free(buddy->vc);
|
|
151 g_free(buddy->ip);
|
|
152 g_free(buddy->msg);
|
|
153
|
|
154 if (buddy->conversation != NULL) {
|
|
155 g_free(buddy->conversation->buddy_name);
|
|
156 g_free(buddy->conversation);
|
|
157 }
|
|
158
|
|
159 free(buddy);
|
|
160 }
|