win.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 __future__ import print_function
  24. import time
  25. import ctypes
  26. import ast
  27. import os
  28. try:
  29. import msvcrt
  30. except ImportError:
  31. pass
  32. from colorama import Fore
  33. import pwman.util.config as config
  34. from pwman.ui.cli import PwmanCli
  35. from pwman.data.nodes import Node
  36. from pwman.ui import tools
  37. from pwman.util.crypto_engine import zerome
  38. def winGetClipboard():
  39. ctypes.windll.user32.OpenClipboard(0)
  40. pcontents = ctypes.windll.user32.GetClipboardData(1) # 1 is CF_TEXT
  41. data = ctypes.c_char_p(pcontents).value
  42. #ctypes.windll.kernel32.GlobalUnlock(pcontents)
  43. ctypes.windll.user32.CloseClipboard()
  44. return data
  45. def winSetClipboard(text):
  46. text = str(text)
  47. GMEM_DDESHARE = 0x2000
  48. ctypes.windll.user32.OpenClipboard(0)
  49. ctypes.windll.user32.EmptyClipboard()
  50. try:
  51. # works on Python 2 (bytes() only takes one argument)
  52. hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE,
  53. len(bytes(text))+1)
  54. except TypeError:
  55. # works on Python 3 (bytes() requires an encoding)
  56. hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE,
  57. len(bytes(text, 'ascii'))+1)
  58. pchData = ctypes.windll.kernel32.GlobalLock(hCd)
  59. try:
  60. # works on Python 2 (bytes() only takes one argument)
  61. ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text))
  62. except TypeError:
  63. # works on Python 3 (bytes() requires an encoding)
  64. ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData),
  65. bytes(text, 'ascii'))
  66. ctypes.windll.kernel32.GlobalUnlock(hCd)
  67. ctypes.windll.user32.SetClipboardData(1, hCd)
  68. ctypes.windll.user32.CloseClipboard()
  69. class PwmanCliWin(PwmanCli):
  70. """
  71. windows ui class
  72. """
  73. def do_new(self, args):
  74. """
  75. can override default config settings the following way:
  76. Pwman3 0.2.1 (c) visit: http://github.com/pwman3/pwman3
  77. pwman> n {'leetify':False, 'numerics':True, 'special_chars':True}
  78. Password (Blank to generate):
  79. """
  80. errmsg = """could not parse config override, please input some"""\
  81. + """ kind of dictionary, e.g.: n {'leetify':False, """\
  82. + """'numerics':True, 'special_chars':True}"""
  83. try:
  84. username = self.get_username()
  85. if args:
  86. try:
  87. args = ast.literal_eval(args)
  88. except Exception:
  89. raise Exception(errmsg)
  90. if not isinstance(args, dict):
  91. raise Exception(errmsg)
  92. password = self.get_password(1, **args)
  93. else:
  94. numerics = config.get_value(
  95. "Generator", "numerics").lower() == 'true'
  96. # TODO: allow custom leetifying through the config
  97. leetify = config.get_value(
  98. "Generator", "leetify").lower() == 'true'
  99. special_chars = config.get_value(
  100. "Generator", "special_chars").lower() == 'true'
  101. password = self.get_password(0,
  102. numerics=numerics,
  103. symbols=leetify,
  104. special_signs=special_chars)
  105. url = self.get_url()
  106. notes = self.get_notes()
  107. node = Node()
  108. node.username = username
  109. node.password = password
  110. node.url = url
  111. node.notes = notes
  112. tags = self.get_tags()
  113. node.tags = tags
  114. self._db.addnodes([node])
  115. print("Password ID: %d" % (node._id))
  116. # when done with node erase it
  117. zerome(password)
  118. except Exception as e:
  119. self.error(e)
  120. def print_node(self, node):
  121. width = tools._defaultwidth
  122. print("Node {}.".format(node._id))
  123. print("{} {}".format(tools.typeset("Username:", Fore.RED).ljust(width),
  124. node.username))
  125. print ("{} {}".format(tools.typeset("Password:",
  126. Fore.RED).ljust(width),
  127. node.password))
  128. print("{} {}".format(tools.typeset("Url:", Fore.RED).ljust(width),
  129. node.url))
  130. print("{} {}".format(tools.typeset("Notes:", Fore.RED).ljust(width),
  131. node.notes))
  132. print("{}".format(tools.typeset("Tags: ", Fore.RED)), end=" ")
  133. for t in node.tags:
  134. print(t)
  135. def heardEnterWin():
  136. c = msvcrt.kbhit()
  137. if c == 1:
  138. ret = msvcrt.getch()
  139. if ret is not None:
  140. return True
  141. return False
  142. def waituntil_enter(somepredicate, timeout, period=0.25):
  143. mustend = time.time() + timeout
  144. while time.time() < mustend:
  145. cond = somepredicate()
  146. if cond:
  147. break
  148. time.sleep(period)
  149. self.do_cls('')
  150. flushtimeout = int(config.get_value("Global", "cls_timeout"))
  151. if flushtimeout > 0:
  152. print("Press any key to flush screen (autoflash "
  153. "in %d sec.)" % flushtimeout)
  154. waituntil_enter(heardEnterWin, flushtimeout)
  155. def do_copy(self, args):
  156. ids = self.get_ids(args)
  157. if len(ids) > 1:
  158. print ("Can copy only 1 password at a time...")
  159. return None
  160. try:
  161. node = self._db.getnodes(ids)
  162. winSetClipboard(node[0].password)
  163. print("copied password for {}@{} clipboard".format(
  164. node[0].username, node[0].url))
  165. print("erasing in 10 sec...")
  166. time.sleep(10)
  167. winSetClipboard("")
  168. except Exception as e:
  169. self.error(e)
  170. def do_open(self, args):
  171. ids = self.get_ids(args)
  172. if not args:
  173. self.help_open()
  174. return
  175. if len(ids) > 1:
  176. print ("Can open only 1 link at a time ...")
  177. return None
  178. try:
  179. node = self._db.getnodes(ids)
  180. url = node[0].url
  181. if not url.startswith(("http://", "https://")):
  182. url = "https://" + url
  183. os.system("start "+url)
  184. except Exception as e:
  185. self.error(e)
  186. def do_cls(self, args):
  187. os.system('cls')