]> iEval git - app-fonbot-daemon.git/blob - Common.pm
300cf6b1fb01c35de7b2dbb3fdc6033dd94fb11b
[app-fonbot-daemon.git] / Common.pm
1 package App::FonBot::Plugin::Common;
2
3 our $VERSION = '0.000_5';
4
5 use v5.14;
6 use strict;
7 use warnings;
8
9 use parent qw/Exporter/;
10
11 use JSON qw/encode_json/;
12 use Log::Log4perl qw//;
13
14 use DB_File qw//;
15 use Storable qw/freeze thaw/;
16
17 use App::FonBot::Plugin::Config qw/$dir $user $group @supplementary_groups/;
18
19 ##################################################
20
21 our (%ok_user_addresses, %commands, %waiting_requests);
22 our @EXPORT = qw/%ok_user_addresses %commands %waiting_requests sendmsg/;
23
24 my $log=Log::Log4perl->get_logger(__PACKAGE__);
25
26 sub init{
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';
36 }
37
38 sub fini{
39 $log->info('finishing '.__PACKAGE__);
40 untie %ok_user_addresses;
41 untie %commands;
42 }
43
44 ##################################################
45
46 sub sendmsg{
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};
54 push $temp, $data;
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 }
64 }
65
66 1;
67 __END__
68
69 =encoding utf-8
70
71 =head1 NAME
72
73 App::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
87 This 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
91 These are the L<App::FonBot::Plugin::Config> configuration variables used in this module
92
93 =over
94
95 =item C<$dir>
96
97 Directory to chdir to.
98
99 =item C<$user>
100
101 User to change to.
102
103 =item C<$group>
104
105 Group to change to.
106
107 =item C<@supplementary_groups>
108
109 Supplementary groups list to set.
110
111 =back
112
113 =head1 EXPORTED SYMBOLS
114
115 =over
116
117 =item B<%ok_user_addresses>
118
119 Hash 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
121 Example entry: C<$ok_user_address{"nobody EMAIL nobody@mailinator.com"}>.
122
123 =item B<%commands>
124
125 Hash from usernames to a C<Storable::freeze>d array of pending commands for the user.
126
127 =item B<%waiting_requests>
128
129 Hash 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
133 Sends 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
143 Sets 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
147 Writes the exported variables to the disk.
148
149 =back
150
151 =head1 AUTHOR
152
153 Marius Gavrilescu C<< <marius@ieval.ro> >>
154
155 =head1 COPYRIGHT AND LICENSE
156
157 Copyright 2013 Marius Gavrilescu
158
159 This file is part of fonbotd.
160
161 fonbotd is free software: you can redistribute it and/or modify
162 it under the terms of the GNU Affero General Public License as published by
163 the Free Software Foundation, either version 3 of the License, or
164 (at your option) any later version.
165
166 fonbotd is distributed in the hope that it will be useful,
167 but WITHOUT ANY WARRANTY; without even the implied warranty of
168 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
169 GNU Affero General Public License for more details.
170
171 You should have received a copy of the GNU Affero General Public License
172 along with fonbotd. If not, see <http://www.gnu.org/licenses/>
173
174
175 =cut
This page took 0.065167 seconds and 3 git commands to generate.