Bump version and update Changes
[app-fonbot-daemon.git] / lib / App / FonBot / Plugin / Common.pm
CommitLineData
8dc70d07
MG
1package App::FonBot::Plugin::Common;
2
b8d4a547 3our $VERSION = '0.001';
8dc70d07
MG
4
5use v5.14;
6use strict;
7use warnings;
8
9use parent qw/Exporter/;
10
11use JSON qw/encode_json/;
12use Log::Log4perl qw//;
13
14use DB_File qw//;
15use Storable qw/freeze thaw/;
16
17use App::FonBot::Plugin::Config qw/$dir $user $group @supplementary_groups/;
18
19##################################################
20
21our (%ok_user_addresses, %commands, %waiting_requests);
22our @EXPORT = qw/%ok_user_addresses %commands %waiting_requests sendmsg/;
23
24my $log=Log::Log4perl->get_logger(__PACKAGE__);
25
26sub init{
962dff7b
MG
27 $log->info('setting user and group');
28 $)=join ' ', scalar getgrnam $group, map {scalar getgrnam $_} @supplementary_groups;
29 $(=scalar getgrnam $group;
30 $<=$>=scalar getpwnam $user;
31 chdir $dir;
32
33 $log->info('initializing '.__PACKAGE__);
34 tie %ok_user_addresses, DB_File => 'ok_user_addresses.db';
35 tie %commands, DB_File => 'commands.db';
8dc70d07
MG
36}
37
38sub fini{
962dff7b
MG
39 $log->info('finishing '.__PACKAGE__);
40 untie %ok_user_addresses;
41 untie %commands;
8dc70d07
MG
42}
43
44##################################################
45
46sub sendmsg{
962dff7b
MG
47 my ($touser,$requestid,$replyto,$command,@args)=@_;
48
49 my $data={command=>$command, replyto=>$replyto, args => \@args };
50 $data->{requestid} = $requestid if defined $requestid;
51
52 if (exists $commands{$touser}) {
53 my $temp = thaw $commands{$touser};
31d9adee 54 push @$temp, $data;
962dff7b
MG
55 $commands{$touser} = freeze $temp
56 } else {
57 $commands{$touser} = freeze [$data]
58 }
59
60 if (exists $waiting_requests{$touser}) {
61 $waiting_requests{$touser}->continue;
62 delete $waiting_requests{$touser}
63 }
8dc70d07
MG
64}
65
661;
67__END__
68
69=encoding utf-8
70
71=head1 NAME
72
73App::FonBot::Plugin::Common - FonBot plugin that provides global variables and functions
74
75=head1 SYNOPSIS
76
77 use App::FonBot::Plugin::Common;
78 App::FonBot::Plugin::Common->init;
79
80 $ok_user_addresses{'marius OFTC mgvx'}=1; # Let user marius send messages to mgvx via the OFTC plugin
81 sendmsg 'marius', 'OFTC mgvx', 'echo', 'Hello', 'world!';
82
83 App::FonBot::Plugin::Common->fini;
84
85=head1 DESCRIPTION
86
87This FonBot plugin provides global variables and functions to the other plugins. It also sets the user and group according to the configuration file. It is a required plugin, since most other plugins depend on it.
88
89=head1 CONFIGURATION VARIABLES
90
91These are the L<App::FonBot::Plugin::Config> configuration variables used in this module
92
93=over
94
95=item C<$dir>
96
97Directory to chdir to.
98
99=item C<$user>
100
101User to change to.
102
103=item C<$group>
104
105Group to change to.
106
107=item C<@supplementary_groups>
108
109Supplementary groups list to set.
110
111=back
112
113=head1 EXPORTED SYMBOLS
114
115=over
116
117=item B<%ok_user_addresses>
118
119Hash that records combinations of username, driver and address that have sent commands to us. The key format is C<"$username $drivername $address">. B<fonbotd> will never send a message to an address which is not found in this hash.
120
121Example entry: C<$ok_user_address{"nobody EMAIL nobody@mailinator.com"}>.
122
123=item B<%commands>
124
125Hash from usernames to a C<Storable::freeze>d array of pending commands for the user.
126
127=item B<%waiting_requests>
128
129Hash from usernames to a waiting HTTP::Response, as defined by the POE::Component::Server::HTTP documentation.
130
131=item B<sendmsg>(I<$touser>, I<$requestid>, I<$replyto>, I<$command>, I<@args>)
132
133Sends a command to C<$touser>'s phone. The command includes a command name (C<$command>), a list of arguments (C<@args>) and a reply address (C<$replyto>). If I<$requestid> is defined, the command will also include that request ID.
134
135=back
136
137=head1 METHODS
138
139=over
140
141=item C<App::FonBot::Plugin::Common-E<gt>init>
142
143Sets the user and group according to the configuration variables and reads the exported variables from the disk.
144
145=item C<App::FonBot::Plugin::Common-E<gt>fini>
146
147Writes the exported variables to the disk.
148
149=back
150
151=head1 AUTHOR
152
153Marius Gavrilescu C<< <marius@ieval.ro> >>
154
155=head1 COPYRIGHT AND LICENSE
156
b8d4a547 157Copyright 2013-2015 Marius Gavrilescu
8dc70d07
MG
158
159This file is part of fonbotd.
160
161fonbotd is free software: you can redistribute it and/or modify
162it under the terms of the GNU Affero General Public License as published by
163the Free Software Foundation, either version 3 of the License, or
164(at your option) any later version.
165
166fonbotd is distributed in the hope that it will be useful,
167but WITHOUT ANY WARRANTY; without even the implied warranty of
168MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
169GNU Affero General Public License for more details.
170
171You should have received a copy of the GNU Affero General Public License
172along with fonbotd. If not, see <http://www.gnu.org/licenses/>
173
174
175=cut
This page took 0.020548 seconds and 4 git commands to generate.