I'm not familiar with "notes email" or the Win32::OLE module you're  
using, so it's unlikely that I can solve your problem.  I do have a few  
general observations to offer though.

First, checking something like e-mail every one second, may border on  
obsessive compulsive behavior!  <laughs>  Seriously, I do think this  
time loop is a bad idea.  This could potentially be a pretty  
significant resource drain on the computer doing the checking not to  
mention the checked server.  My server would ban me for "flooding"  
shortly after I started something like this.  If you need e-mail  
service with less than a five minute delay, I think it's time to  
consider a different form of communication that would meet your needs  
better.

Second, goto programming is BAD, BAD, BAD!  It creates what is often  
affectionately called "spaghetti code", where following the individual  
strands to their natural end can become maddening.  Your code is short  
and simple, and it's still hell to follow because of those gotos.  That  
makes it hard to get help, when you send your code to anyone.  A goto  
call is VERY rarely ever needed in modern programming languages, so  
unlearn it as soon as possible.

All the gotos in your script can be replaced with loops and  
subroutines.  You used a foreach loop at one point, so I assume you're  
at least passingly familiar with the idea.  Your main WAIT label could  
easily be replaced with a while loop, given a condition of 1, which  
will always evaluate to true and thus be infinite.  Then, when you need  
to get back to the top of the loop, as all those gotos function, just  
use a call to next.  For example:

while (1) {             # replaces WAIT:
        # do some processing
        next if CONDITION;              # this sends us back to the while
        # more processing, if next wasn't called
}       # end of loop, repeats back to the while

The other construct you can use to replace goto calls is a subroutine  
(or sub) to create a reusable chunk of code that you can call anywhere.  
  This is how I would replace your BADPASS label, for example:

sub badpass {           # doesn't execute at this point, just declares
        # processing...
        return if CONDITION;    # in a sub, return sends us back to the calling  
code
        # more processing, if we didn't return
}       # end of sub, return to calling code

# we can now call this code later with...

badpass();      # will execute the whole badpass sub, right here

You get the idea.  These are basic Perl concepts that would be in every  
book out there, but especially focused on in learning to program Perl  
type books.  Learning Perl from O'Reilly is very popular, if you would  
like to check one out.

Two other minor observations:  I question the accuracy of your 1 to 3  
foreach loop.  It seems like magic numbers chosen instead of based on  
criteria like the documents available.  I could be way off here though,  
since I don't really know what you're doing.  And two, use diagnostics  
enables warnings, so having both is redundant.

Good luck with your problem.

James

On Friday, October 11, 2002, at 09:31  AM, Chris Benco wrote:

> Below is a perl script I've been working on that logs into and checks a
> notes email account for new messages once every second.  When it finds  
> a
> new message it tests for a password, and if it success it process's the
> email.  The script will based on what is in the email either run  
> another
> script, or do anything that can be done form a command line on the  
> system
> it is running on.  It then clears the mailbox, then goes back into the  
> loop
> where it checks for any mail.  The original propose for this program  
> was to
> allow me to do some diagnostics remotely via email (usually my  
> blackberry
> handheld), but I've already found dozens of other uses for it.
>
> The problem is that the script is supposed to loop basically forever  
> until
> it finds something.  It does this for awhile.  The program works for  
> about
> 24 hours at which point it just stops.  I've tried changing to loop  
> time
> from 1 second to 10, and even 30 seconds, but it still crashes after  
> about
> 24 hours.  I've never managed to be around when this happens to try to
> figure out what is going on.  Looking for improving programs  
> stability, or
> even ideas on how to get it to log what is going wrong.  Only thing I  
> can
> think of is having it open a text file, and have every line in the  
> program
> log it's execution in some way.   I've also already added lines that  
> force
> it back into the waiting loop at almost every stage I can think of it
> possibly halting, but that has had little of any effect it seems.
>
> If anyone has any ideas on how to improve this programs stability, how  
> to
> make perl programs more stably in general, or even a possible reason  
> why
> this, or any general purpose program might crash for unknown reasons  
> let me
> know.  Looking for input of any kind on this issue.
>
> Have been thinking of writing some simple but very system specific  
> network
> monitoring apps using perl and I really need to prove to myself that I  
> can
> keep a perl script running for weeks at a time before I even start.
>
> ####################################################################### 
> #############################
>
> use strict;
> use warnings;
> use diagnostics;
> use Win32::OLE;
>
> #Notes database Info
> my $userid = "xxxxxx";
> my $server = "xxxxx/xxx/xxx";
>
> #Log into Notes database
> my $Notes = Win32::OLE->new('Notes.NotesSession');
> my $Database = $Notes->GetDatabase("$server", "mail\\$userid.nsf");
>
> #Loop a check for mail once every second, send a '.' to console for
> monitoring
> WAIT:
>     sleep 1;
>     print ".";
>     my $AllDocuments = $Database->AllDocuments;
>     my $Document = $AllDocuments->GetFirstDocument or goto WAIT;
>
> #Process mail
> my $From = $Document->GetFirstItem('From')->{Text} or goto WAIT;
> my $Password = $Document->GetFirstItem('Subject')->{Text};
> my $Command = $Document->GetFirstItem('Body')->{Text};
> my $Subject = "CMD: $Command";
>
> #Password Check - Password must be in subject line of email
> unless ($Password eq "xyz") {goto BADPASS}
>
> #execute command
> my $Report = `$Command`;
> print "!";
>
> #Send reply mail
> SENDMAIL:
>     my $ReportMail = $Database->CreateDocument('NEW') or goto WAIT;
>     $ReportMail->{Form} = 'Memo';
>     $ReportMail->{Body} = "$Report";
>     $ReportMail->{SendTo} = "$From";
>     $ReportMail->{Subject} = "$Subject";
>     $ReportMail->Save(1, 1);
>     $ReportMail->Send(0);
>
> #Clear the mailbox
> foreach (1..3) {
>     my $AllDoc = $Database->AllDocuments or goto WAIT;
>     my $delete  = $AllDoc->GetFirstDocument or goto WAIT;
>     $delete->Remove(1) or goto WAIT;
> }
>     goto WAIT;
>
> #Password Fails - Reformat reply mail and log the attack
> BADPASS:
>     $Report ="You do not have access to this system.  This attack has  
> been
> logged";
>     $Subject = "ACCESS DENIED";
>     open(BAD, ">>BADPASS.TXT") or goto WAIT;
>     print BAD "\n\nACCESS VIOLATION: \nFrom: $From\nSubject:
> $Password\nBody:\n\n$Command \n\n " or goto WAIT;
>     close(BAD); print "#" or goto WAIT;
>     goto SENDMAIL;
>
>
> goto WAIT;
>
> ####################################################################### 
> ##################
>
> Chris Benco
> Network Administrator
> Austin Powder Company
> 216-464-2400 x277
> [EMAIL PROTECTED]
>
>
>
>
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to