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