Advertisement
mimosa

bcc.py

Feb 27th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. #! /usr/bin/python
  2. #bcc.py
  3. #
  4. #A script to strip email addresses from a file and convert them to a format suitable for pasting into a bcc field
  5.  
  6. import sys, string
  7.  
  8. def main():
  9.  
  10.     #infile = open("raw.txt", "r")         #to call from within IDE for debugging
  11.     infile = open(sys.argv[1], 'r')        #open the file given to the script as an argument
  12.     rawString = infile.read()
  13.    
  14.     rawString = stripOut(rawString, "\n")   #replace newlines with spaces
  15.    
  16.    
  17.    
  18.     addressList = []                       #create an empty list to put the addresses in
  19.     for address in string.split(rawString):#break it up into items separated by whitespace
  20.        
  21.         address = wellFormed(address)      #strip it of forbidden elements
  22.         if address !="":                   #check the result still contains an email address
  23.             addressList.append(address)    #if so, add it to the list
  24.  
  25.     bccString = ", ".join(addressList)     #join the addresses together separated by commas
  26.     infile.close()                         #close the input file
  27.     outfile = open("bcc.txt", "w")         #open file to write
  28.     outfile.write(bccString)               #write
  29.     outfile.close()                        #close
  30.     #print bccString                        #IDE debugging
  31.    
  32.  
  33. def wellFormed(possAddress):               #turn input into well-formed email address, or empty string
  34.    
  35.     at = False                             #make sure "@" is present
  36.     for char in possAddress:
  37.         if char == "@":
  38.             at = True
  39.     if at == False:
  40.         return ""                          #if not, return an empty string
  41.  
  42.     user, domain = possAddress.split("@")  #divide into user and domain
  43.     user, domain = stripLeft(user), stripRight(domain) #strip off forbidden characters from beginning and end respectively
  44.  
  45.     if user == "" or domain == "":         #if either is empty, return empty string
  46.         return ""
  47.     else:
  48.         return user + "@" + domain
  49.  
  50.  
  51. def stripLeft(text):                       #strip inadmissible characters off the neginning of the username
  52.     if text == "":
  53.         return text
  54.     elif alphaNum(text[len(text)-1]) == False:
  55.         return ""                          #throw away illegal character and anything to its left
  56.     else:
  57.         return stripLeft(text[:-1]) + text[-1] #recursively move leftward if character is legal
  58.  
  59. def stripRight(text):                      #strip inadmissible characters off the end of the domain name
  60.     if text == "":
  61.         return text
  62.     elif alphaNum(text[0]) == False:
  63.         return ""
  64.     else:
  65.         return text[0] + stripRight(text[1:])
  66.  
  67.  
  68. def alphaNum(char):                        #check if character permitted
  69.     if char == "":
  70.         return False                       #if empty string
  71.     ascii = ord(char)                      #get ASCII value of character
  72.     if ascii == 46:                        #is it a full stop?
  73.         return True
  74.     elif ascii > 64 and ascii < 91:        #or a capital letter?
  75.         return True
  76.     elif ascii > 96 and ascii < 123:       #lower case?
  77.         return True
  78.     elif ascii > 47 and ascii < 58:        #a numeral?
  79.         return True
  80.     else:
  81.         return False                       #not a permitted character then ...
  82.  
  83. def stripOut(mainString, subString):
  84.     return " ".join(mainString.split(subString))#replace occurrences of subString with spaces  
  85.  
  86. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement