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