[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" |
---|
| 148 | . "configure [muc]\n" " Configure [muc].\n" |
---|
| 149 | . " Necessary to initalize a new MUC", |
---|
| 150 | usage => "jmuc {command} {args}" |
---|
[38ffdf9] | 151 | } |
---|
| 152 | ); |
---|
| 153 | } |
---|
| 154 | |
---|
[b405ff6] | 155 | sub cmd_login { |
---|
[6a6dd47] | 156 | my $cmd = shift; |
---|
| 157 | my $jid = new Net::XMPP::JID; |
---|
| 158 | $jid->SetJID(shift); |
---|
[b405ff6] | 159 | |
---|
| 160 | my $uid = $jid->GetUserID(); |
---|
[6a6dd47] | 161 | my $componentname = $jid->GetServer(); |
---|
[b405ff6] | 162 | my $resource = $jid->GetResource() || 'owl'; |
---|
[6a6dd47] | 163 | $jid->SetResource($resource); |
---|
| 164 | my $jidStr = $jid->GetJID('full'); |
---|
| 165 | |
---|
[b405ff6] | 166 | if ( !$uid || !$componentname ) { |
---|
| 167 | owl::error("usage: $cmd {jid}"); |
---|
| 168 | return; |
---|
[38ffdf9] | 169 | } |
---|
[b6a253c] | 170 | |
---|
[b405ff6] | 171 | if ( $connections->{$jidStr} ) { |
---|
| 172 | owl::error("Already logged in as $jidStr."); |
---|
| 173 | return; |
---|
[6a6dd47] | 174 | } |
---|
| 175 | |
---|
[b405ff6] | 176 | my ( $server, $port ) = getServerFromJID($jid); |
---|
[6a6dd47] | 177 | |
---|
[b405ff6] | 178 | $connections->{$jidStr}->{client} = Net::Jabber::Client->new( |
---|
| 179 | debuglevel => owl::getvar('debug') eq 'on' ? 1 : 0, |
---|
| 180 | debugfile => 'jabber.log' |
---|
| 181 | ); |
---|
[6a6dd47] | 182 | my $client = \$connections->{$jidStr}->{client}; |
---|
[b405ff6] | 183 | $connections->{$jidStr}->{roster} = |
---|
| 184 | $connections->{$jidStr}->{client}->Roster(); |
---|
[b6a253c] | 185 | |
---|
| 186 | #XXX Todo: Add more callbacks. |
---|
| 187 | # MUC presence handlers |
---|
[b405ff6] | 188 | $$client->SetMessageCallBacks( |
---|
| 189 | chat => sub { owl_jabber::process_incoming_chat_message(@_) }, |
---|
| 190 | error => sub { owl_jabber::process_incoming_error_message(@_) }, |
---|
| 191 | groupchat => sub { owl_jabber::process_incoming_groupchat_message(@_) }, |
---|
| 192 | headline => sub { owl_jabber::process_incoming_headline_message(@_) }, |
---|
| 193 | normal => sub { owl_jabber::process_incoming_normal_message(@_) } |
---|
| 194 | ); |
---|
[6a6dd47] | 195 | |
---|
[b405ff6] | 196 | $vars{jlogin_connhash} = { |
---|
| 197 | hostname => $server, |
---|
| 198 | tls => 1, |
---|
| 199 | port => $port, |
---|
| 200 | componentname => $componentname |
---|
| 201 | }; |
---|
[6a6dd47] | 202 | |
---|
[b405ff6] | 203 | my $status = $$client->Connect( %{ $vars{jlogin_connhash} } ); |
---|
[6a6dd47] | 204 | |
---|
[b405ff6] | 205 | if ( !$status ) { |
---|
| 206 | delete $connections->{$jidStr}; |
---|
| 207 | delete $vars{jlogin_connhash}; |
---|
| 208 | owl::error("We failed to connect"); |
---|
| 209 | return ""; |
---|
[38ffdf9] | 210 | } |
---|
| 211 | |
---|
[b405ff6] | 212 | $vars{jlogin_authhash} = |
---|
| 213 | { username => $uid, resource => $resource, password => '' }; |
---|
| 214 | my @result = $$client->AuthSend( %{ $vars{jlogin_authhash} } ); |
---|
| 215 | if ( $result[0] ne 'ok' ) { |
---|
| 216 | if ( $result[1] == 401 ) { |
---|
| 217 | $vars{jlogin_jid} = $jidStr; |
---|
| 218 | delete $connections->{$jidStr}; |
---|
| 219 | owl::start_password( "Password for $jidStr: ", \&do_login_with_pw ); |
---|
| 220 | return ""; |
---|
| 221 | } |
---|
| 222 | owl::error( |
---|
| 223 | "Error in connect: " . join( " ", $result[ 1 .. $#result ] ) ); |
---|
| 224 | do_logout($jidStr); |
---|
| 225 | delete $vars{jlogin_connhash}; |
---|
| 226 | delete $vars{jlogin_authhash}; |
---|
[6a6dd47] | 227 | return ""; |
---|
[38ffdf9] | 228 | } |
---|
[6a6dd47] | 229 | $connections->{$jidStr}->{roster}->fetch(); |
---|
[b405ff6] | 230 | $$client->PresenceSend( priority => 1 ); |
---|
[6a6dd47] | 231 | queue_admin_msg("Connected to jabber as $jidStr"); |
---|
| 232 | delete $vars{jlogin_connhash}; |
---|
| 233 | delete $vars{jlogin_authhash}; |
---|
| 234 | return ""; |
---|
| 235 | } |
---|
[38ffdf9] | 236 | |
---|
[b405ff6] | 237 | sub do_login_with_pw { |
---|
[6a6dd47] | 238 | $vars{jlogin_authhash}->{password} = shift; |
---|
| 239 | my $jidStr = delete $vars{jlogin_jid}; |
---|
[b405ff6] | 240 | if ( !$jidStr ) { |
---|
| 241 | owl::error("Got password but have no jid!"); |
---|
[6a6dd47] | 242 | } |
---|
| 243 | |
---|
| 244 | $connections->{$jidStr}->{client} = Net::Jabber::Client->new(); |
---|
| 245 | my $client = \$connections->{$jidStr}->{client}; |
---|
[b405ff6] | 246 | $connections->{$jidStr}->{roster} = |
---|
| 247 | $connections->{$jidStr}->{client}->Roster(); |
---|
| 248 | |
---|
| 249 | $$client->SetMessageCallBacks( |
---|
| 250 | chat => sub { owl_jabber::process_incoming_chat_message(@_) }, |
---|
| 251 | error => sub { owl_jabber::process_incoming_error_message(@_) }, |
---|
| 252 | groupchat => sub { owl_jabber::process_incoming_groupchat_message(@_) }, |
---|
| 253 | headline => sub { owl_jabber::process_incoming_headline_message(@_) }, |
---|
| 254 | normal => sub { owl_jabber::process_incoming_normal_message(@_) } |
---|
| 255 | ); |
---|
[38ffdf9] | 256 | |
---|
[b405ff6] | 257 | my $status = $$client->Connect( %{ $vars{jlogin_connhash} } ); |
---|
| 258 | if ( !$status ) { |
---|
| 259 | delete $connections->{$jidStr}; |
---|
| 260 | delete $vars{jlogin_connhash}; |
---|
| 261 | delete $vars{jlogin_authhash}; |
---|
| 262 | owl::error("We failed to connect"); |
---|
| 263 | return ""; |
---|
[6a6dd47] | 264 | } |
---|
| 265 | |
---|
[b405ff6] | 266 | my @result = $$client->AuthSend( %{ $vars{jlogin_authhash} } ); |
---|
[6a6dd47] | 267 | |
---|
[b405ff6] | 268 | if ( $result[0] ne 'ok' ) { |
---|
| 269 | owl::error( |
---|
| 270 | "Error in connect: " . join( " ", $result[ 1 .. $#result ] ) ); |
---|
| 271 | do_logout($jidStr); |
---|
| 272 | delete $vars{jlogin_connhash}; |
---|
| 273 | delete $vars{jlogin_authhash}; |
---|
[6a6dd47] | 274 | return ""; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | $connections->{$jidStr}->{roster}->fetch(); |
---|
[b405ff6] | 278 | $$client->PresenceSend( priority => 1 ); |
---|
[6a6dd47] | 279 | queue_admin_msg("Connected to jabber as $jidStr"); |
---|
| 280 | delete $vars{jlogin_connhash}; |
---|
| 281 | delete $vars{jlogin_authhash}; |
---|
[38ffdf9] | 282 | return ""; |
---|
| 283 | } |
---|
| 284 | |
---|
[b405ff6] | 285 | sub do_logout { |
---|
[6a6dd47] | 286 | my $jid = shift; |
---|
| 287 | $connections->{$jid}->{client}->Disconnect(); |
---|
| 288 | delete $connections->{$jid}; |
---|
| 289 | queue_admin_msg("Jabber disconnected ($jid)."); |
---|
| 290 | } |
---|
| 291 | |
---|
[b405ff6] | 292 | sub cmd_logout { |
---|
| 293 | |
---|
[6a6dd47] | 294 | # Logged into multiple accounts |
---|
[b405ff6] | 295 | if ( connected() > 1 ) { |
---|
| 296 | |
---|
| 297 | # Logged into multiple accounts, no accout specified. |
---|
| 298 | if ( !$_[1] ) { |
---|
| 299 | my $errStr = |
---|
| 300 | "You are logged into multiple accounts. Please specify an account to log out of.\n"; |
---|
| 301 | foreach my $jid ( keys %$connections ) { |
---|
| 302 | $errStr .= "\t$jid\n"; |
---|
| 303 | } |
---|
| 304 | queue_admin_msg($errStr); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | # Logged into multiple accounts, account specified. |
---|
| 308 | else { |
---|
| 309 | if ( $_[1] eq '-a' ) #All accounts. |
---|
| 310 | { |
---|
| 311 | foreach my $jid ( keys %$connections ) { |
---|
| 312 | do_logout($jid); |
---|
| 313 | } |
---|
| 314 | } |
---|
| 315 | else #One account. |
---|
| 316 | { |
---|
| 317 | my $jid = resolveJID( $_[1] ); |
---|
| 318 | do_logout($jid) if ( $jid ne '' ); |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | else # Only one account logged in. |
---|
[6a6dd47] | 323 | { |
---|
[b405ff6] | 324 | |
---|
| 325 | do_logout( ( keys %$connections )[0] ); |
---|
[38ffdf9] | 326 | } |
---|
| 327 | return ""; |
---|
| 328 | } |
---|
| 329 | |
---|
[b405ff6] | 330 | sub cmd_jlist { |
---|
| 331 | if ( !( scalar keys %$connections ) ) { |
---|
| 332 | owl::error("You are not logged in to Jabber."); |
---|
| 333 | return; |
---|
[b6a253c] | 334 | } |
---|
[b405ff6] | 335 | owl::popless_ztext( onGetBuddyList() ); |
---|
[b6a253c] | 336 | } |
---|
| 337 | |
---|
[b405ff6] | 338 | sub cmd_jwrite { |
---|
| 339 | if ( !connected() ) { |
---|
| 340 | owl::error("You are not logged in to Jabber."); |
---|
| 341 | return; |
---|
[38ffdf9] | 342 | } |
---|
| 343 | |
---|
[b405ff6] | 344 | my $jwrite_to = ""; |
---|
| 345 | my $jwrite_from = ""; |
---|
| 346 | my $jwrite_thread = ""; |
---|
[6a6dd47] | 347 | my $jwrite_subject = ""; |
---|
[b405ff6] | 348 | my $jwrite_type = "chat"; |
---|
[38ffdf9] | 349 | |
---|
[6a6dd47] | 350 | my @args = @_; |
---|
| 351 | shift; |
---|
[9f183ff] | 352 | local @ARGV = @_; |
---|
[6a6dd47] | 353 | my $gc; |
---|
[b405ff6] | 354 | GetOptions( |
---|
| 355 | 'thread=s' => \$jwrite_thread, |
---|
| 356 | 'subject=s' => \$jwrite_subject, |
---|
| 357 | 'account=s' => \$jwrite_from, |
---|
| 358 | 'groupchat' => \$gc |
---|
| 359 | ); |
---|
[6a6dd47] | 360 | $jwrite_type = 'groupchat' if $gc; |
---|
| 361 | |
---|
[b405ff6] | 362 | if ( scalar @ARGV != 1 ) { |
---|
| 363 | owl::error( |
---|
| 364 | "Usage: jwrite JID [-g] [-t thread] [-s 'subject'] [-a account]"); |
---|
| 365 | return; |
---|
[6a6dd47] | 366 | } |
---|
[b405ff6] | 367 | else { |
---|
| 368 | $jwrite_to = shift @ARGV; |
---|
[6a6dd47] | 369 | } |
---|
[b6a253c] | 370 | |
---|
[b405ff6] | 371 | if ( !$jwrite_from ) { |
---|
| 372 | if ( connected() == 1 ) { |
---|
| 373 | $jwrite_from = ( keys %$connections )[0]; |
---|
| 374 | } |
---|
| 375 | else { |
---|
| 376 | owl::error("Please specify an account with -a {jid}"); |
---|
| 377 | return; |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | else { |
---|
| 381 | $jwrite_from = resolveJID($jwrite_from); |
---|
| 382 | return unless $jwrite_from; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | $vars{jwrite} = { |
---|
| 386 | to => $jwrite_to, |
---|
| 387 | from => $jwrite_from, |
---|
| 388 | subject => $jwrite_subject, |
---|
| 389 | thread => $jwrite_thread, |
---|
| 390 | type => $jwrite_type |
---|
| 391 | }; |
---|
| 392 | |
---|
| 393 | owl::message( |
---|
| 394 | "Type your message below. End with a dot on a line by itself. ^C will quit." |
---|
| 395 | ); |
---|
| 396 | owl::start_edit_win( join( ' ', @args ), \&process_owl_jwrite ); |
---|
[38ffdf9] | 397 | } |
---|
| 398 | |
---|
[b405ff6] | 399 | sub cmd_jmuc { |
---|
| 400 | die "You are not logged in to Jabber" unless connected(); |
---|
| 401 | my $ocmd = shift; |
---|
| 402 | my $cmd = shift; |
---|
| 403 | if ( !$cmd ) { |
---|
| 404 | |
---|
| 405 | #XXX TODO: Write general usage for jmuc command. |
---|
| 406 | return; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | my %jmuc_commands = ( |
---|
| 410 | join => \&jmuc_join, |
---|
| 411 | part => \&jmuc_part, |
---|
| 412 | invite => \&jmuc_invite, |
---|
| 413 | configure => \&jmuc_configure |
---|
| 414 | ); |
---|
| 415 | my $func = $jmuc_commands{$cmd}; |
---|
| 416 | if ( !$func ) { |
---|
| 417 | owl::error("jmuc: Unknown command: $cmd"); |
---|
| 418 | return; |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | { |
---|
| 422 | local @ARGV = @_; |
---|
| 423 | my $jid; |
---|
| 424 | my $muc; |
---|
| 425 | my $m = owl::getcurmsg(); |
---|
| 426 | if ( $m->is_jabber && $m->{jtype} eq 'groupchat' ) { |
---|
| 427 | $muc = $m->{room}; |
---|
| 428 | $jid = $m->{to}; |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | my $getopt = Getopt::Long::Parser->new; |
---|
| 432 | $getopt->configure('pass_through'); |
---|
| 433 | $getopt->getoptions( 'account=s' => \$jid ); |
---|
| 434 | $jid ||= defaultJID(); |
---|
| 435 | if ($jid) { |
---|
| 436 | $jid = resolveJID($jid); |
---|
| 437 | return unless $jid; |
---|
| 438 | } |
---|
| 439 | else { |
---|
| 440 | owl::error('You must specify an account with -a {jid}'); |
---|
| 441 | } |
---|
| 442 | return $func->( $jid, $muc, @ARGV ); |
---|
| 443 | } |
---|
[6df381b] | 444 | } |
---|
| 445 | |
---|
| 446 | sub jmuc_join { |
---|
[b405ff6] | 447 | my ( $jid, $muc, @args ) = @_; |
---|
| 448 | local @ARGV = @args; |
---|
| 449 | my $password; |
---|
| 450 | GetOptions( 'password=s' => \$password ); |
---|
| 451 | |
---|
| 452 | $muc = shift @ARGV |
---|
| 453 | or die("Usage: jmuc join {muc} [-p password] [-a account]"); |
---|
| 454 | |
---|
| 455 | my $x = new XML::Stream::Node('x'); |
---|
| 456 | $x->put_attrib( xmlns => 'http://jabber.org/protocol/muc' ); |
---|
| 457 | $x->add_child('history')->put_attrib( maxchars => '0' ); |
---|
| 458 | |
---|
| 459 | if ($password) { |
---|
| 460 | $x->add_child('password')->add_cdata($password); |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | my $presence = new Net::Jabber::Presence; |
---|
| 464 | $presence->SetPresence( to => $muc ); |
---|
| 465 | $presence->AddX($x); |
---|
| 466 | $connections->{$jid}->{client}->Send($presence); |
---|
[6df381b] | 467 | } |
---|
| 468 | |
---|
| 469 | sub jmuc_part { |
---|
[b405ff6] | 470 | my ( $jid, $muc, @args ) = @_; |
---|
[9f183ff] | 471 | |
---|
[b405ff6] | 472 | $muc = shift @args if scalar @args; |
---|
| 473 | die("Usage: jmuc part {muc} [-a account]") unless $muc; |
---|
[9f183ff] | 474 | |
---|
[b405ff6] | 475 | $connections->{$jid}->{client} |
---|
| 476 | ->PresenceSend( to => $muc, type => 'unavailable' ); |
---|
| 477 | queue_admin_msg("$jid has left $muc."); |
---|
[6df381b] | 478 | } |
---|
| 479 | |
---|
[b405ff6] | 480 | sub jmuc_invite { |
---|
| 481 | my ( $jid, $muc, @args ) = @_; |
---|
| 482 | |
---|
| 483 | my $invite_jid = shift @args; |
---|
| 484 | $muc = shift @args if scalar @args; |
---|
| 485 | |
---|
| 486 | die('Usage: jmuc invite {jid} [muc] [-a account]') |
---|
| 487 | unless $muc && $invite_jid; |
---|
| 488 | |
---|
| 489 | my $x = new XML::Stream::Node('x'); |
---|
| 490 | $x->put_attrib( xmlns => 'http://jabber.org/protocol/muc#user' ); |
---|
| 491 | $x->add_child('invite')->put_attrib( to => $invite_jid ); |
---|
| 492 | |
---|
| 493 | my $message = new Net::Jabber::Message; |
---|
| 494 | $message->SetTo($muc); |
---|
| 495 | $message->AddX($x); |
---|
| 496 | $connections->{$jid}->{client}->Send($message); |
---|
| 497 | queue_admin_msg("$jid has invited $invite_jid to $muc."); |
---|
[38ffdf9] | 498 | } |
---|
| 499 | |
---|
[9f183ff] | 500 | Net::Jabber::Namespaces::add_ns( |
---|
[b405ff6] | 501 | ns => "http://jabber.org/protocol/muc#owner", |
---|
| 502 | tag => 'query', |
---|
| 503 | ); |
---|
[9f183ff] | 504 | |
---|
| 505 | sub jmuc_configure { |
---|
[b405ff6] | 506 | my ( $jid, $muc, @args ) = @_; |
---|
| 507 | $muc = shift @args if scalar @args; |
---|
| 508 | die("Usage: jmuc configure [muc]") unless $muc; |
---|
| 509 | my $iq = Net::Jabber::IQ->new(); |
---|
| 510 | $iq->SetTo($muc); |
---|
| 511 | $iq->SetType('set'); |
---|
| 512 | my $query = $iq->NewQuery("http://jabber.org/protocol/muc#owner"); |
---|
| 513 | my $x = $query->NewChild("jabber:x:data"); |
---|
| 514 | $x->SetType('submit'); |
---|
| 515 | |
---|
| 516 | $connections->{$jid}->{client}->Send($iq); |
---|
| 517 | queue_admin_msg("Accepted default instant configuration for $muc"); |
---|
[9f183ff] | 518 | } |
---|
| 519 | |
---|
[38ffdf9] | 520 | ################################################################################ |
---|
| 521 | ### Owl Callbacks |
---|
[b405ff6] | 522 | sub process_owl_jwrite { |
---|
[38ffdf9] | 523 | my $body = shift; |
---|
| 524 | |
---|
| 525 | my $j = new Net::XMPP::Message; |
---|
| 526 | $body =~ s/\n\z//; |
---|
[b405ff6] | 527 | $j->SetMessage( |
---|
| 528 | to => $vars{jwrite}{to}, |
---|
| 529 | from => $vars{jwrite}{from}, |
---|
| 530 | type => $vars{jwrite}{type}, |
---|
| 531 | body => $body |
---|
| 532 | ); |
---|
| 533 | $j->SetThread( $vars{jwrite}{thread} ) if ( $vars{jwrite}{thread} ); |
---|
| 534 | $j->SetSubject( $vars{jwrite}{subject} ) if ( $vars{jwrite}{subject} ); |
---|
| 535 | |
---|
| 536 | my $m = j2o( $j, 'out' ); |
---|
| 537 | if ( $vars{jwrite}{type} ne 'groupchat' ) { |
---|
| 538 | |
---|
| 539 | #XXX TODO: Check for displayoutgoing. |
---|
| 540 | owl::queue_message($m); |
---|
[38ffdf9] | 541 | } |
---|
[b405ff6] | 542 | $connections->{ $vars{jwrite}{from} }->{client}->Send($j); |
---|
[6a6dd47] | 543 | delete $vars{jwrite}; |
---|
[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 | |
---|
[b6a253c] | 615 | $props{recipient} = $to->GetJID('base'); |
---|
| 616 | $props{sender} = $from->GetJID('base'); |
---|
[b405ff6] | 617 | $props{subject} = $j->GetSubject() if ( $j->DefinedSubject() ); |
---|
| 618 | $props{thread} = $j->GetThread() if ( $j->DefinedThread() ); |
---|
| 619 | $props{body} = $j->GetBody() if ( $j->DefinedBody() ); |
---|
| 620 | $props{error} = $j->GetError() if ( $j->DefinedError() ); |
---|
| 621 | $props{error_code} = $j->GetErrorCode() if ( $j->DefinedErrorCode() ); |
---|
[b6a253c] | 622 | $props{xml} = $j->GetXML(); |
---|
[38ffdf9] | 623 | |
---|
[b405ff6] | 624 | if ( $jtype eq 'chat' ) { |
---|
| 625 | $props{replycmd} = |
---|
| 626 | "jwrite " . ( ( $dir eq 'in' ) ? $props{from} : $props{to} ); |
---|
| 627 | $props{replycmd} .= |
---|
| 628 | " -a " . ( ( $dir eq 'out' ) ? $props{from} : $props{to} ); |
---|
| 629 | $props{isprivate} = 1; |
---|
[38ffdf9] | 630 | } |
---|
[b405ff6] | 631 | elsif ( $jtype eq 'groupchat' ) { |
---|
| 632 | my $nick = $props{nick} = $from->GetResource(); |
---|
| 633 | my $room = $props{room} = $from->GetJID('base'); |
---|
| 634 | $props{replycmd} = "jwrite -g $room"; |
---|
| 635 | $props{replycmd} .= |
---|
| 636 | " -a " . ( ( $dir eq 'out' ) ? $props{from} : $props{to} ); |
---|
| 637 | |
---|
| 638 | $props{sender} = $nick || $room; |
---|
| 639 | $props{recipient} = $room; |
---|
| 640 | |
---|
| 641 | if ( $props{subject} && !$props{body} ) { |
---|
| 642 | $props{body} = |
---|
| 643 | '[' . $nick . " has set the topic to: " . $props{subject} . "]"; |
---|
| 644 | } |
---|
[b6a253c] | 645 | } |
---|
[b405ff6] | 646 | elsif ( $jtype eq 'normal' ) { |
---|
| 647 | $props{replycmd} = undef; |
---|
| 648 | $props{isprivate} = 1; |
---|
[b6a253c] | 649 | } |
---|
[b405ff6] | 650 | elsif ( $jtype eq 'headline' ) { |
---|
| 651 | $props{replycmd} = undef; |
---|
[b6a253c] | 652 | } |
---|
[b405ff6] | 653 | elsif ( $jtype eq 'error' ) { |
---|
| 654 | $props{replycmd} = undef; |
---|
| 655 | $props{body} = "Error " |
---|
| 656 | . $props{error_code} |
---|
| 657 | . " sending to " |
---|
| 658 | . $props{from} . "\n" |
---|
| 659 | . $props{error}; |
---|
| 660 | } |
---|
| 661 | |
---|
[38ffdf9] | 662 | $props{replysendercmd} = $props{replycmd}; |
---|
[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 | } |
---|