| 1 | # -*- mode: cperl; cperl-indent-level: 4; indent-tabs-mode: nil -*- |
|---|
| 2 | package BarnOwl::Jabber; |
|---|
| 3 | use warnings; |
|---|
| 4 | use strict; |
|---|
| 5 | |
|---|
| 6 | use Authen::SASL qw(Perl); |
|---|
| 7 | use Net::Jabber; |
|---|
| 8 | use Net::Jabber::MUC; |
|---|
| 9 | use Net::DNS; |
|---|
| 10 | use Getopt::Long; |
|---|
| 11 | |
|---|
| 12 | no warnings 'redefine'; |
|---|
| 13 | |
|---|
| 14 | ################################################################################ |
|---|
| 15 | # owl perl jabber support |
|---|
| 16 | # |
|---|
| 17 | # XXX Todo: |
|---|
| 18 | # Rosters for MUCs |
|---|
| 19 | # More user feedback |
|---|
| 20 | # * joining MUC |
|---|
| 21 | # * parting MUC |
|---|
| 22 | # * presence (Roster and MUC) |
|---|
| 23 | # Implementing formatting and logging callbacks for C |
|---|
| 24 | # Appropriate callbacks for presence subscription messages. |
|---|
| 25 | # |
|---|
| 26 | ################################################################################ |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | ################################################################################ |
|---|
| 30 | ################################################################################ |
|---|
| 31 | package BarnOwl::Jabber::Connection; |
|---|
| 32 | |
|---|
| 33 | use base qw(Net::Jabber::Client); |
|---|
| 34 | |
|---|
| 35 | sub new { |
|---|
| 36 | my $class = shift; |
|---|
| 37 | |
|---|
| 38 | my %args = (); |
|---|
| 39 | if(BarnOwl::getvar('debug') eq 'on') { |
|---|
| 40 | $args{debuglevel} = 1; |
|---|
| 41 | $args{debugfile} = 'jabber.log'; |
|---|
| 42 | } |
|---|
| 43 | my $self = $class->SUPER::new(%args); |
|---|
| 44 | $self->{_BARNOWL_MUCS} = []; |
|---|
| 45 | return $self; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | =head2 MUCJoin |
|---|
| 49 | |
|---|
| 50 | Extends MUCJoin to keep track of the MUCs we're joined to as |
|---|
| 51 | Net::Jabber::MUC objects. Takes the same arguments as |
|---|
| 52 | L<Net::Jabber::MUC/new> and L<Net::Jabber::MUC/Connect> |
|---|
| 53 | |
|---|
| 54 | =cut |
|---|
| 55 | |
|---|
| 56 | sub MUCJoin { |
|---|
| 57 | my $self = shift; |
|---|
| 58 | my $muc = Net::Jabber::MUC->new(connection => $self, @_); |
|---|
| 59 | $muc->Join(@_); |
|---|
| 60 | push @{$self->MUCs}, $muc; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | =head2 MUCLeave ARGS |
|---|
| 64 | |
|---|
| 65 | Leave a MUC. The MUC is specified in the same form as L</FindMUC> |
|---|
| 66 | |
|---|
| 67 | =cut |
|---|
| 68 | |
|---|
| 69 | sub MUCLeave { |
|---|
| 70 | my $self = shift; |
|---|
| 71 | my $muc = $self->FindMUC(@_); |
|---|
| 72 | return unless $muc; |
|---|
| 73 | |
|---|
| 74 | $muc->Leave(); |
|---|
| 75 | $self->{_BARNOWL_MUCS} = [grep {$_->BaseJID ne $muc->BaseJID} $self->MUCs]; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | =head2 FindMUC ARGS |
|---|
| 79 | |
|---|
| 80 | Return the Net::Jabber::MUC object representing a specific MUC we're |
|---|
| 81 | joined to, undef if it doesn't exists. ARGS can be either JID => $JID, |
|---|
| 82 | or Room => $room, Server => $server. |
|---|
| 83 | |
|---|
| 84 | =cut |
|---|
| 85 | |
|---|
| 86 | sub FindMUC { |
|---|
| 87 | my $self = shift; |
|---|
| 88 | |
|---|
| 89 | my %args; |
|---|
| 90 | while($#_ >= 0) { $args{ lc(pop(@_)) } = pop(@_); } |
|---|
| 91 | |
|---|
| 92 | my $jid; |
|---|
| 93 | if($args{jid}) { |
|---|
| 94 | $jid = $args{jid}; |
|---|
| 95 | } elsif($args{room} && $args{server}) { |
|---|
| 96 | $jid = Net::Jabber::JID->new(userid => $args{room}, |
|---|
| 97 | server => $args{server}); |
|---|
| 98 | } |
|---|
| 99 | $jid = $jid->GetJID('base') if UNIVERSAL::isa($jid, 'Net::Jabber::JID'); |
|---|
| 100 | |
|---|
| 101 | foreach my $muc ($self->MUCs) { |
|---|
| 102 | return $muc if $muc->BaseJID eq $jid; |
|---|
| 103 | } |
|---|
| 104 | return undef; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | =head2 MUCs |
|---|
| 108 | |
|---|
| 109 | Returns a list (or arrayref in scalar context) of Net::Jabber::MUC |
|---|
| 110 | objects we believe ourself to be connected to. |
|---|
| 111 | |
|---|
| 112 | =cut |
|---|
| 113 | |
|---|
| 114 | sub MUCs { |
|---|
| 115 | my $self = shift; |
|---|
| 116 | my $mucs = $self->{_BARNOWL_MUCS}; |
|---|
| 117 | return wantarray ? @$mucs : $mucs; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | ################################################################################ |
|---|
| 121 | ################################################################################ |
|---|
| 122 | package BarnOwl::Jabber::ConnectionManager; |
|---|
| 123 | sub new { |
|---|
| 124 | my $class = shift; |
|---|
| 125 | return bless { }, $class; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | sub addConnection { |
|---|
| 129 | my $self = shift; |
|---|
| 130 | my $jidStr = shift; |
|---|
| 131 | |
|---|
| 132 | my $client = BarnOwl::Jabber::Connection->new; |
|---|
| 133 | |
|---|
| 134 | $self->{$jidStr}->{Client} = $client; |
|---|
| 135 | $self->{$jidStr}->{Roster} = $client->Roster(); |
|---|
| 136 | $self->{$jidStr}->{Status} = "available"; |
|---|
| 137 | return $client; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | sub removeConnection { |
|---|
| 141 | my $self = shift; |
|---|
| 142 | my $jidStr = shift; |
|---|
| 143 | return 0 unless exists $self->{$jidStr}->{Client}; |
|---|
| 144 | |
|---|
| 145 | $self->{$jidStr}->{Client}->Disconnect(); |
|---|
| 146 | delete $self->{$jidStr}; |
|---|
| 147 | |
|---|
| 148 | return 1; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | sub connected { |
|---|
| 152 | my $self = shift; |
|---|
| 153 | return scalar keys %{ $self }; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | sub getJids { |
|---|
| 157 | my $self = shift; |
|---|
| 158 | return keys %{ $self }; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | sub jidExists { |
|---|
| 162 | my $self = shift; |
|---|
| 163 | my $jidStr = shift; |
|---|
| 164 | return exists $self->{$jidStr}; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | sub sidExists { |
|---|
| 168 | my $self = shift; |
|---|
| 169 | my $sid = shift || ""; |
|---|
| 170 | foreach my $c ( values %{ $self } ) { |
|---|
| 171 | return 1 if ($c->{Client}->{SESSION}->{id} eq $sid); |
|---|
| 172 | } |
|---|
| 173 | return 0; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | sub getConnectionFromSid { |
|---|
| 177 | my $self = shift; |
|---|
| 178 | my $sid = shift; |
|---|
| 179 | foreach my $c (values %{ $self }) { |
|---|
| 180 | return $c->{Client} if $c->{Client}->{SESSION}->{id} eq $sid; |
|---|
| 181 | } |
|---|
| 182 | return undef; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | sub getConnectionFromJidStr { |
|---|
| 186 | my $self = shift; |
|---|
| 187 | my $jidStr = shift; |
|---|
| 188 | return $self->{$jidStr}->{Client}; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | sub getRosterFromSid { |
|---|
| 192 | my $self = shift; |
|---|
| 193 | my $sid = shift; |
|---|
| 194 | foreach my $c (values %{ $self }) { |
|---|
| 195 | return $c->{Roster} |
|---|
| 196 | if $c->{Client}->{SESSION}->{id} eq $sid; |
|---|
| 197 | } |
|---|
| 198 | return undef; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | sub getRosterFromJidStr { |
|---|
| 202 | my $self = shift; |
|---|
| 203 | my $jidStr = shift; |
|---|
| 204 | return $self->{$jidStr}->{Roster}; |
|---|
| 205 | } |
|---|
| 206 | ################################################################################ |
|---|
| 207 | |
|---|
| 208 | package BarnOwl::Jabber; |
|---|
| 209 | |
|---|
| 210 | our $conn = new BarnOwl::Jabber::ConnectionManager unless $conn;; |
|---|
| 211 | our %vars; |
|---|
| 212 | |
|---|
| 213 | sub onStart { |
|---|
| 214 | if ( *BarnOwl::queue_message{CODE} ) { |
|---|
| 215 | register_owl_commands(); |
|---|
| 216 | push @::onMainLoop, sub { BarnOwl::Jabber::onMainLoop(@_) }; |
|---|
| 217 | push @::onGetBuddyList, sub { BarnOwl::Jabber::onGetBuddyList(@_) }; |
|---|
| 218 | $vars{show} = ''; |
|---|
| 219 | } else { |
|---|
| 220 | # Our owl doesn't support queue_message. Unfortunately, this |
|---|
| 221 | # means it probably *also* doesn't support BarnOwl::error. So just |
|---|
| 222 | # give up silently. |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | push @::onStartSubs, sub { BarnOwl::Jabber::onStart(@_) }; |
|---|
| 227 | |
|---|
| 228 | sub onMainLoop { |
|---|
| 229 | return if ( !$conn->connected() ); |
|---|
| 230 | |
|---|
| 231 | $vars{status_changed} = 0; |
|---|
| 232 | my $idletime = owl::getidletime(); |
|---|
| 233 | if ($idletime >= 900 && $vars{show} eq 'away') { |
|---|
| 234 | $vars{show} = 'xa'; |
|---|
| 235 | $vars{status} = 'Auto extended-away after 15 minutes idle.'; |
|---|
| 236 | $vars{status_changed} = 1; |
|---|
| 237 | } elsif ($idletime >= 300 && $vars{show} eq '') { |
|---|
| 238 | $vars{show} = 'away'; |
|---|
| 239 | $vars{status} = 'Auto away after 5 minutes idle.'; |
|---|
| 240 | $vars{status_changed} = 1; |
|---|
| 241 | } elsif ($idletime == 0 && $vars{show} ne '') { |
|---|
| 242 | $vars{show} = ''; |
|---|
| 243 | $vars{status} = ''; |
|---|
| 244 | $vars{status_changed} = 1; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | foreach my $jid ( $conn->getJids() ) { |
|---|
| 248 | my $client = $conn->getConnectionFromJidStr($jid); |
|---|
| 249 | |
|---|
| 250 | my $status = $client->Process(0); |
|---|
| 251 | if ( !defined($status) ) { |
|---|
| 252 | BarnOwl::error("Jabber account $jid disconnected!"); |
|---|
| 253 | do_logout($jid); |
|---|
| 254 | } |
|---|
| 255 | if ($::shutdown) { |
|---|
| 256 | do_logout($jid); |
|---|
| 257 | return; |
|---|
| 258 | } |
|---|
| 259 | if ($vars{status_changed}) { |
|---|
| 260 | my $p = new Net::XMPP::Presence; |
|---|
| 261 | $p->SetShow($vars{show}) if $vars{show}; |
|---|
| 262 | $p->SetStatus($vars{status}) if $vars{status}; |
|---|
| 263 | $client->Send($p); |
|---|
| 264 | } |
|---|
| 265 | } |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | sub blist_listBuddy { |
|---|
| 269 | my $roster = shift; |
|---|
| 270 | my $buddy = shift; |
|---|
| 271 | my $blistStr .= " "; |
|---|
| 272 | my %jq = $roster->query($buddy); |
|---|
| 273 | my $res = $roster->resource($buddy); |
|---|
| 274 | |
|---|
| 275 | $blistStr .= $jq{name} ? $jq{name} . "\t(" .$buddy->GetJID() . ')' : $buddy->GetJID(); |
|---|
| 276 | |
|---|
| 277 | if ($res) { |
|---|
| 278 | my %rq = $roster->resourceQuery( $buddy, $res ); |
|---|
| 279 | $blistStr .= " [" . ( $rq{show} ? $rq{show} : 'online' ) . "]"; |
|---|
| 280 | $blistStr .= " " . $rq{status} if $rq{status}; |
|---|
| 281 | $blistStr = boldify($blistStr); |
|---|
| 282 | } |
|---|
| 283 | else { |
|---|
| 284 | if ($jq{ask}) { |
|---|
| 285 | $blistStr .= " [pending]"; |
|---|
| 286 | } |
|---|
| 287 | elsif ($jq{subscription} eq 'none' || $jq{subscription} eq 'from') { |
|---|
| 288 | $blistStr .= " [not subscribed]"; |
|---|
| 289 | } |
|---|
| 290 | else { |
|---|
| 291 | $blistStr .= " [offline]"; |
|---|
| 292 | } |
|---|
| 293 | } |
|---|
| 294 | return $blistStr . "\n"; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | sub getSingleBuddyList { |
|---|
| 298 | my $jid = shift; |
|---|
| 299 | $jid = resolveJID($jid); |
|---|
| 300 | return "" unless $jid; |
|---|
| 301 | my $blist = ""; |
|---|
| 302 | my $roster = $conn->getRosterFromJidStr($jid); |
|---|
| 303 | if ($roster) { |
|---|
| 304 | $blist .= "\n" . boldify("Jabber Roster for $jid\n"); |
|---|
| 305 | |
|---|
| 306 | foreach my $group ( $roster->groups() ) { |
|---|
| 307 | $blist .= " Group: $group\n"; |
|---|
| 308 | foreach my $buddy ( $roster->jids( 'group', $group ) ) { |
|---|
| 309 | $blist .= blist_listBuddy( $roster, $buddy ); |
|---|
| 310 | } |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | my @unsorted = $roster->jids('nogroup'); |
|---|
| 314 | if (@unsorted) { |
|---|
| 315 | $blist .= " [unsorted]\n"; |
|---|
| 316 | foreach my $buddy (@unsorted) { |
|---|
| 317 | $blist .= blist_listBuddy( $roster, $buddy ); |
|---|
| 318 | } |
|---|
| 319 | } |
|---|
| 320 | } |
|---|
| 321 | return $blist; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | sub onGetBuddyList { |
|---|
| 325 | my $blist = ""; |
|---|
| 326 | foreach my $jid ($conn->getJids()) { |
|---|
| 327 | $blist .= getSingleBuddyList($jid); |
|---|
| 328 | } |
|---|
| 329 | return $blist; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | ################################################################################ |
|---|
| 333 | ### Owl Commands |
|---|
| 334 | sub register_owl_commands() { |
|---|
| 335 | BarnOwl::new_command( |
|---|
| 336 | jabberlogin => \&cmd_login, |
|---|
| 337 | { summary => "Log into jabber", }, |
|---|
| 338 | { usage => "jabberlogin JID" } |
|---|
| 339 | ); |
|---|
| 340 | BarnOwl::new_command( |
|---|
| 341 | jabberlogout => \&cmd_logout, |
|---|
| 342 | { summary => "Log out of jabber" } |
|---|
| 343 | ); |
|---|
| 344 | BarnOwl::new_command( |
|---|
| 345 | jwrite => \&cmd_jwrite, |
|---|
| 346 | { |
|---|
| 347 | summary => "Send a Jabber Message", |
|---|
| 348 | usage => "jwrite JID [-g] [-t thread] [-s subject]" |
|---|
| 349 | } |
|---|
| 350 | ); |
|---|
| 351 | BarnOwl::new_command( |
|---|
| 352 | jlist => \&cmd_jlist, |
|---|
| 353 | { |
|---|
| 354 | summary => "Show your Jabber roster.", |
|---|
| 355 | usage => "jlist" |
|---|
| 356 | } |
|---|
| 357 | ); |
|---|
| 358 | BarnOwl::new_command( |
|---|
| 359 | jmuc => \&cmd_jmuc, |
|---|
| 360 | { |
|---|
| 361 | summary => "Jabber MUC related commands.", |
|---|
| 362 | description => "jmuc sends jabber commands related to muc.\n\n" |
|---|
| 363 | . "The following commands are available\n\n" |
|---|
| 364 | . "join MUC Join a muc.\n\n" |
|---|
| 365 | . "part MUC Part a muc.\n" |
|---|
| 366 | . " The muc is taken from the current message if not supplied.\n\n" |
|---|
| 367 | . "invite JID MUC\n" |
|---|
| 368 | . " Invite JID to MUC.\n" |
|---|
| 369 | . " The muc is taken from the current message if not supplied.\n\n" |
|---|
| 370 | . "configure MUC\n" |
|---|
| 371 | . " Configure [muc].\n" |
|---|
| 372 | . " Necessary to initalize a new MUC\n" |
|---|
| 373 | . " At present, only the default configuration is supported.", |
|---|
| 374 | usage => "jmuc COMMAND ARGS" |
|---|
| 375 | } |
|---|
| 376 | ); |
|---|
| 377 | BarnOwl::new_command( |
|---|
| 378 | jroster => \&cmd_jroster, |
|---|
| 379 | { |
|---|
| 380 | summary => "Jabber Roster related commands.", |
|---|
| 381 | description => "jroster sends jabber commands related to rosters.\n\n", |
|---|
| 382 | usage => "jroster COMMAND ARGS" |
|---|
| 383 | } |
|---|
| 384 | ); |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | sub cmd_login { |
|---|
| 388 | my $cmd = shift; |
|---|
| 389 | my $jid = new Net::XMPP::JID; |
|---|
| 390 | $jid->SetJID(shift); |
|---|
| 391 | |
|---|
| 392 | my $uid = $jid->GetUserID(); |
|---|
| 393 | my $componentname = $jid->GetServer(); |
|---|
| 394 | my $resource = $jid->GetResource() || 'owl'; |
|---|
| 395 | $jid->SetResource($resource); |
|---|
| 396 | my $jidStr = $jid->GetJID('full'); |
|---|
| 397 | |
|---|
| 398 | if ( !$uid || !$componentname ) { |
|---|
| 399 | BarnOwl::error("usage: $cmd JID"); |
|---|
| 400 | return; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | if ( $conn->jidExists($jidStr) ) { |
|---|
| 404 | BarnOwl::error("Already logged in as $jidStr."); |
|---|
| 405 | return; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | my ( $server, $port ) = getServerFromJID($jid); |
|---|
| 409 | |
|---|
| 410 | $vars{jlogin_jid} = $jidStr; |
|---|
| 411 | $vars{jlogin_havepass} = 0; |
|---|
| 412 | $vars{jlogin_connhash} = { |
|---|
| 413 | hostname => $server, |
|---|
| 414 | tls => 1, |
|---|
| 415 | port => $port, |
|---|
| 416 | componentname => $componentname |
|---|
| 417 | }; |
|---|
| 418 | $vars{jlogin_authhash} = |
|---|
| 419 | { username => $uid, |
|---|
| 420 | resource => $resource, |
|---|
| 421 | }; |
|---|
| 422 | |
|---|
| 423 | return do_login(''); |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | sub do_login { |
|---|
| 427 | $vars{jlogin_password} = shift; |
|---|
| 428 | $vars{jlogin_authhash}->{password} = sub { return $vars{jlogin_password} || '' }; |
|---|
| 429 | my $jidStr = $vars{jlogin_jid}; |
|---|
| 430 | if ( !$jidStr && $vars{jlogin_havepass}) { |
|---|
| 431 | BarnOwl::error("Got password but have no jid!"); |
|---|
| 432 | } |
|---|
| 433 | else |
|---|
| 434 | { |
|---|
| 435 | my $client = $conn->addConnection($jidStr); |
|---|
| 436 | |
|---|
| 437 | #XXX Todo: Add more callbacks. |
|---|
| 438 | # * MUC presence handlers |
|---|
| 439 | # We use the anonymous subrefs in order to have the correct behavior |
|---|
| 440 | # when we reload |
|---|
| 441 | $client->SetMessageCallBacks( |
|---|
| 442 | chat => sub { BarnOwl::Jabber::process_incoming_chat_message(@_) }, |
|---|
| 443 | error => sub { BarnOwl::Jabber::process_incoming_error_message(@_) }, |
|---|
| 444 | groupchat => sub { BarnOwl::Jabber::process_incoming_groupchat_message(@_) }, |
|---|
| 445 | headline => sub { BarnOwl::Jabber::process_incoming_headline_message(@_) }, |
|---|
| 446 | normal => sub { BarnOwl::Jabber::process_incoming_normal_message(@_) } |
|---|
| 447 | ); |
|---|
| 448 | $client->SetPresenceCallBacks( |
|---|
| 449 | available => sub { BarnOwl::Jabber::process_presence_available(@_) }, |
|---|
| 450 | unavailable => sub { BarnOwl::Jabber::process_presence_available(@_) }, |
|---|
| 451 | subscribe => sub { BarnOwl::Jabber::process_presence_subscribe(@_) }, |
|---|
| 452 | subscribed => sub { BarnOwl::Jabber::process_presence_subscribed(@_) }, |
|---|
| 453 | unsubscribe => sub { BarnOwl::Jabber::process_presence_unsubscribe(@_) }, |
|---|
| 454 | unsubscribed => sub { BarnOwl::Jabber::process_presence_unsubscribed(@_) }, |
|---|
| 455 | error => sub { BarnOwl::Jabber::process_presence_error(@_) }); |
|---|
| 456 | |
|---|
| 457 | my $status = $client->Connect( %{ $vars{jlogin_connhash} } ); |
|---|
| 458 | if ( !$status ) { |
|---|
| 459 | $conn->removeConnection($jidStr); |
|---|
| 460 | BarnOwl::error("We failed to connect"); |
|---|
| 461 | } else { |
|---|
| 462 | my @result = $client->AuthSend( %{ $vars{jlogin_authhash} } ); |
|---|
| 463 | |
|---|
| 464 | if ( !@result || $result[0] ne 'ok' ) { |
|---|
| 465 | if ( !$vars{jlogin_havepass} && ( !@result || $result[0] eq '401' ) ) { |
|---|
| 466 | $vars{jlogin_havepass} = 1; |
|---|
| 467 | $conn->removeConnection($jidStr); |
|---|
| 468 | BarnOwl::start_password( "Password for $jidStr: ", \&do_login ); |
|---|
| 469 | return ""; |
|---|
| 470 | } |
|---|
| 471 | $conn->removeConnection($jidStr); |
|---|
| 472 | BarnOwl::error( "Error in connect: " . join( " ", @result ) ); |
|---|
| 473 | } else { |
|---|
| 474 | $conn->getRosterFromJidStr($jidStr)->fetch(); |
|---|
| 475 | $client->PresenceSend( priority => 1 ); |
|---|
| 476 | queue_admin_msg("Connected to jabber as $jidStr"); |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | } |
|---|
| 481 | delete $vars{jlogin_jid}; |
|---|
| 482 | $vars{jlogin_password} =~ tr/\0-\377/x/; |
|---|
| 483 | delete $vars{jlogin_password}; |
|---|
| 484 | delete $vars{jlogin_havepass}; |
|---|
| 485 | delete $vars{jlogin_connhash}; |
|---|
| 486 | delete $vars{jlogin_authhash}; |
|---|
| 487 | return ""; |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | sub do_logout { |
|---|
| 491 | my $jid = shift; |
|---|
| 492 | my $disconnected = $conn->removeConnection($jid); |
|---|
| 493 | queue_admin_msg("Jabber disconnected ($jid).") if $disconnected; |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | sub cmd_logout { |
|---|
| 497 | # Logged into multiple accounts |
|---|
| 498 | if ( $conn->connected() > 1 ) { |
|---|
| 499 | # Logged into multiple accounts, no accout specified. |
|---|
| 500 | if ( !$_[1] ) { |
|---|
| 501 | my $errStr = |
|---|
| 502 | "You are logged into multiple accounts. Please specify an account to log out of.\n"; |
|---|
| 503 | foreach my $jid ( $conn->getJids() ) { |
|---|
| 504 | $errStr .= "\t$jid\n"; |
|---|
| 505 | } |
|---|
| 506 | queue_admin_msg($errStr); |
|---|
| 507 | } |
|---|
| 508 | # Logged into multiple accounts, account specified. |
|---|
| 509 | else { |
|---|
| 510 | if ( $_[1] eq '-a' ) #All accounts. |
|---|
| 511 | { |
|---|
| 512 | foreach my $jid ( $conn->getJids() ) { |
|---|
| 513 | do_logout($jid); |
|---|
| 514 | } |
|---|
| 515 | } |
|---|
| 516 | else #One account. |
|---|
| 517 | { |
|---|
| 518 | my $jid = resolveJID( $_[1] ); |
|---|
| 519 | do_logout($jid) if ( $jid ne '' ); |
|---|
| 520 | } |
|---|
| 521 | } |
|---|
| 522 | } |
|---|
| 523 | else # Only one account logged in. |
|---|
| 524 | { |
|---|
| 525 | do_logout( ( $conn->getJids() )[0] ); |
|---|
| 526 | } |
|---|
| 527 | return ""; |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | sub cmd_jlist { |
|---|
| 531 | if ( !( scalar $conn->getJids() ) ) { |
|---|
| 532 | BarnOwl::error("You are not logged in to Jabber."); |
|---|
| 533 | return; |
|---|
| 534 | } |
|---|
| 535 | BarnOwl::popless_ztext( onGetBuddyList() ); |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | sub cmd_jwrite { |
|---|
| 539 | if ( !$conn->connected() ) { |
|---|
| 540 | BarnOwl::error("You are not logged in to Jabber."); |
|---|
| 541 | return; |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | my $jwrite_to = ""; |
|---|
| 545 | my $jwrite_from = ""; |
|---|
| 546 | my $jwrite_sid = ""; |
|---|
| 547 | my $jwrite_thread = ""; |
|---|
| 548 | my $jwrite_subject = ""; |
|---|
| 549 | my $jwrite_type = "chat"; |
|---|
| 550 | |
|---|
| 551 | my @args = @_; |
|---|
| 552 | shift; |
|---|
| 553 | local @ARGV = @_; |
|---|
| 554 | my $gc; |
|---|
| 555 | GetOptions( |
|---|
| 556 | 'thread=s' => \$jwrite_thread, |
|---|
| 557 | 'subject=s' => \$jwrite_subject, |
|---|
| 558 | 'account=s' => \$jwrite_from, |
|---|
| 559 | 'id=s' => \$jwrite_sid, |
|---|
| 560 | 'groupchat' => \$gc |
|---|
| 561 | ); |
|---|
| 562 | $jwrite_type = 'groupchat' if $gc; |
|---|
| 563 | |
|---|
| 564 | if ( scalar @ARGV != 1 ) { |
|---|
| 565 | BarnOwl::error( |
|---|
| 566 | "Usage: jwrite JID [-g] [-t thread] [-s 'subject'] [-a account]"); |
|---|
| 567 | return; |
|---|
| 568 | } |
|---|
| 569 | else { |
|---|
| 570 | $jwrite_to = shift @ARGV; |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | if ( !$jwrite_from ) { |
|---|
| 574 | if ( $conn->connected() == 1 ) { |
|---|
| 575 | $jwrite_from = ( $conn->getJids() )[0]; |
|---|
| 576 | } |
|---|
| 577 | else { |
|---|
| 578 | BarnOwl::error("Please specify an account with -a JID"); |
|---|
| 579 | return; |
|---|
| 580 | } |
|---|
| 581 | } |
|---|
| 582 | else { |
|---|
| 583 | $jwrite_from = resolveJID($jwrite_from); |
|---|
| 584 | return unless $jwrite_from; |
|---|
| 585 | } |
|---|
| 586 | |
|---|
| 587 | $vars{jwrite} = { |
|---|
| 588 | to => $jwrite_to, |
|---|
| 589 | from => $jwrite_from, |
|---|
| 590 | sid => $jwrite_sid, |
|---|
| 591 | subject => $jwrite_subject, |
|---|
| 592 | thread => $jwrite_thread, |
|---|
| 593 | type => $jwrite_type |
|---|
| 594 | }; |
|---|
| 595 | |
|---|
| 596 | BarnOwl::message( |
|---|
| 597 | "Type your message below. End with a dot on a line by itself. ^C will quit." |
|---|
| 598 | ); |
|---|
| 599 | BarnOwl::start_edit_win( join( ' ', @args ), \&process_owl_jwrite ); |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | sub cmd_jmuc { |
|---|
| 603 | die "You are not logged in to Jabber" unless $conn->connected(); |
|---|
| 604 | my $ocmd = shift; |
|---|
| 605 | my $cmd = shift; |
|---|
| 606 | if ( !$cmd ) { |
|---|
| 607 | |
|---|
| 608 | #XXX TODO: Write general usage for jmuc command. |
|---|
| 609 | return; |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | my %jmuc_commands = ( |
|---|
| 613 | join => \&jmuc_join, |
|---|
| 614 | part => \&jmuc_part, |
|---|
| 615 | invite => \&jmuc_invite, |
|---|
| 616 | configure => \&jmuc_configure, |
|---|
| 617 | presence => \&jmuc_presence |
|---|
| 618 | ); |
|---|
| 619 | my $func = $jmuc_commands{$cmd}; |
|---|
| 620 | if ( !$func ) { |
|---|
| 621 | BarnOwl::error("jmuc: Unknown command: $cmd"); |
|---|
| 622 | return; |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | { |
|---|
| 626 | local @ARGV = @_; |
|---|
| 627 | my $jid; |
|---|
| 628 | my $muc; |
|---|
| 629 | my $m = BarnOwl::getcurmsg(); |
|---|
| 630 | if ( $m && $m->is_jabber && $m->{jtype} eq 'groupchat' ) { |
|---|
| 631 | $muc = $m->{room}; |
|---|
| 632 | $jid = $m->{to}; |
|---|
| 633 | } |
|---|
| 634 | |
|---|
| 635 | my $getopt = Getopt::Long::Parser->new; |
|---|
| 636 | $getopt->configure('pass_through'); |
|---|
| 637 | $getopt->getoptions( 'account=s' => \$jid ); |
|---|
| 638 | $jid ||= defaultJID(); |
|---|
| 639 | if ($jid) { |
|---|
| 640 | $jid = resolveJID($jid); |
|---|
| 641 | return unless $jid; |
|---|
| 642 | } |
|---|
| 643 | else { |
|---|
| 644 | BarnOwl::error('You must specify an account with -a {jid}'); |
|---|
| 645 | } |
|---|
| 646 | return $func->( $jid, $muc, @ARGV ); |
|---|
| 647 | } |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | sub jmuc_join { |
|---|
| 651 | my ( $jid, $muc, @args ) = @_; |
|---|
| 652 | local @ARGV = @args; |
|---|
| 653 | my $password; |
|---|
| 654 | GetOptions( 'password=s' => \$password ); |
|---|
| 655 | |
|---|
| 656 | $muc = shift @ARGV |
|---|
| 657 | or die("Usage: jmuc join MUC [-p password] [-a account]"); |
|---|
| 658 | |
|---|
| 659 | $conn->getConnectionFromJidStr($jid)->MUCJoin(Jid => $muc, |
|---|
| 660 | Password => $password, |
|---|
| 661 | History => { |
|---|
| 662 | MaxChars => 0 |
|---|
| 663 | }); |
|---|
| 664 | return; |
|---|
| 665 | } |
|---|
| 666 | |
|---|
| 667 | sub jmuc_part { |
|---|
| 668 | my ( $jid, $muc, @args ) = @_; |
|---|
| 669 | |
|---|
| 670 | $muc = shift @args if scalar @args; |
|---|
| 671 | die("Usage: jmuc part MUC [-a account]") unless $muc; |
|---|
| 672 | |
|---|
| 673 | $conn->getConnectionFromJidStr($jid)->MUCLeave(JID => $muc); |
|---|
| 674 | queue_admin_msg("$jid has left $muc."); |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | sub jmuc_invite { |
|---|
| 678 | my ( $jid, $muc, @args ) = @_; |
|---|
| 679 | |
|---|
| 680 | my $invite_jid = shift @args; |
|---|
| 681 | $muc = shift @args if scalar @args; |
|---|
| 682 | |
|---|
| 683 | die('Usage: jmuc invite JID [muc] [-a account]') |
|---|
| 684 | unless $muc && $invite_jid; |
|---|
| 685 | |
|---|
| 686 | my $message = Net::Jabber::Message->new(); |
|---|
| 687 | $message->SetTo($muc); |
|---|
| 688 | my $x = $message->NewChild('http://jabber.org/protocol/muc#user'); |
|---|
| 689 | $x->AddInvite(); |
|---|
| 690 | $x->GetInvite()->SetTo($invite_jid); |
|---|
| 691 | $conn->getConnectionFromJidStr($jid)->Send($message); |
|---|
| 692 | queue_admin_msg("$jid has invited $invite_jid to $muc."); |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | sub jmuc_configure { |
|---|
| 696 | my ( $jid, $muc, @args ) = @_; |
|---|
| 697 | $muc = shift @args if scalar @args; |
|---|
| 698 | die("Usage: jmuc configure [muc]") unless $muc; |
|---|
| 699 | my $iq = Net::Jabber::IQ->new(); |
|---|
| 700 | $iq->SetTo($muc); |
|---|
| 701 | $iq->SetType('set'); |
|---|
| 702 | my $query = $iq->NewQuery("http://jabber.org/protocol/muc#owner"); |
|---|
| 703 | my $x = $query->NewChild("jabber:x:data"); |
|---|
| 704 | $x->SetType('submit'); |
|---|
| 705 | |
|---|
| 706 | $conn->getConnectionFromJidStr($jid)->Send($iq); |
|---|
| 707 | queue_admin_msg("Accepted default instant configuration for $muc"); |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | sub jmuc_presence { |
|---|
| 711 | my ( $jid, $muc, @args ) = @_; |
|---|
| 712 | |
|---|
| 713 | $muc = shift @args if scalar @args; |
|---|
| 714 | die("Usage: jmuc presence MUC") unless $muc; |
|---|
| 715 | |
|---|
| 716 | my $m = $conn->getConnectionFromJidStr($jid)->FindMUC(jid => $muc); |
|---|
| 717 | die("No such muc: $muc") unless $m; |
|---|
| 718 | |
|---|
| 719 | my @jids = $m->Presence(); |
|---|
| 720 | BarnOwl::popless_ztext("JIDs present in " . $m->BaseJID . "\n\t" . |
|---|
| 721 | join("\n\t", map {$_->GetResource}@jids) . "\n"); |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | |
|---|
| 725 | #XXX TODO: Consider merging this with jmuc and selecting off the first two args. |
|---|
| 726 | sub cmd_jroster { |
|---|
| 727 | die "You are not logged in to Jabber" unless $conn->connected(); |
|---|
| 728 | my $ocmd = shift; |
|---|
| 729 | my $cmd = shift; |
|---|
| 730 | if ( !$cmd ) { |
|---|
| 731 | |
|---|
| 732 | #XXX TODO: Write general usage for jroster command. |
|---|
| 733 | return; |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | my %jroster_commands = ( |
|---|
| 737 | sub => \&jroster_sub, |
|---|
| 738 | unsub => \&jroster_unsub, |
|---|
| 739 | add => \&jroster_add, |
|---|
| 740 | remove => \&jroster_remove, |
|---|
| 741 | auth => \&jroster_auth, |
|---|
| 742 | deauth => \&jroster_deauth |
|---|
| 743 | ); |
|---|
| 744 | |
|---|
| 745 | my $func = $jroster_commands{$cmd}; |
|---|
| 746 | if ( !$func ) { |
|---|
| 747 | BarnOwl::error("jroster: Unknown command: $cmd"); |
|---|
| 748 | return; |
|---|
| 749 | } |
|---|
| 750 | |
|---|
| 751 | { |
|---|
| 752 | local @ARGV = @_; |
|---|
| 753 | my $jid; |
|---|
| 754 | my $name; |
|---|
| 755 | my @groups; |
|---|
| 756 | my $purgeGroups; |
|---|
| 757 | my $getopt = Getopt::Long::Parser->new; |
|---|
| 758 | $getopt->configure('pass_through'); |
|---|
| 759 | $getopt->getoptions( |
|---|
| 760 | 'account=s' => \$jid, |
|---|
| 761 | 'group=s' => \@groups, |
|---|
| 762 | 'purgegroups' => \$purgeGroups, |
|---|
| 763 | 'name=s' => \$name |
|---|
| 764 | ); |
|---|
| 765 | $jid ||= defaultJID(); |
|---|
| 766 | if ($jid) { |
|---|
| 767 | $jid = resolveJID($jid); |
|---|
| 768 | return unless $jid; |
|---|
| 769 | } |
|---|
| 770 | else { |
|---|
| 771 | BarnOwl::error('You must specify an account with -a {jid}'); |
|---|
| 772 | } |
|---|
| 773 | return $func->( $jid, $name, \@groups, $purgeGroups, @ARGV ); |
|---|
| 774 | } |
|---|
| 775 | } |
|---|
| 776 | |
|---|
| 777 | sub jroster_sub { |
|---|
| 778 | my $jid = shift; |
|---|
| 779 | my $name = shift; |
|---|
| 780 | my @groups = @{ shift() }; |
|---|
| 781 | my $purgeGroups = shift; |
|---|
| 782 | my $baseJid = baseJID($jid); |
|---|
| 783 | |
|---|
| 784 | my $roster = $conn->getRosterFromJidStr($jid); |
|---|
| 785 | |
|---|
| 786 | # Adding lots of users with the same name is a bad idea. |
|---|
| 787 | $name = "" unless (1 == scalar(@ARGV)); |
|---|
| 788 | |
|---|
| 789 | my $p = new Net::XMPP::Presence; |
|---|
| 790 | $p->SetType('subscribe'); |
|---|
| 791 | |
|---|
| 792 | foreach my $to (@ARGV) { |
|---|
| 793 | jroster_add($jid, $name, \@groups, $purgeGroups, ($to)) unless ($roster->exists($to)); |
|---|
| 794 | |
|---|
| 795 | $p->SetTo($to); |
|---|
| 796 | $conn->getConnectionFromJidStr($jid)->Send($p); |
|---|
| 797 | queue_admin_msg("You ($baseJid) have requested a subscription to ($to)'s presence."); |
|---|
| 798 | } |
|---|
| 799 | } |
|---|
| 800 | |
|---|
| 801 | sub jroster_unsub { |
|---|
| 802 | my $jid = shift; |
|---|
| 803 | my $name = shift; |
|---|
| 804 | my @groups = @{ shift() }; |
|---|
| 805 | my $purgeGroups = shift; |
|---|
| 806 | my $baseJid = baseJID($jid); |
|---|
| 807 | |
|---|
| 808 | my $p = new Net::XMPP::Presence; |
|---|
| 809 | $p->SetType('unsubscribe'); |
|---|
| 810 | foreach my $to (@ARGV) { |
|---|
| 811 | $p->SetTo($to); |
|---|
| 812 | $conn->getConnectionFromJidStr($jid)->Send($p); |
|---|
| 813 | queue_admin_msg("You ($baseJid) have unsubscribed from ($to)'s presence."); |
|---|
| 814 | } |
|---|
| 815 | } |
|---|
| 816 | |
|---|
| 817 | sub jroster_add { |
|---|
| 818 | my $jid = shift; |
|---|
| 819 | my $name = shift; |
|---|
| 820 | my @groups = @{ shift() }; |
|---|
| 821 | my $purgeGroups = shift; |
|---|
| 822 | my $baseJid = baseJID($jid); |
|---|
| 823 | |
|---|
| 824 | my $roster = $conn->getRosterFromJidStr($jid); |
|---|
| 825 | |
|---|
| 826 | # Adding lots of users with the same name is a bad idea. |
|---|
| 827 | $name = "" unless (1 == scalar(@ARGV)); |
|---|
| 828 | |
|---|
| 829 | foreach my $to (@ARGV) { |
|---|
| 830 | my %jq = $roster->query($to); |
|---|
| 831 | my $iq = new Net::XMPP::IQ; |
|---|
| 832 | $iq->SetType('set'); |
|---|
| 833 | my $item = new XML::Stream::Node('item'); |
|---|
| 834 | $iq->NewChild('jabber:iq:roster')->AddChild($item); |
|---|
| 835 | |
|---|
| 836 | my %allGroups = (); |
|---|
| 837 | |
|---|
| 838 | foreach my $g (@groups) { |
|---|
| 839 | $allGroups{$g} = $g; |
|---|
| 840 | } |
|---|
| 841 | |
|---|
| 842 | unless ($purgeGroups) { |
|---|
| 843 | foreach my $g (@{$jq{groups}}) { |
|---|
| 844 | $allGroups{$g} = $g; |
|---|
| 845 | } |
|---|
| 846 | } |
|---|
| 847 | |
|---|
| 848 | foreach my $g (keys %allGroups) { |
|---|
| 849 | $item->add_child('group')->add_cdata($g); |
|---|
| 850 | } |
|---|
| 851 | |
|---|
| 852 | $item->put_attrib(jid => $to); |
|---|
| 853 | $item->put_attrib(name => $name) if $name; |
|---|
| 854 | $conn->getConnectionFromJidStr($jid)->Send($iq); |
|---|
| 855 | my $msg = "$baseJid: " |
|---|
| 856 | . ($name ? "$name ($to)" : "($to)") |
|---|
| 857 | . " is on your roster in the following groups: { " |
|---|
| 858 | . join(" , ", keys %allGroups) |
|---|
| 859 | . " }"; |
|---|
| 860 | queue_admin_msg($msg); |
|---|
| 861 | } |
|---|
| 862 | } |
|---|
| 863 | |
|---|
| 864 | sub jroster_remove { |
|---|
| 865 | my $jid = shift; |
|---|
| 866 | my $name = shift; |
|---|
| 867 | my @groups = @{ shift() }; |
|---|
| 868 | my $purgeGroups = shift; |
|---|
| 869 | my $baseJid = baseJID($jid); |
|---|
| 870 | |
|---|
| 871 | my $iq = new Net::XMPP::IQ; |
|---|
| 872 | $iq->SetType('set'); |
|---|
| 873 | my $item = new XML::Stream::Node('item'); |
|---|
| 874 | $iq->NewChild('jabber:iq:roster')->AddChild($item); |
|---|
| 875 | $item->put_attrib(subscription=> 'remove'); |
|---|
| 876 | foreach my $to (@ARGV) { |
|---|
| 877 | $item->put_attrib(jid => $to); |
|---|
| 878 | $conn->getConnectionFromJidStr($jid)->Send($iq); |
|---|
| 879 | queue_admin_msg("You ($baseJid) have removed ($to) from your roster."); |
|---|
| 880 | } |
|---|
| 881 | } |
|---|
| 882 | |
|---|
| 883 | sub jroster_auth { |
|---|
| 884 | my $jid = shift; |
|---|
| 885 | my $name = shift; |
|---|
| 886 | my @groups = @{ shift() }; |
|---|
| 887 | my $purgeGroups = shift; |
|---|
| 888 | my $baseJid = baseJID($jid); |
|---|
| 889 | |
|---|
| 890 | my $p = new Net::XMPP::Presence; |
|---|
| 891 | $p->SetType('subscribed'); |
|---|
| 892 | foreach my $to (@ARGV) { |
|---|
| 893 | $p->SetTo($to); |
|---|
| 894 | $conn->getConnectionFromJidStr($jid)->Send($p); |
|---|
| 895 | queue_admin_msg("($to) has been subscribed to your ($baseJid) presence."); |
|---|
| 896 | } |
|---|
| 897 | } |
|---|
| 898 | |
|---|
| 899 | sub jroster_deauth { |
|---|
| 900 | my $jid = shift; |
|---|
| 901 | my $name = shift; |
|---|
| 902 | my @groups = @{ shift() }; |
|---|
| 903 | my $purgeGroups = shift; |
|---|
| 904 | my $baseJid = baseJID($jid); |
|---|
| 905 | |
|---|
| 906 | my $p = new Net::XMPP::Presence; |
|---|
| 907 | $p->SetType('unsubscribed'); |
|---|
| 908 | foreach my $to (@ARGV) { |
|---|
| 909 | $p->SetTo($to); |
|---|
| 910 | $conn->getConnectionFromJidStr($jid)->Send($p); |
|---|
| 911 | queue_admin_msg("($to) has been unsubscribed from your ($baseJid) presence."); |
|---|
| 912 | } |
|---|
| 913 | } |
|---|
| 914 | |
|---|
| 915 | ################################################################################ |
|---|
| 916 | ### Owl Callbacks |
|---|
| 917 | sub process_owl_jwrite { |
|---|
| 918 | my $body = shift; |
|---|
| 919 | |
|---|
| 920 | my $j = new Net::XMPP::Message; |
|---|
| 921 | $body =~ s/\n\z//; |
|---|
| 922 | $j->SetMessage( |
|---|
| 923 | to => $vars{jwrite}{to}, |
|---|
| 924 | from => $vars{jwrite}{from}, |
|---|
| 925 | type => $vars{jwrite}{type}, |
|---|
| 926 | body => $body |
|---|
| 927 | ); |
|---|
| 928 | |
|---|
| 929 | $j->SetThread( $vars{jwrite}{thread} ) if ( $vars{jwrite}{thread} ); |
|---|
| 930 | $j->SetSubject( $vars{jwrite}{subject} ) if ( $vars{jwrite}{subject} ); |
|---|
| 931 | |
|---|
| 932 | my $m = j2o( $j, { direction => 'out' } ); |
|---|
| 933 | if ( $vars{jwrite}{type} ne 'groupchat' && BarnOwl::getvar('displayoutgoing') eq 'on') { |
|---|
| 934 | BarnOwl::queue_message($m); |
|---|
| 935 | } |
|---|
| 936 | |
|---|
| 937 | if ($vars{jwrite}{sid} && $conn->sidExists( $vars{jwrite}{sid} )) { |
|---|
| 938 | $conn->getConnectionFromSid($vars{jwrite}{sid})->Send($j); |
|---|
| 939 | } |
|---|
| 940 | else { |
|---|
| 941 | $conn->getConnectionFromJidStr($vars{jwrite}{from})->Send($j); |
|---|
| 942 | } |
|---|
| 943 | |
|---|
| 944 | delete $vars{jwrite}; |
|---|
| 945 | BarnOwl::message(""); # Kludge to make the ``type your message...'' message go away |
|---|
| 946 | } |
|---|
| 947 | |
|---|
| 948 | ### XMPP Callbacks |
|---|
| 949 | |
|---|
| 950 | sub process_incoming_chat_message { |
|---|
| 951 | my ( $sid, $j ) = @_; |
|---|
| 952 | BarnOwl::queue_message( j2o( $j, { direction => 'in', |
|---|
| 953 | sid => $sid } ) ); |
|---|
| 954 | } |
|---|
| 955 | |
|---|
| 956 | sub process_incoming_error_message { |
|---|
| 957 | my ( $sid, $j ) = @_; |
|---|
| 958 | my %jhash = j2hash( $j, { direction => 'in', |
|---|
| 959 | sid => $sid } ); |
|---|
| 960 | $jhash{type} = 'admin'; |
|---|
| 961 | BarnOwl::queue_message( BarnOwl::Message->new(%jhash) ); |
|---|
| 962 | } |
|---|
| 963 | |
|---|
| 964 | sub process_incoming_groupchat_message { |
|---|
| 965 | my ( $sid, $j ) = @_; |
|---|
| 966 | |
|---|
| 967 | # HACK IN PROGRESS (ignoring delayed messages) |
|---|
| 968 | return if ( $j->DefinedX('jabber:x:delay') && $j->GetX('jabber:x:delay') ); |
|---|
| 969 | BarnOwl::queue_message( j2o( $j, { direction => 'in', |
|---|
| 970 | sid => $sid } ) ); |
|---|
| 971 | } |
|---|
| 972 | |
|---|
| 973 | sub process_incoming_headline_message { |
|---|
| 974 | my ( $sid, $j ) = @_; |
|---|
| 975 | BarnOwl::queue_message( j2o( $j, { direction => 'in', |
|---|
| 976 | sid => $sid } ) ); |
|---|
| 977 | } |
|---|
| 978 | |
|---|
| 979 | sub process_incoming_normal_message { |
|---|
| 980 | my ( $sid, $j ) = @_; |
|---|
| 981 | my %jhash = j2hash( $j, { direction => 'in', |
|---|
| 982 | sid => $sid } ); |
|---|
| 983 | |
|---|
| 984 | # XXX TODO: handle things such as MUC invites here. |
|---|
| 985 | |
|---|
| 986 | # if ($j->HasX('http://jabber.org/protocol/muc#user')) |
|---|
| 987 | # { |
|---|
| 988 | # my $x = $j->GetX('http://jabber.org/protocol/muc#user'); |
|---|
| 989 | # if ($x->HasChild('invite')) |
|---|
| 990 | # { |
|---|
| 991 | # $props |
|---|
| 992 | # } |
|---|
| 993 | # } |
|---|
| 994 | # |
|---|
| 995 | BarnOwl::queue_message( BarnOwl::Message->new(%jhash) ); |
|---|
| 996 | } |
|---|
| 997 | |
|---|
| 998 | sub process_muc_presence { |
|---|
| 999 | my ( $sid, $p ) = @_; |
|---|
| 1000 | return unless ( $p->HasX('http://jabber.org/protocol/muc#user') ); |
|---|
| 1001 | } |
|---|
| 1002 | |
|---|
| 1003 | |
|---|
| 1004 | sub process_presence_available { |
|---|
| 1005 | my ( $sid, $p ) = @_; |
|---|
| 1006 | my $from = $p->GetFrom(); |
|---|
| 1007 | my $to = $p->GetTo(); |
|---|
| 1008 | my $type = $p->GetType(); |
|---|
| 1009 | my %props = ( |
|---|
| 1010 | to => $to, |
|---|
| 1011 | from => $from, |
|---|
| 1012 | recipient => $to, |
|---|
| 1013 | sender => $from, |
|---|
| 1014 | type => 'jabber', |
|---|
| 1015 | jtype => $p->GetType(), |
|---|
| 1016 | status => $p->GetStatus(), |
|---|
| 1017 | show => $p->GetShow(), |
|---|
| 1018 | xml => $p->GetXML(), |
|---|
| 1019 | direction => 'in'); |
|---|
| 1020 | |
|---|
| 1021 | if ($type eq '' || $type eq 'available') { |
|---|
| 1022 | $props{body} = "$from is now online. "; |
|---|
| 1023 | $props{loginout} = 'login'; |
|---|
| 1024 | } |
|---|
| 1025 | else { |
|---|
| 1026 | $props{body} = "$from is now offline. "; |
|---|
| 1027 | $props{loginout} = 'logout'; |
|---|
| 1028 | } |
|---|
| 1029 | $props{replysendercmd} = $props{replycmd} = "jwrite $from -i $sid"; |
|---|
| 1030 | if(BarnOwl::getvar('debug') eq 'on') { |
|---|
| 1031 | BarnOwl::queue_message(BarnOwl::Message->new(%props)); |
|---|
| 1032 | } |
|---|
| 1033 | } |
|---|
| 1034 | |
|---|
| 1035 | sub process_presence_subscribe { |
|---|
| 1036 | my ( $sid, $p ) = @_; |
|---|
| 1037 | my $from = $p->GetFrom(); |
|---|
| 1038 | my $to = $p->GetTo(); |
|---|
| 1039 | my %props = ( |
|---|
| 1040 | to => $to, |
|---|
| 1041 | from => $from, |
|---|
| 1042 | xml => $p->GetXML(), |
|---|
| 1043 | type => 'admin', |
|---|
| 1044 | adminheader => 'Jabber presence: subscribe', |
|---|
| 1045 | direction => 'in'); |
|---|
| 1046 | |
|---|
| 1047 | $props{body} = "Allow user ($from) to subscribe to your ($to) presence?\n" . |
|---|
| 1048 | "(Answer with the `yes' or `no' commands)"; |
|---|
| 1049 | $props{yescommand} = "jroster auth $from -a $to"; |
|---|
| 1050 | $props{nocommand} = "jroster deauth $from -a $to"; |
|---|
| 1051 | $props{question} = "true"; |
|---|
| 1052 | BarnOwl::queue_message(BarnOwl::Message->new(%props)); |
|---|
| 1053 | } |
|---|
| 1054 | |
|---|
| 1055 | sub process_presence_unsubscribe { |
|---|
| 1056 | my ( $sid, $p ) = @_; |
|---|
| 1057 | my $from = $p->GetFrom(); |
|---|
| 1058 | my $to = $p->GetTo(); |
|---|
| 1059 | my %props = ( |
|---|
| 1060 | to => $to, |
|---|
| 1061 | from => $from, |
|---|
| 1062 | xml => $p->GetXML(), |
|---|
| 1063 | type => 'admin', |
|---|
| 1064 | adminheader => 'Jabber presence: unsubscribe', |
|---|
| 1065 | direction => 'in'); |
|---|
| 1066 | |
|---|
| 1067 | $props{body} = "The user ($from) has been unsubscribed from your ($to) presence.\n"; |
|---|
| 1068 | BarnOwl::queue_message(BarnOwl::Message->new(%props)); |
|---|
| 1069 | |
|---|
| 1070 | # Find a connection to reply with. |
|---|
| 1071 | foreach my $jid ($conn->getJids()) { |
|---|
| 1072 | my $cJid = new Net::XMPP::JID; |
|---|
| 1073 | $cJid->SetJID($jid); |
|---|
| 1074 | if ($to eq $cJid->GetJID('base') || |
|---|
| 1075 | $to eq $cJid->GetJID('full')) { |
|---|
| 1076 | my $reply = $p->Reply(type=>"unsubscribed"); |
|---|
| 1077 | $conn->getConnectionFromJidStr($jid)->Send($reply); |
|---|
| 1078 | return; |
|---|
| 1079 | } |
|---|
| 1080 | } |
|---|
| 1081 | } |
|---|
| 1082 | |
|---|
| 1083 | sub process_presence_subscribed { |
|---|
| 1084 | my ( $sid, $p ) = @_; |
|---|
| 1085 | queue_admin_msg("ignoring:".$p->GetXML()); |
|---|
| 1086 | # RFC 3921 says we should respond to this with a "subscribe" |
|---|
| 1087 | # but this causes a flood of sub/sub'd presence packets with |
|---|
| 1088 | # some servers, so we won't. We may want to detect this condition |
|---|
| 1089 | # later, and have per-server settings. |
|---|
| 1090 | return; |
|---|
| 1091 | } |
|---|
| 1092 | |
|---|
| 1093 | sub process_presence_unsubscribed { |
|---|
| 1094 | my ( $sid, $p ) = @_; |
|---|
| 1095 | queue_admin_msg("ignoring:".$p->GetXML()); |
|---|
| 1096 | # RFC 3921 says we should respond to this with a "subscribe" |
|---|
| 1097 | # but this causes a flood of unsub/unsub'd presence packets with |
|---|
| 1098 | # some servers, so we won't. We may want to detect this condition |
|---|
| 1099 | # later, and have per-server settings. |
|---|
| 1100 | return; |
|---|
| 1101 | } |
|---|
| 1102 | |
|---|
| 1103 | sub process_presence_error { |
|---|
| 1104 | my ( $sid, $p ) = @_; |
|---|
| 1105 | my $code = $p->GetErrorCode(); |
|---|
| 1106 | my $error = $p->GetError(); |
|---|
| 1107 | BarnOwl::error("Jabber: $code $error"); |
|---|
| 1108 | } |
|---|
| 1109 | |
|---|
| 1110 | |
|---|
| 1111 | ### Helper functions |
|---|
| 1112 | |
|---|
| 1113 | sub j2hash { |
|---|
| 1114 | my $j = shift; |
|---|
| 1115 | my %initProps = %{ shift() }; |
|---|
| 1116 | |
|---|
| 1117 | my $dir = 'none'; |
|---|
| 1118 | my %props = ( type => 'jabber' ); |
|---|
| 1119 | |
|---|
| 1120 | foreach my $k (keys %initProps) { |
|---|
| 1121 | $dir = $initProps{$k} if ($k eq 'direction'); |
|---|
| 1122 | $props{$k} = $initProps{$k}; |
|---|
| 1123 | } |
|---|
| 1124 | |
|---|
| 1125 | my $jtype = $props{jtype} = $j->GetType(); |
|---|
| 1126 | my $from = $j->GetFrom('jid'); |
|---|
| 1127 | my $to = $j->GetTo('jid'); |
|---|
| 1128 | |
|---|
| 1129 | $props{from} = $from->GetJID('full'); |
|---|
| 1130 | $props{to} = $to->GetJID('full'); |
|---|
| 1131 | |
|---|
| 1132 | $props{recipient} = $to->GetJID('base'); |
|---|
| 1133 | $props{sender} = $from->GetJID('base'); |
|---|
| 1134 | $props{subject} = $j->GetSubject() if ( $j->DefinedSubject() ); |
|---|
| 1135 | $props{thread} = $j->GetThread() if ( $j->DefinedThread() ); |
|---|
| 1136 | $props{body} = $j->GetBody() if ( $j->DefinedBody() ); |
|---|
| 1137 | $props{error} = $j->GetError() if ( $j->DefinedError() ); |
|---|
| 1138 | $props{error_code} = $j->GetErrorCode() if ( $j->DefinedErrorCode() ); |
|---|
| 1139 | $props{xml} = $j->GetXML(); |
|---|
| 1140 | |
|---|
| 1141 | if ( $jtype eq 'chat' ) { |
|---|
| 1142 | $props{replycmd} = |
|---|
| 1143 | "jwrite " . ( ( $dir eq 'in' ) ? $props{from} : $props{to} ); |
|---|
| 1144 | $props{replycmd} .= |
|---|
| 1145 | " -a " . ( ( $dir eq 'out' ) ? $props{from} : $props{to} ); |
|---|
| 1146 | $props{private} = 1; |
|---|
| 1147 | } |
|---|
| 1148 | elsif ( $jtype eq 'groupchat' ) { |
|---|
| 1149 | my $nick = $props{nick} = $from->GetResource(); |
|---|
| 1150 | my $room = $props{room} = $from->GetJID('base'); |
|---|
| 1151 | $props{replycmd} = "jwrite -g $room"; |
|---|
| 1152 | $props{replycmd} .= |
|---|
| 1153 | " -a " . ( ( $dir eq 'out' ) ? $props{from} : $props{to} ); |
|---|
| 1154 | |
|---|
| 1155 | $props{sender} = $nick || $room; |
|---|
| 1156 | $props{recipient} = $room; |
|---|
| 1157 | |
|---|
| 1158 | if ( $props{subject} && !$props{body} ) { |
|---|
| 1159 | $props{body} = |
|---|
| 1160 | '[' . $nick . " has set the topic to: " . $props{subject} . "]"; |
|---|
| 1161 | } |
|---|
| 1162 | } |
|---|
| 1163 | elsif ( $jtype eq 'normal' ) { |
|---|
| 1164 | $props{replycmd} = undef; |
|---|
| 1165 | $props{private} = 1; |
|---|
| 1166 | } |
|---|
| 1167 | elsif ( $jtype eq 'headline' ) { |
|---|
| 1168 | $props{replycmd} = undef; |
|---|
| 1169 | } |
|---|
| 1170 | elsif ( $jtype eq 'error' ) { |
|---|
| 1171 | $props{replycmd} = undef; |
|---|
| 1172 | $props{body} = "Error " |
|---|
| 1173 | . $props{error_code} |
|---|
| 1174 | . " sending to " |
|---|
| 1175 | . $props{from} . "\n" |
|---|
| 1176 | . $props{error}; |
|---|
| 1177 | } |
|---|
| 1178 | |
|---|
| 1179 | $props{replysendercmd} = $props{replycmd}; |
|---|
| 1180 | return %props; |
|---|
| 1181 | } |
|---|
| 1182 | |
|---|
| 1183 | sub j2o { |
|---|
| 1184 | return BarnOwl::Message->new( j2hash(@_) ); |
|---|
| 1185 | } |
|---|
| 1186 | |
|---|
| 1187 | sub queue_admin_msg { |
|---|
| 1188 | my $err = shift; |
|---|
| 1189 | my $m = BarnOwl::Message->new( |
|---|
| 1190 | type => 'admin', |
|---|
| 1191 | direction => 'none', |
|---|
| 1192 | body => $err |
|---|
| 1193 | ); |
|---|
| 1194 | BarnOwl::queue_message($m); |
|---|
| 1195 | } |
|---|
| 1196 | |
|---|
| 1197 | sub boldify($) { |
|---|
| 1198 | my $str = shift; |
|---|
| 1199 | |
|---|
| 1200 | return '@b(' . $str . ')' if ( $str !~ /\)/ ); |
|---|
| 1201 | return '@b<' . $str . '>' if ( $str !~ /\>/ ); |
|---|
| 1202 | return '@b{' . $str . '}' if ( $str !~ /\}/ ); |
|---|
| 1203 | return '@b[' . $str . ']' if ( $str !~ /\]/ ); |
|---|
| 1204 | |
|---|
| 1205 | my $txt = "$str"; |
|---|
| 1206 | $txt =~ s{[)]}{)\@b[)]\@b(}g; |
|---|
| 1207 | return '@b(' . $txt . ')'; |
|---|
| 1208 | } |
|---|
| 1209 | |
|---|
| 1210 | sub getServerFromJID { |
|---|
| 1211 | my $jid = shift; |
|---|
| 1212 | my $res = new Net::DNS::Resolver; |
|---|
| 1213 | my $packet = |
|---|
| 1214 | $res->search( '_xmpp-client._tcp.' . $jid->GetServer(), 'srv' ); |
|---|
| 1215 | |
|---|
| 1216 | if ($packet) # Got srv record. |
|---|
| 1217 | { |
|---|
| 1218 | my @answer = $packet->answer; |
|---|
| 1219 | return $answer[0]{target}, $answer[0]{port}; |
|---|
| 1220 | } |
|---|
| 1221 | |
|---|
| 1222 | return $jid->GetServer(), 5222; |
|---|
| 1223 | } |
|---|
| 1224 | |
|---|
| 1225 | sub defaultJID { |
|---|
| 1226 | return ( $conn->getJids() )[0] if ( $conn->connected() == 1 ); |
|---|
| 1227 | return; |
|---|
| 1228 | } |
|---|
| 1229 | |
|---|
| 1230 | sub baseJID { |
|---|
| 1231 | my $givenJidStr = shift; |
|---|
| 1232 | my $givenJid = new Net::XMPP::JID; |
|---|
| 1233 | $givenJid->SetJID($givenJidStr); |
|---|
| 1234 | return $givenJid->GetJID('base'); |
|---|
| 1235 | } |
|---|
| 1236 | |
|---|
| 1237 | sub resolveJID { |
|---|
| 1238 | my $givenJidStr = shift; |
|---|
| 1239 | my $givenJid = new Net::XMPP::JID; |
|---|
| 1240 | $givenJid->SetJID($givenJidStr); |
|---|
| 1241 | |
|---|
| 1242 | # Account fully specified. |
|---|
| 1243 | if ( $givenJid->GetResource() ) { |
|---|
| 1244 | # Specified account exists |
|---|
| 1245 | return $givenJidStr if ($conn->jidExists($givenJidStr) ); |
|---|
| 1246 | BarnOwl::error("Invalid account: $givenJidStr"); |
|---|
| 1247 | } |
|---|
| 1248 | |
|---|
| 1249 | # Disambiguate. |
|---|
| 1250 | else { |
|---|
| 1251 | my $matchingJid = ""; |
|---|
| 1252 | my $errStr = |
|---|
| 1253 | "Ambiguous account reference. Please specify a resource.\n"; |
|---|
| 1254 | my $ambiguous = 0; |
|---|
| 1255 | |
|---|
| 1256 | foreach my $jid ( $conn->getJids() ) { |
|---|
| 1257 | my $cJid = new Net::XMPP::JID; |
|---|
| 1258 | $cJid->SetJID($jid); |
|---|
| 1259 | if ( $givenJidStr eq $cJid->GetJID('base') ) { |
|---|
| 1260 | $ambiguous = 1 if ( $matchingJid ne "" ); |
|---|
| 1261 | $matchingJid = $jid; |
|---|
| 1262 | $errStr .= "\t$jid\n"; |
|---|
| 1263 | } |
|---|
| 1264 | } |
|---|
| 1265 | |
|---|
| 1266 | # Need further disambiguation. |
|---|
| 1267 | if ($ambiguous) { |
|---|
| 1268 | queue_admin_msg($errStr); |
|---|
| 1269 | } |
|---|
| 1270 | |
|---|
| 1271 | # Not one of ours. |
|---|
| 1272 | elsif ( $matchingJid eq "" ) { |
|---|
| 1273 | BarnOwl::error("Invalid account: $givenJidStr"); |
|---|
| 1274 | } |
|---|
| 1275 | |
|---|
| 1276 | # It's this one. |
|---|
| 1277 | else { |
|---|
| 1278 | return $matchingJid; |
|---|
| 1279 | } |
|---|
| 1280 | } |
|---|
| 1281 | return ""; |
|---|
| 1282 | } |
|---|
| 1283 | |
|---|
| 1284 | ##################################################################### |
|---|
| 1285 | ##################################################################### |
|---|
| 1286 | |
|---|
| 1287 | package BarnOwl::Message::Jabber; |
|---|
| 1288 | |
|---|
| 1289 | our @ISA = qw( BarnOwl::Message ); |
|---|
| 1290 | |
|---|
| 1291 | sub jtype { shift->{jtype} }; |
|---|
| 1292 | sub from { shift->{from} }; |
|---|
| 1293 | sub to { shift->{to} }; |
|---|
| 1294 | sub room { shift->{room} }; |
|---|
| 1295 | |
|---|
| 1296 | sub smartfilter { |
|---|
| 1297 | my $self = shift; |
|---|
| 1298 | my $inst = shift; |
|---|
| 1299 | |
|---|
| 1300 | my ($filter, $ftext); |
|---|
| 1301 | |
|---|
| 1302 | if($self->jtype eq 'chat') { |
|---|
| 1303 | my $user; |
|---|
| 1304 | if($self->direction eq 'in') { |
|---|
| 1305 | $user = $self->from; |
|---|
| 1306 | } else { |
|---|
| 1307 | $user = $self->to; |
|---|
| 1308 | } |
|---|
| 1309 | $user = Net::Jabber::JID->new($user)->GetJID($inst ? 'full' : 'base'); |
|---|
| 1310 | $filter = "jabber-user-$user"; |
|---|
| 1311 | $ftext = qq{type ^jabber\$ and ( ( direction ^in\$ and from ^$user ) } . |
|---|
| 1312 | qq{or ( direction ^out\$ and to ^$user ) ) }; |
|---|
| 1313 | BarnOwl::filter("$filter $ftext"); |
|---|
| 1314 | return $filter; |
|---|
| 1315 | } elsif ($self->jtype eq 'groupchat') { |
|---|
| 1316 | my $room = $self->room; |
|---|
| 1317 | $filter = "jabber-room-$room"; |
|---|
| 1318 | $ftext = qq{type ^jabber\$ and room ^$room\$}; |
|---|
| 1319 | BarnOwl::filter("$filter $ftext"); |
|---|
| 1320 | return $filter; |
|---|
| 1321 | } |
|---|
| 1322 | } |
|---|
| 1323 | |
|---|
| 1324 | 1; |
|---|