Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 133 lines (116 sloc) 3.135 kB
70aa551 @sugyan add NotifyGrowlOrImkayac.pm
authored
1 package NotifyGrowlOrImkayac;
2 use strict;
3 use warnings;
4 use base 'ZNC::Module';
5 use feature 'switch';
6
7 use AnyEvent::WebService::ImKayac;
8 use Growl::GNTP;
9 use Log::Minimal;
10 use Time::Piece;
11 use Try::Tiny;
12
13 sub description {
029f9ea @sugyan NotifyGrowlOrImkayac: modify description
authored
14 "Notifify to Growl or im.kayac.com perl module for ZNC"
70aa551 @sugyan add NotifyGrowlOrImkayac.pm
authored
15 }
16
17 # arguments will be processed as space-separated <key>=<value> pairs
18 sub OnLoad {
19 my $self = shift;
20 my $arg = shift;
21 for my $param (split / +/, $arg) {
22 my ($key, $value) = $param =~ /([^=]+)=(.*)/;
23 next unless $key && $value;
24 given ($key) {
25 when (/(?:username|type|password|secret_key)/) {
26 $self->{config}{$key} = $value;
27 }
28 when ('keyword') {
29 $self->{config}{keywords} = [ split /,/, $value ];
30 }
31 default {}
32 }
33 infof($param);
34 }
35
36 if ($self->{config}{username} && $self->{config}{keywords}) {
37 return 1;
38 }
39 else {
40 $_[0] = 'missing mandatory parameter "username" or "keyword"';
41 return 0;
42 }
43 }
44
45 sub OnChanMsg {
46 my $self = shift;
47 my ($nick, $channel, $message) = @_;
48
49 if ($self->_judge($message)) {
50 $self->_notify($nick, $channel, $message);
51 }
52
53 return $ZNC::CONTINUE;
54 }
55
56 sub OnPrivMsg {
57 my $self = shift;
58 my ($nick, $message) = @_;
59
60 if ($self->_judge($message)) {
61 $self->_notify($nick, undef, $message);
62 }
63
64 return $ZNC::CONTINUE;
65 }
66
67 # match keywords
68 sub _judge {
69 my $self = shift;
70 my ($message) = @_;
71
72 my $flg = 0;
73 for my $keyword (@{ $self->{config}{keywords} }) {
74 if ($message =~ qr/$keyword/) {
75 $flg = 1;
76 last;
77 }
78 }
79 return $flg;
80 }
81
82 sub _notify {
83 my $self = shift;
84 my ($nick, $channel, $message) = @_;
85
86 # notification message
87 my $notification = sprintf '(%s) %s <%s>: %s', (
88 localtime->datetime,
89 $channel ? $channel->GetName : 'privmsg',
90 $nick->GetNick,
91 $message,
92 );
93 infof('notify: %s', $notification);
94
95 # notify to Growl or im.kayac.com
96 try {
97 my $growl = Growl::GNTP->new(AppName => 'ZNC Notify');
98 $growl->register([{
99 Name => 'notify',
100 }]);
101 $growl->notify(
102 Event => 'notify',
103 Title => 'ZNC Notify',
104 Message => $notification,
105 Sticky => 'True',
106 );
107 } catch {
108 my $im = AnyEvent::WebService::ImKayac->new(
109 user => $self->{config}{username},
110 type => $self->{config}{type} || 'none',
111 password => $self->{config}{password} || '',
112 secret_key => $self->{config}{secret_key} || '',
113 );
114 my $cv = AE::cv;
115 $im->send(
116 message => $notification,
117 cb => sub {
118 my ($hdr, $json, $err) = @_;
119 if ($json->{result}) {
120 infof($json->{result});
121 }
122 if ($json->{error}) {
123 critf($json->{error});
124 }
125 $cv->send;
126 },
127 );
128 $cv->recv;
129 };
130 }
131
132 1;
Something went wrong with that request. Please try again.