Showing 2 changed files with 106 additions and 9 deletions
+25 -5
action/action
... ...
@@ -23,6 +23,26 @@ else {
23 23
     exit;
24 24
 }
25 25
 
26
+if (-r $cfg::config{notification}->{flag}) {
27
+    open FLAG, $cfg::config{notification}->{flag};
28
+    while (<FLAG>) {
29
+        if (/^to:(.+)$/) {
30
+            $cfg::config{test}->{address} = $1;
31
+        }
32
+        elsif (/^intro:([[:alnum:]]+)/) {
33
+            $cfg::config{test}->{intro} = $cfg::config{notification}->{intro_default_path} . $1 . $cfg::config{notification}->{intro_suffix};
34
+        }
35
+    }
36
+    close FLAG;
37
+    if (exists $cfg::config{test}->{address} and exists $cfg::config{test}->{intro} and -r $cfg::config{test}->{intro}) {
38
+        delete $cfg::config{notification}->{to};
39
+        $cfg::config{notification}->{to}->{test}->{address} = $cfg::config{test}->{address};
40
+        $cfg::config{notification}->{to}->{test}->{intro} = $cfg::config{test}->{intro};
41
+        $cfg::config{notification}->{to}->{test}->{outro} = "pour test";
42
+        $cfg::config{notification}->{to}->{test}->{subject} = "test de l'envoi des actions de la section";
43
+    }
44
+}
45
+
26 46
 my $dbh = DBI->connect($cfg::config{db}->{driver}, $cfg::config{db}->{user}, $cfg::config{db}->{password}, {'RaiseError' => 1, 'AutoCommit' => 1});
27 47
 $dbh->{'mysql_enable_utf8'} = 1;
28 48
 $dbh->do(qq{SET NAMES "utf8"});
... ...
@@ -64,7 +84,7 @@ foreach my $category (keys(%{$polls})) {
64 84
         }
65 85
 
66 86
 # PURGE DES ACTIONS DÉJÀ PASSÉES ET NON RÉCURRENTES
67
-        if ($last_date < $dt && $polls->{$category}->{$poll_id}->{recurrent} < 1) {
87
+        if ($last_date < $dt and ( ! defined $polls->{$category}->{$poll_id}->{recurrent} or $polls->{$category}->{$poll_id}->{recurrent} < 1)) {
68 88
             print "purge de $category/$polls->{$category}->{$poll_id}->{title}\n";
69 89
             $request = "delete from $tables{polls} where id = '$poll_id'";
70 90
             delete $polls->{$category}->{$poll_id};
... ...
@@ -122,9 +142,9 @@ foreach my $dest (keys(%{$cfg::config{notification}->{to}})) {
122 142
 
123 143
     my $data = '';
124 144
 
125
-    if (substr($cfg::config{notification}->{to}->{$dest}->{intro}, 0, 1) eq "/"
126
-    and -r $cfg::config{notification}->{to}->{$dest}->{intro}) {
127
-        open INTRO, $cfg::config{notification}->{to}->{$dest}->{intro};
145
+    if (substr($cfg::config{notification}->{to}->{$dest}->{intro}, 0, 1) eq "@"
146
+    and -r $cfg::config{notification}->{intro_default_path} . substr($cfg::config{notification}->{to}->{$dest}->{intro},1) . $cfg::config{notification}->{intro_suffix}) {
147
+        open INTRO, $cfg::config{notification}->{intro_default_path} . substr($cfg::config{notification}->{to}->{$dest}->{intro},1) . $cfg::config{notification}->{intro_suffix};
128 148
         while (<INTRO>) {
129 149
             if (/^<body /../^<\/body>/) {
130 150
                 $data .= $_ unless /^<body / or /^<\/body>/;
... ...
@@ -159,7 +179,7 @@ foreach my $dest (keys(%{$cfg::config{notification}->{to}})) {
159 179
     $mail->send(
160 180
             -from=>$cfg::config{notification}{from_infos}{from},
161 181
             -replyto=>$cfg::config{notification}{from_infos}{replyto},
162
-            -to=>$cfg::config{notification}->{to}->{$dest}->{adress},
182
+            -to=>$cfg::config{notification}->{to}->{$dest}->{address},
163 183
             -subject=>$cfg::config{notification}->{to}->{$dest}->{subject},
164 184
             -contenttype=>'text/html',
165 185
             -body=>$data
+81 -4
sms/bot
... ...
@@ -287,9 +287,15 @@ sub xmpp_send_sms {
287 287
 }
288 288
 
289 289
 sub is_authorized {
290
-    my ($requestor) = @_;
291
-    my @results = sql_request("SELECT * FROM __authorized__ WHERE phone = '$requestor'");
292
-    return (scalar(@results) == 1);
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
+    }
293 299
 }
294 300
 
295 301
 sub authorized_on_table {
... ...
@@ -426,10 +432,81 @@ sub from_gtalksms_parse {
426 432
                 log_bot("envoi d'un pong à $request{phone} $request{phone_owner}");
427 433
                 $to_gtalksms_queue->enqueue([$request{phone}, "pong ($phone_status)"]);
428 434
             },
435
+            '^\s*test\s+intro\s+(\w+)\s*$' => sub {
436
+                my $intro = $1;
437
+                my $intro_file = $cfg::config{flag_prefix} . $intro . $cfg::config{flag_suffix};
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
+                }
449
+                if (-r $cfg::config{flagdir} . "/" . $cfg::config{flag}) {
450
+                    open FLAG, $cfg::config{flagdir} . "/" . $cfg::config{flag};
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}");
466
+                    mkdir $cfg::config{flagdir};
467
+                    chmod 0770, $cfg::config{flagdir};
468
+                    open(FLAG, ">" . $cfg::config{flagdir} . "/" . $cfg::config{flag});
469
+                    print FLAG "to:" . $request{address} . "\nintro:" . $intro . "\nfirstname:" . $request{phone} . " " . $request{phone_owner};
470
+                    close FLAG;
471
+                    chmod 0666, $cfg::config{flagdir} . "/" . $cfg::config{flag};
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
+            },
475
+            '^\s*action\s*$' => sub {
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
+                }
483
+                if (-r $cfg::config{flagdir} . "/" . $cfg::config{flag}) {
484
+                    open FLAG, $cfg::config{flagdir} . "/" . $cfg::config{flag};
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}");
499
+                    mkdir $cfg::config{flagdir};
500
+                    chmod 0770, $cfg::config{flagdir};
501
+                    open(FLAG, ">" . $cfg::config{flagdir} . "/" . $cfg::config{flag});
502
+                    chmod 0666, $cfg::config{flagdir} . "/" . $cfg::config{flag};
503
+                    $to_gtalksms_queue->enqueue([$request{phone}, "le mail des actions partira dans $time_left"]);
504
+                }
505
+            },
429 506
         );
430 507
 
431 508
         if (react_on_message(\%part_from_gtalksms, \%request)) {
432
-            if (defined $request{phone} and !(is_authorized($request{phone}) and react_on_message(\%part_from_user, \%request))) {
509
+            if (defined $request{phone} and !(is_authorized(\%request) and react_on_message(\%part_from_user, \%request))) {
433 510
                 $request{subject} = "SMS recu de $request{phone} $request{phone_owner}";
434 511
                 $mail_queue->enqueue(\%request);
435 512
             }