]>
Commit | Line | Data |
---|---|---|
566f7a16 MG |
1 | package POE::Component::IRC::Plugin::Seen; |
2 | ||
d37b05fc | 3 | our $VERSION = 0.001001; |
566f7a16 MG |
4 | |
5 | use v5.14; | |
6 | use strict; | |
7 | use warnings; | |
8 | ||
9 | use DB_File; | |
10 | ||
11 | use IRC::Utils qw/lc_irc parse_user/; | |
12 | use POE::Component::IRC::Plugin qw/PCI_EAT_NONE PCI_EAT_PLUGIN/; | |
13 | ||
14 | ################################################## | |
15 | ||
16 | sub new{ | |
f45fe0ce MG |
17 | my $class = shift; |
18 | my $self = { @_ }; | |
566f7a16 | 19 | |
f45fe0ce MG |
20 | $self->{dbobj} = tie my %db, DB_File => $self->{filename}; |
21 | $self->{db} = \%db; | |
22 | bless $self, $class | |
566f7a16 MG |
23 | } |
24 | ||
25 | sub log_event { | |
f45fe0ce MG |
26 | my ($self, $nick, $event) = @_; |
27 | my $time = localtime; | |
28 | $self->{db}->{$nick} = "$time $event"; | |
29 | $self->{dbobj}->sync; | |
30 | PCI_EAT_NONE | |
566f7a16 MG |
31 | } |
32 | ||
33 | sub seen { | |
f45fe0ce MG |
34 | my ($self, $irc, $nick, $to, $from) = @_; |
35 | if (exists $self->{db}->{$nick}) { | |
36 | $irc->yield(privmsg => $to => "I last saw $nick $self->{db}->{$nick}") | |
37 | } else { | |
38 | $irc->yield(privmsg => $to => "I haven't seen $nick") | |
39 | } | |
40 | PCI_EAT_PLUGIN | |
566f7a16 MG |
41 | } |
42 | ||
43 | sub PCI_register { | |
f45fe0ce MG |
44 | my ($self, $irc) = @_; |
45 | $irc->plugin_register($self, SERVER => qw/ctcp_action join part public msg/); | |
46 | 1 | |
566f7a16 MG |
47 | } |
48 | ||
49 | sub PCI_unregister { 1 } | |
50 | ||
51 | sub S_ctcp_action { | |
f45fe0ce MG |
52 | my ($self, $irc, $rfullname, $rchannels, $rmessage) = @_; |
53 | my $nick = parse_user $$rfullname; | |
566f7a16 | 54 | |
f45fe0ce | 55 | log_event $self, $nick => "on $$rchannels->[0] doing: * $$rmessage" |
566f7a16 MG |
56 | } |
57 | ||
58 | sub S_public { | |
f45fe0ce MG |
59 | my ($self, $irc, $rfullname, $rchannels, $rmessage) = @_; |
60 | my $nick = parse_user $$rfullname; | |
61 | my $mynick = $irc->nick_name; | |
566f7a16 | 62 | |
3b40dfa4 | 63 | seen $self, $irc, $1, $$rchannels->[0], $nick if $$rmessage =~ /^(?:$mynick [,:])?\s*!?seen\s+([^ ]+)/x; |
f45fe0ce | 64 | log_event $self, $nick => "on $$rchannels->[0] saying $$rmessage" |
566f7a16 MG |
65 | } |
66 | ||
67 | sub S_join { | |
f45fe0ce MG |
68 | my ($self, $irc, $rfullname, $rchannel) = @_; |
69 | my $nick = parse_user $$rfullname; | |
566f7a16 | 70 | |
f45fe0ce | 71 | log_event $self, $nick => "joining $$rchannel" |
566f7a16 MG |
72 | } |
73 | ||
74 | sub S_part { | |
f45fe0ce MG |
75 | my ($self, $irc, $rfullname, $rchannel, $rmessage) = @_; |
76 | my $nick = parse_user $$rfullname; | |
77 | my $msg = $$rmessage ? " with message '$$rmessage'" : ''; | |
566f7a16 | 78 | |
f45fe0ce | 79 | log_event $self, $nick => "parting $$rchannel$msg" |
566f7a16 MG |
80 | } |
81 | ||
82 | sub S_msg { | |
f45fe0ce MG |
83 | my ($self, $irc, $rfullname, $rtargets, $rmessage) = @_; |
84 | my $nick = parse_user $$rfullname; | |
566f7a16 | 85 | |
b9ec22f5 | 86 | seen $self, $irc, $1, $$rtargets->[0], $nick if $$rmessage =~ /^\s*!?seen\s+([^ ]+)/ |
566f7a16 MG |
87 | } |
88 | ||
89 | 1; | |
90 | __END__ | |
91 | ||
92 | =head1 NAME | |
93 | ||
94 | POE::Component::IRC::Plugin::Seen - PoCo-IRC plugin that remembers seeing people | |
95 | ||
96 | =head1 SYNOPSIS | |
97 | ||
98 | use POE::Component::IRC::Plugin::Seen; | |
99 | ||
100 | my $irc = POE::Component::IRC->spawn; | |
101 | $irc->plugin_add(Seen => POE::Component::IRC::Plugin::Seen->new(filename => 'mycache.db')); | |
102 | ||
5f2b55be MG |
103 | # In chat |
104 | # <mgv> Hi there! | |
105 | # <foo> !seen mgv | |
106 | # <bot> I last saw mgv [DATE] on channel #whatever saying Hi there! | |
107 | ||
566f7a16 MG |
108 | =head1 DESCRIPTION |
109 | ||
5f2b55be MG |
110 | POE::Component::IRC::Plugin::Seen is a PoCo-IRC plugin that remembers |
111 | what each person around it did last. It remembers public messages, | |
112 | joins and parts. | |
566f7a16 | 113 | |
5f2b55be MG |
114 | When somebody sends him a private message of the form 'seen NICKNAME' |
115 | or somebody says 'seen NICKNAME' or 'botnick: seen NICKNAME' in a | |
116 | channel with the bot, the plugin answers with the last action NICKNAME | |
117 | did. There can be an exclamation mark before the word 'seen'. | |
566f7a16 MG |
118 | |
119 | =head1 METHODS | |
120 | ||
121 | =over | |
122 | ||
123 | =item B<new>([I<filename> => value]) | |
124 | ||
5f2b55be MG |
125 | Creates a new plugin object suitable for L<POE::Component::IRC>'s |
126 | C<plugin_add> method. | |
566f7a16 | 127 | |
5f2b55be MG |
128 | Takes one optional argument, C<filename>, the name of the file to |
129 | store the plugin's state in. If C<undef> or not given, it keeps the | |
130 | state in memory. | |
566f7a16 MG |
131 | |
132 | =back | |
133 | ||
134 | =head1 SEE ALSO | |
135 | ||
136 | L<POE::Component::IRC::Plugin> | |
137 | ||
138 | =head1 AUTHOR | |
139 | ||
140 | Marius Gavrilescu C<< <marius@ieval.ro> >> | |
141 | ||
142 | =head1 COPYRIGHT AND LICENSE | |
143 | ||
5f2b55be | 144 | Copyright (C) 2013-2015 by Marius Gavrilescu |
566f7a16 MG |
145 | |
146 | This library is free software; you can redistribute it and/or modify | |
147 | it under the same terms as Perl itself, either Perl version 5.14.2 or, | |
148 | at your option, any later version of Perl 5 you may have available. | |
149 | ||
150 | ||
151 | =cut |