mana.el 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ;;; mana.el --- Start ManaPlus without writing credentials
  2. ;; Copyright © 2014 Alex Kost
  3. ;; Author: Alex Kost <alezost@gmail.com>
  4. ;; Created: 12 Nov 2014
  5. ;; Version: 0.1
  6. ;; URL: https://github.com/alezost/mana.el
  7. ;; Keywords: games
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; This program is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; ManaPlus is a 2D MMORPG client, see <http://manaplus.org/>.
  20. ;; This package provides a command for starting ManaPlus using Emacs
  21. ;; facility to get credentials from authinfo file. So you can keep your
  22. ;; user and password for "The Mana World" game in "~/.authinfo.gpg" and
  23. ;; do not bother about writing those every time you start manaplus.
  24. ;; To install this package manually, add the following to your emacs
  25. ;; init file:
  26. ;;
  27. ;; (add-to-list 'load-path "/path/to/dir-with-mana-package")
  28. ;; (autoload 'mana-start "mana" nil t)
  29. ;; Then add a line like the following to an authinfo file you use (one of
  30. ;; the files from `auth-sources' variable):
  31. ;;
  32. ;; machine server.themanaworld.org login <your-user> password <your-password>
  33. ;; Once it is done, "M-x mana-start" and enjoy :-)
  34. ;; TODO:
  35. ;;
  36. ;; This package can be easily modified to use different servers and to
  37. ;; ask a user which one he wants to connect. And it should support
  38. ;; other analogous games (like Evol Online <http://evolonline.org/>) but
  39. ;; as I play only "The Mana World" and I'm the only user of this
  40. ;; package, currently it is limited to the main server of TMW. But if
  41. ;; you somehow found this package and you want it to support other
  42. ;; servers, contact me and I will gladly add such support.
  43. ;;; Code:
  44. (require 'cl-lib)
  45. (require 'auth-source)
  46. (defgroup mana nil
  47. "Interface for starting manaplus client using authinfo file.
  48. See info node `(auth) Help for users' to learn how to use
  49. authentication files from `auth-sources' variable."
  50. :link '(url-link "http://manaplus.org/")
  51. :link '(info-link "(auth) Help for users")
  52. :group 'games)
  53. (defcustom mana-server "server.themanaworld.org"
  54. "Default server to connect."
  55. :type 'string
  56. :group 'mana)
  57. (defcustom mana-program "manaplus"
  58. "Filename of the client program."
  59. :type 'string
  60. :group 'mana)
  61. (defcustom mana-character nil
  62. "Name of the character to be automatically chosen after a game start."
  63. :type '(choice (const :tag "No default character")
  64. (string :tag "Name"))
  65. :group 'mana)
  66. (defvar mana-process-name "mana"
  67. "Name of a process used by `start-process'.")
  68. (defvar mana-buffer-name "*mana-output*"
  69. "Name of a buffer used by `start-process'.")
  70. (cl-defstruct
  71. (mana-credentials
  72. (:constructor nil) ; no default constructor
  73. (:constructor mana-make-credentials (user password))
  74. (:copier nil))
  75. user password)
  76. (defun mana-credentials ()
  77. "Return credentials from authinfo file."
  78. (let ((auth (car (auth-source-search :host mana-server)))
  79. user password)
  80. (when auth
  81. (let ((secret (plist-get auth :secret)))
  82. (setq user (plist-get auth :user)
  83. password (if (functionp secret)
  84. (funcall secret)
  85. secret))))
  86. (or user
  87. (setq user (read-string "User name: ")))
  88. (or password
  89. (setq password (read-passwd "Password: ")))
  90. (mana-make-credentials user password)))
  91. (defun mana-argument (key val)
  92. "Return a 'long-option' shell argument by KEY and VAL."
  93. (and val
  94. (concat "--" key "=" (shell-quote-argument val))))
  95. (defun mana-arguments ()
  96. "Return list of arguments for a client program."
  97. (let* ((credentials (mana-credentials))
  98. (user (mana-credentials-user credentials))
  99. (password (mana-credentials-password credentials)))
  100. (delq nil
  101. (list (mana-argument "server" mana-server)
  102. (mana-argument "username" user)
  103. (mana-argument "password" password)
  104. (mana-argument "character" mana-character)))))
  105. ;;;###autoload
  106. (defun mana-start ()
  107. "Start manaplus client."
  108. (interactive)
  109. (apply #'start-process
  110. mana-process-name mana-buffer-name
  111. mana-program (mana-arguments)))
  112. (provide 'mana)
  113. ;;; mana.el ends here