| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Net::RVP::Session - encapsulation of a session (conversation) | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | my $session = $rvp->session(); | ||
| 8 | $session->add($user1,$user2); | ||
| 9 | $session->say("hello"); | ||
| 10 | |||
| 11 | =head1 METHODS | ||
| 12 | |||
| 13 | =cut | ||
| 14 | package Net::RVP::Session; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | |||
| 18 | |||
| 19 | =head2 new ( RVP object, session id (GUID) ) | ||
| 20 | |||
| 21 | =cut | ||
| 22 | sub new { | ||
| 23 | my ($class,$rvp,$id) = @_; | ||
| 24 | my $self = bless { rvp => $rvp, id => $id, user => {} }, ref $class || $class; | ||
| 25 | return $self; | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | =head2 id | ||
| 30 | |||
| 31 | Return the session id. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | sub id { | ||
| 35 | shift->{id} | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | # _notify ( RVP::User object, MIME body text ) | ||
| 40 | # | ||
| 41 | # create a notify request, pass to _send, return result | ||
| 42 | # | ||
| 43 | sub _notify { | ||
| 44 | my ($self,$user,$body) = @_; | ||
| 45 | my $src = $self->{rvp}->url(); | ||
| 46 | my $dst = $user->url(); | ||
| 47 | |||
| 48 | my $xml = <<XML; | ||
| 49 | <r:notification xmlns:d='DAV:' xmlns:r='http://schemas.microsoft.com/rvp/'><r:message><r:notification-from><r:contact><d:href>$src</d:href></r:contact></r:notification-from><r:notification-to><r:contact><d:href>$dst</d:href></r:contact></r:notification-to><r:msgbody><r:mime-data><![CDATA[MIME-Version: 1.0\r | ||
| 50 | Session-Id: $self->{id}\r | ||
| 51 | $body]]></r:mime-data></r:msgbody></r:message></r:notification> | ||
| 52 | XML | ||
| 53 | |||
| 54 | my $req = $self->{rvp}->_request(NOTIFY => $user->url(), | ||
| 55 | RVP_ack_type => 'DeepOr', RVP_hop_count => 1, XML => $xml); | ||
| 56 | return $self->{rvp}->_send($req); | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | =head2 add ( RVP::User object, ... ) | ||
| 61 | |||
| 62 | Sends a message to the users to add them to the conversation. | ||
| 63 | |||
| 64 | Returns number of users successfully added. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | sub add { | ||
| 68 | my $self = shift; | ||
| 69 | |||
| 70 | my $success = 0; | ||
| 71 | |||
| 72 | for my $user(@_) { | ||
| 73 | next if $self->{user}->{$user->url()}; # can't join multiply | ||
| 74 | my $res = $self->_notify($user, | ||
| 75 | "Content-Type: text/x-msmsgscontrol\r\n\r\n\r\n"); | ||
| 76 | $self->join_event($user), $success++ if $res->is_success(); | ||
| 77 | } | ||
| 78 | |||
| 79 | return $success; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | =head2 leave | ||
| 84 | |||
| 85 | Leave the conversation. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | sub leave { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | for my $user(values %{$self->{user}}) { | ||
| 92 | my $res = $self->_notify($user, | ||
| 93 | "Content-Type: text/x-imleave\x0d\x0a\x0d\x0a\x0d\x0a"); | ||
| 94 | } | ||
| 95 | delete $self->{rvp}->{sesss}->{$self->{id}}; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | =head2 typing | ||
| 100 | |||
| 101 | Send 'typing' message. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | sub typing { | ||
| 105 | my $self = shift; | ||
| 106 | |||
| 107 | for my $user(values %{$self->{user}}) { | ||
| 108 | my $res = $self->_notify($user, | ||
| 109 | "Content-Type: text/x-msmsgscontrol\r\nTypingUser: ".$self->{rvp}->name(). | ||
| 110 | '@'.$self->{rvp}->server()."\x0d\x0a\x0d\x0a\x0d\x0a"); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | =head2 has ( RVP::User ) | ||
| 116 | |||
| 117 | Returns true iff the given user is in the session. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | sub has { | ||
| 121 | my ($self,$user) = @_; | ||
| 122 | return exists $self->{user}->{$user->url()}; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | =head2 users | ||
| 127 | |||
| 128 | Returns list of users (RVP::User object) in the session. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | sub users { | ||
| 132 | my $self = shift; | ||
| 133 | return values %{$self->{user}}; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | =head2 say ( text ) | ||
| 138 | |||
| 139 | Send message. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | sub say { | ||
| 143 | my ($self,$text) = @_; | ||
| 144 | |||
| 145 | # XXX don't send 'X-MMS-IM-Format: FN=MS%20Shell%20Dlg; EF=; CO=0; CS=0; PF=0' | ||
| 146 | # XXX do we need to? | ||
| 147 | for my $user(values %{$self->{user}}) { | ||
| 148 | my $res = $self->_notify($user, | ||
| 149 | "Content-Type: text/plain; charset=UTF-8\x0d\x0a\x0d\x0a$text"); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | =head2 join_event ( RVP::User ) | ||
| 155 | |||
| 156 | A user has been added to the conversation. Called in response to an | ||
| 157 | 'x-msmsgscontrol' event (non-typing). | ||
| 158 | |||
| 159 | =cut | ||
| 160 | sub join_event { | ||
| 161 | my ($self,$user) = @_; | ||
| 162 | $self->{user}->{$user->url()} = $user; | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | =head2 part_event ( RVP::User ) | ||
| 167 | |||
| 168 | A user has left the conversation. Should be called in response to an | ||
| 169 | 'x-imleave' event (e.g. as generated by the leave method here, but received | ||
| 170 | on our notification server). | ||
| 171 | |||
| 172 | Returns true on success, false if not found. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | sub part_event { | ||
| 176 | my ($self,$user) = @_; | ||
| 177 | return delete $self->{user}->{$user->url()}; | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | =head2 typing_event ( RVP::User ) | ||
| 182 | |||
| 183 | A user is typing. Called in response to a notification. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | sub typing_event { | ||
| 187 | # do nothing | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | =head2 message_event ( RVP::User, message ) | ||
| 192 | |||
| 193 | Message was sent to a user. | ||
| 194 | |||
| 195 | =cut | ||
| 196 | sub message_event { | ||
| 197 | # do nothing | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | =head1 AUTHOR | ||
| 202 | |||
| 203 | David Robins E<lt>dbrobins@davidrobins.netE<gt>. | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | |||
| 208 | 1; |