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