summaryrefslogtreecommitdiffstats
path: root/scripts/elist.pl
blob: 104c108dc315e9a0d60c5c3504247f5cc7dc1b43 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/perl

# (c) 2007, Ilya Cassina <icassina@gmail.com>
#
# inspired by 'xlist.pl' by Matthäus 'JonnyBG' Wander <jbg@swznet.de>

# Usage: /elist [-min <usercount>] [-max <usercount] [#]<channelmask>


use strict;
use vars qw($VERSION %IRSSI);

use Irssi;
use Getopt::Long;

$VERSION = '1.0';
%IRSSI = (
    authors     => 'Ilya Cassina',
    contact     => 'icassina@gmail.com',
    name        => 'Enanched LIST',
    description => 'This script allow advanced parametrization ' .
                   'of the /list command. Accepted parameters are ' .
                   '-minusers <#users> and -maxusers <#users>. ',
    license     => 'GPLv2',
);

use Irssi qw(
    command_bind
    signal_add
);

### global variables ####
my %elist_channels = ();
my %elist_config = ();


sub elist_channels_free {
  %elist_channels = ();
}

sub elist_config_init {
  %elist_config = (
    mincount => 0,
    maxcount => 10000,
    chanmask => ""
  );
}

sub elist {
  my ($data, $server, $witem) = @_;

  ### init variables ###
  elist_config_init();
  
  #### processing arguments using Getopt ###
  Getopt::Long::config('permute', 'no_ignore_case');
  
  local(@ARGV) = split(/\s/, $data,);
  GetOptions (
    'mincount|m=i' => \$elist_config{"mincount"},
    'maxcount|M=i' => \$elist_config{"maxcount"},
  );
 
  ## setting chanmask (remaining argument) ##
  if (@ARGV . length == 0) {
    $elist_config{"chanmask"} = "";
  } else {
    # adding '#' character at the beginning if not already present! #
    if ($ARGV[0] !~/^\#.*/) {
      $elist_config{"chanmask"} = "\#". $ARGV[0];
    } else {
      $elist_config{"chanmask"} = $ARGV[0];
    }
  }

  ### sending LIST command to the server ###
  print "%K[%n".$server->{'tag'}."%K]%n %B<-->%n %m"."elist %n%B(%y"."min=%m".$elist_config{"mincount"}."%n".
                                                     ", %y"."max=%m".$elist_config{"maxcount"}."%n".
                                                     ", %y"."mask=%K'%m".$elist_config{"chanmask"}."%K'%B)";
  $server->command("LIST " . $elist_config{"chanmask"});
}


sub elist_collect {
  my ($server, $data) = @_;
          
  my (undef, $channel, $users, $topic) = split(/\s/, $data, 4);
  $topic = substr($topic, 1);
                 
  if ($users >= $elist_config{"mincount"} and $users <= $elist_config{"maxcount"}) {
    push @{$elist_channels{$users}}, [ $channel, $topic ];
  }
}

sub elist_show {
  my ($server) = @_;
  my ($printstring, $channel);

  ## keys of elist_channels are (int) users in channel ##
  foreach (reverse sort { $a <=> $b } keys %elist_channels) {
    my $user_count = $_;
    ## values are arrays of [ channel_name, topic ] ##
    foreach (@{$elist_channels{$user_count}}) {
      $printstring = "%K[%n" . $server->{'tag'} . "%K]%n " .
                      sprintf("%4d", $user_count ) .
                      " " . @{$_}[0];  ## channel name
      if (length @{$_}[1] > 0) {
        $printstring .= " %B->%n " . @{$_}[1]; ## topic
      }

      print $printstring;
    }
  }

  elist_channels_free();

  print "%K[%n".$server->{'tag'}."%K]%n %B<-->%n End of %m"."elist%n";
}

command_bind('elist', \&elist);
signal_add('event 322', \&elist_collect);
signal_add('event 323', \&elist_show);


##print "Usage: /elist [-min <usercount>] [-max <usercount] [#]<channelmask>"

# EOF #