| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Net::RVP - implementation of the Rendez-Vous Protocol for instant messaging | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | my $rvp = RVP->new( | ||
| 8 | host => '1.2.3.4:80', | ||
| 9 | name => 'First_Last', | ||
| 10 | user => 'domain\\username', | ||
| 11 | pass => 'password', | ||
| 12 | site => 'machine.domain.com'); | ||
| 13 | |||
| 14 | $rvp->login(); | ||
| 15 | |||
| 16 | # ... | ||
| 17 | |||
| 18 | =head1 METHODS | ||
| 19 | |||
| 20 | =cut | ||
| 21 | package Net::RVP; | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use LWP::UserAgent; | ||
| 25 | use HTTP::Request; | ||
| 26 | use HTTP::Response; | ||
| 27 | use HTTP::Status; | ||
| 28 | use URI; | ||
| 29 | use XML::Simple; | ||
| 30 | use Digest::MD5 qw(md5_hex); | ||
| 31 | use Time::HiRes qw(gettimeofday); | ||
| 32 | |||
| 33 | use Net::RVP::User; | ||
| 34 | use Net::RVP::Session; | ||
| 35 | use Net::RVP::Sink; | ||
| 36 | |||
| 37 | our $Debug = 0; | ||
| 38 | |||
| 39 | |||
| 40 | =head2 new ( named parameters ) | ||
| 41 | |||
| 42 | Requires host (this machine, dotted-IP acceptable, for callbacks), name | ||
| 43 | ('Joe_Smith'), user ('hq\jsmith'), password, and site for server (optional | ||
| 44 | ':port' at end allowed). | ||
| 45 | |||
| 46 | Optional: agent - user-agent string; renew - renewal time for subscriptions; | ||
| 47 | sink - event sink object (see L<Net::RVP::Sink>). | ||
| 48 | |||
| 49 | =cut | ||
| 50 | sub new { | ||
| 51 | my ($class,%param) = @_; | ||
| 52 | die 'host, name, user, pass, and site required' if | ||
| 53 | grep { not $param{$_} } qw(host name user pass site); | ||
| 54 | my $self; | ||
| 55 | @{$self}{qw(host name user pass site sink)} = | ||
| 56 | delete @param{qw(host name user pass site sink)}; | ||
| 57 | bless $self, ref $class || $class; | ||
| 58 | $Debug = delete $param{debug}; | ||
| 59 | $self->{port} = ($self->{site} =~ s/:(\d+)$//) ? $1 : 80; | ||
| 60 | $self->{agent} = delete $param{agent}; | ||
| 61 | $self->{renew} = delete $param{renew} || 14400; | ||
| 62 | |||
| 63 | $self->{parser} = XML::Simple->new(ContentKey => '', ForceArray => 0, | ||
| 64 | ForceContent => 0, KeepRoot => 1); | ||
| 65 | |||
| 66 | die "unknown parameter(s): ".(join ', ', keys %param) if keys %param; | ||
| 67 | |||
| 68 | $_ = lc $_ for(@{$self}{qw(name site)}); | ||
| 69 | |||
| 70 | $self->{sink} ||= Net::RVP::Sink->new(); | ||
| 71 | $self->{sink}->RVP($self); | ||
| 72 | |||
| 73 | return $self; | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | =head2 login | ||
| 78 | |||
| 79 | Try to log in, returns false on error, renewal time otherwise. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | sub login { | ||
| 83 | my ($self,$agent) = @_; | ||
| 84 | $self->logout(); # if logged in | ||
| 85 | |||
| 86 | $self->{ua} = LWP::UserAgent->new(keep_alive => 1); | ||
| 87 | $self->{ua}->agent($self->{agent}) if $self->{agent}; # MS is msmsgs/4.6.0.84 | ||
| 88 | |||
| 89 | # cb is the callback, node is without the port i.e. for the request URL | ||
| 90 | # NB: we get an access error if we include the port for a foreign SUBSCRIBE! | ||
| 91 | # i.e. if it's not port 80 it probably won't work | ||
| 92 | $self->{base} = | ||
| 93 | "http://$self->{site}".($self->{port}==80 ? '' : ":$self->{port}"); | ||
| 94 | $self->{url} = "$self->{base}/instmsg/aliases/$self->{name}"; | ||
| 95 | |||
| 96 | $self->{ua}->credentials("$self->{site}:$self->{port}",'', | ||
| 97 | $self->{user},$self->{pass}); | ||
| 98 | |||
| 99 | # link logical address to our server | ||
| 100 | my $req = $self->_request(SUBSCRIBE => "/instmsg/aliases/$self->{name}", | ||
| 101 | call_back => "http://$self->{host}", notification_type => 'pragma/notify', | ||
| 102 | subscription_lifetime => $self->{renew}); | ||
| 103 | my $res = $self->_send($req); | ||
| 104 | return 0 unless $res->is_success(); | ||
| 105 | |||
| 106 | # set login flag and start subscriptions, users, and conversations lists | ||
| 107 | $self->{login} = $res->header('subscription-id'); | ||
| 108 | $self->{subs} = {}; # don't count the above, we can't unsubscribe it anyway | ||
| 109 | $self->{users} = {}; # keyed by user URL | ||
| 110 | $self->{sesss} = {}; # keyed by Session-Id | ||
| 111 | |||
| 112 | # create our own user object to get our properties | ||
| 113 | my $me = $self->user($self->{name}); | ||
| 114 | $me->watch(); | ||
| 115 | |||
| 116 | # clear out old subscriptions | ||
| 117 | my $id = $res->header('subscription_id'); | ||
| 118 | my @id = grep { $_->{id} != $id } $self->_subscriptions($self->{name}); | ||
| 119 | $self->_unsubscribe($_) for @id; | ||
| 120 | |||
| 121 | return $res->header('subscription_lifetime'); | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | =head2 renew | ||
| 126 | |||
| 127 | Renew login subscription. Returns new renewal time, false on error. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | sub renew { | ||
| 131 | my $self = shift; | ||
| 132 | die 'not logged in' unless $self->{login}; | ||
| 133 | |||
| 134 | my $req = $self->_request(SUBSCRIBE => "/instmsg/aliases/$self->{name}", | ||
| 135 | subscription_id => $self->{login}, subscription_lifetime => $self->{renew}); | ||
| 136 | my $res = $self->_send($req); | ||
| 137 | return $res->is_success() ? $self->{renew} : 0; | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | =head2 logout | ||
| 142 | |||
| 143 | Log out. | ||
| 144 | |||
| 145 | =cut | ||
| 146 | sub logout { | ||
| 147 | my $self = shift; | ||
| 148 | return unless $self->{login}; | ||
| 149 | |||
| 150 | # unsubscribe to everything (except the first, which returns not implemented) | ||
| 151 | $self->_unsubscribe($self->{subs}->{$_}) for keys %{$self->{subs}}; | ||
| 152 | $self->{subs} = {}; | ||
| 153 | $self->{login} = 0; | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | =head2 base | ||
| 158 | |||
| 159 | Return the base address of the server e.g. http://foo.bar:port | ||
| 160 | |||
| 161 | =cut | ||
| 162 | sub base { | ||
| 163 | shift->{base} | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | =head2 url | ||
| 168 | |||
| 169 | Return local node (must be logged in) as http URL. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | sub url { | ||
| 173 | shift->{url} | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | =head2 name | ||
| 178 | |||
| 179 | Return user name (short form, e.g. jsmith or bob_jones). | ||
| 180 | |||
| 181 | =cut | ||
| 182 | sub name { | ||
| 183 | shift->{name} | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | =head2 server | ||
| 188 | |||
| 189 | Return name of the remote server we're hitting, with no 'http' etc. in front. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | sub server { | ||
| 193 | shift->{site} | ||
| 194 | } | ||
| 195 | |||
| 196 | |||
| 197 | =head2 host | ||
| 198 | |||
| 199 | Return local callback: (address, port) in list context, proto://address:port in | ||
| 200 | scalar context. | ||
| 201 | |||
| 202 | =cut | ||
| 203 | sub host { | ||
| 204 | my $self = shift; | ||
| 205 | if(wantarray) { | ||
| 206 | return ($1,$2) if $self->{host} =~ /^([^:]+):(\d+)$/; | ||
| 207 | return ($self->{host},80); | ||
| 208 | } | ||
| 209 | return "http://$self->{host}"; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | =head2 self | ||
| 214 | |||
| 215 | Return our own user object. | ||
| 216 | |||
| 217 | =cut | ||
| 218 | sub self { | ||
| 219 | my $self = shift; | ||
| 220 | return $self->{users}->{$self->{url}}; | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | =head2 user ( name | URL ) | ||
| 225 | |||
| 226 | Create and return a user object. Doesn't necessarily imply that the user | ||
| 227 | exists. Name should be the short name on the server ('jsmith'), or a URL. | ||
| 228 | |||
| 229 | =cut | ||
| 230 | sub user { | ||
| 231 | my ($self,$url) = @_; | ||
| 232 | $url = "$self->{base}/instmsg/aliases/$url" unless $url =~ /^(\w+):/; | ||
| 233 | $url = lc $url; | ||
| 234 | if(my $obj = $self->{users}->{$url}) { | ||
| 235 | return $obj; | ||
| 236 | } | ||
| 237 | return $self->{users}->{$url} = Net::RVP::User->new($self,$url); | ||
| 238 | } | ||
| 239 | |||
| 240 | |||
| 241 | =head2 session | ||
| 242 | |||
| 243 | Create and return a session (conversation) object (see L<Net::RVP::Session>). | ||
| 244 | |||
| 245 | =cut | ||
| 246 | sub session { | ||
| 247 | my $self = shift; | ||
| 248 | |||
| 249 | # create a session-id (GUID); use MD5, insert hyphens, convert to uppercase | ||
| 250 | # we want {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} (8,4,4,12) where x = %X | ||
| 251 | # md5_hex emits data like '098f6bcd4621d373cade4e832627b4f6' | ||
| 252 | for(my $tries = 10; $tries >= 0; $tries--) { | ||
| 253 | my $guid = uc md5_hex("$self->{host}:$self->{user}:".(scalar gettimeofday). | ||
| 254 | rand); | ||
| 255 | $guid =~ s/^(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})$/{$1-$2-$3-$4-$5}/; | ||
| 256 | return $self->{sesss}->{$guid} = Net::RVP::Session->new($self,$guid) | ||
| 257 | unless $self->{sesss}->{$guid}; | ||
| 258 | die "can't create unique GUID!" unless $tries; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | # internal sub: pass method, URL (or just a path), and headers (name => value) | ||
| 264 | # returns an HTTP::Request object; adds default required headers | ||
| 265 | # special 'XML' header means set content and content-type; adds XML version | ||
| 266 | sub _request { | ||
| 267 | my ($self,$type,$url,%param) = @_; | ||
| 268 | |||
| 269 | $url = $self->{url} unless $url; | ||
| 270 | unless($url =~ /^\w+:/) { | ||
| 271 | $url =~ s!^/!!; | ||
| 272 | $url = "http://$self->{site}/$url"; | ||
| 273 | } | ||
| 274 | |||
| 275 | my $req = HTTP::Request->new($type => $url); | ||
| 276 | |||
| 277 | if(my $content = delete $param{XML}) { | ||
| 278 | $content = "<?xml version=\"1.0\"?>\n$content"; | ||
| 279 | $req->content($content); # content-length set in _sendXML | ||
| 280 | $param{content_type} = 'text/xml'; | ||
| 281 | } | ||
| 282 | |||
| 283 | $param{host} ||= $self->{site}; # w/o the port | ||
| 284 | $param{RVP_notifications_version} ||= 0.2; | ||
| 285 | $param{RVP_from_principal} ||= $self->{url}; | ||
| 286 | |||
| 287 | $req->header(%param); | ||
| 288 | return $req; | ||
| 289 | } | ||
| 290 | |||
| 291 | |||
| 292 | # pass true parameter to get the response back else just true/success false/fail | ||
| 293 | # returns: result object | ||
| 294 | sub _send { | ||
| 295 | my ($self,$req) = @_; | ||
| 296 | |||
| 297 | # set content-length header if needed | ||
| 298 | my $content = $req->content_ref(); | ||
| 299 | $req->header(content_length => length $$content) if length $$content; | ||
| 300 | |||
| 301 | $Debug->("sent:\n".$req->as_string()."\n---\n") if $Debug; | ||
| 302 | my $res = $self->{ua}->request($req); | ||
| 303 | $Debug->("got:\n".$res->as_string()."\n---\n") if $Debug; | ||
| 304 | return $res; | ||
| 305 | } | ||
| 306 | |||
| 307 | |||
| 308 | # normalize schemas using the passed-in map (helper for _sendXML) | ||
| 309 | # we return rather than changing in place because we need to change keys... | ||
| 310 | sub _normalize { | ||
| 311 | my ($map,$tree) = @_; | ||
| 312 | my %new; | ||
| 313 | while(my ($k,$v) = each %{$tree}) { | ||
| 314 | if($k =~ /^(\w+):/ and $map->{$1}) { | ||
| 315 | my $prefix = $1; | ||
| 316 | $k =~ s/^$prefix/$map->{$prefix}/; | ||
| 317 | } | ||
| 318 | $v = [$v] unless ref $v eq 'ARRAY'; | ||
| 319 | for(@{$v}) { | ||
| 320 | $_ = _normalize($map,$_) if ref eq 'HASH'; | ||
| 321 | } | ||
| 322 | $new{$k} = @{$v} > 1 ? $v : $v->[0]; | ||
| 323 | } | ||
| 324 | \%new | ||
| 325 | } | ||
| 326 | |||
| 327 | |||
| 328 | # wrap send: return (response object, parsed XML) on success, (response object) | ||
| 329 | # on failure | ||
| 330 | sub _sendXML { | ||
| 331 | die unless wantarray; | ||
| 332 | my ($self,$req) = @_; | ||
| 333 | my $res = $self->_send($req); | ||
| 334 | return ($res) unless $res->is_success(); | ||
| 335 | |||
| 336 | my $tree; | ||
| 337 | $tree = $self->_parseXML($res->content()) | ||
| 338 | if $res->header('content_type') =~ /^text\/xml\b/i; | ||
| 339 | return ($res,$tree); | ||
| 340 | } | ||
| 341 | |||
| 342 | |||
| 343 | # internal method to parse and normalize XML | ||
| 344 | my %canon = ('DAV:' => 'd', | ||
| 345 | 'http://schemas.microsoft.com/rvp/' => 'r', | ||
| 346 | 'http://schemas.microsoft.com/rvp/acl/' => 'a', | ||
| 347 | ); | ||
| 348 | sub _parseXML { | ||
| 349 | my ($self,$data) = @_; | ||
| 350 | my $tree = eval { $self->{parser}->XMLin($data); }; # dies on error | ||
| 351 | if($@) { | ||
| 352 | print STDERR "parse failed: $data\n"; | ||
| 353 | die $@; | ||
| 354 | } | ||
| 355 | |||
| 356 | # normalize the XML schemas: d=DAV, r=RVP, a=RVL ACL | ||
| 357 | # we need the top level form XMLin for the xmlns declarations, but we can go | ||
| 358 | # ahead and strip it off for our clients | ||
| 359 | my %map; | ||
| 360 | my @keys = keys %{$tree}; | ||
| 361 | my $top = $tree->{$keys[0]}; | ||
| 362 | $top = $top->[0] if ref $top eq 'ARRAY'; | ||
| 363 | if(ref $top eq 'HASH') { | ||
| 364 | while(my ($k,$v) = each %{$top}) { | ||
| 365 | if($k =~ /^xmlns:(\w+)$/) { | ||
| 366 | $map{$1} = $canon{$v} if exists $canon{$v}; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | } | ||
| 370 | return _normalize(\%map,$tree); | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | # helper to look through XML for a 'path' | ||
| 375 | # | ||
| 376 | # pass in an XML::Simple-type hash representing an XML document, | ||
| 377 | # and a list of element names, which should be contained within each other | ||
| 378 | # e.g. qw(foo bar baz) => <foo><bar><baz>[returns this]</baz></bar></foo> | ||
| 379 | # the last element may be '1' to force returning an arrayref | ||
| 380 | # append '?' to a (non-final) element to make it optional (i.e. skip if absent) | ||
| 381 | sub _findTree { | ||
| 382 | my ($self,$top) = splice @_,0,2; | ||
| 383 | my $path = ''; | ||
| 384 | push @_,'0' unless $#_ >= 0 and $_[-1] eq '1'; | ||
| 385 | while(defined(my $elem = shift)) { | ||
| 386 | my $optional = $elem =~ s/\?$//; | ||
| 387 | my $multi = ref $top eq 'ARRAY'; | ||
| 388 | return $multi ? $top : [$top] if $elem eq '1'; | ||
| 389 | |||
| 390 | if($multi) { | ||
| 391 | die "too many elements at $path" unless $#$top < 1; | ||
| 392 | $top = $top->[0]; | ||
| 393 | } | ||
| 394 | return $top if $elem eq '0'; | ||
| 395 | $path .= "/$elem"; # (add to error path even if optional) | ||
| 396 | |||
| 397 | if(exists $top->{$elem}) { | ||
| 398 | $top = $top->{$elem}; | ||
| 399 | } elsif(not $optional) { | ||
| 400 | die "can't find $path"; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | |||
| 406 | # helper to look through XML for a text string | ||
| 407 | sub _findText { | ||
| 408 | my $self = shift; | ||
| 409 | my $ret = $self->_findTree(@_); | ||
| 410 | $ret = $ret->{''} if ref $ret eq 'HASH' and exists $ret->{''}; | ||
| 411 | die "expected text, got reference [@_]" if ref $ret; | ||
| 412 | return $ret; | ||
| 413 | } | ||
| 414 | |||
| 415 | |||
| 416 | # helper to look through XML for a list | ||
| 417 | sub _findArray { | ||
| 418 | my $self = shift; | ||
| 419 | return $self->_findTree(@_,1); | ||
| 420 | } | ||
| 421 | |||
| 422 | |||
| 423 | =head2 status ( value ) | ||
| 424 | |||
| 425 | Set online status ('online', 'offline', others...). | ||
| 426 | |||
| 427 | =cut | ||
| 428 | sub status { | ||
| 429 | my ($self,$value) = @_; | ||
| 430 | my $req = $self->_request(PROPPATCH => 0, XML => <<XML); | ||
| 431 | <d:propertyupdate xmlns:d="DAV:" xmlns:r="http://schemas.microsoft.com/rvp/"> | ||
| 432 | <d:set><d:prop><r:state><r:leased-value> | ||
| 433 | <r:value><r:$value/></r:value> | ||
| 434 | <r:default-value><r:offline/></r:default-value> | ||
| 435 | <r:timeout>$self->{renew}</r:timeout> | ||
| 436 | </r:leased-value></r:state></d:prop></d:set> | ||
| 437 | </d:propertyupdate> | ||
| 438 | XML | ||
| 439 | return $self->_send($req)->is_success(); | ||
| 440 | } | ||
| 441 | |||
| 442 | |||
| 443 | # _type ( type ) | ||
| 444 | # | ||
| 445 | # Return canonical type given index, leaves alone if not an in-range number. | ||
| 446 | # | ||
| 447 | my @Type = qw(pragma/notify update/propchange); | ||
| 448 | sub _type { | ||
| 449 | my $type = shift; | ||
| 450 | $type ||= 0; | ||
| 451 | $type = $Type[$type] if $type =~ /^\d+$/ and $type <= $#Type; | ||
| 452 | $type | ||
| 453 | } | ||
| 454 | |||
| 455 | |||
| 456 | # _subscriptions | ||
| 457 | # | ||
| 458 | # Stores a list of (user uri,id,principal,href,timeout) hashrefs. The URI | ||
| 459 | # and the principal will probably be the same (and match the user). | ||
| 460 | # | ||
| 461 | sub _subscriptions { | ||
| 462 | die unless wantarray; | ||
| 463 | my ($self,$user,$type) = @_; | ||
| 464 | $type = _type($type); | ||
| 465 | my $req = $self->_request(SUBSCRIPTIONS => "/instmsg/aliases/$user", | ||
| 466 | notification_type => $type); | ||
| 467 | |||
| 468 | my ($res,$data) = $self->_sendXML($req); | ||
| 469 | return 0 unless $data; | ||
| 470 | |||
| 471 | # get our URI | ||
| 472 | my $uri = $req->uri(); | ||
| 473 | $uri = $uri->as_string() if ref $uri; | ||
| 474 | |||
| 475 | # walk returned subscriptions | ||
| 476 | my $subs = $self->_findArray($data,qw(r:subscriptions? r:subscription)); | ||
| 477 | |||
| 478 | my @ret; | ||
| 479 | for my $sub(@{$subs}) { | ||
| 480 | my $pr = $data->{'a:principal'}; | ||
| 481 | $pr = $pr->{'a:rvp-principal'} if ref $pr; | ||
| 482 | push @ret, { uri => $uri, principal => $pr, href => $sub->{'d:href'}, | ||
| 483 | id => $sub->{'r:subscription-id'}, timeout => $sub->{'d:timeout'} }; | ||
| 484 | } | ||
| 485 | |||
| 486 | @ret; | ||
| 487 | } | ||
| 488 | |||
| 489 | |||
| 490 | # _unsubscribe ( item from subscriptions() call ) | ||
| 491 | # | ||
| 492 | # Returns true on success. | ||
| 493 | # | ||
| 494 | sub _unsubscribe { | ||
| 495 | my ($self,$item) = @_; | ||
| 496 | my ($href,$id) = @{$item}{qw(principal id)}; | ||
| 497 | my $req = $self->_request(UNSUBSCRIBE => $href, subscription_id => $id); | ||
| 498 | return $self->_send($req)->is_success(); | ||
| 499 | } | ||
| 500 | |||
| 501 | |||
| 502 | =head2 notify ( HTTP::Request object ) | ||
| 503 | |||
| 504 | Pass in an HTTP request parsed from the notify server (how you obtain and parse | ||
| 505 | incoming requests is beyond the scope of this module). It will make callbacks | ||
| 506 | to methods in this object which may be overloaded as desired. | ||
| 507 | |||
| 508 | Returns an HTTP::Response object. | ||
| 509 | |||
| 510 | =cut | ||
| 511 | sub notify { | ||
| 512 | my ($self,$req) = @_; | ||
| 513 | my ($uri,$top) = $req->uri(); | ||
| 514 | $uri = URI->new($uri) unless ref $uri; | ||
| 515 | |||
| 516 | # notification to our node | ||
| 517 | if($req->method() eq 'NOTIFY' and $uri->path() eq '/') { | ||
| 518 | $top = $self->_findTree($self->_parseXML($req->content()),'r:notification'); | ||
| 519 | |||
| 520 | while(my($top_k,$top_v) = each %$top) { | ||
| 521 | |||
| 522 | # conversation | ||
| 523 | if($top_k eq 'r:message') { | ||
| 524 | |||
| 525 | # parse out source and destination | ||
| 526 | my $src = $self->_findText($top_v,'r:notification-from','r:contact', | ||
| 527 | 'd:href'); | ||
| 528 | $src = $self->user($src); | ||
| 529 | my $dst = $self->_findText($top_v,'r:notification-to','r:contact', | ||
| 530 | 'd:href'); | ||
| 531 | $dst = $self->user($dst); | ||
| 532 | my $msg = $self->_findText($top_v,'r:msgbody','r:mime-data'); | ||
| 533 | |||
| 534 | # parse the message | ||
| 535 | my @part = split /\x0d?\x0a\x0d?\x0a/, $msg, 2; | ||
| 536 | $part[0] =~ s/\x0d?\x0a\s+/ /g; | ||
| 537 | my @header = split /\x0d?\x0a/, $part[0]; | ||
| 538 | my %header = map { /^([^:]+):\s*(.*)$/; (lc $1 => $2) } @header; | ||
| 539 | return 'message missing session-id' | ||
| 540 | unless my $sid = uc $header{'session-id'}; | ||
| 541 | my $sess = $self->{sesss}->{$sid}; | ||
| 542 | return 'message missing content-type' | ||
| 543 | unless my $type = lc $header{'content-type'}; | ||
| 544 | $type =~ s/;.*$//; | ||
| 545 | |||
| 546 | # we might be coming in mid-session; be robust, create a new one | ||
| 547 | my $res; | ||
| 548 | unless($sess) { | ||
| 549 | $sess = $self->{sesss}->{$sid} = Net::RVP::Session->new($self,$sid); | ||
| 550 | $self->{sink}->open_event($sess); | ||
| 551 | } | ||
| 552 | unless($sess->has($src)) { | ||
| 553 | $sess->join_event($src); | ||
| 554 | $res = $self->{sink}->join_event($src,$sess); | ||
| 555 | } | ||
| 556 | |||
| 557 | # control message: open conversation or typing message | ||
| 558 | if($type eq 'text/x-msmsgscontrol') { | ||
| 559 | if($header{'typinguser'}) { | ||
| 560 | $sess->typing_event($src); | ||
| 561 | my $ret = $self->{sink}->typing_event($src,$sess); | ||
| 562 | $src->activity(); | ||
| 563 | return $ret; | ||
| 564 | } else { | ||
| 565 | $src->activity(); | ||
| 566 | return $res || HTTP::Response->new(RC_OK,'User Already Present'); | ||
| 567 | } | ||
| 568 | |||
| 569 | # part message | ||
| 570 | } elsif($type eq 'text/x-imleave') { | ||
| 571 | $sess->part_event($src); | ||
| 572 | my $ret = $self->{sink}->part_event($src,$sess); | ||
| 573 | $src->activity(); | ||
| 574 | return $ret; | ||
| 575 | |||
| 576 | # actual message, should be text/plain but we take anything | ||
| 577 | } else { | ||
| 578 | $sess->message_event($src,$part[1]); | ||
| 579 | my $ret = $self->{sink}->message_event($src,$sess,$part[1]); | ||
| 580 | $src->activity(); | ||
| 581 | return $ret; | ||
| 582 | } | ||
| 583 | |||
| 584 | # property change | ||
| 585 | } elsif($top_k eq 'r:propnotification') { | ||
| 586 | |||
| 587 | # parse out source | ||
| 588 | my $src = $self->_findText($top_v,'r:notification-from','r:contact', | ||
| 589 | 'd:href'); | ||
| 590 | $src = $self->user($src); | ||
| 591 | |||
| 592 | return $src->change_event($top_v); | ||
| 593 | |||
| 594 | # unknown key, return error | ||
| 595 | } elsif($top_k !~ /^xmlns:/) { | ||
| 596 | return "unknown top-level key for NOTIFY: '$top_k'"; | ||
| 597 | } | ||
| 598 | } | ||
| 599 | } | ||
| 600 | |||
| 601 | return 'unknown request'; | ||
| 602 | } | ||
| 603 | |||
| 604 | |||
| 605 | # DESTROY | ||
| 606 | # | ||
| 607 | # Destructor. | ||
| 608 | # | ||
| 609 | sub DESTROY { | ||
| 610 | my $self = shift; | ||
| 611 | $self->logout(); | ||
| 612 | } | ||
| 613 | |||
| 614 | |||
| 615 | =head1 AUTHOR | ||
| 616 | |||
| 617 | David Robins E<lt>dbrobins@davidrobins.netE<gt>. | ||
| 618 | |||
| 619 | =head1 SEE ALSO | ||
| 620 | |||
| 621 | L<Net::RVP::User>, L<LWP::UserAgent>, L<HTTP::Parser> for parsing HTTP requests. | ||
| 622 | |||
| 623 | =cut | ||
| 624 | |||
| 625 | |||
| 626 | 1; |