Showing 1 changed files with 59 additions and 102 deletions
+59 -102
sms/bot
... ...
@@ -187,34 +187,28 @@ sub sql_request ($) {
187 187
 
188 188
 sub massive_send_sms {
189 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
-        if ($to_gtalksms_queue->pending() > 0) {
193
-            $to_gtalksms_queue->insert(0, [$request->{phone}, "un envoi massif est déjà en cours, lancé par " . $cfg::config{last_sender} . ", reste " . $to_gtalksms_queue->pending() . ", annulation"]);
194
-            return;
190
+    if ($to_gtalksms_queue->pending() > 0) {
191
+        $to_gtalksms_queue->insert(0, [$request->{phone}, "un envoi massif est déjà en cours, lancé par " . $cfg::config{last_sender} . ", reste " . $to_gtalksms_queue->pending() . ", annulation"]);
192
+        return;
193
+    }
194
+    $cfg::config{last_sender} = $request->{phone_owner};
195
+    my @results = sql_request("SELECT phone, firstname, gender FROM " . $cfg::config{table} . " WHERE " . $cfg::config{group_prefix} . $request->{table} . " > '0'");
196
+    my $qty = scalar(@results);
197
+    my $start_msg = "envoi de " . $qty . " SMS (fin prévue entre ". strftime("%H:%M", localtime(time() + $qty * 60)) . " et " . strftime("%H:%M", localtime(time() + $qty * 90)) . ")";
198
+    $to_gtalksms_queue->enqueue([$request->{phone}, $start_msg]);
199
+    foreach my $contact (@results) {
200
+        $contact->{phone} =~ s/[\s\.]//g;
201
+        $_ = $request->{body};
202
+        s/\@prénom/$contact->{firstname}/g;
203
+        if ($contact->{gender} eq 'F') {
204
+            s/\@\(\s*(\w+)\s*,\s*\w+\s*\)/$1/g;
195 205
         }
196
-        $cfg::config{last_sender} = $request->{phone_owner};
197
-        my @results = sql_request("SELECT * FROM $request->{table}");
198
-        my $qty = scalar(@results);
199
-        my $start_msg = "envoi de " . $qty . " SMS (fin prévue entre ". strftime("%H:%M", localtime(time() + $qty * 60)) . " et " . strftime("%H:%M", localtime(time() + $qty * 90)) . ")";
200
-        $to_gtalksms_queue->enqueue([$request->{phone}, $start_msg]);
201
-        foreach my $contact (@results) {
202
-            $contact->{phone} =~ s/[\s\.]//g;
203
-            $_ = $request->{body};
204
-            s/\@prénom/$contact->{firstname}/g;
205
-            if ($contact->{gender} eq 'F') {
206
-                s/\@\(\s*(\w+)\s*,\s*\w+\s*\)/$1/g;
207
-            }
208
-            else {
209
-                s/\@\(\s*\w+\s*,\s*(\w+)\s*\)/$1/g;
210
-            }
211
-            $to_gtalksms_queue->enqueue([$contact->{phone}, $_])
206
+        else {
207
+            s/\@\(\s*\w+\s*,\s*(\w+)\s*\)/$1/g;
212 208
         }
213
-        $to_gtalksms_queue->enqueue([$request->{phone}, "envoi des SMS terminé !"]);
214
-    }
215
-    else {
216
-        $to_gtalksms_queue->enqueue([$request->{phone}, "pas la bonne base $request->{table} pour $request->{phone}"]);
209
+        $to_gtalksms_queue->enqueue([$contact->{phone}, $_])
217 210
     }
211
+    $to_gtalksms_queue->enqueue([$request->{phone}, "envoi des SMS terminé !"]);
218 212
 }
219 213
 
220 214
 sub control_sms_flow {
... ...
@@ -283,10 +277,9 @@ sub wait_open_time ($$) {
283 277
 
284 278
 sub xmpp_send_sms {
285 279
     while (my $ref = $to_gtalksms_queue->dequeue) {
286
-        wait_open_time(21, 8); # pas d'envoi entre 21h et 8h
280
+        wait_open_time($cfg::config{close_hour}, $cfg::config{begin_hour}); # pas d'envoi entre 21h et 8h
287 281
 #        TODO lock($next_sms);
288 282
 #        TODO $next_sms = $$ref[0];
289
-#        control_sms_flow(\$$ref[1], 30, 1800); # pas plus de 30 messages de 140 caractères par demi-heure
290 283
         xmpp_send(\"sms:$$ref[0]:$$ref[1]");
291 284
         log_bot("envoi à $$ref[0] : $$ref[1]");
292 285
         sleep 60 + int(rand(30)); # en attendant la maîtrise de cond_wait et cond_signal: cond_signal() called on unlocked variable at ./bot line 348
... ...
@@ -296,79 +289,46 @@ sub xmpp_send_sms {
296 289
 
297 290
 sub is_authorized {
298 291
     my ($request) = @_;
299
-    my @results = sql_request("SELECT * FROM __authorized__ WHERE phone = '$request->{phone}'");
292
+    my @results = sql_request("SELECT * FROM $cfg::config{table} WHERE phone = '$request->{phone}'");
300 293
     if (scalar(@results) == 1) {
301
-        $request->{address} = $results[0]->{address};
302
-        return 1;
294
+        my $_results = $results[0];
295
+        foreach my $column (keys(%$_results)) {
296
+            if ($column =~ /^$cfg::config{group_prefix}/) {
297
+                if ($results[0]->{$column} == 2) {
298
+                    $request->{address} = $results[0]->{address};
299
+                    return 1; # true
300
+                }
301
+            }
302
+        }
303
+        return 0; # false
303 304
     }
304 305
     else {
305
-        return 0;
306
+        return 0; # false
306 307
     }
307 308
 }
308 309
 
309 310
 sub authorized_on_table {
310 311
     my %request = @_;
311
-    $request{writeable} = (defined($request{writeable})) ? "AND write_auth = '1'" : "";
312
-    my @results = sql_request("SELECT * FROM __tables__ WHERE table_id = '$request{table}' AND phone = '$request{id}' $request{writeable}");
313
-    if (scalar(@results) == 0) {
314
-        my @list = sql_request("SELECT * FROM __tables__ WHERE phone = '$request{id}'");
315
-        my $list_str = "";
316
-        foreach (@list) {
317
-            $list_str .= "\n- $_->{table_id}";
312
+    my $table_name = $cfg::config{group_prefix} . $request{groupe};
313
+    my @results = sql_request("SELECT * FROM $cfg::config{table} WHERE phone = '$request{id}'");
314
+    if (scalar(@results) == 1) {
315
+        if (! defined($results[0]->{$table_name})) {
316
+            $request{error} = "le groupe $request{groupe} n'existe pas";
317
+            $to_gtalksms_queue->insert(0, [$request{id}, $request{error}]);
318
+            return 0; # false
318 319
         }
319
-        ${$request{error}} = sprintf("%s%s%s%s", 
320
-                "désolé, $request{table} n'est pas autorisé pour toi", 
321
-                ($request{writeable} ne "")? " en écriture" : "", 
322
-                "... ou n'existe pas:",
323
-                $list_str);
324
-    }
325
-    return (scalar(@results) == 1);
326
-}
327
-
328
-sub is_table {
329
-    my %request = @_;
330
-    my @results = sql_request("SHOW TABLES WHERE Tables_in_sms = '$request{table}'");
331
-    if (scalar(@results) == 0) {
332
-        ${$request{error}} = "$request{table} n'existe pas... essaie la commande 'liste' pour voir tes tables";
333 320
     }
334
-    return (scalar(@results) == 1);
335
-}
336
-
337
-sub copy {
338
-    my $request = shift;
339
-    my $rmsg = "";
340
-    if (authorized_on_table(table => $request->{origin}, id => $request->{phone}, error => \$rmsg)) {
341
-        if (! is_table(table => $request->{destination}, error => \$rmsg)) {
342
-            sql_request("CREATE TABLE $request->{destination} SELECT * FROM $request->{origin}");
343
-            sql_request("INSERT INTO __tables__ (phone,table_id,creation_date,update_date,comment,write_auth) values('"
344
-                    . $request->{phone} . "','"
345
-                    . $request->{destination} . "','"
346
-                    . strftime("%Y-%m-%d %H:%M:%S", localtime) . "','"
347
-                    . strftime("%Y-%m-%d %H:%M:%S", localtime) . "','"
348
-                    . $request->{comment} . "','1')"
349
-                    );
350
-            $rmsg = "copie de $request->{origin} vers $request->{destination} faite";
351
-        }
352
-        else { # on change le message d'erreur fourni par is_table()
353
-            $rmsg = "$request->{destination} existe déjà !"; 
321
+    @results = sql_request("SELECT $table_name FROM $cfg::config{table} WHERE phone = '$request{id}'");
322
+    if (scalar(@results) == 1) {
323
+        if ($results[0]->{$table_name} != 2) {
324
+            $request{error} = "désolé, écrire au groupe $request{groupe} n'est pas autorisé pour toi";
325
+            $to_gtalksms_queue->insert(0, [$request{id}, $request{error}]);
326
+            return 0; # false
354 327
         }
355
-    }
356
-    $to_gtalksms_queue->enqueue([$request->{phone}, $rmsg]);
357
-}
358
-
359
-sub insert {
360
-    my $request = shift;
361
-    my $rmsg = "";
362
-    if (authorized_on_table(table => $request->{table}, id => $request->{phone}, writeable => 1, error => \$rmsg)) {
363
-        my $contact_number = 0;
364
-        foreach my $contact (split("\n", $request->{body})) {
365
-            my ($firstname, $lastname, $phone, $gender) = split(";", $contact);
366
-            sql_request("INSERT INTO $request->{table} (firstname,lastname,phone,gender) values ($firstname, $lastname, $phone, $gender)");
367
-            $contact_number++;
328
+        else {
329
+            return 1; # true
368 330
         }
369
-        $rmsg = "$contact_number contacts ajoutés dans $request->{table}"
370 331
     }
371
-    $to_gtalksms_queue->enqueue([$request->{phone}, $rmsg]);
372 332
 }
373 333
 
374 334
 sub react_on_message {
... ...
@@ -392,11 +352,11 @@ sub from_gtalksms_parse {
392 352
     while (my $msg = $from_gtalksms_queue->dequeue) {
393 353
         my %request = (body => $msg->GetBody);
394 354
         my %part_from_gtalksms = (
395
-            $cfg::gtalksms{"from"} => sub { # Message de +33612345678 :
355
+            $cfg::gtalksms{from} => sub { # Message de +33612345678 :
396 356
                 $request{phone} = "0" . $1;
397 357
                 $request{phone_owner} = '';
398 358
                 $request{email} = '';
399
-                my @results = sql_request("SELECT * FROM section WHERE phone = '$request{phone}'");
359
+                my @results = sql_request("SELECT * FROM " . $cfg::config{table} . " WHERE phone = '$request{phone}'");
400 360
                 my $number_of_candidates = @results;
401 361
                 $number_of_candidates == 0 and return;
402 362
                 $request{phone_owner} .= '(';
... ...
@@ -405,7 +365,7 @@ sub from_gtalksms_parse {
405 365
                     --$number_of_candidates > 0 and $request{phone_owner} .= ' ou ';
406 366
                     $request{email} = $_->{email};
407 367
                 } 
408
-                $request{"phone_owner"} .= ')';
368
+                $request{phone_owner} .= ')';
409 369
                 log_bot("message de $request{phone} $request{phone_owner}");
410 370
             },
411 371
             $cfg::gtalksms{"delivered"} => sub { # SMS "un contenu de SMS" pour 0612345678 délivré.
... ...
@@ -460,21 +420,13 @@ sub from_gtalksms_parse {
460 420
             },
461 421
             $cfg::user{"message de groupe"} => sub {
462 422
                 $request{table} = lc $1;
463
-                &massive_send_sms(\%request);
423
+                if (authorized_on_table(groupe => $request{table}, id => $request{phone})) {
424
+                    &massive_send_sms(\%request);
425
+                }
464 426
             },
465 427
             $cfg::user{"message pour un destinataire"} => sub {
466 428
                 $to_gtalksms_queue->enqueue([$1, $2]);
467 429
             },
468
-            $cfg::user{"copie d'une base"} => sub {
469
-                $request{origin} = lc $1;
470
-                $request{destination} = lc $2;
471
-                $request{comment} = $3 =~ s/^\s*//r;
472
-                &copy(\%request);
473
-            },
474
-            $cfg::user{"ajout d'une entrée dans un base"} => sub {
475
-                $request{table} = lc $1;
476
-                &insert(\%request);
477
-            },
478 430
             $cfg::user{"ping"} => sub {
479 431
                 my $envoi_en_cours = "";
480 432
                 if ($to_gtalksms_queue->pending() > 0) {
... ...
@@ -547,5 +499,10 @@ $roster->add($cfg::config{xmpp}->{phoneBuddy});
547 499
 while(defined($bot->Process())) { 
548 500
     $bot->RosterGet();
549 501
     my $status = $roster->query($cfg::config{xmpp}->{phoneBuddy},'resources');
550
-    $phone_status = $status->{GTalkSMS}->{status} =~ s/^GTalkSMS - //r if $status;
502
+    if ($status) {
503
+        $phone_status = $status->{GTalkSMS}->{status} =~ s/^GTalkSMS - //r;
504
+    }
505
+    else {
506
+        $phone_status = "";
507
+    }
551 508
 }