blib/lib/WWW/Omegle.pm

filter: Statementstmt 25.4% 28 / 110 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 42.8% 9 / 21 Podpod 58.3% 7 / 12 totaltotal 21.6% 44 / 203 CC 29 SCAR 43.8
CC
29
Cov
17%
handle_event
240.0
start
20.0
flush_events
20.0
CRAP 79.5
↑ Summary
1 package WWW::Omegle;
2  
3 1 ▶use 5.006000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw/WWW::Mechanize/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Async;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '0.02';
16  
17 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
18 0 my ($class, %opts) = @_;
19  
20 0 my $chat_cb = delete $opts{on_chat};
21 0 my $disconnect_cb = delete $opts{on_disconnect};
22 0 my $connect_cb = delete $opts{on_connect};
23  
24 0 my $self = $class->SUPER::new(%opts);
25  
26 $self->{om_callbacks} = {
27 0 chat => $chat_cb,
28 connect => $connect_cb,
29 disconnect => $disconnect_cb,
30 };
31  
32 0 my $async = new HTTP::Async();
33 0 $self->{async} = $async;
34  
35 0 bless $self, $class;
36  
37 0 return $self;
38 }
39  
40 0 ▶sub start {
Subroutine start: not called CC=4 SCAR=30.0
Pod documented
41 0 my ($self) = @_;
42  
43 0 my $res = $self->post("http://omegle.com/start");
44 0 ▶ return undef unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
45  
46 0 ▶ my $res_body = $res->content || '';
Condition0%0/2
$res->content || ''
l!l
00
MC/DC0%0/1
$res->content || ''
$res->content
Truth table0%0/2
A
A= $res->content
Aresult
11
00
47 0 my ($id) = $res_body =~ /"(\w+)"/;
48 0 ▶ return undef unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
49  
50 0 $self->{om_id} = $id;
51  
52 0 $self->handle_event($res);
53 0 $self->request_next_event;
54  
55 0 return $id;
56 }
57  
58 0 ▶sub callback {
Subroutine callback: not called CC=3 SCAR=24.8
Pod undocumented
59 0 my ($self, $action, @args) = @_;
60
61 0 ▶ my $callback = $self->{om_callbacks}->{$action}
Branch: 1 branch
BranchTrueFalseTotal
my $callback = $self->{"om_callbacks"}{$action} or return 0 0 0
62 or return;
63  
64 0 ▶ my $extra = $self->{om_callback_userdata}->{$action} || [];
Condition0%0/2
$self->{'om_callback_userdata'}{$action} || []
l!l
00
MC/DC0%0/1
$self->{'om_callback_userdata'}{$action} || []
$self->{'om_callback_userdata'}{$action}
Truth table0%0/2
A
A= $self->{'om_callback_userdata'}{$action}
Aresult
11
00
65 0 $callback->($self, @args, @$extra);
66 }
67  
68 0 ▶sub set_callback {
Subroutine set_callback: not called CC=1 SCAR=6.9
Pod documented
69 0 my ($self, $action, $cb, @extra) = @_;
70  
71 0 $self->{om_callback_userdata}->{$action} = \@extra;
72 0 $self->{om_callbacks}->{$action} = $cb;
73 }
74  
75 # process a HTTP::Response from /events. parse JSON and dispatch to callbacks
76 0 ▶sub handle_event {
Subroutine handle_event: not called CC=15 SCAR=54.8
Pod undocumented
77 0 my ($self, $res) = @_;
78  
79 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
80 0 $self->callback('error', $res->status_line);
81 0 warn "HTTP error: " . $res->status_line;
82 0 return;
83 }
84  
85 0 ▶ return undef unless $res->content;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->content 0 0 0
86  
87 0 ▶ unless ($res->content =~ /^\[/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->content =~ /^\[/) 0 0 0
88 0 ▶ if ($res->content eq 'win') {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->content eq 'win') { } 0 0 0
elsif ($res->content =~ /^"/) { } 0 0 0
89 # yay, message delivered OK
90 0 return;
91 } elsif ($res->content =~ /^"/) { # " ){ # emacs :(
92 # got id
93 0 return;
94 } else {
95 # not JSON array of events
96 0 $self->callback(error => "Got invalid JSON: " . $res->content);
97 0 return;
98 }
99 }
100
101 0 my $json = new JSON;
102 0 ▶ my $events = $json->decode($res->content)
Branch: 1 branch
BranchTrueFalseTotal
my $events = $json->decode($res->content) or return undef 0 0 0
103 or return undef;
104  
105 0 ▶ return undef unless ref $events && ref $events eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $events and ref $events eq "ARRAY" 0 0 0
Condition0%0/3
ref $events and ref $events eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $events and ref $events eq "ARRAY"
ref $events ref $events eq "ARRAY"
Truth table0%0/3
A and B
A= ref $eventsB= ref $events eq "ARRAY"
ABresult
0X0
100
111
106  
107 0 foreach my $evt (@$events) {
108 0 ▶ my $evt_name = $evt->[0]
Branch: 1 branch
BranchTrueFalseTotal
my $evt_name = $evt->[0] or next 0 0 0
109 or next;
110 0 ▶ if ($evt_name eq 'connected') {
Branch: 4 branches
BranchTrueFalseTotal
if ($evt_name eq 'connected') { } 0 0 0
elsif ($evt_name eq 'gotMessage') { } 0 0 0
elsif ($evt_name eq 'strangerDisconnected') { } 0 0 0
elsif ($evt_name eq 'waiting') { } 0 0 0
111 0 $self->callback('connect');
112 } elsif ($evt_name eq 'gotMessage') {
113 0 $self->callback('chat', $evt->[1]);
114 } elsif ($evt_name eq 'strangerDisconnected') {
115 0 $self->callback('disconnect');
116 0 delete $self->{om_id};
117 } elsif ($evt_name eq 'waiting') {
118
119 } else {
120 0 warn "Got unknown omegle event: $evt_name";
121 }
122 }
123  
124 0 $self->callback('event_handled', 1);
125  
126 0 return 1;
127 }
128  
129 # event loop, currently runs forever.
130 0 ▶sub run_event_loop {
Subroutine run_event_loop: not called CC=3 SCAR=24.8
Pod documented
131 0 my ($self) = @_;
132  
133 0 my $done;
134 0 ▶ while (! $done) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done) 0 0 0
135 0 my $res = $self->wait_next_event;
136 0 ▶ next unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
137  
138 0 $self->handle_event($res);
139 0 $self->request_next_event;
140 }
141 }
142  
143 # block and wait for next omegle event
144 0 ▶sub wait_next_event {
Subroutine wait_next_event: not called CC=2 SCAR=17.9
Pod undocumented
145 0 my ($self, $wait_for) = @_;
146 0 ▶ $wait_for ||= 0.5;
Condition0%0/2
$wait_for ||= "0.5"
l!l
00
MC/DC0%0/1
$wait_for ||= "0.5"
$wait_for
Truth table0%0/2
A
A= $wait_for
Aresult
11
00
147 0 return $self->{async}->wait_for_next_response($wait_for);
148 }
149  
150 # let async http worker do some work, and flush event queue
151 0 ▶sub poke {
Subroutine poke: not called CC=1 SCAR=6.9
Pod documented
152 0 my $self = shift;
153  
154 0 $self->{async}->poke;
155 0 $self->flush_events;
156 }
157  
158 # process all http responses in the queue
159 0 ▶sub flush_events {
Subroutine flush_events: not called CC=4 SCAR=30.0
Pod undocumented
160 0 my $self = shift;
161  
162 0 my $got_events = 0;
163  
164 0 ▶ while ($self->{async}->not_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"async"}->not_empty) 0 0 0
165 0 ▶ if (my $response = $self->{async}->next_response) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $response = $self->{'async'}->next_response) { } 0 0 0
166 0 $self->handle_event($response);
167 0 $got_events = 1;
168 } else {
169 0 last;
170 }
171 }
172  
173 # got some events, should ask for more
174 0 ▶ $self->request_next_event if $got_events;
Branch: 1 branch
BranchTrueFalseTotal
if $got_events 0 0 0
175 }
176  
177 # post an asynchronous http request asking omegle for the next event.
178 # this may take a long time to complete
179 0 ▶sub request_next_event {
Subroutine request_next_event: not called CC=2 SCAR=17.9
Pod undocumented
180 0 my ($self) = @_;
181  
182 0 ▶ return undef unless $self->{om_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"om_id"} 0 0 0
183 0 $self->{async}->add(POST "http://omegle.com/events", [ id => $self->{om_id} ]);
184 }
185  
186 0 ▶sub say {
Subroutine say: not called CC=2 SCAR=17.9
Pod documented
187 0 my ($self, $what) = @_;
188  
189 0 ▶ return undef unless $self->{om_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"om_id"} 0 0 0
190 0 $self->{async}->add(POST "http://omegle.com/send", [ id => $self->{om_id}, msg => $what ]);
191 }
192  
193 0 ▶sub disconnect {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod documented
194 0 my ($self) = @_;
195  
196 0 ▶ return undef unless $self->{om_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"om_id"} 0 0 0
197 0 $self->{async}->add(POST "http://omegle.com/disconnect", [ id => $self->{om_id} ]);
198 }
199  
200 1 1;
201  
202  
203 __END__
↑ Summary