2014-02-14 16:30:38 +13:00
# TODO: Add some kind of pronounceable password generation
# TODO: Improve randomness
2011-11-20 22:23:31 +13:00
import string
import random
2014-02-14 16:36:57 +13:00
from util import hook
2012-03-01 12:20:44 -08:00
2012-09-04 22:38:54 +12:00
@hook.command
def password ( inp , notice = None ) :
2013-09-04 18:30:04 +08:00
""" password <length> [types] -- Generates a password of <length> (default 10).
[ types ] can include ' alpha ' , ' no caps ' , ' numeric ' , ' symbols ' or any combination of the inp , eg . ' numbers symbols ' """
2011-11-20 22:23:31 +13:00
okay = [ ]
2012-09-04 22:38:54 +12:00
2012-06-11 10:36:29 +12:00
# find the length needed for the password
2012-09-04 22:38:54 +12:00
numb = inp . split ( " " )
2012-10-12 12:23:37 +13:00
2012-06-11 10:21:44 +12:00
try :
length = int ( numb [ 0 ] )
except ValueError :
length = 10
2012-06-11 10:36:29 +12:00
# add alpha characters
2012-09-04 22:38:54 +12:00
if " alpha " in inp or " letter " in inp :
okay = okay + list ( string . ascii_lowercase )
2011-11-20 22:23:31 +13:00
#adds capital characters if not told not to
2012-09-04 22:38:54 +12:00
if " no caps " not in inp :
okay = okay + list ( string . ascii_uppercase )
2012-06-11 10:36:29 +12:00
# add numbers
2012-09-04 22:38:54 +12:00
if " numeric " in inp or " number " in inp :
okay = okay + [ str ( x ) for x in xrange ( 0 , 10 ) ]
2012-06-11 10:36:29 +12:00
# add symbols
2012-09-04 22:38:54 +12:00
if " symbol " in inp :
2013-09-04 18:30:04 +08:00
sym = [ ' ! ' , ' @ ' , ' # ' , ' $ ' , ' % ' , ' ^ ' , ' & ' , ' * ' , ' ( ' , ' ) ' , ' - ' , ' = ' , ' _ ' , ' + ' , ' [ ' , ' ] ' , ' { ' , ' } ' , ' \\ ' , ' | ' , ' ; ' ,
' : ' , " ' " , ' . ' , ' > ' , ' , ' , ' < ' , ' / ' , ' ? ' , ' ` ' , ' ~ ' , ' " ' ]
2012-06-11 10:36:29 +12:00
okay + = okay + sym
# defaults to lowercase alpha password if the okay list is empty
if not okay :
2012-09-04 22:38:54 +12:00
okay = okay + list ( string . ascii_lowercase )
2012-06-11 10:36:29 +12:00
2014-02-13 14:34:50 +13:00
pw = " "
2012-10-12 12:23:37 +13:00
2012-06-11 10:36:29 +12:00
# generates password
2011-11-20 22:23:31 +13:00
for x in range ( length ) :
2014-02-13 14:34:50 +13:00
pw = pw + random . choice ( okay )
2011-11-20 22:23:31 +13:00
2014-02-13 14:34:50 +13:00
notice ( pw )