diff options
| author | Julie Lala <jules@okfoc.us> | 2015-01-17 14:28:59 -0500 |
|---|---|---|
| committer | Julie Lala <jules@okfoc.us> | 2015-01-17 14:28:59 -0500 |
| commit | b063761323e93adb8b04dd26722dd2d6f2d6f3ef (patch) | |
| tree | 7f2b11fe0878ec1d935cd513c3e98cd4c15a503d /xdcc.pl | |
| parent | a25dd89c32448c593818acf078a43a551d68af38 (diff) | |
add stats and stuff
Diffstat (limited to 'xdcc.pl')
| -rwxr-xr-x | xdcc.pl | 448 |
1 files changed, 261 insertions, 187 deletions
@@ -4,46 +4,54 @@ use strict; use Irssi; use vars qw($VERSION %IRSSI); -$VERSION = "1.00"; +$VERSION = "1.01"; %IRSSI = ( - authors => 'Julie LaLa', - contact => 'jules@okfoc.us', - name => 'xdcc.pl', - description => 'This script sets up a little XDCC server.', - license => 'Jollo LNT license', - url => 'http://asdf.us/xdcc/xdcc.pl', - changed => 'Wed Jan 15 23:55:44 EST 2015', + authors => 'Julie LaLa', + contact => 'jules@okfoc.us', + name => 'xdcc.pl', + description => 'This script sets up a little XDCC server.', + license => 'Jollo LNT license', + url => 'http://asdf.us/xdcc/xdcc.pl', + changed => 'Wed Jan 15 23:55:44 EST 2015', ); my @files; my @queue; my $queue_max = 10; +my $bother_delay = 5000; my $irssidir = Irssi::get_irssi_dir(); my $sending = 0; +my $stats = { + files_sent => 0, + files => {}, + users => {}, +}; -my $help = <<EOF; +my $help_local = <<EOF; Usage: /XDCC [-add <filename> <description>] [-del <id>] [-list] [-help] -add: Add a file to our XDCC server. -del: Remove a file from the offerings. --list: Display the XDCC list. +-list: Display the XDCC list (default). +-reset: Reset the file list and the queue. +-stats: Statistics for this session -help: Display this help. Examples: -/xdcc -add jollo.png Jollo in his native habitat :) +/xdcc -add sally.gif Jollo in his native habitat :) /xdcc -add jollo.mp3 Distant cry of the Jollo, 5:43 am /xdcc -del 1 Note: The default parameter is -list. -People can request files from you using these commands: +People can request files from you using these commands: /ctcp <nickname> XDCC list -/ctcp <nickname> XDCC send 1 +/ctcp <nickname> XDCC get 1 /ctcp <nickname> XDCC queue Only one file will be sent at a time. @@ -52,232 +60,298 @@ Filenames should not contain spaces. EOF -Irssi::theme_register([ - 'xdcc_request', '%R>>%n %_XDCC:%_ Sending the file %_$1%_ to %_$0%_', - 'xdcc_print', '$[!-2]0 $[20]1 $2', +my $help_remote = <<EOF; +[%_XDCC%_] plugin $VERSION +%_/ctcp %nick XDCC list%_ - see a list of files +%_/ctcp %nick XDCC get 1%_ - request a file +%_/ctcp %nick XDCC queue%_ - see where you are in the queue +%_/ctcp %nick XDCC help%_ - this message +EOF - 'xdcc_sending_file', '[%_XDCC:%_] Sending the file [$0] %_$2%_ to %_$1%_', - 'xdcc_no_files', '[%_XDCC:%_] No files offered', - 'xdcc_print_file', '[%_XDCC:%_] [%_$0%_] %_$1%_ ... %_$2%_', - 'xdcc_queue_empty', '[%_XDCC:%_] The queue is currently empty', - 'xdcc_hr', '[%_XDCC:%_] ----', - 'xdcc_print_queue', '[%_XDCC:%_] $0. $1 - [$2] $3', - 'xdcc_file_not_found', '[%_XDCC:%_] File does not exist', - 'xdcc_added_file', '[%_XDCC:%_] Added [$0] $1', - 'xdcc_removed_file', '[%_XDCC:%_] Removed [$0] $1', - 'xdcc_reset', '[%_XDCC:%_] Reset!', - 'xdcc_log', '[%_XDCC:%_] $0', +Irssi::theme_register([ + 'xdcc_sending_file', '[%_XDCC%_] Sending the file [$0] %_$2%_ to %_$1%_', + 'xdcc_no_files', '[%_XDCC%_] No files offered', + 'xdcc_print_file', '[%_XDCC%_] [%_$0%_] %_$1%_ ... %_$2%_', + 'xdcc_queue_empty', '[%_XDCC%_] The queue is currently empty', + 'xdcc_hr', '[%_XDCC%_] ----', + 'xdcc_print_queue', '[%_XDCC%_] $0. $1 - [$2] $3', + 'xdcc_file_not_found', '[%_XDCC%_] File does not exist', + 'xdcc_added_file', '[%_XDCC%_] Added [$0] $1', + 'xdcc_removed_file', '[%_XDCC%_] Removed [$0] $1', + 'xdcc_reset', '[%_XDCC%_] Reset!', + 'xdcc_log', '[%_XDCC%_] $0', + 'xdcc_stats', '[%_XDCC%_] $0 ... %_$1%_', - 'xdcc_help', '$0', - 'loaded', '%R>>%n %_Scriptinfo:%_ Loaded $0 version $1 by $2.' + 'xdcc_help', '$0', + 'loaded', '%R>>%n %_Scriptinfo:%_ Loaded $0 version $1 by $2.' ]); my $messages = { - 'queue_is_full' => "The XDCC queue is currently full.", - 'no_files_offered' => "Sorry there's no warez today", - 'queue_is_empty' => "The XDCC queue is currently empty.", + 'queue_is_full' => "[%_XDCC%_] The XDCC queue is currently full.", + 'queue_is_empty' => "[%_XDCC%_] The XDCC queue is currently empty.", + 'no_files_offered' => "[%_XDCC%_] Sorry there's no warez today", - 'file_entry' => '[%d] %s ... %s', - 'file_count' => '%d file%s', - 'in_queue' => 'You are #%d in queue. Requested [%d] %s', - 'queue_length' => '%d request%s in queue', - 'sending_file' => 'Sending you %s ...!', -}; + 'file_entry' => '[%_XDCC%_] [%d] %s ... %s', + 'file_count' => '[%_XDCC%_] %d file%s', + 'file_help_get' => '[%_XDCC%_] Type %_/ctcp %nick xdcc send N%_ to request a file', -my $ctcp_version = 'BlAaaGhhee kHaLed RuLez l0l u0.1'; + 'in_queue' => '[%_XDCC%_] You are #%d in queue. Requested [%d] %s', + 'queue_length' => '[%_XDCC%_] %d request%s in queue', + 'sending_file' => '[%_XDCC%_] Sending you %s ...!', + 'file_help_send' => '[%_XDCC%_] Type %_/dcc %nick get%_ to accept the file', + + 'xdcc_final_warning' => '[%_XDCC%_] This is your last warning!', + 'xdcc_cancelled' => '[%_XDCC%_] Your download has been cancelled for inactivity.', + 'xdcc_autoget_tip' => '[%_XDCC%_] Tip: in irssi, type %_/set dcc_autoget ON%_', + + 'xdcc_help' => $help_remote, + 'xdcc_version' => "[%_XDCC%_] plugin $VERSION", +}; sub ctcp_reply { - my ($server, $data, $nick, $address, $target) = @_; + my ($server, $data, $nick, $address, $target) = @_; + + my ($ctcp, $cmd, $index) = split (" ", lc($data), 3); - my ($ctcp, $cmd, $index) = split (" ", lc($data), 3); - - if ($ctcp eq "version") { - $server->command("^NCTCP $nick $data $ctcp_version"); - Irssi::signal_stop(); - } - elsif ($ctcp ne "xdcc") { return; } - - if ($cmd eq "get") { xdcc_enqueue($server, $nick, $index) } - elsif ($cmd eq "send") { xdcc_enqueue($server, $nick, $index) } - elsif ($cmd eq "list") { xdcc_list($server, $nick) } - elsif ($cmd eq "queue") { xdcc_queue($server, $nick) } + if ($ctcp ne "xdcc") { return; } - Irssi::signal_stop(); + if ($cmd eq "get") { xdcc_enqueue($server, $nick, $index) } + elsif ($cmd eq "send") { xdcc_enqueue($server, $nick, $index) } + elsif ($cmd eq "queue") { xdcc_queue($server, $nick) } + elsif ($cmd eq "list") { xdcc_list($server, $nick) } + elsif ($cmd eq "help") { xdcc_message($server, $nick, 'xdcc_help_remote') } + else { xdcc_list($server, $nick) } + + Irssi::signal_stop(); } sub xdcc_message { - my ($server, $nick, $msgname, @params) = @_; - my $msg = $messages->{$msgname}; - $msg =~ s/%_/\x002/g; - $msg =~ s/%-/\x003/g; - $msg = sprintf $msg, @params; - $server->send_message( $nick, $msg, 1 ); + my ($server, $nick, $msgname, @params) = @_; + my (@msgs) = split ("\n", $messages->{$msgname}); + for my $msg (@msgs) { + $msg =~ s/%_/\x002/g; + $msg =~ s/%-/\x003/g; + $msg =~ s/%nick/$server->{nick}/g; + $msg = sprintf $msg, @params; + $server->send_message( $nick, $msg, 1 ); + } } sub xdcc_enqueue { - my ($server, $nick, $index) = @_; - my $id = int $index; - $id -= 1; + my ($server, $nick, $index) = @_; + my $id = int $index; + $id -= 1; - my $request = { - server => $server, - nick => $nick, - id => $id - }; + my $request = { + server => $server, + nick => $nick, + id => $id + }; - if (! $sending && @queue == 0) { - return xdcc_send($request); - } - elsif (@queue > $queue_max) { - xdcc_message( $server, $nick, 'queue_is_full' ); - return; - } - push(@queue, $request); - xdcc_queue($server, $nick); + if (! $sending && @queue == 0) { + return xdcc_send($request); + } + elsif (@queue > $queue_max) { + xdcc_message( $server, $nick, 'queue_is_full' ); + return; + } + push(@queue, $request); + xdcc_queue($server, $nick); } sub xdcc_list { - my ($server, $nick) = @_; - if (scalar @files == 0) { - xdcc_message( $server, $nick, 'no_files_offered' ); - return; - } - my ($msg, $file); - for (my $n = 0; $n < @files ; ++$n) { - xdcc_message( $server, $nick, 'file_entry', $n+1, $files[$n]->{fn}, $files[$n]->{desc} ); - } - xdcc_message( $server, $nick, 'file_count', scalar @files, scalar @files == 1 ? "" : "s" ); + my ($server, $nick) = @_; + if (scalar @files == 0) { + xdcc_message( $server, $nick, 'no_files_offered' ); + return; + } + my ($msg, $file); + for (my $n = 0; $n < @files ; ++$n) { + xdcc_message( $server, $nick, 'file_entry', $n+1, $files[$n]->{fn}, $files[$n]->{desc} ); + } + xdcc_message( $server, $nick, 'file_count', scalar @files, scalar @files == 1 ? "" : "s" ); + xdcc_message( $server, $nick, 'file_help_get'); } sub xdcc_queue { - my ($server, $nick) = @_; - if (scalar @queue == 0) { - xdcc_message( $server, $nick, 'queue_is_empty' ); - return - } - my $msg; - for (my $n = 0; $n < @queue; ++$n) { - if ($queue[$n]->{nick} == $nick) { - xdcc_message( $server, $nick, 'in_queue', $n+1, $queue[$n]->{id}+1, $files[$queue[$n]->{id}]->{fn} ) - # break - } - } - xdcc_message( $server, $nick, 'queue_length', scalar @queue, scalar @queue == 1 ? "" : "s" ) + my ($server, $nick) = @_; + if (scalar @queue == 0) { + xdcc_message( $server, $nick, 'queue_is_empty' ); + return + } + my $msg; + for (my $n = 0; $n < @queue; ++$n) { + if ($queue[$n]->{nick} == $nick) { + xdcc_message( $server, $nick, 'in_queue', $n+1, $queue[$n]->{id}+1, $files[$queue[$n]->{id}]->{fn} ) + # break + } + } + xdcc_message( $server, $nick, 'queue_length', scalar @queue, scalar @queue == 1 ? "" : "s" ) } sub xdcc_send { - my ($request) = @_; - my $server = $request->{server}; - my $nick = $request->{nick}; - my $id = $request->{id}; - my $file = $files[$id]; - my $path = $file->{path}; - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_sending_file', $id, $nick, $file->{fn}); - $server->command("/DCC send $nick $path"); - xdcc_message( $server, $nick, 'sending_file', $file->{fn} ); - $sending = 1; + my ($request) = @_; + my $server = $request->{server}; + my $nick = $request->{nick}; + my $id = $request->{id}; + my $file = $files[$id]; + my $path = $file->{path}; + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_sending_file', $id, $nick, $file->{fn}); + $server->command("/DCC send $nick $path"); + xdcc_message( $server, $nick, 'sending_file', $file->{fn} ); + xdcc_message( $server, $nick, 'file_help_send'); + $sending = 1; + $stats->{files_sent}++; + $stats->{users}->{$nick} ||= 0; + $stats->{users}->{$nick}++; + $stats->{files}->{$file->{fn}} ||= 0; + $stats->{files}->{$file->{fn}}++; } # client stuff sub xdcc_report { - if (scalar @files == 0) { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_no_files'); - } - else { - for (my $n = 0; $n < @files ; ++$n) { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_print_file', $n+1, $files[$n]->{fn}, $files[$n]->{desc}); - } - } - if (scalar @queue == 0) { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_queue_empty'); - } - else { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_hr'); - for (my $n = 0; $n < @files ; ++$n) { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_print_queue', $n+1, $queue[$n]->{nick}, $queue[$n]->{id}, $files[$queue[$n]->{id}-1]->{fn}); - } - } + if (scalar @files == 0) { + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_no_files'); + } + else { + for (my $n = 0; $n < @files ; ++$n) { + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_print_file', $n+1, $files[$n]->{fn}, $files[$n]->{desc}); + } + } + if (scalar @queue == 0) { + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_queue_empty'); + } + else { + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_hr'); + for (my $n = 0; $n < @files ; ++$n) { + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_print_queue', $n+1, $queue[$n]->{nick}, $queue[$n]->{id}, $files[$queue[$n]->{id}-1]->{fn}); + } + } +} +sub xdcc_stats { + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_stats', "plugin version", $VERSION); + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_stats', "total sent", $stats->{files_sent}); + + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_hr'); + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'top files'); + map { Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_stats', $_->[0], $_->[1] } + sort { $b->[1] <=> $a->[1] } + map { [$_, $stats->{files}->{$_}] } + keys $stats->{files}; + + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_hr'); + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'top users'); + map { Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_stats', $_->[0], $_->[1] } + sort { $b->[1] <=> $a->[1] } + map { [$_, $stats->{users}->{$_}] } + keys $stats->{users}; } sub xdcc_add { - my ($path, $desc) = @_; - if (substr($path, 0, 1) eq "~") { - $path =~ s/^~//; - $path = $ENV{"HOME"} . $path; - } - if (! -e $path) { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_file_not_found'); - return; - } - - my $fn = $path; - $fn =~ s|^.*\/||; - - my $id = scalar @files; - - my $file = { - id => $id, - fn => $fn, - path => $path, - desc => $desc, - }; - - push(@files, $file); + my ($path, $desc) = @_; + if (substr($path, 0, 1) eq "~") { + $path =~ s/^~//; + $path = $ENV{"HOME"} . $path; + } + if (! -e $path) { + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_file_not_found'); + return; + } + + my $fn = $path; + $fn =~ s|^.*\/||; - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_added_file', $id+1, $fn); + my $id = scalar @files; + + my $file = { + id => $id, + fn => $fn, + path => $path, + desc => $desc, + }; + + push(@files, $file); + + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_added_file', $id+1, $fn); } sub xdcc_del { - my ($id) = @_; - $id = (int $id) - 1; - my $file = $files[$id]; + my ($id) = @_; + $id = (int $id) - 1; + my $file = $files[$id]; - splice(@files, $id, 1); + splice(@files, $id, 1); - for (my $n = @queue; $n >= 0; --$n) { - if ($queue[$n]->{id} == $id) { - # send a message to the user that the file is no longer being offered - splice(@queue, $n, 1); - } - elsif ($queue[$n]->{id} > $id) { - --$queue[$n]->{id}; - } - } + for (my $n = @queue; $n >= 0; --$n) { + if ($queue[$n]->{id} == $id) { + # send a message to the user that the file is no longer being offered + splice(@queue, $n, 1); + } + elsif ($queue[$n]->{id} > $id) { + --$queue[$n]->{id}; + } + } - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_removed_file', $id+1, $file->{fn}); + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_removed_file', $id+1, $file->{fn}); } sub xdcc_reset { - @files = (); - @queue = (); - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_reset'); + @files = (); + @queue = (); + if ($dcc) { $dcc->destroy() } + $sending = 0; + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_reset'); } -sub xdcc_help { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_help', $help); +sub xdcc_help_local { + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_help', $help_local); } sub xdcc { - my ($cmd, $fn, $desc) = split (" ", $_[0], 3); + my ($cmd, $fn, $desc) = split (" ", $_[0], 3); - $cmd = lc($cmd); + $cmd = lc($cmd); - if ($cmd eq "-add") { xdcc_add($fn, $desc) } - elsif ($cmd eq "-del") { xdcc_remove($fn) } - elsif ($cmd eq "-list") { xdcc_report() } - elsif ($cmd eq "-reset") { xdcc_reset() } - elsif ($cmd eq "-help") { xdcc_help() } - else { xdcc_report() } + if ($cmd eq "-add") { xdcc_add($fn, $desc) } + elsif ($cmd eq "-del") { xdcc_remove($fn) } + elsif ($cmd eq "-list") { xdcc_report() } + elsif ($cmd eq "-reset") { xdcc_reset() } + elsif ($cmd eq "-stats") { xdcc_stats() } + elsif ($cmd eq "-help") { xdcc_help_local() } + else { xdcc_report() } } +my $timeout = undef; sub dcc_created { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'dcc created'); + my ($dcc) = @_; + # Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'dcc created'); + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_help', $_); + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_help', @_); + if ($timeout) { timeout_remove($timeout) } + $timeout = timeout_add_once($bother_delay, xdcc_bother, $dcc, $times); +} +sub xdcc_bother { + my ($dcc, $times) = @_; + if ($times == 3) { xdcc_message($dcc->{server}, $dcc->{nick}, 'xdcc_final_warning') } + if ($times <= 3) { + xdcc_message($dcc->{server}, $dcc->{nick}, 'file_help_send') + $timeout = timeout_add_once($bother_delay, xdcc_bother, $times, $server, $nick); + } + else { + xdcc_message($server, $nick, 'xdcc_transfer_cancelled'); + xdcc_message($server, $nick, 'xdcc_autoget_tip'); + $dcc->destroy(); + undef $timeout; + return + } } sub dcc_destroyed { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'dcc destroyed'); - $sending = 0; - if (@queue == 0) { return; } - my $request = shift @queue; - xdcc_send($request); + # Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'dcc destroyed'); + if ($timeout) { timeout_remove($timeout) } + $sending = 0; + if (@queue == 0) { return; } + my $request = shift @queue; + xdcc_send($request); } sub dcc_connected { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'dcc connected'); +# Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'dcc connected'); + if ($timeout) { timeout_remove($timeout) } } sub dcc_rejecting { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'dcc rejecting'); +# Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'dcc rejecting'); } sub dcc_closed { - Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'dcc closed'); +# Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'xdcc_log', 'dcc closed'); } # listen for xdcc end/cancel/close @@ -288,5 +362,5 @@ Irssi::signal_add('dcc rejecting', 'dcc_rejecting'); Irssi::signal_add('dcc closed', 'dcc_closed'); Irssi::signal_add('default ctcp msg', 'ctcp_reply'); Irssi::command_bind('xdcc', 'xdcc'); -Irssi::command_set_options('xdcc','add del list reset help'); +Irssi::command_set_options('xdcc','add del list stats reset help'); Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'loaded', $IRSSI{name}, $VERSION, $IRSSI{authors}); |
