[Tutor] breakfast of champions

Kirk Bailey deliberatus@my995internet.com
Wed, 12 Dec 2001 03:05:55 -0500


cold pizzia.

OK, here is the latest state. Can find a subscriber if they are in
there, can send a message, wax your porcupine, and almost work. Gotta
digest them rfc822 headers still. Coming along. Found the definition for
find int he on line documenting. Makes sense really, although I would
have thought there would be a search method alredy in the language, but
hey, it works.

Also must add a loop to read the subscriber file and STRIP OFF the \n on
the end of each line, and append it to the list before searching it, as
it kills the search. Pity I cannot think of  way to do that on the fly
while loading with readlines. Any suggestions?


----------woohoo----------------------------------------
#!/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 find(str, ch):
	index = 0				# initialize counter
	while index < len(str):		# define termination condition
		if str[index] == ch:	# yes/no decision - is it there or not?
			return index	# yes, return positive value
		index = index + 1		# no, keep looking
	return -1				# field exausted, so it's not there.


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.

if find(members, from):							# IF the sender is in the subscriber, 
		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 form
		ftr = b.open(pathtostuff + "/lists/" + listname + ".footer",'r') #
read the footer
		footer = ftr.readlines()				# reads the footer into the variable
		ftr.close()							# close that file,
		message = message + footer				# and append it to the message. 
	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.
		
		"""
		
							# 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!

							# above is a blank line, DO NOT DELETE IT!
							# 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')
# 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 each 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!

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!
					
----------------------goodnightall---------------------------

-- 
Respectfully,
             -Kirk D Bailey (C)2001
              Addme! icq #27840081
end


Within the sweep of his sword, Each man is an Ubar.

http://www.howlermonkey.net/
http://www.sacredelectron.org/