Add perlcritic test and appease perlcritic
[app-remotegnokii.git] / lib / App / RemoteGnokii.pm
1 package App::RemoteGnokii;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6 our $VERSION = '0.000_001';
7
8 use Config::Any;
9 use File::Copy qw/move/;
10 use File::Temp qw/tempfile/;
11 use Plack::Request;
12
13 my $cfg;
14
15 sub cfg ($){ ## no critic (ProhibitSubroutinePrototypes)
16 unless ($cfg) {
17 $cfg = Config::Any->load_stems({stems => [$ENV{RGCONFIG} // '/etc/rg'], use_ext => 1, flatten_to_hash => 1});
18 my @cfg = values %$cfg;
19 $cfg = $cfg[0];
20 }
21
22 $cfg->{$_[0]}
23 }
24
25 sub sendsms {
26 my ($number, $text) = @_;
27 my ($fh, $file) = tempfile 'smsXXXX', TMPDIR => 1;
28 print $fh "$number\n$text" or warn "print: $!"; ## no critic (RequireCarping)
29 close $fh or warn "close: $!"; ## no critic (RequireCarping)
30 move $file, cfg 'spool';
31 }
32
33 ##################################################
34
35 sub action {
36 my ($number, $date, $text) = @_;
37 my $password = cfg 'password';
38 sendsms cfg 'number', <<"EOF"
39 $password
40 $number
41 $date
42 $text
43 EOF
44 }
45
46 sub psgi {
47 my $correct_password = cfg 'password';
48 sub {
49 my $r = Plack::Request->new(shift);
50 my @numbers = split /,/s, $r->param('numbers');
51 my $password = $r->param('password');
52 return [403, ['Content-Type', 'text/plain'], ['Bad password']] unless $password eq $correct_password;
53 my $text = $r->param('text');
54
55 sendsms $_, $text for @numbers
56 }
57 }
58
59 1;
60 __END__
61
62 =encoding utf-8
63
64 =head1 NAME
65
66 App::RemoteGnokii - Send SMS over the internet with gnokii-smsd
67
68 =head1 SYNOPSIS
69
70 use App::RemoteGnokii;
71 my $config_option = App::RemoteGnokii::cfg 'name';
72 App::RemoteGnokii::sendsms '0755555555', 'Hello world';
73 App::RemoteGnokii::action('0755555555', '2014-02-01', 'Goodbye');
74
75 =head1 DESCRIPTION
76
77 RemoteGnokii is a set of scripts that add networking to gnokii-smsd. With them, all messages received are forwarded to a given phone number, and messages can be sent via the HTTP gateway provided by RemoteGnokii.
78
79 =head1 CONFIGURATION OPTIONS
80
81 See below for the location of the configuration file. The following options are recognised:
82
83 =over
84
85 =item number
86
87 Forward incoming messages to this number (used by L<rg-action>).
88
89 =item password
90
91 The password needed to send a message with L<rg-psgi> and included in forwarded messages.
92
93 =item spool
94
95 The gnokii-smsd spool directory. Needs to be readable and writable by gnokii-smsd and L<rg-psgi>.
96
97 =back
98
99 =head1 ENVIRONMENT
100
101 =over
102
103 =item RGCONF
104
105 The basename of the configuration file. For example, if the configuration file is '/srv/rg/config.yml', RGCONF should be set to '/srv/rg/config'. Defaults to '/etc/rg'.
106
107 =back
108
109 =head1 TODO
110
111 =over
112
113 =item Write a section 7 manpage explaining everything
114
115 =item Add tests
116
117 =item Add a way to store messages for later retrieval via the webapp, instead of sending them immediately via SMS
118
119 =back
120
121 =head1 AUTHOR
122
123 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
124
125 =head1 COPYRIGHT AND LICENSE
126
127 Copyright (C) 2014 by Marius Gavrilescu
128
129 This library is free software: you can redistribute it and/or modify
130 it under the terms of the GNU General Public License as published by
131 the Free Software Foundation, either version 3 of the License, or (at
132 your option) any later version.
133
134
135 =cut
This page took 0.027144 seconds and 4 git commands to generate.