KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > event > listeners > TrialSiteBot


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD 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 DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.event.listeners;
19
20 import java.util.Calendar JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import net.sf.drftpd.Bytes;
24 import net.sf.drftpd.event.listeners.Trial.Limit;
25 import net.sf.drftpd.master.BaseFtpConnection;
26 import net.sf.drftpd.master.usermanager.NoSuchUserException;
27 import net.sf.drftpd.master.usermanager.User;
28 import net.sf.drftpd.master.usermanager.UserFileException;
29
30 import org.apache.log4j.Logger;
31 import org.drftpd.plugins.SiteBot;
32 import org.tanesha.replacer.ReplacerEnvironment;
33
34 import f00f.net.irc.martyr.GenericCommandAutoService;
35 import f00f.net.irc.martyr.InCommand;
36 import f00f.net.irc.martyr.commands.MessageCommand;
37
38 public class TrialSiteBot extends GenericCommandAutoService {
39     private static final Logger logger = Logger.getLogger(TrialSiteBot.class);
40     private final Trial _trial;
41
42     private SiteBot _irc;
43
44     protected TrialSiteBot(Trial trial, SiteBot irc) {
45         super(irc.getIRCConnection());
46         _trial = trial;
47         _irc = irc;
48     }
49
50     protected String JavaDoc jprintf(
51         String JavaDoc key,
52         User user,
53         Limit limit,
54         long bytesleft,
55         boolean unique) {
56
57         ReplacerEnvironment env = new ReplacerEnvironment(SiteBot.GLOBAL_ENV);
58         env.add("user", user.getUsername());
59         if (limit != null) {
60             env.add("period", Trial.getPeriodName(limit.getPeriod()));
61             env.add("name", limit.getName());
62             if (unique) {
63                 env.add(
64                     "bonusexpires",
65                     Trial
66                         .getCalendarForEndOfBonus(user, limit.getPeriod())
67                         .getTime());
68                 env.add(
69                     "uniqueexpires",
70                     Trial
71                         .getCalendarForEndOfFirstPeriod(user, limit.getPeriod())
72                         .getTime());
73             }
74             env.add(
75                 "expires",
76                 Trial.getCalendarForEndOfPeriod(limit.getPeriod()).getTime());
77         }
78         env.add("bytesleft", Bytes.formatBytes(bytesleft));
79         env.add("bytespassed", Bytes.formatBytes(-bytesleft));
80
81         return BaseFtpConnection.jprintf(
82             TrialSiteBot.class.getName(),
83             key,
84             env,
85             user);
86     }
87     protected void updateCommand(InCommand command) {
88         if (!(command instanceof MessageCommand))
89             return;
90         MessageCommand msgc = (MessageCommand) command;
91         if (msgc.isPrivateToUs(_irc.getIRCConnection().getClientState()))
92             return;
93         String JavaDoc msg = msgc.getMessage();
94         if (!msg.startsWith("!passed "))
95             return;
96
97         String JavaDoc username = msg.substring("!passed ".length());
98         User user;
99         try {
100             user =
101                 _trial.getConnectionManager().getUserManager().getUserByName(
102                     username);
103         } catch (NoSuchUserException e) {
104             _irc.sayChannel(msgc.getDest(), "[passed] No such user: " + username);
105             logger.info("", e);
106             return;
107         } catch (UserFileException e) {
108             logger.warn("", e);
109             return;
110         }
111         int i = 0;
112         for (Iterator JavaDoc iter = _trial.getLimits().iterator(); iter.hasNext();) {
113             Limit limit = (Limit) iter.next();
114             if (limit.getPerm().check(user)) {
115                 i++;
116
117                 Calendar JavaDoc endofbonus =
118                     Trial.getCalendarForEndOfBonus(user, limit.getPeriod());
119                 if (System.currentTimeMillis()
120                     <= endofbonus.getTimeInMillis()) {
121                     //in bonus or unique period
122
long bytesleft = limit.getBytes() - user.getUploadedBytes();
123
124                     if (bytesleft <= 0) {
125                         //in bonus or passed
126
_irc.sayChannel(
127                             msgc.getDest(),
128                             jprintf(
129                                 "passedunique",
130                                 user,
131                                 limit,
132                                 bytesleft,
133                                 true));
134                     } else {
135                         //in unique period
136
_irc.sayChannel(
137                             msgc.getDest(),
138                             jprintf(
139                                 "trialunique",
140                                 user,
141                                 limit,
142                                 bytesleft,
143                                 true));
144                     }
145                 } else {
146                     //plain trial
147
long bytesleft =
148                         limit.getBytes()
149                             - Trial.getUploadedBytesForPeriod(
150                                 user,
151                                 limit.getPeriod());
152
153                     if (bytesleft <= 0) {
154                         _irc.sayChannel(
155                             msgc.getDest(),
156                             jprintf("passed", user, limit, bytesleft, false));
157                     } else {
158                         _irc.sayChannel(
159                             msgc.getDest(),
160                             jprintf("trial", user, limit, bytesleft, false));
161                     }
162
163                 }
164             }
165         }
166         if (i == 0) {
167             _irc.sayChannel(msgc.getDest(), jprintf("exempt", user, null, 0, false));
168         }
169     }
170 }
Popular Tags