-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzkill-watch.pl
executable file
·291 lines (229 loc) · 8.05 KB
/
zkill-watch.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
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
#!/usr/bin/perl
use strict;
use warnings;
# Zkill - Use websockets via Mojo
use Mojo::UserAgent;
use Mojo::IOLoop;
# ESI calls - Simple GETs etc
use REST::Client;
use JSON;
use Data::Dumper;
# Declare prototypes
sub esi_search($$);
# Add config simple for slack bot key
use Config::Simple;
# Get config object
our $cfg = new Config::Simple('zkill-watch.config');
# Get global config options we need to assume state on if not present
our $debug = $cfg->param("DEBUG") || 1;
# Default distances - Blops range with no minimum
our $max_ly = $cfg->param("MAX_LY") || 8;
our $min_ly = $cfg->param("MIN_LY") || 0;
# Declare hashes we will use while we sit running to prevent duplicate API calls
our $systems = {};
our $constellations = {};
our $ships = {};
# Get ESI and slack client
our $esi_client = esi_get_client();
our $slack_client = slack_get_client();
our $ship_groups = {};
# Pull ship groups from config file and build ship_groups list
foreach my $ship_group ($cfg->param("SHIP_GROUPS")) {
my @entry = split(/:/,$ship_group);
$ship_groups->{$entry[0]} = $entry[1] if ($entry[0] =~ m/^\d*$/);
print "Ship group entry $entry[0]:$entry[1] looks bad, skipped\n" unless ($entry[0] =~ m/^\d*$/);
}
our $system_checks = {};
# Pull system list from config, get IDs and build system_checks list
foreach my $sys_check ($cfg->param("SYSTEMS")) {
my $sys_id = esi_search("solar_system",$sys_check);
$system_checks->{$sys_id->{"solar_system"}->[0]} = $sys_check if $sys_id->{"solar_system"}->[0];
print "$sys_check not found, skipping\n" unless $sys_id->{"solar_system"}->[0];
}
# Get system objects for distance calcs
foreach my $id (keys %{$system_checks}) {
esi_get_system($id);
}
# Open WebSocket to echo service
my $ua = Mojo::UserAgent->new;
# Set timers to be higher to prevent script stops
$ua->inactivity_timeout(300);
$ua->request_timeout(30);
while(42) {
$ua->websocket('wss://zkillboard.com:2096/' => { DNT => 1 } => sub {
my ($ua, $tx) = @_;
# Check if WebSocket handshake was successful
print "Error - ".$tx->error->{"message"}."\n" if $tx->error;
print "WebSocket handshake failed!\n" and return unless $tx->is_websocket;
# Send a message to the server
$tx->send('{"action":"sub","channel":"killstream"}');
$ua->on(error => sub {
my ($ua, $err) = @_;
print "UA Error: $err\n";
});
$tx->on(error => sub {
my ($tx, $err) = @_;
print "TX Error: $err\n";
});
# Wait for WebSocket to be closed
$tx->on(finish => sub {
my ($tx, $code, $reason) = @_;
print "WebSocket closed with status $code.\n" if ($debug == 1);
});
$tx->on(message => sub {
my ($tx, $msg) = @_;
process_kill(decode_json($msg));
#Stop after this message - useful for testing
#$tx->finish;
});
});
# Start event loop if not running
print "starting new loop\n" if ($debug == 1);
Mojo::IOLoop->start;
Mojo::IOLoop->stop;
sleep 1;
}
sub process_kill {
my $kill = shift;
my $ship_match = 0;
my $distance_match = 0;
my $message = "";
my $found_ships = {};
my $distances = "";
print "Kill ".$kill->{"zkb"}->{"url"}."\n";
# Get attacker ship IDs and process through ESI if we don't already know what they are
foreach my $attacker (@{$kill->{"attackers"}}) {
# Skip attacker entries with no ship type at all - the ? entries in zkill
next unless defined $attacker->{"ship_type_id"};
esi_get_ship($attacker->{"ship_type_id"}) unless $ships->{$attacker->{"ship_type_id"}};
if ($ship_groups->{$ships->{$attacker->{"ship_type_id"}}->{"group_id"}}) {
#Found matching ship group ID
print "Found matching ship - ".$ships->{$attacker->{"ship_type_id"}}->{"name"}."\n" if ($debug == 1);
#$message .= "Found ".$ships->{$attacker->{"ship_type_id"}}->{"name"}."\n";
$found_ships->{$ships->{$attacker->{"ship_type_id"}}->{"name"}}++;
$ship_match = 1;
}
}
# Get system details for kill system if we don't already have it
esi_get_system($kill->{"solar_system_id"}) unless $systems->{$kill->{"solar_system_id"}};
foreach my $id (keys %{$system_checks}) {
my $distance = calc_distance($systems->{$id},$systems->{$kill->{"solar_system_id"}});
$distances .= "Distance to ".$system_checks->{$id}.": ".sprintf("%.4f", $distance)." LY\n";
$distance_match = 1 if ($distance <= $max_ly && $distance >= $min_ly);
}
if ($ship_match eq "1") {
if ($distance_match eq "1") {
$message .= "Kill in *".$systems->{$kill->{"solar_system_id"}}->{"name"}."*\n";
foreach my $found_ship (keys %{$found_ships}) {
$message .= $found_ship." x".$found_ships->{$found_ship}."\n";
}
$message .= $distances;
$message .= $kill->{"zkb"}->{"url"};
slack_post_kill($message);
} else {
print $message if ($debug == 1);
}
} else {
print " No matching ship types\n" if ($debug == 1);
}
}
sub check_system_in_region($$) {
my $sys_id = shift;
my $reg_id = shift;
esi_get_system($sys_id) unless $systems->{$sys_id};
esi_get_constellation($systems->{$sys_id}->{"constellation_id"}) unless $constellations->{$systems->{$sys_id}->{"constellation_id"}};
if ($constellations->{$systems->{$sys_id}->{"constellation_id"}} eq $reg_id) {
#System in region
return 1;
} else {
#System not in region
return 0;
}
}
sub esi_get_client {
my $server = "https://esi.evetech.net";
my $client = REST::Client->new();
$client->setHost("$server");
$client->getUseragent()->ssl_opts(verify_hostname => '0');
$client->getUseragent()->ssl_opts(SSL_verify_mode => '0');
$client->getUseragent()->agent("zkill-watch: https://github.com/abeeson/zkill-watch");
return $client;
}
sub esi_get_ship {
#Pull ID
my $id = shift;
#Collect object from ESI
my $ship = esi_get("/v3/universe/types/".$id."/");
#Add to ships object
$ships->{$id} = $ship;
}
sub esi_get_system {
#Pull ID
my $id = shift;
#Collect object from ESI
my $system = esi_get("/v3/universe/systems/".$id."/");
#Add to systems object
$systems->{$id} = $system;
}
sub esi_get_constellation {
#Pull ID
my $id = shift;
#Collect object from ESI
my $constellation = esi_get("/v3/universe/constellations/".$id."/");
#Add to constellations object
$constellations->{$id} = $constellation;
}
sub esi_search($$) {
#Pull category and name
my $category = shift;
my $name = shift;
return esi_get("/v2/search/?strict=1&categories=".$category."&search=".$name);
}
sub esi_get($) {
#Pull call
my $call = shift;
#make GET call
$esi_client->GET($call);
#Return after decoding the JSON
return decode_json($esi_client->responseContent);
}
sub slack_get_client {
my $server = "https://slack.com";
my $client = REST::Client->new();
$client->setHost("$server");
$client->addHeader("Authorization", "Bearer ".$cfg->param("SLACK_API"));
$client->addHeader("Content-Type", "application/json; charset=utf-8");
$client->getUseragent()->ssl_opts(verify_hostname => '0');
$client->getUseragent()->ssl_opts(SSL_verify_mode => '0');
return $client;
}
sub slack_post_kill($) {
my $message = shift;
my $object = {
"channel" => $cfg->param("SLACK_CHANNEL"),
"text" => $message,
"unfurl_links" => "true",
"as_user" => "true"
};
$slack_client->POST("/api/chat.postMessage",encode_json($object));
}
sub calc_distance($$) {
# Must be system objects, not names
my $system1 = shift;
my $system2 = shift;
#Get distance differences between the two points for x,y,z
my $x_diff = get_diff($system1->{"position"}->{"x"},$system2->{"position"}->{"x"});
my $y_diff = get_diff($system1->{"position"}->{"y"},$system2->{"position"}->{"y"});
my $z_diff = get_diff($system1->{"position"}->{"z"},$system2->{"position"}->{"z"});
#Sum ^2s and square root for direct line distance
my $xyz_diff = sqrt($x_diff**2 + $y_diff**2 + $z_diff**2);
#Convert from metres to LY and return
return ($xyz_diff/(9.461*10**15));
}
sub get_diff($$) {
#Take two integer values and calculate the positive difference
my $val1 = shift;
my $val2 = shift;
return $val1-$val2 if $val1 >= $val2;
return $val2-$val1 if $val2 > $val1;
}