-
Notifications
You must be signed in to change notification settings - Fork 2
/
75_MSG.pm
executable file
·2544 lines (2264 loc) · 112 KB
/
75_MSG.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
# $Id$
#
#TODO
# - be able to use type "default" to let read from attr
# - forceType/forceDevice in user parameters
# - implement default messages in RESIDENTS using msg command
# - queue message until recipient is available again (e.g. when absent)
# also see https://forum.fhem.de/index.php/topic,69683.0.html
# - new msgType "queue"
# - escalation to type "queue" when n/a
# - automatically trigger to release queue messages by arriving at home
# (ROOMMATE)
# - allow some other ? to only reach people when they are at home
# - if ROOMMATE is asleep, queue message for next day
# (usefull escalate for screen with PostMe?)
# - delivery options as attributes (like ! or ? to gateways, devices or types)
# - all messages should be queued and then delivered so a timer may come back
# and check the gateway device for successful delivery
# - fix readings: store texts with \x{0000} notation
# - end user documentation / commandref !!
#
package main;
use strict;
use warnings;
use Data::Dumper;
use Time::HiRes qw(time);
use Encode;
use utf8;
# initialize ##################################################################
sub MSG_Initialize($$) {
my %hash = (
Fn => "CommandMsg",
Hlp =>
"[<type>] [<\@device>|<e-mail address>] [<priority>] [|<title>|] <message-text>",
);
$cmds{msg} = \%hash;
require "$attr{global}{modpath}/FHEM/msgSchema.pm";
}
# regular Fn ##################################################################
sub CommandMsg($$;$$);
sub CommandMsg($$;$$) {
my ( $cl, $msg, $testMode ) = @_;
my $return = "";
if ( $featurelevel >= 5.7 ) {
my %dummy;
my ( $err, @a ) = ReplaceSetMagic( \%dummy, 0, ($msg) );
$msg = join( " ", @a )
unless ($err);
}
# find existing msgConfig device or create a new instance
my $globalDevName = "globalMsg";
if ( defined( $modules{msgConfig}{defptr} ) ) {
$globalDevName = $modules{msgConfig}{defptr}{NAME};
}
else {
fhem "define $globalDevName msgConfig";
$return .=
"Global configuration device $globalDevName was created.\n\n";
}
if ( $msg eq "" || $msg =~ /^\?[\s\t ]*$/ || $msg eq "help" ) {
return $return
. "Usage: msg [<type>] [<\@device>|<e-mail address>] [<priority>] [|<title>|] <message>";
}
# default settings
my $cmdSchema = msgSchema::get();
my $settings = {
audio => {
typeEscalation => {
gwUnavailable => 'text',
emergency => 'text',
residentGone => 'text',
residentAbsent => 'text',
},
},
light => {
typeEscalation => {
gwUnavailable => 'audio',
emergency => 'audio',
residentGone => 'audio',
residentAbsent => 'audio',
},
},
push => {
typeEscalation => {
gwUnavailable => 'mail',
emergency => 'mail',
},
},
screen => {
typeEscalation => {
gwUnavailable => 'light',
emergency => 'light',
residentGone => 'light',
residentAbsent => 'light',
},
},
};
################################################################
### extract message details
###
my ( $msgA, $params ) = parseParams( $msg, "[^\\S\\n]", " " );
# only use output from parseParams when
# parameters where found
if ( ref($params) eq "HASH" && keys %$params ) {
if ( scalar @$msgA > 0 ) {
$msg = join( " ", @$msgA );
}
else {
$msg = "";
}
}
if ( defined( $params->{msgText} ) ) {
Log3 $globalDevName, 5,
"msg: Adding message text from given user parameters";
$msg .= " " unless ( $msg eq "" );
$msg .= $params->{msgText};
delete $params->{msgText};
}
return $return
. "Usage: msg [<type>] [<\@device>|<e-mail address>] [<priority>] [|<title>|] <message>"
if ( $msg =~ m/^[\s\t\n ]*$/ );
Log3 $globalDevName, 5, "msg: Extracted user parameters\n" . Dumper($params)
if ( ref($params) eq "HASH" && keys %$params );
my $types = "";
my $recipients = "";
my $priority = "";
my $title = "-";
my $priorityCat = "";
# check for message types
if ( $params->{msgType} ) {
Log3 $globalDevName, 5, "msg: given types=$params->{msgType}";
$types = $params->{msgType};
$types =~ s/[\s\t ]+//g;
delete $params->{msgType};
}
elsif ( $msg =~
s/^[\s\t ]*([a-z,]*!?(screen|light|audio|text|push|mail|queue)[\w,!?&|]*)[\s\t ]+//i
)
{
Log3 $globalDevName, 5, "msg: found types=$1"
unless ( defined($testMode) && $testMode eq "1" );
$types = $1;
}
# programatic exception:
# e.g. recipients were given automatically from empty readings
if (
$msg =~ m/^[\s\t ]*([!]?(([A-Za-z0-9%+._-])*@([,\-:|]+)))[\s\t ]+/
|| ( $params->{msgRcpt}
&& $params->{msgRcpt} =~
m/^[\s\t ]*([!]?(([A-Za-z0-9%+._-])*@([,\-:|]+)))[\s\t ]+/ )
)
{
Log3 $globalDevName, 4,
"msg: message won't be sent - recipient '$1' contains special"
. " characters like ',-:|' or behind the @ character is simply"
. " emptiness. This might be okay, e.g. if you are using something"
. " like a reading from RESIDENTS/ROOMMATE/GUEST to address present"
. " or absent residents and this list is simply empty at this time."
. " ($msg)";
return;
}
# check for given recipients
if ( $params->{msgRcpt} ) {
Log3 $globalDevName, 5, "msg: given recipient=$params->{msgRcpt}";
$recipients = $params->{msgRcpt};
$recipients =~ s/[\s\t ]+//g;
delete $params->{msgRcpt};
}
elsif ( $msg =~
s/^[\s\t ]*([!]?(([A-Za-z0-9%+._-])*@([%+a-z0-9A-Z.-]+))[\w,@.!?|:]*)[\s\t ]+//
)
{
Log3 $globalDevName, 5, "msg: found recipient=$1"
unless ( defined($testMode) && $testMode eq "1" );
$recipients = $1;
}
# check for given priority
if ( defined( $params->{msgPrio} ) ) {
Log3 $globalDevName, 5, "msg: given priority=$params->{msgPrio}";
$priority = $params->{msgPrio};
$priority =~ s/[\s\t ]+//g;
delete $params->{msgPrio};
}
elsif ( $msg =~ s/^[\s\t ]*([-+]{0,1}\d+[\.\d]*)[\s\t ]*// ) {
Log3 $globalDevName, 5, "msg: found priority=$1"
unless ( defined($testMode) && $testMode eq "1" );
$priority = $1;
}
$priority = int($priority) if ( $priority =~ /^[-+]{0,1}\d+\.\d*$/ );
return "Invalid priority $priority: Needs to be an integer value"
unless ( $priority eq "" || $priority =~ /^[-+]{0,1}\d+$/ );
# check for given message title
if ( defined( $params->{msgTitle} ) ) {
Log3 $globalDevName, 5, "msg: given title=$params->{msgTitle}";
$title = $params->{msgTitle};
$title =~ s/^[\s\t ]*\|(.*?)\|[\s\t ]*/$1/;
delete $params->{msgTitle};
}
elsif ( $msg =~ s/^[\s\t ]*\|(.*?)\|[\s\t ]*// ) {
Log3 $globalDevName, 5, "msg: found title=$1"
unless ( defined($testMode) && $testMode eq "1" );
$title = $1;
}
# check for user parameters (DEPRECATED / legacy compatibility only)
if ( $msg =~ s/[\s\t ]*O(\[\{.*\}\])[\s\t ]*$// ) {
Log3 $globalDevName, 5, "msg: found options=$1"
unless ( defined($testMode) && $testMode eq "1" );
# Use JSON module if possible
eval {
require JSON::PP;
import JSON::PP qw( decode_json );
};
if ($@) {
Log3 $globalDevName, 3,
"msg: Error loading JSON::PP. "
. "Please switch to new syntax to use user parameters";
}
else {
Log3 $globalDevName, 4,
"msg: Please switch to new syntax to use user parameters";
my $o;
eval '$o = decode_json( encode_utf8($1) ); 1';
if ($@) {
Log3 $globalDevName, 5,
"msg: Error decoding JSON for user parameters: $@";
}
elsif ( ref($o) eq "ARRAY" ) {
for my $item (@$o) {
next unless ( ref($item) eq "HASH" );
for my $key ( keys(%$item) ) {
next if ( ref( $item->{$key} ) );
my $val = $item->{$key};
$params->{$key} = $item->{$key}
unless ( $params->{$key} );
}
}
Log3 $globalDevName, 5,
"msg: Decoded user parameters\n" . Dumper($params)
if ($params);
}
}
}
$types = "default"
if ( $types eq "" );
my $msgSent = 0;
my $forwarded = "";
my %sentTypesPerDevice;
my $sentCounter = 0;
my $msgID = time();
my $msgDateTime = TimeNow();
my $isTypeOr = 1;
my $isRecipientOr = 1;
my $hasTypeOr = 0;
my $hasRecipientOr = 0;
my $softFail = 0;
$recipients = "\@" . $globalDevName if ( $recipients eq "" );
################################################################
### recipient loop
###
my @recipientsOr = split( /\|/, $recipients );
Log3 $globalDevName, 6,
"msg: recipientOr total is " . scalar( grep { defined $_ } @recipientsOr )
unless ( defined($testMode) && $testMode eq "1" );
for (
my $iRecipOr = 0 ;
$iRecipOr < scalar( grep { defined $_ } @recipientsOr ) ;
$iRecipOr++
)
{
Log3 $globalDevName, 6,
"msg: "
. "running loop recipientsOr for recipient(s) $recipientsOr[$iRecipOr]"
unless ( defined($testMode) && $testMode eq "1" );
my $loopReturn1 = "";
$hasRecipientOr = 1
if ( scalar( grep { defined $_ } @recipientsOr ) > 1 );
my @recipient = split( /,/, $recipientsOr[$iRecipOr] );
foreach my $device (@recipient) {
Log3 $globalDevName, 6, "msg: running loop for device $device"
unless ( defined($testMode) && $testMode eq "1" );
my $loopReturn2 = "";
################################################################
### type loop
###
my @typesOr = split( /\|/, $types );
Log3 $globalDevName, 6,
"msg: typeOr total is " . scalar( grep { defined $_ } @typesOr )
unless ( defined($testMode) && $testMode eq "1" );
for (
my $iTypesOr = 0 ;
$iTypesOr < scalar( grep { defined $_ } @typesOr ) ;
$iTypesOr++
)
{
Log3 $globalDevName, 6,
"msg: running loop typeOr for type(s) $typesOr[$iTypesOr]"
unless ( defined($testMode) && $testMode eq "1" );
my $loopReturn3 = "";
$hasTypeOr = 1
if ( scalar( grep { defined $_ } @typesOr ) > 1 );
my @type = split( /,/, lc( $typesOr[$iTypesOr] ) );
for (
my $i = 0 ;
$i < scalar( grep { defined $_ } @type ) ;
$i++
)
{
Log3 $globalDevName, 6,
"msg: running loop for type $type[$i]"
unless ( defined($testMode) && $testMode eq "1" );
last unless ( defined( $type[$i] ) );
# check for correct type
my @msgCmds = (
"screen", "light", "audio", "text",
"push", "mail", "queue", "default"
);
unless ( grep { $type[$i] =~ /^$_/i } @msgCmds ) {
$loopReturn3 .= "Unknown message type $type[$i]\n";
next;
}
###
### /type loop
################################################################
my @unavailabilityIndicators = (
"0", "false",
"absent", "disappeared",
"unauthorized", "unavailable",
"unreachable", "disconnected"
);
my $logDevice;
$logDevice = $globalDevName;
$logDevice = $device
if (
MSG_FindAttrVal( $device, "verbose", undef, undef ) );
my $msgSentDev = 0;
my $gatewayDevs = "";
my $forceDevice = 0;
my $forceQueue = 0;
# for device type
my $deviceType = "device";
if ( $device =~
m/^(([A-Za-z0-9%+._-])+@+([%+a-z0-9A-Z.-]*))$/ )
{
$gatewayDevs = $1;
$deviceType = "email";
}
elsif ( $device =~
m/^@?([A-Za-z\d_\.\-\/]+)([^A-Za-z\d_\.\-\/]+)[\s\t ]*$/
)
{
$device = $1;
foreach ( split( /(\S)/, $2 ) ) {
$forceDevice = 1 if ( $_ eq "!" );
$softFail = 1 if ( $_ eq "?" );
$forceQueue = 1 if ( $_ eq "&" );
}
Log3 $logDevice, 5,
"msg $device: forceDevice=$forceDevice (from device contact)"
if ($forceDevice);
Log3 $logDevice, 5,
"msg $device: softFail=$softFail (from device contact)"
if ($softFail);
Log3 $logDevice, 5,
"msg $device: forceQueue=$forceQueue (from device contact)"
if ($forceQueue);
}
elsif ( $device =~ /^@(.*)/ ) {
$device = $1;
}
# sub-recipient
my $subRecipient = "";
my $termRecipient = "";
if ( $device =~
m/^@?([A-Za-z0-9._]+):([A-Za-z0-9._\-\/@+]*):?([A-Za-z0-9._\-\/@+]*)$/
)
{
$device = $1;
$subRecipient = $2;
$termRecipient = $3;
}
# FATAL ERROR: device does not exist
if ( !IsDevice($device)
&& $deviceType eq "device" )
{
$loopReturn3 .= "Device $device does not exist\n"
unless ($softFail);
unless ( defined($testMode) && $testMode eq "1" ) {
Log3 $logDevice, 2,
"msg $device: Device does not exist"
unless ($softFail);
Log3 $logDevice, 5,
"msg $device: Device does not exist"
if ($softFail);
}
my $regex1 =
"\\s*!?@?" . $device . "[,|]"; # at the beginning
my $regex2 = "[,|]!?@?" . $device . "\\s*"; # at the end
my $regex3 =
",!?@?" . $device . ","; # in the middle with comma
my $regex4 =
"[\|,]!?@?"
. $device
. "[\|,]"; # in the middle with pipe and/or comma
$recipients =~ s/^$regex1//gi;
$recipients =~ s/$regex2$/|/gi;
$recipients =~ s/$regex3/,/gi;
$recipients =~ s/$regex4/|/gi;
next;
}
# Find custom types for devices
if ( $type[$i] eq "default" ) {
delete $typesOr[$iTypesOr]
if ( $typesOr[$iTypesOr] eq "default" );
delete $type[$i];
Log3 $logDevice, 5,
"msg $device: msgType lookup for $device:";
my @t = split(
m/\|/,
MSG_FindAttrVal(
$device, "msgType", undef, "text"
)
);
$hasTypeOr = 1
if ( scalar( grep { defined $_ } @t ) > 1 );
foreach (@t) {
Log3 $logDevice, 5,
"msg $device: Adding to \@typesOr: $_";
push @typesOr, $_;
foreach ( split( /,/, lc($_) ) ) {
Log3 $logDevice, 5,
"msg $device: Adding to \@type: $_";
push @type, $_;
}
}
}
my $forceType = 0;
if ( $type[$i] =~
m/^([A-Za-z\d_\.\-\/]+)([^A-Za-z\d_\.\-\/]+)[\s\t ]*$/ )
{
$type[$i] = $1;
foreach ( split( /(\S)/, $2 ) ) {
$forceType = 1 if ( $_ eq "!" );
$softFail = 1 if ( $_ eq "?" );
$forceQueue = 1 if ( $_ eq "&" );
}
Log3 $logDevice, 5,
"msg $device: forceType=$forceType (from type)"
if ($forceType);
Log3 $logDevice, 5,
"msg $device: softFail=$softFail (from type)"
if ($softFail);
Log3 $logDevice, 5,
"msg $device: forceQueue=$forceQueue (from type)"
if ($forceQueue);
}
# next type loop if device is an email address
# and this is not the mail type loop run
if ( $deviceType eq "email"
&& $type[$i] ne "mail"
&& $type[$i] ne "text" )
{
Log3 $logDevice, 5,
"msg $device: "
. "Skipping loop for device type 'email' with unmatched message type '"
. $type[$i] . "'";
next;
}
my $typeUc = ucfirst( $type[$i] );
my $catchall = 0;
my $useLocation = 0;
################################################################
### get target information from device location
###
# search for location references
my @locationDevs;
@locationDevs = split(
/,/,
MSG_FindAttrVal(
$device, "msgLocationDevs", $typeUc, ""
)
);
if ( $deviceType eq "device" ) {
# get device location
my $deviceLocation =
msgConfig_FindReadingsVal( $device, "location",
$typeUc, "" );
my $locationDev = "";
if ( $deviceLocation ne "" && $deviceType eq "device" )
{
# lookup matching location
foreach (@locationDevs) {
if ( $featurelevel >= 5.7 ) {
my %dummy;
my ( $err, @a ) =
ReplaceSetMagic( \%dummy, 0, ($_) );
$_ = join( " ", @a )
unless ($err);
}
my $lName =
AttrVal( $_, "msgLocationName", "" );
if ( $lName ne "" && $lName eq $deviceLocation )
{
$locationDev = $_;
last;
}
}
if ( $featurelevel >= 5.7 ) {
my %dummy;
my ( $err, @a ) =
ReplaceSetMagic( \%dummy, 0, ($locationDev) );
$locationDev = join( " ", @a )
unless ($err);
}
# look for gateway device
$gatewayDevs =
MSG_FindAttrVal( $locationDev, "msgContact",
$typeUc, "" );
# at least one of the location gateways needs to
# be available. Otherwise we fall back to
# non-location contacts
if ( $gatewayDevs ne "" ) {
if ( $featurelevel >= 5.7 ) {
my %dummy;
my ( $err, @a ) =
ReplaceSetMagic( \%dummy, 0,
($gatewayDevs) );
$gatewayDevs = join( " ", @a )
unless ($err);
}
foreach
my $gatewayDevOr ( split /\|/, $gatewayDevs )
{
foreach my $gatewayDev ( split /,/,
$gatewayDevOr )
{
my $tmpSubRecipient;
if ( $gatewayDev =~ /:(.*)/ ) {
$tmpSubRecipient = $1;
}
if ( $type[$i] ne "mail"
&& !IsDevice($gatewayDev)
&& $deviceType eq "device" )
{
$useLocation = 2
if ( $useLocation == 0 );
}
elsif ( $type[$i] ne "mail"
&& IsDisabled($gatewayDev) )
{
$useLocation = 2
if ( $useLocation == 0 );
}
elsif (
$type[$i] ne "mail"
&& (
(
grep {
ReadingsVal(
$gatewayDev,
"presence",
"present" ) eq $_
} @unavailabilityIndicators
)
|| (
grep {
ReadingsVal(
$gatewayDev,
"state",
"present" ) eq $_
} @unavailabilityIndicators
)
|| (
defined(
$defs{$gatewayDev}
)
&& defined(
$defs{$gatewayDev}
{STATE}
)
&& (
grep {
$defs{$gatewayDev}
{STATE} eq $_
} @unavailabilityIndicators
)
)
|| ReadingsVal(
$gatewayDev, "available",
"yes"
) =~ m/^(0|no|false)$/i
|| ReadingsVal(
$gatewayDev, "reachable",
"yes"
) =~ m/^(0|no|false)$/i
)
)
{
$useLocation = 2
if ( $useLocation == 0 );
}
else {
$useLocation = 1;
}
}
}
# use gatewayDevs from location only
# if it has been confirmed to be available
if ( $useLocation == 1 ) {
Log3 $logDevice, 4, "msg $device: "
. "Matching location definition found";
}
else {
$gatewayDevs = "";
}
}
}
}
################################################################
### given device name is already a gateway device itself
###
my $deviceType2 = GetType($device);
if (
$gatewayDevs eq ""
&& $deviceType eq "device"
&& $deviceType2 ne ""
&& (
(
$type[$i] eq "audio" && defined(
$cmdSchema->{ $type[$i] }{$deviceType2}
)
)
|| (
$type[$i] eq "light"
&& defined(
$cmdSchema->{ $type[$i] }{$deviceType2}
)
)
|| (
$type[$i] eq "push"
&& defined(
$cmdSchema->{ $type[$i] }{$deviceType2}
)
)
|| (
$type[$i] eq "screen"
&& defined(
$cmdSchema->{ $type[$i] }{$deviceType2}
)
)
|| (
$type[$i] eq "queue"
&& defined(
$cmdSchema->{ $type[$i] }{$deviceType2}
)
)
)
)
{
Log3 $logDevice, 4,
"msg $device: Recipient type $deviceType2 "
. "is a gateway device itself for message type "
. $type[$i]
. ". Still checking for any delegates ..."
unless ( defined($testMode) && $testMode eq "1" );
$gatewayDevs =
MSG_FindAttrVal( $device, "msgContact", $typeUc,
$device );
}
################################################################
### get target information from device
###
elsif ( $deviceType eq "device" && $gatewayDevs eq "" ) {
# look for gateway device
$gatewayDevs =
MSG_FindAttrVal( $device, "msgContact", $typeUc, "" );
# fallback/catchall
if ( $gatewayDevs eq "" ) {
$catchall = 1
if ( $device ne $globalDevName );
Log3 $logDevice, 6,
"msg $device: (No $typeUc contact defined, "
. "trying global instead)"
if ( $catchall == 1 );
$gatewayDevs =
MSG_FindAttrVal( $globalDevName, "msgContact",
$typeUc, "" );
}
}
# Find priority if none was explicitly specified
my $loopPriority = $priority;
$loopPriority =
MSG_FindAttrVal( $device, "msgPriority$typeUc", $typeUc,
MSG_FindAttrVal( $device, "msgPriority", $typeUc, 0 ) )
if ( $priority eq "" );
# check for available routes
#
my %routes;
$routes{screen} = 0;
$routes{light} = 0;
$routes{audio} = 0;
$routes{text} = 0;
$routes{push} = 0;
$routes{mail} = 0;
$routes{queue} = 1;
if (
!defined($testMode)
|| ( $testMode ne "1"
&& $testMode ne "2" )
)
{
Log3 $logDevice, 5,
"msg $device: Checking for available routes "
. "(triggered by type $type[$i])";
$routes{screen} = 1
if (
$deviceType eq "device"
&& CommandMsg( "screen",
"screen \@$device $priority Routing Test", 1 )
eq "ROUTE_AVAILABLE"
);
$routes{light} = 1
if (
$deviceType eq "device"
&& CommandMsg( "light",
"light \@$device $priority Routing Test", 1 )
eq "ROUTE_AVAILABLE"
);
$routes{audio} = 1
if (
$deviceType eq "device"
&& CommandMsg( "audio",
"audio \@$device $priority Routing Test", 1 )
eq "ROUTE_AVAILABLE"
);
if (
$deviceType eq "device"
&& CommandMsg( "push",
"push \@$device $priority Routing Test", 1 ) eq
"ROUTE_AVAILABLE"
)
{
$routes{push} = 1;
$routes{text} = 1;
}
if (
CommandMsg( "mail",
"mail \@$device $priority Routing Test", 1 ) eq
"ROUTE_AVAILABLE"
)
{
$routes{mail} = 1;
$routes{text} = 1;
}
$routes{mail} = 1
if ( $deviceType eq "email" );
Log3 $logDevice, 4,
"msg $device: Available routes: screen="
. $routes{screen}
. " light="
. $routes{light}
. " audio="
. $routes{audio}
. " text="
. $routes{text}
. " push="
. $routes{push}
. " mail="
. $routes{mail};
}
##################################################
### dynamic routing for text (->push, ->mail)
###
if ( $type[$i] eq "text" ) {
# user selected emergency priority text threshold
my $prioThresTextEmg =
MSG_FindAttrVal( $device, "msgThPrioTextEmergency",
$typeUc, 2 );
# user selected low priority text threshold
my $prioThresTextNormal =
MSG_FindAttrVal( $device, "msgThPrioTextNormal",
$typeUc, -2 );
# Decide push and/or e-mail destination based
# on priorities
if ( $loopPriority >= $prioThresTextEmg
&& $routes{push} == 1
&& $routes{mail} == 1 )
{
Log3 $logDevice, 4,
"msg $device: "
. "Text routing decision: push+mail(1)";
$forwarded .= ","
if ( $forwarded ne "" );
$forwarded .= "text>push+mail";
push @type, "push"
unless grep { "push" eq $_ } @type;
push @type, "mail"
unless grep { "mail" eq $_ } @type;
}
elsif ($loopPriority >= $prioThresTextEmg
&& $routes{push} == 1
&& $routes{mail} == 0 )
{
Log3 $logDevice, 4,
"msg $device: Text routing decision: push(2)";
$forwarded .= ","
if ( $forwarded ne "" );
$forwarded .= "text>push";
push @type, "push"
unless grep { "push" eq $_ } @type;
}
elsif ($loopPriority >= $prioThresTextEmg
&& $routes{push} == 0
&& $routes{mail} == 1 )
{
Log3 $logDevice, 4,
"msg $device: Text routing decision: mail(3)";
$forwarded .= ","
if ( $forwarded ne "" );
$forwarded .= "text>mail";
push @type, "mail"
unless grep { "mail" eq $_ } @type;
}
elsif ($loopPriority >= $prioThresTextNormal
&& $routes{push} == 1 )
{
Log3 $logDevice, 4,
"msg $device: Text routing decision: push(4)";
$forwarded .= ","
if ( $forwarded ne "" );
$forwarded .= "text>push";
push @type, "push"
unless grep { "push" eq $_ } @type;
}
elsif ($loopPriority >= $prioThresTextNormal
&& $routes{mail} == 1 )
{
Log3 $logDevice, 4,
"msg $device: Text routing decision: mail(5)";
$forwarded .= ","
if ( $forwarded ne "" );
$forwarded .= "text>mail";
push @type, "mail"
unless grep { "mail" eq $_ } @type;
}
elsif ( $routes{mail} == 1 ) {
Log3 $logDevice, 4,
"msg $device: Text routing decision: mail(6)";
$forwarded .= ","
if ( $forwarded ne "" );
$forwarded .= "text>mail";
push @type, "mail"
unless grep { "mail" eq $_ } @type;
}
elsif ( $routes{push} == 1 ) {
Log3 $logDevice, 4,
"msg $device: Text routing decision: push(7)";
$forwarded .= ","
if ( $forwarded ne "" );
$forwarded .= "text>push";
push @type, "push"
unless grep { "push" eq $_ } @type;
}
# FATAL ERROR: routing decision failed
else {
Log3 $logDevice, 4,
"msg $device: "
. "Text routing FAILED - priority=$loopPriority push="
. $routes{push}
. " mail="
. $routes{mail};
$loopReturn3 .=
"ERROR: Could not find any Push or Mail contact "
. "for device $device - set attributes: msgContactPush "
. "| msgContactMail | msgContactText | msgRecipientPush "
. "| msgRecipientMail | msgRecipientText | msgRecipient\n";
}
next;
}
# FATAL ERROR: we could not find any targets for
# user specified device...
if ( $gatewayDevs eq ""
&& $device ne $globalDevName )
{
$loopReturn3 .=