Koozali.org: home of the SME Server

Debugging Perl in email-ipup script

John Crisp

Debugging Perl in email-ipup script
« on: November 18, 2002, 10:13:19 PM »
Hi,

Have posted on this before without success.

http://forums.contribs.org/index.php?topic=15657.msg60278#msg60278

Hope someone can help on the following :

I have been having a bit of a mail problem in my 5.1.2 server which seems to emanate from the email-ipup script. Unfortunately I am no Perl programmer and wondered if anyone could shed some light on this. I have posted to the forums but have no reply.

It seems that the call to startmail via crontab works OK to run the timed mail runs, but when the server tries to run email-ipup when it connects, the email-ipup script fails.

As far as I can tell it doesn't seem to correctly pull the Method key from the fetchmail line in the database - in my case it should return multidrop:

my $EmailRetrieval = db_get_prop(\%conf, "fetchmail", 'Method');

return unless (defined $properties{'EmailRetrieval'});

If I insert "use diagnostics;" and a line to print the $EmailRetrieval variable to check what it is :

print "  $properties{EmailRetrieval}\n";

I get the following error :


[root@wingate actions]# ./email-ipup
Use of uninitialized value in concatenation (.) at ./email-ipup line 65 (#1)

    (W uninitialized) An undefined value was used as if it were already defined.  It was
    interpreted as a "" or a 0, but maybe it was a mistake.  To suppress this
    warning assign a defined value to your variables.

Line 65 is the print statement.

I presume that this means that the variable is not being correctly obtained (or my print statement is wrong, although I have used it at other points in the script to check variables and it works fine).

Can someone tell me what is happening  or how to debug it ?

/sbin/e-smith/db configuration getprop fetchmail Method at the command prompt shows : multidrop = enabled

Is there a way to do a "print db_get_prop(\%conf, "fetchmail", 'Method');" in the script to see the result ?

Yours in desperation...........

B. Rgds
John

Nathan Fowler

Re: Debugging Perl in email-ipup script
« Reply #1 on: November 19, 2002, 04:06:43 AM »
" $properties{EmailRetrieval}\n";

is not the same as:

" $properties{'EmailRetrieval'}\n";

The error is being produced because EmailRetrieval is being treated as a variable, which consequently is not being refrenced, because you are not including the single quoutes around the property name.  'EmailRetrieval' is a string value, EmailRetrieval is a variable.

Your debugging line should read as:
print $properties{'EmailRetrieval'} . "\n";



I doubt that helps your entire situation, but at least that should help you explain the error on line 64.

Hope this helped,
Nathan

Greg Zartman

Re: Debugging Perl in email-ipup script
« Reply #2 on: November 19, 2002, 04:26:32 AM »
> " $properties{EmailRetrieval}\n";
> is not the same as:
> " $properties{'EmailRetrieval'}\n";

They most certainly are the same.  The first is sloppy syntax for a hash element that perl will happily evaluates for you.  The second is correct hash syntax for the same hash element.   In both cases, EmailRetrival is treated as a simple scalar, not a variable.

It's a bit difficult to tell what the problem is with only the few lines of code presented.  However, it appears to me that John is wanting to pull a hash element named by the value of the EmailRetrieval variable.  If this is the case, he needs to preceed EmailRetrieval with a $.  

What $properties{''}?  Is it an object or something.  You shouldn't be getting the error you received from an print statment containing a variable with a null value.

Greg

Nathan Fowler

Re: Debugging Perl in email-ipup script
« Reply #3 on: November 19, 2002, 04:28:46 AM »
Thanks Greg, I stand corrected.

Nathan

John Crisp

Re: Debugging Perl in email-ipup script
« Reply #4 on: November 19, 2002, 07:16:45 PM »
<> ;-)

Sorry - this is quite long - I have tried to include all the info on what I have done. Please bear with me.

Setup : 5.1.2. SP3 server. ISDN dialup. relevant add ons include Stephen Noble's multipop and Kami Svetli's e-smith-dialup rpms - from what I can see I don't think that they change anything here though.

I originally wanted to jigger around with mail collection times. In looking at the logs and seeing what was happening, I noticed that mail was being collected on the crontab schedule, but not when the line went up. Often problems such as this have been attributed to fetchmail problems, but I wasn’t so sure in this case. Why was it collecting mail if crontab runs /etc/startmail, but not via the email-ipup script ?

When the line goes up, the mail collection is run from the email-ipup script in /etc/e-smith/events/actions – complete script below. So I decided to have a mosey and see what’s happening.

The script all seems to run fine apart from the snips that I showed you. The part where the script keeps blowing out is as described previously and as below. The EmailRetrieval variable is set when it checks to see the mail retrieval method from fetchmail in the config database. In my case it should return the word multidrop but doesn’t appear too.

The line

return unless (defined $properties{'EmailRetrieval'});  

does return, presumably because it is not parsing a value from the database and hence the error message generated when I add a print statement and run it manually. The script returns from here, and fetchmail is not run.

I was using the print statement to try and debug where the script was failing. For instance I put a line

Print  “  $properties{status}\n”

In here :

return unless (defined $properties{'status'});

Print  “  $properties{status}\n”  # My hacked line

    return unless ($properties{'status'} eq "enabled");

to see what value it returned. This worked OK (I did say that I’m no Perl programmer – I just read round a bit and had a go) and gives the result 'enabled' at the command line. This how I found that the EmailRetrieval seems to be failing.

I have now amended the relevant part of the script too :

my $EmailRetrieval = db_get_prop(\%conf, "fetchmail", 'Method');

print $properties{'EmailRetrieval'} . "\n"; # this is line 65

    return unless (defined $properties{'EmailRetrieval'});
    return unless ($properties{'EmailRetrieval'} =~ /^(etrn|multidrop)$/);


This now gives the following error :

Use of uninitialized value in concatenation (.) at ./email-ipup line 65.

I guess that this means that $EmailRetrieval = db_get_prop(\%conf, "fetchmail", 'Method');  is still not returning a value when it should.


FYI, to check that the key is correct I have used :

/sbin/e-smith/db configuration printprop fetchmail Method

This returns

Method=multidrop


Hope this makes sense. I have wracked my brains but cannot suss it out. Completely bemused.


B. Rgds,  John



The whole script is below.

/etc/e-smith/events/actions/email-ipup

#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 2001 Mitel Networks Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#       
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#       
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
# Technical support for this program is available from Mitel Networks
# Please visit our web site www.e-smith.com for details.
#----------------------------------------------------------------------
use strict;

package esmith;
sub fetchmailIfRequired();

use Errno;
use esmith::config;
use esmith::db;

#------------------------------------------------------------
# Reset qmail TCP timeouts, and tell qmail-send to retry sending
#------------------------------------------------------------

system("/var/qmail/bin/qmail-tcpok");
system("/usr/bin/killall", "-ALRM", "qmail-send");

fetchmailIfRequired();

exit (0);

#------------------------------------------------------------
# Trigger fetchmail if the service is enabled
#------------------------------------------------------------
sub fetchmailIfRequired()
{
    tie my %conf, 'esmith::config';

    my ($type, %properties) = db_get(\%conf, 'fetchmail');

    return if (exists $properties{'NoIPUP'});

    return unless (defined $properties{'status'});
    return unless ($properties{'status'} eq "enabled");

# All Ok to here. Now we have problems.

    my $EmailRetrieval = db_get_prop(\%conf, "fetchmail", 'Method');

# here lies demons, and broken scripts. Above does not return method so
# the next line sends us to jail without passing Go.
   
     return unless (defined $properties{'EmailRetrieval'});

# We never make it this far unless the above line is hashed out. We then have to hash the next
# as well.

    return unless ($properties{'EmailRetrieval'} =~ /^(etrn|multidrop)$/);

# the following works fine and dandy

    return if ( ($properties{'FreqOffice'}  eq 'never') and
                ($properties{'FreqOutside'} eq 'never') and
                ($properties{'FreqWeekend'} eq 'never') );

    system("/etc/fetchmail");
}