SHOW:
|
|
- or go back to the newest paste.
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 | rawString = stripOut(rawString, ",") #replace commas with spaces | |
17 | ||
18 | ||
19 | ||
20 | addressList = [] #create an empty list to put the addresses in | |
21 | for address in string.split(rawString):#break it up into items separated by whitespace | |
22 | ||
23 | address = wellFormed(address) #strip it of forbidden elements | |
24 | if address !="": #check the result still contains an email address | |
25 | addressList.append(address) #if so, add it to the list | |
26 | ||
27 | bccString = ", ".join(addressList) #join the addresses together separated by commas | |
28 | infile.close() #close the input file | |
29 | outfile = open("bcc.txt", "w") #open file to write | |
30 | outfile.write(bccString) #write | |
31 | outfile.close() #close | |
32 | #print bccString #IDE debugging | |
33 | ||
34 | ||
35 | def wellFormed(possAddress): #turn input into well-formed email address, or empty string | |
36 | ||
37 | at = False #make sure "@" is present | |
38 | for char in possAddress: | |
39 | if char == "@": | |
40 | at = True | |
41 | if at == False: | |
42 | return "" #if not, return an empty string | |
43 | ||
44 | user, domain = possAddress.split("@") #divide into user and domain | |
45 | user, domain = stripLeft(user), stripRight(domain) #strip off forbidden characters from beginning and end respectively | |
46 | ||
47 | if user == "" or domain == "": #if either is empty, return empty string | |
48 | return "" | |
49 | else: | |
50 | return user + "@" + domain | |
51 | ||
52 | ||
53 | def stripLeft(text): #strip inadmissible characters off the neginning of the username | |
54 | if text == "": | |
55 | return text | |
56 | elif alphaNum(text[len(text)-1]) == False: | |
57 | return "" #throw away illegal character and anything to its left | |
58 | else: | |
59 | return stripLeft(text[:-1]) + text[-1] #recursively move leftward if character is legal | |
60 | ||
61 | def stripRight(text): #strip inadmissible characters off the end of the domain name | |
62 | if text == "": | |
63 | return text | |
64 | elif alphaNum(text[0]) == False: | |
65 | return "" | |
66 | else: | |
67 | return text[0] + stripRight(text[1:]) | |
68 | ||
69 | ||
70 | def alphaNum(char): #check if character permitted | |
71 | if char == "": | |
72 | return False #if empty string | |
73 | ascii = ord(char) #get ASCII value of character | |
74 | if ascii == 46: #is it a full stop? | |
75 | return True | |
76 | elif ascii > 64 and ascii < 91: #or a capital letter? | |
77 | return True | |
78 | elif ascii > 96 and ascii < 123: #lower case? | |
79 | return True | |
80 | elif ascii > 47 and ascii < 58: #a numeral? | |
81 | return True | |
82 | else: | |
83 | return False #not a permitted character then ... | |
84 | ||
85 | def stripOut(mainString, subString): | |
86 | return " ".join(mainString.split(subString))#replace occurrences of subString with spaces | |
87 | ||
88 | main() |