-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc-eew.pl
executable file
·244 lines (199 loc) · 5.97 KB
/
irc-eew.pl
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
#!/usr/bin/env perl
# irc-eew.pl
# charset = utf-8
# walkure at 3pf.jp
use utf8;
use strict;
use warnings;
use IO::Select;
use YAML;
use Encode;
use File::Spec;
use File::Path qw/mkpath/;
use lib '.','./lib';
use Earthquake::EEW::Decoder;
use IRCSock;
use EEWSock;
use SlackWebhookSock;
binmode STDOUT,':utf8';
$|=1;
$SIG{'TERM'} = sub {die "SIGTERMed\n";};
$SIG{'INT'} = sub {die "SIGINTed\n";};
my $path = $ARGV[0] || 'config.yaml';
print "++Loading YAML:$path\n";
my $yaml = YAML::LoadFile($path);
my $eewlog = $yaml->{logdir};
if(defined $eewlog){
print "++LogDir:[$eewlog]\n";
my $tmpdn = sprintf('%s/tmi-%d/',$eewlog,$$);
mkdir $tmpdn or die "create a dir($tmpdn) failure:$!";
my $tmpfn = sprintf('%s/tmi-%d/tmf-%d',$eewlog,$$,$$);
open(my $fh,">$tmpfn") or die "create a file($tmpfn) err:$!";
print $fh "TEST\n" or die "write a file err:$!";
close $fh;
unlink $tmpfn or die "delete a file err:$!";
rmdir $tmpdn or die "delete a dir err:$!";
print "++LogDir access check OK\n";
}else{
print "++LogDir:N/A not save EEW data\n";
}
my $select = IO::Select->new();
print "++Get WNIEEW server list\n";
my $serv_list = EEWSock->get_server_list();
my $eewsock;
do{
print "++Connect WNIEEW Server\n";
$eewsock = EEWSock->new(
ServerList=>$serv_list,
Callback=>\&eew_callback,
Logs=>$yaml->{WNIEEW}{Logs}
);
}until(defined $eewsock);
my $eewdec = Earthquake::EEW::Decoder->new();
$select->add($eewsock);
$eewsock->logon($yaml->{WNIEEW});
my (@join_ch,%channel_noticeall,%channel_noticelimited,$ircsock);
if(defined $yaml->{irc}){
$ircsock = IRCSock->new($yaml->{irc}{server});
$select->add($ircsock);
if(defined $yaml->{irc}{'all-notice'}){
foreach my $ch(split(/,/,$yaml->{irc}{'all-notice'})){
unless(defined $channel_noticeall{$ch} && defined $channel_noticelimited{$ch}){
push(@join_ch,$ch);
$channel_noticeall{$ch} = 1;
}
}
}
if(defined $yaml->{irc}{'limited-notice'}){
foreach my $ch(split(/,/,$yaml->{irc}{'limited-notice'})){
unless(defined $channel_noticeall{$ch} && defined $channel_noticelimited{$ch}){
push(@join_ch,$ch);
$channel_noticelimited{$ch} = 1;
}
}
}
$ircsock->login(\@join_ch);
}
my (@all_hooks,@limited_hooks);
if(defined $yaml->{slack}){
push(@all_hooks,@{$yaml->{slack}{all}}) if defined $yaml->{slack}{all};
push(@limited_hooks,@{$yaml->{slack}{limited}}) if defined $yaml->{slack}{limited};
}
my $last_eq_id = '';
$! = 0;
while(my @ready = $select->can_read(60 * 3.5)) {
if( scalar(@ready) == 0 ){
die $! == 0 ? "TCP Timeout..." : "Select Error:$!";
}
$! = 0;
foreach my $sock(@ready){
my $buf;
my $len = $sock->sysread($buf,65535);
if($len){
$sock->parse_body($buf);
}else{
#connection closed
print "++Connection closed...\n";
$select->remove($sock);
$sock->close();
if(ref($sock) eq 'EEWSock'){
do{
$eewsock = EEWSock->new(
ServerList => $serv_list,
Callback=>\&eew_callback,
Logs=>$yaml->{WNIEEW}{Logs}
);
}until(defined $eewsock);
$select->add($eewsock);
$eewsock->logon($yaml->{WNIEEW});
}elsif(ref($sock) eq 'IRCSock'){
$ircsock = IRCSock->new($yaml->{irc});
$select->add($ircsock);
$ircsock->login();
}elsif(ref($sock) eq 'SlackWebhookSock'){
my $res = $sock->get_response();
print $sock->name.":[".$res->message."]\n";
}
}
}
if($! != 0){
print "Errored:$!\n";
}
$! = 0;
}
sub eew_callback
{
my($self,$md5,$buffer) = @_;
my $tmpfname = time;
$tmpfname .= ".$md5";
if(defined $eewlog){
print "+++Saved:$tmpfname\n";
if(open(LOG,">$eewlog/$tmpfname")){
print LOG $buffer;
close LOG;
}
}
my $d = $eewdec->read_data($buffer);
my @wd = $d->{warn_time} =~ /\d\d/og;
my @ed = $d->{eq_time} =~ /\d\d/og;
my $warnmsg = "20$wd[0]/$wd[1]/$wd[2] $wd[3]:$wd[4]:$wd[5]";
my $eqedmsg = "20$ed[0]/$ed[1]/$wd[2] $ed[3]:$ed[4]:$ed[5]";
my $times = ' 第'.$d->{'warn_num'}.'報';
$times .= '(最終)' if $d->{NCN_type} > 0;
my $msg;
if($d->{'msg_type_code'} == 10){
$msg = $warnmsg.$times.' ('.$eqedmsg.'発生) 取り消されました';
}else{
my $mapuri = sprintf 'http://maps.google.com/maps?q=%0.01f,%0.01f', $d->{'center_lat'},$d->{'center_lng'};
my $center = sprintf('震央:<%s|N%.01f/E%0.01f>(%s)深さ%dkm',$mapuri,$d->{'center_lat'},$d->{'center_lng'},$d->{'center_name'},$d->{'center_depth'});
my $magnitude = sprintf(' 最大:M%.01f 震度%s',$d->{'magnitude'},$d->{'shindo'});
$msg = $warnmsg.$times.' ('.$eqedmsg.'発生)'.$center.$magnitude;
}
my @notice_ch = keys %channel_noticeall;
# push(@notice_ch,keys %channel_noticelimited) if($d->{'warn_num'} > 900 || $d->{'warn_num'} == 1 || $d->{'msg_type_code'} == 10 );
my @notice_hook = @all_hooks;
if($d->{eq_id} ne $last_eq_id || $d->{msg_type_code} == 10 || $d->{NCN_type} > 0){
$last_eq_id = $d->{eq_id};
push(@notice_ch,keys %channel_noticelimited);
push(@notice_hook,@limited_hooks);
}
my sub remove_link {
my $body = shift;
$body =~ s/\<(.*?)\|(.*?)\>/$2/g;
$body;
}
print "+++>>[$msg]\n";
if(defined $ircsock){
foreach my $ch (@notice_ch){
print "++Noticed [$ch]\n";
$ircsock->notice($ch, remove_link($msg));
}
}
if(defined $yaml->{slack}){
foreach my $endpoint (@notice_hook){
my $webhsock = SlackWebhookSock->new($endpoint);
next unless defined $webhsock;
print '++Noticed ['.$webhsock->name."]\n";
my $title = '緊急地震速報';
$title = $d->{warn_type}.$title if defined $d->{warn_type};
$webhsock->send_json({"text"=> $msg, 'username'=>$title});
$select->add($webhsock);
}
}
if(defined $eewlog && -e "$eewlog/$tmpfname"){
my $newname = $d->{eq_id}.'.'.$d->{warn_num};
my $fullname = get_fn_from_eqid($eewlog,$newname);
print "++Rename [$tmpfname] -> [$fullname]\n";
rename "$eewlog/$tmpfname" , $fullname;
}
}
sub get_fn_from_eqid
{
my ($dir,$eqid) = @_;
my $year = substr($eqid,0,4);
my $month = substr($eqid,4,2);
my $day = substr($eqid,6,2);
my $fdir = File::Spec->catdir($dir,$year,$month,$day);
mkpath($fdir);
File::Spec->catfile($fdir,$eqid);
}