| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | |
|---|
| 4 | package BarnOwl::Module::Jabber; |
|---|
| 5 | |
|---|
| 6 | =head1 NAME |
|---|
| 7 | |
|---|
| 8 | BarnOwl::Module::Jabber |
|---|
| 9 | |
|---|
| 10 | =head1 DESCRIPTION |
|---|
| 11 | |
|---|
| 12 | This module implements Jabber support for BarnOwl. |
|---|
| 13 | |
|---|
| 14 | =cut |
|---|
| 15 | |
|---|
| 16 | use BarnOwl; |
|---|
| 17 | use BarnOwl::Hooks; |
|---|
| 18 | use BarnOwl::Message::Jabber; |
|---|
| 19 | use BarnOwl::Module::Jabber::Connection; |
|---|
| 20 | use BarnOwl::Module::Jabber::ConnectionManager; |
|---|
| 21 | use BarnOwl::Completion::Util qw(complete_flags); |
|---|
| 22 | |
|---|
| 23 | use Authen::SASL qw(Perl); |
|---|
| 24 | use Net::Jabber; |
|---|
| 25 | use Net::Jabber::MUC; |
|---|
| 26 | use Net::DNS; |
|---|
| 27 | use Getopt::Long; |
|---|
| 28 | Getopt::Long::Configure(qw(no_getopt_compat prefix_pattern=-|--)); |
|---|
| 29 | |
|---|
| 30 | use utf8; |
|---|
| 31 | |
|---|
| 32 | our $VERSION = 0.1; |
|---|
| 33 | |
|---|
| 34 | BEGIN { |
|---|
| 35 | if(eval {require IO::Socket::SSL;}) { |
|---|
| 36 | if($IO::Socket::SSL::VERSION eq "0.97") { |
|---|
| 37 | BarnOwl::error("You are using IO::Socket:SSL 0.97, which \n" . |
|---|
| 38 | "contains bugs causing it not to work with BarnOwl's\n" . |
|---|
| 39 | "Jabber support. We recommend updating to the latest\n" . |
|---|
| 40 | "IO::Socket::SSL from CPAN. \n"); |
|---|
| 41 | die("Not loading Jabber.par\n"); |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | no warnings 'redefine'; |
|---|
| 47 | |
|---|
| 48 | ################################################################################ |
|---|
| 49 | # owl perl jabber support |
|---|
| 50 | # |
|---|
| 51 | # XXX Todo: |
|---|
| 52 | # Rosters for MUCs |
|---|
| 53 | # More user feedback |
|---|
| 54 | # * joining MUC |
|---|
| 55 | # * parting MUC |
|---|
| 56 | # * presence (Roster and MUC) |
|---|
| 57 | # Implementing formatting and logging callbacks for C |
|---|
| 58 | # Appropriate callbacks for presence subscription messages. |
|---|
| 59 | # |
|---|
| 60 | ################################################################################ |
|---|
| 61 | |
|---|
| 62 | our $conn; |
|---|
| 63 | $conn ||= BarnOwl::Module::Jabber::ConnectionManager->new; |
|---|
| 64 | our %vars; |
|---|
| 65 | our %completion_jids; |
|---|
| 66 | |
|---|
| 67 | sub onStart { |
|---|
| 68 | if ( *BarnOwl::queue_message{CODE} ) { |
|---|
| 69 | register_owl_commands(); |
|---|
| 70 | register_keybindings(); |
|---|
| 71 | register_filters(); |
|---|
| 72 | $BarnOwl::Hooks::getBuddyList->add("BarnOwl::Module::Jabber::onGetBuddyList"); |
|---|
| 73 | $BarnOwl::Hooks::getQuickstart->add("BarnOwl::Module::Jabber::onGetQuickstart"); |
|---|
| 74 | $vars{show} = ''; |
|---|
| 75 | BarnOwl::new_variable_bool("jabber:show_offline_buddies", |
|---|
| 76 | { default => 1, |
|---|
| 77 | summary => 'Show offline or pending buddies.'}); |
|---|
| 78 | BarnOwl::new_variable_bool("jabber:show_logins", |
|---|
| 79 | { default => 0, |
|---|
| 80 | summary => 'Show login/logout messages.'}); |
|---|
| 81 | BarnOwl::new_variable_bool("jabber:spew", |
|---|
| 82 | { default => 0, |
|---|
| 83 | summary => 'Display unrecognized Jabber messages.'}); |
|---|
| 84 | BarnOwl::new_variable_int("jabber:auto_away_timeout", |
|---|
| 85 | { default => 5, |
|---|
| 86 | summary => 'After minutes idle, auto away.', |
|---|
| 87 | }); |
|---|
| 88 | BarnOwl::new_variable_int("jabber:auto_xa_timeout", |
|---|
| 89 | { default => 15, |
|---|
| 90 | summary => 'After minutes idle, auto extended away.' |
|---|
| 91 | }); |
|---|
| 92 | BarnOwl::new_variable_bool("jabber:reconnect", |
|---|
| 93 | { default => 1, |
|---|
| 94 | summary => 'Auto-reconnect when disconnected from servers.' |
|---|
| 95 | }); |
|---|
| 96 | # Force these. Reload can screw them up. |
|---|
| 97 | # Taken from Net::Jabber::Protocol. |
|---|
| 98 | $Net::XMPP::Protocol::NEWOBJECT{'iq'} = "Net::Jabber::IQ"; |
|---|
| 99 | $Net::XMPP::Protocol::NEWOBJECT{'message'} = "Net::Jabber::Message"; |
|---|
| 100 | $Net::XMPP::Protocol::NEWOBJECT{'presence'} = "Net::Jabber::Presence"; |
|---|
| 101 | $Net::XMPP::Protocol::NEWOBJECT{'jid'} = "Net::Jabber::JID"; |
|---|
| 102 | } else { |
|---|
| 103 | # Our owl doesn't support queue_message. Unfortunately, this |
|---|
| 104 | # means it probably *also* doesn't support BarnOwl::error. So just |
|---|
| 105 | # give up silently. |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | $BarnOwl::Hooks::startup->add("BarnOwl::Module::Jabber::onStart"); |
|---|
| 110 | |
|---|
| 111 | sub do_keep_alive_and_auto_away { |
|---|
| 112 | if ( !$conn->connected() ) { |
|---|
| 113 | # We don't need this timer any more. |
|---|
| 114 | if (defined $vars{keepAliveTimer}) { |
|---|
| 115 | $vars{keepAliveTimer}->stop; |
|---|
| 116 | } |
|---|
| 117 | delete $vars{keepAliveTimer}; |
|---|
| 118 | return; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | $vars{status_changed} = 0; |
|---|
| 122 | my $auto_away = BarnOwl::getvar('jabber:auto_away_timeout'); |
|---|
| 123 | my $auto_xa = BarnOwl::getvar('jabber:auto_xa_timeout'); |
|---|
| 124 | my $idletime = BarnOwl::getidletime(); |
|---|
| 125 | if ($auto_xa != 0 && $idletime >= (60 * $auto_xa) && ($vars{show} eq 'away' || $vars{show} eq '' )) { |
|---|
| 126 | $vars{show} = 'xa'; |
|---|
| 127 | $vars{status} = 'Auto extended-away after '.$auto_xa.' minute'.($auto_xa == 1 ? '' : 's').' idle.'; |
|---|
| 128 | $vars{status_changed} = 1; |
|---|
| 129 | } elsif ($auto_away != 0 && $idletime >= (60 * $auto_away) && $vars{show} eq '') { |
|---|
| 130 | $vars{show} = 'away'; |
|---|
| 131 | $vars{status} = 'Auto away after '.$auto_away.' minute'.($auto_away == 1 ? '' : 's').' idle.'; |
|---|
| 132 | $vars{status_changed} = 1; |
|---|
| 133 | } elsif ($idletime <= $vars{idletime} && $vars{show} ne '') { |
|---|
| 134 | $vars{show} = ''; |
|---|
| 135 | $vars{status} = ''; |
|---|
| 136 | $vars{status_changed} = 1; |
|---|
| 137 | } |
|---|
| 138 | $vars{idletime} = $idletime; |
|---|
| 139 | |
|---|
| 140 | foreach my $jid ( $conn->getJIDs() ) { |
|---|
| 141 | next if $conn->jidActive($jid); |
|---|
| 142 | $conn->tryReconnect($jid); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | foreach my $jid ( $conn->getJIDs() ) { |
|---|
| 146 | next unless $conn->jidActive($jid); |
|---|
| 147 | |
|---|
| 148 | my $client = $conn->getConnectionFromJID($jid); |
|---|
| 149 | unless($client) { |
|---|
| 150 | $conn->removeConnection($jid); |
|---|
| 151 | BarnOwl::error("Connection for $jid undefined -- error in reload?"); |
|---|
| 152 | } |
|---|
| 153 | my $status = $client->Process(0); # keep-alive |
|---|
| 154 | if ( !defined($status) ) { |
|---|
| 155 | $conn->scheduleReconnect($jid); |
|---|
| 156 | } |
|---|
| 157 | if ($::shutdown) { |
|---|
| 158 | do_logout($jid); |
|---|
| 159 | next; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | if ($vars{status_changed}) { |
|---|
| 163 | my $p = new Net::Jabber::Presence; |
|---|
| 164 | $p->SetShow($vars{show}) if $vars{show}; |
|---|
| 165 | $p->SetStatus($vars{status}) if $vars{status}; |
|---|
| 166 | $client->Send($p); |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | our $showOffline = 0; |
|---|
| 172 | |
|---|
| 173 | sub blist_getBuddyStatus { |
|---|
| 174 | my $jid = shift; |
|---|
| 175 | my $buddy = shift; |
|---|
| 176 | my $roster = $conn->getRosterFromJID($jid); |
|---|
| 177 | my %jq = $roster->query($buddy); |
|---|
| 178 | my $res = $roster->resource($buddy); |
|---|
| 179 | if ($res) { |
|---|
| 180 | my %rq = $roster->resourceQuery( $buddy, $res ); |
|---|
| 181 | return $rq{show} ? $rq{show} : 'online'; |
|---|
| 182 | } else { |
|---|
| 183 | return "unknown"; |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | sub blist_listBuddy { |
|---|
| 188 | my $roster = shift; |
|---|
| 189 | my $buddy = shift; |
|---|
| 190 | my $blistStr .= " "; |
|---|
| 191 | my %jq = $roster->query($buddy); |
|---|
| 192 | my $res = $roster->resource($buddy); |
|---|
| 193 | |
|---|
| 194 | my $name = $jq{name} || $buddy->GetUserID(); |
|---|
| 195 | |
|---|
| 196 | $blistStr .= sprintf '%-15s %s', $name, $buddy->GetJID(); |
|---|
| 197 | $completion_jids{$name} = 1; |
|---|
| 198 | $completion_jids{$buddy->GetJID()} = 1; |
|---|
| 199 | |
|---|
| 200 | if ($res) { |
|---|
| 201 | my %rq = $roster->resourceQuery( $buddy, $res ); |
|---|
| 202 | $blistStr .= " [" . ( $rq{show} ? $rq{show} : 'online' ) . "]"; |
|---|
| 203 | $blistStr .= " " . $rq{status} if $rq{status}; |
|---|
| 204 | $blistStr = BarnOwl::Style::boldify($blistStr) if $showOffline; |
|---|
| 205 | } |
|---|
| 206 | else { |
|---|
| 207 | return '' unless $showOffline; |
|---|
| 208 | if ($jq{ask}) { |
|---|
| 209 | $blistStr .= " [pending]"; |
|---|
| 210 | } |
|---|
| 211 | elsif ($jq{subscription} eq 'none' || $jq{subscription} eq 'from') { |
|---|
| 212 | $blistStr .= " [not subscribed]"; |
|---|
| 213 | } |
|---|
| 214 | else { |
|---|
| 215 | $blistStr .= " [offline]"; |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | return $blistStr . "\n"; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | # Sort, ignoring markup. |
|---|
| 222 | sub blistSort { |
|---|
| 223 | return uc(BarnOwl::ztext_stylestrip($a)) cmp uc(BarnOwl::ztext_stylestrip($b)); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | sub getSingleBuddyList { |
|---|
| 227 | my $jid = shift; |
|---|
| 228 | $jid = resolveConnectedJID($jid); |
|---|
| 229 | return "" unless $jid; |
|---|
| 230 | my $blist = ""; |
|---|
| 231 | my $roster = $conn->getRosterFromJID($jid); |
|---|
| 232 | if ($roster) { |
|---|
| 233 | $blist .= BarnOwl::Style::boldify("Jabber roster for $jid\n"); |
|---|
| 234 | |
|---|
| 235 | my @gTexts = (); |
|---|
| 236 | foreach my $group ( $roster->groups() ) { |
|---|
| 237 | my @buddies = $roster->jids( 'group', $group ); |
|---|
| 238 | my @bTexts = (); |
|---|
| 239 | foreach my $buddy ( @buddies ) { |
|---|
| 240 | push(@bTexts, blist_listBuddy( $roster, $buddy )); |
|---|
| 241 | } |
|---|
| 242 | push(@gTexts, " Group: $group\n".join('',sort blistSort @bTexts)); |
|---|
| 243 | } |
|---|
| 244 | # Sort groups before adding ungrouped entries. |
|---|
| 245 | @gTexts = sort blistSort @gTexts; |
|---|
| 246 | |
|---|
| 247 | my @unsorted = $roster->jids('nogroup'); |
|---|
| 248 | if (@unsorted) { |
|---|
| 249 | my @bTexts = (); |
|---|
| 250 | foreach my $buddy (@unsorted) { |
|---|
| 251 | push(@bTexts, blist_listBuddy( $roster, $buddy )); |
|---|
| 252 | } |
|---|
| 253 | push(@gTexts, " [unsorted]\n".join('',sort blistSort @bTexts)); |
|---|
| 254 | } |
|---|
| 255 | $blist .= join('', @gTexts); |
|---|
| 256 | } |
|---|
| 257 | return $blist; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | sub onGetBuddyList { |
|---|
| 261 | $showOffline = BarnOwl::getvar('jabber:show_offline_buddies') eq 'on'; |
|---|
| 262 | my $blist = ""; |
|---|
| 263 | foreach my $jid ($conn->getJIDs()) { |
|---|
| 264 | $blist .= getSingleBuddyList($jid); |
|---|
| 265 | } |
|---|
| 266 | return $blist; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | sub onGetQuickstart { |
|---|
| 270 | return <<'EOF' |
|---|
| 271 | @b(Jabber:) |
|---|
| 272 | Type ':jabberlogin @b(username@mit.edu)' to log in to Jabber. The command |
|---|
| 273 | ':jroster sub @b(somebody@gmail.com)' will request that they let you message |
|---|
| 274 | them. Once you get a message saying you are subscribed, you can message |
|---|
| 275 | them by typing ':jwrite @b(somebody@gmail.com)' or just 'j @b(somebody)'. |
|---|
| 276 | EOF |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | ################################################################################ |
|---|
| 280 | ### Owl Commands |
|---|
| 281 | sub register_owl_commands() { |
|---|
| 282 | BarnOwl::new_command( |
|---|
| 283 | jabberlogin => \&cmd_login, |
|---|
| 284 | { |
|---|
| 285 | summary => "Log in to Jabber", |
|---|
| 286 | usage => "jabberlogin <jid> [<password>]" |
|---|
| 287 | } |
|---|
| 288 | ); |
|---|
| 289 | BarnOwl::new_command( |
|---|
| 290 | jabberlogout => \&cmd_logout, |
|---|
| 291 | { |
|---|
| 292 | summary => "Log out of Jabber", |
|---|
| 293 | usage => "jabberlogout [-A|<jid>]", |
|---|
| 294 | description => "jabberlogout logs you out of Jabber.\n\n" |
|---|
| 295 | . "If you are connected to one account, no further arguments are necessary.\n\n" |
|---|
| 296 | . "-A Log out of all accounts.\n" |
|---|
| 297 | . "<jid> Which account to log out of.\n" |
|---|
| 298 | } |
|---|
| 299 | ); |
|---|
| 300 | BarnOwl::new_command( |
|---|
| 301 | jwrite => \&cmd_jwrite, |
|---|
| 302 | { |
|---|
| 303 | summary => "Send a Jabber Message", |
|---|
| 304 | usage => "jwrite <jid> [-t <thread>] [-s <subject>] [-a <account>] [-m <message>]" |
|---|
| 305 | } |
|---|
| 306 | ); |
|---|
| 307 | BarnOwl::new_command( |
|---|
| 308 | jaway => \&cmd_jaway, |
|---|
| 309 | { |
|---|
| 310 | summary => "Set Jabber away / presence information", |
|---|
| 311 | usage => "jaway [-s online|dnd|...] [<message>]" |
|---|
| 312 | } |
|---|
| 313 | ); |
|---|
| 314 | BarnOwl::new_command( |
|---|
| 315 | jlist => \&cmd_jlist, |
|---|
| 316 | { |
|---|
| 317 | summary => "Show your Jabber roster.", |
|---|
| 318 | usage => "jlist" |
|---|
| 319 | } |
|---|
| 320 | ); |
|---|
| 321 | BarnOwl::new_command( |
|---|
| 322 | jabber_get_buddy_status => \&cmd_jabber_get_buddy_status, |
|---|
| 323 | { |
|---|
| 324 | summary => "Get the status of a given buddy.", |
|---|
| 325 | usage => "jabber_get_buddy_status [-a account] buddy" |
|---|
| 326 | } |
|---|
| 327 | ); |
|---|
| 328 | BarnOwl::new_command( |
|---|
| 329 | jmuc => \&cmd_jmuc, |
|---|
| 330 | { |
|---|
| 331 | summary => "Jabber MUC related commands.", |
|---|
| 332 | description => "jmuc sends Jabber commands related to MUC.\n\n" |
|---|
| 333 | . "The following commands are available\n\n" |
|---|
| 334 | . "join <muc>[/<nick>]\n" |
|---|
| 335 | . " Join a MUC (with a given nickname, or otherwise your JID).\n\n" |
|---|
| 336 | . "part <muc> Part a MUC.\n" |
|---|
| 337 | . " The MUC is taken from the current message if not supplied.\n\n" |
|---|
| 338 | . "invite <jid> [<muc>]\n" |
|---|
| 339 | . " Invite <jid> to <muc>.\n" |
|---|
| 340 | . " The MUC is taken from the current message if not supplied.\n\n" |
|---|
| 341 | . "configure [<muc>]\n" |
|---|
| 342 | . " Configures a MUC.\n" |
|---|
| 343 | . " Necessary to initialize a new MUC.\n" |
|---|
| 344 | . " At present, only the default configuration is supported.\n" |
|---|
| 345 | . " The MUC is taken from the current message if not supplied.\n\n" |
|---|
| 346 | . "presence [<muc>]\n" |
|---|
| 347 | . " Shows the roster for <muc>.\n" |
|---|
| 348 | . " The MUC is taken from the current message if not supplied.\n\n" |
|---|
| 349 | . "presence -a\n" |
|---|
| 350 | . " Shows rosters for all MUCs you're participating in.\n\n", |
|---|
| 351 | usage => "jmuc <command> [<args>]" |
|---|
| 352 | } |
|---|
| 353 | ); |
|---|
| 354 | BarnOwl::new_command( |
|---|
| 355 | jroster => \&cmd_jroster, |
|---|
| 356 | { |
|---|
| 357 | summary => "Jabber roster related commands.", |
|---|
| 358 | description => "jroster sends Jabber commands related to rosters.\n\n" |
|---|
| 359 | . "The following commands are available\n\n" |
|---|
| 360 | . "sub <jid> Subscribe to <jid>'s presence. (implicit add)\n\n" |
|---|
| 361 | . "add <jid> Adds <jid> to your roster.\n\n" |
|---|
| 362 | . "unsub <jid> Unsubscribe from <jid>'s presence.\n\n" |
|---|
| 363 | . "remove <jid> Removes <jid> from your roster. (implicit unsub)\n\n" |
|---|
| 364 | . "auth <jid> Authorizes <jid> to subscribe to your presence.\n\n" |
|---|
| 365 | . "deauth <jid> De-authorizes <jid>'s subscription to your presence.\n\n" |
|---|
| 366 | . "The following arguments are supported for all commands\n\n" |
|---|
| 367 | . "-a <jid> Specify which account to make the roster changes on.\n" |
|---|
| 368 | . " Required if you're signed into more than one account.\n\n" |
|---|
| 369 | . "The following arguments only work with the add and sub commands.\n\n" |
|---|
| 370 | . "-g <group> Add <jid> to group <group>.\n" |
|---|
| 371 | . " May be specified more than once, will not remove <jid> from any groups.\n\n" |
|---|
| 372 | . "-p Purge. Removes <jid> from all groups.\n" |
|---|
| 373 | . " May be combined with -g.\n\n" |
|---|
| 374 | . "-n <name> Sets <name> as <jid>'s short name.\n\n" |
|---|
| 375 | . "Note: Unless -n is used, you can specify multiple <jid> arguments.\n", |
|---|
| 376 | usage => "jroster <command> <args>" |
|---|
| 377 | } |
|---|
| 378 | ); |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | sub register_keybindings { |
|---|
| 382 | BarnOwl::bindkey(qw(recv j command start-command), 'jwrite '); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | sub register_filters { |
|---|
| 386 | BarnOwl::filter(qw(jabber type ^jabber$)); |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | sub cmd_login { |
|---|
| 390 | my $cmd = shift; |
|---|
| 391 | my $jidStr = shift; |
|---|
| 392 | my $jid = new Net::Jabber::JID; |
|---|
| 393 | $jid->SetJID($jidStr); |
|---|
| 394 | my $password = ''; |
|---|
| 395 | $password = shift if @_; |
|---|
| 396 | |
|---|
| 397 | my $uid = $jid->GetUserID(); |
|---|
| 398 | my $componentname = $jid->GetServer(); |
|---|
| 399 | my $resource = $jid->GetResource(); |
|---|
| 400 | |
|---|
| 401 | if ($resource eq '') { |
|---|
| 402 | my $cjidStr = $conn->baseJIDExists($jidStr); |
|---|
| 403 | if ($cjidStr) { |
|---|
| 404 | die("Already logged in as $cjidStr.\n"); |
|---|
| 405 | } |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | $resource ||= 'barnowl'; |
|---|
| 409 | $jid->SetResource($resource); |
|---|
| 410 | $jidStr = $jid->GetJID('full'); |
|---|
| 411 | |
|---|
| 412 | if ( !$uid || !$componentname ) { |
|---|
| 413 | die("usage: $cmd JID\n"); |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | if ( $conn->jidActive($jidStr) ) { |
|---|
| 417 | die("Already logged in as $jidStr.\n"); |
|---|
| 418 | } elsif ($conn->jidExists($jidStr)) { |
|---|
| 419 | return $conn->tryReconnect($jidStr, 1); |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | my ( $server, $port ) = getServerFromJID($jid); |
|---|
| 423 | |
|---|
| 424 | $vars{jlogin_jid} = $jidStr; |
|---|
| 425 | $vars{jlogin_connhash} = { |
|---|
| 426 | hostname => $server, |
|---|
| 427 | tls => 1, |
|---|
| 428 | port => $port, |
|---|
| 429 | componentname => $componentname |
|---|
| 430 | }; |
|---|
| 431 | $vars{jlogin_authhash} = |
|---|
| 432 | { username => $uid, |
|---|
| 433 | resource => $resource, |
|---|
| 434 | }; |
|---|
| 435 | |
|---|
| 436 | return do_login($password); |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | sub do_login { |
|---|
| 440 | $vars{jlogin_password} = shift; |
|---|
| 441 | $vars{jlogin_authhash}->{password} = sub { return $vars{jlogin_password} || '' }; |
|---|
| 442 | my $jidStr = $vars{jlogin_jid}; |
|---|
| 443 | if ( !$jidStr && $vars{jlogin_havepass}) { |
|---|
| 444 | BarnOwl::error("Got password but have no JID!"); |
|---|
| 445 | } |
|---|
| 446 | else |
|---|
| 447 | { |
|---|
| 448 | my $client = $conn->addConnection($jidStr); |
|---|
| 449 | |
|---|
| 450 | #XXX Todo: Add more callbacks. |
|---|
| 451 | # * MUC presence handlers |
|---|
| 452 | # We use the anonymous subrefs in order to have the correct behavior |
|---|
| 453 | # when we reload |
|---|
| 454 | $client->SetMessageCallBacks( |
|---|
| 455 | chat => sub { BarnOwl::Module::Jabber::process_incoming_chat_message(@_) }, |
|---|
| 456 | error => sub { BarnOwl::Module::Jabber::process_incoming_error_message(@_) }, |
|---|
| 457 | groupchat => sub { BarnOwl::Module::Jabber::process_incoming_groupchat_message(@_) }, |
|---|
| 458 | headline => sub { BarnOwl::Module::Jabber::process_incoming_headline_message(@_) }, |
|---|
| 459 | normal => sub { BarnOwl::Module::Jabber::process_incoming_normal_message(@_) } |
|---|
| 460 | ); |
|---|
| 461 | $client->SetPresenceCallBacks( |
|---|
| 462 | available => sub { BarnOwl::Module::Jabber::process_presence_available(@_) }, |
|---|
| 463 | unavailable => sub { BarnOwl::Module::Jabber::process_presence_available(@_) }, |
|---|
| 464 | subscribe => sub { BarnOwl::Module::Jabber::process_presence_subscribe(@_) }, |
|---|
| 465 | subscribed => sub { BarnOwl::Module::Jabber::process_presence_subscribed(@_) }, |
|---|
| 466 | unsubscribe => sub { BarnOwl::Module::Jabber::process_presence_unsubscribe(@_) }, |
|---|
| 467 | unsubscribed => sub { BarnOwl::Module::Jabber::process_presence_unsubscribed(@_) }, |
|---|
| 468 | error => sub { BarnOwl::Module::Jabber::process_presence_error(@_) }); |
|---|
| 469 | |
|---|
| 470 | my $status = $client->Connect( %{ $vars{jlogin_connhash} } ); |
|---|
| 471 | if ( !$status ) { |
|---|
| 472 | $conn->removeConnection($jidStr); |
|---|
| 473 | BarnOwl::error("We failed to connect."); |
|---|
| 474 | } else { |
|---|
| 475 | my @result = $client->AuthSend( %{ $vars{jlogin_authhash} } ); |
|---|
| 476 | |
|---|
| 477 | if ( !@result || $result[0] ne 'ok' ) { |
|---|
| 478 | if ( !$vars{jlogin_havepass} && ( !@result || $result[0] eq '401' || $result[0] eq 'error') ) { |
|---|
| 479 | $vars{jlogin_havepass} = 1; |
|---|
| 480 | $conn->removeConnection($jidStr); |
|---|
| 481 | BarnOwl::start_password("Password for $jidStr: ", \&do_login ); |
|---|
| 482 | return ""; |
|---|
| 483 | } |
|---|
| 484 | $conn->removeConnection($jidStr); |
|---|
| 485 | BarnOwl::error( "Error in connect: " . join( " ", @result ) ); |
|---|
| 486 | } else { |
|---|
| 487 | $conn->setAuth( |
|---|
| 488 | $jidStr, |
|---|
| 489 | { %{ $vars{jlogin_authhash} }, |
|---|
| 490 | password => $vars{jlogin_password} |
|---|
| 491 | } |
|---|
| 492 | ); |
|---|
| 493 | $client->onConnect($conn, $jidStr); |
|---|
| 494 | } |
|---|
| 495 | } |
|---|
| 496 | } |
|---|
| 497 | delete $vars{jlogin_jid}; |
|---|
| 498 | $vars{jlogin_password} =~ tr/\0-\377/x/ if $vars{jlogin_password}; |
|---|
| 499 | delete $vars{jlogin_password}; |
|---|
| 500 | delete $vars{jlogin_havepass}; |
|---|
| 501 | delete $vars{jlogin_connhash}; |
|---|
| 502 | delete $vars{jlogin_authhash}; |
|---|
| 503 | |
|---|
| 504 | return ""; |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | sub do_logout { |
|---|
| 508 | my $jid = shift; |
|---|
| 509 | my $disconnected = $conn->removeConnection($jid); |
|---|
| 510 | queue_admin_msg("Jabber disconnected ($jid).") if $disconnected; |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | sub cmd_logout { |
|---|
| 514 | return "You are not logged into Jabber." unless ($conn->connected() > 0); |
|---|
| 515 | # Logged into multiple accounts |
|---|
| 516 | if ( $conn->connected() > 1 ) { |
|---|
| 517 | # Logged into multiple accounts, no accout specified. |
|---|
| 518 | if ( !$_[1] ) { |
|---|
| 519 | my $errStr = |
|---|
| 520 | "You are logged into multiple accounts. Please specify an account to log out of.\n"; |
|---|
| 521 | foreach my $jid ( $conn->getJIDs() ) { |
|---|
| 522 | $errStr .= "\t$jid\n"; |
|---|
| 523 | } |
|---|
| 524 | queue_admin_msg($errStr); |
|---|
| 525 | } |
|---|
| 526 | # Logged into multiple accounts, account specified. |
|---|
| 527 | else { |
|---|
| 528 | if ( $_[1] eq '-A' ) #All accounts. |
|---|
| 529 | { |
|---|
| 530 | foreach my $jid ( $conn->getJIDs() ) { |
|---|
| 531 | do_logout($jid); |
|---|
| 532 | } |
|---|
| 533 | } |
|---|
| 534 | else #One account. |
|---|
| 535 | { |
|---|
| 536 | my $jid = resolveConnectedJID( $_[1] ); |
|---|
| 537 | do_logout($jid) if ( $jid ne '' ); |
|---|
| 538 | } |
|---|
| 539 | } |
|---|
| 540 | } |
|---|
| 541 | else # Only one account logged in. |
|---|
| 542 | { |
|---|
| 543 | do_logout( ( $conn->getJIDs() )[0] ); |
|---|
| 544 | } |
|---|
| 545 | return ""; |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | sub cmd_jlist { |
|---|
| 549 | if ( !( scalar $conn->getJIDs() ) ) { |
|---|
| 550 | die("You are not logged in to Jabber.\n"); |
|---|
| 551 | } |
|---|
| 552 | BarnOwl::popless_ztext( onGetBuddyList() ); |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | sub cmd_jwrite { |
|---|
| 556 | if ( !$conn->connected() ) { |
|---|
| 557 | die("You are not logged in to Jabber.\n"); |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | my $jwrite_to = ""; |
|---|
| 561 | my $jwrite_from = ""; |
|---|
| 562 | my $jwrite_sid = ""; |
|---|
| 563 | my $jwrite_thread = ""; |
|---|
| 564 | my $jwrite_subject = ""; |
|---|
| 565 | my $jwrite_body; |
|---|
| 566 | my ($to, $from); |
|---|
| 567 | my $jwrite_type = "chat"; |
|---|
| 568 | |
|---|
| 569 | my @args = @_; |
|---|
| 570 | shift; |
|---|
| 571 | local @ARGV = @_; |
|---|
| 572 | my $gc; |
|---|
| 573 | GetOptions( |
|---|
| 574 | 'thread=s' => \$jwrite_thread, |
|---|
| 575 | 'subject=s' => \$jwrite_subject, |
|---|
| 576 | 'account=s' => \$from, |
|---|
| 577 | 'id=s' => \$jwrite_sid, |
|---|
| 578 | 'message=s' => \$jwrite_body, |
|---|
| 579 | ) or die("Usage: jwrite <jid> [-t <thread>] [-s <subject>] [-a <account>]\n"); |
|---|
| 580 | $jwrite_type = 'groupchat' if $gc; |
|---|
| 581 | |
|---|
| 582 | if ( scalar @ARGV != 1 ) { |
|---|
| 583 | die("Usage: jwrite <jid> [-t <thread>] [-s <subject>] [-a <account>]\n"); |
|---|
| 584 | } |
|---|
| 585 | else { |
|---|
| 586 | $to = shift @ARGV; |
|---|
| 587 | } |
|---|
| 588 | |
|---|
| 589 | my @candidates = guess_jwrite($from, $to); |
|---|
| 590 | |
|---|
| 591 | unless(scalar @candidates) { |
|---|
| 592 | die("Unable to resolve JID $to\n"); |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | @candidates = grep {defined $_->[0]} @candidates; |
|---|
| 596 | |
|---|
| 597 | unless(scalar @candidates) { |
|---|
| 598 | if(!$from) { |
|---|
| 599 | die("You must specify an account with -a\n"); |
|---|
| 600 | } else { |
|---|
| 601 | die("Unable to resolve account $from\n"); |
|---|
| 602 | } |
|---|
| 603 | } |
|---|
| 604 | |
|---|
| 605 | |
|---|
| 606 | ($jwrite_from, $jwrite_to, $jwrite_type) = @{$candidates[0]}; |
|---|
| 607 | |
|---|
| 608 | $vars{jwrite} = { |
|---|
| 609 | to => $jwrite_to, |
|---|
| 610 | from => $jwrite_from, |
|---|
| 611 | sid => $jwrite_sid, |
|---|
| 612 | subject => $jwrite_subject, |
|---|
| 613 | thread => $jwrite_thread, |
|---|
| 614 | type => $jwrite_type |
|---|
| 615 | }; |
|---|
| 616 | |
|---|
| 617 | if (defined($jwrite_body)) { |
|---|
| 618 | process_owl_jwrite($jwrite_body); |
|---|
| 619 | return; |
|---|
| 620 | } |
|---|
| 621 | |
|---|
| 622 | if(scalar @candidates > 1) { |
|---|
| 623 | BarnOwl::message( |
|---|
| 624 | "Warning: Guessing account and/or destination JID" |
|---|
| 625 | ); |
|---|
| 626 | } else { |
|---|
| 627 | BarnOwl::message( |
|---|
| 628 | "Type your message below. End with a dot on a line by itself. ^C will quit." |
|---|
| 629 | ); |
|---|
| 630 | } |
|---|
| 631 | |
|---|
| 632 | my @cmd = ('jwrite', $jwrite_to, '-a', $jwrite_from); |
|---|
| 633 | push @cmd, '-t', $jwrite_thread if $jwrite_thread; |
|---|
| 634 | push @cmd, '-s', $jwrite_subject if $jwrite_subject; |
|---|
| 635 | |
|---|
| 636 | BarnOwl::start_edit_win(BarnOwl::quote(@cmd), \&process_owl_jwrite); |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | sub cmd_jmuc { |
|---|
| 640 | die "You are not logged in to Jabber" unless $conn->connected(); |
|---|
| 641 | my $ocmd = shift; |
|---|
| 642 | my $cmd = shift; |
|---|
| 643 | if ( !$cmd ) { |
|---|
| 644 | |
|---|
| 645 | #XXX TODO: Write general usage for jmuc command. |
|---|
| 646 | return; |
|---|
| 647 | } |
|---|
| 648 | |
|---|
| 649 | my %jmuc_commands = ( |
|---|
| 650 | join => \&jmuc_join, |
|---|
| 651 | part => \&jmuc_part, |
|---|
| 652 | invite => \&jmuc_invite, |
|---|
| 653 | configure => \&jmuc_configure, |
|---|
| 654 | presence => \&jmuc_presence |
|---|
| 655 | ); |
|---|
| 656 | my $func = $jmuc_commands{$cmd}; |
|---|
| 657 | if ( !$func ) { |
|---|
| 658 | die("jmuc: Unknown command: $cmd\n"); |
|---|
| 659 | } |
|---|
| 660 | |
|---|
| 661 | { |
|---|
| 662 | local @ARGV = @_; |
|---|
| 663 | my $jid; |
|---|
| 664 | my $muc; |
|---|
| 665 | my $m = BarnOwl::getcurmsg(); |
|---|
| 666 | if ( $m && $m->is_jabber && $m->{jtype} eq 'groupchat' ) { |
|---|
| 667 | $muc = $m->{room}; |
|---|
| 668 | $jid = $m->{to}; |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | my $getopt = Getopt::Long::Parser->new; |
|---|
| 672 | $getopt->configure('pass_through', 'no_getopt_compat'); |
|---|
| 673 | $getopt->getoptions( 'account=s' => \$jid ); |
|---|
| 674 | $jid ||= defaultJID(); |
|---|
| 675 | if ($jid) { |
|---|
| 676 | $jid = resolveConnectedJID($jid); |
|---|
| 677 | return unless $jid; |
|---|
| 678 | } |
|---|
| 679 | else { |
|---|
| 680 | die("You must specify an account with -a <jid>\n"); |
|---|
| 681 | } |
|---|
| 682 | return $func->( $jid, $muc, @ARGV ); |
|---|
| 683 | } |
|---|
| 684 | } |
|---|
| 685 | |
|---|
| 686 | sub jmuc_join { |
|---|
| 687 | my ( $jid, $muc, @args ) = @_; |
|---|
| 688 | local @ARGV = @args; |
|---|
| 689 | my $password; |
|---|
| 690 | GetOptions( 'password=s' => \$password ); |
|---|
| 691 | |
|---|
| 692 | $muc = shift @ARGV |
|---|
| 693 | or die("Usage: jmuc join <muc> [-p <password>] [-a <account>]\n"); |
|---|
| 694 | |
|---|
| 695 | die("Error: Must specify a fully-qualified MUC name (e.g. barnowl\@conference.mit.edu)\n") |
|---|
| 696 | unless $muc =~ /@/; |
|---|
| 697 | $muc = Net::Jabber::JID->new($muc); |
|---|
| 698 | $jid = Net::Jabber::JID->new($jid); |
|---|
| 699 | $muc->SetResource($jid->GetJID('full')) unless length $muc->GetResource(); |
|---|
| 700 | |
|---|
| 701 | $conn->getConnectionFromJID($jid)->MUCJoin(JID => $muc, |
|---|
| 702 | Password => $password, |
|---|
| 703 | History => { |
|---|
| 704 | MaxChars => 0 |
|---|
| 705 | }); |
|---|
| 706 | $completion_jids{$muc->GetJID('base')} = 1; |
|---|
| 707 | return; |
|---|
| 708 | } |
|---|
| 709 | |
|---|
| 710 | sub jmuc_part { |
|---|
| 711 | my ( $jid, $muc, @args ) = @_; |
|---|
| 712 | |
|---|
| 713 | $muc = shift @args if scalar @args; |
|---|
| 714 | die("Usage: jmuc part [<muc>] [-a <account>]\n") unless $muc; |
|---|
| 715 | |
|---|
| 716 | if($conn->getConnectionFromJID($jid)->MUCLeave(JID => $muc)) { |
|---|
| 717 | queue_admin_msg("$jid has left $muc."); |
|---|
| 718 | } else { |
|---|
| 719 | die("Error: Not joined to $muc\n"); |
|---|
| 720 | } |
|---|
| 721 | } |
|---|
| 722 | |
|---|
| 723 | sub jmuc_invite { |
|---|
| 724 | my ( $jid, $muc, @args ) = @_; |
|---|
| 725 | |
|---|
| 726 | my $invite_jid = shift @args; |
|---|
| 727 | $muc = shift @args if scalar @args; |
|---|
| 728 | |
|---|
| 729 | die("Usage: jmuc invite <jid> [<muc>] [-a <account>]\n") |
|---|
| 730 | unless $muc && $invite_jid; |
|---|
| 731 | |
|---|
| 732 | my $message = Net::Jabber::Message->new(); |
|---|
| 733 | $message->SetTo($muc); |
|---|
| 734 | my $x = $message->NewChild('http://jabber.org/protocol/muc#user'); |
|---|
| 735 | $x->AddInvite(); |
|---|
| 736 | $x->GetInvite()->SetTo($invite_jid); |
|---|
| 737 | $conn->getConnectionFromJID($jid)->Send($message); |
|---|
| 738 | queue_admin_msg("$jid has invited $invite_jid to $muc."); |
|---|
| 739 | } |
|---|
| 740 | |
|---|
| 741 | sub jmuc_configure { |
|---|
| 742 | my ( $jid, $muc, @args ) = @_; |
|---|
| 743 | $muc = shift @args if scalar @args; |
|---|
| 744 | die("Usage: jmuc configure [<muc>]\n") unless $muc; |
|---|
| 745 | my $iq = Net::Jabber::IQ->new(); |
|---|
| 746 | $iq->SetTo($muc); |
|---|
| 747 | $iq->SetType('set'); |
|---|
| 748 | my $query = $iq->NewQuery("http://jabber.org/protocol/muc#owner"); |
|---|
| 749 | my $x = $query->NewChild("jabber:x:data"); |
|---|
| 750 | $x->SetType('submit'); |
|---|
| 751 | |
|---|
| 752 | $conn->getConnectionFromJID($jid)->Send($iq); |
|---|
| 753 | queue_admin_msg("Accepted default instant configuration for $muc"); |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | sub jmuc_presence_single { |
|---|
| 757 | my $m = shift; |
|---|
| 758 | my @jids = $m->Presence(); |
|---|
| 759 | |
|---|
| 760 | my $presence = "JIDs present in " . $m->BaseJID; |
|---|
| 761 | $completion_jids{$m->BaseJID} = 1; |
|---|
| 762 | if($m->Anonymous) { |
|---|
| 763 | $presence .= " [anonymous MUC]"; |
|---|
| 764 | } |
|---|
| 765 | $presence .= "\n\t"; |
|---|
| 766 | $presence .= join("\n\t", map {pp_jid($m, $_);} @jids) . "\n"; |
|---|
| 767 | return $presence; |
|---|
| 768 | } |
|---|
| 769 | |
|---|
| 770 | sub pp_jid { |
|---|
| 771 | my ($m, $jid) = @_; |
|---|
| 772 | my $nick = $jid->GetResource; |
|---|
| 773 | my $full = $m->GetFullJID($jid); |
|---|
| 774 | if($full && $full ne $nick) { |
|---|
| 775 | return "$nick ($full)"; |
|---|
| 776 | } else { |
|---|
| 777 | return "$nick"; |
|---|
| 778 | } |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | sub jmuc_presence { |
|---|
| 782 | my ( $jid, $muc, @args ) = @_; |
|---|
| 783 | |
|---|
| 784 | $muc = shift @args if scalar @args; |
|---|
| 785 | die("Usage: jmuc presence [<muc>]\n") unless $muc; |
|---|
| 786 | |
|---|
| 787 | if ($muc eq '-a') { |
|---|
| 788 | my $str = ""; |
|---|
| 789 | foreach my $jid ($conn->getJIDs()) { |
|---|
| 790 | $str .= BarnOwl::Style::boldify("Conferences for $jid:\n"); |
|---|
| 791 | my $connection = $conn->getConnectionFromJID($jid); |
|---|
| 792 | foreach my $muc ($connection->MUCs) { |
|---|
| 793 | $str .= jmuc_presence_single($muc)."\n"; |
|---|
| 794 | } |
|---|
| 795 | } |
|---|
| 796 | BarnOwl::popless_ztext($str); |
|---|
| 797 | } |
|---|
| 798 | else { |
|---|
| 799 | my $m = $conn->getConnectionFromJID($jid)->FindMUC(jid => $muc); |
|---|
| 800 | die("No such muc: $muc\n") unless $m; |
|---|
| 801 | BarnOwl::popless_ztext(jmuc_presence_single($m)); |
|---|
| 802 | } |
|---|
| 803 | } |
|---|
| 804 | |
|---|
| 805 | |
|---|
| 806 | #XXX TODO: Consider merging this with jmuc and selecting off the first two args. |
|---|
| 807 | sub cmd_jroster { |
|---|
| 808 | die "You are not logged in to Jabber" unless $conn->connected(); |
|---|
| 809 | my $ocmd = shift; |
|---|
| 810 | my $cmd = shift; |
|---|
| 811 | if ( !$cmd ) { |
|---|
| 812 | |
|---|
| 813 | #XXX TODO: Write general usage for jroster command. |
|---|
| 814 | return; |
|---|
| 815 | } |
|---|
| 816 | |
|---|
| 817 | my %jroster_commands = ( |
|---|
| 818 | sub => \&jroster_sub, |
|---|
| 819 | unsub => \&jroster_unsub, |
|---|
| 820 | add => \&jroster_add, |
|---|
| 821 | remove => \&jroster_remove, |
|---|
| 822 | auth => \&jroster_auth, |
|---|
| 823 | deauth => \&jroster_deauth |
|---|
| 824 | ); |
|---|
| 825 | my $func = $jroster_commands{$cmd}; |
|---|
| 826 | if ( !$func ) { |
|---|
| 827 | die("jroster: Unknown command: $cmd\n"); |
|---|
| 828 | } |
|---|
| 829 | |
|---|
| 830 | { |
|---|
| 831 | local @ARGV = @_; |
|---|
| 832 | my $jid; |
|---|
| 833 | my $name; |
|---|
| 834 | my @groups; |
|---|
| 835 | my $purgeGroups; |
|---|
| 836 | my $getopt = Getopt::Long::Parser->new; |
|---|
| 837 | $getopt->configure('pass_through', 'no_getopt_compat'); |
|---|
| 838 | $getopt->getoptions( |
|---|
| 839 | 'account=s' => \$jid, |
|---|
| 840 | 'group=s' => \@groups, |
|---|
| 841 | 'purgegroups' => \$purgeGroups, |
|---|
| 842 | 'name=s' => \$name |
|---|
| 843 | ); |
|---|
| 844 | $jid ||= defaultJID(); |
|---|
| 845 | if ($jid) { |
|---|
| 846 | $jid = resolveConnectedJID($jid); |
|---|
| 847 | return unless $jid; |
|---|
| 848 | } |
|---|
| 849 | else { |
|---|
| 850 | die("You must specify an account with -a <jid>\n"); |
|---|
| 851 | } |
|---|
| 852 | return $func->( $jid, $name, \@groups, $purgeGroups, @ARGV ); |
|---|
| 853 | } |
|---|
| 854 | } |
|---|
| 855 | |
|---|
| 856 | sub cmd_jaway { |
|---|
| 857 | my $cmd = shift; |
|---|
| 858 | local @ARGV = @_; |
|---|
| 859 | my $getopt = Getopt::Long::Parser->new; |
|---|
| 860 | my ($jid, $show); |
|---|
| 861 | my $p = new Net::Jabber::Presence; |
|---|
| 862 | |
|---|
| 863 | $getopt->configure('pass_through', 'no_getopt_compat'); |
|---|
| 864 | $getopt->getoptions( |
|---|
| 865 | 'account=s' => \$jid, |
|---|
| 866 | 'show=s' => \$show |
|---|
| 867 | ); |
|---|
| 868 | $jid ||= defaultJID(); |
|---|
| 869 | if ($jid) { |
|---|
| 870 | $jid = resolveConnectedJID($jid); |
|---|
| 871 | return unless $jid; |
|---|
| 872 | } |
|---|
| 873 | else { |
|---|
| 874 | die("You must specify an account with -a <jid>\n"); |
|---|
| 875 | } |
|---|
| 876 | |
|---|
| 877 | $p->SetShow($show eq "online" ? "" : $show) if $show; |
|---|
| 878 | $p->SetStatus(join(' ', @ARGV)) if @ARGV; |
|---|
| 879 | $conn->getConnectionFromJID($jid)->Send($p); |
|---|
| 880 | } |
|---|
| 881 | |
|---|
| 882 | sub cmd_jabber_get_buddy_status { |
|---|
| 883 | my $cmd = shift; |
|---|
| 884 | local @ARGV = @_; |
|---|
| 885 | my $getopt = Getopt::Long::Parser->new; |
|---|
| 886 | my ($jid, $buddy); |
|---|
| 887 | |
|---|
| 888 | $getopt->configure('no_getopt_compat'); |
|---|
| 889 | $getopt->getoptions( |
|---|
| 890 | 'account=s' => \$jid, |
|---|
| 891 | ); |
|---|
| 892 | $jid ||= defaultJID(); |
|---|
| 893 | if ($jid) { |
|---|
| 894 | $jid = resolveConnectedJID($jid); |
|---|
| 895 | return unless $jid; |
|---|
| 896 | } |
|---|
| 897 | else { |
|---|
| 898 | die("You must specify an account with -a <jid>\n"); |
|---|
| 899 | } |
|---|
| 900 | $buddy = shift @ARGV; |
|---|
| 901 | if(!$buddy) { |
|---|
| 902 | die("You must specify a JID to get the status of.\n"); |
|---|
| 903 | } |
|---|
| 904 | my $buddy_jid = resolveDestJID($buddy, $jid); |
|---|
| 905 | return blist_getBuddyStatus($jid, $buddy_jid); |
|---|
| 906 | } |
|---|
| 907 | |
|---|
| 908 | |
|---|
| 909 | sub jroster_sub { |
|---|
| 910 | my $jid = shift; |
|---|
| 911 | my $name = shift; |
|---|
| 912 | my @groups = @{ shift() }; |
|---|
| 913 | my $purgeGroups = shift; |
|---|
| 914 | my $baseJID = baseJID($jid); |
|---|
| 915 | |
|---|
| 916 | my $roster = $conn->getRosterFromJID($jid); |
|---|
| 917 | |
|---|
| 918 | # Adding lots of users with the same name is a bad idea. |
|---|
| 919 | $name = "" unless (1 == scalar(@ARGV)); |
|---|
| 920 | |
|---|
| 921 | my $p = new Net::Jabber::Presence; |
|---|
| 922 | $p->SetType('subscribe'); |
|---|
| 923 | |
|---|
| 924 | foreach my $to (@ARGV) { |
|---|
| 925 | jroster_add($jid, $name, \@groups, $purgeGroups, ($to)) unless ($roster->exists($to)); |
|---|
| 926 | |
|---|
| 927 | $p->SetTo($to); |
|---|
| 928 | $conn->getConnectionFromJID($jid)->Send($p); |
|---|
| 929 | queue_admin_msg("You ($baseJID) have requested a subscription to ($to)'s presence."); |
|---|
| 930 | } |
|---|
| 931 | } |
|---|
| 932 | |
|---|
| 933 | sub jroster_unsub { |
|---|
| 934 | my $jid = shift; |
|---|
| 935 | my $name = shift; |
|---|
| 936 | my @groups = @{ shift() }; |
|---|
| 937 | my $purgeGroups = shift; |
|---|
| 938 | my $baseJID = baseJID($jid); |
|---|
| 939 | |
|---|
| 940 | my $p = new Net::Jabber::Presence; |
|---|
| 941 | $p->SetType('unsubscribe'); |
|---|
| 942 | foreach my $to (@ARGV) { |
|---|
| 943 | $p->SetTo($to); |
|---|
| 944 | $conn->getConnectionFromJID($jid)->Send($p); |
|---|
| 945 | queue_admin_msg("You ($baseJID) have unsubscribed from ($to)'s presence."); |
|---|
| 946 | } |
|---|
| 947 | } |
|---|
| 948 | |
|---|
| 949 | sub jroster_add { |
|---|
| 950 | my $jid = shift; |
|---|
| 951 | my $name = shift; |
|---|
| 952 | my @groups = @{ shift() }; |
|---|
| 953 | my $purgeGroups = shift; |
|---|
| 954 | my $baseJID = baseJID($jid); |
|---|
| 955 | |
|---|
| 956 | my $roster = $conn->getRosterFromJID($jid); |
|---|
| 957 | |
|---|
| 958 | # Adding lots of users with the same name is a bad idea. |
|---|
| 959 | $name = "" unless (1 == scalar(@ARGV)); |
|---|
| 960 | |
|---|
| 961 | $completion_jids{$baseJID} = 1; |
|---|
| 962 | $completion_jids{$name} = 1 if $name; |
|---|
| 963 | |
|---|
| 964 | foreach my $to (@ARGV) { |
|---|
| 965 | my %jq = $roster->query($to); |
|---|
| 966 | my $iq = new Net::Jabber::IQ; |
|---|
| 967 | $iq->SetType('set'); |
|---|
| 968 | my $item = new XML::Stream::Node('item'); |
|---|
| 969 | $iq->NewChild('jabber:iq:roster')->AddChild($item); |
|---|
| 970 | |
|---|
| 971 | my %allGroups = (); |
|---|
| 972 | |
|---|
| 973 | foreach my $g (@groups) { |
|---|
| 974 | $allGroups{$g} = $g; |
|---|
| 975 | } |
|---|
| 976 | |
|---|
| 977 | unless ($purgeGroups) { |
|---|
| 978 | foreach my $g (@{$jq{groups}}) { |
|---|
| 979 | $allGroups{$g} = $g; |
|---|
| 980 | } |
|---|
| 981 | } |
|---|
| 982 | |
|---|
| 983 | foreach my $g (keys %allGroups) { |
|---|
| 984 | $item->add_child('group')->add_cdata($g); |
|---|
| 985 | } |
|---|
| 986 | |
|---|
| 987 | $item->put_attrib(jid => $to); |
|---|
| 988 | $item->put_attrib(name => $name) if $name; |
|---|
| 989 | $conn->getConnectionFromJID($jid)->Send($iq); |
|---|
| 990 | my $msg = "$baseJID: " |
|---|
| 991 | . ($name ? "$name ($to)" : "($to)") |
|---|
| 992 | . " is on your roster in the following groups: { " |
|---|
| 993 | . join(" , ", keys %allGroups) |
|---|
| 994 | . " }"; |
|---|
| 995 | queue_admin_msg($msg); |
|---|
| 996 | } |
|---|
| 997 | } |
|---|
| 998 | |
|---|
| 999 | sub jroster_remove { |
|---|
| 1000 | my $jid = shift; |
|---|
| 1001 | my $name = shift; |
|---|
| 1002 | my @groups = @{ shift() }; |
|---|
| 1003 | my $purgeGroups = shift; |
|---|
| 1004 | my $baseJID = baseJID($jid); |
|---|
| 1005 | |
|---|
| 1006 | my $iq = new Net::Jabber::IQ; |
|---|
| 1007 | $iq->SetType('set'); |
|---|
| 1008 | my $item = new XML::Stream::Node('item'); |
|---|
| 1009 | $iq->NewChild('jabber:iq:roster')->AddChild($item); |
|---|
| 1010 | $item->put_attrib(subscription=> 'remove'); |
|---|
| 1011 | foreach my $to (@ARGV) { |
|---|
| 1012 | $item->put_attrib(jid => $to); |
|---|
| 1013 | $conn->getConnectionFromJID($jid)->Send($iq); |
|---|
| 1014 | queue_admin_msg("You ($baseJID) have removed ($to) from your roster."); |
|---|
| 1015 | } |
|---|
| 1016 | } |
|---|
| 1017 | |
|---|
| 1018 | sub jroster_auth { |
|---|
| 1019 | my $jid = shift; |
|---|
| 1020 | my $name = shift; |
|---|
| 1021 | my @groups = @{ shift() }; |
|---|
| 1022 | my $purgeGroups = shift; |
|---|
| 1023 | my $baseJID = baseJID($jid); |
|---|
| 1024 | |
|---|
| 1025 | my $p = new Net::Jabber::Presence; |
|---|
| 1026 | $p->SetType('subscribed'); |
|---|
| 1027 | foreach my $to (@ARGV) { |
|---|
| 1028 | $p->SetTo($to); |
|---|
| 1029 | $conn->getConnectionFromJID($jid)->Send($p); |
|---|
| 1030 | queue_admin_msg("($to) has been subscribed to your ($baseJID) presence."); |
|---|
| 1031 | } |
|---|
| 1032 | } |
|---|
| 1033 | |
|---|
| 1034 | sub jroster_deauth { |
|---|
| 1035 | my $jid = shift; |
|---|
| 1036 | my $name = shift; |
|---|
| 1037 | my @groups = @{ shift() }; |
|---|
| 1038 | my $purgeGroups = shift; |
|---|
| 1039 | my $baseJID = baseJID($jid); |
|---|
| 1040 | |
|---|
| 1041 | my $p = new Net::Jabber::Presence; |
|---|
| 1042 | $p->SetType('unsubscribed'); |
|---|
| 1043 | foreach my $to (@ARGV) { |
|---|
| 1044 | $p->SetTo($to); |
|---|
| 1045 | $conn->getConnectionFromJID($jid)->Send($p); |
|---|
| 1046 | queue_admin_msg("($to) has been unsubscribed from your ($baseJID) presence."); |
|---|
| 1047 | } |
|---|
| 1048 | } |
|---|
| 1049 | |
|---|
| 1050 | ################################################################################ |
|---|
| 1051 | ### Owl Callbacks |
|---|
| 1052 | sub process_owl_jwrite { |
|---|
| 1053 | my $body = shift; |
|---|
| 1054 | |
|---|
| 1055 | my $j = new Net::Jabber::Message; |
|---|
| 1056 | $body =~ s/\n\z//; |
|---|
| 1057 | $j->SetMessage( |
|---|
| 1058 | to => $vars{jwrite}{to}, |
|---|
| 1059 | from => $vars{jwrite}{from}, |
|---|
| 1060 | type => $vars{jwrite}{type}, |
|---|
| 1061 | body => $body |
|---|
| 1062 | ); |
|---|
| 1063 | |
|---|
| 1064 | $j->SetThread( $vars{jwrite}{thread} ) if ( $vars{jwrite}{thread} ); |
|---|
| 1065 | $j->SetSubject( $vars{jwrite}{subject} ) if ( $vars{jwrite}{subject} ); |
|---|
| 1066 | |
|---|
| 1067 | my $m = j2o( $j, { direction => 'out' } ); |
|---|
| 1068 | if ( $vars{jwrite}{type} ne 'groupchat') { |
|---|
| 1069 | BarnOwl::queue_message($m); |
|---|
| 1070 | } |
|---|
| 1071 | |
|---|
| 1072 | $j->RemoveFrom(); # Kludge to get around gtalk's random bits after the resource. |
|---|
| 1073 | if ($vars{jwrite}{sid} && $conn->sidExists( $vars{jwrite}{sid} )) { |
|---|
| 1074 | $conn->getConnectionFromSid($vars{jwrite}{sid})->Send($j); |
|---|
| 1075 | } |
|---|
| 1076 | else { |
|---|
| 1077 | $conn->getConnectionFromJID($vars{jwrite}{from})->Send($j); |
|---|
| 1078 | } |
|---|
| 1079 | |
|---|
| 1080 | delete $vars{jwrite}; |
|---|
| 1081 | BarnOwl::message(""); # Kludge to make the ``type your message...'' message go away |
|---|
| 1082 | } |
|---|
| 1083 | |
|---|
| 1084 | ### XMPP Callbacks |
|---|
| 1085 | |
|---|
| 1086 | sub process_incoming_chat_message { |
|---|
| 1087 | my ( $sid, $j ) = @_; |
|---|
| 1088 | if ($j->DefinedBody() || BarnOwl::getvar('jabber:spew') eq 'on') { |
|---|
| 1089 | BarnOwl::queue_message( j2o( $j, { direction => 'in', |
|---|
| 1090 | sid => $sid } ) ); |
|---|
| 1091 | } |
|---|
| 1092 | } |
|---|
| 1093 | |
|---|
| 1094 | sub process_incoming_error_message { |
|---|
| 1095 | my ( $sid, $j ) = @_; |
|---|
| 1096 | my %jhash = j2hash( $j, { direction => 'in', |
|---|
| 1097 | sid => $sid } ); |
|---|
| 1098 | $jhash{type} = 'admin'; |
|---|
| 1099 | |
|---|
| 1100 | BarnOwl::queue_message( BarnOwl::Message->new(%jhash) ); |
|---|
| 1101 | } |
|---|
| 1102 | |
|---|
| 1103 | sub process_incoming_groupchat_message { |
|---|
| 1104 | my ( $sid, $j ) = @_; |
|---|
| 1105 | |
|---|
| 1106 | # HACK IN PROGRESS (ignoring delayed messages) |
|---|
| 1107 | return if ( $j->DefinedX('jabber:x:delay') && $j->GetX('jabber:x:delay') ); |
|---|
| 1108 | BarnOwl::queue_message( j2o( $j, { direction => 'in', |
|---|
| 1109 | sid => $sid } ) ); |
|---|
| 1110 | } |
|---|
| 1111 | |
|---|
| 1112 | sub process_incoming_headline_message { |
|---|
| 1113 | my ( $sid, $j ) = @_; |
|---|
| 1114 | BarnOwl::queue_message( j2o( $j, { direction => 'in', |
|---|
| 1115 | sid => $sid } ) ); |
|---|
| 1116 | } |
|---|
| 1117 | |
|---|
| 1118 | sub process_incoming_normal_message { |
|---|
| 1119 | my ( $sid, $j ) = @_; |
|---|
| 1120 | my %jhash = j2hash( $j, { direction => 'in', |
|---|
| 1121 | sid => $sid } ); |
|---|
| 1122 | |
|---|
| 1123 | # XXX TODO: handle things such as MUC invites here. |
|---|
| 1124 | |
|---|
| 1125 | # if ($j->HasX('http://jabber.org/protocol/muc#user')) |
|---|
| 1126 | # { |
|---|
| 1127 | # my $x = $j->GetX('http://jabber.org/protocol/muc#user'); |
|---|
| 1128 | # if ($x->HasChild('invite')) |
|---|
| 1129 | # { |
|---|
| 1130 | # $props |
|---|
| 1131 | # } |
|---|
| 1132 | # } |
|---|
| 1133 | # |
|---|
| 1134 | if ($j->DefinedBody() || BarnOwl::getvar('jabber:spew') eq 'on') { |
|---|
| 1135 | BarnOwl::queue_message( BarnOwl::Message->new(%jhash) ); |
|---|
| 1136 | } |
|---|
| 1137 | } |
|---|
| 1138 | |
|---|
| 1139 | sub process_muc_presence { |
|---|
| 1140 | my ( $sid, $p ) = @_; |
|---|
| 1141 | return unless ( $p->HasX('http://jabber.org/protocol/muc#user') ); |
|---|
| 1142 | } |
|---|
| 1143 | |
|---|
| 1144 | |
|---|
| 1145 | sub process_presence_available { |
|---|
| 1146 | my ( $sid, $p ) = @_; |
|---|
| 1147 | my $from = $p->GetFrom('jid')->GetJID('base'); |
|---|
| 1148 | $completion_jids{$from} = 1; |
|---|
| 1149 | return unless (BarnOwl::getvar('jabber:show_logins') eq 'on'); |
|---|
| 1150 | my $to = $p->GetTo(); |
|---|
| 1151 | my $type = $p->GetType(); |
|---|
| 1152 | my %props = ( |
|---|
| 1153 | to => $to, |
|---|
| 1154 | from => $p->GetFrom(), |
|---|
| 1155 | recipient => $to, |
|---|
| 1156 | sender => $from, |
|---|
| 1157 | type => 'jabber', |
|---|
| 1158 | jtype => $p->GetType(), |
|---|
| 1159 | status => $p->GetStatus(), |
|---|
| 1160 | show => $p->GetShow(), |
|---|
| 1161 | xml => $p->GetXML(), |
|---|
| 1162 | direction => 'in'); |
|---|
| 1163 | |
|---|
| 1164 | if ($type eq '' || $type eq 'available') { |
|---|
| 1165 | $props{body} = "$from is now online. "; |
|---|
| 1166 | $props{loginout} = 'login'; |
|---|
| 1167 | } |
|---|
| 1168 | else { |
|---|
| 1169 | $props{body} = "$from is now offline. "; |
|---|
| 1170 | $props{loginout} = 'logout'; |
|---|
| 1171 | } |
|---|
| 1172 | BarnOwl::queue_message(BarnOwl::Message->new(%props)); |
|---|
| 1173 | } |
|---|
| 1174 | |
|---|
| 1175 | sub process_presence_subscribe { |
|---|
| 1176 | my ( $sid, $p ) = @_; |
|---|
| 1177 | my $from = $p->GetFrom(); |
|---|
| 1178 | my $to = $p->GetTo(); |
|---|
| 1179 | my %props = ( |
|---|
| 1180 | to => $to, |
|---|
| 1181 | from => $from, |
|---|
| 1182 | xml => $p->GetXML(), |
|---|
| 1183 | type => 'admin', |
|---|
| 1184 | adminheader => 'Jabber presence: subscribe', |
|---|
| 1185 | direction => 'in'); |
|---|
| 1186 | |
|---|
| 1187 | $props{body} = "Allow user ($from) to subscribe to your ($to) presence?\n" . |
|---|
| 1188 | "(Answer with the `yes' or `no' commands)"; |
|---|
| 1189 | $props{yescommand} = BarnOwl::quote('jroster', 'auth', $from, '-a', $to); |
|---|
| 1190 | $props{nocommand} = BarnOwl::quote('jroster', 'deauth', $from, '-a', $to); |
|---|
| 1191 | $props{question} = "true"; |
|---|
| 1192 | BarnOwl::queue_message(BarnOwl::Message->new(%props)); |
|---|
| 1193 | } |
|---|
| 1194 | |
|---|
| 1195 | sub process_presence_unsubscribe { |
|---|
| 1196 | my ( $sid, $p ) = @_; |
|---|
| 1197 | my $from = $p->GetFrom(); |
|---|
| 1198 | my $to = $p->GetTo(); |
|---|
| 1199 | my %props = ( |
|---|
| 1200 | to => $to, |
|---|
| 1201 | from => $from, |
|---|
| 1202 | xml => $p->GetXML(), |
|---|
| 1203 | type => 'admin', |
|---|
| 1204 | adminheader => 'Jabber presence: unsubscribe', |
|---|
| 1205 | direction => 'in'); |
|---|
| 1206 | |
|---|
| 1207 | $props{body} = "The user ($from) has been unsubscribed from your ($to) presence.\n"; |
|---|
| 1208 | BarnOwl::queue_message(BarnOwl::Message->new(%props)); |
|---|
| 1209 | |
|---|
| 1210 | # Find a connection to reply with. |
|---|
| 1211 | foreach my $jid ($conn->getJIDs()) { |
|---|
| 1212 | my $cJID = new Net::Jabber::JID; |
|---|
| 1213 | $cJID->SetJID($jid); |
|---|
| 1214 | if ($to eq $cJID->GetJID('base') || |
|---|
| 1215 | $to eq $cJID->GetJID('full')) { |
|---|
| 1216 | my $reply = $p->Reply(type=>"unsubscribed"); |
|---|
| 1217 | $conn->getConnectionFromJID($jid)->Send($reply); |
|---|
| 1218 | return; |
|---|
| 1219 | } |
|---|
| 1220 | } |
|---|
| 1221 | } |
|---|
| 1222 | |
|---|
| 1223 | sub process_presence_subscribed { |
|---|
| 1224 | my ( $sid, $p ) = @_; |
|---|
| 1225 | queue_admin_msg("ignoring:".$p->GetXML()) if BarnOwl::getvar('jabber:spew') eq 'on'; |
|---|
| 1226 | # RFC 3921 says we should respond to this with a "subscribe" |
|---|
| 1227 | # but this causes a flood of sub/sub'd presence packets with |
|---|
| 1228 | # some servers, so we won't. We may want to detect this condition |
|---|
| 1229 | # later, and have per-server settings. |
|---|
| 1230 | return; |
|---|
| 1231 | } |
|---|
| 1232 | |
|---|
| 1233 | sub process_presence_unsubscribed { |
|---|
| 1234 | my ( $sid, $p ) = @_; |
|---|
| 1235 | queue_admin_msg("ignoring:".$p->GetXML()) if BarnOwl::getvar('jabber:spew') eq 'on'; |
|---|
| 1236 | # RFC 3921 says we should respond to this with a "subscribe" |
|---|
| 1237 | # but this causes a flood of unsub/unsub'd presence packets with |
|---|
| 1238 | # some servers, so we won't. We may want to detect this condition |
|---|
| 1239 | # later, and have per-server settings. |
|---|
| 1240 | return; |
|---|
| 1241 | } |
|---|
| 1242 | |
|---|
| 1243 | sub process_presence_error { |
|---|
| 1244 | my ( $sid, $p ) = @_; |
|---|
| 1245 | my $code = $p->GetErrorCode(); |
|---|
| 1246 | my $error = $p->GetError(); |
|---|
| 1247 | BarnOwl::error("Jabber: $code $error"); |
|---|
| 1248 | } |
|---|
| 1249 | |
|---|
| 1250 | |
|---|
| 1251 | ### Helper functions |
|---|
| 1252 | |
|---|
| 1253 | sub j2hash { |
|---|
| 1254 | my $j = shift; |
|---|
| 1255 | my %props = (type => 'jabber', |
|---|
| 1256 | dir => 'none', |
|---|
| 1257 | %{$_[0]}); |
|---|
| 1258 | |
|---|
| 1259 | my $dir = $props{direction}; |
|---|
| 1260 | |
|---|
| 1261 | my $jtype = $props{jtype} = $j->GetType(); |
|---|
| 1262 | my $from = $j->GetFrom('jid'); |
|---|
| 1263 | my $to = $j->GetTo('jid'); |
|---|
| 1264 | |
|---|
| 1265 | $props{from} = $from->GetJID('full'); |
|---|
| 1266 | $props{to} = $to->GetJID('full'); |
|---|
| 1267 | |
|---|
| 1268 | $props{recipient} = $to->GetJID('base'); |
|---|
| 1269 | $props{sender} = $from->GetJID('base'); |
|---|
| 1270 | $props{subject} = $j->GetSubject() if ( $j->DefinedSubject() ); |
|---|
| 1271 | $props{thread} = $j->GetThread() if ( $j->DefinedThread() ); |
|---|
| 1272 | if ( $j->DefinedBody() ) { |
|---|
| 1273 | $props{body} = $j->GetBody(); |
|---|
| 1274 | $props{body} =~ s/\xEF\xBB\xBF//g; # Strip stray Byte-Order-Marks. |
|---|
| 1275 | } |
|---|
| 1276 | $props{error} = $j->GetError() if ( $j->DefinedError() ); |
|---|
| 1277 | $props{error_code} = $j->GetErrorCode() if ( $j->DefinedErrorCode() ); |
|---|
| 1278 | $props{xml} = $j->GetXML(); |
|---|
| 1279 | |
|---|
| 1280 | if ( $jtype eq 'groupchat' ) { |
|---|
| 1281 | my $nick = $props{nick} = $from->GetResource(); |
|---|
| 1282 | my $room = $props{room} = $from->GetJID('base'); |
|---|
| 1283 | $completion_jids{$room} = 1; |
|---|
| 1284 | |
|---|
| 1285 | my $muc; |
|---|
| 1286 | if ($dir eq 'in') { |
|---|
| 1287 | my $connection = $conn->getConnectionFromSid($props{sid}); |
|---|
| 1288 | $muc = $connection->FindMUC(jid => $from); |
|---|
| 1289 | } else { |
|---|
| 1290 | my $connection = $conn->getConnectionFromJID($props{from}); |
|---|
| 1291 | $muc = $connection->FindMUC(jid => $to); |
|---|
| 1292 | } |
|---|
| 1293 | $props{from} = $muc->GetFullJID($from) || $props{from}; |
|---|
| 1294 | $props{sender} = $nick || $room; |
|---|
| 1295 | $props{recipient} = $room; |
|---|
| 1296 | |
|---|
| 1297 | if ( $props{subject} && !$props{body} ) { |
|---|
| 1298 | $props{body} = |
|---|
| 1299 | '[' . $nick . " has set the topic to: " . $props{subject} . "]"; |
|---|
| 1300 | } |
|---|
| 1301 | } |
|---|
| 1302 | elsif ( $jtype eq 'headline' ) { |
|---|
| 1303 | ; |
|---|
| 1304 | } |
|---|
| 1305 | elsif ( $jtype eq 'error' ) { |
|---|
| 1306 | $props{body} = "Error " |
|---|
| 1307 | . $props{error_code} |
|---|
| 1308 | . " sending to " |
|---|
| 1309 | . $props{from} . "\n" |
|---|
| 1310 | . $props{error}; |
|---|
| 1311 | } |
|---|
| 1312 | else { # chat, or normal (default) |
|---|
| 1313 | $props{private} = 1; |
|---|
| 1314 | |
|---|
| 1315 | my $connection; |
|---|
| 1316 | if ($dir eq 'in') { |
|---|
| 1317 | $connection = $conn->getConnectionFromSid($props{sid}); |
|---|
| 1318 | } |
|---|
| 1319 | else { |
|---|
| 1320 | $connection = $conn->getConnectionFromJID($props{from}); |
|---|
| 1321 | } |
|---|
| 1322 | |
|---|
| 1323 | # Check to see if we're doing personals with someone in a muc. |
|---|
| 1324 | # If we are, show the full jid because the base jid is the room. |
|---|
| 1325 | if ($connection) { |
|---|
| 1326 | $props{sender} = $props{from} |
|---|
| 1327 | if ($connection->FindMUC(jid => $from)); |
|---|
| 1328 | $props{recipient} = $props{to} |
|---|
| 1329 | if ($connection->FindMUC(jid => $to)); |
|---|
| 1330 | } |
|---|
| 1331 | |
|---|
| 1332 | # Populate completion. |
|---|
| 1333 | if ($dir eq 'in') { |
|---|
| 1334 | $completion_jids{ $props{sender} }= 1; |
|---|
| 1335 | } |
|---|
| 1336 | else { |
|---|
| 1337 | $completion_jids{ $props{recipient} } = 1; |
|---|
| 1338 | } |
|---|
| 1339 | } |
|---|
| 1340 | |
|---|
| 1341 | return %props; |
|---|
| 1342 | } |
|---|
| 1343 | |
|---|
| 1344 | sub j2o { |
|---|
| 1345 | return BarnOwl::Message->new( j2hash(@_) ); |
|---|
| 1346 | } |
|---|
| 1347 | |
|---|
| 1348 | sub queue_admin_msg { |
|---|
| 1349 | my $err = shift; |
|---|
| 1350 | BarnOwl::admin_message("Jabber", $err); |
|---|
| 1351 | } |
|---|
| 1352 | |
|---|
| 1353 | sub getServerFromJID { |
|---|
| 1354 | my $jid = shift; |
|---|
| 1355 | my $res = new Net::DNS::Resolver; |
|---|
| 1356 | my $packet = |
|---|
| 1357 | $res->search( '_xmpp-client._tcp.' . $jid->GetServer(), 'srv' ); |
|---|
| 1358 | |
|---|
| 1359 | if ($packet) # Got srv record. |
|---|
| 1360 | { |
|---|
| 1361 | my @answer = $packet->answer; |
|---|
| 1362 | return $answer[0]->target, $answer[0]->port if @answer; |
|---|
| 1363 | } |
|---|
| 1364 | |
|---|
| 1365 | return $jid->GetServer(), 5222; |
|---|
| 1366 | } |
|---|
| 1367 | |
|---|
| 1368 | sub defaultJID { |
|---|
| 1369 | return ( $conn->getJIDs() )[0] if ( $conn->connected() == 1 ); |
|---|
| 1370 | return; |
|---|
| 1371 | } |
|---|
| 1372 | |
|---|
| 1373 | sub baseJID { |
|---|
| 1374 | my $givenJIDStr = shift; |
|---|
| 1375 | my $givenJID = new Net::Jabber::JID; |
|---|
| 1376 | $givenJID->SetJID($givenJIDStr); |
|---|
| 1377 | return $givenJID->GetJID('base'); |
|---|
| 1378 | } |
|---|
| 1379 | |
|---|
| 1380 | sub resolveConnectedJID { |
|---|
| 1381 | my $givenJIDStr = shift; |
|---|
| 1382 | my $loose = shift || 0; |
|---|
| 1383 | my $givenJID = new Net::Jabber::JID; |
|---|
| 1384 | $givenJID->SetJID($givenJIDStr); |
|---|
| 1385 | |
|---|
| 1386 | # Account fully specified. |
|---|
| 1387 | if ( $givenJID->GetResource() ) { |
|---|
| 1388 | # Specified account exists |
|---|
| 1389 | return $givenJIDStr if ($conn->jidExists($givenJIDStr) ); |
|---|
| 1390 | return resolveConnectedJID($givenJID->GetJID('base')) if $loose; |
|---|
| 1391 | die("Invalid account: $givenJIDStr\n"); |
|---|
| 1392 | } |
|---|
| 1393 | |
|---|
| 1394 | # Disambiguate. |
|---|
| 1395 | else { |
|---|
| 1396 | my $JIDMatchingJID = ""; |
|---|
| 1397 | my $strMatchingJID = ""; |
|---|
| 1398 | my $JIDMatches = ""; |
|---|
| 1399 | my $strMatches = ""; |
|---|
| 1400 | my $JIDAmbiguous = 0; |
|---|
| 1401 | my $strAmbiguous = 0; |
|---|
| 1402 | |
|---|
| 1403 | foreach my $jid ( $conn->getJIDs() ) { |
|---|
| 1404 | my $cJID = new Net::Jabber::JID; |
|---|
| 1405 | $cJID->SetJID($jid); |
|---|
| 1406 | if ( $givenJIDStr eq $cJID->GetJID('base') ) { |
|---|
| 1407 | $JIDAmbiguous = 1 if ( $JIDMatchingJID ne "" ); |
|---|
| 1408 | $JIDMatchingJID = $jid; |
|---|
| 1409 | $JIDMatches .= "\t$jid\n"; |
|---|
| 1410 | } |
|---|
| 1411 | if ( $cJID->GetJID('base') =~ /$givenJIDStr/ ) { |
|---|
| 1412 | $strAmbiguous = 1 if ( $strMatchingJID ne "" ); |
|---|
| 1413 | $strMatchingJID = $jid; |
|---|
| 1414 | $strMatches .= "\t$jid\n"; |
|---|
| 1415 | } |
|---|
| 1416 | } |
|---|
| 1417 | |
|---|
| 1418 | # Need further disambiguation. |
|---|
| 1419 | if ($JIDAmbiguous) { |
|---|
| 1420 | my $errStr = |
|---|
| 1421 | "Ambiguous account reference. Please specify a resource.\n"; |
|---|
| 1422 | die($errStr.$JIDMatches); |
|---|
| 1423 | } |
|---|
| 1424 | |
|---|
| 1425 | # It's this one. |
|---|
| 1426 | elsif ($JIDMatchingJID ne "") { |
|---|
| 1427 | return $JIDMatchingJID; |
|---|
| 1428 | } |
|---|
| 1429 | |
|---|
| 1430 | # Further resolution by substring. |
|---|
| 1431 | elsif ($strAmbiguous) { |
|---|
| 1432 | my $errStr = |
|---|
| 1433 | "Ambiguous account reference. Please be more specific.\n"; |
|---|
| 1434 | die($errStr.$strMatches); |
|---|
| 1435 | } |
|---|
| 1436 | |
|---|
| 1437 | # It's this one, by substring. |
|---|
| 1438 | elsif ($strMatchingJID ne "") { |
|---|
| 1439 | return $strMatchingJID; |
|---|
| 1440 | } |
|---|
| 1441 | |
|---|
| 1442 | # Not one of ours. |
|---|
| 1443 | else { |
|---|
| 1444 | die("Invalid account: $givenJIDStr\n"); |
|---|
| 1445 | } |
|---|
| 1446 | |
|---|
| 1447 | } |
|---|
| 1448 | return ""; |
|---|
| 1449 | } |
|---|
| 1450 | |
|---|
| 1451 | sub resolveDestJID { |
|---|
| 1452 | my ($to, $from) = @_; |
|---|
| 1453 | my $jid = Net::Jabber::JID->new($to); |
|---|
| 1454 | |
|---|
| 1455 | my $roster = $conn->getRosterFromJID($from); |
|---|
| 1456 | my @jids = $roster->jids('all'); |
|---|
| 1457 | for my $j (@jids) { |
|---|
| 1458 | if(($roster->query($j, 'name') || $j->GetUserID()) eq $to) { |
|---|
| 1459 | return $j->GetJID('full'); |
|---|
| 1460 | } elsif($j->GetJID('base') eq baseJID($to)) { |
|---|
| 1461 | return $jid->GetJID('full'); |
|---|
| 1462 | } |
|---|
| 1463 | } |
|---|
| 1464 | |
|---|
| 1465 | # If we found nothing being clever, check to see if our input was |
|---|
| 1466 | # sane enough to look like a jid with a UserID. |
|---|
| 1467 | return $jid->GetJID('full') if $jid->GetUserID(); |
|---|
| 1468 | return undef; |
|---|
| 1469 | } |
|---|
| 1470 | |
|---|
| 1471 | sub resolveType { |
|---|
| 1472 | my $to = shift; |
|---|
| 1473 | my $from = shift; |
|---|
| 1474 | return unless $from; |
|---|
| 1475 | my @mucs = $conn->getConnectionFromJID($from)->MUCs; |
|---|
| 1476 | if(grep {$_->BaseJID eq $to } @mucs) { |
|---|
| 1477 | return 'groupchat'; |
|---|
| 1478 | } else { |
|---|
| 1479 | return 'chat'; |
|---|
| 1480 | } |
|---|
| 1481 | } |
|---|
| 1482 | |
|---|
| 1483 | sub guess_jwrite { |
|---|
| 1484 | # Heuristically guess what jids a jwrite was meant to be going to/from |
|---|
| 1485 | my ($from, $to) = (@_); |
|---|
| 1486 | my ($from_jid, $to_jid); |
|---|
| 1487 | my @matches; |
|---|
| 1488 | if($from) { |
|---|
| 1489 | $from_jid = resolveConnectedJID($from, 1); |
|---|
| 1490 | die("Unable to resolve account $from\n") unless $from_jid; |
|---|
| 1491 | $to_jid = resolveDestJID($to, $from_jid); |
|---|
| 1492 | push @matches, [$from_jid, $to_jid] if $to_jid; |
|---|
| 1493 | } else { |
|---|
| 1494 | for my $f ($conn->getJIDs) { |
|---|
| 1495 | $to_jid = resolveDestJID($to, $f); |
|---|
| 1496 | if(defined($to_jid)) { |
|---|
| 1497 | push @matches, [$f, $to_jid]; |
|---|
| 1498 | } |
|---|
| 1499 | } |
|---|
| 1500 | if($to =~ /@/) { |
|---|
| 1501 | push @matches, [$_, $to] |
|---|
| 1502 | for ($conn->getJIDs); |
|---|
| 1503 | } |
|---|
| 1504 | } |
|---|
| 1505 | |
|---|
| 1506 | for my $m (@matches) { |
|---|
| 1507 | my $type = resolveType($m->[1], $m->[0]); |
|---|
| 1508 | push @$m, $type; |
|---|
| 1509 | } |
|---|
| 1510 | |
|---|
| 1511 | return @matches; |
|---|
| 1512 | } |
|---|
| 1513 | |
|---|
| 1514 | ################################################################################ |
|---|
| 1515 | ### Completion |
|---|
| 1516 | |
|---|
| 1517 | sub complete_user_or_muc { return keys %completion_jids; } |
|---|
| 1518 | sub complete_account { return $conn->getJIDs(); } |
|---|
| 1519 | |
|---|
| 1520 | sub complete_jwrite { |
|---|
| 1521 | my $ctx = shift; |
|---|
| 1522 | return complete_flags($ctx, |
|---|
| 1523 | [qw(-t -i -s)], |
|---|
| 1524 | { |
|---|
| 1525 | "-a" => \&complete_account, |
|---|
| 1526 | }, |
|---|
| 1527 | \&complete_user_or_muc |
|---|
| 1528 | ); |
|---|
| 1529 | } |
|---|
| 1530 | |
|---|
| 1531 | sub complete_jabberlogout { |
|---|
| 1532 | my $ctx = shift; |
|---|
| 1533 | if($ctx->word == 1) { |
|---|
| 1534 | return ("-A", complete_account() ); |
|---|
| 1535 | } else { |
|---|
| 1536 | return (); |
|---|
| 1537 | } |
|---|
| 1538 | } |
|---|
| 1539 | |
|---|
| 1540 | sub complete_jabber_get_buddy_status { |
|---|
| 1541 | my $ctx = shift; |
|---|
| 1542 | return complete_flags($ctx, |
|---|
| 1543 | [qw()], |
|---|
| 1544 | { |
|---|
| 1545 | "-a" => \&complete_account, |
|---|
| 1546 | }, |
|---|
| 1547 | \&complete_user_or_muc |
|---|
| 1548 | ); |
|---|
| 1549 | } |
|---|
| 1550 | |
|---|
| 1551 | BarnOwl::Completion::register_completer(jwrite => sub { BarnOwl::Module::Jabber::complete_jwrite(@_) }); |
|---|
| 1552 | BarnOwl::Completion::register_completer(jabberlogout => sub { BarnOwl::Module::Jabber::complete_jabberlogout(@_) }); |
|---|
| 1553 | BarnOwl::Completion::register_completer(jabber_get_buddy_status => sub { BarnOwl::Module::Jabber::complete_jabber_get_buddy_status(@_) }); |
|---|
| 1554 | |
|---|
| 1555 | 1; |
|---|