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