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