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