[Tutor] class envy

Kirk Bailey highprimate@howlermonkey.net
Thu, 13 Dec 2001 1:21:44 -0500


ok, now I know I am in up to my eyes, some one get me a snorkel. This novice is finally in WAY over his head.

I am reading the official online python tutoral about classes, and I am totally lost. I mean I am SNOWBLIND. 
If you gave me a clue I would spread it on toast, I need SEVERAL clues, superglue, a serious skulldrill, and 
BIG RED STRAPS to hold it on with.

rfc822 is a pretty understandable document, and I understand email fine. But the module rfc822 is leaving me 
going -huh?- and CLASS and classes leave me without a clue.
I need to grok parsing the incoming message to extract the data contained there. 

A alias feeds an incoming message to the program on the standard input, and is readable several ways, simplest 
being raw_input() and friends. The list name we know from reading the command line arguement.





OK, this is what exists thus far. It's 90% done now, even will read a random footer appendment
(A slogan, witty saying, quote, or text advertisement from a flat text file) to the footer of the message.


--------------rare fresh bits follow---------------------
#!/usr/local/bin/python
# Tinylist module, Pitchtime unit.
# this handles receiving and sending out postings to a list.

import sys, re, string, rfc822, smtplib

localhost = 'howlermonkey.net'	# make sure you set this to the domain YOU use!!!
						# next line declares the path to stuff. DEFINE IT
pathtostuff = '/www/www.howlermonkey.net/cgi-bin/'	# to the cgi-bin for your web dir with
						# a trailing '/' as this example shows!
						# note this tells where to start looking.
						# everything is either here, or under this
						# point in '/lists' dir.
					
					
listname = sys.argv[1]			# we read a command line arguement to determine the list name
						# Arguement 0 is the name of the script run, 1 is the
						# first arguement.
						# after that name in the command line, so if this were
						# program FOO,
						# that line would be "|/path/FOO listname"
						# and the arguement here would be 'listname'!
					
					
message = raw_input.lines()		# Variable 'message' is assigned the entire message
						# which is piped to the program by the alias definition
						# such as:
						# foolist:"|/pathtoprogram/programname foolist"


						# now we must determine the sender and see if they are in
						# the subscriber file!
						# still got to learn more about how this thing handles
						# parsing the incoming message, but smtp and rfc822
						# are dep sorcery on this material.

from = message["From"]			# we must dig out the 'from: ' field and read it's contents.
						# Note we already know who it is TO - 'listname' !
						# Remember, the mail ssytem fed it here with a command
						# line arguement, after sorting it out to a known identity,
						# so the list's defining alias contains that info.

def gangstrip(str):			# ok, everybody STRIP! STR is a list variable
	index = 0				# We want to strip off all leading and
	while index<len(str)		# trailing whitespace characters
	str[index]=string.strip(str[index])	# in each element in the list.

open a.file(pathtostuff + "/lists/" + listname, 'r')						
members = a.readlines()			# we build a list of all the members of the email list
a.close()					# and close the file
						# then we look to see if that FROM exists in the membership.

gangstrip(members)			# strip all leading and trailing whitespace
						# from each element in the list 'members'.

if from in members :			# IF the sender is in the subscriber file, 
		subject = '[' + listname + ']' + subject		# then acept the submission.
		Reply_to = listname + "@" + localhost		# this sets the reply-to field.
		X-Loop = "X-Loop: " + listname + "@" + localhost	# This breaks email loops from forming
		ftr = b.open(pathtostuff + "/lists/" + listname + ".footer",'r') # read the footer file
		footer = ftr.readlines()				# reads the footer into the variable
		ftr.close()							# close that file,
		message = message + footer + CRLF			# and append it to the message. 
		f1=open(path/lists/listname,'r')			# Open the footer rotation
		randomline=gangstrip(f1.readlines())		# load the contents of the rotation
		message = message + randomline + CRLF		# add the random element to the footer.
	else									# BUT IF THEY ARE NOT SUBSCRIBED...
		listnamemembers = from					# put their addres as the only recipient
		message = ""						# clear the mesage.
		From = "From: tinylist@" + localhost + \n 	# From: tinylist@mydomain.foo !
		Subject = "Subject: Unauthorized posting to list" + listname + \n
		Reply_to = "Reply-to:postmaster@" + localhost + \n # no comments possible now!
		X-Loop = "X-Loop: postmaster@" + localhost	# This breaks email loops from forming
		message = """
		
		To whom it may concern;
		
		Sorry, but as you are not a current member of """ + listname + """, 
		you may not post to it. Your recent posting has been rejected and destroyed.
		
		Feel free to contact the postmaster if there is any question.
		Any reply to this letter should go directly to the postmaster.
		
		You can also subscribe to this list if you like.
		
		Goodbye.
		
		"""

							# ABOVE IS 1 BLANK LINE, DO NOT DELETE IT!
							# there cannot be comments in those line 
							# 							# above this one, or they would be 
part
							# of the triplequoted string!
							# ok, if they are not a member, THEY GET 
							# THE REPLY SHOWN ABOVE MAILED TO THEM! 
							# (Maybe we could read a stock answer from a file?)
							# there is no endif or fi in python.
							# whitespace and tabulation therefore
							# is rather important here.
							# now we send whatever message is to go out to 
							# whatever is in the recipient tupple.

server = smtplib.SMTP('localhost')	# setup for a smtp run.
# helo(localhost)				# open a connection to the smtp server, possibly not needed
						# so it is commented out. If all else fails, use it.
server.connect()				# and log in to the thing as the identity this script runs as.
						#
for to_addr in listnamemembers :		# for each address in the list listnamemembers,
	server.sendmail(from_addr, to_addr, Subject, reply-to, to, X-Loop, msg)	# send a envlope and msg!

server.quit()				# then close the connection.

	
						# make sure this script runs as a TRUSTED USER-
						# and NOT as root!!! Make sure that a
						# NON-priviliged user can run this script,
						# and make sure it is owned by that identity!
					
-------------end of line---------------------------------------


end

In total confusion,
                   Kirk D Bailey

|----------------------------------------------------|
|           Consulting  Agent  Provecateur           |
|    Webmaster, Howlermonkey Email services Co.      |
|   Please visit us at http://www.howlermonkey.net/  |
|----------------------------------------------------|