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