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