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