Debian Codegraph @

src/bnetd/watch.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1999,2000 Ross Combs ([email protected])
3 * Copyright (C) 2005 Dizzy
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include "common/setup_before.h"
20 #include "watch.h"
21 #include "common/field_sizes.h"
22 #include "common/eventlog.h"
23 #include "common/list.h"
24 #include "message.h"
25 #include "friends.h"
26 #include "prefs.h"
27 #include "common/setup_after.h"
28
29
30 namespace pvpgn
31 {
32
33 namespace bnetd
34 {
35
36 scoped_ptr<WatchComponent> watchlist;
37
38 Watch::Watch(t_connection* owner_, t_account* who_, unsigned what_, t_clienttag ctag_)
39 :owner(owner_), who(who_), what(what_), ctag(ctag_)
40 {
41 }
42
43 Watch::~Watch() throw()
44 {
45 }
46
47 t_connection*
48 Watch::getOwner() const
49 {
50 return owner;
51 }
52
53 t_account*
54 Watch::getAccount() const
55 {
56 return who;
57 }
58
59 unsigned
60 Watch::getEventMask() const
61 {
62 return what;
63 }
64
65 t_clienttag
66 Watch::getClientTag() const
67 {
68 return ctag;
69 }
70
71 void
72 Watch::setEventMask(unsigned what_)
73 {
74 what = what_;
75 }
76
77 /* who == NULL means anybody */
78 void
79 WatchComponent::add(t_connection * owner, t_account * who, t_clienttag clienttag, unsigned events)
80 {
81 for (WatchList::iterator it(wlist.begin()); it != wlist.end(); ++it)
82 {
83 if (owner == it->getOwner() && who == it->getAccount() && clienttag == it->getClientTag())
84 {
85 it->setEventMask(it->getEventMask() | events);
86 return;
87 }
88 }
89
90 wlist.push_back(Watch(owner, who, events, clienttag));
91 }
92
93
94 /* who == NULL means anybody */
95 int
96 WatchComponent::del(t_connection * owner, t_account * who, t_clienttag clienttag, unsigned events)
97 {
98 for (WatchList::iterator it(wlist.begin()); it != wlist.end(); ++it)
99 {
100 if (owner == it->getOwner() && who == it->getAccount() && (!clienttag || clienttag == it->getClientTag()))
101 {
102 unsigned evmask = it->getEventMask() & (~events);
103 if (!evmask) wlist.erase(it);
104 else it->setEventMask(evmask);
105 return 0;
106 }
107 }
108
109 return -1;
110 }
111
112
113 /* this differs from del_events because it doesn't return an error if nothing was found */
114 void
115 WatchComponent::del(t_connection * owner)
116 {
117 for (WatchList::iterator it(wlist.begin()); it != wlist.end();)
118 {
119 if (owner == it->getOwner())
120 it = wlist.erase(it);
121 else ++it;
122 }
123 }
124
125 int
126 WatchComponent::dispatch_whisper(t_account *account, char const *gamename, t_clienttag clienttag, Watch::EventType event) const
127 {
128 t_elem const * curr;
129 char msg[512];
130 int cnt = 0;
131 char const *myusername;
132 t_list * flist;
133 t_connection * dest_c, *my_c;
134 t_friend * fr;
135 char const * game_title;
136
137 if (!(myusername = account_get_name(account)))
138 {
139 ERROR0("got NULL account name");
140 return -1;
141 }
142
143 my_c = account_get_conn(account);
144
145 game_title = clienttag_get_title(clienttag);
146
147 /* mutual friends handling */
148 flist = account_get_friends(account);
149 if (flist)
150 {
151 switch (event)
152 {
153 case Watch::ET_joingame:
154 if (gamename)
155 std::snprintf(msg, sizeof(msg), "Your friend %s has entered a %s game named \"%s\".", myusername, game_title, gamename);
156 else
157 std::snprintf(msg, sizeof(msg), "Your friend %s has entered a %s game", myusername, game_title);
158 break;
159 case Watch::ET_leavegame:
160 std::snprintf(msg, sizeof(msg), "Your friend %s has left a %s game.", myusername, game_title);
161 break;
162 case Watch::ET_login:
163 std::snprintf(msg, sizeof(msg), "Your friend %s has entered %s.", myusername, prefs_get_servername());
164 break;
165 case Watch::ET_logout:
166 std::snprintf(msg, sizeof(msg), "Your friend %s has left %s.", myusername, prefs_get_servername());
167 break;
168 }
169 LIST_TRAVERSE(flist, curr)
170 {
171 if (!(fr = (t_friend*)elem_get_data(curr)))
172 {
173 ERROR0("found NULL entry in list");
174 continue;
175 }
176
177 dest_c = connlist_find_connection_by_account(fr->friendacc);
178
179 if (dest_c == NULL) /* If friend is offline, go on to next */
180 continue;
181 else {
182 cnt++; /* keep track of successful whispers */
183 if (friend_get_mutual(fr))
184 message_send_text(dest_c, message_type_whisper, my_c, msg);
185 }
186 }
187 }
188
189 if (cnt) DEBUG2("notified {} friends about {}", cnt, myusername);
190
191 /* watchlist handling */
192 switch (event)
193 {
194 case Watch::ET_joingame:
195 if (gamename)
196 std::snprintf(msg, sizeof(msg), "Watched user %s has entered a %s game named \"%s\".", myusername, game_title, gamename);
197 else
198 std::snprintf(msg, sizeof(msg), "Watched user %s has entered a %s game", myusername, game_title);
199 break;
200 case Watch::ET_leavegame:
201 std::snprintf(msg, sizeof(msg), "Watched user %s has left a %s game.", myusername, game_title);
202 break;
203 case Watch::ET_login:
204 std::snprintf(msg, sizeof(msg), "Watched user %s has entered %s.", myusername, prefs_get_servername());
205 break;
206 case Watch::ET_logout:
207 std::snprintf(msg, sizeof(msg), "Watched user %s has left %s", myusername, prefs_get_servername());
208 break;
209 }
210
211 for (WatchList::const_iterator it(wlist.begin()); it != wlist.end(); ++it)
212 {
213 if (it->getOwner() && (!it->getAccount() || it->getAccount() == account) && (!it->getClientTag() || (clienttag == it->getClientTag())) && (it->getEventMask() & event))
214 {
215 message_send_text(it->getOwner(), message_type_whisper, my_c, msg);
216 }
217 }
218
219 return 0;
220 }
221
222 int
223 WatchComponent::dispatch(t_account * who, char const * gamename, t_clienttag clienttag, Watch::EventType event) const
224 {
225 switch (event)
226 {
227 case Watch::ET_login:
228 case Watch::ET_logout:
229 case Watch::ET_joingame:
230 case Watch::ET_leavegame:
231 return dispatch_whisper(who, gamename, clienttag, event);
232 default:
233 eventlog(eventlog_level_error, __FUNCTION__, "got unknown event {}", (unsigned int)event);
234 return -1;
235 }
236 return 0;
237 }
238
239
240 WatchComponent::WatchComponent()
241 :wlist()
242 {
243 }
244
245
246 WatchComponent::~WatchComponent() throw ()
247 {
248 }
249
250 }
251
252 }