Changeset 84296f6
- Timestamp:
- Nov 14, 2006, 9:44:50 AM (18 years ago)
- Branches:
- master, barnowl_perlaim, debian, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
- Children:
- 99dabee
- Parents:
- 6e9e50e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
perl/modules/jabber.pl
r6e9e50e r84296f6 8 8 use Net::DNS; 9 9 use Getopt::Long; 10 11 no warnings 'redefine'; 10 12 11 13 ################################################################################ … … 20 22 # Implementing formatting and logging callbacks for C 21 23 # Appropriate callbacks for presence subscription messages. 22 # * Current behavior =>auto-accept (default for Net::Jabber)24 # * Current behavior is auto-accept (default for Net::Jabber) 23 25 # 24 26 ################################################################################ … … 34 36 } 35 37 else { 36 37 38 # Our owl doesn't support queue_message. Unfortunately, this 38 39 # means it probably *also* doesn't support owl::error. So just … … 77 78 } 78 79 else { 79 $blistStr .= $jq{ask} ? " [pending]" : " [offline]"; 80 } 81 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 } 89 } 82 90 return $blistStr . "\n"; 91 } 92 93 sub getSingleBuddyList { 94 my $jid = shift; 95 $jid = resolveJID($jid); 96 return "" unless $jid; 97 my $blist = ""; 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 ); 106 } 107 } 108 109 my @unsorted = $$roster->jids('nogroup'); 110 if (@unsorted) { 111 $blist .= " [unsorted]\n"; 112 foreach my $buddy (@unsorted) { 113 $blist .= blist_listBuddy( $roster, $buddy ); 114 } 115 } 116 } 117 return $blist; 83 118 } 84 119 85 120 sub onGetBuddyList { 86 121 my $blist = ""; 87 foreach my $jid ( keys %{$connections} ) { 88 my $roster = \$connections->{$jid}->{roster}; 89 if ($$roster) { 90 $blist .= "\n" . boldify("Jabber Roster for $jid\n"); 91 92 foreach my $group ( $$roster->groups() ) { 93 $blist .= " Group: $group\n"; 94 foreach my $buddy ( $$roster->jids( 'group', $group ) ) { 95 $blist .= blist_listBuddy( $roster, $buddy ); 96 } 97 } 98 99 my @unsorted = $$roster->jids('nogroup'); 100 if (@unsorted) { 101 $blist .= " [unsorted]\n"; 102 foreach my $buddy (@unsorted) { 103 $blist .= blist_listBuddy( $roster, $buddy ); 104 } 105 } 106 } 122 foreach my $jid (keys %{$connections}) { 123 $blist .= getSingleBuddyList($jid); 107 124 } 108 125 return $blist; … … 177 194 my ( $server, $port ) = getServerFromJID($jid); 178 195 179 $connections->{$jidStr}->{client} = Net::Jabber::Client->new( 180 debuglevel => owl::getvar('debug') eq 'on' ? 1 : 0, 181 debugfile => 'jabber.log' 182 ); 183 my $client = \$connections->{$jidStr}->{client}; 184 $connections->{$jidStr}->{roster} = 185 $connections->{$jidStr}->{client}->Roster(); 186 187 #XXX Todo: Add more callbacks. 188 # MUC presence handlers 189 $$client->SetMessageCallBacks( 190 chat => sub { owl_jabber::process_incoming_chat_message(@_) }, 191 error => sub { owl_jabber::process_incoming_error_message(@_) }, 192 groupchat => sub { owl_jabber::process_incoming_groupchat_message(@_) }, 193 headline => sub { owl_jabber::process_incoming_headline_message(@_) }, 194 normal => sub { owl_jabber::process_incoming_normal_message(@_) } 195 ); 196 196 $vars{jlogin_jid} = $jidStr; 197 $vars{jlogin_havepass} = 0; 197 198 $vars{jlogin_connhash} = { 198 199 hostname => $server, … … 201 202 componentname => $componentname 202 203 }; 203 204 my $status = $$client->Connect( %{ $vars{jlogin_connhash} } );205 206 if ( !$status ) {207 delete $connections->{$jidStr};208 delete $vars{jlogin_connhash};209 owl::error("We failed to connect");210 return "";211 }212 213 204 $vars{jlogin_authhash} = 214 { username => $uid, resource => $resource, password => '' }; 215 my @result = $$client->AuthSend( %{ $vars{jlogin_authhash} } ); 216 if ( $result[0] ne 'ok' ) { 217 if ( $result[1] == 401 ) { 218 $vars{jlogin_jid} = $jidStr; 205 { username => $uid, 206 resource => $resource, 207 }; 208 209 return do_login(''); 210 } 211 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}) { 217 owl::error("Got password but have no jid!"); 218 } 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 ) { 219 244 delete $connections->{$jidStr}; 220 owl::start_password( "Password for $jidStr: ", \&do_login_with_pw ); 221 return ""; 222 } 223 owl::error( 224 "Error in connect: " . join( " ", $result[ 1 .. $#result ] ) ); 225 do_logout($jidStr); 226 delete $vars{jlogin_connhash}; 227 delete $vars{jlogin_authhash}; 228 return ""; 229 } 230 $connections->{$jidStr}->{roster}->fetch(); 231 $$client->PresenceSend( priority => 1 ); 232 queue_admin_msg("Connected to jabber as $jidStr"); 233 delete $vars{jlogin_connhash}; 234 delete $vars{jlogin_authhash}; 235 return ""; 236 } 237 238 sub do_login_with_pw { 239 $vars{jlogin_authhash}->{password} = shift; 240 my $jidStr = delete $vars{jlogin_jid}; 241 if ( !$jidStr ) { 242 owl::error("Got password but have no jid!"); 243 } 244 245 $connections->{$jidStr}->{client} = Net::Jabber::Client->new(); 246 my $client = \$connections->{$jidStr}->{client}; 247 $connections->{$jidStr}->{roster} = 248 $connections->{$jidStr}->{client}->Roster(); 249 250 $$client->SetMessageCallBacks( 251 chat => sub { owl_jabber::process_incoming_chat_message(@_) }, 252 error => sub { owl_jabber::process_incoming_error_message(@_) }, 253 groupchat => sub { owl_jabber::process_incoming_groupchat_message(@_) }, 254 headline => sub { owl_jabber::process_incoming_headline_message(@_) }, 255 normal => sub { owl_jabber::process_incoming_normal_message(@_) } 256 ); 257 258 my $status = $$client->Connect( %{ $vars{jlogin_connhash} } ); 259 if ( !$status ) { 260 delete $connections->{$jidStr}; 261 delete $vars{jlogin_connhash}; 262 delete $vars{jlogin_authhash}; 263 owl::error("We failed to connect"); 264 return ""; 265 } 266 267 my @result = $$client->AuthSend( %{ $vars{jlogin_authhash} } ); 268 269 if ( $result[0] ne 'ok' ) { 270 owl::error( 271 "Error in connect: " . join( " ", $result[ 1 .. $#result ] ) ); 272 do_logout($jidStr); 273 delete $vars{jlogin_connhash}; 274 delete $vars{jlogin_authhash}; 275 return ""; 276 } 277 278 $connections->{$jidStr}->{roster}->fetch(); 279 $$client->PresenceSend( priority => 1 ); 280 queue_admin_msg("Connected to jabber as $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 } 267 } 268 delete $vars{jlogin_jid}; 269 $vars{jlogin_password} =~ tr/\0-\377/x/; 270 delete $vars{jlogin_password}; 271 delete $vars{jlogin_havepass}; 281 272 delete $vars{jlogin_connhash}; 282 273 delete $vars{jlogin_authhash}; … … 292 283 293 284 sub cmd_logout { 294 295 285 # Logged into multiple accounts 296 286 if ( connected() > 1 ) { 297 298 287 # Logged into multiple accounts, no accout specified. 299 288 if ( !$_[1] ) { 300 289 my $errStr = 301 "You are logged into multiple accounts. Please specify an account to log out of.\n";290 "You are logged into multiple accounts. Please specify an account to log out of.\n"; 302 291 foreach my $jid ( keys %$connections ) { 303 292 $errStr .= "\t$jid\n"; … … 305 294 queue_admin_msg($errStr); 306 295 } 307 308 296 # Logged into multiple accounts, account specified. 309 297 else { … … 323 311 else # Only one account logged in. 324 312 { 325 326 313 do_logout( ( keys %$connections )[0] ); 327 314 } … … 707 694 } 708 695 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 709 703 sub resolveJID { 710 704 my $givenJidStr = shift; … … 752 746 } 753 747 754 # Log outthis one.748 # It's this one. 755 749 else { 756 750 return $matchingJid; … … 759 753 return ""; 760 754 } 755 756 1;
Note: See TracChangeset
for help on using the changeset viewer.