1 18 package net.sf.drftpd.event.listeners; 19 20 import java.io.FileNotFoundException ; 22 import java.io.FileWriter ; 23 import java.io.IOException ; 24 import java.io.PrintWriter ; 25 import java.net.UnknownHostException ; 26 import java.text.DateFormat ; 27 import java.text.SimpleDateFormat ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Collections ; 31 import java.util.Comparator ; 32 import java.util.Date ; 33 import java.util.Iterator ; 34 import java.util.Locale ; 35 37 import net.sf.drftpd.FatalException; 38 import net.sf.drftpd.NoAvailableSlaveException; 39 import net.sf.drftpd.Nukee; 40 import net.sf.drftpd.SFVFile; 41 import net.sf.drftpd.SlaveUnavailableException; 42 import net.sf.drftpd.SFVFile.SFVStatus; 43 import net.sf.drftpd.event.DirectoryFtpEvent; 44 import net.sf.drftpd.event.Event; 45 import net.sf.drftpd.event.FtpListener; 46 import net.sf.drftpd.event.InviteEvent; 47 import net.sf.drftpd.event.MessageEvent; 48 import net.sf.drftpd.event.NukeEvent; 49 import net.sf.drftpd.event.SlaveEvent; 50 import net.sf.drftpd.event.TransferEvent; 51 import net.sf.drftpd.master.ConnectionManager; 52 import net.sf.drftpd.master.GroupPosition; 53 import net.sf.drftpd.master.UploaderPosition; 54 import net.sf.drftpd.master.command.plugins.Nuke; 55 import net.sf.drftpd.master.usermanager.NoSuchUserException; 58 import net.sf.drftpd.master.usermanager.User; 59 import net.sf.drftpd.master.usermanager.UserFileException; 60 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 61 import net.sf.drftpd.slave.SlaveStatus; 62 63 import org.apache.log4j.Level; 64 import org.apache.log4j.Logger; 65 import org.tanesha.replacer.FormatterException; 66 67 71 public class GlftpdLog implements FtpListener { 72 PrintWriter out; 73 74 private ConnectionManager _cm; 75 76 private static Logger logger = Logger.getLogger(GlftpdLog.class); 77 static { 78 logger.setLevel(Level.ALL); 79 } 80 81 public GlftpdLog() throws UnknownHostException , IOException { 82 reload(); 83 logger.info("Glftpd plugin loaded successfully"); 84 out = new PrintWriter (new FileWriter ("logs/glftpd.log")); 85 } 86 87 public void init(ConnectionManager mgr) { 88 _cm = mgr; 89 } 90 91 public void actionPerformed(Event event) { 92 try { 93 if (event instanceof DirectoryFtpEvent) { 94 actionPerformedDirectory((DirectoryFtpEvent) event); 95 } else if (event instanceof NukeEvent) { 96 actionPerformedNuke((NukeEvent) event); 97 } else if (event instanceof SlaveEvent) { 98 actionPerformedSlave((SlaveEvent) event); 99 } else if (event instanceof InviteEvent) { 100 actionPerformedInvite((InviteEvent) event); 101 } else if (event.getCommand().equals("SHUTDOWN")) { 102 MessageEvent mevent = (MessageEvent) event; 103 print( 104 "SHUTDOWN: \"" 105 + mevent.getMessage() 106 + "\""); 107 } 108 } catch (FormatterException ex) { 109 } 110 } 111 112 private void actionPerformedDirectory(DirectoryFtpEvent direvent) 113 throws FormatterException { 114 115 if ("MKD".equals(direvent.getCommand())) { 116 sayDirectorySection(direvent, "NEWDIR", direvent.getDirectory()); 117 } else if ("REQUEST".equals(direvent.getCommand())) { 118 sayDirectorySection(direvent, "REQUEST", direvent.getDirectory()); 119 } else if ("REQFILLED".equals(direvent.getCommand())) { 120 sayDirectorySection(direvent, "REQFILLED", direvent.getDirectory()); 121 } else if ("RMD".equals(direvent.getCommand())) { 122 sayDirectorySection(direvent, "DELDIR", direvent.getDirectory()); 123 } else if ("WIPE".equals(direvent.getCommand())) { 124 if (direvent.getDirectory().isDirectory()) { 125 sayDirectorySection(direvent, "WIPE", direvent.getDirectory()); 126 } 127 138 } else if (direvent.getCommand().equals("STOR")) { 139 actionPerformedDirectorySTOR((TransferEvent)direvent); 140 } else { 141 } 143 } 144 145 private void sayDirectorySection(DirectoryFtpEvent direvent, String string, LinkedRemoteFileInterface dir) 146 throws FormatterException { 147 148 print( 151 "" 152 + string 153 + ": \"" 154 + dir.getPath() 155 + "\" \"" 156 + direvent.getUser().getUsername() 157 + "\" \"" 158 + direvent.getUser().getGroupName() 159 + "\" \"" 160 + direvent.getUser().getTagline() 161 + "\""); 162 } 163 164 private void actionPerformedDirectorySTOR(TransferEvent direvent) 165 throws FormatterException { 166 167 if(!direvent.isComplete()) return; 168 LinkedRemoteFileInterface dir; 169 try { 170 dir = direvent.getDirectory().getParentFile(); 171 } catch (FileNotFoundException e) { 172 throw new FatalException(e); 173 } 174 SFVFile sfvfile; 175 try { 176 sfvfile = dir.lookupSFVFile(); 177 } catch (FileNotFoundException ex) { 179 return; 181 } catch (NoAvailableSlaveException e) { 182 return; 184 } catch (IOException e) { 185 return; 187 } 188 189 long starttime = Long.MAX_VALUE; 190 for (Iterator iter = sfvfile.getFiles().iterator(); iter.hasNext();) { 191 LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next(); 192 if (file.lastModified() < starttime) 193 starttime = file.lastModified(); 194 } 195 196 if (!sfvfile.hasFile(direvent.getDirectory().getName())) 197 return; 198 199 int halfway = (int) Math.floor((double) sfvfile.size() / 2); 200 202 String username = direvent.getUser().getUsername(); 204 SFVStatus sfvstatus = sfvfile.getStatus(); 205 if (sfvfile.size() - sfvstatus.getMissing() != 1) { 206 for (Iterator iter = sfvfile.getFiles().iterator(); 207 iter.hasNext(); 208 ) { 209 LinkedRemoteFileInterface sfvFileEntry = (LinkedRemoteFileInterface) iter.next(); 210 if (sfvFileEntry == direvent.getDirectory()) 211 continue; 212 if (sfvFileEntry.getUsername().equals(username)) 213 break; 214 if (!iter.hasNext()) { 215 216 print( 219 "RACE: \"" 220 + dir.getPath() 221 + "\" \"" 222 + direvent.getUser().getUsername() 223 + "\" \"" 224 + direvent.getUser().getGroupName() 225 + "\" \"" 226 + sfvfile.getXferspeed() 227 + "\" \"" 228 + Integer.toString(sfvstatus.getMissing()) 229 + "\" \"" 230 + Long.toString((direvent.getTime() - starttime) / 1000) 231 + "\""); 232 } 233 } 234 } 235 236 if (sfvstatus.isFinished()) { 238 Collection racers = UserSort(sfvfile.getFiles(), "bytes", "high"); 239 Collection groups = topFileGroup(sfvfile.getFiles()); 240 Collection fast = UserSort(sfvfile.getFiles(), "xfearspeed", "high"); 241 Collection slow = UserSort(sfvfile.getFiles(), "xfearspeed", "low"); 242 243 UploaderPosition fastestuser = (UploaderPosition) fast.iterator().next(); 244 UploaderPosition slowestuser = (UploaderPosition) slow.iterator().next(); 245 246 User fastuser; 247 User slowuser; 248 try { 249 fastuser = _cm.getUserManager().getUserByName(fastestuser.getUsername()); 250 slowuser = _cm.getUserManager().getUserByName(slowestuser.getUsername()); 251 } catch (NoSuchUserException e2) { 252 return; 253 } catch (UserFileException e2) { 254 logger.log(Level.FATAL, "Error reading userfile", e2); 255 return; 256 } 257 258 print( 263 "COMPLETE: \"" 264 + dir.getPath() 265 + "\" \"" 266 + sfvfile.getTotalBytes() 267 + "\" \"" 268 + Integer.toString(sfvfile.size()) 269 + "\" \"" 270 + sfvfile.getXferspeed() 271 + "\" \"" 272 + Long.toString((direvent.getTime() - starttime) / 1000) 273 + "\" \"" 274 + Integer.toString(racers.size()) 275 + "\" \"" 276 + Integer.toString(groups.size()) 277 + "\" \"" 278 + fastuser.getUsername() 279 + "\" \"" 280 + fastuser.getGroupName() 281 + "\" \"" 282 + fastestuser.getXferspeed() 283 + "\" \"" 284 + slowuser.getUsername() 285 + "\" \"" 286 + slowuser.getGroupName() 287 + "\" \"" 288 + slowestuser.getXferspeed() 289 + "\""); 290 291 print( 292 "STATS: \"" 293 + dir.getPath() 294 + "\" \"UserTop:\""); 295 296 int position = 1; 297 for (Iterator iter = racers.iterator(); iter.hasNext();) { 298 UploaderPosition stat = (UploaderPosition) iter.next(); 299 300 User raceuser; 301 try { 302 raceuser = _cm.getUserManager().getUserByName(stat.getUsername()); 303 } catch (NoSuchUserException e2) { 304 continue; 305 } catch (UserFileException e2) { 306 logger.log(Level.FATAL, "Error reading userfile", e2); 307 continue; 308 } 309 310 print( 313 "STATSUSER: \"" 314 + dir.getPath() 315 + "\" \"" 316 + new Integer (position++) 317 + "\" \"" 318 + raceuser.getUsername() 319 + "\" \"" 320 + raceuser.getGroupName() 321 + "\" \"" 322 + stat.getBytes() 323 + "\" \"" 324 + Integer.toString(stat.getFiles()) 325 + "\" \"" 326 + Integer.toString(stat.getFiles() * 100 / sfvfile.size()) 327 + "\" \"" 328 + stat.getXferspeed() 329 + "\""); 330 } 331 332 print( 333 "STATS: \"" 334 + dir.getPath() 335 + "\" \"GroupTop:\""); 336 337 position = 1; 338 for (Iterator iter = groups.iterator(); iter.hasNext();) { 339 GroupPosition stat = (GroupPosition) iter.next(); 340 341 print( 344 "STATSGROUP: \"" 345 + dir.getPath() 346 + "\" \"" 347 + new Integer (position++) 348 + "\" \"" 349 + stat.getGroupname() 350 + "\" \"" 351 + stat.getBytes() 352 + "\" \"" 353 + Integer.toString(stat.getFiles()) 354 + "\" \"" 355 + Integer.toString(stat.getFiles() * 100 / sfvfile.size()) 356 + "\" \"" 357 + stat.getXferspeed() 358 + "\""); 359 } 360 } else if (sfvfile.size() >= 4 && sfvstatus.getMissing() == halfway) { 362 Collection uploaders = UserSort(sfvfile.getFiles(), "bytes", "high"); 363 UploaderPosition stat = 364 (UploaderPosition) uploaders.iterator().next(); 365 366 User leaduser; 367 try { 368 leaduser = 369 _cm.getUserManager().getUserByName(stat.getUsername()); 370 } catch (NoSuchUserException e3) { 371 return; 372 } catch (UserFileException e3) { 373 logger.log(Level.FATAL, "Error reading userfile", e3); 374 return; 375 } 376 377 print( 380 "HALFWAY: \"" 381 + dir.getPath() 382 + "\" \"" 383 + leaduser.getUsername() 384 + "\" \"" 385 + leaduser.getGroupName() 386 + "\" \"" 387 + stat.getBytes() 388 + "\" \"" 389 + Integer.toString(stat.getFiles()) 390 + "\" \"" 391 + Integer.toString(stat.getFiles() * 100 / sfvfile.size()) 392 + "\" \"" 393 + stat.getXferspeed() 394 + "\" \"" 395 + Integer.toString(sfvstatus.getMissing()) 396 + "\""); 397 } 398 } 399 400 private void actionPerformedSlave(SlaveEvent event) 401 throws FormatterException { 402 SlaveEvent sevent = (SlaveEvent) event; 403 404 if (event.getCommand().equals("ADDSLAVE")) { 405 SlaveStatus status; 406 try { 407 status = sevent.getRSlave().getStatus(); 408 } catch (SlaveUnavailableException e) { 409 return; 410 } 411 412 print( 413 "SLAVEONLINE: \"" 414 + sevent.getRSlave().getName() 415 + "\" \"" 416 + sevent.getMessage() 417 + "\" \"" 418 + status.getDiskSpaceCapacity() 419 + "\" \"" 420 + status.getDiskSpaceAvailable() 421 + "\""); 422 423 } else if (event.getCommand().equals("DELSLAVE")) { 424 print( 425 "SLAVEOFFLINE: \"" 426 + sevent.getRSlave().getName() 427 + "\""); 428 } 429 } 430 431 private void actionPerformedInvite(InviteEvent event) { 432 String user = event.getUser(); 433 print( 434 "INVITE: \"" 435 + user 436 + "\""); 437 } 438 439 private void actionPerformedNuke(NukeEvent event) 440 throws FormatterException { 441 String cmd = event.getCommand(); 442 443 if (cmd.equals("NUKE")) { 444 445 print( 446 "NUKE: \"" 447 + event.getPath() 448 + "\" \"" 449 + event.getUser().getUsername() 450 + "\" \"" 451 + event.getUser().getGroupName() 452 + "\" \"" 453 + String.valueOf(event.getMultiplier()) 454 + " " 455 + event.getSize() 456 + "\" \"" 457 + event.getReason() 458 + "\""); 459 460 int position = 1; 461 long nobodyAmount = 0; 462 for (Iterator iter = event.getNukees2().iterator(); 463 iter.hasNext();) { 464 Nukee stat = (Nukee) iter.next(); 465 466 User raceuser; 467 try { 468 raceuser = 469 _cm.getUserManager().getUserByName(stat.getUsername()); 470 } catch (NoSuchUserException e2) { 471 nobodyAmount += stat.getAmount(); 472 continue; 473 } catch (UserFileException e2) { 474 logger.log(Level.FATAL, "Error reading userfile", e2); 475 continue; 476 } 477 478 long nukedamount = 479 Nuke.calculateNukedAmount( 480 stat.getAmount(), 481 raceuser.getRatio(), 482 event.getMultiplier()); 483 484 print( 485 "NUKEE: \"" 486 + raceuser.getUsername() 487 + "\" \"" 488 + raceuser.getGroupName() 489 + "\" \"" 490 + position++ 491 + "\" \"" 492 + stat.getAmount() 493 + " " 494 + nukedamount 495 + "\""); 496 497 } 498 if (nobodyAmount != 0) { 499 500 print( 501 "NUKEE: \"" 502 + "nobody" 503 + "\" \"" 504 + "nogroup" 505 + "\" \"" 506 + "?" 507 + "\" \"" 508 + nobodyAmount 509 + " " 510 + nobodyAmount 511 + "\""); 512 513 } 514 } else if (cmd.equals("UNNUKE")) { 515 516 print( 517 "UNNUKE: \"" 518 + event.getPath() 519 + "\" \"" 520 + event.getUser().getUsername() 521 + "\" \"" 522 + event.getUser().getGroupName() 523 + "\" \"" 524 + String.valueOf(event.getMultiplier()) 525 + " " 526 + event.getSize() 527 + "\" \"" 528 + event.getReason() 529 + "\""); 530 } 531 } 532 533 public static Collection topFileGroup(Collection files) { 534 ArrayList ret = new ArrayList (); 535 for (Iterator iter = files.iterator(); iter.hasNext();) { 536 LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next(); 537 String groupname = file.getGroupname(); 538 539 GroupPosition stat = null; 540 for (Iterator iter2 = ret.iterator(); iter2.hasNext();) { 541 GroupPosition stat2 = (GroupPosition) iter2.next(); 542 if (stat2.getGroupname().equals(groupname)) { 543 stat = stat2; 544 break; 545 } 546 } 547 if (stat == null) { 548 stat = 549 new GroupPosition( 550 groupname, 551 file.length(), 552 1, 553 file.getXfertime()); 554 ret.add(stat); 555 } else { 556 stat.updateBytes(file.length()); 557 stat.updateFiles(1); 558 stat.updateXfertime(file.getXfertime()); 559 } 560 } 561 Collections.sort(ret); 562 return ret; 563 } 564 565 public static Collection UserSort(Collection files, String type, String sort) { 566 ArrayList ret = new ArrayList (); 567 for (Iterator iter = files.iterator(); iter.hasNext();) { 568 LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next(); 569 String username = file.getUsername(); 570 571 UploaderPosition stat = null; 572 for (Iterator iter2 = ret.iterator(); iter2.hasNext();) { 573 UploaderPosition stat2 = (UploaderPosition) iter2.next(); 574 if (stat2.getUsername().equals(username)) { 575 stat = stat2; 576 break; 577 } 578 } 579 if (stat == null) { 580 stat = 581 new UploaderPosition( 582 username, 583 file.length(), 584 1, 585 file.getXfertime()); 586 ret.add(stat); 587 } else { 588 stat.updateBytes(file.length()); 589 stat.updateFiles(1); 590 stat.updateXfertime(file.getXfertime()); 591 } 592 } 593 Collections.sort(ret, new UserComparator(type, sort)); 594 return ret; 595 } 596 597 DateFormat DATE_FMT = new SimpleDateFormat ("EEE MMM d HH:mm:ss yyyy ", Locale.ENGLISH); 598 599 public void print(String line) { 600 print(new Date (), line); 601 } 602 603 public void print(Date date, String line) { 604 out.println(DATE_FMT.format(date) + line); 605 out.flush(); 606 } 607 608 private void reload() { 609 } 616 617 public void unload() { 618 } 619 } 620 621 class UserComparator implements Comparator { 622 private String _type; 623 private String _sort; 624 625 public UserComparator(String type, String sort) { 626 _type = type; 627 _sort = sort; 628 } 629 630 static long getType(String type, UploaderPosition user) { 631 if (type.equals("bytes")) { 632 return user.getBytes(); 633 } else if (type.equals("xfearspeed")) { 634 return user.getXferspeed(); 635 } else if (type.equals("xfeartime")) { 636 return user.getXfertime(); 637 } 638 return 0; 639 } 640 641 public int compare(Object o1, Object o2) { 642 UploaderPosition u1 = (UploaderPosition) o1; 643 UploaderPosition u2 = (UploaderPosition) o2; 644 645 long thisVal = getType(_type, u1); 646 long anotherVal = getType(_type, u2); 647 if (_sort.equals("low")) { 648 return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1)); 649 } else { 650 return (thisVal > anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1)); 651 } 652 } 653 } | Popular Tags |