1 18 package net.sf.drftpd.event.irc; 19 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.StringTokenizer ; 24 25 import net.sf.drftpd.Bytes; 26 import net.sf.drftpd.event.listeners.Trial; 27 import net.sf.drftpd.master.ConnectionManager; 28 import net.sf.drftpd.master.command.plugins.TransferStatistics; 29 import net.sf.drftpd.master.config.FtpConfig; 30 import net.sf.drftpd.master.config.Permission; 31 import net.sf.drftpd.master.usermanager.User; 32 import net.sf.drftpd.master.usermanager.UserFileException; 33 import net.sf.drftpd.util.ReplacerUtils; 34 import net.sf.drftpd.util.UserComparator; 35 36 import org.drftpd.plugins.SiteBot; 37 import org.tanesha.replacer.FormatterException; 38 import org.tanesha.replacer.ReplacerEnvironment; 39 import org.tanesha.replacer.SimplePrintf; 40 41 import f00f.net.irc.martyr.GenericCommandAutoService; 42 import f00f.net.irc.martyr.InCommand; 43 import f00f.net.irc.martyr.commands.MessageCommand; 44 45 49 public class Stats 50 extends GenericCommandAutoService 51 implements IRCPluginInterface { 52 53 private ConnectionManager _cm; 54 public Stats(SiteBot ircListener) { 55 super(ircListener.getIRCConnection()); 56 _cm = ircListener.getConnectionManager(); 57 } 58 59 public String getCommands() { 60 return "!{al,wk,month,day}{up,dn}"; 61 } 62 63 protected void updateCommand(InCommand command) { 64 if (!(command instanceof MessageCommand)) 65 return; 66 67 MessageCommand msgc = (MessageCommand) command; 68 StringTokenizer st = new StringTokenizer (msgc.getMessage()); 69 if (!st.hasMoreTokens()) { 70 return; 71 } 72 String msg = st.nextToken(); 73 String type = null; 74 if (msg.startsWith("!alup")) { 75 type = "ALUP"; 76 } else if (msg.startsWith("!aldn")) { 77 type = "ALDN"; 78 } else if (msg.startsWith("!wkup")) { 79 type = "WKUP"; 80 } else if (msg.startsWith("!wkdn")) { 81 type = "WKDN"; 82 } else if (msg.startsWith("!daydn")) { 83 type = "DAYDN"; 84 } else if (msg.startsWith("!dayup")) { 85 type = "DAYUP"; 86 } else if (msg.startsWith("!monthdn")) { 87 type = "MONTHDN"; 88 } else if (msg.startsWith("!monthup")) { 89 type = "MONTHUP"; 90 } 91 92 if (type == null) 93 return; String destination = null; 95 if (msgc.isPrivateToUs(getConnection().getClientState())) { 96 } else 98 destination = msgc.getDest(); 99 100 List users = null; 101 try { 102 users = _cm.getUserManager().getAllUsers(); 103 } catch (UserFileException e) { 104 getConnection().sendCommand( 105 new MessageCommand(destination, "Error processing userfiles")); 106 return; 107 } 108 int number = fixNumberAndUserlist(msgc.getMessage(), users); 109 110 Collections.sort(users, new UserComparator(type)); 111 112 int i = 0; 113 for (Iterator iter = users.iterator(); iter.hasNext();) { 114 if (++i > number) 115 break; 116 User user = (User) iter.next(); 117 ReplacerEnvironment env = 118 new ReplacerEnvironment(SiteBot.GLOBAL_ENV); 119 env.add("pos", "" + i); 120 env.add("user", user.getUsername()); 121 env.add("tagline", user.getTagline()); 122 env.add( 123 "upbytesday", 124 Bytes.formatBytes(user.getUploadedBytesDay())); 125 env.add("upfilesday", "" + user.getUploadedFilesDay()); 126 env.add( 127 "uprateday", 128 TransferStatistics.getUpRate(user, Trial.PERIOD_DAILY)); 129 env.add( 130 "upbytesweek", 131 Bytes.formatBytes(user.getUploadedBytesWeek())); 132 env.add("upfilesweek", "" + user.getUploadedFilesWeek()); 133 env.add( 134 "uprateweek", 135 TransferStatistics.getDownRate(user, Trial.PERIOD_WEEKLY)); 136 env.add( 137 "upbytesmonth", 138 Bytes.formatBytes(user.getUploadedBytesMonth())); 139 env.add("upfilesmonth", "" + user.getUploadedFilesMonth()); 140 env.add( 141 "upratemonth", 142 TransferStatistics.getUpRate(user, Trial.PERIOD_MONTHLY)); 143 env.add("upbytes", Bytes.formatBytes(user.getUploadedBytes())); 144 env.add("upfiles", "" + user.getUploadedFiles()); 145 env.add( 146 "uprate", 147 TransferStatistics.getUpRate(user, Trial.PERIOD_ALL)); 148 149 env.add( 150 "dnbytesday", 151 Bytes.formatBytes(user.getDownloadedBytesDay())); 152 env.add("dnfilesday", "" + user.getDownloadedFilesDay()); 153 env.add( 154 "dnrateday", 155 TransferStatistics.getDownRate(user, Trial.PERIOD_DAILY)); 156 env.add( 157 "dnbytesweek", 158 Bytes.formatBytes(user.getDownloadedBytesWeek())); 159 env.add("dnfilesweek", "" + user.getDownloadedFilesWeek()); 160 env.add( 161 "dnrateweek", 162 TransferStatistics.getDownRate(user, Trial.PERIOD_WEEKLY)); 163 env.add( 164 "dnbytesmonth", 165 Bytes.formatBytes(user.getDownloadedBytesMonth())); 166 env.add("dnfilesmonth", "" + user.getDownloadedFilesMonth()); 167 env.add( 168 "dnratemonth", 169 TransferStatistics.getDownRate(user, Trial.PERIOD_MONTHLY)); 170 env.add("dnbytes", Bytes.formatBytes(user.getDownloadedBytes())); 171 env.add("dnfiles", "" + user.getDownloadedFiles()); 172 env.add( 173 "dnrate", 174 TransferStatistics.getDownRate(user, Trial.PERIOD_ALL)); 175 type = type.toLowerCase(); 176 try { 177 getConnection().sendCommand( 178 new MessageCommand( 179 destination, 180 SimplePrintf.jprintf( 181 ReplacerUtils.jprintf( 182 "transferstatistics" + type, 183 env, 184 Stats.class.getName()), 185 env))); 186 } catch (FormatterException e) { 187 getConnection().sendCommand( 188 new MessageCommand( 189 destination, 190 "FormatterException for transferstatistics" + type)); 191 break; 192 } 193 } 194 } 195 196 public static int fixNumberAndUserlist(String params, List userList) { 197 int number = 10; 198 com.Ostermiller.util.StringTokenizer st = new com.Ostermiller.util.StringTokenizer(params); 199 st.nextToken(); if (!st.hasMoreTokens()) { 201 return 10; 202 } 203 if (st.hasMoreTokens()) { 204 try { 206 number = Integer.parseInt(st.peek()); 207 st.nextToken(); 208 } catch (NumberFormatException ex) { 209 } 210 211 while(st.hasMoreTokens()) { 212 Permission perm = new Permission(FtpConfig.makeUsers(st)); 213 for (Iterator iter = userList.iterator(); iter.hasNext();) { 214 User user = (User) iter.next(); 215 if (!perm.check(user)) { 216 iter.remove(); 217 } 218 } 219 } 220 } 221 return number; 222 } 223 } | Popular Tags |