-
Notifications
You must be signed in to change notification settings - Fork 4
/
znclinker.pm
executable file
·189 lines (168 loc) · 5.33 KB
/
znclinker.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
use strict;
use warnings;
use diagnostics;
use utf8;
use Data::Munge;
use HTTP::Response;
use JSON;
package znclinker;
use base 'ZNC::Module';
sub description {
"ZNC-Linker bot"
}
sub module_types {
$ZNC::CModInfo::UserModule
}
sub put_chan {
my ($self, $chan, $msg) = @_;
$self->PutIRC("PRIVMSG $chan :$msg");
}
sub OnChanMsg {
my ($self, $nick, $chan, $what) = @_;
$nick = $nick->GetNick;
$chan = $chan->GetName;
return $ZNC::CONTINUE if $nick eq 'travis-ci';
return $ZNC::CONTINUE if $nick eq 'LitBot';
my $now = time;
while (my ($key, $value) = each %{$self->{last}}) {
delete $self->{last}{$key} if $value->{t} + 3600 < $now; # 1 hour
}
my $thiskey = "$nick $chan ".$self->GetNetwork->GetName;
my $regexError;
if (my ($sep, $old, $new, $flags) = $what =~ /^
s
([\/`~!#%&]) # separator
((?:(?!\1).|\\\1)+)
\1
((?:(?!\1).|\\\1)*)
(?:
\1
(\w*) # flags
)?
$/x) {
if (exists $self->{last}{$thiskey}) {
my ($g, $i);
$g = 'g' if $flags =~ /g/; $flags =~ s/g//g;
$i = 'i' if $flags =~ /i/; $flags =~ s/i//g;
if ($flags) {
$self->put_chan($chan, "Supported regex flags: g, i. Flags “$flags” are unknown.");
$regexError = 1;
} else {
my $str = $self->{last}{$thiskey}{msg};
eval {
my $re;
if ($i) {
$re = qr/$old/i;
} else {
$re = qr/$old/;
}
$what = Data::Munge::replace($str, $re, $new, $g);
$self->put_chan($chan, "$nick meant: “$what”") if $what ne $str;
};
if ($@) {
print $@;
my $error = "$@";
$error =~ s# at [/.\w]+ line \d+\.$##;
$self->put_chan($chan, $error);
$regexError = 1;
}
}
}
}
$self->{last}{$thiskey} = {
msg => $what,
t => $now,
} unless $regexError;
if (my ($to) = $what=~/^!q\s+(\S+)/) {
$self->put_chan($chan=>"$to, we are not telepaths, please ask a concrete question and wait for an answer. Be sure that you have checked https://wiki.znc.in/FAQ first. You may want to read http://catb.org/~esr/faqs/smart-questions.html");
}
if (my ($to) = $what=~/^!d\s+(\S+)/) {
$self->put_chan($chan=>"$to, when asking for help, be sure to provide as many details as possible: What did you try to do, how exactly did you try it (step by step), all error messages, ZNC version, bindhosts, etc. Without details, the only possible answer is '$to, you're doing something wrong.'");
}
if (my ($to) = $what=~/^!request(?:\s+(\S+))?/i) {
$to = $to // $nick;
$self->put_chan($chan=>"$to, ZNC is free software. Just install and use it. If you wanted a free BNC account instead, go somewhere else. https://wiki.znc.in/Providers may be a good start.");
}
if ($what=~/^!win/) {
$self->put_chan($chan=>'Installing ZNC on Windows: https://wiki.znc.in/Installation#Cygwin');
}
if ($what eq '!help') {
$self->put_chan($chan=>'Need any help?');
}
my $count = 0;
my @wiki;
for(my ($w,$q,$foo)=($what,'','');($q,$foo,$w)=$w=~/.*?\[\[([^\]\|]*)(\|[^\]]*)?\]\](.*)/ and $count++<4;){
$q=~s/ /_/g;
$q=~s/\003\d{0,2}(,\d{0,2})?//g;#color
$q=~s/[\x{2}\x{f}\x{16}\x{1f}]//g;
$q=~s/[\r\n]//g;
push @wiki, "https://wiki.znc.in/$q";
}
if (@wiki) {
my $wikis = join(' ', @wiki);
$self->put_chan($chan=>$wikis);
}
if ($what=~/(?:any|some)\s*(?:one|body)\s+(?:alive|around|awake|here|home|in|round|there)\s*(?:\?|$)/i) {
$self->put_chan($chan=>"Pointless question detected! $nick, we are not telepaths, please ask a concrete question and wait for an answer. Be sure that you have checked https://wiki.znc.in/FAQ first. You may want to read http://catb.org/~esr/faqs/smart-questions.html Sorry if this is a false alarm.");
}
if (my ($issue) = $what=~m@(?|\B#(\d{2,})|https://github.com/znc/znc/(?:issues|pull)/(\d+))@) {
if ($issue > 0) {
$self->CreateSocket('znclinker::github', $issue, $self->GetNetwork, $chan);
}
}
return $ZNC::CONTINUE;
}
sub OnUserMsg {
my ($self, $tgt, $msg) = @_;
my @targets = (
{
network => 'freenode',
chan => '#znc-dev',
},
);
for my $target (@targets) {
$self->GetUser->FindNetwork($target->{network})->PutIRC("PRIVMSG $target->{chan} :$msg");
}
return $ZNC::HALT;
}
package znclinker::github;
use base 'ZNC::Socket';
sub Init {
my $self = shift;
$self->{issue} = shift;
$self->{network} = shift;
$self->{chan} = shift;
$self->{response} = '';
$self->DisableReadLine;
$self->Connect('api.github.com', 443, ssl=>1);
$self->Write("GET https://api.github.com/repos/znc/znc/issues/$self->{issue} HTTP/1.0\r\n");
$self->Write("User-Agent: https://github.com/DarthGandalf/znclinker\r\n");
$self->Write("Host: api.github.com\r\n");
$self->Write("\r\n");
}
sub OnReadData {
my $self = shift;
my $data = shift;
print "new data |$data|\n";
$self->{response} .= $data;
}
sub OnDisconnected {
my $self = shift;
my $response = HTTP::Response->parse($self->{response});
if ($response->is_success) {
my $data = JSON->new->utf8->decode($response->decoded_content);
$self->{network}->PutIRC("PRIVMSG $self->{chan} :$data->{html_url} “$data->{title}” ($data->{state})");
} else {
my $error = $response->status_line;
$self->{network}->PutIRC("PRIVMSG $self->{chan} :https://github.com/znc/znc/issues/$self->{issue} – $error");
}
}
sub OnTimeout {
my $self = shift;
$self->{network}->PutIRC("PRIVMSG $self->{chan} :github timeout");
}
sub OnConnectionRefused {
my $self = shift;
$self->{network}->PutIRC("PRIVMSG $self->{chan} :github connection refused");
}
1;