#!/usr/bin/perl ############################# ## Note that this is currently only intended for the Windows version of the client. # Sanitization path # i.e C:/Documents and Settings//Application Data/X-Chat 2/ScrollBack/ # or C:/Documents and Settings//Application Data/X-Chat 2/xchatlogs/ # or C:/Documents and Settings//Application Data/X-Chat 2/ (to cover both) my $sanitize_path="--NOT-CONFIGURED--"; Xchat::register("Sanitize", 0.1, "Quick relief for those hard-to-remove stains late at night. :)"); Xchat::hook_command("sanitize", \&cmd_sanitize); my $verbose=0; sub cmd_sanitize { if ($sanitize_path eq "--NOT-CONFIGURED--" || !-d $sanitize_path) { Xchat::print("\n"); Xchat::print("You have not properly configured the $sanitize_path in the xcsanitize.pl script. Get to it.\n"); Xchat::print("\n"); return Xchat::EAT_XCHAT; } my $arg = $_[0][1]; $verbose = length($_[0][2]); if (length($arg)>0) { # validate arg if (validate($arg)) { process_files($sanitize_path, $arg); } else { Xchat::print("\n"); Xchat::print("Invalid search string. Try to stay within the allowed OS file naming conventions.\n"); Xchat::print("\n"); return Xchat::EAT_XCHAT; } } else { # display help Xchat::print("\n"); Xchat::print("Sanitize - Quick relief for those hard-to-remove stains late at night. :)\n"); Xchat::print("Usage: /sanitize [verbose]\n"); Xchat::print(" The script will search the configured path and find all .txt and .log\n"); Xchat::print(" files that have the given string in their name and truncate them to \n"); Xchat::print(" zero bytes.\n"); Xchat::print("\n"); Xchat::print(" Optionally you can also specify the verbose parameter to see what the\n"); Xchat::print(" script is doing.\n"); Xchat::print("\n"); } return Xchat::EAT_XCHAT; } sub validate { my $arg = shift; my @reserved = {'<', '>', ':', '"', '/', '\\', '|', '?', '*'}; if (length($arg)>0) { for (@reserved) { if (index($arg, $_)!=-1) { return false; } } return true; } return false; } sub process_files { my $path = shift; my $arg = shift; if ($verbose) { Xchat::print("Looking in: '$path'\n"); } opendir(DIR, $path) or die "Unable to open $path: $!"; my @files = grep { !/^\.{1,2}$/ } readdir(DIR); closedir(DIR); for (@files) { my $item=$path."\\".$_; if (-d $item) { process_files($item, $arg); } else { my $ext = ($_ =~ m/([^.]+)$/)[0]; if (($ext eq "txt" || $ext eq "log") && index($_, $arg)>=0) { if ($verbose) { Xchat::print("sanitized file: '$path\\$_'\n"); } sanitize($item); } } } } sub sanitize { my $path = shift; if (-e $path) { open(FILE, ">$path") or die "Unable to sanitize $path: $!"; close(FILE); } }