[Tutor] gangstrip

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


# 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
						# 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.

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 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. 
		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
		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.
							# 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 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
	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!
--------------you can get dressed now----------------------
             -Kirk D Bailey (C)2001
              Addme! icq #27840081

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