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
|
# -----------------------------------------------------------------------------
# $Id: Notify.pm 36552 2010-01-27 14:53:29Z drry $
# -----------------------------------------------------------------------------
package Auto::Notify;
use strict;
use warnings;
use base qw(Module);
use Module::Use qw(Auto::AliasDB Tools::HTTPClient Auto::Utils);
use Auto::AliasDB;
use Tools::HTTPClient; # >= r11345
use Auto::Utils;
use HTTP::Request::Common;
sub new {
my ($class) = shift;
my $this = $class->SUPER::new(@_);
$this->config_reload(undef);
return $this;
}
sub config_reload {
my ($this, $old_config) = @_;
my $regex = join '|', (
(map { "(?:$_)" } $this->config->regex_keyword('all')),
(map { "(?i:\Q$_\E)" } map { split /,/ } $this->config->keyword('all')),
);
eval {
$this->{regex} = qr/$regex/;
}; if ($@) {
$this->_runloop->notify_error($@);
}
$this->{blocks} = [];
foreach my $blockname (map {split /\s+/} $this->config->blocks('all')) {
my $block = $this->config->get($blockname, 'block');
if (!defined $block) {
die "not found block: $blockname";
}
my $type = $block->type;
if (!defined $type) {
die "type definition not found in block";
}
my $meth = $this->can('config_'.$type);
if (!defined $meth) {
die "unknown type: $type";
}
$this->$meth($block);
push(@{$this->{blocks}}, $block);
}
return $this;
}
sub message_arrived {
my ($this,$msg,$sender) = @_;
my @result = ($msg);
# サーバーからのメッセージか?
if ($sender->isa('IrcIO::Server')) {
# PRIVMSGか?
if ($msg->command eq 'PRIVMSG') {
my $text = $msg->param(1);
my $full_ch_name = $msg->param(0);
if ($text =~ $this->{regex} && Mask::match_deep_chan(
[Mask::array_or_all_chan($this->config->mask('all'))],
$msg->prefix,$full_ch_name)) {
foreach my $block (@{$this->{blocks}}) {
my $type = $block->type;
my $meth = $this->can('send_'.$type);
eval {
$this->$meth($block, $text, $msg, $sender, $full_ch_name);
}; if ($@) {
$this->_runloop->notify_warn(__PACKAGE__." send failed: $@");
}
}
}
}
}
return @result;
}
sub strip_mirc_formatting {
my ($this, $text) = @_;
$text =~ s/(?:\x03\d\d?(?:,\d\d?)?|[\x0f\x02\x1f\x16])//g;
$text;
}
sub config_im_kayac {
my ($this, $config) = @_;
if ($config->secret) {
# signature required
require Digest::SHA;
}
1;
}
sub send_im_kayac {
my ($this, $config, $text, $msg, $sender, $full_ch_name) = @_;
my $url = "http://im.kayac.com/api/post/" . $config->user;
$text = Auto::AliasDB->stdreplace(
$msg->prefix,
$config->format || $this->config->format || '[tiarra][#(channel):#(nick.now)] #(text)',
$msg, $sender,
channel => $full_ch_name,
raw_channel => Auto::Utils::get_raw_ch_name($msg, 0),
text => $this->strip_mirc_formatting($text),
);
my @data = (message => $text);
if ($config->secret) {
push(@data, sig => Digest::SHA->new(1)
->add($text . $config->secret)->hexdigest);
} elsif ($config->password) {
push(@data, password => $config->password);
}
my $runloop = $this->_runloop;
Tools::HTTPClient->new(
Request => POST($url, \@data),
)->start(
Callback => sub {
my $stat = shift;
if (!ref($stat)) {
$runloop->notify_warn(__PACKAGE__." im.kayac.com: post failed: $stat");
} elsif ($stat->{Content} !~ /"result":\s*"(?:ok|posted)"/) {
# http://im.kayac.com/#docs
# (but actually responce is '"result": "ok"')
(my $content = $stat->{Content}) =~ s/\s+/ /;
$runloop->notify_warn(__PACKAGE__." im.kayac.com: post failed: $content");
}
},
);
}
sub config_prowl {
my ($this, $config) = @_;
require Crypt::SSLeay; # https support
require URI;
my $url = URI->new("https://prowl.weks.net/publicapi/verify");
$url->query_form(apikey => $config->apikey);
my $runloop = $this->_runloop;
Tools::HTTPClient->new(
Request => GET($url->as_string()),
)->start(
Callback => sub {
my $stat = shift;
$runloop->notify_warn(__PACKAGE__." verify failed: $stat")
unless ref($stat);
## FIXME: check response (should check 'error')
},
);
}
sub send_prowl {
my ($this, $config, $text, $msg, $sender, $full_ch_name) = @_;
my $url = URI->new("https://prowl.weks.net/publicapi/add");
$text = Auto::AliasDB->stdreplace(
$msg->prefix,
$config->format || $this->config->format || '[tiarra][#(channel):#(nick.now)] #(text)',
$msg, $sender,
channel => $full_ch_name,
raw_channel => Auto::Utils::get_raw_ch_name($msg, 0),
text => $this->strip_mirc_formatting($text),
);
my @data = (apikey => $config->apikey,
priority => $config->priority || 0,
application => $config->application || 'tiarra',
event => $config->event || 'keyword',
description => $text);
$url->query_form(@data);
my $runloop = $this->_runloop;
Tools::HTTPClient->new(
Request => GET($url->as_string()),
)->start(
Callback => sub {
my $stat = shift;
if (!ref($stat)) {
$runloop->notify_warn(__PACKAGE__." prowl: post failed: $stat");
} elsif ($stat->{Content} !~ /<success /) {
(my $content = $stat->{Content}) =~ s/\s+/ /;
$runloop->notify_warn(__PACKAGE__." prowl: post failed: $content");
}
},
);
}
1;
=pod
info: 名前が呼ばれると、その発言をim.kayac.comに送信する
default: off
# 反応する人のマスクを指定します。
# 省略すると全員に反応します。
mask: * *!*@*
# 反応するキーワードを正規表現で指定します。
# 複数指定したい時は複数行指定してください。
-regex-keyword: (?i:fugahoge)
# 反応するキーワードを指定します。
# 複数指定したい時は,(コンマ)で区切るか、複数行指定してください。
keyword: hoge
# メッセージのフォーマットを指定します。
# デフォルト値: [tiarra][#(channel):#(nick.now)] #(text)
# #(channel) のかわりに #(raw_channel) を利用するとネットワーク名がつきません。
format: [tiarra][#(channel):#(nick.now)] #(text)
# 使用するブロックを指定します
-blocks: im prowl
im {
# 通知先のタイプを指定します。
type: im_kayac
# im.kayac.comで登録したユーザ名を入力します。
# im.kayac.comについては http://im.kayac.com/#docs を参考にしてください。
user: username
# im.kayac.comで秘密鍵認証を選択した場合は設定してください。
# 省略すると認証なしになります。
-secret: some secret
# im.kayac.comでパスワード認証を選択した場合は設定してください。
# 省略すると認証なしになります。
# secret と両方指定した場合は secret が優先されています。
-password: some password
}
prowl {
# 通知先のタイプを指定します。
type: prowl
# 通知先ごとにフォーマットを指定できます。
# この例では先頭に時刻を追加しています。
-format: #(date:%H:%M:%S) [#(channel):#(nick.now)] #(text)
# Prowl で表示された apikey を入力します。
# Prowl については http://prowl.weks.net/ を参考にしてください。
-apikey: XXXXXX
# http://forums.cocoaforge.com/viewtopic.php?f=45&t=20339
priority: 0
application: tiarra
event: keyword
}
=cut
|