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