-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.pl
executable file
·283 lines (227 loc) · 6.98 KB
/
reply.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
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use Encode;
use FindBin;
use YAML::Tiny;
use AnyEvent::Twitter;
use AnyEvent::Twitter::Stream;
binmode STDOUT, ":utf8";
#
# logging
#
sub time_stamp {
my $time = localtime(time);
return $time . " $0" . "[$$]: ";
}
sub print_log {
my ($level, $msg) = @_;
chomp($level);
chomp($msg);
print time_stamp() . "$level: $msg\n";
return;
}
#
# special functions
#
{
my $str = "";
# ping pong
sub ping {
$str = "\@" . $_[0]->{user}{screen_name} . " " . "pong";
return $str;
}
# revision sasairc_yasuna
sub revision {
my $sha = decode_utf8(`git -C $FindBin::Bin rev-parse HEAD`);
$str = "\@" . $_[0]->{user}{screen_name} . " " . "sasairc_yasuna: $sha";
return $str;
}
# system status
sub uptime {
my $hostname = decode_utf8(`hostname`);
my $uptime = decode_utf8(`uptime`);
chomp($hostname);
$str = "\@" . $_[0]->{user}{screen_name} . " " . $hostname . ": " . $uptime;
return $str;
}
# Oudon is a traditional noodle cuisine of Japan
sub oudon {
if (check_user_authority($_[0])) {
print_log("info", "allow user");
$str = "\@" . "keep_off07" . " " . "🍜";
} else {
print_log("info", "deny user");
$str = "\@" . $_[0]->{user}{screen_name} . " " . "おうどんをあげる許可がありません。";
}
return $str;
}
# fish age
sub osakana {
$str = "\@" . "sasairc_2" . " " . "🐟";
return $str;
}
# encode n_cipher
sub encode_n_cipher {
($str = $_[0]->{text}) =~ /encode\s/;
$str = decode_utf8(`n_cipher_encode "$'"`);
if ($?) {
$str = "暗号になってない!!";
}
$str = "\@" . $_[0]->{user}{screen_name} . " " . $str;
return $str;
}
# decode n_cipher
sub decode_n_cipher {
($str = $_[0]->{text}) =~ /decode\s/;
$str = decode_utf8(`n_cipher_decode "$'"`);
if ($?) {
$str = "暗号になってない!!";
} else {
if (!check_user_authority($_[0])) {
$str =~ s/\@/\@ /g;
}
}
$str = "\@" . $_[0]->{user}{screen_name} . " " . $str;
return $str;
}
# yasuna will talk
sub yasuna_talk {
$str = "\@" . $_[0]->{user}{screen_name} . " " . decode_utf8(`yasuna`);
return $str;
}
# yasuna --number N option
sub yasuna_number {
my $max = `yasuna -l | wc -l`;
our @number = split(/ /, $_[0]->{text});
our $arrnum = @number - 1;
chomp($max);
chomp(@number);
if ($number[$arrnum] < $max) {
$str = "\@" . $_[0]->{user}{screen_name} . " " . decode_utf8(`yasuna -n $number[$arrnum]`);
} else {
$str = "\@" . $_[0]->{user}{screen_name} . " " . "numberは $max 以内で指定して下さい";
}
return $str;
}
# yasuna --version option
sub yasuna_version {
$str = "\@" . $_[0]->{user}{screen_name} . " " . decode_utf8(`yasuna --version`);
return $str;
}
}
#
# regex/function table
#
my %regex = (
'ping$' => \&ping,
'revision$' => \&revision,
'uptime$' => \&uptime,
'^@sasairc_yasuna\s(お?うどん|o?udon)$' => \&oudon,
'^@sasairc_yasuna\s(お?さかな|o?sakana)$' => \&osakana,
'encode\s(.+)' => \&encode_n_cipher,
'decode\s(.+)' => \&decode_n_cipher,
'talk(?:.*)\z' => \&yasuna_talk,
'(number|n)\s[0-9]+$' => \&yasuna_number,
'version$' => \&yasuna_version,
);
#
# processing of message
#
sub if_message_type {
my $str = "";
# check special function
while (my ($key, $value) = each(%regex)) {
if ($_[0]->{text} =~ /$key/) {
print_log("recv", "$_[0]->{user}{screen_name}: $_[0]->{text} ($key)");
$str = $value->($_[0]);
}
}
# checking string length
if ((my $len = length($str)) > 140) {
$str = "\@" . $_[0]->{user}{screen_name} . " " . "何 $len 文字て!送信できないじゃん!";
}
return $str;
}
#
# check user authority
#
my $user = (YAML::Tiny->read($FindBin::Bin . '/user.yml'))->[0];
sub check_user_authority {
for (my $i = 0; $i < @{$user->{allow}}; $i++) {
if ($user->{allow}[$i] eq $_[0]->{user}{screen_name}) {
return 1; # allow
}
}
return 0; # deny
}
#
# main
#
my $config = (YAML::Tiny->read($FindBin::Bin . '/config.yml'))->[0];
while (1) {
my $done_cv = AE::cv;
my $connected;
my $sender = AnyEvent::Twitter->new(
consumer_key => $config->{'TWITTER_CONSUMER_KEY'},
consumer_secret => $config->{'TWITTER_CONSUMER_SECRET'},
token => $config->{'TWITTER_ACCESS_TOKEN'},
token_secret => $config->{'TWITTER_ACCESS_TOKEN_SECRET'},
);
my $listener = AnyEvent::Twitter::Stream->new(
consumer_key => $config->{'TWITTER_CONSUMER_KEY'},
consumer_secret => $config->{'TWITTER_CONSUMER_SECRET'},
token => $config->{'TWITTER_ACCESS_TOKEN'},
token_secret => $config->{'TWITTER_ACCESS_TOKEN_SECRET'},
method => 'filter',
track => $config->{'TWITTER_BOT_SCREEN_NAME'},
on_tweet => sub {
$connected = 1 unless $connected;
my $tweet = shift;
my $str = "";
if (($str = if_message_type($tweet)) ne "") {
$sender->post('statuses/update', {
status => $str,
in_reply_to_status_id => $tweet->{id},
}, sub {
my ($header, $response, $reason) = @_;
print_log("send", $str);
});
}
$done_cv->end;
},
on_connect => sub {
$connected = 1 unless $connected;
print_log("info", "stream connected.");
$sender->post('statuses/update', {
status => "pong",
}, sub {
my ($header, $response, $reason) = @_;
});
$done_cv->end;
},
on_keepalive => sub {
$connected = 1 unless $connected;
},
on_error => sub {
my $error = shift;
print_log("error", $error);
$done_cv->send;
},
on_eof => sub {
$done_cv->send;
},
);
$done_cv->recv;
undef $listener;
#
# retry wait
#
my $wait = $connected ? 0:2;
my $wait_cv = AE::cv;
my $wait_t = AE::timer $wait, 0, $wait_cv;
$wait_cv->recv;
}
print_log("error", "abort.");
exit 1;