initial commit
|
1 |
#!/usr/bin/perl |
2 | ||
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
6 |
use DBI; |
|
7 |
use POSIX qw/ceil strftime/; |
|
changement de moyen de commu...
|
8 |
use HiPi::Huawei::E3531; |
9 |
use HiPi::Huawei::Errors; |
|
initial commit
|
10 | |
11 |
use open ':utf8'; |
|
12 |
use open ':std'; |
|
13 |
use utf8; |
|
changement de moyen de commu...
|
14 | |
fix problème avec dequeue co...
|
15 |
#use Data::Dumper; |
initial commit
|
16 | |
17 |
use threads; |
|
18 |
use threads::shared; |
|
19 |
use Thread::Queue; |
|
changement de moyen de commu...
|
20 |
use Thread::Semaphore; |
initial commit
|
21 | |
changement de moyen de commu...
|
22 |
my $scriptconf = $0; |
initial commit
|
23 |
$scriptconf =~ s/\.pl$//; |
24 |
$scriptconf =~ s/$/.conf/; |
|
25 |
if (-r $scriptconf) { |
|
26 |
package cfg; |
|
27 |
unless (my $return = do $scriptconf) { |
|
28 |
warn "couldn't parse $scriptconf: $@" if $@; |
|
29 |
warn "couldn't do $scriptconf: $!" unless defined $return; |
|
30 |
warn "couldn't run $scriptconf" unless $return; |
|
31 |
} |
|
32 |
} |
|
33 |
else { |
|
34 |
print "pas de config\n"; |
|
35 |
exit; |
|
36 |
} |
|
37 | ||
38 |
$SIG{INT}=\&terminate; |
|
39 | ||
changement de moyen de commu...
|
40 |
my $inbox_sms_queue = Thread::Queue->new; |
41 |
my $outbox_sms_queue = Thread::Queue->new; |
|
initial commit
|
42 |
my $mail_queue = Thread::Queue->new; |
43 | ||
changement de moyen de commu...
|
44 |
my $mutex = Thread::Semaphore->new(); |
45 |
STDERR->autoflush(); |
|
initial commit
|
46 | |
changement de moyen de commu...
|
47 |
my $mail = threads->new(\&send_mail); |
initial commit
|
48 | |
changement de moyen de commu...
|
49 |
my $inbox_sms = threads->new(\&inbox_sms_parse); |
initial commit
|
50 | |
changement de moyen de commu...
|
51 |
my $outbox_sms = threads->new(\&hilink_send_sms); |
initial commit
|
52 | |
changement de moyen de commu...
|
53 |
my $hlink = HiPi::Huawei::E3531->new(); |
initial commit
|
54 | |
55 |
sub log_bot { |
|
changement de moyen de commu...
|
56 |
$mutex->down(); |
57 |
print STDERR sprintf("%s\n", shift); |
|
58 |
$mutex->up(); |
|
initial commit
|
59 |
} |
60 | ||
61 |
sub terminate () { |
|
changement de moyen de commu...
|
62 |
log_bot($mail_queue->pending() . " mails en attente d'envoi !") if ($mail_queue->pending() > 0); |
63 |
$mail->exit(); |
|
64 |
log_bot($inbox_sms_queue->pending() . " SMS en attente de traitement !") if ($inbox_sms_queue->pending() > 0); |
|
65 |
$inbox_sms->exit(); |
|
66 |
log_bot($outbox_sms_queue->pending() . " SMS en attente d'envoi !") if ($outbox_sms_queue->pending() > 0); |
|
67 |
$outbox_sms->exit(); |
|
68 |
log_bot("arrêt"); |
|
initial commit
|
69 |
exit 0; |
70 |
} |
|
71 | ||
72 |
sub sql_request ($) { |
|
changement de moyen de commu...
|
73 |
my $msg = shift; |
initial commit
|
74 |
my @result; |
75 |
my $dbh = DBI->connect($cfg::config{db}->{driver}, $cfg::config{db}->{user}, $cfg::config{db}->{password}, {'RaiseError' => 1}); |
|
76 |
$dbh->{'mysql_enable_utf8'} = 1; |
|
77 |
$dbh->do(qq{SET NAMES "utf8"}); |
|
changement de moyen de commu...
|
78 |
my $sth = $dbh->prepare($msg); |
initial commit
|
79 |
$sth->execute(); |
80 |
if (defined($sth->{NUM_OF_FIELDS})) { |
|
81 |
while (my $ref = $sth->fetchrow_hashref()) { |
|
82 |
push @result, $ref; |
|
83 |
} |
|
84 |
} |
|
85 |
$sth->finish(); |
|
86 |
$dbh->disconnect(); |
|
87 |
return @result; |
|
88 |
} |
|
89 | ||
90 |
sub massive_send_sms { |
|
changement de moyen de commu...
|
91 |
my $msg = shift; |
92 |
if ($outbox_sms_queue->pending() > 0) { |
|
93 |
$outbox_sms_queue->insert(0, [$msg->{Phone}, "un envoi massif est déjà en cours, lancé par " . $cfg::config{last_sender} . ", reste " . $outbox_sms_queue->pending() . ", annulation"]); |
|
simplification table + répon...
|
94 |
return; |
95 |
} |
|
changement de moyen de commu...
|
96 |
$cfg::config{last_sender} = $msg->{PhoneOwner}; |
97 |
my @results = sql_request("SELECT phone, firstname, gender FROM " . $cfg::config{table} . " WHERE " . $cfg::config{group_prefix} . $msg->{groupe} . " > '0'"); |
|
simplification table + répon...
|
98 |
my $qty = scalar(@results); |
99 |
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)) . ")"; |
|
changement de moyen de commu...
|
100 |
$outbox_sms_queue->enqueue([$msg->{Phone}, $start_msg]); |
simplification table + répon...
|
101 |
foreach my $contact (@results) { |
102 |
$contact->{phone} =~ s/[\s\.]//g; |
|
changement de moyen de commu...
|
103 |
$_ = $msg->{Content}; |
simplification table + répon...
|
104 |
s/\@prénom/$contact->{firstname}/g; |
105 |
if ($contact->{gender} eq 'F') { |
|
106 |
s/\@\(\s*(\w+)\s*,\s*\w+\s*\)/$1/g; |
|
réponse prioritaire au ping ...
|
107 |
} |
simplification table + répon...
|
108 |
else { |
109 |
s/\@\(\s*\w+\s*,\s*(\w+)\s*\)/$1/g; |
|
initial commit
|
110 |
} |
changement de moyen de commu...
|
111 |
$outbox_sms_queue->enqueue([$contact->{phone}, $_]) |
initial commit
|
112 |
} |
changement de moyen de commu...
|
113 |
$outbox_sms_queue->enqueue([$msg->{Phone}, "envoi des SMS terminé !"]); |
initial commit
|
114 |
} |
115 | ||
changement de moyen de commu...
|
116 |
sub send_mail { |
fix problème avec dequeue co...
|
117 |
while (1) { |
118 |
my $msg = $mail_queue->dequeue_timed(10, 1); |
|
119 |
next if ! defined $msg; |
|
changement de moyen de commu...
|
120 |
defined($msg->{to}) or $msg->{to} = $cfg::config{mail}; |
121 |
open(MAIL, "|msmtp $msg->{to}"); |
|
122 |
print MAIL "Subject: $msg->{Subject}\n"; |
|
123 |
print MAIL "Reply-To: $msg->{Email}\n" if ($msg->{Email} ne ''); |
|
initial commit
|
124 | |
changement de moyen de commu...
|
125 |
print MAIL "$msg->{Content}\n"; |
initial commit
|
126 |
close(MAIL); |
changement de moyen de commu...
|
127 |
log_bot("mail envoyé à $msg->{to}"); |
initial commit
|
128 |
} |
129 |
} |
|
130 | ||
131 |
sub wait_open_time ($$) { |
|
132 |
my ($close_hour, $open_hour) = @_; |
|
fix: heures silencieuses
|
133 |
my @lt = localtime; |
134 |
while ($lt[2] >= $close_hour or $lt[2] <= $open_hour) { |
|
initial commit
|
135 |
sleep 1800; |
136 |
} |
|
137 |
} |
|
138 | ||
changement de moyen de commu...
|
139 |
sub hilink_send_sms { |
140 |
my $sendbox = HiPi::Huawei::E3531->new(); |
|
141 |
while (1) { |
|
142 |
my $sms = $outbox_sms_queue->dequeue_timed(10, 1); |
|
143 |
next if ! defined $sms; |
|
144 | ||
145 |
if ($sendbox->{code}) { |
|
146 |
log_bot('sendbox new: ' . HiPi::Huawei::Errors->get_error_message($sendbox->{code})); |
|
147 |
} |
|
148 |
else { |
|
149 |
my $reponse = $sendbox->send_sms($$sms[0], , $$sms[1]); |
|
150 |
if ($reponse->{code}) { |
|
151 |
log_bot('send_sms: ' . HiPi::Huawei::Errors->get_error_message($reponse->{code})); |
|
152 |
} |
|
153 | ||
154 |
log_bot("envoi à $$sms[0] : $$sms[1]"); |
|
155 |
} |
|
156 |
sleep 60 + int(rand(30)); |
|
initial commit
|
157 |
} |
changement de moyen de commu...
|
158 |
log_bot('fin du thread outbox_sms'); |
initial commit
|
159 |
} |
160 | ||
161 |
sub is_authorized { |
|
changement de moyen de commu...
|
162 |
my ($msg) = @_; |
163 |
my @results = sql_request("SELECT * FROM $cfg::config{table} WHERE phone = '$msg->{Phone}'"); |
|
sms/action: envoi du mail de...
|
164 |
if (scalar(@results) == 1) { |
simplification table + répon...
|
165 |
my $_results = $results[0]; |
166 |
foreach my $column (keys(%$_results)) { |
|
167 |
if ($column =~ /^$cfg::config{group_prefix}/) { |
|
168 |
if ($results[0]->{$column} == 2) { |
|
changement de moyen de commu...
|
169 |
$msg->{address} = $results[0]->{address}; |
simplification table + répon...
|
170 |
return 1; # true |
171 |
} |
|
172 |
} |
|
173 |
} |
|
174 |
return 0; # false |
|
sms/action: envoi du mail de...
|
175 |
} |
176 |
else { |
|
simplification table + répon...
|
177 |
return 0; # false |
sms/action: envoi du mail de...
|
178 |
} |
initial commit
|
179 |
} |
180 | ||
181 |
sub authorized_on_table { |
|
changement de moyen de commu...
|
182 |
my %msg = @_; |
183 |
my $table_name = $cfg::config{group_prefix} . $msg{groupe}; |
|
184 |
my @results = sql_request("SELECT * FROM $cfg::config{table} WHERE phone = '$msg{id}'"); |
|
simplification table + répon...
|
185 |
if (scalar(@results) == 1) { |
186 |
if (! defined($results[0]->{$table_name})) { |
|
changement de moyen de commu...
|
187 |
$msg{error} = "le groupe $msg{groupe} n'existe pas"; |
188 |
$outbox_sms_queue->insert(0, [$msg{id}, $msg{error}]); |
|
simplification table + répon...
|
189 |
return 0; # false |
initial commit
|
190 |
} |
191 |
} |
|
changement de moyen de commu...
|
192 |
@results = sql_request("SELECT $table_name FROM $cfg::config{table} WHERE phone = '$msg{id}'"); |
simplification table + répon...
|
193 |
if (scalar(@results) == 1) { |
194 |
if ($results[0]->{$table_name} != 2) { |
|
changement de moyen de commu...
|
195 |
$msg{error} = "désolé, écrire au groupe $msg{groupe} n'est pas autorisé pour toi"; |
196 |
$outbox_sms_queue->insert(0, [$msg{id}, $msg{error}]); |
|
simplification table + répon...
|
197 |
return 0; # false |
initial commit
|
198 |
} |
simplification table + répon...
|
199 |
else { |
200 |
return 1; # true |
|
initial commit
|
201 |
} |
202 |
} |
|
203 |
} |
|
204 | ||
205 |
sub react_on_message { |
|
changement de moyen de commu...
|
206 |
my ($hashref , $msg) = @_; |
initial commit
|
207 |
for my $regex (keys(%$hashref)) { |
changement de moyen de commu...
|
208 |
if ($msg->{Content} =~ m/$regex/i) { |
209 |
$msg->{Content} =~ s/$regex//i; |
|
initial commit
|
210 |
$hashref->{$regex}(); |
211 |
return 1; # true |
|
212 |
} |
|
213 |
} |
|
214 |
return 0; # false |
|
215 |
} |
|
216 | ||
changement de moyen de commu...
|
217 |
sub inbox_sms_parse { |
fix problème avec dequeue co...
|
218 |
while (1) { |
219 |
my $msg = $inbox_sms_queue->dequeue_timed(10, 1); |
|
220 |
next if ! defined $msg; |
|
signal d'envoi immédiat du m...
|
221 | |
initial commit
|
222 |
my %part_from_user = ( |
passage des regex dans le fi...
|
223 |
$cfg::user{"message de groupe"} => sub { |
changement de moyen de commu...
|
224 |
$msg->{groupe} = lc $1; |
225 |
if (authorized_on_table(groupe => $msg->{groupe}, id => $msg->{Phone})) { |
|
226 |
&massive_send_sms(\%$msg); |
|
simplification table + répon...
|
227 |
} |
initial commit
|
228 |
}, |
passage des regex dans le fi...
|
229 |
$cfg::user{"message pour un destinataire"} => sub { |
changement de moyen de commu...
|
230 |
$outbox_sms_queue->enqueue([$1, $msg->{Content}]); |
initial commit
|
231 |
}, |
passage des regex dans le fi...
|
232 |
$cfg::user{"ping"} => sub { |
changement de moyen de commu...
|
233 |
my $envoi_en_cours = ''; |
234 |
if ($outbox_sms_queue->pending() > 0) { |
|
fix problème avec dequeue co...
|
235 |
# for (my $queue_id = $outbox_sms_queue->pending(); $queue_id >= 0; $queue_id--) { |
236 |
# log_bot(Dumper($outbox_sms_queue->peek($queue_id))); |
|
237 |
# } |
|
changement de moyen de commu...
|
238 |
$envoi_en_cours = "\nenvoi en cours de traitement (reste " . $outbox_sms_queue->pending() . ") "; |
239 |
$envoi_en_cours .= "par $cfg::config{last_sender}" if defined($cfg::config{last_sender}); |
|
sms/action: envoi du mail de...
|
240 |
} |
changement de moyen de commu...
|
241 |
log_bot("envoi d'un pong à $msg->{Phone} $msg->{PhoneOwner} $envoi_en_cours"); |
242 |
$outbox_sms_queue->insert(0, [$msg->{Phone}, "pong" . $envoi_en_cours]); |
|
sms/action: envoi du mail de...
|
243 |
}, |
initial commit
|
244 |
); |
245 | ||
changement de moyen de commu...
|
246 |
$msg->{Phone} =~ s/^\+33/0/; |
247 |
$msg->{Email} = ''; |
|
248 | ||
249 |
my @results = sql_request("SELECT * FROM " . $cfg::config{table} . " WHERE phone = '$msg->{Phone}'"); |
|
250 |
my $number_of_candidates = @results; |
|
251 |
$number_of_candidates == 0 and return; |
|
252 |
$msg->{PhoneOwner} = '('; |
|
253 |
foreach (@results) { |
|
254 |
$msg->{PhoneOwner} .= "$_->{firstname} $_->{lastname}"; |
|
255 |
--$number_of_candidates > 0 and $msg->{PhoneOwner} .= ' ou '; |
|
256 |
$msg->{Email} = $_->{email}; |
|
initial commit
|
257 |
} |
changement de moyen de commu...
|
258 |
$msg->{PhoneOwner} .= ')'; |
259 |
$msg->{Subject} = "SMS recu de $msg->{Phone} $msg->{PhoneOwner}"; |
|
260 | ||
261 |
if (defined $msg->{Phone} and !(is_authorized(\%$msg) and react_on_message(\%part_from_user, \%$msg))) { |
|
262 |
log_bot("message de $msg->{Phone} $msg->{PhoneOwner}"); |
|
263 |
$mail_queue->enqueue(\%$msg); |
|
initial commit
|
264 |
} |
265 | ||
changement de moyen de commu...
|
266 |
undef $msg; |
initial commit
|
267 |
} |
268 |
} |
|
269 | ||
changement de moyen de commu...
|
270 |
$inbox_sms->detach; # gère la file des événements produits par GTalkSMS |
271 |
log_bot("inbox thread ok"); |
|
272 |
$outbox_sms->detach; # gère la file des envois de SMS par GTalkSMS |
|
273 |
log_bot("outbox thread ok"); |
|
274 |
$mail->detach; # gère la file des mails |
|
275 |
log_bot("mail thread ok"); |
|
initial commit
|
276 | |
changement de moyen de commu...
|
277 |
my $loop = 0; |
278 |
log_bot("robot prêt"); |
|
279 |
while ( 1 ) { |
|
initial commit
|
280 | |
changement de moyen de commu...
|
281 |
$loop++; |
282 | ||
283 |
my $notifications = $hlink->check_notifications(); |
|
284 |
if ($notifications->{code}) { |
|
285 |
log_bot('check_notifications: ' . HiPi::Huawei::Errors->get_error_message($notifications->{code})); |
|
simplification table + répon...
|
286 |
} |
changement de moyen de commu...
|
287 |
#TODO elsif ($notifications->{OnlineUpdateStatus} != 10) { |
288 |
#TODO trouver les significations |
|
289 |
#TODO } |
|
290 |
elsif ($notifications->{UnreadMessage}) { |
|
291 |
my $inbox = $hlink->get_inbox(); |
|
292 |
if ($inbox->{code}) { |
|
293 |
log_bot('get_inbox: ' . HiPi::Huawei::Errors->get_error_message($inbox->{code})); |
|
294 |
} |
|
295 |
elsif (defined $inbox->{Count} and $inbox->{Count} > 0) { |
|
296 |
# log_bot($inbox->{Count} . " messages dans inbox"); |
|
297 |
for (my $i = 0; $i < $inbox->{Count}; $i++) { |
|
298 |
# log_bot("id " . $inbox->{Messages}[$i]->{Index} . ", status: " . $inbox->{Messages}[$i]->{Smstat}); |
|
299 |
if ($inbox->{Messages}[$i]->{Smstat}) { # message lu |
|
300 |
my $delete = $hlink->delete_sms($inbox->{Messages}[$i]->{Index}); |
|
301 |
if ($delete->{code}) { |
|
302 |
log_bot('delete_sms: ' . HiPi::Huawei::Errors->get_error_message($delete->{code})); |
|
303 |
} |
|
304 |
# log_bot("id " . $inbox->{Messages}[$i]->{Index} . " deleted"); |
|
305 |
} |
|
306 |
else { |
|
307 |
$inbox_sms_queue->enqueue($inbox->{Messages}[$i]); |
|
308 |
# log_bot("id " . $inbox->{Messages}[$i]->{Index} . " enqueued"); |
|
309 |
my $read = $hlink->set_sms_read($inbox->{Messages}[$i]->{Index}); |
|
310 |
if ($read->{code}) { |
|
311 |
log_bot('set_sms_read: ' . HiPi::Huawei::Errors->get_error_message($read->{code})); |
|
312 |
} |
|
313 |
# log_bot("id " . $inbox->{Messages}[$i]->{Index} . " marked as read") if defined $inbox->{Messages}[$i]->{Index}; |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
} |
|
318 | ||
319 |
#on vérifie toutes les 30 secondes |
|
320 |
sleep 30; |
|
321 |
log_bot('outbox_sms not running') unless ($outbox_sms->is_running()); |
|
322 |
next if ($loop % 20); |
|
323 |
$loop = 0; |
|
324 | ||
325 |
# nettoyage des envoyés toutes les 10 minutes |
|
326 |
my $outbox = $hlink->get_outbox(); |
|
327 |
if ($outbox->{code}) { |
|
328 |
log_bot('get_outbox: ' . HiPi::Huawei::Errors->get_error_message($outbox->{code})); |
|
329 |
} |
|
330 |
elsif (defined $outbox->{Count} and $outbox->{Count} > 1) { |
|
331 |
# log_bot($outbox->{Count} . " messages dans outbox"); |
|
332 |
# on conserve le dernier envoyé |
|
333 |
for (my $i = 1; $i < $outbox->{Count}; $i++) { |
|
334 |
my $delete = $hlink->delete_sms($outbox->{Messages}[$i]->{Index}); |
|
335 |
if ($delete->{code}) { |
|
336 |
log_bot('delete_sms: ' . HiPi::Huawei::Errors->get_error_message($delete->{code})); |
|
337 |
} |
|
338 |
# log_bot("id " . $outbox->{Messages}[$i]->{Index} . " deleted"); |
|
339 |
} |
|
simplification table + répon...
|
340 |
} |
initial commit
|
341 |
} |