[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 | |
---|
[38ffdf9] | 11 | ################################################################################ |
---|
| 12 | # owl perl jabber support |
---|
| 13 | # |
---|
[b6a253c] | 14 | # XXX Todo: |
---|
| 15 | # Rosters for MUCs |
---|
| 16 | # More user feedback |
---|
| 17 | # * joining MUC |
---|
| 18 | # * parting MUC |
---|
| 19 | # * presence (Roster and MUC) |
---|
| 20 | # Implementing formatting and logging callbacks for C |
---|
| 21 | # Appropriate callbacks for presence subscription messages. |
---|
| 22 | # * Current behavior => auto-accept (default for Net::Jabber) |
---|
[38ffdf9] | 23 | # |
---|
| 24 | ################################################################################ |
---|
| 25 | |
---|
[6a6dd47] | 26 | our $connections; |
---|
| 27 | our %vars; |
---|
[38ffdf9] | 28 | |
---|
[b405ff6] | 29 | sub onStart { |
---|
| 30 | if ( eval { \&owl::queue_message } ) { |
---|
| 31 | register_owl_commands(); |
---|
| 32 | push @::onMainLoop, sub { owl_jabber::onMainLoop(@_) }; |
---|
| 33 | push @::onGetBuddyList, sub { owl_jabber::onGetBuddyList(@_) }; |
---|
[38ffdf9] | 34 | } |
---|
[b405ff6] | 35 | else { |
---|
| 36 | |
---|
[38ffdf9] | 37 | # Our owl doesn't support queue_message. Unfortunately, this |
---|
| 38 | # means it probably *also* doesn't support owl::error. So just |
---|
| 39 | # give up silently. |
---|
| 40 | } |
---|
| 41 | } |
---|
[9f183ff] | 42 | |
---|
[b6a253c] | 43 | push @::onStartSubs, sub { owl_jabber::onStart(@_) }; |
---|
[38ffdf9] | 44 | |
---|
[b405ff6] | 45 | sub onMainLoop { |
---|
| 46 | return if ( !connected() ); |
---|
[6a6dd47] | 47 | |
---|
[b405ff6] | 48 | foreach my $jid ( keys %$connections ) { |
---|
| 49 | my $client = \$connections->{$jid}->{client}; |
---|
| 50 | |
---|
| 51 | my $status = $$client->Process(0); |
---|
| 52 | if ( !defined($status) ) { |
---|
[960395d] | 53 | owl::error("Jabber account $jid disconnected!"); |
---|
| 54 | do_logout($jid); |
---|
| 55 | } |
---|
[b405ff6] | 56 | if ($::shutdown) { |
---|
| 57 | do_logout($jid); |
---|
| 58 | return; |
---|
| 59 | } |
---|
[38ffdf9] | 60 | } |
---|
| 61 | } |
---|
[b6a253c] | 62 | |
---|
[b405ff6] | 63 | sub blist_listBuddy { |
---|
[6a6dd47] | 64 | my $roster = shift; |
---|
[b405ff6] | 65 | my $buddy = shift; |
---|
[b6a253c] | 66 | my $blistStr .= " "; |
---|
[b405ff6] | 67 | my %jq = $$roster->query($buddy); |
---|
[6a6dd47] | 68 | my $res = $$roster->resource($buddy); |
---|
[b6a253c] | 69 | |
---|
| 70 | $blistStr .= $jq{name} ? $jq{name} : $buddy->GetJID(); |
---|
[b405ff6] | 71 | |
---|
| 72 | if ($res) { |
---|
| 73 | my %rq = $$roster->resourceQuery( $buddy, $res ); |
---|
| 74 | $blistStr .= " [" . ( $rq{show} ? $rq{show} : 'online' ) . "]"; |
---|
| 75 | $blistStr .= " " . $rq{status} if $rq{status}; |
---|
| 76 | $blistStr = boldify($blistStr); |
---|
[b6a253c] | 77 | } |
---|
[b405ff6] | 78 | else { |
---|
| 79 | $blistStr .= $jq{ask} ? " [pending]" : " [offline]"; |
---|
[b6a253c] | 80 | } |
---|
| 81 | |
---|
[b405ff6] | 82 | return $blistStr . "\n"; |
---|
[b6a253c] | 83 | } |
---|
| 84 | |
---|
[b405ff6] | 85 | sub onGetBuddyList { |
---|
[6a6dd47] | 86 | my $blist = ""; |
---|
[b405ff6] | 87 | foreach my $jid ( keys %{$connections} ) { |
---|
| 88 | my $roster = \$connections->{$jid}->{roster}; |
---|
| 89 | if ($$roster) { |
---|
| 90 | $blist .= "\n" . boldify("Jabber Roster for $jid\n"); |
---|
| 91 | |
---|
| 92 | foreach my $group ( $$roster->groups() ) { |
---|
| 93 | $blist .= " Group: $group\n"; |
---|
| 94 | foreach my $buddy ( $$roster->jids( 'group', $group ) ) { |
---|
| 95 | $blist .= blist_listBuddy( $roster, $buddy ); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | my @unsorted = $$roster->jids('nogroup'); |
---|
| 100 | if (@unsorted) { |
---|
| 101 | $blist .= " [unsorted]\n"; |
---|
| 102 | foreach my $buddy (@unsorted) { |
---|
| 103 | $blist .= blist_listBuddy( $roster, $buddy ); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | } |
---|
[b6a253c] | 107 | } |
---|
[6a6dd47] | 108 | return $blist; |
---|
[b6a253c] | 109 | } |
---|
[38ffdf9] | 110 | |
---|
| 111 | ################################################################################ |
---|
| 112 | ### Owl Commands |
---|
[b405ff6] | 113 | sub register_owl_commands() { |
---|
[38ffdf9] | 114 | owl::new_command( |
---|
| 115 | jabberlogin => \&cmd_login, |
---|
| 116 | { summary => "Log into jabber", } |
---|
| 117 | ); |
---|
| 118 | owl::new_command( |
---|
| 119 | jabberlogout => \&cmd_logout, |
---|
| 120 | { summary => "Log out of jabber" } |
---|
| 121 | ); |
---|
| 122 | owl::new_command( |
---|
| 123 | jwrite => \&cmd_jwrite, |
---|
| 124 | { |
---|
[b405ff6] | 125 | summary => "Send a Jabber Message", |
---|
| 126 | usage => "jwrite JID [-g] [-t thread] [-s subject]" |
---|
[38ffdf9] | 127 | } |
---|
| 128 | ); |
---|
| 129 | owl::new_command( |
---|
[b6a253c] | 130 | jlist => \&cmd_jlist, |
---|
[38ffdf9] | 131 | { |
---|
[b405ff6] | 132 | summary => "Show your Jabber roster.", |
---|
| 133 | usage => "jlist" |
---|
[38ffdf9] | 134 | } |
---|
| 135 | ); |
---|
| 136 | owl::new_command( |
---|
[b6a253c] | 137 | jmuc => \&cmd_jmuc, |
---|
[38ffdf9] | 138 | { |
---|
[b6a253c] | 139 | summary => "Jabber MUC related commands.", |
---|
[b405ff6] | 140 | description => "jmuc sends jabber commands related to muc.\n\n" |
---|
| 141 | . "The following commands are available\n\n" |
---|
| 142 | . "join {muc} Join a muc.\n\n" |
---|
| 143 | . "part [muc] Part a muc.\n" |
---|
| 144 | . " The muc is taken from the current message if not supplied.\n\n" |
---|
| 145 | . "invite {jid} [muc]\n" |
---|
| 146 | . " Invite {jid} to [muc].\n" |
---|
| 147 | . " The muc is taken from the current message if not supplied.\n\n" |
---|
[d9f4a5c] | 148 | . "configure [muc]\n" |
---|
| 149 | . " Configure [muc].\n" |
---|
[b405ff6] | 150 | . " Necessary to initalize a new MUC", |
---|
| 151 | usage => "jmuc {command} {args}" |
---|
[38ffdf9] | 152 | } |
---|
| 153 | ); |
---|
| 154 | } |
---|
| 155 | |
---|
[b405ff6] | 156 | sub cmd_login { |
---|
[6a6dd47] | 157 | my $cmd = shift; |
---|
| 158 | my $jid = new Net::XMPP::JID; |
---|
| 159 | $jid->SetJID(shift); |
---|
[b405ff6] | 160 | |
---|
| 161 | my $uid = $jid->GetUserID(); |
---|
[6a6dd47] | 162 | my $componentname = $jid->GetServer(); |
---|
[b405ff6] | 163 | my $resource = $jid->GetResource() || 'owl'; |
---|
[6a6dd47] | 164 | $jid->SetResource($resource); |
---|
| 165 | my $jidStr = $jid->GetJID('full'); |
---|
| 166 | |
---|
[b405ff6] | 167 | if ( !$uid || !$componentname ) { |
---|
| 168 | owl::error("usage: $cmd {jid}"); |
---|
| 169 | return; |
---|
[38ffdf9] | 170 | } |
---|
[b6a253c] | 171 | |
---|
[b405ff6] | 172 | if ( $connections->{$jidStr} ) { |
---|
| 173 | owl::error("Already logged in as $jidStr."); |
---|
| 174 | return; |
---|
[6a6dd47] | 175 | } |
---|
| 176 | |
---|
[b405ff6] | 177 | my ( $server, $port ) = getServerFromJID($jid); |
---|
[6a6dd47] | 178 | |
---|
[b405ff6] | 179 | $connections->{$jidStr}->{client} = Net::Jabber::Client->new( |
---|
| 180 | debuglevel => owl::getvar('debug') eq 'on' ? 1 : 0, |
---|
| 181 | debugfile => 'jabber.log' |
---|
| 182 | ); |
---|
[6a6dd47] | 183 | my $client = \$connections->{$jidStr}->{client}; |
---|
[b405ff6] | 184 | $connections->{$jidStr}->{roster} = |
---|
| 185 | $connections->{$jidStr}->{client}->Roster(); |
---|
[b6a253c] | 186 | |
---|
| 187 | #XXX Todo: Add more callbacks. |
---|
| 188 | # MUC presence handlers |
---|
[b405ff6] | 189 | $$client->SetMessageCallBacks( |
---|
| 190 | chat => sub { owl_jabber::process_incoming_chat_message(@_) }, |
---|
| 191 | error => sub { owl_jabber::process_incoming_error_message(@_) }, |
---|
| 192 | groupchat => sub { owl_jabber::process_incoming_groupchat_message(@_) }, |
---|
| 193 | headline => sub { owl_jabber::process_incoming_headline_message(@_) }, |
---|
| 194 | normal => sub { owl_jabber::process_incoming_normal_message(@_) } |
---|
| 195 | ); |
---|
[6a6dd47] | 196 | |
---|
[b405ff6] | 197 | $vars{jlogin_connhash} = { |
---|
| 198 | hostname => $server, |
---|
| 199 | tls => 1, |
---|
| 200 | port => $port, |
---|
| 201 | componentname => $componentname |
---|
| 202 | }; |
---|
[6a6dd47] | 203 | |
---|
[b405ff6] | 204 | my $status = $$client->Connect( %{ $vars{jlogin_connhash} } ); |
---|
[6a6dd47] | 205 | |
---|
[b405ff6] | 206 | if ( !$status ) { |
---|
| 207 | delete $connections->{$jidStr}; |
---|
| 208 | delete $vars{jlogin_connhash}; |
---|
| 209 | owl::error("We failed to connect"); |
---|
| 210 | return ""; |
---|
[38ffdf9] | 211 | } |
---|
| 212 | |
---|
[b405ff6] | 213 | $vars{jlogin_authhash} = |
---|
| 214 | { username => $uid, resource => $resource, password => '' }; |
---|
| 215 | my @result = $$client->AuthSend( %{ $vars{jlogin_authhash} } ); |
---|
| 216 | if ( $result[0] ne 'ok' ) { |
---|
| 217 | if ( $result[1] == 401 ) { |
---|
| 218 | $vars{jlogin_jid} = $jidStr; |
---|
| 219 | delete $connections->{$jidStr}; |
---|
| 220 | owl::start_password( "Password for $jidStr: ", \&do_login_with_pw ); |
---|
| 221 | return ""; |
---|
| 222 | } |
---|
| 223 | owl::error( |
---|
| 224 | "Error in connect: " . join( " ", $result[ 1 .. $#result ] ) ); |
---|
| 225 | do_logout($jidStr); |
---|
| 226 | delete $vars{jlogin_connhash}; |
---|
| 227 | delete $vars{jlogin_authhash}; |
---|
[6a6dd47] | 228 | return ""; |
---|
[38ffdf9] | 229 | } |
---|
[6a6dd47] | 230 | $connections->{$jidStr}->{roster}->fetch(); |
---|
[b405ff6] | 231 | $$client->PresenceSend( priority => 1 ); |
---|
[6a6dd47] | 232 | queue_admin_msg("Connected to jabber as $jidStr"); |
---|
| 233 | delete $vars{jlogin_connhash}; |
---|
| 234 | delete $vars{jlogin_authhash}; |
---|
| 235 | return ""; |
---|
| 236 | } |
---|
[38ffdf9] | 237 | |
---|
[b405ff6] | 238 | sub do_login_with_pw { |
---|
[6a6dd47] | 239 | $vars{jlogin_authhash}->{password} = shift; |
---|
| 240 | my $jidStr = delete $vars{jlogin_jid}; |
---|
[b405ff6] | 241 | if ( !$jidStr ) { |
---|
| 242 | owl::error("Got password but have no jid!"); |
---|
[6a6dd47] | 243 | } |
---|
| 244 | |
---|
| 245 | $connections->{$jidStr}->{client} = Net::Jabber::Client->new(); |
---|
| 246 | my $client = \$connections->{$jidStr}->{client}; |
---|
[b405ff6] | 247 | $connections->{$jidStr}->{roster} = |
---|
| 248 | $connections->{$jidStr}->{client}->Roster(); |
---|
| 249 | |
---|
| 250 | $$client->SetMessageCallBacks( |
---|
| 251 | chat => sub { owl_jabber::process_incoming_chat_message(@_) }, |
---|
| 252 | error => sub { owl_jabber::process_incoming_error_message(@_) }, |
---|
| 253 | groupchat => sub { owl_jabber::process_incoming_groupchat_message(@_) }, |
---|
| 254 | headline => sub { owl_jabber::process_incoming_headline_message(@_) }, |
---|
| 255 | normal => sub { owl_jabber::process_incoming_normal_message(@_) } |
---|
| 256 | ); |
---|
[38ffdf9] | 257 | |
---|
[b405ff6] | 258 | my $status = $$client->Connect( %{ $vars{jlogin_connhash} } ); |
---|
| 259 | if ( !$status ) { |
---|
| 260 | delete $connections->{$jidStr}; |
---|
| 261 | delete $vars{jlogin_connhash}; |
---|
| 262 | delete $vars{jlogin_authhash}; |
---|
| 263 | owl::error("We failed to connect"); |
---|
| 264 | return ""; |
---|
[6a6dd47] | 265 | } |
---|
| 266 | |
---|
[b405ff6] | 267 | my @result = $$client->AuthSend( %{ $vars{jlogin_authhash} } ); |
---|
[6a6dd47] | 268 | |
---|
[b405ff6] | 269 | if ( $result[0] ne 'ok' ) { |
---|
| 270 | owl::error( |
---|
| 271 | "Error in connect: " . join( " ", $result[ 1 .. $#result ] ) ); |
---|
| 272 | do_logout($jidStr); |
---|
| 273 | delete $vars{jlogin_connhash}; |
---|
| 274 | delete $vars{jlogin_authhash}; |
---|
[6a6dd47] | 275 | return ""; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | $connections->{$jidStr}->{roster}->fetch(); |
---|
[b405ff6] | 279 | $$client->PresenceSend( priority => 1 ); |
---|
[6a6dd47] | 280 | queue_admin_msg("Connected to jabber as $jidStr"); |
---|
| 281 | delete $vars{jlogin_connhash}; |
---|
| 282 | delete $vars{jlogin_authhash}; |
---|
[38ffdf9] | 283 | return ""; |
---|
| 284 | } |
---|
| 285 | |
---|
[b405ff6] | 286 | sub do_logout { |
---|
[6a6dd47] | 287 | my $jid = shift; |
---|
| 288 | $connections->{$jid}->{client}->Disconnect(); |
---|
| 289 | delete $connections->{$jid}; |
---|
| 290 | queue_admin_msg("Jabber disconnected ($jid)."); |
---|
| 291 | } |
---|
| 292 | |
---|
[b405ff6] | 293 | sub cmd_logout { |
---|
| 294 | |
---|
[6a6dd47] | 295 | # Logged into multiple accounts |
---|
[b405ff6] | 296 | if ( connected() > 1 ) { |
---|
| 297 | |
---|
| 298 | # Logged into multiple accounts, no accout specified. |
---|
| 299 | if ( !$_[1] ) { |
---|
| 300 | my $errStr = |
---|
| 301 | "You are logged into multiple accounts. Please specify an account to log out of.\n"; |
---|
| 302 | foreach my $jid ( keys %$connections ) { |
---|
| 303 | $errStr .= "\t$jid\n"; |
---|
| 304 | } |
---|
| 305 | queue_admin_msg($errStr); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | # Logged into multiple accounts, account specified. |
---|
| 309 | else { |
---|
| 310 | if ( $_[1] eq '-a' ) #All accounts. |
---|
| 311 | { |
---|
| 312 | foreach my $jid ( keys %$connections ) { |
---|
| 313 | do_logout($jid); |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | else #One account. |
---|
| 317 | { |
---|
| 318 | my $jid = resolveJID( $_[1] ); |
---|
| 319 | do_logout($jid) if ( $jid ne '' ); |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | else # Only one account logged in. |
---|
[6a6dd47] | 324 | { |
---|
[b405ff6] | 325 | |
---|
| 326 | do_logout( ( keys %$connections )[0] ); |
---|
[38ffdf9] | 327 | } |
---|
| 328 | return ""; |
---|
| 329 | } |
---|
| 330 | |
---|
[b405ff6] | 331 | sub cmd_jlist { |
---|
| 332 | if ( !( scalar keys %$connections ) ) { |
---|
| 333 | owl::error("You are not logged in to Jabber."); |
---|
| 334 | return; |
---|
[b6a253c] | 335 | } |
---|
[b405ff6] | 336 | owl::popless_ztext( onGetBuddyList() ); |
---|
[b6a253c] | 337 | } |
---|
| 338 | |
---|
[b405ff6] | 339 | sub cmd_jwrite { |
---|
| 340 | if ( !connected() ) { |
---|
| 341 | owl::error("You are not logged in to Jabber."); |
---|
| 342 | return; |
---|
[38ffdf9] | 343 | } |
---|
| 344 | |
---|
[b405ff6] | 345 | my $jwrite_to = ""; |
---|
| 346 | my $jwrite_from = ""; |
---|
| 347 | my $jwrite_thread = ""; |
---|
[6a6dd47] | 348 | my $jwrite_subject = ""; |
---|
[b405ff6] | 349 | my $jwrite_type = "chat"; |
---|
[38ffdf9] | 350 | |
---|
[6a6dd47] | 351 | my @args = @_; |
---|
| 352 | shift; |
---|
[9f183ff] | 353 | local @ARGV = @_; |
---|
[6a6dd47] | 354 | my $gc; |
---|
[b405ff6] | 355 | GetOptions( |
---|
| 356 | 'thread=s' => \$jwrite_thread, |
---|
| 357 | 'subject=s' => \$jwrite_subject, |
---|
| 358 | 'account=s' => \$jwrite_from, |
---|
| 359 | 'groupchat' => \$gc |
---|
| 360 | ); |
---|
[6a6dd47] | 361 | $jwrite_type = 'groupchat' if $gc; |
---|
| 362 | |
---|
[b405ff6] | 363 | if ( scalar @ARGV != 1 ) { |
---|
| 364 | owl::error( |
---|
| 365 | "Usage: jwrite JID [-g] [-t thread] [-s 'subject'] [-a account]"); |
---|
| 366 | return; |
---|
[6a6dd47] | 367 | } |
---|
[b405ff6] | 368 | else { |
---|
| 369 | $jwrite_to = shift @ARGV; |
---|
[6a6dd47] | 370 | } |
---|
[b6a253c] | 371 | |
---|
[b405ff6] | 372 | if ( !$jwrite_from ) { |
---|
| 373 | if ( connected() == 1 ) { |
---|
| 374 | $jwrite_from = ( keys %$connections )[0]; |
---|
| 375 | } |
---|
| 376 | else { |
---|
| 377 | owl::error("Please specify an account with -a {jid}"); |
---|
| 378 | return; |
---|
| 379 | } |
---|
| 380 | } |
---|
| 381 | else { |
---|
| 382 | $jwrite_from = resolveJID($jwrite_from); |
---|
| 383 | return unless $jwrite_from; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | $vars{jwrite} = { |
---|
| 387 | to => $jwrite_to, |
---|
| 388 | from => $jwrite_from, |
---|
| 389 | subject => $jwrite_subject, |
---|
| 390 | thread => $jwrite_thread, |
---|
| 391 | type => $jwrite_type |
---|
| 392 | }; |
---|
| 393 | |
---|
| 394 | owl::message( |
---|
[005562f] | 395 | "Type your message below. End with a dot on a line by itself. ^C will quit." |
---|
| 396 | ); |
---|
[b405ff6] | 397 | owl::start_edit_win( join( ' ', @args ), \&process_owl_jwrite ); |
---|
[38ffdf9] | 398 | } |
---|
| 399 | |
---|
[b405ff6] | 400 | sub cmd_jmuc { |
---|
| 401 | die "You are not logged in to Jabber" unless connected(); |
---|
| 402 | my $ocmd = shift; |
---|
| 403 | my $cmd = shift; |
---|
| 404 | if ( !$cmd ) { |
---|
| 405 | |
---|
| 406 | #XXX TODO: Write general usage for jmuc command. |
---|
| 407 | return; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | my %jmuc_commands = ( |
---|
| 411 | join => \&jmuc_join, |
---|
| 412 | part => \&jmuc_part, |
---|
| 413 | invite => \&jmuc_invite, |
---|
| 414 | configure => \&jmuc_configure |
---|
| 415 | ); |
---|
| 416 | my $func = $jmuc_commands{$cmd}; |
---|
| 417 | if ( !$func ) { |
---|
| 418 | owl::error("jmuc: Unknown command: $cmd"); |
---|
| 419 | return; |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | { |
---|
| 423 | local @ARGV = @_; |
---|
| 424 | my $jid; |
---|
| 425 | my $muc; |
---|
| 426 | my $m = owl::getcurmsg(); |
---|
| 427 | if ( $m->is_jabber && $m->{jtype} eq 'groupchat' ) { |
---|
| 428 | $muc = $m->{room}; |
---|
| 429 | $jid = $m->{to}; |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | my $getopt = Getopt::Long::Parser->new; |
---|
| 433 | $getopt->configure('pass_through'); |
---|
| 434 | $getopt->getoptions( 'account=s' => \$jid ); |
---|
| 435 | $jid ||= defaultJID(); |
---|
| 436 | if ($jid) { |
---|
| 437 | $jid = resolveJID($jid); |
---|
| 438 | return unless $jid; |
---|
| 439 | } |
---|
| 440 | else { |
---|
| 441 | owl::error('You must specify an account with -a {jid}'); |
---|
| 442 | } |
---|
| 443 | return $func->( $jid, $muc, @ARGV ); |
---|
| 444 | } |
---|
[6df381b] | 445 | } |
---|
| 446 | |
---|
| 447 | sub jmuc_join { |
---|
[b405ff6] | 448 | my ( $jid, $muc, @args ) = @_; |
---|
| 449 | local @ARGV = @args; |
---|
| 450 | my $password; |
---|
| 451 | GetOptions( 'password=s' => \$password ); |
---|
| 452 | |
---|
| 453 | $muc = shift @ARGV |
---|
| 454 | or die("Usage: jmuc join {muc} [-p password] [-a account]"); |
---|
| 455 | |
---|
| 456 | my $x = new XML::Stream::Node('x'); |
---|
| 457 | $x->put_attrib( xmlns => 'http://jabber.org/protocol/muc' ); |
---|
| 458 | $x->add_child('history')->put_attrib( maxchars => '0' ); |
---|
| 459 | |
---|
| 460 | if ($password) { |
---|
| 461 | $x->add_child('password')->add_cdata($password); |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | my $presence = new Net::Jabber::Presence; |
---|
| 465 | $presence->SetPresence( to => $muc ); |
---|
| 466 | $presence->AddX($x); |
---|
| 467 | $connections->{$jid}->{client}->Send($presence); |
---|
[6df381b] | 468 | } |
---|
| 469 | |
---|
| 470 | sub jmuc_part { |
---|
[b405ff6] | 471 | my ( $jid, $muc, @args ) = @_; |
---|
[9f183ff] | 472 | |
---|
[b405ff6] | 473 | $muc = shift @args if scalar @args; |
---|
| 474 | die("Usage: jmuc part {muc} [-a account]") unless $muc; |
---|
[9f183ff] | 475 | |
---|
[b405ff6] | 476 | $connections->{$jid}->{client} |
---|
| 477 | ->PresenceSend( to => $muc, type => 'unavailable' ); |
---|
| 478 | queue_admin_msg("$jid has left $muc."); |
---|
[6df381b] | 479 | } |
---|
| 480 | |
---|
[b405ff6] | 481 | sub jmuc_invite { |
---|
| 482 | my ( $jid, $muc, @args ) = @_; |
---|
| 483 | |
---|
| 484 | my $invite_jid = shift @args; |
---|
| 485 | $muc = shift @args if scalar @args; |
---|
| 486 | |
---|
| 487 | die('Usage: jmuc invite {jid} [muc] [-a account]') |
---|
| 488 | unless $muc && $invite_jid; |
---|
| 489 | |
---|
[d9f4a5c] | 490 | my $message = Net::Jabber::Message->new(); |
---|
[b405ff6] | 491 | $message->SetTo($muc); |
---|
[d9f4a5c] | 492 | my $x = $message->NewChild('http://jabber.org/protocol/muc#user'); |
---|
| 493 | $x->AddInvite(); |
---|
| 494 | $x->GetInvite()->SetTo($invite_jid); |
---|
[b405ff6] | 495 | $connections->{$jid}->{client}->Send($message); |
---|
| 496 | queue_admin_msg("$jid has invited $invite_jid to $muc."); |
---|
[38ffdf9] | 497 | } |
---|
| 498 | |
---|
[9f183ff] | 499 | Net::Jabber::Namespaces::add_ns( |
---|
[b405ff6] | 500 | ns => "http://jabber.org/protocol/muc#owner", |
---|
| 501 | tag => 'query', |
---|
| 502 | ); |
---|
[9f183ff] | 503 | |
---|
| 504 | sub jmuc_configure { |
---|
[b405ff6] | 505 | my ( $jid, $muc, @args ) = @_; |
---|
| 506 | $muc = shift @args if scalar @args; |
---|
| 507 | die("Usage: jmuc configure [muc]") unless $muc; |
---|
| 508 | my $iq = Net::Jabber::IQ->new(); |
---|
| 509 | $iq->SetTo($muc); |
---|
| 510 | $iq->SetType('set'); |
---|
| 511 | my $query = $iq->NewQuery("http://jabber.org/protocol/muc#owner"); |
---|
| 512 | my $x = $query->NewChild("jabber:x:data"); |
---|
| 513 | $x->SetType('submit'); |
---|
| 514 | |
---|
| 515 | $connections->{$jid}->{client}->Send($iq); |
---|
| 516 | queue_admin_msg("Accepted default instant configuration for $muc"); |
---|
[9f183ff] | 517 | } |
---|
| 518 | |
---|
[38ffdf9] | 519 | ################################################################################ |
---|
| 520 | ### Owl Callbacks |
---|
[b405ff6] | 521 | sub process_owl_jwrite { |
---|
[38ffdf9] | 522 | my $body = shift; |
---|
| 523 | |
---|
| 524 | my $j = new Net::XMPP::Message; |
---|
| 525 | $body =~ s/\n\z//; |
---|
[b405ff6] | 526 | $j->SetMessage( |
---|
| 527 | to => $vars{jwrite}{to}, |
---|
| 528 | from => $vars{jwrite}{from}, |
---|
| 529 | type => $vars{jwrite}{type}, |
---|
| 530 | body => $body |
---|
| 531 | ); |
---|
| 532 | $j->SetThread( $vars{jwrite}{thread} ) if ( $vars{jwrite}{thread} ); |
---|
| 533 | $j->SetSubject( $vars{jwrite}{subject} ) if ( $vars{jwrite}{subject} ); |
---|
| 534 | |
---|
| 535 | my $m = j2o( $j, 'out' ); |
---|
| 536 | if ( $vars{jwrite}{type} ne 'groupchat' ) { |
---|
| 537 | |
---|
| 538 | #XXX TODO: Check for displayoutgoing. |
---|
| 539 | owl::queue_message($m); |
---|
[38ffdf9] | 540 | } |
---|
[b405ff6] | 541 | $connections->{ $vars{jwrite}{from} }->{client}->Send($j); |
---|
[6a6dd47] | 542 | delete $vars{jwrite}; |
---|
[005562f] | 543 | owl::message(""); # Kludge to make the ``type your message...'' message go away |
---|
[38ffdf9] | 544 | } |
---|
| 545 | |
---|
| 546 | ### XMPP Callbacks |
---|
| 547 | |
---|
[b405ff6] | 548 | sub process_incoming_chat_message { |
---|
| 549 | my ( $session, $j ) = @_; |
---|
| 550 | owl::queue_message( j2o( $j, 'in' ) ); |
---|
[38ffdf9] | 551 | } |
---|
| 552 | |
---|
[b405ff6] | 553 | sub process_incoming_error_message { |
---|
| 554 | my ( $session, $j ) = @_; |
---|
| 555 | my %jhash = j2hash( $j, 'in' ); |
---|
[b6a253c] | 556 | $jhash{type} = 'admin'; |
---|
[b405ff6] | 557 | owl::queue_message( owl::Message->new(%jhash) ); |
---|
[38ffdf9] | 558 | } |
---|
| 559 | |
---|
[b405ff6] | 560 | sub process_incoming_groupchat_message { |
---|
| 561 | my ( $session, $j ) = @_; |
---|
| 562 | |
---|
[38ffdf9] | 563 | # HACK IN PROGRESS (ignoring delayed messages) |
---|
[b405ff6] | 564 | return if ( $j->DefinedX('jabber:x:delay') && $j->GetX('jabber:x:delay') ); |
---|
| 565 | owl::queue_message( j2o( $j, 'in' ) ); |
---|
[38ffdf9] | 566 | } |
---|
| 567 | |
---|
[b405ff6] | 568 | sub process_incoming_headline_message { |
---|
| 569 | my ( $session, $j ) = @_; |
---|
| 570 | owl::queue_message( j2o( $j, 'in' ) ); |
---|
[38ffdf9] | 571 | } |
---|
| 572 | |
---|
[b405ff6] | 573 | sub process_incoming_normal_message { |
---|
| 574 | my ( $session, $j ) = @_; |
---|
| 575 | my %props = j2hash( $j, 'in' ); |
---|
[b6a253c] | 576 | |
---|
| 577 | # XXX TODO: handle things such as MUC invites here. |
---|
| 578 | |
---|
[b405ff6] | 579 | # if ($j->HasX('http://jabber.org/protocol/muc#user')) |
---|
| 580 | # { |
---|
| 581 | # my $x = $j->GetX('http://jabber.org/protocol/muc#user'); |
---|
| 582 | # if ($x->HasChild('invite')) |
---|
| 583 | # { |
---|
| 584 | # $props |
---|
| 585 | # } |
---|
| 586 | # } |
---|
| 587 | # |
---|
| 588 | owl::queue_message( owl::Message->new(%props) ); |
---|
[b6a253c] | 589 | } |
---|
| 590 | |
---|
[b405ff6] | 591 | sub process_muc_presence { |
---|
| 592 | my ( $session, $p ) = @_; |
---|
| 593 | return unless ( $p->HasX('http://jabber.org/protocol/muc#user') ); |
---|
[38ffdf9] | 594 | |
---|
[b405ff6] | 595 | } |
---|
[38ffdf9] | 596 | |
---|
| 597 | ### Helper functions |
---|
| 598 | |
---|
[b405ff6] | 599 | sub j2hash { |
---|
| 600 | my $j = shift; |
---|
[38ffdf9] | 601 | my $dir = shift; |
---|
| 602 | |
---|
[b405ff6] | 603 | my %props = ( |
---|
| 604 | type => 'jabber', |
---|
| 605 | direction => $dir |
---|
| 606 | ); |
---|
[38ffdf9] | 607 | |
---|
[b6a253c] | 608 | my $jtype = $props{jtype} = $j->GetType(); |
---|
[b405ff6] | 609 | my $from = $j->GetFrom('jid'); |
---|
| 610 | my $to = $j->GetTo('jid'); |
---|
[38ffdf9] | 611 | |
---|
[b6a253c] | 612 | $props{from} = $from->GetJID('full'); |
---|
| 613 | $props{to} = $to->GetJID('full'); |
---|
[38ffdf9] | 614 | |
---|
[005562f] | 615 | my $account = ( $dir eq 'out' ) ? $props{from} : $props{to}; |
---|
| 616 | |
---|
[b6a253c] | 617 | $props{recipient} = $to->GetJID('base'); |
---|
| 618 | $props{sender} = $from->GetJID('base'); |
---|
[b405ff6] | 619 | $props{subject} = $j->GetSubject() if ( $j->DefinedSubject() ); |
---|
| 620 | $props{thread} = $j->GetThread() if ( $j->DefinedThread() ); |
---|
| 621 | $props{body} = $j->GetBody() if ( $j->DefinedBody() ); |
---|
| 622 | $props{error} = $j->GetError() if ( $j->DefinedError() ); |
---|
| 623 | $props{error_code} = $j->GetErrorCode() if ( $j->DefinedErrorCode() ); |
---|
[b6a253c] | 624 | $props{xml} = $j->GetXML(); |
---|
[38ffdf9] | 625 | |
---|
[b405ff6] | 626 | if ( $jtype eq 'chat' ) { |
---|
| 627 | $props{replycmd} = |
---|
[005562f] | 628 | "jwrite " . ( ( $dir eq 'in' ) ? $props{from} : $props{to} ) . " -a $account"; |
---|
[b405ff6] | 629 | $props{isprivate} = 1; |
---|
[005562f] | 630 | $props{replysendercmd} = $props{replycmd}; |
---|
[38ffdf9] | 631 | } |
---|
[b405ff6] | 632 | elsif ( $jtype eq 'groupchat' ) { |
---|
| 633 | my $nick = $props{nick} = $from->GetResource(); |
---|
| 634 | my $room = $props{room} = $from->GetJID('base'); |
---|
[005562f] | 635 | $props{replycmd} = "jwrite -g $room -a $account"; |
---|
| 636 | |
---|
| 637 | $props{replysendercmd} = "jwrite " . $from->GetJID('full') . " -a $account"; |
---|
[b405ff6] | 638 | |
---|
| 639 | $props{sender} = $nick || $room; |
---|
| 640 | $props{recipient} = $room; |
---|
| 641 | |
---|
| 642 | if ( $props{subject} && !$props{body} ) { |
---|
| 643 | $props{body} = |
---|
| 644 | '[' . $nick . " has set the topic to: " . $props{subject} . "]"; |
---|
| 645 | } |
---|
[b6a253c] | 646 | } |
---|
[b405ff6] | 647 | elsif ( $jtype eq 'normal' ) { |
---|
| 648 | $props{replycmd} = undef; |
---|
| 649 | $props{isprivate} = 1; |
---|
[b6a253c] | 650 | } |
---|
[b405ff6] | 651 | elsif ( $jtype eq 'headline' ) { |
---|
| 652 | $props{replycmd} = undef; |
---|
[b6a253c] | 653 | } |
---|
[b405ff6] | 654 | elsif ( $jtype eq 'error' ) { |
---|
| 655 | $props{replycmd} = undef; |
---|
| 656 | $props{body} = "Error " |
---|
| 657 | . $props{error_code} |
---|
| 658 | . " sending to " |
---|
| 659 | . $props{from} . "\n" |
---|
| 660 | . $props{error}; |
---|
| 661 | } |
---|
| 662 | |
---|
[b6a253c] | 663 | return %props; |
---|
| 664 | } |
---|
[38ffdf9] | 665 | |
---|
[b405ff6] | 666 | sub j2o { |
---|
| 667 | return owl::Message->new( j2hash(@_) ); |
---|
[38ffdf9] | 668 | } |
---|
| 669 | |
---|
[b405ff6] | 670 | sub queue_admin_msg { |
---|
[38ffdf9] | 671 | my $err = shift; |
---|
[b405ff6] | 672 | my $m = owl::Message->new( |
---|
| 673 | type => 'admin', |
---|
| 674 | direction => 'none', |
---|
| 675 | body => $err |
---|
| 676 | ); |
---|
[38ffdf9] | 677 | owl::queue_message($m); |
---|
| 678 | } |
---|
[b6a253c] | 679 | |
---|
[b405ff6] | 680 | sub boldify($) { |
---|
[9f183ff] | 681 | my $str = shift; |
---|
[b6a253c] | 682 | |
---|
[b405ff6] | 683 | return '@b(' . $str . ')' if ( $str !~ /\)/ ); |
---|
| 684 | return '@b<' . $str . '>' if ( $str !~ /\>/ ); |
---|
| 685 | return '@b{' . $str . '}' if ( $str !~ /\}/ ); |
---|
| 686 | return '@b[' . $str . ']' if ( $str !~ /\]/ ); |
---|
[b6a253c] | 687 | |
---|
| 688 | my $txt = "\@b($str"; |
---|
| 689 | $txt =~ s/\)/\)\@b\[\)\]\@b\(/g; |
---|
[b405ff6] | 690 | return $txt . ')'; |
---|
[b6a253c] | 691 | } |
---|
| 692 | |
---|
[b405ff6] | 693 | sub getServerFromJID { |
---|
[6a6dd47] | 694 | my $jid = shift; |
---|
| 695 | my $res = new Net::DNS::Resolver; |
---|
[b405ff6] | 696 | my $packet = |
---|
| 697 | $res->search( '_xmpp-client._tcp.' . $jid->GetServer(), 'srv' ); |
---|
[6a6dd47] | 698 | |
---|
[b405ff6] | 699 | if ($packet) # Got srv record. |
---|
[6a6dd47] | 700 | { |
---|
[b405ff6] | 701 | my @answer = $packet->answer; |
---|
| 702 | return $answer[0]{target}, $answer[0]{port}; |
---|
[6a6dd47] | 703 | } |
---|
| 704 | |
---|
| 705 | return $jid->GetServer(), 5222; |
---|
| 706 | } |
---|
| 707 | |
---|
[b405ff6] | 708 | sub connected { |
---|
[6a6dd47] | 709 | return scalar keys %$connections; |
---|
| 710 | } |
---|
| 711 | |
---|
[9f183ff] | 712 | sub defaultJID { |
---|
[b405ff6] | 713 | return ( keys %$connections )[0] if ( connected() == 1 ); |
---|
| 714 | return; |
---|
[9f183ff] | 715 | } |
---|
| 716 | |
---|
[b405ff6] | 717 | sub resolveJID { |
---|
[6a6dd47] | 718 | my $givenJidStr = shift; |
---|
[b405ff6] | 719 | my $givenJid = new Net::XMPP::JID; |
---|
[6a6dd47] | 720 | $givenJid->SetJID($givenJidStr); |
---|
[b405ff6] | 721 | |
---|
[6a6dd47] | 722 | # Account fully specified. |
---|
[b405ff6] | 723 | if ( $givenJid->GetResource() ) { |
---|
| 724 | |
---|
| 725 | # Specified account exists |
---|
| 726 | if ( defined $connections->{$givenJidStr} ) { |
---|
| 727 | return $givenJidStr; |
---|
| 728 | } |
---|
| 729 | else #Specified account doesn't exist |
---|
| 730 | { |
---|
| 731 | owl::error("Invalid account: $givenJidStr"); |
---|
| 732 | } |
---|
[6a6dd47] | 733 | } |
---|
[b405ff6] | 734 | |
---|
[6a6dd47] | 735 | # Disambiguate. |
---|
[b405ff6] | 736 | else { |
---|
| 737 | my $matchingJid = ""; |
---|
| 738 | my $errStr = |
---|
| 739 | "Ambiguous account reference. Please specify a resource.\n"; |
---|
| 740 | my $ambiguous = 0; |
---|
| 741 | |
---|
| 742 | foreach my $jid ( keys %$connections ) { |
---|
| 743 | my $cJid = new Net::XMPP::JID; |
---|
| 744 | $cJid->SetJID($jid); |
---|
| 745 | if ( $givenJidStr eq $cJid->GetJID('base') ) { |
---|
| 746 | $ambiguous = 1 if ( $matchingJid ne "" ); |
---|
| 747 | $matchingJid = $jid; |
---|
| 748 | $errStr .= "\t$jid\n"; |
---|
| 749 | } |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | # Need further disambiguation. |
---|
| 753 | if ($ambiguous) { |
---|
| 754 | queue_admin_msg($errStr); |
---|
| 755 | } |
---|
| 756 | |
---|
| 757 | # Not one of ours. |
---|
| 758 | elsif ( $matchingJid eq "" ) { |
---|
| 759 | owl::error("Invalid account: $givenJidStr"); |
---|
| 760 | } |
---|
| 761 | |
---|
| 762 | # Log out this one. |
---|
| 763 | else { |
---|
| 764 | return $matchingJid; |
---|
| 765 | } |
---|
[6a6dd47] | 766 | } |
---|
| 767 | return ""; |
---|
| 768 | } |
---|