Showing 1 changed files with 55 additions and 53 deletions
+55 -53
sms/bot
... ...
@@ -406,6 +406,43 @@ sub from_gtalksms_parse {
406 406
                 return;
407 407
             },
408 408
         );
409
+
410
+        my %sms_action = (
411
+            "create flag" => sub {
412
+                my ($flag) = @_;
413
+                my $gid = getgrnam("sms-action");
414
+                mkdir $cfg::sms_action{flagdir};
415
+                chmod 0770, $cfg::sms_action{flagdir};
416
+                chown $>, $gid, $cfg::sms_action{flagdir};
417
+
418
+                open(FLAG, ">" . $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag});
419
+                chmod 0660, $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag};
420
+                chown $>, $gid, $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag};
421
+
422
+                print FLAG $$flag if defined $flag;
423
+                close FLAG;
424
+            },
425
+            "start action" => sub {
426
+                # 0 = false
427
+                return ! system("sudo /bin/systemctl start action.service");
428
+            },
429
+            "get locker" => sub {
430
+                open FLAG, $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag};
431
+                my $locker = '';
432
+                my $intro_ = '';
433
+                while (<FLAG>) {
434
+                    if (/^firstname:(.+)$/) {
435
+                        $locker = " par " . $1;
436
+                    }
437
+                    if (/^intro:(.+)$/) {
438
+                        $intro_ = " de test ($1)";
439
+                    }
440
+                }
441
+                close FLAG;
442
+                return "l'envoi " . $intro_ . " est déjà prévu" . $locker . ", abandon";
443
+            },
444
+        );
445
+
409 446
         my %part_from_user = (
410 447
             $cfg::user{"stop"} => sub {
411 448
                 log_bot("arrêt demandé par $request{phone} $request{phone_owner}");
... ...
@@ -434,73 +471,38 @@ sub from_gtalksms_parse {
434 471
             },
435 472
             $cfg::user{"test intro pour action"} => sub {
436 473
                 my $intro = $1;
437
-                my $intro_file = $cfg::sms_action{flag_prefix} . $intro . $cfg::sms_action{flag_suffix};
438
-                if (! -r $intro_file) {
474
+                if (! -r $cfg::sms_action{flag_prefix} . $intro . $cfg::sms_action{flag_suffix}) {
439 475
                     $to_gtalksms_queue->enqueue([$request{phone}, "l'intro $intro n'existe pas, abandon"]);
440 476
                     return;
441 477
                 }
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 478
                 if (-r $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag}) {
450
-                    open FLAG, $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{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"]);
479
+                    $to_gtalksms_queue->enqueue([$request{phone}, $sms_action{"get locker"}()]);
463 480
                 }
464 481
                 else {
465 482
                     log_bot("demande envoi du mail des actions avec intro $intro depuis $request{phone} $request{phone_owner} à $request{address}");
466
-                    mkdir $cfg::sms_action{flagdir};
467
-                    chmod 0770, $cfg::sms_action{flagdir};
468
-                    open(FLAG, ">" . $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag});
469
-                    print FLAG "to:" . $request{address} . "\nintro:" . $intro . "\nfirstname:" . $request{phone} . " " . $request{phone_owner};
470
-                    close FLAG;
471
-                    chmod 0666, $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag};
472
-                    $to_gtalksms_queue->enqueue([$request{phone}, "le mail de test des actions avec intro $intro partira dans $time_left sur $request{address}"]);
483
+                    my $flag_content = "to:" . $request{address} . "\nintro:" . $intro . "\nfirstname:" . $request{phone} . " " . $request{phone_owner};
484
+                    $sms_action{"create flag"}(\$flag_content);
485
+                    if ($sms_action{"start action"}()) {
486
+                        $to_gtalksms_queue->enqueue([$request{phone}, "le mail de test des actions avec intro $intro envoyé à $request{address}"]);
487
+                    }
488
+                    else {
489
+                        $to_gtalksms_queue->enqueue([$request{phone}, "problème: le mail de test des actions avec intro $intro pour $request{address}, n'est probablement pas parti"]);
490
+                    }
473 491
                 }
474 492
             },
475 493
             $cfg::user{"envoi normal pour action"} => 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 494
                 if (-r $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{flag}) {
484
-                    open FLAG, $cfg::sms_action{flagdir} . "/" . $cfg::sms_action{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"]);
495
+                    $to_gtalksms_queue->enqueue([$request{phone}, $sms_action{"get locker"}()]);
496 496
                 }
497 497
                 else {
498 498
                     log_bot("demande envoi du mail des actions depuis $request{phone} $request{phone_owner}");
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};
503
-                    $to_gtalksms_queue->enqueue([$request{phone}, "le mail des actions partira dans $time_left"]);
499
+                    $sms_action{"create flag"}();
500
+                    if ($sms_action{"start action"}()) {
501
+                        $to_gtalksms_queue->enqueue([$request{phone}, "le mail des actions est parti"]);
502
+                    }
503
+                    else {
504
+                        $to_gtalksms_queue->enqueue([$request{phone}, "problème: le mail des actions n'est probablement pas parti"]);
505
+                    }
504 506
                 }
505 507
             },
506 508
         );