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