win.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """"
  2. #============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #============================================================================
  18. # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
  19. #============================================================================
  20. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  21. #============================================================================
  22. """
  23. from pwman.ui.cli import PwmanCliNew
  24. from pwman.data.nodes import NewNode
  25. from pwman.ui import tools
  26. import time
  27. import msvcrt
  28. import pwman.util.config as config
  29. import ast
  30. from pwman.util.crypto import zerome
  31. class PwmanCliWinNew(PwmanCliNew):
  32. def do_new(self, args):
  33. """
  34. can override default config settings the following way:
  35. Pwman3 0.2.1 (c) visit: http://github.com/pwman3/pwman3
  36. pwman> n {'leetify':False, 'numerics':True, 'special_chars':True}
  37. Password (Blank to generate):
  38. """
  39. errmsg = """could not parse config override, please input some"""\
  40. + """ kind of dictionary, e.g.: n {'leetify':False, """\
  41. + """'numerics':True, 'special_chars':True}"""
  42. try:
  43. username = self.get_username()
  44. if args:
  45. try:
  46. args = ast.literal_eval(args)
  47. except Exception:
  48. raise Exception(errmsg)
  49. if not isinstance(args, dict):
  50. raise Exception(errmsg)
  51. password = self.get_password(1, **args)
  52. else:
  53. numerics = config.get_value(
  54. "Generator", "numerics").lower() == 'true'
  55. # TODO: allow custom leetifying through the config
  56. leetify = config.get_value(
  57. "Generator", "leetify").lower() == 'true'
  58. special_chars = config.get_value(
  59. "Generator", "special_chars").lower() == 'true'
  60. password = self.get_password(0,
  61. numerics=numerics,
  62. symbols=leetify,
  63. special_signs=special_chars)
  64. url = self.get_url()
  65. notes = self.get_notes()
  66. node = NewNode(username, password, url, notes)
  67. tags = self.get_tags()
  68. node.set_tags(tags)
  69. self._db.addnodes([node])
  70. print "Password ID: %d" % (node.get_id())
  71. # when done with node erase it
  72. zerome(password)
  73. except Exception, e:
  74. self.error(e)
  75. def print_node(self, node):
  76. width = str(tools._defaultwidth)
  77. print "Node %d." % (node.get_id())
  78. print ("%"+width+"s %s") % (tools.typeset("Username:", tools.ANSI.Red),
  79. node.get_username())
  80. print ("%"+width+"s %s") % (tools.typeset("Password:", tools.ANSI.Red),
  81. node.get_password())
  82. print ("%"+width+"s %s") % (tools.typeset("Url:", tools.ANSI.Red),
  83. node.get_url())
  84. print ("%"+width+"s %s") % (tools.typeset("Notes:", tools.ANSI.Red),
  85. node.get_notes())
  86. print tools.typeset("Tags: ", tools.ANSI.Red),
  87. for t in node.get_tags():
  88. print " %s \n" % t.get_name(),
  89. def heardEnterWin():
  90. c = msvcrt.kbhit()
  91. if c == 1:
  92. ret = msvcrt.getch()
  93. if ret is not None:
  94. return True
  95. return False
  96. def waituntil_enter(somepredicate, timeout, period=0.25):
  97. mustend = time.time() + timeout
  98. while time.time() < mustend:
  99. cond = somepredicate()
  100. if cond:
  101. break
  102. time.sleep(period)
  103. self.do_cls('')
  104. flushtimeout = int(config.get_value("Global", "cls_timeout"))
  105. if flushtimeout > 0:
  106. print "Press any key to flush screen (autoflash "\
  107. + "in %d sec.)" % flushtimeout
  108. waituntil_enter(heardEnterWin, flushtimeout)