All pastes #296192 Raw Edit

rab's perl theme grabber

public text v1 · immutable
#296192 ·published 2006-12-30 00:29 UTC
rendered paste body
#!/usr/bin/perl
#
# my version 0.2 ? ( dispite all of the other forks )
#
#  
# + Fri Dec 22 10:15:09 PST 2006
#
#   + Added 6 more WM support
#   + Created a function that for opening a file and 
#     grabbing something out of it
#
#     - ( $first_reg, $second_$reg) = fopen_reg( \[ "file path", "if more"], \[ [ "reg exp", "if more" ] ] );
#     - It returns an array of the matched strings
#     - Arrays must be passed as a reference to the actual array
#     - If you can improve to it, be my guest
#
#
# Basically just one big config array

use LWP::UserAgent;

%sh = ( 
        kernel    => {
            f         => 1,
            i         => `uname -r`
        },

        OS        => {
            f         => 1,
            i         => `cat /etc/arch-release`
        },

        wm        => {
            f           => 1,
            wmtheme     => undef,
            wmname      => undef
        },

        Gtk       => {
            f           => 1,
            Gtktheme    => undef,
            Gtkfont     => undef,
            Gtkicons    => undef,
            Gtkfiles    => [ "$ENV{HOME}/.gtkrc-2.0", "$ENV{HOME}/.gtkrc.mine" ],
            Gtkregex    =>  [
                    [
                        "include \"/.+/(?:\\.themes|themes)/(.+)/gtk-(1|2)\\.0/gtkrc\"",
                        "gtk-theme-name\\s*=\\s*\"(.+)\""  #Gtk Themes 
                    ],
                    [
                        "gtk-icon-theme-name\\s*=\\s*\"(.+)\"" #Gtk Icon themes
                    ],
                    [
                        "gtk-font-name\\s*=\\s*\"(.+)\"" #Gtk Font name
                    ] ]                
        },

        Qt        => {
            f         => 0,
            Qtfont    => undef,
            Qttheme   => undef,
            Qticons   => undef,
            Qtfiles   => [ "$ENV{HOME}/.kde/share/config/kdeglobals", "$ENV{HOME}/.kde/share/config/kdesktoprc" ],
            Qtregex   => [
                    [
                        "Theme=(.+)"
                    ],
                    [
                        "widgetStyle=(.+)"
                    ],
                    [
                        "font=(.+)"
                    ],
                    [
                        "Wallpaper=(.+)"
                    ] ]
        },

        de        => {
            f         => 1,
            is        => 0,
            name      => undef,
            theme     => undef,
            font      => undef,
            icon      => undef,
            wp        => undef

        },

        conky   => {
            f       => 1,
            fp      => "$ENV{HOME}/.conkyrc",
            url     => undef
        }
    );            

%de = ( Gnome	=> "gnome-session",
        Xfce4	=> "xfdesktop",
        Kde     => "kdesktop"  );

@wm = qw(Ratpoison Fluxbox Openbox Blackbox Xfwm4 E16 Metacity Kwin Pekwm Pwm Icewm Ion3 WMii);

# %de goes by the name of the DE
# and the 'id' you can find it in 
# the PS list...
#
# Prints little debugging messages if set to 0;
$quiet = 1; 
$tk = ( $sh{Gtk}{f} ? "Gtk" : "Qt" );

chomp $sh{OS}{i};
chomp $sh{kernel}{i};

parsePS("de");

if( !$sh{de}{is} ) {

    print "No DE found, not running one?..\n" 
        unless $quiet;

    if( parsePS("pname", "gnome-settings-daemon") ) {
        $sh{$tk}{"${tk}theme"} = `gconftool-2 -g /desktop/gnome/interface/gtk_theme`;
        $sh{$tk}{"${tk}icon"} = `gconftool-2 -g /desktop/gnome/interface/icon_theme`;
        $sh{$tk}{"${tk}font"} = `gconftool-2 -g /desktop/gnome/interface/font_name`;
    } else {
       ( $sh{$tk}{"${tk}theme"}, $sh{$tk}{"${tk}icon"}, $sh{$tk}{"${tk}font"} ) = fopen_reg(\$sh{$tk}{"${tk}files"}, \$sh{$tk}{"${tk}regex"});
    }

	## Processes First
	parsePS("wm");

	## Couldn't find a WM in PS
	print "No WM found, yours isn't on the list?...\n" if ( !$quiet && !$sh{wm}{wmname} );
} 

if( $sh{conky}{f} ) {
    if( parsePS("pname", "conky") ) {
        if( -e $sh{conky}{fp} ) {
            $sh{conky}{url} = fileupload("text", $sh{conky}{fp});
        }
    }
}

&printlogo;

sub fileupload {
    my ( $type, $file ) = @_;
    $_ = $type;
    undef $/;
    my ( $url, $fileconts ) = undef;
    my $fp = LWP::UserAgent->new( agent => "Perl Theme Grabber (Uploading)" );

    SWITCH: {
        /text/
            && do {
                open FILE, "<", $file || die "Error opening '$file'\n$!\n";
                $fileconts = <FILE>;
                close FILE;

            # pastebin.archlinux.org seems good enough
                my $ret = $fp->post( "http://pastebin.archlinux.org/pastebin.php",
                            [
                                parent_pid  => undef,
                                format      => "text",
                                code2       => $fileconts,
                                poster      => "ThemeBot - ".`whoami`."'s file",
                                paste       => "Send",
                                expiry      => "f",
                            ] );
            
                $ret = $fp->get( "http://pastebin.archlinux.org/" );

                ( $url ) = $ret->content =~ /<a href="http:\/\/pastebin\.archlinux\.org\/(\d+)">ThemeBot - /;
                $url = "http://pastebin.archlinux.org/$url"
                    unless !defined $url;
        };
        
#        /image/
#            && do {
#                open FILE, "<", $file || die "Error opening '$file'\n$!\n";
#                binmode FILE;
#                $fileconts .= $_ while read FILE, $_, 1;
#                close FILE;
#
#                # imageshack.us shall do :)
#
#                my $ret = $fp->
    }

return $url;
}

sub printlogo {
    my @temp;

    push @temp, "OS: $sh{OS}{i}"
        unless !$sh{OS}{f};
    push @temp, "Kernel: $sh{kernel}{i}"    
        unless !$sh{kernel}{f};

    if( $sh{de}{is} && $sh{de}{f} ) {
        push @temp, "DE: $sh{de}{name}";
        push @temp, "$sh{de}{name} Theme: $sh{de}{theme}"
            unless !defined $sh{de}{theme};
        push @temp, "$sh{de}{name} Icons: $sh{de}{icon}"
            unless !defined $sh{de}{icon};
        push @temp, "$sh{de}{name} Font: $sh{de}{font}"
            unless !defined $sh{de}{font};
        push @temp, "$sh{de}{name} Wall: $sh{de}{wp}"
            unless !defined $sh{de}{wp};
    } elsif( $sh{$tk}{f} ) {
        push @temp, "$tk Theme: ".$sh{$tk}{"${tk}theme"}
            unless !defined $sh{$tk}{"${tk}theme"};
        push @temp, "$tk Icons: ".$sh{$tk}{"${tk}icon"}
            unless !defined $sh{$tk}{"${tk}icon"};
        push @temp, "$tk Font: ".$sh{$tk}{"${tk}font"}
            unless !defined $sh{$tk}{"${tk}font"};
    }

    push @temp, "WM: $sh{wm}{wmname}"
        unless (!defined $sh{wm}{wmname});
    push @temp, "$sh{wm}{wmname} Theme: $sh{wm}{wmtheme}"
        unless (!defined $sh{wm}{wmtheme}); 
    push @temp, "Conky: $sh{conky}{url}"
        unless !defined $sh{conky}{url};

    for( $x = 0; $x < @temp; $x++) { print $temp[$x]."\n"; }
}

sub parsePS {
    $_ = shift;
    my $pname = shift;
	my @psl = split /\n/, `ps -A | awk {'print \$4'}`;

    SWITCH: {
		/wm/
            && do  {
    			for( my $x = 0; $x < @wm; $x++ ) {
    				for( my $y = 0; $y < @psl; $y++ ) {
    					print "Testing '$psl[$y]' with '$wm[$x]'\n"
                            unless $quiet;
    					if( $psl[$y] =~ /^$wm[$x]$/i ) {
    						print "WM found as $wm[$x]\n"
                                unless $quiet;
                            $sh{wm}{wmname} = $wm[$x];
                            &wmtheme;
    						last;
    					}
    				}
                    $sh{wm}{wmname} && last;
	    	    }
            last SWITCH;
		};
		/de/ 
            && do {
    			while(($de, $deid) = each %de) {
    				for( my $x = 0; $x < @psl; $x++ ) {
    					print "Testing '$psl[$x]' with '$deid'\n" 
                            unless $quiet;
    					if( $psl[$x] eq $deid ) {
    						print "DE found as $de\n" 
                                unless $quiet;
    						$sh{de}{is} = 1;
                            $sh{de}{name} = $de;
                            ($sh{de}{theme}, $sh{de}{icon}, $sh{de}{font}, $sh{de}{wp}) = &deinfo;
    						last;
    					}
    				}
    				$sh{de}{is} && last;
    			}

            parsePS("wm");
            last SWITCH;
    	};	
        /pname/
            && do {
                for( my $y = 0; $y < @psl; $y++ ) {
                    print "Testing '$psl[$y]' with '$pname'\n"
                        unless $quiet;
    	    	    if( $psl[$y] =~ /^$pname/i ) {
    		            print "$pname was found\n"
                            unless $quiet;
                        return 1;
    		        }
    	        }
        };
	}
}

sub fopen_reg {
    my ( $files, $regexp ) = @_;
    my @ret;
    
    for( my $file = 0; $file < @$$files; $file++ ) {
        print "Opening $$files->[$file]\n"
            unless $quiet;
        open FILE, "<", $$files->[$file] || die "$!\nFile: $$files->[$file]\n";
        while( <FILE> ) {
            for( my $y = 0; $y < @$$regexp; $y++) {
                my $regex = $$$regexp[$y];
                for( $z = 0; $z < @$regex; $z++ ) {
                    print "\tSearching for $regex->[$z]\n"
                        unless $quiet;
                    if( /$regex->[$z]/ ) {
                        $ret[$y] = $1;
                        print "Found a value \"$1\"\n"
                            unless $quiet;
                        pop @$regex, $regex->[$z];
                    }
                }
            }
        }
        close FILE;
    }
    return @ret;
}

sub wmtheme {
    $_ = $sh{wm}{wmname};

    SWITCH: {
		/openbox/i && 
            do {
			    ($sh{wm}{wmtheme}) = fopen_reg(
                        \[ "$ENV{HOME}/.config/openbox/rc.xml" ],
                        \[ ["<name>(.+)</name>"] ] );
			    
                last SWITCH;
		    };
		/metacity/i &&
            do {
			    $sh{wm}{wmtheme} = `gconftool-2 -g /apps/metacity/general/theme`;
			               
                last SWITCH;
            };
		/fluxbox/i && 
            do {
			    ($sh{wm}{wmtheme}) = fopen_reg( 
                        \[ "$ENV{HOME}/.fluxbox/init"],
                        \[ [ "session\\.styleFile:\\s*/.+/(.+)/?\$" ] ] );
                
                last SWITCH;
		    };
		/blackbox/i &&
            do {
			    ($sh{wm}{wmtheme}) = fopen_reg( 
                        \[ "$ENV{HOME}/.blackboxrc" ],
                        \[ [ "session\\.styleFile:\\s*/.+/(.+)/?\$" ] ] );
                
                last SWITCH;
    		};
		/xfwm/i &&
            do {
			    ($sh{wm}{wmtheme}) = fopen_reg(
                        \[ "$ENV{HOME}/.config/xfce4/mcs_settings/xfwm4.xml" ],
                        \[ [ "<option name=\"Xfwm/ThemeName\" type=\"string\" value=\"(.+)\"/>" ] ] );
				
                last SWITCH;
		    };
        /ratpoison/i &&
            do {
                $sh{wm}{wmtheme} = "Ratpoison has no themes dumby!";
                
                last SWITCH;
            };
        /kwin/i && 
            do {
                ($sh{wm}{wmtheme}) = fopen_reg( 
                        \[ "$ENV{HOME}/.kde/share/config/kwinrc" ],
                        \[ [ "PluginLib=(.+)" ] ] );

                last SWITCH;
            };
        /e16/i && 
            do {
                ($sh{wm}{wmtheme}) = fopen_reg(
                        \[ "$ENV{HOME}/.e16/e_config--0.0.cfg" ],
                        \[ [ "theme\\.name = (.+)" ] ] );

                last SWITCH;
            };
        /pekwm/i &&
            do {
                ($sh{wm}{wmtheme}) = fopen_reg(
                        \[ "$ENV{HOME}/.pekwm/config" ],
                        \[ [ "Theme = \"/?.+/(.+)/?\"" ] ] );

                last SWITCH;
            };
        /pwm/i && 
            do {
                ($sh{wm}{wmtheme}) = fopen_reg(
                        \[ "$ENV{HOME}/.pwm/pwm.conf", 
                           "/etc/pwm/pwm.conf" ],
                        \[ [ "include\\s*\"/?.*/?look-(.+).conf\"" ] ] );

                last SWITCH;
            };
        /icewm/i && 
            do {
                ($sh{wm}{wmtheme}) = fopen_reg(
                        \[ "$ENV{HOME}/.icewm/theme" ],
                        \[ [ "Theme=\"([^/]+)/default.theme\"" ] ] );

                last SWITCH;
            };
        /ion3/i && 
            do {
                ($sh{wm}{wmtheme}) = fopen_reg(
                        \[ "$ENV{HOME}/.ion3/default-session--0/look.lua" ],
                        \[ [ "dopath\\(\"(.+)\"\\)" ] ] );

                last SWITCH;
            };
        /wmii/i && 
            do {
                $sh{wm}{wmtheme} = "'$ENV{WMII_NORMCOLORS}' '$ENV{WMII_SELCOLORS}'";

                last SWITCH;
            };
	}
}

sub deinfo {
    $_ = $sh{de}{name};
	
    SWITCH: {
	    /gnome/i && 
            do  {
                $tk = "Gtk";
			    $theme = `gconftool-2 -g /desktop/gnome/interface/gtk_theme`;
                $icon = `gconftool-2 -g /desktop/gnome/interface/icon_theme`;
                $font = `gconftool-2 -g /desktop/gnome/interface/font_name`;
                $wp = `gconftool-2 -g /desktop/gnome/background/picture_filename`;

                last SWITCH;
            };
		/xfce4/i && 
            do {
                $tk = "Gtk";
			    ( $theme, $icon, $font, $wp ) = fopen_reg( 
                            \[ "$ENV{HOME}/.config/xfce4/mcs_settings/gtk.xml",
                               "$ENV{HOME}/.config/xfce4/mcs_settings/desktop.xml" ],
                            \[
                                [ "<option name=\"Net\/ThemeName\" type=\"string\" value=\"(.+)\"\/>" ],
                                [ "<option name=\"Net\/IconThemeName\" type=\"string\" value=\"(.+)\"\/" ],
                                [ "<option name=\"Gtk\/FontName\" type=\"string\" value=\"(.+)\"\/>" ],
                                [ "<option name=\"imagepath_0_0\" type=\"string\" value=\"(.+)\"\/>" ]
                             ] );
                
                last SWITCH;
            };
        /kde/i &&
            do {
                $tk = "Qt";
                ( $theme, $icon, $font, $wp ) = fopen_reg(\$sh{$tk}{"${tk}files"}, \$sh{$tk}{"${tk}regex"});

                last SWITCH;
            };
        }

    return ( $theme, $icon, $font, $wp );
}