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