jao-proton-utils.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ;; jao-proton-utils.el -- simple interaction with Proton mail and vpn
  2. ;; Copyright (c) 2018, 2019, 2020, 2023 Jose Antonio Ortega Ruiz
  3. ;; This file is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation; either version 3, or (at your option)
  6. ;; any later version.
  7. ;; This file is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with GNU Emacs; see the file COPYING. If not, write to
  13. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. ;; Boston, MA 02111-1307, USA.
  15. ;; Author: Jose Antonio Ortega Ruiz <mail@jao.io>
  16. ;; Start date: Fri Dec 21, 2018 23:56
  17. ;;; Comentary:
  18. ;; This is a very simple comint-derived mode to run the CLI version
  19. ;; of PM's Bridge within the comfort of emacs.
  20. ;;; Code:
  21. (define-derived-mode proton-bridge-mode comint-mode "proton-bridge"
  22. "A very simple comint-based mode to run ProtonMail's bridge"
  23. (setq comint-prompt-read-only t)
  24. (setq comint-prompt-regexp "^>>> "))
  25. ;;;###autoload
  26. (defun run-proton-bridge ()
  27. "Run or switch to an existing bridge process, using its CLI"
  28. (interactive)
  29. (pop-to-buffer (make-comint "proton-bridge" "protonmail-bridge" nil "-c"))
  30. (unless (eq major-mode 'proton-bridge-mode)
  31. (proton-bridge-mode)))
  32. ;;;###autoload
  33. (defun proton-bridge-sendmail-setup ()
  34. "Configure message sending for local proton bridge."
  35. (setq send-mail-function #'smtpmail-send-it)
  36. (setq message-send-mail-function #'smtpmail-send-it)
  37. (setq smtpmail-servers-requiring-authorization
  38. (regexp-opt '("localhost" "127.0.0.1")))
  39. (setq smtpmail-auth-supported '(plain login))
  40. (setq smtpmail-smtp-user "mail@jao.io")
  41. (setq smtpmail-smtp-server "localhost")
  42. (setq smtpmail-smtp-service 1025))
  43. (defvar jao-proton-vpn-font-lock-keywords '("\\[.+\\]"))
  44. ;;;###autoload
  45. (defun proton-vpn-mode ()
  46. "A very simple mode to show the output of ProtonVPN commands"
  47. (interactive)
  48. (kill-all-local-variables)
  49. (buffer-disable-undo)
  50. (use-local-map proton-vpn-mode-map)
  51. (setq-local font-lock-defaults '(jao-proton-vpn-font-lock-keywords))
  52. (setq-local truncate-lines t)
  53. (setq-local next-line-add-newlines nil)
  54. (setq major-mode 'proton-vpn-mode)
  55. (setq mode-name "proton-vpn")
  56. (read-only-mode 1))
  57. (defvar jao-proton-vpn--buffer "*pvpn*")
  58. (defun jao-proton-vpn--do (things)
  59. (let ((b (pop-to-buffer (get-buffer-create jao-proton-vpn--buffer))))
  60. (let ((inhibit-read-only t)
  61. (cmd (format "protonvpn-cli %s" things)))
  62. (delete-region (point-min) (point-max))
  63. (message "Running: %s ...." cmd)
  64. (shell-command cmd b)
  65. (message ""))
  66. (proton-vpn-mode)))
  67. ;;;###autoload
  68. (defun proton-vpn-status ()
  69. (interactive)
  70. (jao-proton-vpn--do "s"))
  71. (defun proton-vpn--get-status ()
  72. (or (when-let ((b (get-buffer jao-proton-vpn--buffer)))
  73. (with-current-buffer b
  74. (goto-char (point-min))
  75. (if (re-search-forward "^Status: *\\(.+\\)$" nil t)
  76. (match-string-no-properties 1)
  77. (when (re-search-forward "^Connected!$")
  78. "Connected"))))
  79. "Disconnected"))
  80. ;;;###autoload
  81. (defun proton-vpn-connect (cc)
  82. (interactive "P")
  83. (let ((cc (when cc (read-string "Country code: "))))
  84. (jao-proton-vpn--do (if cc (format "c --cc %s" cc) "c --sc"))
  85. (proton-vpn-status)))
  86. (defun proton-vpn-reconnect ()
  87. (interactive)
  88. (jao-proton-vpn--do "r"))
  89. (setenv "PVPN_WAIT" "300")
  90. ;;;###autoload
  91. (defun proton-vpn-maybe-reconnect ()
  92. (interactive)
  93. (when (string= "Connected" (proton-vpn--get-status))
  94. (jao-proton-vpn--do "d")
  95. (sit-for 5)
  96. (jao-proton-vpn--do "r")))
  97. ;;;###autoload
  98. (defun proton-vpn-disconnect ()
  99. (interactive)
  100. (jao-proton-vpn--do "d"))
  101. (setq proton-vpn-mode-map
  102. (let ((map (make-keymap)))
  103. (suppress-keymap map)
  104. (define-key map [?q] 'bury-buffer)
  105. (define-key map [?n] 'next-line)
  106. (define-key map [?p] 'previous-line)
  107. (define-key map [?g] 'proton-vpn-status)
  108. (define-key map [?r] 'proton-vpn-reconnect)
  109. (define-key map [?d] (lambda ()
  110. (interactive)
  111. (when (y-or-n-p "Disconnect?")
  112. (proton-vpn-disconnect))))
  113. (define-key map [?c] 'proton-vpn-connect)
  114. map))
  115. (provide 'jao-proton-utils)
  116. ;;; jao-proton.el ends here