/[LeafOK_CVS]/pvpgn-1.7.4/src/bnetd/watch.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/bnetd/watch.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Jun 6 03:41:37 2006 UTC (19 years, 9 months ago) by sysadm
Branch point for: GNU, MAIN
Content type: text/x-csrc
Initial revision

1 sysadm 1.1 /*
2     * Copyright (C) 1999,2000 Ross Combs (rocombs@cs.nmsu.edu)
3     *
4     * This program is free software; you can redistribute it and/or
5     * modify it under the terms of the GNU General Public License
6     * as published by the Free Software Foundation; either version 2
7     * of the License, or (at your option) any later version.
8     *
9     * This program is distributed in the hope that it will be useful,
10     * but WITHOUT ANY WARRANTY; without even the implied warranty of
11     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     * GNU General Public License for more details.
13     *
14     * You should have received a copy of the GNU General Public License
15     * along with this program; if not, write to the Free Software
16     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17     */
18     #define WATCH_INTERNAL_ACCESS
19     #include "common/setup_before.h"
20     #ifdef STDC_HEADERS
21     # include <stdlib.h>
22     #else
23     # ifdef HAVE_MALLOC_H
24     # include <malloc.h>
25     # endif
26     #endif
27     #ifdef HAVE_STRING_H
28     # include <string.h>
29     #else
30     # ifdef HAVE_STRINGS_H
31     # include <strings.h>
32     # endif
33     # ifdef HAVE_MEMORY_H
34     # include <memory.h>
35     # endif
36     #endif
37     #include "compat/strcasecmp.h"
38     #include "common/field_sizes.h"
39     #include "common/list.h"
40     #include "account.h"
41     #include "connection.h"
42     #include "common/eventlog.h"
43     #include "message.h"
44     #include "watch.h"
45     #include "friends.h"
46     #include "common/tag.h"
47     #include "common/xalloc.h"
48     #include "common/setup_after.h"
49    
50    
51     static t_list * watchlist_head=NULL;
52    
53    
54     /* who == NULL means anybody */
55     extern int watchlist_add_events(t_connection * owner, t_account * who, t_clienttag clienttag, t_watch_event events)
56     {
57     t_elem const * curr;
58     t_watch_pair * pair;
59    
60     if (!owner)
61     {
62     eventlog(eventlog_level_error,__FUNCTION__,"got NULL owner");
63     return -1;
64     }
65    
66     LIST_TRAVERSE_CONST(watchlist_head,curr)
67     {
68     pair = elem_get_data(curr);
69     if (!pair) /* should not happen */
70     {
71     eventlog(eventlog_level_error,__FUNCTION__,"watchlist contains NULL item");
72     return -1;
73     }
74     if (pair->owner==owner && pair->who==who && ((clienttag == pair->clienttag)))
75     {
76     pair->what |= events;
77     return 0;
78     }
79     }
80    
81     pair = xmalloc(sizeof(t_watch_pair));
82     pair->owner = owner;
83     pair->who = who;
84     pair->what = events;
85     pair->clienttag = clienttag;
86    
87     list_prepend_data(watchlist_head,pair);
88    
89     return 0;
90     }
91    
92    
93     /* who == NULL means anybody */
94     extern int watchlist_del_events(t_connection * owner, t_account * who, t_clienttag clienttag, t_watch_event events)
95     {
96     t_elem * curr;
97     t_watch_pair * pair;
98    
99     if (!owner)
100     {
101     eventlog(eventlog_level_error,__FUNCTION__,"got NULL owner");
102     return -1;
103     }
104    
105     LIST_TRAVERSE(watchlist_head,curr)
106     {
107     pair = elem_get_data(curr);
108     if (!pair) /* should not happen */
109     {
110     eventlog(eventlog_level_error,__FUNCTION__,"watchlist contains NULL item");
111     return -1;
112     }
113     if (pair->owner==owner && pair->who==who && ((!clienttag) || (clienttag == pair->clienttag)))
114     {
115     pair->what &= ~events;
116     if (pair->what==0)
117     {
118     if (list_remove_elem(watchlist_head,&curr)<0)
119     {
120     eventlog(eventlog_level_error,__FUNCTION__,"could not remove item");
121     pair->owner = NULL;
122     }
123     else
124     xfree(pair);
125     }
126    
127     return 0;
128     }
129     }
130    
131     return -1; /* not found */
132     }
133    
134    
135     /* this differs from del_events because it doesn't return an error if nothing was found */
136     extern int watchlist_del_all_events(t_connection * owner)
137     {
138     t_elem * curr;
139     t_watch_pair * pair;
140    
141     if (!owner)
142     {
143     eventlog(eventlog_level_error,__FUNCTION__,"got NULL owner");
144     return -1;
145     }
146    
147     LIST_TRAVERSE(watchlist_head,curr)
148     {
149     pair = elem_get_data(curr);
150     if (!pair) /* should not happen */
151     {
152     eventlog(eventlog_level_error,__FUNCTION__,"watchlist contains NULL item");
153     return -1;
154     }
155     if (pair->owner==owner)
156     {
157     if (list_remove_elem(watchlist_head,&curr)<0)
158     {
159     eventlog(eventlog_level_error,__FUNCTION__,"could not remove item");
160     pair->owner = NULL;
161     }
162     else
163     { xfree(pair); }
164     }
165     }
166    
167     return 0;
168     }
169    
170     /* this differs from del_events because it doesn't return an error if nothing was found */
171     extern int watchlist_del_by_account(t_account * who)
172     {
173     t_elem * curr;
174     t_watch_pair * pair;
175    
176     if (!who)
177     {
178     eventlog(eventlog_level_error,__FUNCTION__,"got NULL account");
179     return -1;
180     }
181    
182     LIST_TRAVERSE(watchlist_head,curr)
183     {
184     pair = elem_get_data(curr);
185     if (!pair) /* should not happen */
186     {
187     eventlog(eventlog_level_error,__FUNCTION__,"watchlist contains NULL item");
188     return -1;
189     }
190     if (pair->who==who)
191     {
192     if (list_remove_elem(watchlist_head,&curr)<0)
193     {
194     eventlog(eventlog_level_error,__FUNCTION__,"could not remove item");
195     pair->owner = NULL;
196     }
197     else
198     { xfree(pair); }
199     }
200     }
201    
202     return 0;
203     }
204    
205     static int handle_event_whisper(t_account *account, char const *gamename, t_clienttag clienttag, t_watch_event event)
206     {
207     t_elem const * curr;
208     t_watch_pair * pair;
209     char msg[512];
210     int cnt = 0;
211     char const *myusername;
212     t_list * flist;
213     t_connection * dest_c, * my_c;
214     t_friend * fr;
215     char const * game_title;
216    
217     if (!account)
218     {
219     eventlog(eventlog_level_error,__FUNCTION__,"got NULL account");
220     return -1;
221     }
222    
223     if (!(myusername = account_get_name(account)))
224     {
225     eventlog(eventlog_level_error,__FUNCTION__,"got NULL account name");
226     return -1;
227     }
228    
229     my_c = account_get_conn(account);
230    
231     game_title = clienttag_get_title(clienttag);
232    
233     /* mutual friends handling */
234     flist = account_get_friends(account);
235     if(flist)
236     {
237     if (event == watch_event_joingame) {
238     if (gamename)
239     sprintf(msg,"Your friend %s has entered a %s game named \"%s\".",myusername,game_title,gamename);
240     else
241     sprintf(msg,"Your friend %s has entered a %s game",myusername,game_title);
242     }
243     if (event == watch_event_leavegame)sprintf(msg,"Your friend %s has left a %s game.",myusername,game_title);
244     if (event == watch_event_login) sprintf(msg,"Your friend %s has entered the PvPGN Realm.",myusername);
245     if (event == watch_event_logout) sprintf(msg,"Your friend %s has left the PvPGN Realm",myusername);
246     LIST_TRAVERSE(flist,curr)
247     {
248     if (!(fr = elem_get_data(curr)))
249     {
250     eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
251     continue;
252     }
253    
254     dest_c = connlist_find_connection_by_account(fr->friendacc);
255    
256     if (dest_c==NULL) /* If friend is offline, go on to next */
257     continue;
258     else {
259     if (!my_c) my_c = dest_c;
260     cnt++; /* keep track of successful whispers */
261     if(friend_get_mutual(fr))
262     message_send_text(dest_c,message_type_whisper,my_c,msg);
263     }
264     }
265     }
266     if (cnt) eventlog(eventlog_level_info,__FUNCTION__,"notified %d friends about %s",cnt,myusername);
267    
268     /* watchlist handling */
269    
270     if (event == watch_event_joingame)
271     {
272     if (gamename)
273     sprintf(msg,"Watched user %s has entered a %s game named \"%s\".",myusername,game_title,gamename);
274     else
275     sprintf(msg,"Watched user %s has entered a %s game",myusername,game_title);
276     }
277    
278     if (event == watch_event_leavegame)sprintf(msg,"Watched user %s has left a %s game.",myusername,game_title);
279     if (event == watch_event_login) sprintf(msg,"Watched user %s has entered the PvPGN Realm.",myusername);
280     if (event == watch_event_logout) sprintf(msg,"Watched user %s has left the PvPGN Realm",myusername);
281    
282     LIST_TRAVERSE_CONST(watchlist_head,curr)
283     {
284     pair = elem_get_data(curr);
285     if (!pair) /* should not happen */
286     {
287     eventlog(eventlog_level_error,__FUNCTION__,"watchlist contains NULL item");
288     return -1;
289     }
290     if (pair->owner && (!pair->who || pair->who==account) && ((!pair->clienttag) || (clienttag == pair->clienttag)) && (pair->what&event))
291     {
292     if (!my_c) my_c = pair->owner;
293     message_send_text(pair->owner,message_type_whisper,my_c,msg);
294     }
295     }
296    
297     return 0;
298     }
299    
300     extern int watchlist_notify_event(t_account * who, char const * gamename, t_clienttag clienttag, t_watch_event event)
301     {
302    
303     switch (event)
304     {
305     case watch_event_login:
306     case watch_event_logout:
307     case watch_event_joingame:
308     case watch_event_leavegame:
309     handle_event_whisper(who,gamename,clienttag,event);
310     break;
311     default:
312     eventlog(eventlog_level_error,__FUNCTION__,"got unknown event %u",(unsigned int)event);
313     return -1;
314     }
315     return 0;
316     }
317    
318    
319     extern int watchlist_create(void)
320     {
321     watchlist_head = list_create();
322     return 0;
323     }
324    
325    
326     extern int watchlist_destroy(void)
327     {
328     t_elem * curr;
329     t_watch_pair * pair;
330    
331     if (watchlist_head)
332     {
333     LIST_TRAVERSE(watchlist_head,curr)
334     {
335     pair = elem_get_data(curr);
336     if (!pair) /* should not happen */
337     {
338     eventlog(eventlog_level_error,__FUNCTION__,"watchlist contains NULL item");
339     continue;
340     }
341    
342     if (list_remove_elem(watchlist_head,&curr)<0)
343     eventlog(eventlog_level_error,__FUNCTION__,"could not remove item from list");
344     xfree(pair);
345     }
346    
347     if (list_destroy(watchlist_head)<0)
348     return -1;
349     watchlist_head = NULL;
350     }
351    
352     return 0;
353     }
354    

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1