initial commit
|
1 |
#!/usr/bin/perl |
2 | ||
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
6 |
use Net::XMPP; |
|
7 |
use DBI; |
|
8 |
use POSIX qw/ceil strftime/; |
|
9 | ||
10 |
use open ':utf8'; |
|
11 |
use open ':std'; |
|
12 |
use utf8; |
|
13 |
#use Data::Dumper; |
|
14 | ||
15 |
use threads; |
|
16 |
use threads::shared; |
|
17 |
use Thread::Queue; |
|
18 | ||
remplacement de nom de varia...
|
19 |
$ENV{PWD} = "" if not defined $ENV{PWD}; |
initial commit
|
20 |
my $scriptconf = $ENV{PWD} . "/$0"; |
21 |
$scriptconf =~ s/\.pl$//; |
|
22 |
$scriptconf =~ s/$/.conf/; |
|
23 |
if (-r $scriptconf) { |
|
24 |
package cfg; |
|
25 |
unless (my $return = do $scriptconf) { |
|
26 |
warn "couldn't parse $scriptconf: $@" if $@; |
|
27 |
warn "couldn't do $scriptconf: $!" unless defined $return; |
|
28 |
warn "couldn't run $scriptconf" unless $return; |
|
29 |
} |
|
30 |
} |
|
31 |
else { |
|
32 |
print "pas de config\n"; |
|
33 |
exit; |
|
34 |
} |
|
35 | ||
36 |
$SIG{INT}=\&terminate; |
|
37 |
#my $next_sms :shared; # utilisé pour le top du procahin envoi |
|
38 |
my $debugLevel = 0; |
|
39 | ||
remplacement de nom de varia...
|
40 |
my $phone_status : shared = ""; |
initial commit
|
41 | |
42 |
my $from_gtalksms_queue = Thread::Queue->new; |
|
43 |
my $to_gtalksms_queue = Thread::Queue->new; |
|
44 |
my $mail_queue = Thread::Queue->new; |
|
45 | ||
46 |
my $bot = &xmpp_login($cfg::config{xmpp}->{hostName}, |
|
47 |
$cfg::config{xmpp}->{portNumber}, |
|
48 |
$cfg::config{xmpp}->{userName}, |
|
49 |
$cfg::config{xmpp}->{passWord}, |
|
50 |
$cfg::config{xmpp}->{componentName}, |
|
51 |
$cfg::config{xmpp}->{resource}, |
|
52 |
$cfg::config{xmpp}->{tls}, |
|
53 |
1, "", 0, |
|
54 |
$cfg::config{DEBUG}); |
|
55 | ||
56 |
$SIG{INT} = sub { $bot->Disconnect; }; |
|
57 | ||
58 |
my $mail = threads->new(\&ssmtp_send_mail); |
|
59 | ||
60 |
my $from_gtalksms = threads->new(\&from_gtalksms_parse); |
|
61 | ||
62 |
my $to_gtalksms = threads->new(\&xmpp_send_sms); |
|
63 | ||
64 |
sub debug_print { |
|
65 |
print STDERR "sendxmpp: " . (join ' ', @_) . "\n" |
|
66 |
if (@_ && ($cfg::config{DEBUG} ||$cfg::config{VERBOSE})); |
|
67 |
} |
|
68 | ||
69 |
sub log_bot { |
|
70 |
# open(LOGFILE,'>>', 'bot.log'); |
|
71 |
# print LOGFILE sprintf("[%s] %s\n",strftime("%Y-%m-%d %H:%M:%S", localtime), shift); |
|
72 |
print sprintf("[%s] %s\n",strftime("%Y-%m-%d %H:%M:%S", localtime), shift); |
|
73 |
# close LOGFILE; |
|
74 |
} |
|
75 | ||
76 |
sub xmpp_logout($) { |
|
77 |
# HACK |
|
78 |
# messages may not be received if we log out too quickly... |
|
79 |
sleep 1; |
|
80 | ||
81 |
my $cnx = shift; |
|
82 |
$cnx->Disconnect(); |
|
83 |
xmpp_check_result ('Disconnect',0); # well, nothing to check, really |
|
84 |
} |
|
85 | ||
86 |
sub terminate () { |
|
87 |
my $cnx = shift; |
|
88 |
debug_print "caught TERM"; |
|
89 |
xmpp_logout($cnx); |
|
90 |
exit 0; |
|
91 |
} |
|
92 | ||
93 |
sub error_exit { |
|
94 |
my ($err,$cnx) = @_; |
|
95 |
print STDERR "$err\n"; |
|
96 |
xmpp_logout ($cnx) |
|
97 |
if ($cnx); |
|
98 |
exit 1; |
|
99 |
} |
|
100 | ||
101 |
sub xmpp_check_result { |
|
102 |
my ($txt, $res, $cnx)=@_; |
|
103 | ||
104 |
error_exit ("Error '$txt': result undefined") |
|
105 |
unless (defined $res); |
|
106 | ||
107 |
# res may be 0 |
|
108 |
if ($res == 0) { |
|
109 |
debug_print "$txt"; |
|
110 |
# result can be true or 'ok' |
|
111 |
} |
|
112 |
elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') { |
|
113 |
debug_print "$txt: " . $$res[0]; |
|
114 |
# otherwise, there is some error |
|
115 |
} |
|
116 |
else { |
|
117 |
my $errmsg = $cnx->GetErrorCode() || '?'; |
|
118 |
error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx); |
|
119 |
} |
|
120 |
} |
|
121 | ||
122 |
sub xmpp_login ($$$$$$$$$$$) { |
|
123 |
my ($host, $port, $user, $pw, $comp, $res, $tls, $no_tls_verify, $tls_ca_path, $ssl, $debug) = @_; |
|
124 |
my $cnx = new Net::XMPP::Client(debuglevel=>0); |
|
125 |
error_exit "could not create XMPP client object: $!" unless ($cnx); |
|
126 | ||
127 |
my $ssl_verify = 0x01; |
|
128 |
if ($no_tls_verify) { $ssl_verify = 0x00; } |
|
129 |
debug_print "ssl_verify: $ssl_verify"; |
|
130 | ||
131 |
debug_print "tls_ca_path: $tls_ca_path"; |
|
132 | ||
133 |
my @res; |
|
134 |
my $arghash = { |
|
135 |
hostname => $host, |
|
136 |
port => $port, |
|
137 |
tls => $tls, |
|
138 |
ssl_verify => $ssl_verify, |
|
139 |
ssl_ca_path => $tls_ca_path, |
|
140 |
ssl => $ssl, |
|
141 |
connectiontype => 'tcpip', |
|
142 |
componentname => $comp |
|
143 |
}; |
|
144 | ||
145 |
delete $arghash->{port} unless $port; |
|
146 |
if ($arghash->{port}) { |
|
147 |
@res = $cnx->Connect(%$arghash); |
|
148 |
error_exit ("Could not connect to '$host' on port $port: $@") unless @res; |
|
149 |
} else { |
|
150 |
@res = $cnx->Connect(%$arghash); |
|
151 |
error_exit ("Could not connect to server '$host': $@") unless @res; |
|
152 |
} |
|
153 | ||
154 |
xmpp_check_result("Connect",\@res,$cnx); |
|
155 | ||
156 |
if ($comp) { |
|
157 |
my $sid = $cnx->{SESSION}->{id}; |
|
158 |
$cnx->{STREAM}->{SIDS}->{$sid}->{hostname} = $comp |
|
159 |
} |
|
160 | ||
161 |
@res = $cnx->AuthSend(#'hostname' => $host, |
|
162 |
'username' => $user, |
|
163 |
'password' => $pw, |
|
164 |
'resource' => $res); |
|
165 |
xmpp_check_result('AuthSend',\@res,$cnx); |
|
166 | ||
167 |
return $cnx; |
|
168 |
} |
|
169 | ||
170 |
sub sql_request ($) { |
|
171 |
my $request = shift; |
|
172 |
my @result; |
|
173 |
my $dbh = DBI->connect($cfg::config{db}->{driver}, $cfg::config{db}->{user}, $cfg::config{db}->{password}, {'RaiseError' => 1}); |
|
174 |
$dbh->{'mysql_enable_utf8'} = 1; |
|
175 |
$dbh->do(qq{SET NAMES "utf8"}); |
|
176 |
my $sth = $dbh->prepare($request); |
|
177 |
$sth->execute(); |
|
178 |
if (defined($sth->{NUM_OF_FIELDS})) { |
|
179 |
while (my $ref = $sth->fetchrow_hashref()) { |
|
180 |
push @result, $ref; |
|
181 |
} |
|
182 |
} |
|
183 |
$sth->finish(); |
|
184 |
$dbh->disconnect(); |
|
185 |
return @result; |
|
186 |
} |
|
187 | ||
188 |
sub massive_send_sms { |
|
189 |
my $request = shift; |
|
190 |
my @results = sql_request("SELECT * FROM __tables__ WHERE phone = '$request->{phone}' AND table_id = '$request->{table}'"); |
|
191 |
if (scalar(@results) == 1) { |
|
192 |
my @results = sql_request("SELECT * FROM $request->{table}"); |
|
193 |
my $start_msg = "envoi de " . scalar(@results) . " SMS (30 max par demi-heure)"; |
|
194 |
$to_gtalksms_queue->enqueue([$request->{phone}, $start_msg]); |
|
195 |
foreach my $contact (@results) { |
|
196 |
$contact->{phone} =~ s/[\s\.]//g; |
|
197 |
$_ = $request->{body}; |
|
198 |
s/\@prénom/$contact->{firstname}/g; |
|
199 |
if ($contact->{gender} eq 'F') { |
|
200 |
s/\@\(\s*(\w+)\s*,\s*\w+\s*\)/$1/g; |
|
201 |
} |
|
202 |
else { |
|
203 |
s/\@\(\s*\w+\s*,\s*(\w+)\s*\)/$1/g; |
|
204 |
} |
|
205 |
$to_gtalksms_queue->enqueue([$contact->{phone}, $_]) |
|
206 |
} |
|
207 |
$to_gtalksms_queue->enqueue([$request->{phone}, "envoi des SMS terminé !"]); |
|
208 |
} |
|
209 |
else { |
|
210 |
$to_gtalksms_queue->enqueue([$request->{phone}, "pas la bonne base $request->{table} pour $request->{phone}"]); |
|
211 |
} |
|
212 |
} |
|
213 | ||
214 |
sub control_sms_flow { |
|
215 |
my ($body, $max, $interval) = @_; |
|
216 |
my $flow_control="/home/sms/flow.control"; |
|
217 |
# use bytes; # les caractères UTF-8 peuvent être codés sur plus d'un octet |
|
218 |
# my $nbr = ceil(length($$body)/140); # taille d'un SMS 140 octets |
|
219 |
my $nbr = 1; |
|
220 |
# no bytes; |
|
221 |
while (1) { |
|
222 |
my $left = $max; |
|
223 |
my $t = time; |
|
224 |
open (FILE, "<", $flow_control); |
|
225 |
my @lines = <FILE>; |
|
226 |
close FILE; |
|
227 |
open(FILE, ">", $flow_control); |
|
228 |
foreach my $line (@lines) { |
|
229 |
$line =~ /(\d+)/; |
|
230 |
my $ts = $1; |
|
231 |
if ($ts + $interval > $t) { |
|
232 |
$left--; |
|
233 |
print FILE $line; |
|
234 |
} |
|
235 |
} |
|
236 |
close FILE; |
|
237 |
log_bot(" ---> to send: $nbr; left: $left"); |
|
238 |
last if $nbr < $left; |
|
239 |
sleep 10; |
|
240 |
} |
|
241 |
open (FILE,">>", $flow_control); |
|
242 |
for (my $i = 0; $i < $nbr; $i++) { print FILE time, "\n"; } |
|
243 |
close FILE; |
|
244 |
} |
|
245 | ||
246 |
sub xmpp_send { |
|
247 |
my $msg = shift; |
|
248 |
$bot->MessageSend( |
|
remplacement de nom de varia...
|
249 |
to => $cfg::config{xmpp}->{phoneBuddy}, |
initial commit
|
250 |
from => $cfg::config{xmpp}->{userName} . "@" . $cfg::config{xmpp}->{hostName}, |
251 |
resource => $cfg::config{xmpp}->{resource}, |
|
252 |
type => 'chat', |
|
253 |
body => $$msg |
|
254 |
); |
|
255 |
} |
|
256 | ||
257 |
sub ssmtp_send_mail { |
|
258 |
while (my $request = $mail_queue->dequeue) { |
|
259 |
defined($request->{to}) or $request->{to} = $cfg::config{mail}; |
|
260 |
open(MAIL, "|/usr/lib/sendmail -t"); |
|
261 |
print MAIL "Subject: $request->{subject}\n"; |
|
262 |
print MAIL "To: $request->{to}\n"; |
|
263 |
print MAIL "$request->{body}\n"; |
|
264 |
close(MAIL); |
|
265 |
log_bot("mail envoyé à $request->{to}"); |
|
266 |
} |
|
267 |
} |
|
268 | ||
269 |
sub wait_open_time ($$) { |
|
270 |
my ($close_hour, $open_hour) = @_; |
|
271 |
while (strftime('%H', localtime) > $close_hour or strftime('%H', localtime) < $open_hour) { |
|
272 |
sleep 1800; |
|
273 |
} |
|
274 |
} |
|
275 | ||
276 |
sub xmpp_send_sms { |
|
277 |
while (my $ref = $to_gtalksms_queue->dequeue) { |
|
278 |
wait_open_time(21, 8); # pas d'envoi entre 21h et 8h |
|
279 |
# TODO lock($next_sms); |
|
280 |
# TODO $next_sms = $$ref[0]; |
|
281 |
control_sms_flow(\$$ref[1], 30, 1800); # pas plus de 30 messages de 140 caractères par demi-heure |
|
282 |
xmpp_send(\"sms:$$ref[0]:$$ref[1]"); |
|
283 |
log_bot("envoi à $$ref[0] : $$ref[1]"); |
|
284 |
sleep 10; # en attendant la maîtrise de cond_wait et cond_signal: cond_signal() called on unlocked variable at ./bot line 348 |
|
285 |
# TODO cond_wait($next_sms); # attend le top |
|
286 |
} |
|
287 |
} |
|
288 | ||
289 |
sub is_authorized { |
|
sms/action: envoi du mail de...
|
290 |
my ($request) = @_; |
291 |
my @results = sql_request("SELECT * FROM __authorized__ WHERE phone = '$request->{phone}'"); |
|
292 |
if (scalar(@results) == 1) { |
|
293 |
$request->{address} = $results[0]->{address}; |
|
294 |
return 1; |
|
295 |
} |
|
296 |
else { |
|
297 |
return 0; |
|
298 |
} |
|
initial commit
|
299 |
} |
300 | ||
301 |
sub authorized_on_table { |
|
302 |
my %request = @_; |
|
303 |
$request{writeable} = (defined($request{writeable})) ? "AND write_auth = '1'" : ""; |
|
304 |
my @results = sql_request("SELECT * FROM __tables__ WHERE table_id = '$request{table}' AND phone = '$request{id}' $request{writeable}"); |
|
305 |
if (scalar(@results) == 0) { |
|
306 |
my @list = sql_request("SELECT * FROM __tables__ WHERE phone = '$request{id}'"); |
|
307 |
my $list_str = ""; |
|
308 |
foreach (@list) { |
|
309 |
$list_str .= "\n- $_->{table_id}"; |
|
310 |
} |
|
311 |
${$request{error}} = sprintf("%s%s%s%s", |
|
312 |
"désolé, $request{table} n'est pas autorisé pour toi", |
|
313 |
($request{writeable} ne "")? " en écriture" : "", |
|
314 |
"... ou n'existe pas:", |
|
315 |
$list_str); |
|
316 |
} |
|
317 |
return (scalar(@results) == 1); |
|
318 |
} |
|
319 | ||
320 |
sub is_table { |
|
321 |
my %request = @_; |
|
322 |
my @results = sql_request("SHOW TABLES WHERE Tables_in_sms = '$request{table}'"); |
|
323 |
if (scalar(@results) == 0) { |
|
324 |
${$request{error}} = "$request{table} n'existe pas... essaie la commande 'liste' pour voir tes tables"; |
|
325 |
} |
|
326 |
return (scalar(@results) == 1); |
|
327 |
} |
|
328 | ||
329 |
sub copy { |
|
330 |
my $request = shift; |
|
331 |
my $rmsg = ""; |
|
332 |
if (authorized_on_table(table => $request->{origin}, id => $request->{phone}, error => \$rmsg)) { |
|
333 |
if (! is_table(table => $request->{destination}, error => \$rmsg)) { |
|
334 |
sql_request("CREATE TABLE $request->{destination} SELECT * FROM $request->{origin}"); |
|
335 |
sql_request("INSERT INTO __tables__ (phone,table_id,creation_date,update_date,comment,write_auth) values('" |
|
336 |
. $request->{phone} . "','" |
|
337 |
. $request->{destination} . "','" |
|
338 |
. strftime("%Y-%m-%d %H:%M:%S", localtime) . "','" |
|
339 |
. strftime("%Y-%m-%d %H:%M:%S", localtime) . "','" |
|
340 |
. $request->{comment} . "','1')" |
|
341 |
); |
|
342 |
$rmsg = "copie de $request->{origin} vers $request->{destination} faite"; |
|
343 |
} |
|
344 |
else { # on change le message d'erreur fourni par is_table() |
|
345 |
$rmsg = "$request->{destination} existe déjà !"; |
|
346 |
} |
|
347 |
} |
|
348 |
$to_gtalksms_queue->enqueue([$request->{phone}, $rmsg]); |
|
349 |
} |
|
350 | ||
351 |
sub insert { |
|
352 |
my $request = shift; |
|
353 |
my $rmsg = ""; |
|
354 |
if (authorized_on_table(table => $request->{table}, id => $request->{phone}, writeable => 1, error => \$rmsg)) { |
|
355 |
my $contact_number = 0; |
|
356 |
foreach my $contact (split("\n", $request->{body})) { |
|
357 |
my ($firstname, $lastname, $phone, $gender) = split(";", $contact); |
|
358 |
sql_request("INSERT INTO $request->{table} (firstname,lastname,phone,gender) values ($firstname, $lastname, $phone, $gender)"); |
|
359 |
$contact_number++; |
|
360 |
} |
|
361 |
$rmsg = "$contact_number contacts ajoutés dans $request->{table}" |
|
362 |
} |
|
363 |
$to_gtalksms_queue->enqueue([$request->{phone}, $rmsg]); |
|
364 |
} |
|
365 | ||
366 |
sub react_on_message { |
|
367 |
my ($hashref , $request) = @_; |
|
368 |
for my $regex (keys(%$hashref)) { |
|
369 |
if ($request->{body} =~ m/$regex/i) { |
|
370 |
$request->{body} =~ s/$regex//i; |
|
371 |
$hashref->{$regex}(); |
|
372 |
return 1; # true |
|
373 |
} |
|
374 |
} |
|
375 |
return 0; # false |
|
376 |
} |
|
377 | ||
378 |
sub messageCB { |
|
379 |
my ($sid, $msg) = @_; |
|
380 |
$from_gtalksms_queue->enqueue($msg) if $msg->DefinedBody(); |
|
381 |
} |
|
382 | ||
383 |
sub from_gtalksms_parse { |
|
384 |
while (my $msg = $from_gtalksms_queue->dequeue) { |
|
385 |
my %request = (body => $msg->GetBody); |
|
386 |
my %part_from_gtalksms = ( |
|
passage des regex dans le fi...
|
387 |
$cfg::gtalksms{"from"} => sub { # Message de +33612345678 : |
initial commit
|
388 |
$request{phone} = "0" . $1; |
389 |
$request{phone_owner} = ''; |
|
390 |
my @results = sql_request("SELECT * FROM section WHERE phone = '$request{phone}'"); |
|
391 |
my $number_of_candidates = @results; |
|
392 |
$number_of_candidates == 0 and return; |
|
393 |
$request{phone_owner} .= '('; |
|
394 |
foreach (@results) { |
|
395 |
$request{phone_owner} .= "$_->{firstname} $_->{lastname}"; |
|
396 |
--$number_of_candidates > 0 and $request{phone_owner} .= ' ou '; |
|
397 |
} |
|
passage des regex dans le fi...
|
398 |
$request{"phone_owner"} .= ')'; |
initial commit
|
399 |
log_bot("message de $request{phone} $request{phone_owner}"); |
400 |
}, |
|
passage des regex dans le fi...
|
401 |
$cfg::gtalksms{"delivered"} => sub { # SMS "un contenu de SMS" pour 0612345678 délivré. |
initial commit
|
402 |
log_bot("message $1 délivré pour $2"); |
403 |
# TODO cond_signal($next_sms); |
|
404 |
}, |
|
passage des regex dans le fi...
|
405 |
$cfg::gtalksms{forget} => sub { # Le destinataire par défaut est 0612345678: inutile |
initial commit
|
406 |
return; |
407 |
}, |
|
408 |
); |
|
409 |
my %part_from_user = ( |
|
passage des regex dans le fi...
|
410 |
$cfg::user{"stop"} => sub { |
initial commit
|
411 |
log_bot("arrêt demandé par $request{phone} $request{phone_owner}"); |
412 |
$bot->Disconnect(); |
|
413 |
}, |
|
passage des regex dans le fi...
|
414 |
$cfg::user{"message de groupe"} => sub { |
initial commit
|
415 |
$request{table} = lc $1; |
416 |
&massive_send_sms(\%request); |
|
417 |
}, |
|
passage des regex dans le fi...
|
418 |
$cfg::user{"message pour un destinataire"} => sub { |
initial commit
|
419 |
$to_gtalksms_queue->enqueue([$1, $2]); |
420 |
}, |
|
passage des regex dans le fi...
|
421 |
$cfg::user{"copie d'une base"} => sub { |
initial commit
|
422 |
$request{origin} = lc $1; |
423 |
$request{destination} = lc $2; |
|
424 |
$request{comment} = $3 =~ s/^\s*//r; |
|
425 |
©(\%request); |
|
426 |
}, |
|
passage des regex dans le fi...
|
427 |
$cfg::user{"ajout d'une entrée dans un base"} => sub { |
initial commit
|
428 |
$request{table} = lc $1; |
429 |
&insert(\%request); |
|
430 |
}, |
|
passage des regex dans le fi...
|
431 |
$cfg::user{"ping"} => sub { |
initial commit
|
432 |
log_bot("envoi d'un pong à $request{phone} $request{phone_owner}"); |
remplacement de nom de varia...
|
433 |
$to_gtalksms_queue->enqueue([$request{phone}, "pong ($phone_status)"]); |
initial commit
|
434 |
}, |
passage des regex dans le fi...
|
435 |
$cfg::user{"test intro pour action"} => sub { |
sms/action: envoi du mail de...
|
436 |
my $intro = $1; |
séparation de la configurati...
|
437 |
my $intro_file = $cfg::sms_action{flag_prefix} . $intro . $cfg::sms_action{flag_suffix}; |
sms/action: envoi du mail de...
|
438 |
if (! -r $intro_file) { |
439 |
$to_gtalksms_queue->enqueue([$request{phone}, "l'intro $intro n'existe pas, abandon"]); |
|
440 |
return; |
|
441 |
} |
|
442 |
my $time_left = 'time_error'; |
|
443 |
open(TIMER, "systemctl list-timers action.timer --no-legend |"); |
|
444 |
while (<TIMER>) { |
|
445 |
/^.+\s([0-9]+min)?\s*([0-9]+s)?\s+left\s.+$/; |
|
446 |
$time_left = $1 if defined $1; |
|
447 |
$time_left .= $2 if defined $2; |
|
448 |
} |
|
séparation de la configurati...
|
449 |
if (-r $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag}) { |
450 |
open FLAG, $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag}; |
|
sms/action: envoi du mail de...
|
451 |
my $locker = ''; |
452 |
my $intro_ = ''; |
|
453 |
while (<FLAG>) { |
|
454 |
if (/^firstname:(.+)$/) { |
|
455 |
$locker = " par " . $1; |
|
456 |
} |
|
457 |
if (/^intro:(.+)$/) { |
|
458 |
$intro_ = " de test ($1)"; |
|
459 |
} |
|
460 |
} |
|
461 |
close FLAG; |
|
462 |
$to_gtalksms_queue->enqueue([$request{phone}, "l'envoi " . $intro_ . " est déjà prévu" . $locker . ", abandon"]); |
|
463 |
} |
|
464 |
else { |
|
465 |
log_bot("demande envoi du mail des actions avec intro $intro depuis $request{phone} $request{phone_owner} à $request{address}"); |
|
séparation de la configurati...
|
466 |
mkdir $cfg::sms_action{flagdir}; |
467 |
chmod 0770, $cfg::sms_action{flagdir}; |
|
468 |
open(FLAG, ">" . $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag}); |
|
sms/action: envoi du mail de...
|
469 |
print FLAG "to:" . $request{address} . "\nintro:" . $intro . "\nfirstname:" . $request{phone} . " " . $request{phone_owner}; |
470 |
close FLAG; |
|
séparation de la configurati...
|
471 |
chmod 0666, $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag}; |
sms/action: envoi du mail de...
|
472 |
$to_gtalksms_queue->enqueue([$request{phone}, "le mail de test des actions avec intro $intro partira dans $time_left sur $request{address}"]); |
473 |
} |
|
474 |
}, |
|
passage des regex dans le fi...
|
475 |
$cfg::user{"envoi normal pour action"} => sub { |
sms/action: envoi du mail de...
|
476 |
my $time_left = 'time_error'; |
477 |
open(TIMER, "systemctl list-timers action.timer --no-legend |"); |
|
478 |
while (<TIMER>) { |
|
479 |
/^.+\s([0-9]+min)?\s*([0-9]+s)?\s+left\s.+$/; |
|
480 |
$time_left = $1 if defined $1; |
|
481 |
$time_left .= $2 if defined $2; |
|
482 |
} |
|
séparation de la configurati...
|
483 |
if (-r $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag}) { |
484 |
open FLAG, $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag}; |
|
sms/action: envoi du mail de...
|
485 |
my $locker = ''; |
486 |
my $intro_ = ''; |
|
487 |
while (<FLAG>) { |
|
488 |
if (/^firstname:(.+)$/) { |
|
489 |
$locker = " par " . $1; |
|
490 |
} |
|
491 |
if (/^intro:(.+)$/) { |
|
492 |
$intro_ = " de test ($1)"; |
|
493 |
} |
|
494 |
} |
|
495 |
$to_gtalksms_queue->enqueue([$request{phone}, "l'envoi" . $intro_ . " est déjà prévu" . $locker . ", abandon"]); |
|
496 |
} |
|
497 |
else { |
|
498 |
log_bot("demande envoi du mail des actions depuis $request{phone} $request{phone_owner}"); |
|
séparation de la configurati...
|
499 |
mkdir $cfg::sms_action{flagdir}; |
500 |
chmod 0770, $cfg::sms_action{flagdir}; |
|
501 |
open(FLAG, ">" . $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag}); |
|
502 |
chmod 0666, $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag}; |
|
sms/action: envoi du mail de...
|
503 |
$to_gtalksms_queue->enqueue([$request{phone}, "le mail des actions partira dans $time_left"]); |
504 |
} |
|
505 |
}, |
|
initial commit
|
506 |
); |
507 | ||
508 |
if (react_on_message(\%part_from_gtalksms, \%request)) { |
|
sms/action: envoi du mail de...
|
509 |
if (defined $request{phone} and !(is_authorized(\%request) and react_on_message(\%part_from_user, \%request))) { |
initial commit
|
510 |
$request{subject} = "SMS recu de $request{phone} $request{phone_owner}"; |
511 |
$mail_queue->enqueue(\%request); |
|
512 |
} |
|
513 |
} |
|
514 |
else { |
|
515 |
log_bot("message de type inconnu: $request{body}") unless $request{body} eq ""; |
|
516 |
} |
|
517 | ||
518 |
undef %request; |
|
519 |
} |
|
520 |
} |
|
521 | ||
522 |
$from_gtalksms->detach; # gère la file des événements produits par GTalkSMS |
|
523 |
$to_gtalksms->detach; # gère la file des envois de SMS par GTalkSMS |
|
524 |
$mail->detach; # gère la file des mails |
|
525 |
$bot->SetMessageCallBacks(chat => \&messageCB); |
|
526 | ||
527 |
#print "Logged in to $hostName:$portNumber...\n"; |
|
528 |
$bot->PresenceSend(); |
|
529 |
my $roster = $bot->Roster; |
|
remplacement de nom de varia...
|
530 |
$roster->add($cfg::config{xmpp}->{phoneBuddy}); |
initial commit
|
531 | |
532 |
while(defined($bot->Process())) { |
|
533 |
$bot->RosterGet(); |
|
remplacement de nom de varia...
|
534 |
my $status = $roster->query($cfg::config{xmpp}->{phoneBuddy},'resources'); |
535 |
$phone_status = $status->{GTalkSMS}->{status} =~ s/^GTalkSMS - //r if $status; |
|
initial commit
|
536 |
} |