From: Manni H. <man...@us...> - 2008-04-10 15:17:41
|
Update of /cvsroot/popfile/engine/Services In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2153/Services Modified Files: IMAP.pm Log Message: 1. Make the IMAP module aware of multi-user mode. - Get an administrator session key in service() - get a list of users in the DB - Keep track of the user-id that the module is currently working with. TODO: - Add a per-user configuration variable that let's users en/disable the use of IMAP. - Check this variable in service(). 2. Update the IMAP tests in the test suite. - Get an administrato session key - Correctly call History::get_slot_fields(). => The tests now run without errors TODO: Make this work in multi-user mode. Index: IMAP.pm =================================================================== RCS file: /cvsroot/popfile/engine/Services/IMAP.pm,v retrieving revision 1.35 retrieving revision 1.36 diff -C2 -d -r1.35 -r1.36 *** IMAP.pm 5 Apr 2008 11:02:20 -0000 1.35 --- IMAP.pm 10 Apr 2008 15:17:35 -0000 1.36 *************** *** 210,263 **** my $self = shift; ! if ( time - $self->{last_update__} >= $self->user_config_( 1, 'update_interval' ) ) { ! # Since the IMAP-Client module can throw an exception, i.e. die if ! # it detects a lost connection, we eval the following code to be able ! # to catch the exception. We also tell Perl to ignore broken pipes. ! eval { ! local $SIG{'PIPE'} = 'IGNORE'; ! local $SIG{'__DIE__'}; ! if ( $self->user_config_( 1, 'training_mode' ) == 1 ) { ! $self->train_on_archive__(); ! } ! else { ! # If we haven't yet set up a list of serviced folders, ! # or if the list was changed by the user, build up a ! # list of folder in $self->{folders__} ! if ( ( keys %{$self->{folders__}} == 0 ) || ( $self->{folder_change_flag__} == 1 ) ) { ! $self->build_folder_list__(); ! } ! $self->connect_server__(); ! # Reset the hash containing the hash values we have seen the ! # last time through service. ! $self->{hash_values__} = (); ! # Now do the real job ! foreach my $folder ( keys %{$self->{folders__}} ) { ! $self->scan_folder( $folder ) if exists $self->{folders__}{$folder}{imap}; ! } ! } ! }; ! # if an exception occurred, we try to catch it here ! if ( $@ ) { ! $self->disconnect_folders__(); ! # If we caught an exception, we better reset training_mode ! $self->user_config_( 1, 'training_mode', 0 ); ! # say__() and get_response__() will die with this message: ! if ( $@ =~ /^POPFILE-IMAP-EXCEPTION: (.+\)\))/ ) { ! $self->log_( 0, $1 ); ! } ! # If we didn't die but somebody else did, we have empathy. ! else { ! die $@; ! } ! } ! # Save the current time. ! $self->{last_update__} = time; } --- 210,274 ---- my $self = shift; ! # We have to get a session key first, then loop over all users that ! # wish to use our services. ! my $sessionKey = $self->classifier_()->get_administrator_session_key(); ! my $users = $self->classifier_()->get_user_list( $sessionKey ); ! foreach my $userId ( keys %{$users} ) { ! $self->log_( 1, "Checking user $users->{$userId} for IMAP settings." ); ! $self->user_id( $userId ); ! if ( time - $self->{last_update__} >= $self->user_config_( $userId, 'update_interval' ) ) { ! # Since the IMAP-Client module can throw an exception, i.e. die if ! # it detects a lost connection, we eval the following code to be able ! # to catch the exception. We also tell Perl to ignore broken pipes. ! eval { ! local $SIG{'PIPE'} = 'IGNORE'; ! local $SIG{'__DIE__'}; ! if ( $self->user_config_( $userId, 'training_mode' ) == 1 ) { ! $self->train_on_archive__(); ! } ! else { ! # If we haven't yet set up a list of serviced folders, ! # or if the list was changed by the user, build up a ! # list of folder in $self->{folders__} ! if ( ( keys %{$self->{folders__}} == 0 ) || ( $self->{folder_change_flag__} == 1 ) ) { ! $self->build_folder_list__(); ! } ! $self->connect_server__(); ! ! # Reset the hash containing the hash values we have seen the ! # last time through service. ! $self->{hash_values__} = (); ! ! # Now do the real job ! foreach my $folder ( keys %{$self->{folders__}} ) { ! $self->scan_folder( $folder ) if exists $self->{folders__}{$folder}{imap}; ! } ! } ! }; ! # if an exception occurred, we try to catch it here ! if ( $@ ) { ! $self->disconnect_folders__(); ! # If we caught an exception, we better reset training_mode ! $self->user_config_( $userId, 'training_mode', 0 ); ! ! # say__() and get_response__() will die with this message: ! if ( $@ =~ /^POPFILE-IMAP-EXCEPTION: (.+\)\))/ ) { ! $self->log_( 0, $1 ); ! } ! # If we didn't die but somebody else did, we have empathy. ! else { ! die $@; ! } ! } ! # Save the current time. ! $self->{last_update__} = time; ! } } *************** *** 559,563 **** # After we are done with the folder, we issue an EXPUNGE command # if we were told to do so. ! if ( $moved_message && $self->user_config_( 1, 'expunge' ) ) { $imap->expunge(); } --- 570,574 ---- # After we are done with the folder, we issue an EXPUNGE command # if we were told to do so. ! if ( $moved_message && $self->user_config_( $self->user_id(), 'expunge' ) ) { $imap->expunge(); } *************** *** 775,779 **** my $folder = shift; ! my $all = $self->user_config_( 1, 'bucket_folder_mappings' ); my %mapping = split /$cfg_separator/, $all; --- 786,790 ---- my $folder = shift; ! my $all = $self->user_config_( $self->user_id(), 'bucket_folder_mappings' ); my %mapping = split /$cfg_separator/, $all; *************** *** 786,790 **** $all .= "$k$cfg_separator$v$cfg_separator"; } ! $self->user_config_( 1, 'bucket_folder_mappings', $all ); } # get --- 797,801 ---- $all .= "$k$cfg_separator$v$cfg_separator"; } ! $self->user_config_( $self->user_id(), 'bucket_folder_mappings', $all ); } # get *************** *** 813,817 **** my @folders = @_; ! my $all = $self->user_config_( 1, 'watched_folders' ); # set --- 824,828 ---- my @folders = @_; ! my $all = $self->user_config_( $self->user_id(), 'watched_folders' ); # set *************** *** 821,825 **** $all .= "$_$cfg_separator"; } ! $self->user_config_( 1, 'watched_folders', $all ); } # get --- 832,836 ---- $all .= "$_$cfg_separator"; } ! $self->user_config_( $self->user_id(), 'watched_folders', $all ); } # get *************** *** 841,844 **** --- 852,857 ---- if ( ! $self->{api_session__} ) { + # TODO: Where the hell should I get a session from?? + # my $user = $self->classifier_()->valid_session_key__( $session ); $self->{api_session__} = $self->classifier_()->get_session_key( 'admin', '' ); } *************** *** 847,850 **** --- 860,879 ---- } + #---------------------------------------------------------------------------- + # + # user_id + # + # Get or set the id of the user that we are currently working for. + #---------------------------------------------------------------------------- + + sub user_id { + my $self = shift; + + if ( @_ ) { + $self->{ current_userid } = shift; + } + + return $self->{ current_userid }; + } #---------------------------------------------------------------------------- *************** *** 1032,1042 **** my $language = shift; # conection details if ( $name eq 'imap_0_connection_details' ) { ! $templ->param( 'IMAP_hostname', $self->user_config_( 1, 'hostname' ) ); ! $templ->param( 'IMAP_port', $self->user_config_( 1, 'port' ) ); ! $templ->param( 'IMAP_login', $self->user_config_( 1, 'login' ) ); ! $templ->param( 'IMAP_password', $self->user_config_( 1, 'password' ) ); ! $templ->param( 'IMAP_ssl_checked', $self->user_config_( 1, 'use_ssl' ) ? 'checked="checked"' : '' ); } --- 1061,1074 ---- my $language = shift; + my $userId = $self->user_id(); + # conection details if ( $name eq 'imap_0_connection_details' ) { ! ! $templ->param( 'IMAP_hostname', $self->user_config_( $userId, 'hostname' ) ); ! $templ->param( 'IMAP_port', $self->user_config_( $userId, 'port' ) ); ! $templ->param( 'IMAP_login', $self->user_config_( $userId, 'login' ) ); ! $templ->param( 'IMAP_password', $self->user_config_( $userId, 'password' ) ); ! $templ->param( 'IMAP_ssl_checked', $self->user_config_( $userId, 'use_ssl' ) ? 'checked="checked"' : '' ); } *************** *** 1158,1162 **** # Read the list of mailboxes from the server. Now! if ( $name eq 'imap_4_update_mailbox_list' ) { ! if ( $self->user_config_( 1, 'hostname' ) eq '' ) { $templ->param( IMAP_if_connection_configured => 0 ); } --- 1190,1194 ---- # Read the list of mailboxes from the server. Now! if ( $name eq 'imap_4_update_mailbox_list' ) { ! if ( $self->user_config_( $userId, 'hostname' ) eq '' ) { $templ->param( IMAP_if_connection_configured => 0 ); } *************** *** 1170,1183 **** # Are we expunging after moving messages? ! my $checked = $self->user_config_( 1, 'expunge' ) ? 'checked="checked"' : ''; $templ->param( IMAP_expunge_is_checked => $checked ); # Update interval in seconds ! $templ->param( IMAP_interval => $self->user_config_( 1, 'update_interval' ) ); } # Switch the module to training mode if ( $name eq 'imap_6_training' ) { ! $templ->param( imap_currently_training => $self->user_config_( 1, 'training_mode' ) ); } } --- 1202,1215 ---- # Are we expunging after moving messages? ! my $checked = $self->user_config_( $userId, 'expunge' ) ? 'checked="checked"' : ''; $templ->param( IMAP_expunge_is_checked => $checked ); # Update interval in seconds ! $templ->param( IMAP_interval => $self->user_config_( $userId, 'update_interval' ) ); } # Switch the module to training mode if ( $name eq 'imap_6_training' ) { ! $templ->param( imap_currently_training => $self->user_config_( $userId, 'training_mode' ) ); } } *************** *** 1223,1227 **** if ( $name eq 'imap_6_training' ) { if ( defined $form->{do_imap_training} ) { ! $self->user_config_( 1, 'training_mode', 1 ); return ( $language->{Imap_DoingTraining}, undef ); } --- 1255,1259 ---- if ( $name eq 'imap_6_training' ) { if ( defined $form->{do_imap_training} ) { ! $self->user_config_( $self->user_id(), 'training_mode', 1 ); return ( $language->{Imap_DoingTraining}, undef ); } *************** *** 1240,1249 **** my ( $status_message, $error_message ); if ( defined $form->{update_imap_0_connection_details} ) { my $something_happened = 0; if ( $form->{imap_hostname} && $form->{imap_hostname} =~ /^\S+/ ) { ! if ( $self->user_config_( 1, 'hostname' ) ne $form->{imap_hostname} ) { ! $self->user_config_( 1, 'hostname', $form->{imap_hostname} ); $something_happened++; } --- 1272,1283 ---- my ( $status_message, $error_message ); + my $userId = $self->user_id(); + if ( defined $form->{update_imap_0_connection_details} ) { my $something_happened = 0; if ( $form->{imap_hostname} && $form->{imap_hostname} =~ /^\S+/ ) { ! if ( $self->user_config_( $userId, 'hostname' ) ne $form->{imap_hostname} ) { ! $self->user_config_( $userId, 'hostname', $form->{imap_hostname} ); $something_happened++; } *************** *** 1254,1259 **** if ( $form->{imap_port} && $form->{imap_port} =~ m/^\d+$/ && $form->{imap_port} >= 1 && $form->{imap_port} < 65536 ) { ! if ( $self->user_config_( 1, 'port' ) != $form->{imap_port} ) { ! $self->user_config_( 1, 'port', $form->{imap_port} ); $something_happened++; } --- 1288,1293 ---- if ( $form->{imap_port} && $form->{imap_port} =~ m/^\d+$/ && $form->{imap_port} >= 1 && $form->{imap_port} < 65536 ) { ! if ( $self->user_config_( $userId, 'port' ) != $form->{imap_port} ) { ! $self->user_config_( $userId, 'port', $form->{imap_port} ); $something_happened++; } *************** *** 1264,1269 **** if ( defined $form->{imap_login} && $form->{imap_login} =~ /^\S/ ) { ! if ( $self->user_config_( 1, 'login' ) ne $form->{imap_login} ) { ! $self->user_config_( 1, 'login', $form->{imap_login} ); $something_happened++; } --- 1298,1303 ---- if ( defined $form->{imap_login} && $form->{imap_login} =~ /^\S/ ) { ! if ( $self->user_config_( $userId, 'login' ) ne $form->{imap_login} ) { ! $self->user_config_( $userId, 'login', $form->{imap_login} ); $something_happened++; } *************** *** 1274,1279 **** if ( defined $form->{imap_password} && $form->{imap_password} =~ /^\S/ ) { ! if ( $self->user_config_( 1, 'password' ) ne $form->{imap_password} ) { ! $self->user_config_( 1, 'password', $form->{imap_password} ); $something_happened++; } --- 1308,1313 ---- if ( defined $form->{imap_password} && $form->{imap_password} =~ /^\S/ ) { ! if ( $self->user_config_( $userId, 'password' ) ne $form->{imap_password} ) { ! $self->user_config_( $userId, 'password', $form->{imap_password} ); $something_happened++; } *************** *** 1283,1289 **** } ! my $use_ssl_now = $self->user_config_( 1, 'use_ssl' ); if ( $form->{imap_use_ssl} ) { ! $self->user_config_( 1, 'use_ssl', 1 ); if ( ! $use_ssl_now ) { $something_happened = 1; --- 1317,1323 ---- } ! my $use_ssl_now = $self->user_config_( $userId, 'use_ssl' ); if ( $form->{imap_use_ssl} ) { ! $self->user_config_( $userId, 'use_ssl', 1 ); if ( ! $use_ssl_now ) { $something_happened = 1; *************** *** 1291,1295 **** } else { ! $self->user_config_( 1, 'use_ssl', 0 ); if ( $use_ssl_now ) { $something_happened = 1; --- 1325,1329 ---- } else { ! $self->user_config_( $userId, 'use_ssl', 0 ); if ( $use_ssl_now ) { $something_happened = 1; *************** *** 1405,1414 **** my ( $status_message, $error_message ); if ( defined $form->{do_imap_4_update_mailbox_list} ) { ! if ( $self->user_config_( 1, 'hostname' ) ! && $self->user_config_( 1, 'login' ) ! && $self->user_config_( 1, 'login' ) ! && $self->user_config_( 1, 'port' ) ! && $self->user_config_( 1, 'password' ) ) { my $imap = $self->new_imap_client(); --- 1439,1450 ---- my ( $status_message, $error_message ); + my $userId = $self->user_id(); + if ( defined $form->{do_imap_4_update_mailbox_list} ) { ! if ( $self->user_config_( $userId, 'hostname' ) ! && $self->user_config_( $userId, 'login' ) ! && $self->user_config_( $userId, 'login' ) ! && $self->user_config_( $userId, 'port' ) ! && $self->user_config_( $userId, 'password' ) ) { my $imap = $self->new_imap_client(); *************** *** 1445,1455 **** if ( defined $form->{update_imap_5_options} ) { # expunge or not? if ( defined $form->{imap_options_expunge} ) { ! $self->user_config_( 1, 'expunge', 1 ); } else { ! $self->user_config_( 1, 'expunge', 0 ); } --- 1481,1492 ---- if ( defined $form->{update_imap_5_options} ) { + my $userId = $self->user_id(); # expunge or not? if ( defined $form->{imap_options_expunge} ) { ! $self->user_config_( $userId, 'expunge', 1 ); } else { ! $self->user_config_( $userId, 'expunge', 0 ); } *************** *** 1457,1461 **** my $form_interval = $form->{imap_options_update_interval}; if ( $form_interval =~ /^\d+$/ && $form_interval >= 10 && $form_interval <= 60*60 ) { ! $self->user_config_( 1, 'update_interval', $form_interval ); } else { --- 1494,1498 ---- my $form_interval = $form->{imap_options_update_interval}; if ( $form_interval =~ /^\d+$/ && $form_interval >= 10 && $form_interval <= 60*60 ) { ! $self->user_config_( $userId, 'update_interval', $form_interval ); } else { *************** *** 1546,1550 **** # And disable training mode so we won't do this again the next time service is called. ! $self->user_config_( 1, 'training_mode', 0 ); } --- 1583,1587 ---- # And disable training mode so we won't do this again the next time service is called. ! $self->user_config_( $self->user_id(), 'training_mode', 0 ); } *************** *** 1568,1572 **** my $imap = Services::IMAP::Client->new( ! sub { $self->user_config_( 1, @_ ) }, $self->get_module__( 'logger', 'POPFile::Logger' ), sub { $self->global_config_( @_ ) }, --- 1605,1609 ---- my $imap = Services::IMAP::Client->new( ! sub { $self->user_config_( $self->user_id(), @_ ) }, $self->get_module__( 'logger', 'POPFile::Logger' ), sub { $self->global_config_( @_ ) }, |