summaryrefslogtreecommitdiff
path: root/scripts/perl/kernel.pl
blob: ba8fae1a4f7f93c8badb3301846ce6241257e4c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 
# This script is mostly a copy/paste from kernel.pl irssi script
# This script is in the public domain.
# 
# Julien Louis <ptitlouis@sysif.net>

use IO::Socket;

weechat::register("kernel", "0.1", "", "Return latest kernel versions from kernel.org" );

weechat::add_command_handler("kernel_version", kernel_version);

sub finger($$) {
    # Yes, Net::Finger is already done and i'm reinventing the wheel.
   my ($user, $host) = @_;
   my $buffer;
   if (my $socket = IO::Socket::INET->new(
   	PeerHost => $host,
        PeerPort => 'finger(79)',
        Proto    => 'tcp',
      ))
    {
      if (syswrite $socket, "$user\n") {
          unless (sysread $socket, $buffer, 1024) {
              # Should i use $! here?
              weechat::print("Unable to read from the socket: $!");
	  }
	} else {
		 # ..and here?
		weechat::print("Unable to write to the socket: $!");
	}
    } else {
	weechat::print("Connection to $host failed: $!");
   }
	return $buffer;
}

sub kernel_version {
    my @version;
    if (my $finger = finger("", "finger.kernel.org")) {
        # The magic of the regexps :)
        @version = $finger =~ /:\s*(\S+)\s*$/gm;
        # Modify this to do whatever you want.
        
	my %branches = (
        	26 => "",
	        24 => "",
        	22 => "",
	        20 => "",
	);

	foreach my $kernel (@version) {
        	if($kernel =~ /2\.6/) {
	                $branches{26} .= "   $kernel";
	        } elsif($kernel =~ /2\.4/) {
        	        $branches{24} .= "   $kernel";
	        } elsif ($kernel =~ /2\.2/) {
        	        $branches{22} .= "   $kernel";
	        } elsif ($kernel =~ /2\.0/) {
        	        $branches{20} .= "   $kernel";
	        }
	}

	my @keys = sort(keys(%branches));
	foreach my $key (@keys) {
	        weechat::print("branche " . join('.', split(//, $key)));
        	weechat::print("$branches{$key}");
	}

#        weechat::print("@version");
	return weechat::PLUGIN_RC_OK;
    }
}