85e3fe2b0057dc6853e1297e4127c141c60774e6
[spider.git] / cmd / show / groups.pl
1 #
2 # show recently used groups
3 #
4 # by Tommy SM3OSM
5 #
6 #
7 #
8
9 use Time::Local;
10
11 my $to;
12
13 sub handle
14 {
15         my $self = shift;
16         $to = shift;
17
18         if ($to =~ /\D/) {
19                 return (1, "try sh/chatgroups xxx where xxx is the number of chat messages to search.");
20         }
21         $to = 500 unless $to;
22
23         if ($self->{_nospawn}) {
24                 return (1, doit($self, DXLog::print(undef, $to, $main::systime, 'chat', undef)));
25         }
26         return (1, $self->spawn_cmd("show/groups $to", \&DXLog::print, args => [0, $to, $main::systime, 'chat', undef], cb => \&doit));
27 }
28
29 sub doit {
30         my $self = shift;
31         
32                 my @chatlog = @_;
33
34                 my $g= {};
35                 my @out;
36                 my $row;
37                 my ($time, $call, $group);
38                 my $found;
39                 my %month = (
40                                          Jan => 0,
41                                          Feb => 1,
42                                          Mar => 2,
43                                          Apr => 3,
44                                          May => 4,
45                                          Jun => 5,
46                                          Jul => 6,
47                                          Aug => 7,
48                                          Sep => 8,
49                                          Oct => 9,
50                                          Nov => 10,
51                                          Dec => 11,
52                                         );
53
54                 @chatlog = reverse @chatlog;
55                 foreach $row(@chatlog) {
56                         ($time, $call, $group) = ($row =~ m/^(\S+) (\S+) -> (\S+) /o);
57                         if (!exists $g->{$group}) {
58                                 $time =~ m/^(\d\d)(\w{3})(\d{4})\@(\d\d):(\d\d):(\d\d)/o;
59                                 $g->{$group}->{sec} = timegm($6, $5, $4, $1, $month{$2}, $3-1900);
60                                 $time =~ s/\@/ at /;
61                                 $g->{$group}->{last} = $time;
62                                 push @{ $g->{$group}->{calls} }, $call;
63                         }
64                         else {
65                                 $found = 0;
66                                 foreach (@{ $g->{$group}->{calls} }) {
67                                         if (/$call/) {
68                                                 $found = 1;
69                                                 last;
70                                         }
71                                 }
72                                 push @{ $g->{$group}->{calls} }, $call unless $found;
73                         }
74                         $g->{$group}->{msgcount}++;
75                 }
76
77                 push (@out, "Chat groups recently used:");
78                 push (@out, "($to messages searched)");
79                 push (@out, "--------------------------");
80                 my @calls;
81                 my @l;
82                 my $max = 6;
83                 my $mtext;
84                 foreach $group (sort { $g->{$b}->{sec}  <=> $g->{$a}->{sec} } keys %$g) {
85                         @calls = sort( @{ $g->{$group}->{calls} } );
86                         $mtext = "  " . $g->{$group}->{msgcount} . " messages by:";
87                         push (@out, "$group: Last active " . $g->{$group}->{last});
88                         if (@calls <= $max) {
89                                 push (@out, "$mtext @calls");
90                         }
91                         else {
92                                 foreach $call(@calls) {
93                                         push @l, $call;
94                                         if (@l >= $max) {
95                                                 if ($max == 6) {
96                                                         push (@out, "$mtext @l");
97                                                 }
98                                                 else {
99                                                         push (@out, "  @l");
100                                                 }
101                                                 @l = ();
102                                                 $max = 8;
103                                         }
104                                 }
105                                 push (@out, "  @l") if (@l);
106                                 $max = 6;
107                                 @l = ();
108                         }
109                         push (@out, "-");
110                 }
111                 return @out;
112         }
113
114