1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (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 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 "common/setup_before.h"
18 #include <stdio.h>
19 #ifdef HAVE_STDDEF_H
20 # include <stddef.h>
21 #else
22 # ifndef NULL
23 #  define NULL ((void *)0)
24 # endif
25 #endif
26 #ifdef STDC_HEADERS
27 # include <stdlib.h>
28 #else
29 # ifdef HAVE_MALLOC_H
30 #  include <malloc.h>
31 # endif
32 #endif
33 
34 #ifdef HAVE_STRING_H
35 # include <string.h>
36 #else
37 # ifdef HAVE_STRINGS_H
38 #  include <strings.h>
39 # endif
40 #endif
41 
42 #include "compat/strcasecmp.h"
43 
44 #include "common/eventlog.h"
45 #include "common/list.h"
46 #include "common/xalloc.h"
47 #include "account.h"
48 #include "prefs.h"
49 #include "friends.h"
50 #include "common/setup_after.h"
51 
friend_get_account(t_friend * fr)52 extern t_account * friend_get_account(t_friend * fr)
53 {
54     if (fr == NULL)
55     {
56         eventlog(eventlog_level_error, __FUNCTION__,"got NULL account");
57         return NULL;
58     }
59     return fr->friendacc;
60 }
61 
friend_set_account(t_friend * fr,t_account * acc)62 extern int friend_set_account(t_friend * fr, t_account * acc)
63 {
64     if (fr == NULL)
65     {
66         eventlog(eventlog_level_error, __FUNCTION__,"got NULL account");
67         return -1;
68     }
69     fr->friendacc=acc;
70     return 0;
71 }
72 
friend_get_mutual(t_friend * fr)73 extern char friend_get_mutual(t_friend * fr)
74 {
75     if (fr == NULL)
76     {
77         eventlog(eventlog_level_error, __FUNCTION__,"got NULL account");
78         return 0;
79     }
80     return fr->mutual;
81 }
82 
friend_set_mutual(t_friend * fr,char mutual)83 extern int friend_set_mutual(t_friend * fr, char mutual)
84 {
85     if (fr == NULL)
86     {
87         eventlog(eventlog_level_error, __FUNCTION__,"got NULL account");
88         return -1;
89     }
90     fr->mutual=mutual;
91     return 0;
92 }
93 
friendlist_unload(t_list * flist)94 extern int friendlist_unload(t_list * flist)
95 {
96     t_elem  * curr;
97     t_friend * fr;
98     if(flist==NULL)
99         return -1;
100     LIST_TRAVERSE(flist,curr)
101     {
102         if (!(fr = elem_get_data(curr)))
103         {
104             eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
105             continue;
106         }
107         fr->mutual=-1;
108     }
109     return 0;
110 }
111 
friendlist_close(t_list * flist)112 extern int friendlist_close(t_list * flist)
113 {
114     t_elem * curr;
115     t_friend * fr;
116 
117     if(flist==NULL)
118         return -1;
119     LIST_TRAVERSE(flist,curr)
120     {
121         if (!(fr = elem_get_data(curr)))
122         {
123             eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
124             continue;
125         }
126 
127 	if (list_remove_elem(flist, &curr) < 0)
128 	    eventlog(eventlog_level_error, __FUNCTION__, "could not remove elem from flist");
129         xfree((void *) fr);
130     }
131     list_destroy(flist);
132     return 0;
133 }
134 
friendlist_purge(t_list * flist)135 extern int friendlist_purge(t_list * flist)
136 {
137     t_elem  * curr;
138     t_friend * fr;
139 
140     if(flist==NULL)
141         return -1;
142     LIST_TRAVERSE(flist,curr)
143     {
144         if (!(fr = elem_get_data(curr)))
145         {
146             eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
147             continue;
148         }
149         if (fr->mutual<0)
150           {
151             if(list_remove_elem(flist, &curr)<0)
152                 eventlog(eventlog_level_error,__FUNCTION__,"could not remove item from list");
153           }
154     }
155     return 0;
156 }
157 
friendlist_add_account(t_list * flist,t_account * acc,int mutual)158 extern int friendlist_add_account(t_list * flist, t_account * acc, int mutual)
159 {
160     t_friend * fr;
161 
162     if(flist==NULL)
163         return -1;
164 
165     fr = xmalloc(sizeof(t_friend));
166     fr->friendacc = acc;
167     fr->mutual = mutual;
168     list_append_data(flist, fr);
169     return 0;
170 }
171 
friendlist_remove_friend(t_list * flist,t_friend * fr)172 extern int friendlist_remove_friend(t_list * flist, t_friend * fr)
173 {
174     t_elem * elem;
175 
176     if(flist==NULL)
177         return -1;
178 
179     if(fr!=NULL)
180     {
181         if(list_remove_data(flist, fr, &elem)<0)
182         {
183             eventlog(eventlog_level_error,__FUNCTION__,"could not remove item from list");
184             return -1;
185         }
186 
187 	xfree((void *)fr);
188         return 0;
189     }
190     return -1;
191 }
192 
friendlist_remove_account(t_list * flist,t_account * acc)193 extern int friendlist_remove_account(t_list * flist, t_account * acc)
194 {
195     t_elem * elem;
196     t_friend * fr;
197 
198     if(flist==NULL)
199         return -1;
200 
201     fr=friendlist_find_account(flist, acc);
202     if(fr!=NULL)
203     {
204         if(list_remove_data(flist, fr, &elem)<0)
205         {
206             eventlog(eventlog_level_error,__FUNCTION__,"could not remove item from list");
207             return -1;
208         }
209 
210 	xfree((void *)fr);
211         return 0;
212     }
213     return -1;
214 }
215 
friendlist_remove_username(t_list * flist,const char * accname)216 extern int friendlist_remove_username(t_list * flist, const char * accname)
217 {
218     t_elem * elem;
219     t_friend * fr;
220 
221     if(flist==NULL)
222         return -1;
223 
224     fr=friendlist_find_username(flist, accname);
225     if(fr!=NULL)
226     {
227         if(list_remove_data(flist, fr, &elem)<0)
228         {
229             eventlog(eventlog_level_error,__FUNCTION__,"could not remove item from list");
230             return -1;
231         }
232 
233 	xfree((void *)fr);
234         return 0;
235     }
236     return -1;
237 }
238 
friendlist_find_account(t_list * flist,t_account * acc)239 extern t_friend * friendlist_find_account(t_list * flist, t_account * acc)
240 {
241     t_elem  * curr;
242     t_friend * fr;
243 
244     if(flist==NULL)
245         return NULL;
246 
247     LIST_TRAVERSE(flist,curr)
248     {
249         if (!(fr = elem_get_data(curr)))
250         {
251             eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
252             continue;
253         }
254         if (fr->friendacc == acc)
255             return fr;
256     }
257     return NULL;
258 }
259 
friendlist_find_username(t_list * flist,const char * accname)260 extern t_friend * friendlist_find_username(t_list * flist, const char * accname)
261 {
262     t_elem  * curr;
263     t_friend * fr;
264 
265     if(flist==NULL)
266         return NULL;
267 
268     LIST_TRAVERSE(flist,curr)
269     {
270         if (!(fr = elem_get_data(curr)))
271         {
272             eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
273             continue;
274         }
275         if (strcasecmp(account_get_name(fr->friendacc),accname)==0) return fr;
276     }
277     return NULL;
278 }
279 
friendlist_find_uid(t_list * flist,int uid)280 extern t_friend * friendlist_find_uid(t_list * flist, int uid)
281 {
282     t_elem  * curr;
283     t_friend * fr;
284 
285     if(flist==NULL)
286         return NULL;
287 
288     LIST_TRAVERSE(flist,curr)
289     {
290         if (!(fr = elem_get_data(curr)))
291         {
292             eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
293             continue;
294         }
295         if (account_get_uid(fr->friendacc)==uid) return fr;
296     }
297     return NULL;
298 }
299