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