win.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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-2014 Oz Nahum Tiram <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 os
  27. try:
  28. import msvcrt
  29. except ImportError:
  30. pass
  31. from pwman.ui.cli import PwmanCli
  32. from pwman.util.crypto_engine import CryptoEngine
  33. def heardEnterWin():
  34. c = msvcrt.kbhit()
  35. if c == 1:
  36. ret = msvcrt.getch()
  37. if ret is not None:
  38. return True
  39. return False
  40. def _wait_until_enter(predicate, timeout, period=0.25): # pragma: no cover
  41. mustend = time.time() + timeout
  42. while time.time() < mustend:
  43. cond = predicate()
  44. if cond:
  45. break
  46. time.sleep(period)
  47. def winGetClipboard():
  48. ctypes.windll.user32.OpenClipboard(0)
  49. pcontents = ctypes.windll.user32.GetClipboardData(1) # 1 is CF_TEXT
  50. data = ctypes.c_char_p(pcontents).value
  51. #ctypes.windll.kernel32.GlobalUnlock(pcontents)
  52. ctypes.windll.user32.CloseClipboard()
  53. return data
  54. def winSetClipboard(text):
  55. text = str(text)
  56. GMEM_DDESHARE = 0x2000
  57. ctypes.windll.user32.OpenClipboard(0)
  58. ctypes.windll.user32.EmptyClipboard()
  59. try:
  60. # works on Python 2 (bytes() only takes one argument)
  61. hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE,
  62. len(bytes(text))+1)
  63. except TypeError:
  64. # works on Python 3 (bytes() requires an encoding)
  65. hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE,
  66. len(bytes(text, 'ascii'))+1)
  67. pchData = ctypes.windll.kernel32.GlobalLock(hCd)
  68. try:
  69. # works on Python 2 (bytes() only takes one argument)
  70. ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text))
  71. except TypeError:
  72. # works on Python 3 (bytes() requires an encoding)
  73. ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData),
  74. bytes(text, 'ascii'))
  75. ctypes.windll.kernel32.GlobalUnlock(hCd)
  76. ctypes.windll.user32.SetClipboardData(1, hCd)
  77. ctypes.windll.user32.CloseClipboard()
  78. class PwmanCliWin(PwmanCli):
  79. """
  80. windows ui class
  81. """
  82. def do_print(self, args):
  83. if not args.isdigit():
  84. print("print accepts only a single ID ...")
  85. return
  86. nodes = self._db.getnodes([args])
  87. node = self._db_entries_to_nodes(nodes)[0]
  88. print(node)
  89. flushtimeout = self.config.get_value('Global', 'cls_timeout')
  90. flushtimeout = flushtimeout or 10
  91. print("Type Enter to flush screen or wait %s sec. " % flushtimeout)
  92. _wait_until_enter(heardEnterWin, float(flushtimeout))
  93. self.do_cls('')
  94. def do_copy(self, args):
  95. ids = self._get_ids(args)
  96. if len(ids) > 1:
  97. print ("Can copy only 1 password at a time...")
  98. return None
  99. ce = CryptoEngine.get()
  100. nodes = self._db.getnodes(ids)
  101. for node in nodes:
  102. password = ce.decrypt(node[2])
  103. winSetClipboard(password)
  104. flushtimeout = self.config.get_value('Global', 'cp_timeout')
  105. flushtimeout = flushtimeout or 10
  106. print("erasing in {} sec...".format(flushtimeout))
  107. time.sleep(int(flushtimeout))
  108. winSetClipboard("")
  109. def do_open(self, args):
  110. ids = self._get_ids(args)
  111. if not args:
  112. self.help_open()
  113. return
  114. if len(ids) > 1:
  115. print ("Can open only 1 link at a time ...")
  116. return None
  117. ce = CryptoEngine.get()
  118. nodes = self._db.getnodes(ids)
  119. for node in nodes:
  120. url = ce.decrypt(node[3])
  121. if not url.startswith(("http://", "https://")):
  122. url = "https://" + url
  123. os.system("start "+url)
  124. def do_cls(self, args):
  125. os.system('cls')