All pastes #1642644 Raw Edit

Someone

public text v1 · immutable
#1642644 ·published 2009-10-25 13:23 UTC
rendered paste body
use strict;

use Irssi;

use warnings;

use LWP::Simple;

use vars qw($VERSION %IRSSI $size_min $size_max $size_str $size_rls $passkey $watch_root $release $tmpdir

                        $site $server $msg $nick $address $target $channel $botname $botaddr $torrent $cat

                        $torid $rlsok @section_ok @section_bad %release_ok %watch_dirs @release_bad $dldir $def_dl

                        $section $minfree);

 

$VERSION="0.2";
#
%IRSSI = (
#
        name => 'sct_auto',
#
        authors => 'roto',
#
        contact=> 'roto@irc',
#
        description => 'IRSSI Auto downloader for ScT IRC',
#
        license => 'Public Domain'
#
);
#
 
#
# Put your passkey here, obviously..
#
$passkey = "passkey";
#
$site = "https://www.eu.scenetorrents.org/";
#
$channel = "#scenetorrents";
#
$botname = "SceneTorrents";
#
$botaddr = 'sct@ScT.bot';
#
 
#
# Set this to a value to make sure the torrents always leave at lesat this much space (in megabytes)
#
$minfree = 1000; # 1gb
#
 
#
#Enable release size restrictions (in megabytes)
#
$size_min = 0;
#
$size_max = 2500; # 2.5gb
#
 
#
 #
# Set to your clients auto download/watch root or you can specify the full paths below
#
$watch_root = "/home/masta/watch/";
#
$tmpdir = "/home/masta/tmp/";
mkdir $watch_root;
#
mkdir $tmpdir;
#
%watch_dirs = ( def => $watch_root, #set this to a default dir to fallback too
#
                                                                tmpdir => $tmpdir, #used as temporary storage (required)
#
                          );
                               
#
# Setup release filters only allowing matched names
#
# The first parameter is used as a key to set the download directory
#
 
#
# Some very basic examples:
#
%release_ok = ( def => ['.'], );
#
 
#
#Not allowed (-ScT are packs)
#
@release_bad = ('scat','granny');
#
 
#
# Filter to only these sections
#
# Movies/DVDR, XXX, Music/Videos
#
@section_ok = ('.'); # Allowed
#
@section_bad = ('DOX','Music/Videos','Music/MP3','Games/Wii','Games/Xbox 360'); # Not allowed
#
 
#
###################################################################################################
#
#                                                                                                 #
#
# You shouldnt change anything below this unless your feeling adventurous or know wtf your doing! #
#
#                                                                                                 #
#
###################################################################################################
#
 
#
# Download the .torrent
#
sub fetch {
#
        my $url = $site . 'download.php/' . $torid . '/' . $release . '.torrent?passkey=' . $passkey;
#
        print $url if Irssi::settings_get_bool('debug_enabled');
#
    if (mirror($url, $watch_dirs{tmpdir}.$release.'.torrent'))
#
    {
#
        my $a = $watch_dirs{tmpdir} . $release.'.torrent';
#
        my $b = $watch_dirs{$section} . $release.'.torrent';
#
        unless (rename($a,$b))
#
        {
#
            Irssi::print("Failed to move temp file: $!",MSGLEVEL_CLIENTERROR);
#
            return 0;
#
        }
#
        return 1;
#
    }
#
        return 0;
#
}
#
 
#
# Check if enough space is available
#
sub check_space
#
{
#
        my $path = shift;
#
        my @free = split('\s+',`df -Pm $path | tail -n 1`);
#
        Irssi::print("Free space: $free[3]",MSGLEVEL_CRAP) if Irssi::settings_get_bool('debug_enabled');
#
        return 1 unless ($minfree + $size_rls > $free[3]);
#
        Irssi::print("There is not enough space left to download $release",MSGLEVEL_CLIENTNOTICE);
#
        return 0;
#
}
#
 
#
#Check the release name filters
#
sub check_rls
#
{
#
        foreach (@release_bad)
#
        {
#
                Irssi::print($_,MSGLEVEL_CRAP) if Irssi::settings_get_bool('debug_enabled');
#
                return 0 if $release =~ m/($_)/;
#
        }
#
        for my $sec ( keys %release_ok )
#
        {
#
                foreach (@{$release_ok{$sec}})
#
                {
#
                        Irssi::print("$_",MSGLEVEL_CRAP) if Irssi::settings_get_bool('debug_enabled');
#
                        if ($release =~ m/($_)/) {
#
                                $section = $sec;
#
                                Irssi::print("Section set to $section",MSGLEVEL_CLIENTNOTICE);
#
                                return 1;
#
                        }
#
                }
#
        }
#
        return 0
#
}
#
 
#
#Check the section name filter
#
sub check_section
#
{
#
        foreach (@section_bad)
#
        {
#
                Irssi::print($_,MSGLEVEL_CRAP) if Irssi::settings_get_bool('debug_enabled');
#
                return 0 if $cat =~ m/^($_)$/;
#
        }
#
 
#
        foreach (@section_ok)
#
        {
#
                Irssi::print($_,MSGLEVEL_CRAP) if Irssi::settings_get_bool('debug_enabled');
#
                return 1 if $cat =~ m/^($_)$/;
#
        }
#
        return 0;
#
}
#
 
#
# Check the size of the release
#
sub check_size
#
{
#
        my @s = split('\s',$size_str);
#
        $size_rls = $s[0] if $s[1] eq 'MB';
#
        $size_rls = $s[0] * 1000 if $s[1] eq 'GB';
#
        if (Irssi::settings_get_bool('freechk_enabled'))
#
        {
#
                return 0 unless check_space($watch_dirs{$section});
#
        }
#
        Irssi::print("Size converted to: $size_rls",MSGLEVEL_CRAP) if Irssi::settings_get_bool('debug_enabled');
#
        return 1 if ($size_rls > $size_min and $size_rls < $size_max);
#
        return 0;
#
}
#
 
#
# Parse channel message for the bots announce
#
sub parsemsg
#
{
#
        ($server,$msg,$nick,$address,$target) = @_;
#
        #print "$server,$msg,$nick,$address,$target";
#
        if (Irssi::settings_get_bool('sct_enabled') and $target eq $channel and $nick eq $botname and $address eq $botaddr)
#
        {
#
 
#
                if ($msg =~ /^Name....:\s(.*)$/) {
#
                        $release = $1;
#
                        if (Irssi::settings_get_bool('sct_enabled'))
#
                        {
#
                                if (check_rls())
#
                                {
#
                                        $rlsok = 1;
#
                                        Irssi::print(">Set release to: $release",MSGLEVEL_CRAP) ;#if Irssi::settings_get_bool('debug_enabled');
#
                                }
#
                                else {
#
                                        $rlsok = 0;
#
                                        Irssi::print("Skipped: $release",MSGLEVEL_CRAP) ;#if Irssi::settings_get_bool('debug_enabled');
#
                                }
#
                        }
#
                }
#
                if ($rlsok)
#
                {
#
                        # Step through each of our requirements if enabled
#
                        if ($msg =~ /^Category:\s(.*)$/)
#
                        {
#
                                $cat = $1;
#
                                Irssi::print("->Set cat to: $cat",MSGLEVEL_CRAP) if Irssi::settings_get_bool('debug_enabled');
#
                                if (Irssi::settings_get_bool('section_enabled'))
#
                                {
#
                                        $rlsok = 0 unless check_section();
#
                                }
#
                        }
#
                        elsif ($msg =~ /^Size....:\s(.*)$/) {
#
                                $size_str = $1;
#
                                Irssi::print("-->Set file size to: $size_str",MSGLEVEL_CRAP) if Irssi::settings_get_bool('debug_enabled');
#
                                if (Irssi::settings_get_bool('size_enabled'))
#
                                {
#
                                        $rlsok = 0 unless check_size();
#
                                }
#
                        }
#
                        elsif ($msg =~ /^URL.....:.*=(\d+)&/) {
#
                                $torid = $1;
#
                                Irssi::print("--->Set torrent id to: $torid",MSGLEVEL_CRAP) if Irssi::settings_get_bool('debug_enabled');
#
                                #fetch();
#
 
#
                                if (fetch()) { Irssi::print("---->Downloaded successfully!",MSGLEVEL_CRAP);
#
                                } else { Irssi::print("---->Download failed!",MSGLEVEL_CRAP);}
#
                                $rlsok = 0;
#
                        }
#
                }
#
        }
#
}
#
 
#
sub sct_enable
#
{
#
        print "Enabling ScT AUTO!";
#
        Irssi::settings_set_bool('sct_enabled', 1);
#
}
#
 
#
sub sct_disable
#
{
#
        print "Disabling ScT AUTO!";
#
        Irssi::settings_set_bool('sct_enabled', 0);
#
}
#
sub rls_enable
#
{
#
        print "Enabling ScT release name filter!";
#
        Irssi::settings_set_bool('rls_enabled', 1);
#
}
#
sub rls_disable
#
{
#
        print "Disabling ScT release name filter!";
#
        Irssi::settings_set_bool('rls_enabled', 0);
#
}
#
sub section_enable
#
{
#
        print "Enabling ScT section filter!";
#
        Irssi::settings_set_bool('section_enabled', 1);
#
}
#
sub section_disable
#
{
#
        print "Disableing ScT section filter!";
#
        Irssi::settings_set_bool('section_enabled', 0);
#
}
#
sub size_enable
#
{
#
        print "Enabling ScT size filter!";
#
        Irssi::settings_set_bool('size_enabled', 1);
#
}
#
sub size_disable
#
{
#
        print "Disableing ScT size filter!";
#
        Irssi::settings_set_bool('size_enabled', 0);
#
}
#
 
#
sub debug_enable
#
{
#
        print "Enabling ScT debug!";
#
        Irssi::settings_set_bool('debug_enabled', 1);
#
}
#
 
#
sub debug_disable
#
{
#
        print "Disableing ScT debug!";
#
        Irssi::settings_set_bool('debug_enabled', 0);
#
}
#
 
#
sub freechk_enable
#
{
#
        print "Enabling ScT free space check!";
#
        Irssi::settings_set_bool('freechk_enabled', 1);
#
}
#
 
#
sub freechk_disable
#
{
#
        print "Disableing ScT free space check!";
#
        Irssi::settings_set_bool('freechk_enabled', 0);
#
}
#
 
#
sub setup
#
{
#
        # Im not entirly sure how to do this properly, doesnt work as i would have expected
#
        Irssi::settings_add_bool('sct', 'sct_enabled', 1);# if not defined Irssi::settings_get_bool('sct_enabled');
#
        Irssi::settings_add_bool('sct', 'rls_enabled', 1);# if not defined Irssi::settings_get_bool('rls_enabled');
#
        Irssi::settings_add_bool('sct', 'section_enabled', 1);# if not defined Irssi::settings_get_bool('section_enabled');
#
        Irssi::settings_add_bool('sct', 'size_enabled', 0);# if not defined Irssi::settings_get_bool('size_enabled');
#
        Irssi::settings_add_bool('sct', 'debug_enabled', 0);# if not defined Irssi::settings_get_bool('size_enabled');
#
        Irssi::settings_add_bool('sct', 'freechk_enabled', 1);# if not defined Irssi::settings_get_bool('size_enabled');
#
}
#
sub header {
#
        Irssi::print("======================================================================", MSGLEVEL_CRAP);
#
        Irssi::print("|  ScT Auto downloader by roto, loosly based off of engywook's dler  |", MSGLEVEL_CRAP);
#
        Irssi::print("|                        Version $VERSION                                 |", MSGLEVEL_CRAP);
#
        Irssi::print("======================================================================", MSGLEVEL_CRAP);
#
}
#
sub sct_help
#
{
#
        header();
#
        Irssi::print("/sct_help             This message obvously :)", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_setup            Initialize the config with default values (only required once)", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_enable           Enable the sct auto downloader", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_disable          Disable the sct auto downloader", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_rls_enable       Enable the release name filter", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_rls_disable      Disable the release name filter", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_section_enable   Enable the section filter", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_section_disable  Disable the section filter", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_size_enable      Enable the size filter", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_size_disable     Disable the size filter", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_freechk_enable   Enable sct freespace check", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_freechk_disable  Disable sct freespace check", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_debug_enable     Enable sct debug output", MSGLEVEL_CRAP);
#
        Irssi::print("/sct_debug_disable    Disable sct debug output", MSGLEVEL_CRAP);
#
}
#
 
#
Irssi::signal_add("message public", \&parsemsg);
#
Irssi::command_bind sct_help => \&sct_help;
#
Irssi::command_bind sct_setup => \&setup;
#
Irssi::command_bind sct_disable => \&sct_disable;
#
Irssi::command_bind sct_enable => \&sct_enable;
#
Irssi::command_bind sct_rls_disable => \&rls_disable;
#
Irssi::command_bind sct_rls_enable => \&rls_enable;
#
Irssi::command_bind sct_section_disable => \&section_disable;
#
Irssi::command_bind sct_section_enable => \&section_enable;
#
Irssi::command_bind sct_size_disable => \&size_disable;
#
Irssi::command_bind sct_size_enable => \&size_enable;
#
Irssi::command_bind sct_freechk_disable => \&freechk_disable;
#
Irssi::command_bind sct_freechk_enable => \&freechk_enable;
#
Irssi::command_bind sct_debug_disable => \&debug_disable;
#
Irssi::command_bind sct_debug_enable => \&debug_enable;
#
 
#
#Initialize
#
setup();
#
header();