[Tutor] spamguard is cooking in the kitchen [using templated strings]

Daniel Yoo dyoo@CSUA.Berkeley.EDU
Sun, 15 Sep 2002 19:49:16 -0700 (PDT)


Hi Kirk,

Some comments on the code:


> # ok, where am I? I just woke up!
> fullpathtoscript = os.path.split(os.path.abspath(sys.argv[0]))
> print fullpathtoscript	# debug code, delete later.
> #
> # ok, now my config file is supposed to be RIGHT HERE with me!
> # So let's read the thing!
> f1 = open(fullpathtoscript[0]+"/spamguard.cf",'r')

I'd recommend using the os.path.join() function here, just for symmetry's
sake.  We had just used the os.path.split() a few lines back; we're
rebuilding a new path string, so our operations should reflect that:

###
f1 = open(os.path.join(fullpathtoscript[0], "spamguard.cf"))
###


Ideally, this should make your program more portable.  (But since Sendmail
is not a common component on Windows systems, portability may not be too
important here.  *grin*)

I've also removed the second parameter to the open() call, because read
mode "r" is the default that open() expects to use.





> webdomain=string.strip(f1.readline())	#70 read the domain name
> pathtopop=string.strip(f1.readline())	# read the pathto the user pop files
> f1.close()

Good, so the webdomain and pathtopop are defined in a separate
configuration file called 'sendmail.cf'.  We can take this one step
further: we may want to keep some of the program's long string messages in
there too!  This way, people who want to change the program's notification
messages won't need to hunt through source code.


In particular, we can yank this chunk of code here:

> 	msg=msg+"To: "+ from+"\n"
> 	msg=msg+'From: critter@'+localhost+"\n"
> 	msg=msg + "Subject: Unauthorized posting to account:" +  + "\n"
> 	msg=msg + "X-Loop: postmaster@"+ domainname + "\n"
> 	msg=msg + "X-spamfilter: SPAMGUARD V:1.0.0" + CRLF
> 	msg=msg + "Reply-To: postmaster@"+ localhost +"\n"
> 	msg=msg + """
> To whom it may concern;
> Recently, someone using this account tried to send a message to \" """ +
> username + "@" + localhost + """.
> It was never delivered, and has been destroyed. Here is why.
[text cut]


And once this is out of the code, we can relocate it into some kind of
"template" in your configuration file.

###
"""
To: %(from)s
From: critter@%(localhost)
Subject: Unauthorized posting to account:
X-Loop: postmaster@%(domainname)s
X-spamfilter: SPAMGUARD V:1.0.0
Reply-To: postmaster@%(localhost)s
To whom it may concern;
Recently, someone using this account tried to send a message to
%(username)s@%(localhost)s.  It was never delivered, and has been
destroyed. Here is why...
"""
###


Think of this template as a form letter, to be filled in when we really
want to use it.  When we want to print this out, we can use string
formatting to fill in the blanks:

###
error_notification = get_error_message_from_cf_file(f1)
print error_notification % { 'from' : from,
                             'localhost' : localhost,
                             'domainname' : domainname,
                             'username' : username }
###

(I haven't defined get_error_message_from_cf_file() --- you'll want to
think about how to do this.)


These changes shouldn't add any new functionality to your program, but
they do make it nicer to read and edit.


I hope this helps!