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 #include <glib.h>
|
|
18 #include <stdlib.h>
|
|
19
|
|
20 #include "buddy.h"
|
|
21 #include "account.h"
|
|
22 #include "blist.h"
|
|
23 #include "bonjour.h"
|
|
24 #include "debug.h"
|
|
25
|
|
26 /**
|
|
27 * Creates a new buddy.
|
|
28 */
|
11539
|
29 BonjourBuddy *
|
|
30 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)
|
11477
|
33 {
|
11539
|
34 BonjourBuddy *buddy = malloc(sizeof(BonjourBuddy));
|
11477
|
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;
|
11539
|
49
|
11477
|
50 return buddy;
|
|
51 }
|
|
52
|
|
53 /**
|
|
54 * Check if all the compulsory buddy data is present.
|
|
55 */
|
11539
|
56 gboolean
|
|
57 bonjour_buddy_check(BonjourBuddy *buddy)
|
11477
|
58 {
|
11539
|
59 if (buddy->name == NULL) {
|
11477
|
60 return FALSE;
|
|
61 }
|
11539
|
62
|
|
63 if (buddy->first == NULL) {
|
11477
|
64 return FALSE;
|
|
65 }
|
11539
|
66
|
|
67 if (buddy->last == NULL) {
|
11477
|
68 return FALSE;
|
|
69 }
|
11539
|
70
|
|
71 if (buddy->port_p2pj == -1) {
|
11477
|
72 return FALSE;
|
|
73 }
|
11539
|
74
|
|
75 if (buddy->status == NULL) {
|
11477
|
76 return FALSE;
|
|
77 }
|
11539
|
78
|
11477
|
79 return TRUE;
|
|
80 }
|
|
81
|
|
82 /**
|
11496
|
83 * If the buddy does not yet exist, then create it and add it to
|
|
84 * our buddy list. In either case we set the correct status for
|
|
85 * the buddy.
|
11477
|
86 */
|
11496
|
87 void
|
|
88 bonjour_buddy_add_to_gaim(GaimAccount *account, BonjourBuddy *bonjour_buddy)
|
11477
|
89 {
|
11496
|
90 GaimBuddy *buddy;
|
|
91 GaimGroup *group;
|
|
92 const char *status_id, *first, *last;
|
|
93 char *alias;
|
11477
|
94
|
11496
|
95 /* Translate between the Bonjour status and the Gaim status */
|
|
96 if (g_ascii_strcasecmp("dnd", bonjour_buddy->status) == 0)
|
|
97 status_id = BONJOUR_STATUS_ID_AWAY;
|
|
98 else
|
|
99 status_id = BONJOUR_STATUS_ID_AVAILABLE;
|
|
100
|
|
101 /*
|
|
102 * TODO: Figure out the idle time by getting the "away"
|
|
103 * field from the DNS SD.
|
|
104 */
|
11477
|
105
|
11496
|
106 /* Create the alias for the buddy using the first and the last name */
|
|
107 first = bonjour_buddy->first;
|
|
108 last = bonjour_buddy->last;
|
|
109 alias = g_strdup_printf("%s%s%s",
|
|
110 (first && *first ? first : ""),
|
|
111 (first && *first && last && *last ? " " : ""),
|
|
112 (last && *last ? last : ""));
|
|
113
|
|
114 /* Make sure the Bonjour group exists in our buddy list */
|
|
115 group = gaim_find_group(BONJOUR_GROUP_NAME); /* Use the buddy's domain, instead? */
|
|
116 if (group == NULL)
|
|
117 {
|
|
118 group = gaim_group_new(BONJOUR_GROUP_NAME);
|
|
119 gaim_blist_add_group(group, NULL);
|
11477
|
120 }
|
11496
|
121
|
|
122 /* Make sure the buddy exists in our buddy list */
|
|
123 buddy = gaim_find_buddy(account, bonjour_buddy->name);
|
|
124 if (buddy == NULL)
|
|
125 {
|
|
126 buddy = gaim_buddy_new(account, bonjour_buddy->name, alias);
|
11519
|
127 buddy->proto_data = bonjour_buddy;
|
11496
|
128 gaim_blist_node_set_flags((GaimBlistNode *)buddy, GAIM_BLIST_NODE_FLAG_NO_SAVE);
|
|
129 gaim_blist_add_buddy(buddy, NULL, group, NULL);
|
11477
|
130 }
|
11496
|
131
|
|
132 /* Set the user's status */
|
11519
|
133 if (bonjour_buddy->msg != NULL)
|
|
134 gaim_prpl_got_user_status(account, buddy->name, status_id,
|
|
135 "message", bonjour_buddy->msg,
|
|
136 NULL);
|
|
137 else
|
|
138 gaim_prpl_got_user_status(account, buddy->name, status_id,
|
|
139 NULL);
|
11496
|
140 gaim_prpl_got_user_idle(account, buddy->name, FALSE, 0);
|
|
141
|
|
142 g_free(alias);
|
11477
|
143 }
|
|
144
|
|
145 /**
|
|
146 * Deletes a buddy from memory.
|
|
147 */
|
11539
|
148 void
|
|
149 bonjour_buddy_delete(BonjourBuddy *buddy)
|
11477
|
150 {
|
|
151 g_free(buddy->name);
|
|
152 g_free(buddy->first);
|
|
153 g_free(buddy->phsh);
|
|
154 g_free(buddy->status);
|
|
155 g_free(buddy->email);
|
|
156 g_free(buddy->last);
|
|
157 g_free(buddy->jid);
|
|
158 g_free(buddy->AIM);
|
|
159 g_free(buddy->vc);
|
|
160 g_free(buddy->ip);
|
|
161 g_free(buddy->msg);
|
11539
|
162
|
|
163 if (buddy->conversation != NULL)
|
|
164 {
|
11477
|
165 g_free(buddy->conversation->buddy_name);
|
|
166 g_free(buddy->conversation);
|
|
167 }
|
11539
|
168
|
11477
|
169 free(buddy);
|
|
170 }
|