line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Chaton::API; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
22645
|
use 5.012001; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
28
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
15
|
|
|
1
|
|
|
|
|
29
|
|
6
|
1
|
|
|
1
|
|
12978
|
use Pipe::Between::Object; |
|
1
|
|
|
|
|
751
|
|
|
1
|
|
|
|
|
87
|
|
7
|
1
|
|
|
1
|
|
19722
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
93519
|
|
|
1
|
|
|
|
|
34
|
|
8
|
1
|
|
|
1
|
|
11730
|
use HTTP::Request::Common qw(GET POST); |
|
1
|
|
|
|
|
3364
|
|
|
1
|
|
|
|
|
95
|
|
9
|
1
|
|
|
1
|
|
1318
|
use JSON; |
|
1
|
|
|
|
|
17093
|
|
|
1
|
|
|
|
|
5
|
|
10
|
1
|
|
|
1
|
|
1166
|
use utf8; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
5
|
|
11
|
1
|
|
|
1
|
|
486
|
use Desktop::Notify; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use Encode::Guess qw/shiftjis euc-jp 7bit-jis/; |
13
|
|
|
|
|
|
|
use Encode qw/from_to decode encode/; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
require Exporter; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
20
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
21
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# This allows declaration use Net::Chaton::API ':all'; |
24
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
25
|
|
|
|
|
|
|
# will save memory. |
26
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
) ] ); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
our @EXPORT = qw( |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Preloaded methods go here. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
our $ua = LWP::UserAgent->new; |
42
|
|
|
|
|
|
|
our $json = JSON->new->allow_nonref; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub new {#{{{ |
45
|
|
|
|
|
|
|
my $class = shift; |
46
|
|
|
|
|
|
|
my $self = { |
47
|
|
|
|
|
|
|
who => 'Net::Chaton::API', |
48
|
|
|
|
|
|
|
@_, |
49
|
|
|
|
|
|
|
}; |
50
|
|
|
|
|
|
|
return bless($self, $class); |
51
|
|
|
|
|
|
|
}#}}} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub login {#{{{ |
54
|
|
|
|
|
|
|
my $self = shift; |
55
|
|
|
|
|
|
|
defined($self->{'room'}) or die "Error::Room uri is undefined"; |
56
|
|
|
|
|
|
|
my $apilogin_url = $self->{'room'} . "apilogin"; |
57
|
|
|
|
|
|
|
my %postdata = ( |
58
|
|
|
|
|
|
|
who => $self->{who}, |
59
|
|
|
|
|
|
|
s => 0, |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
my $req = POST($apilogin_url, [%postdata]); |
62
|
|
|
|
|
|
|
my $responce = $ua->request($req); |
63
|
|
|
|
|
|
|
my $decoded_responce = $json->decode($responce->content); |
64
|
|
|
|
|
|
|
$self->{'post-uri'} = $decoded_responce->{'post-uri'}; |
65
|
|
|
|
|
|
|
$self->{'comet-uri'} = $decoded_responce->{'comet-uri'}; |
66
|
|
|
|
|
|
|
$self->{'cid'} = $decoded_responce->{'cid'}; |
67
|
|
|
|
|
|
|
$self->{'pos'} = $decoded_responce->{'pos'}; |
68
|
|
|
|
|
|
|
}#}}} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub Post {#{{{ |
71
|
|
|
|
|
|
|
my ($self, $nick, $message) = @_; |
72
|
|
|
|
|
|
|
my $enc_nick = guess_encoding($nick); |
73
|
|
|
|
|
|
|
my $enc_message = guess_encoding($message); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
if(ref $enc_nick) { |
76
|
|
|
|
|
|
|
from_to($nick,$enc_nick->name, 'utf8'); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
if(ref $enc_message) { |
79
|
|
|
|
|
|
|
from_to($message,$enc_message->name, 'utf8'); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
my %postdata = ( |
82
|
|
|
|
|
|
|
nick => $nick, |
83
|
|
|
|
|
|
|
text => $message, |
84
|
|
|
|
|
|
|
cid => $self->{'cid'}, |
85
|
|
|
|
|
|
|
); |
86
|
|
|
|
|
|
|
my $req = HTTP::Request::Common::POST($self->{'post-uri'}, [%postdata]); |
87
|
|
|
|
|
|
|
$ua->request($req); |
88
|
|
|
|
|
|
|
}#}}} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub Observe{#{{{ |
91
|
|
|
|
|
|
|
my ($self,$p, $c) = @_; |
92
|
|
|
|
|
|
|
my $decoded_responce; |
93
|
|
|
|
|
|
|
if(defined($p) && defined($c)) { |
94
|
|
|
|
|
|
|
my $req = GET("$self->{'comet-uri'}?p=$p&c=$c&s=0"); |
95
|
|
|
|
|
|
|
my $res = $ua->request($req); |
96
|
|
|
|
|
|
|
$decoded_responce = $json->decode($res->content); |
97
|
|
|
|
|
|
|
$self->{'cid'} = $decoded_responce->{'cid'}; |
98
|
|
|
|
|
|
|
$self->{'pos'} = $decoded_responce->{'pos'}; |
99
|
|
|
|
|
|
|
if($decoded_responce->{'content'} eq ""){ |
100
|
|
|
|
|
|
|
#if responce is empty the observe again immidietry. |
101
|
|
|
|
|
|
|
@_ = ($self,$self->{'pid'}, $self->{'cid'}); |
102
|
|
|
|
|
|
|
goto &Observe; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
else { |
106
|
|
|
|
|
|
|
my $req = GET("$self->{'comet-uri'}?p=$self->{'pos'}&c=$self->{'cid'}&s=0"); |
107
|
|
|
|
|
|
|
my $res = $ua->request($req); |
108
|
|
|
|
|
|
|
$decoded_responce = $json->decode($res->content); |
109
|
|
|
|
|
|
|
$self->{'cid'} = $decoded_responce->{'cid'}; |
110
|
|
|
|
|
|
|
$self->{'pos'} = $decoded_responce->{'pos'}; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# if content is empty retry immediately |
113
|
|
|
|
|
|
|
if($decoded_responce->{'content'} eq "") { |
114
|
|
|
|
|
|
|
@_ = ($self,$self->{'pid'}, $self->{'cid'}); |
115
|
|
|
|
|
|
|
goto &Observe; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
my $name = @{$decoded_responce->{'content'}}[0]->[0]; |
119
|
|
|
|
|
|
|
my $txt = @{$decoded_responce->{'content'}}[0]->[2]; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $enc_name = guess_encoding($name); |
122
|
|
|
|
|
|
|
my $enc_txt = guess_encoding($txt); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
if(ref $enc_name) { |
125
|
|
|
|
|
|
|
from_to($name,$enc_name->name, 'utf8'); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
if(ref $enc_txt) { |
128
|
|
|
|
|
|
|
from_to($txt,$enc_txt->name, 'utf8'); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
my $notify = Desktop::Notify->new(); |
132
|
|
|
|
|
|
|
$notify->create( |
133
|
|
|
|
|
|
|
summary => $name, |
134
|
|
|
|
|
|
|
body => $txt, |
135
|
|
|
|
|
|
|
timeout => 5000)->show(); |
136
|
|
|
|
|
|
|
@_ = ($self,$self->{'pid'}, $self->{'cid'}); |
137
|
|
|
|
|
|
|
goto &Observe; |
138
|
|
|
|
|
|
|
}#}}} |
139
|
|
|
|
|
|
|
__END__ |