-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsaysomething.pl
107 lines (95 loc) · 3.67 KB
/
saysomething.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
use strict;
use POSIX qw(strftime);
use Irssi qw(command_bind timeout_add server_find_tag channel_find);
use Net::MQTT::Simple "mosquitto.space.revspace.nl";
my $space_state = 0;
our $state_changed;
*state_changed = \$::state_changed;
our $counter_reset;
*counter_reset = \$::counter_reset;
my $reset_after = 10 * 60;
my $openings = 0;
my %ppl;
sub read_file ($) { local (@ARGV) = shift; <> }
sub revspace_topic_hack {
my ($server, $chan, $channame, $line) = @_;
$line =~ /^RevSpace (dicht|open)$/ or return;
my $status = $1;
$space_state = $status eq 'open';
$state_changed = time;
my $topic = $chan->{topic};
my ($oldstatus, $rest) = split /\s*%\s*/, $topic, 2;
$rest &&= " % $rest";
$server->command("/topic #$channame RevSpace is \U$status\E$rest");
return 1;
}
my %queue;
my $silence = 0;
my $timer = timeout_add(500, sub {
eval {
opendir my $dh, "saysomething" or return;
my @entries = grep -f "saysomething/$_", readdir $dh;
if (@entries) {
$silence = 0;
for my $fn (@entries) {
my @lines = read_file("saysomething/$fn")
or next;
unlink "saysomething/$fn" or warn "Couldn't unlink $fn: $!";
chomp @lines;
s/\r//g for @lines;
s/^\[revdoor\] // for @lines;
push @{ $queue{$fn} }, @lines;
for (@lines) {
if (/^(\S+) unlocked by (\S+)/) {
my $naam = $2;
if (!$space_state
and defined $state_changed
and $state_changed <= time() - $reset_after) {
%ppl = ();
$openings = 0;
$state_changed = time;
$counter_reset = time;
}
my $by = '';
if ($naam ne '[X]') {
my $temp_id = exists $ppl{$naam}
? $ppl{$naam}
: ($ppl{$naam} = (keys %ppl) + 1);
$by = " by #$temp_id";
}
my $time = strftime "%Y-%m-%d %H:%M:%S", localtime;
# publish "revspace/doorduino" => "$1 unlocked$by ($time)";
# retain "revspace/doorduino/unique" => 0 + (keys %ppl);
# retain "revspace/doorduino/count" => ++$openings;
}
}
}
} else {
return if not keys %queue;
return if $silence++ < 15;
for my $fn (keys %queue) {
my ($server, $channame) = split /_/, $fn;
my $server = server_find_tag($server) or do {
Irssi::print("No such server tag '$server'");
next;
};
my $chan = $server->channel_find("#$channame") or do {
Irssi::print("No such channel '#$channame'");
next;
};
my @queue = @{ $queue{$fn} };
if ($fn eq "liberachat_revspace") {
revspace_topic_hack($server, $chan, $channame, $_)
for @queue;
}
$server->command(
"/notice #$channame " . join " % ", @{ $queue{$fn} }
);
}
$silence = 0;
%queue = ();
};
};
Irssi::print($@) if $@; # Something makes it crash; I wanna know what.
}, "dummy");
Irssi::command_bind(saynothing => sub { Irssi::timeout_remove($timer) });