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