| 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
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $res_body = $res->content || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$res->content || ''
MC/DC0%0/1
$res->content || ''
$res->content
Truth table0%0/2
A
A= $res->content
| |||||||||||||||||||||||||||||
| 47 | 0 | my ($id) = $res_body =~ /"(\w+)"/; | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | return undef unless $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 62 | or return; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $extra = $self->{om_callback_userdata}->{$action} || []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'om_callback_userdata'}{$action} || []
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}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | unless ($res->content =~ /^\[/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($res->content eq 'win') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 103 | or return undef; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | return undef unless ref $events && ref $events eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $events and ref $events eq "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | foreach my $evt (@$events) { | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | my $evt_name = $evt->[0] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | or next; | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($evt_name eq 'connected') { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 135 | 0 | my $res = $self->wait_next_event; | |||||||||||||||||||||||||||
| 136 | 0 | ▶ | next unless $res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$wait_for ||= "0.5"
$wait_for
Truth table0%0/2
A
A= $wait_for
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if (my $response = $self->{async}->next_response) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 197 | 0 | $self->{async}->add(POST "http://omegle.com/disconnect", [ id => $self->{om_id} ]); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 1 | 1; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | __END__ | ||||||||||||||||||||||||||||