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