Mercurial > pidgin
comparison libpurple/privacy.c @ 15373:5fe8042783c1
Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Sat, 20 Jan 2007 02:32:10 +0000 |
parents | |
children | 32c366eeeb99 |
comparison
equal
deleted
inserted
replaced
15372:f79e0f4df793 | 15373:5fe8042783c1 |
---|---|
1 /** | |
2 * gaim | |
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 "privacy.h" | |
26 #include "server.h" | |
27 #include "util.h" | |
28 | |
29 static GaimPrivacyUiOps *privacy_ops = NULL; | |
30 | |
31 gboolean | |
32 gaim_privacy_permit_add(GaimAccount *account, const char *who, | |
33 gboolean local_only) | |
34 { | |
35 GSList *l; | |
36 char *name; | |
37 GaimBuddy *buddy; | |
38 | |
39 g_return_val_if_fail(account != NULL, FALSE); | |
40 g_return_val_if_fail(who != NULL, FALSE); | |
41 | |
42 name = g_strdup(gaim_normalize(account, who)); | |
43 | |
44 for (l = account->permit; l != NULL; l = l->next) { | |
45 if (!gaim_utf8_strcasecmp(name, (char *)l->data)) | |
46 break; | |
47 } | |
48 | |
49 if (l != NULL) | |
50 { | |
51 g_free(name); | |
52 return FALSE; | |
53 } | |
54 | |
55 account->permit = g_slist_append(account->permit, name); | |
56 | |
57 if (!local_only && gaim_account_is_connected(account)) | |
58 serv_add_permit(gaim_account_get_connection(account), who); | |
59 | |
60 if (privacy_ops != NULL && privacy_ops->permit_added != NULL) | |
61 privacy_ops->permit_added(account, who); | |
62 | |
63 gaim_blist_schedule_save(); | |
64 | |
65 /* This lets the UI know a buddy has had its privacy setting changed */ | |
66 buddy = gaim_find_buddy(account, name); | |
67 if (buddy != NULL) { | |
68 gaim_signal_emit(gaim_blist_get_handle(), | |
69 "buddy-privacy-changed", buddy); | |
70 } | |
71 return TRUE; | |
72 } | |
73 | |
74 gboolean | |
75 gaim_privacy_permit_remove(GaimAccount *account, const char *who, | |
76 gboolean local_only) | |
77 { | |
78 GSList *l; | |
79 const char *name; | |
80 GaimBuddy *buddy; | |
81 char *del; | |
82 | |
83 g_return_val_if_fail(account != NULL, FALSE); | |
84 g_return_val_if_fail(who != NULL, FALSE); | |
85 | |
86 name = gaim_normalize(account, who); | |
87 | |
88 for (l = account->permit; l != NULL; l = l->next) { | |
89 if (!gaim_utf8_strcasecmp(name, (char *)l->data)) | |
90 break; | |
91 } | |
92 | |
93 if (l == NULL) | |
94 return FALSE; | |
95 | |
96 /* We should not free l->data just yet. There can be occasions where | |
97 * l->data == who. In such cases, freeing l->data here can cause crashes | |
98 * later when who is used. */ | |
99 del = l->data; | |
100 account->permit = g_slist_delete_link(account->permit, l); | |
101 | |
102 if (!local_only && gaim_account_is_connected(account)) | |
103 serv_rem_permit(gaim_account_get_connection(account), who); | |
104 | |
105 if (privacy_ops != NULL && privacy_ops->permit_removed != NULL) | |
106 privacy_ops->permit_removed(account, who); | |
107 | |
108 gaim_blist_schedule_save(); | |
109 | |
110 buddy = gaim_find_buddy(account, name); | |
111 if (buddy != NULL) { | |
112 gaim_signal_emit(gaim_blist_get_handle(), | |
113 "buddy-privacy-changed", buddy); | |
114 } | |
115 g_free(del); | |
116 return TRUE; | |
117 } | |
118 | |
119 gboolean | |
120 gaim_privacy_deny_add(GaimAccount *account, const char *who, | |
121 gboolean local_only) | |
122 { | |
123 GSList *l; | |
124 char *name; | |
125 GaimBuddy *buddy; | |
126 | |
127 g_return_val_if_fail(account != NULL, FALSE); | |
128 g_return_val_if_fail(who != NULL, FALSE); | |
129 | |
130 name = g_strdup(gaim_normalize(account, who)); | |
131 | |
132 for (l = account->deny; l != NULL; l = l->next) { | |
133 if (!gaim_utf8_strcasecmp(name, gaim_normalize(account, (char *)l->data))) | |
134 break; | |
135 } | |
136 | |
137 if (l != NULL) | |
138 { | |
139 g_free(name); | |
140 return FALSE; | |
141 } | |
142 | |
143 account->deny = g_slist_append(account->deny, name); | |
144 | |
145 if (!local_only && gaim_account_is_connected(account)) | |
146 serv_add_deny(gaim_account_get_connection(account), who); | |
147 | |
148 if (privacy_ops != NULL && privacy_ops->deny_added != NULL) | |
149 privacy_ops->deny_added(account, who); | |
150 | |
151 gaim_blist_schedule_save(); | |
152 | |
153 buddy = gaim_find_buddy(account, name); | |
154 if (buddy != NULL) { | |
155 gaim_signal_emit(gaim_blist_get_handle(), | |
156 "buddy-privacy-changed", buddy); | |
157 } | |
158 return TRUE; | |
159 } | |
160 | |
161 gboolean | |
162 gaim_privacy_deny_remove(GaimAccount *account, const char *who, | |
163 gboolean local_only) | |
164 { | |
165 GSList *l; | |
166 const char *normalized; | |
167 char *name; | |
168 GaimBuddy *buddy; | |
169 | |
170 g_return_val_if_fail(account != NULL, FALSE); | |
171 g_return_val_if_fail(who != NULL, FALSE); | |
172 | |
173 normalized = gaim_normalize(account, who); | |
174 | |
175 for (l = account->deny; l != NULL; l = l->next) { | |
176 if (!gaim_utf8_strcasecmp(normalized, (char *)l->data)) | |
177 break; | |
178 } | |
179 | |
180 buddy = gaim_find_buddy(account, normalized); | |
181 | |
182 if (l == NULL) | |
183 return FALSE; | |
184 | |
185 name = l->data; | |
186 account->deny = g_slist_delete_link(account->deny, l); | |
187 | |
188 if (!local_only && gaim_account_is_connected(account)) | |
189 serv_rem_deny(gaim_account_get_connection(account), name); | |
190 | |
191 if (privacy_ops != NULL && privacy_ops->deny_removed != NULL) | |
192 privacy_ops->deny_removed(account, who); | |
193 | |
194 if (buddy != NULL) { | |
195 gaim_signal_emit(gaim_blist_get_handle(), | |
196 "buddy-privacy-changed", buddy); | |
197 } | |
198 | |
199 g_free(name); | |
200 gaim_blist_schedule_save(); | |
201 | |
202 return TRUE; | |
203 } | |
204 | |
205 /* This makes sure that only all the buddies are in the permit list. */ | |
206 static void | |
207 add_buddies_in_permit(GaimAccount *account, gboolean local) | |
208 { | |
209 GSList *list, *iter; | |
210 /* Remove anyone in the permit list who is not in the buddylist */ | |
211 for (list = account->permit; list != NULL; ) { | |
212 char *person = list->data; | |
213 list = list->next; | |
214 if (!gaim_find_buddy(account, person)) | |
215 gaim_privacy_permit_remove(account, person, local); | |
216 } | |
217 /* Now make sure everyone in the buddylist is in the permit list */ | |
218 for (iter = list = gaim_find_buddies(account, NULL); iter; iter = iter->next) { | |
219 GaimBuddy *buddy = iter->data; | |
220 if (!g_slist_find_custom(account->permit, buddy->name, (GCompareFunc)g_utf8_collate)) | |
221 gaim_privacy_permit_add(account, buddy->name, local); | |
222 } | |
223 g_slist_free(list); | |
224 } | |
225 | |
226 void | |
227 gaim_privacy_allow(GaimAccount *account, const char *who, gboolean local, | |
228 gboolean restore) | |
229 { | |
230 GSList *list; | |
231 | |
232 switch (account->perm_deny) { | |
233 case GAIM_PRIVACY_ALLOW_ALL: | |
234 return; | |
235 case GAIM_PRIVACY_ALLOW_USERS: | |
236 gaim_privacy_permit_add(account, who, local); | |
237 break; | |
238 case GAIM_PRIVACY_DENY_USERS: | |
239 gaim_privacy_deny_remove(account, who, local); | |
240 break; | |
241 case GAIM_PRIVACY_DENY_ALL: | |
242 if (!restore) { | |
243 /* Empty the allow-list. */ | |
244 for (list = account->permit; list != NULL;) { | |
245 char *who = list->data; | |
246 list = list->next; | |
247 gaim_privacy_permit_remove(account, who, local); | |
248 } | |
249 } | |
250 gaim_privacy_permit_add(account, who, local); | |
251 account->perm_deny = GAIM_PRIVACY_ALLOW_USERS; | |
252 break; | |
253 case GAIM_PRIVACY_ALLOW_BUDDYLIST: | |
254 if (!gaim_find_buddy(account, who)) { | |
255 add_buddies_in_permit(account, local); | |
256 gaim_privacy_permit_add(account, who, local); | |
257 account->perm_deny = GAIM_PRIVACY_ALLOW_USERS; | |
258 } | |
259 break; | |
260 default: | |
261 g_return_if_reached(); | |
262 } | |
263 } | |
264 | |
265 void | |
266 gaim_privacy_deny(GaimAccount *account, const char *who, gboolean local, | |
267 gboolean restore) | |
268 { | |
269 GSList *list; | |
270 | |
271 switch (account->perm_deny) { | |
272 case GAIM_PRIVACY_ALLOW_ALL: | |
273 if (!restore) { | |
274 /* Empty the deny-list. */ | |
275 for (list = account->deny; list != NULL; ) { | |
276 char *person = list->data; | |
277 list = list->next; | |
278 gaim_privacy_deny_remove(account, person, local); | |
279 } | |
280 } | |
281 gaim_privacy_deny_add(account, who, local); | |
282 account->perm_deny = GAIM_PRIVACY_DENY_USERS; | |
283 break; | |
284 case GAIM_PRIVACY_ALLOW_USERS: | |
285 gaim_privacy_permit_remove(account, who, local); | |
286 break; | |
287 case GAIM_PRIVACY_DENY_USERS: | |
288 gaim_privacy_deny_add(account, who, local); | |
289 break; | |
290 case GAIM_PRIVACY_DENY_ALL: | |
291 break; | |
292 case GAIM_PRIVACY_ALLOW_BUDDYLIST: | |
293 if (gaim_find_buddy(account, who)) { | |
294 add_buddies_in_permit(account, local); | |
295 gaim_privacy_permit_remove(account, who, local); | |
296 account->perm_deny = GAIM_PRIVACY_ALLOW_USERS; | |
297 } | |
298 break; | |
299 default: | |
300 g_return_if_reached(); | |
301 } | |
302 } | |
303 | |
304 gboolean | |
305 gaim_privacy_check(GaimAccount *account, const char *who) | |
306 { | |
307 GSList *list; | |
308 | |
309 switch (account->perm_deny) { | |
310 case GAIM_PRIVACY_ALLOW_ALL: | |
311 return TRUE; | |
312 | |
313 case GAIM_PRIVACY_DENY_ALL: | |
314 return FALSE; | |
315 | |
316 case GAIM_PRIVACY_ALLOW_USERS: | |
317 who = gaim_normalize(account, who); | |
318 for (list=account->permit; list!=NULL; list=list->next) { | |
319 if (!gaim_utf8_strcasecmp(who, (char *)list->data)) | |
320 return TRUE; | |
321 } | |
322 return FALSE; | |
323 | |
324 case GAIM_PRIVACY_DENY_USERS: | |
325 who = gaim_normalize(account, who); | |
326 for (list=account->deny; list!=NULL; list=list->next) { | |
327 if (!gaim_utf8_strcasecmp(who, (char *)list->data )) | |
328 return FALSE; | |
329 } | |
330 return TRUE; | |
331 | |
332 case GAIM_PRIVACY_ALLOW_BUDDYLIST: | |
333 return (gaim_find_buddy(account, who) != NULL); | |
334 | |
335 default: | |
336 g_return_val_if_reached(TRUE); | |
337 } | |
338 } | |
339 | |
340 void | |
341 gaim_privacy_set_ui_ops(GaimPrivacyUiOps *ops) | |
342 { | |
343 privacy_ops = ops; | |
344 } | |
345 | |
346 GaimPrivacyUiOps * | |
347 gaim_privacy_get_ui_ops(void) | |
348 { | |
349 return privacy_ops; | |
350 } | |
351 | |
352 void | |
353 gaim_privacy_init(void) | |
354 { | |
355 } |