blib/lib/Bot/BasicBot/Pluggable/Module/Seen.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 38 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 189 SCAR 61.4
CC
21
Cov
0%
told
182.0
secs_to_string
56.0
update_seen
12.0
CRAP 462.0
1 package Bot::BasicBot::Pluggable::Module::Seen;
2 use base qw(Bot::BasicBot::Pluggable::Module);
3 use warnings;
4 use strict;
5  
6 our $VERSION = '0.86';
7  
8 sub init {
9 my $self = shift;
10 $self->config( { user_allow_hiding => 1 } );
11 }
12  
13 sub help {
14 return
15 "Tracks when and where people were last seen.
16 Usage:
17 seen <nick> (find out where 'nick' was last seen)
18 hide (Start hiding yourself from 'seen' reporting)
19 unhide (Stop hiding yourself from 'seen' reporting)
20 hidechan #chan (Hide a private channel from seen reporting)
21 unhidechan #chan (Stop hiding a private channel from seen reporting)
22 privatechan #chan (Don't show message contents from #chan in other channels)
23 unprivatechan #chan (Show message contents from #chan in other channels)
24 ";
25 }
26  
27 sub seen {
28 my ( $self, $mess ) = @_;
29 my $what = 'saying "' . $mess->{body} . '"';
30 $self->update_seen( $mess->{who}, $mess->{channel}, $what );
31 return;
32 }
33  
34 sub chanpart {
35 my ( $self, $mess ) = @_;
36 my $what = 'leaving the channel';
37 $self->update_seen( $mess->{who}, $mess->{channel}, $what );
38 return;
39 }
40  
41 sub chanjoin {
42 my ( $self, $mess ) = @_;
43 my $what = 'joining the channel';
44 $self->update_seen( $mess->{who}, $mess->{channel}, $what );
45 return;
46 }
47  
48 sub update_seen {
49 my ( $self, $who, $channel, $what ) = @_;
50 my $nick = lc $who;
51 my $ignore_channels = $self->get('user_ignore_channels') || {};
52 return if exists $ignore_channels->{$channel};
53  
54 $self->set(
55 "seen_$nick" => {
56 time => time,
57 channel => $channel,
58 what => $channel ne 'msg' ? $what : '<private message>',
59 }
60 );
61 }
62  
63 sub told {
64 my ( $self, $mess ) = @_;
65 my $body = $mess->{body};
66 return unless defined $body;
67  
68 my ( $command, $param ) = split( /\s+/, $body, 2 );
69 $command = lc($command);
70  
71 if ( $command eq "seen" and $param =~ /^(\S+)\??$/ ) {
72 my $who = lc($1);
73  
74 # First off, if they're asking about themselves, it's easy
75 if ($who eq lc $mess->{who}) {
76 $self->bot->emote(
77 channel => $mess->{channel},
78 body => "hands $who a mirror",
79 );
80 return;
81 }
82  
83 my $seen = $self->get("seen_$who");
84
85 my $ignore_channels = $self->get('user_ignore_channels') || {};
86 my $hidden_channel =
87 $seen && exists $ignore_channels->{ $seen->{channel} };
88  
89 if (!$seen || $hidden_channel
90 || ( $self->get("user_allow_hiding") and $self->get("hide_$who") )
91 ) {
92 return "Sorry, I haven't seen $1.";
93 }
94  
95 # If the channel is private, and it's a different channel to the one we
96 # are in, hide what they were saying
97 my $private_channels = $self->get('user_private_channels') || {};
98 if (exists $private_channels->{ $seen->{channel} }
99 && $mess->{channel} ne $seen->{channel})
100 {
101 $seen->{what} = "saying something";
102 }
103  
104  
105 my $diff = time - $seen->{time};
106 my $time_string = secs_to_string($diff);
107 return
108 "$1 was last seen in $seen->{channel} $time_string "
109 . $seen->{what} . ".";
110  
111 }
112 elsif ( $command eq "hide" and $mess->{address} ) {
113 my $nick = lc( $mess->{who} );
114 if ( !$self->get("user_allow_hiding") ) {
115 return "Hiding has been disabled by the administrator.";
116 }
117  
118 $self->set( "hide_$nick" => 1 );
119 return "Ok, you're hiding from seen status.";
120  
121 }
122 elsif ( $command eq "unhide" and $mess->{address} ) {
123 my $nick = lc( $mess->{who} );
124 $self->unset("hide_$nick");
125 return "Ok, you're visible to seen status.";
126 }
127 elsif ( my ($action, $type) = $command =~ /^(un)?(hide|private)chan$/ ) {
128 $action = $action ? 'remove' : 'add';
129 my $response;
130 my $reply_action = {
131 hide => "tracking users",
132 private => "externally revealing messages from users",
133 }->{$type};
134  
135 if ($self->authed($mess->{who})) {
136 my $channels = $self->get('user_' . $type .'_channels') || {};
137 if ($action eq 'add') {
138 $channels->{$param}++;
139 $response = "OK, not $reply_action in $param";
140 } else {
141 delete $channels->{$param};
142 $response = "OK, $reply_action in $param";
143 }
144 $self->set('user_' . $type . '_channels', $channels);
145 return $response;
146 } else {
147 return "You need to be authenticated to do that.";
148 }
149 }
150 }
151  
152 sub secs_to_string {
153 my $secs = shift;
154  
155 # Hopefully never used. But if the seen time is in the future, catch it.
156 my $weird = 0;
157 if ( $secs < 0 ) { $secs = -$secs; $weird = 1; }
158  
159 my $days = int( $secs / 86400 );
160 $secs = $secs % 86400;
161 my $hours = int( $secs / 3600 );
162 $secs = $secs % 3600;
163 my $mins = int( $secs / 60 );
164 $secs = $secs % 60;
165  
166 my $string = "";
167 $string .= "$days days " if $days;
168 $string .= "$hours hours " if $hours;
169 $string .= "$mins mins " if ( $mins and !$days );
170 $string .= "$secs seconds " if ( !$days and !$hours );
171  
172 return $string . ( $weird ? "in the FUTURE!!!" : "ago" );
173 }
174  
175 1;
176  
177 __END__
178  
179 =head1 NAME
180  
181 Bot::BasicBot::Pluggable::Module::Seen - track when and where people were seen
182  
183 =head1 VERSION
184  
185 version 1.20
186  
187 =head1 IRC USAGE
188  
189 =over 4
190  
191 =item seen <nick>
192  
193 Find out when the last time a nick was seen and where.
194  
195 =item hide
196  
197 Hide yourself from the seen reporting.
198  
199 =item unhide
200  
201 Stops hiding yourself from the seen reporting.
202  
203 =back
204  
205 =head1 VARS
206  
207 =over 4
208  
209 =item allow_hiding
210  
211 Defaults to 1; whether or not a nick can hide themselves from seen status.
212  
213 =back
214  
215 =head1 AUTHOR
216  
217 Mario Domgoergen <mdom@cpan.org>
218  
219 This program is free software; you can redistribute it
220 and/or modify it under the same terms as Perl itself.