| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Net::RVP::User - RVP user encapsulation | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | my $user = $rvp->user('Fred_Smith'); | ||
| 8 | $user->watch() or die 'no such user'; | ||
| 9 | print "Email for ".($user->display())." is ".$user->email()."\n"; | ||
| 10 | |||
| 11 | =head1 METHODS | ||
| 12 | |||
| 13 | =cut | ||
| 14 | package Net::RVP::User; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | |||
| 18 | |||
| 19 | =head2 new ( RVP object, URL ) | ||
| 20 | |||
| 21 | Create new user object. | ||
| 22 | |||
| 23 | =cut | ||
| 24 | sub new { | ||
| 25 | my ($class,$rvp,$url) = @_; | ||
| 26 | die unless $url =~ /\/(\w+)$/; | ||
| 27 | my $self = bless { rvp => $rvp, url => $url, user => $1, prop => {} }, | ||
| 28 | ref $class || $class; | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | =head2 name | ||
| 34 | |||
| 35 | Return the short name e.g. jsmith, bob_jones. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | sub name { | ||
| 39 | shift->{user} | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | =head2 url | ||
| 44 | |||
| 45 | Return the URL representing the user. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | sub url { | ||
| 49 | shift->{url} | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | =head2 display | ||
| 54 | |||
| 55 | Return the display name for the user. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | sub display { | ||
| 59 | shift->get('d:displayname'); | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | =head2 email | ||
| 64 | |||
| 65 | Return the user email address. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | sub email { | ||
| 69 | shift->get('r:email'); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | =head2 state | ||
| 74 | |||
| 75 | Return user state (without the namespace prefix) e.g. 'idle', 'online', | ||
| 76 | 'offline', etc. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | sub state { | ||
| 80 | # OK to use cache, we get propchange notifications on changes | ||
| 81 | my $state = shift->get('r:state'); | ||
| 82 | $state =~ s/^r://; | ||
| 83 | $state | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | # helper for get_all and watch; parse out properties into hash and store/return | ||
| 88 | # pass in the data tree and an optional arrayref for _findTree | ||
| 89 | sub _parse_prop { | ||
| 90 | my ($self,$data,$where) = @_; | ||
| 91 | |||
| 92 | # if we weren't given a search path, use default for properties | ||
| 93 | $where ||= [qw(d:multistatus? d:response d:propstat d:prop)]; | ||
| 94 | |||
| 95 | # parse the returned XML | ||
| 96 | $data = $self->{rvp}->_findTree($data,@$where); | ||
| 97 | |||
| 98 | # set keys | ||
| 99 | my %ret; | ||
| 100 | while(my ($k,$v) = each %{$data}) { | ||
| 101 | if(ref $v eq 'HASH') { | ||
| 102 | my @keys = keys %$v; | ||
| 103 | $v = @keys ? $keys[0] : '' if @keys < 2; | ||
| 104 | } | ||
| 105 | $ret{$k} = $v; | ||
| 106 | } | ||
| 107 | |||
| 108 | return \%ret; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | =head2 change_event | ||
| 113 | |||
| 114 | Send XML tree for NOTIFY property change event (under r:notification). | ||
| 115 | |||
| 116 | =cut | ||
| 117 | sub change_event { | ||
| 118 | my ($self,$data) = @_; | ||
| 119 | my @ret = $self->_parse_prop($data,[qw(d:propertyupdate d:set d:prop)]); | ||
| 120 | return $self->{rvp}->{sink}->change_event($self,@ret); | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | =head2 get_all | ||
| 125 | |||
| 126 | Read all available properties for the user. | ||
| 127 | |||
| 128 | Returns hashref on success, false on failure. Some possible keys are: user | ||
| 129 | (this object), href (the canonical URL), and properties: d:displayname, | ||
| 130 | r:mobile-state, r:mobile-description, r:email, r:state (note that we normalize | ||
| 131 | the XML so the namespaces will stay the same). | ||
| 132 | |||
| 133 | =cut | ||
| 134 | sub get_all { | ||
| 135 | my $self = shift; | ||
| 136 | |||
| 137 | my $req = $self->{rvp}->_request(PROPFIND => "/instmsg/aliases/$self->{user}", | ||
| 138 | depth => 0, XML => <<XML); | ||
| 139 | <d:propfind xmlns:d="DAV:" xmlns:r="http://schemas.microsoft.com/rvp/"> | ||
| 140 | <d:allprop/> | ||
| 141 | </d:propfind> | ||
| 142 | XML | ||
| 143 | |||
| 144 | my ($res,$data) = $self->{rvp}->_sendXML($req); | ||
| 145 | return 0 unless $data; | ||
| 146 | |||
| 147 | return $self->{prop} = $self->_parse_prop($data); | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | =head2 watch | ||
| 152 | |||
| 153 | Subscribe to notifications for this user; renew if already subscribed. | ||
| 154 | |||
| 155 | Returns subscription timeout if successful, false otherwise. | ||
| 156 | |||
| 157 | =cut | ||
| 158 | sub watch { | ||
| 159 | my $self = shift; | ||
| 160 | |||
| 161 | my $req = $self->{rvp}->_request(SUBSCRIBE => | ||
| 162 | "/instmsg/aliases/$self->{user}", | ||
| 163 | subscription_lifetime => $self->{rvp}->{renew}); | ||
| 164 | |||
| 165 | # first time or renewal? | ||
| 166 | if($self->{watch}) { | ||
| 167 | $req->header(subscription_id => $self->{watch}->{id}); | ||
| 168 | } else { | ||
| 169 | $req->header(notification_type => 'update/propchange', | ||
| 170 | call_back => $self->{rvp}->{url}); | ||
| 171 | } | ||
| 172 | |||
| 173 | my ($res,$data) = $self->{rvp}->_sendXML($req); | ||
| 174 | return 0 unless $res->is_success(); | ||
| 175 | return $self->{rvp}->{renew} if not $data; # re-subscribe gets no XML | ||
| 176 | |||
| 177 | # we get sent all properties, like a get_all, so set them | ||
| 178 | $self->{prop} = $self->_parse_prop($data); | ||
| 179 | |||
| 180 | # store the subscription id | ||
| 181 | $self->{watch} = { principal => $req->uri(), | ||
| 182 | id => $res->header('subscription_id') }; | ||
| 183 | $self->{rvp}->{subs}->{$self->{watch}->{id}} = $self->{watch}; | ||
| 184 | |||
| 185 | return $self->{rvp}->{renew}; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | =head2 unwatch | ||
| 190 | |||
| 191 | Stop watching (unsubscribe). | ||
| 192 | |||
| 193 | Note that we don't unsubscribe on object destruction (because global | ||
| 194 | destruction order may be wrong), but we do when the RVP object is destroyed. | ||
| 195 | |||
| 196 | =cut | ||
| 197 | sub unwatch { | ||
| 198 | my $self = shift; | ||
| 199 | return unless $self->{watch}; | ||
| 200 | $self->{rvp}->unsubscribe($self->{watch}); | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | =head2 get ( property [, no cache flag ] ) | ||
| 205 | |||
| 206 | User name should be the URL name final part e.g. 'jsmith'. | ||
| 207 | If the 'no cache' flag is set, DON'T use the cache (default: use cache). | ||
| 208 | |||
| 209 | Properties may be 'd:' (DAV) or 'r:' (RVP) or 'a:' (ACL), e.g. 'd:displayname'. | ||
| 210 | |||
| 211 | Returns value, undef on failure. | ||
| 212 | |||
| 213 | =cut | ||
| 214 | sub get { | ||
| 215 | my ($self,$prop,$no_cache) = @_; | ||
| 216 | |||
| 217 | # use cache, user properties shouldn't change much | ||
| 218 | if(not $no_cache and exists $self->{prop}->{$prop}) { | ||
| 219 | return $self->{prop}->{$prop}; | ||
| 220 | } | ||
| 221 | |||
| 222 | my $req = $self->{rvp}->_request(PROPFIND => "/instmsg/aliases/$self->{user}", | ||
| 223 | depth => 0, XML => <<XML); | ||
| 224 | <d:propfind xmlns:d="DAV:" xmlns:r="http://schemas.microsoft.com/rvp/"> | ||
| 225 | <d:prop><$prop/></d:prop> | ||
| 226 | </d:propfind> | ||
| 227 | XML | ||
| 228 | # <d:multistatus xmlns:d='DAV:' xmlns:r='http://schemas.microsoft.com/rvp/' xmlns:a='http://schemas.microsoft.com/rvp/acl/'><d:response><d:href>http://laxhhim1.hq.ad.hilton.com/instmsg/aliases/peter_conrey</d:href><d:propstat><d:prop><d:displayname>Peter Conrey</d:displayname></d:prop><d:status>HTTP/1.1 200 Successful</d:status></d:propstat></d:response></d:multistatus> | ||
| 229 | |||
| 230 | my ($res,$data) = $self->{rvp}->_sendXML($req); | ||
| 231 | return 0 unless $data; | ||
| 232 | my $value = $self->_parse_prop($data); | ||
| 233 | $self->{prop}->{$prop} = $value->{$prop}; | ||
| 234 | return $self->{prop}->{$prop}; | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | =head2 set( prop, value ) | ||
| 239 | |||
| 240 | Set property. Returns true on success, false on failure. | ||
| 241 | |||
| 242 | This should only be done on our own node, it should fail with an access error | ||
| 243 | on others. | ||
| 244 | |||
| 245 | =cut | ||
| 246 | sub set { | ||
| 247 | my ($self,$prop,$value) = @_; | ||
| 248 | |||
| 249 | my $req = $self->{rvp}->_request(PROPPATCH => $self->{url}, XML => <<XML); | ||
| 250 | <d:propertyupdate xmlns:d="DAV:" xmlns:r="http://schemas.microsoft.com/rvp/"> | ||
| 251 | <d:set><d:prop><$prop><r:leased-value> | ||
| 252 | <r:value>$value</r:value> | ||
| 253 | </r:leased-value></$prop></d:prop></d:set> | ||
| 254 | </d:propertyupdate> | ||
| 255 | XML | ||
| 256 | my $success = $self->{rvp}->_send($req)->is_success(); | ||
| 257 | $self->{prop}->{$prop} = $value if $success; | ||
| 258 | return $success; | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | =head2 acl | ||
| 263 | |||
| 264 | Get ACL for user. | ||
| 265 | |||
| 266 | Returns true on success, false on failure (this method isn't needed so we don't | ||
| 267 | bother to parse out the XML yet, sorry). | ||
| 268 | |||
| 269 | Note that although it isn't shown in the Microsoft RVP "specification", ACLs | ||
| 270 | can also be set although this seems to be unnecessary for the most part | ||
| 271 | (although perhaps handy e.g. to disallow someone from receiving presence | ||
| 272 | information about onesself). | ||
| 273 | |||
| 274 | =cut | ||
| 275 | sub acl { | ||
| 276 | my $self = shift; | ||
| 277 | my $req = $self->_request(ACL => "/instmsg/aliases/$self->{user}"); | ||
| 278 | return $self->_send($req)->is_success(); | ||
| 279 | } | ||
| 280 | |||
| 281 | |||
| 282 | =head2 activity | ||
| 283 | |||
| 284 | Sent when there's activity. Updates the last activity time. | ||
| 285 | |||
| 286 | =cut | ||
| 287 | sub activity { | ||
| 288 | my $self = shift; | ||
| 289 | $self->{active} = time; | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | =head2 lag | ||
| 294 | |||
| 295 | Returns lag time since last activity, undef if no activity. | ||
| 296 | |||
| 297 | =cut | ||
| 298 | sub lag { | ||
| 299 | my $self = shift; | ||
| 300 | return $self->{active} && time-$self->{active}; | ||
| 301 | } | ||
| 302 | |||
| 303 | |||
| 304 | =head1 AUTHOR | ||
| 305 | |||
| 306 | David Robins E<lt>dbrobins@davidrobins.netE<gt>. | ||
| 307 | |||
| 308 | =cut | ||
| 309 | |||
| 310 | |||
| 311 | 1; |