win.py 4.2 KB

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