| 1 | package Net::PSYC::Client; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | our $VERSION = '0.4'; | ||
| 6 | # | ||
| 7 | # implements some basic client functionality... | ||
| 8 | # | ||
| 9 | # your perl-script (main::) needs to have following subs | ||
| 10 | # - getPassword() | ||
| 11 | # | ||
| 12 | use Net::PSYC qw(same_host parse_uniform make_uniform psyctext W register_route | ||
| 13 | get_connection); | ||
| 14 | use Net::PSYC::Event qw(register_uniform unregister_uniform); | ||
| 15 | use Net::PSYC::Tie::AbbrevHash; | ||
| 16 | use base 'Exporter'; | ||
| 17 | |||
| 18 | our @EXPORT = qw(register_context unregister_context register_new msg psycLink psycUnlink sendmsg UNI NICK enter talk get_context); | ||
| 19 | |||
| 20 | my ($new, %ContextReg, $UNI, $NICK, %react, $SERVER_UNI, $SERVER_HOST, %tags, $action); | ||
| 21 | our %friends; | ||
| 22 | sub UNI () { $UNI } | ||
| 23 | |||
| 24 | # we like the idea how this "sub" gets inlined, that's why it looks so weird | ||
| 25 | sub NICK () { $NICK || $UNI? ($NICK = substr((parse_uniform($UNI))[4], 1)) : '' } | ||
| 26 | |||
| 27 | # register_context ( uni, obj ) | ||
| 28 | sub register_context { | ||
| 29 | |||
| 30 | unless (ref $_[1]) { | ||
| 31 | delete $ContextReg{$_[0]}; | ||
| 32 | W1('register_context: %s', $_[0]); | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | W1('register_context(%s, %s)', $_[0], $_[1]); | ||
| 36 | |||
| 37 | $ContextReg{$_[0]} = $_[1]; | ||
| 38 | return; | ||
| 39 | } | ||
| 40 | |||
| 41 | # unregister_context ( uni ) | ||
| 42 | sub unregister_context { | ||
| 43 | W1('unregister_context(%s)', $_[0]); | ||
| 44 | if (ref $_[0]) { | ||
| 45 | foreach (keys %ContextReg) { | ||
| 46 | if ($ContextReg{$_} eq $_[0]) { | ||
| 47 | delete $ContextReg{$_}; | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | delete $ContextReg{$_[0]}; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub register_new { | ||
| 56 | $new = shift; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub get_context { | ||
| 60 | my $vars = shift; | ||
| 61 | my ($addr ,$obj, $u); | ||
| 62 | unless (ref $vars eq 'HASH') { | ||
| 63 | $addr = $vars; | ||
| 64 | $vars = {}; | ||
| 65 | } else { | ||
| 66 | $addr = $vars->{'_context'} || $vars->{'_source'}; | ||
| 67 | } | ||
| 68 | |||
| 69 | $obj = $ContextReg{$addr}; | ||
| 70 | return $obj if ($obj); | ||
| 71 | if ($addr && ($u = parse_uniform($addr))) { | ||
| 72 | |||
| 73 | if ($u->{'object'} !~ /^\@/ && $new) { | ||
| 74 | my $name; | ||
| 75 | if (same_host($u->{'host'}, $SERVER_HOST)) { | ||
| 76 | |||
| 77 | if ($u->{'scheme'} eq 'psyc') { | ||
| 78 | $name = substr($u->{'object'}, 1); | ||
| 79 | } elsif ($u->{'user'}) { | ||
| 80 | $name = $u->{'user'}; | ||
| 81 | } else { | ||
| 82 | $name = $addr; | ||
| 83 | } | ||
| 84 | } else { | ||
| 85 | $name = $addr; | ||
| 86 | # this is maybe evil. should be done in the core lib since | ||
| 87 | # the psyc-routing rules should make that clear | ||
| 88 | #register_route($addr, $vars->{'_INTERNAL_origin'}) | ||
| 89 | # if (exists $vars->{'_INTERNAL_origin'}); | ||
| 90 | } | ||
| 91 | $obj = &{$new}( $addr, $name, 0 ); | ||
| 92 | register_context($addr, $obj); | ||
| 93 | } | ||
| 94 | } else { | ||
| 95 | W0('%s does not seem to be a valid UNL. Dumping message.', $addr); | ||
| 96 | return 0; | ||
| 97 | } | ||
| 98 | return $obj; | ||
| 99 | } | ||
| 100 | |||
| 101 | # internal | ||
| 102 | sub verify_enter { | ||
| 103 | my $vars = shift; | ||
| 104 | my ($addr ,$obj, $u); | ||
| 105 | |||
| 106 | $addr = $vars->{'_context'} || $vars->{'_source'}; | ||
| 107 | |||
| 108 | return 1 if (exists $ContextReg{$addr}); | ||
| 109 | |||
| 110 | unless (defined delete $tags{$vars->{'_tag'}} | ||
| 111 | || Net::PSYC::get_connection(UNI()) eq $vars->{'_INTERNAL_origin'}) { | ||
| 112 | W0('%s is trying to join us into a room without a proper _tag', $addr); | ||
| 113 | return 0; | ||
| 114 | } | ||
| 115 | $u = parse_uniform($addr); | ||
| 116 | |||
| 117 | if (ref $u && $u->{'object'} =~ /^\@/ && $new) { | ||
| 118 | my $silent = (exists $vars->{'_control'} | ||
| 119 | && $vars->{'_control'} eq '_silent') ? 1 : 0; | ||
| 120 | my $name; | ||
| 121 | if (same_host($u->{'host'}, $SERVER_HOST)) { | ||
| 122 | $name = substr($u->{'object'}, 1); | ||
| 123 | } else { | ||
| 124 | $name = $addr; | ||
| 125 | } | ||
| 126 | $obj = &{$new}( $addr, $name, $silent ); | ||
| 127 | register_context($addr, $obj); | ||
| 128 | return 1; | ||
| 129 | } | ||
| 130 | return 0; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub getUni { | ||
| 134 | my $obj = shift; | ||
| 135 | return unless(ref $obj); | ||
| 136 | foreach(keys %ContextReg) { | ||
| 137 | if($ContextReg{$_} == $obj) { | ||
| 138 | return $_; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | return 0; | ||
| 142 | } | ||
| 143 | |||
| 144 | # incoming messages | ||
| 145 | # msg ( source, mc, data, vars) | ||
| 146 | sub msg { | ||
| 147 | my ($source, $mc, $data, $vars) = @_; | ||
| 148 | unless ($mc) { | ||
| 149 | return 1; | ||
| 150 | } | ||
| 151 | my $func = $react{$mc}; | ||
| 152 | if ($func) { | ||
| 153 | &{$func}($source, $mc, $data, $vars); | ||
| 154 | } else { | ||
| 155 | my $c = get_context($vars); | ||
| 156 | # we expect msg to do the right thing | ||
| 157 | unless (ref $c) { | ||
| 158 | # this is a message from a room we never entered | ||
| 159 | # we may think about sending _request_place_leave | ||
| 160 | W0('Client->msg(%s, %s, %s, %s) is baklawa.', $source, $mc, $data, | ||
| 161 | $vars); | ||
| 162 | return 1; | ||
| 163 | } | ||
| 164 | |||
| 165 | my $time = int($vars->{'_time_place'}||$vars->{'_time_raw'}); | ||
| 166 | my @t = localtime(int($time)); | ||
| 167 | $vars->{'_time'} = sprintf '%02d:%02d:%02d', $t[2], $t[1], $t[0]; | ||
| 168 | |||
| 169 | $c->msg($source, $mc, $data, $vars) if $c; | ||
| 170 | } | ||
| 171 | |||
| 172 | return 1; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub zwirbel { | ||
| 176 | my $word = shift; | ||
| 177 | |||
| 178 | return $word if (length($word) <= 3); | ||
| 179 | |||
| 180 | my @t = split(//, $word); | ||
| 181 | foreach (1..$#t-1) { | ||
| 182 | my $n = int(rand($#t - 1)) + 1; | ||
| 183 | if ($n != $_) { | ||
| 184 | my $a = $t[$_]; | ||
| 185 | $t[$_] = $t[$n]; | ||
| 186 | $t[$n] = $a; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | return join('', @t); | ||
| 190 | } | ||
| 191 | |||
| 192 | sub sendmsg { | ||
| 193 | my ($target, $mc, $data, $vars, $MMPvars) = @_; | ||
| 194 | if ($target =~ /^xmpp/) { | ||
| 195 | register_route($target, get_connection(UNI())); | ||
| 196 | } | ||
| 197 | W1('Client->sendmsg(%s, %s, %.10s ..., %s)', $target, $mc, $data, $vars); | ||
| 198 | $MMPvars ||= {}; | ||
| 199 | $MMPvars->{'_identification'} ||= UNI(); | ||
| 200 | |||
| 201 | if ($mc =~ /^_message/) { | ||
| 202 | # this is cooler but muve may not be entirely ready for it yet | ||
| 203 | # $vars->{'_nick'} ||= NICK() unless (lc(NICK()) eq NICK()); | ||
| 204 | $vars->{'_nick'} ||= NICK(); | ||
| 205 | # hmm.. a bit excentric to force zwirbel on all users of perlpsyc | ||
| 206 | $vars->{'_action'} ||= zwirbel($action) | ||
| 207 | if ($action && $mc eq '_message_public'); | ||
| 208 | } | ||
| 209 | |||
| 210 | !Net::PSYC::sendmsg($target, $mc, $data, $vars, $MMPvars); | ||
| 211 | } | ||
| 212 | |||
| 213 | # link to a given uni | ||
| 214 | sub psycLink { | ||
| 215 | $UNI = shift; | ||
| 216 | my $u = parse_uniform($UNI); | ||
| 217 | unless ($u) { | ||
| 218 | return W1('I cannot link to that uni: %s (It is not valid)', $UNI); | ||
| 219 | } | ||
| 220 | # register_uniform($UNI); | ||
| 221 | register_uniform(); # this is no good | ||
| 222 | $SERVER_HOST = $u->{'host'}; | ||
| 223 | $SERVER_UNI = make_uniform($u->{'user'}, $u->{'host'}, $u->{'port'}, | ||
| 224 | $u->{'transport'}, ''); | ||
| 225 | Net::PSYC::sendmsg($UNI, '_request_link', '', | ||
| 226 | { _password => main::getPassword(), | ||
| 227 | }); | ||
| 228 | # we need to do that raw.. since we want no _identification == UNI | ||
| 229 | } | ||
| 230 | |||
| 231 | sub psycUnlink () { | ||
| 232 | unregister_uniform(UNI()); | ||
| 233 | sendmsg(UNI(), '_request_exit'); | ||
| 234 | } | ||
| 235 | |||
| 236 | sub enter { | ||
| 237 | my $target = shift; | ||
| 238 | my $vars = {}; | ||
| 239 | unless (parse_uniform($target)) { | ||
| 240 | $target =~ s/^[@|#]//o; | ||
| 241 | $target = $SERVER_UNI."/\@$target"; | ||
| 242 | } | ||
| 243 | $vars->{'_tag'} = unpack( "H*",pack("F", rand(10))); | ||
| 244 | $tags{$vars->{'_tag'}} = $target; | ||
| 245 | $vars->{'_nick'} = NICK(); | ||
| 246 | sendmsg($target, '_request_enter', '', $vars); | ||
| 247 | } | ||
| 248 | |||
| 249 | sub talk { | ||
| 250 | my $target = shift; | ||
| 251 | unless ($target =~ /psyc:\/\//) { | ||
| 252 | $target = $SERVER_UNI."/\~$target"; | ||
| 253 | } | ||
| 254 | my $vars = { | ||
| 255 | '_nick' => NICK(), | ||
| 256 | }; | ||
| 257 | $vars->{'_action'} = $action if $action; | ||
| 258 | |||
| 259 | sendmsg($target, '_message_private', join(' ', @_), {_nick=>NICK()}); | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | tie %react, 'Net::PSYC::Tie::AbbrevHash'; | ||
| 264 | %react = ( | ||
| 265 | '_echo_place_leave' => sub { | ||
| 266 | my ($source, $mc, $data, $vars) = @_; | ||
| 267 | my $obj = get_context($vars->{'_context'}); | ||
| 268 | return unless ($obj); | ||
| 269 | unless ($obj->{'type'} eq 'place') { | ||
| 270 | return W0('Got an _echo_place_leave from someone (%s) who does not appear to be a room. Dropping message.', $source); | ||
| 271 | } | ||
| 272 | $obj->msg(@_); | ||
| 273 | $obj->leanforwardandchokeyourself(); | ||
| 274 | }, | ||
| 275 | '_echo_set' => sub { | ||
| 276 | my ($source, $mc, $data, $vars) = @_; | ||
| 277 | return unless($source eq UNI()); | ||
| 278 | if ($vars->{'_key'} eq 'name' && exists $vars->{'_value'}) { | ||
| 279 | $NICK = $vars->{'_value'}; | ||
| 280 | } elsif ($vars->{'_key'} eq 'speakaction' && exists $vars->{'_value'}) { | ||
| 281 | $action = $vars->{'_value'}; | ||
| 282 | } | ||
| 283 | my $c = get_context($vars); | ||
| 284 | return unless ($c); | ||
| 285 | # we expect msg to do the right thing | ||
| 286 | $c->msg($source, $mc, $data, $vars) if $c; | ||
| 287 | }, | ||
| 288 | '_message_echo_private' => sub { | ||
| 289 | my ($source, $mc, $data, $vars) = @_; | ||
| 290 | |||
| 291 | my $obj = get_context($vars->{'_source_relay'} || $source); | ||
| 292 | return unless ($obj); | ||
| 293 | |||
| 294 | $obj->msg(@_); | ||
| 295 | }, | ||
| 296 | '_notice_place_leave' => sub { | ||
| 297 | my ($source, $mc, $data, $vars) = @_; | ||
| 298 | my $obj = get_context($vars); | ||
| 299 | return unless ($obj); | ||
| 300 | $obj->msg(@_); | ||
| 301 | $obj->leave( $vars->{'_nick'} || $source ) if $obj->can('leave'); | ||
| 302 | }, | ||
| 303 | '_echo_place_enter' => sub { | ||
| 304 | my ($source, $mc, $data, $vars) = @_; | ||
| 305 | unless (verify_enter($vars)) { | ||
| 306 | return; | ||
| 307 | } | ||
| 308 | my $obj = get_context($vars); | ||
| 309 | return unless ($obj); | ||
| 310 | $obj->enter( $vars->{'_nick'} || $source, UNI()) if $obj->can('enter'); | ||
| 311 | $obj->msg(@_); | ||
| 312 | }, | ||
| 313 | '_notice_place_enter' => sub { | ||
| 314 | my ($source, $mc, $data, $vars) = @_; | ||
| 315 | unless (verify_enter($vars)) { | ||
| 316 | return; | ||
| 317 | } | ||
| 318 | my $obj = get_context($vars); | ||
| 319 | return unless ($obj); | ||
| 320 | $obj->enter( $vars->{'_nick'} || $source, $source ) if $obj->can('enter'); | ||
| 321 | $obj->msg(@_); | ||
| 322 | }, | ||
| 323 | '_query_password' => sub { | ||
| 324 | my ($source, $mc, $data, $vars) = @_; | ||
| 325 | sendmsg(UNI(), '_set_password', '', {_password=> main::getPassword() }); | ||
| 326 | }, | ||
| 327 | '_status_place_members' => sub { | ||
| 328 | my ($source, $mc, $data, $vars) = @_; | ||
| 329 | my $obj = get_context($source); | ||
| 330 | return unless ($obj); | ||
| 331 | |||
| 332 | if ($obj->can('members') && exists $vars->{'_list_members_nicks'}) { | ||
| 333 | my $members = {}; # name -> uni | ||
| 334 | for (0 .. @{$vars->{'_list_members_nicks'}} - 1) { | ||
| 335 | $members->{$vars->{'_list_members_nicks'}->[$_]} = $vars->{'_list_members'}->[$_]; | ||
| 336 | } | ||
| 337 | $obj->members($members); | ||
| 338 | } | ||
| 339 | $obj->msg(@_); | ||
| 340 | }, | ||
| 341 | '_notice_link' => sub { | ||
| 342 | my ($source, $mc, $data, $vars) = @_; | ||
| 343 | my @user = parse_uniform($source); | ||
| 344 | my @uni = parse_uniform(UNI()); | ||
| 345 | if(same_host($user[1], $uni[1]) && lc($user[4]) eq lc($uni[4])) { | ||
| 346 | $UNI = $source; | ||
| 347 | if (exists $vars->{'_nick'}) { | ||
| 348 | $NICK = $vars->{'_nick'}; | ||
| 349 | } | ||
| 350 | Net::PSYC::get_connection($UNI)->TRUST(6); | ||
| 351 | sendmsg(UNI(), '_request_execute', 'set echo on'); | ||
| 352 | } else { | ||
| 353 | W0('Got a _notice_link from an uni (%s) we did not try to link to.', | ||
| 354 | $source); | ||
| 355 | } | ||
| 356 | }, | ||
| 357 | '_notice_link_removed' => sub { | ||
| 358 | my ($source, $mc, $data, $vars) = @_; | ||
| 359 | my $obj = get_context($source); | ||
| 360 | return unless ($obj); | ||
| 361 | |||
| 362 | $obj->msg(@_); | ||
| 363 | }, | ||
| 364 | '_status_place' => sub { | ||
| 365 | my ($source, $mc, $data, $vars) = @_; | ||
| 366 | my $c = get_context($source); | ||
| 367 | |||
| 368 | $c->msg(@_) if $c; | ||
| 369 | }, | ||
| 370 | '_info_description' => sub { | ||
| 371 | my ($source, $mc, $data, $vars) = @_; | ||
| 372 | |||
| 373 | if ($source eq UNI()) { | ||
| 374 | $action = $vars->{'_action'}; | ||
| 375 | } | ||
| 376 | my $obj = get_context($source); | ||
| 377 | |||
| 378 | $obj->msg(@_) if $obj; | ||
| 379 | }, | ||
| 380 | '_list_friends_present' => sub { | ||
| 381 | my ($source, $mc, $data, $vars) = @_; | ||
| 382 | my $obj = get_context($source); | ||
| 383 | return unless ($obj); | ||
| 384 | |||
| 385 | my $friends = {}; # name -> uni | ||
| 386 | for (0 .. @{$vars->{'_list_friends_nicknames'}} - 1) { | ||
| 387 | $friends->{$vars->{'_list_friends_nicknames'}->[$_]} = $vars->{'_list_friends'}->[$_]; | ||
| 388 | } | ||
| 389 | |||
| 390 | if ($obj->can('friends')) { | ||
| 391 | $obj->friends($friends); | ||
| 392 | } else { | ||
| 393 | %friends = %$friends; | ||
| 394 | } | ||
| 395 | |||
| 396 | $obj->msg(@_); | ||
| 397 | }, | ||
| 398 | '_notice' => sub { | ||
| 399 | my ($source, $mc, $data, $vars) = @_; | ||
| 400 | my $obj = $ContextReg{$vars->{'_context'}||$vars->{'_source'}}; | ||
| 401 | |||
| 402 | $obj = get_context(UNI()) unless ($obj); | ||
| 403 | $obj->msg(@_) if $obj; | ||
| 404 | }, | ||
| 405 | ); | ||
| 406 | $react{'_error'} = $react{'_notice'}; | ||
| 407 | $react{'_status'} = $react{'_notice'}; | ||
| 408 | $react{'_info_nickname'} = $react{'_info_description'}; | ||
| 409 | |||
| 410 | |||
| 411 | 1; | ||
| 412 | |||
| 413 | __END__ |