Index: trunk/extensions/ConfirmEdit/captcha.py |
— | — | @@ -114,7 +114,27 @@ |
115 | 115 | if not os.path.exists(fulldir): |
116 | 116 | os.mkdir(fulldir) |
117 | 117 | return subdir |
118 | | - |
| 118 | + |
| 119 | +def try_pick_word(words, blacklist, verbose): |
| 120 | + word1 = words[random.randint(0,len(words)-1)] |
| 121 | + word2 = words[random.randint(0,len(words)-1)] |
| 122 | + word = word1+word2 |
| 123 | + for naughty in blacklist: |
| 124 | + if naughty in word: |
| 125 | + if verbose: |
| 126 | + print "skipping word pair '%s' because it contains blacklisted word '%s'" % (word, naughty) |
| 127 | + return None |
| 128 | + return word |
| 129 | + |
| 130 | +def pick_word(words, blacklist, verbose): |
| 131 | + while True: |
| 132 | + word = try_pick_word(words, blacklist, verbose) |
| 133 | + if word: |
| 134 | + return word |
| 135 | + |
| 136 | +def read_wordlist(filename): |
| 137 | + return [string.lower(x.strip()) for x in open(wordlist).readlines()] |
| 138 | + |
119 | 139 | if __name__ == '__main__': |
120 | 140 | """This grabs random words from the dictionary 'words' (one |
121 | 141 | word per line) and generates a captcha image for each one, |
— | — | @@ -125,6 +145,7 @@ |
126 | 146 | """ |
127 | 147 | font = "VeraBd.ttf" |
128 | 148 | wordlist = "awordlist.txt" |
| 149 | + blacklistfile = None |
129 | 150 | key = "CHANGE_THIS_SECRET!" |
130 | 151 | output = "." |
131 | 152 | count = 20 |
— | — | @@ -132,12 +153,14 @@ |
133 | 154 | dirs = 0 |
134 | 155 | verbose = False |
135 | 156 | |
136 | | - opts, args = getopt.getopt(sys.argv[1:], "", ["font=", "wordlist=", "key=", "output=", "count=", "fill=", "dirs=", "verbose"]) |
| 157 | + opts, args = getopt.getopt(sys.argv[1:], "", ["font=", "wordlist=", "blacklist=", "key=", "output=", "count=", "fill=", "dirs=", "verbose"]) |
137 | 158 | for o, a in opts: |
138 | 159 | if o == "--font": |
139 | 160 | font = a |
140 | 161 | if o == "--wordlist": |
141 | 162 | wordlist = a |
| 163 | + if o == "--blacklist": |
| 164 | + blacklistfile = a |
142 | 165 | if o == "--key": |
143 | 166 | key = a |
144 | 167 | if o == "--output": |
— | — | @@ -156,15 +179,19 @@ |
157 | 180 | # files after... |
158 | 181 | count = max(0, fill - len(os.listdir(output))) |
159 | 182 | |
160 | | - words = [string.lower(x.strip()) for x in open(wordlist).readlines()] |
| 183 | + words = read_wordlist(wordlist) |
161 | 184 | words = [x for x in words |
162 | 185 | if len(x) <= 5 and len(x) >= 4 and x[0] != "f" |
163 | 186 | and x[0] != x[1] and x[-1] != x[-2] |
164 | 187 | and (not "'" in x)] |
| 188 | + |
| 189 | + if blacklistfile: |
| 190 | + blacklist = read_wordlist(blacklistfile) |
| 191 | + else: |
| 192 | + blacklist = [] |
| 193 | + |
165 | 194 | for i in range(count): |
166 | | - word1 = words[random.randint(0,len(words)-1)] |
167 | | - word2 = words[random.randint(0,len(words)-1)] |
168 | | - word = word1+word2 |
| 195 | + word = pick_word(words, blacklist, verbose) |
169 | 196 | salt = "%08x" % random.randrange(2**32) |
170 | 197 | # 64 bits of hash is plenty for this purpose |
171 | 198 | hash = md5.new(key+salt+word+key+salt).hexdigest()[:16] |