From: Antonio Ospite <ospite@studenti.unina.it>
Date: Tue, 9 Apr 2013 12:01:21 +0000 (+0200)
Subject: Use exrex.count() to know how many strings the regex generates
X-Git-Url: https://git.ao2.it/crackpop.git/commitdiff_plain/38d8e69e7cc789683e68005846a747aa6e23f1cc

Use exrex.count() to know how many strings the regex generates

exrex uses generators, so this saves memory as we don't have to store
the whole list of results anymore.

While at it refactor things a little bit so to pass the number of
elements to the crackpop() function too.
---

diff --git a/crackpop.py b/crackpop.py
index b032da6..8d9a45b 100755
--- a/crackpop.py
+++ b/crackpop.py
@@ -26,26 +26,31 @@ __version = "0.1"
 __author_info = "Antonio Ospite"
 
 
+# returns a tuple: (num_passwords, passwords)
+# where passwords is an iterable type
 def generate_passwords(password_pattern, dry_run=False):
-    passwords = list(exrex.generate(password_pattern))
+    num_passwords = exrex.count(password_pattern)
+    passwords = exrex.generate(password_pattern)
 
     if dry_run:
-        print "Generated %d passwords." % len(passwords)
+        print "Generated %d passwords." % num_passwords
         for p in passwords:
             print p
-        return []
+        return (0, iter([]))
 
-    return passwords
+    return (num_passwords, passwords)
 
 
+# the passwords parameter is a tuple: (n, L)
+# where L is an iterable type and n is the number of elements in L
 def crackpop(host, port, ssl, user, passwords):
     if ssl:
         pop3_connect = poplib.POP3_SSL
     else:
         pop3_connect = poplib.POP3
 
-    print "Testing %d passwords." % len(passwords)
-    for p in passwords:
+    print "Testing %d passwords." % passwords[0]
+    for p in passwords[1]:
         # TODO maybe the same connection can be reused for more than one try,
         # but some logic needs to be added to detect the maximum allowed
         # authentication attempts or a disconnection from the server.