1 18 package net.sf.drftpd.event.listeners; 19 20 import java.util.Calendar ; 21 import java.util.Iterator ; 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 jprintf( 51 String 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 msg = msgc.getMessage(); 94 if (!msg.startsWith("!passed ")) 95 return; 96 97 String 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 iter = _trial.getLimits().iterator(); iter.hasNext();) { 113 Limit limit = (Limit) iter.next(); 114 if (limit.getPerm().check(user)) { 115 i++; 116 117 Calendar endofbonus = 118 Trial.getCalendarForEndOfBonus(user, limit.getPeriod()); 119 if (System.currentTimeMillis() 120 <= endofbonus.getTimeInMillis()) { 121 long bytesleft = limit.getBytes() - user.getUploadedBytes(); 123 124 if (bytesleft <= 0) { 125 _irc.sayChannel( 127 msgc.getDest(), 128 jprintf( 129 "passedunique", 130 user, 131 limit, 132 bytesleft, 133 true)); 134 } else { 135 _irc.sayChannel( 137 msgc.getDest(), 138 jprintf( 139 "trialunique", 140 user, 141 limit, 142 bytesleft, 143 true)); 144 } 145 } else { 146 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 |