r23563 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23562‎ | r23563 | r23564 >
Date:19:57, 29 June 2007
Author:brion
Status:old
Tags:
Comment:
Optional blacklist for word pair generation
Modified paths:
  • /trunk/extensions/ConfirmEdit/captcha.py (modified) (history)

Diff [purge]

Index: trunk/extensions/ConfirmEdit/captcha.py
@@ -114,7 +114,27 @@
115115 if not os.path.exists(fulldir):
116116 os.mkdir(fulldir)
117117 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+
119139 if __name__ == '__main__':
120140 """This grabs random words from the dictionary 'words' (one
121141 word per line) and generates a captcha image for each one,
@@ -125,6 +145,7 @@
126146 """
127147 font = "VeraBd.ttf"
128148 wordlist = "awordlist.txt"
 149+ blacklistfile = None
129150 key = "CHANGE_THIS_SECRET!"
130151 output = "."
131152 count = 20
@@ -132,12 +153,14 @@
133154 dirs = 0
134155 verbose = False
135156
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"])
137158 for o, a in opts:
138159 if o == "--font":
139160 font = a
140161 if o == "--wordlist":
141162 wordlist = a
 163+ if o == "--blacklist":
 164+ blacklistfile = a
142165 if o == "--key":
143166 key = a
144167 if o == "--output":
@@ -156,15 +179,19 @@
157180 # files after...
158181 count = max(0, fill - len(os.listdir(output)))
159182
160 - words = [string.lower(x.strip()) for x in open(wordlist).readlines()]
 183+ words = read_wordlist(wordlist)
161184 words = [x for x in words
162185 if len(x) <= 5 and len(x) >= 4 and x[0] != "f"
163186 and x[0] != x[1] and x[-1] != x[-2]
164187 and (not "'" in x)]
 188+
 189+ if blacklistfile:
 190+ blacklist = read_wordlist(blacklistfile)
 191+ else:
 192+ blacklist = []
 193+
165194 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)
169196 salt = "%08x" % random.randrange(2**32)
170197 # 64 bits of hash is plenty for this purpose
171198 hash = md5.new(key+salt+word+key+salt).hexdigest()[:16]

Status & tagging log