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