commands.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program 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 Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "strings"
  19. "maunium.net/go/maulogger"
  20. "maunium.net/go/mautrix-appservice"
  21. "maunium.net/go/mautrix-whatsapp/types"
  22. )
  23. type CommandHandler struct {
  24. bridge *Bridge
  25. log maulogger.Logger
  26. }
  27. // NewCommandHandler creates a CommandHandler
  28. func NewCommandHandler(bridge *Bridge) *CommandHandler {
  29. return &CommandHandler{
  30. bridge: bridge,
  31. log: bridge.Log.Sub("Command handler"),
  32. }
  33. }
  34. // CommandEvent stores all data which might be used to handle commands
  35. type CommandEvent struct {
  36. Bot *appservice.IntentAPI
  37. Bridge *Bridge
  38. Handler *CommandHandler
  39. RoomID types.MatrixRoomID
  40. User *User
  41. Args []string
  42. }
  43. // Reply sends a reply to command as notice
  44. func (ce *CommandEvent) Reply(msg string) {
  45. _, err := ce.Bot.SendNotice(string(ce.RoomID), msg)
  46. if err != nil {
  47. ce.Handler.log.Warnfln("Failed to reply to command from %s: %v", ce.User.MXID, err)
  48. }
  49. }
  50. // Handle handles messages to the bridge
  51. func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, message string) {
  52. args := strings.Split(message, " ")
  53. cmd := strings.ToLower(args[0])
  54. ce := &CommandEvent{
  55. Bot: handler.bridge.Bot,
  56. Bridge: handler.bridge,
  57. Handler: handler,
  58. RoomID: roomID,
  59. User: user,
  60. Args: args[1:],
  61. }
  62. switch cmd {
  63. case "login":
  64. handler.CommandLogin(ce)
  65. case "logout":
  66. handler.CommandLogout(ce)
  67. case "help":
  68. handler.CommandHelp(ce)
  69. default:
  70. ce.Reply("Unknown Command")
  71. }
  72. }
  73. const cmdLoginHelp = `login - Authenticate this Bridge as WhatsApp Web Client`
  74. // CommandLogin handles login command
  75. func (handler *CommandHandler) CommandLogin(ce *CommandEvent) {
  76. if ce.User.Session != nil {
  77. ce.Reply("You're already logged in.")
  78. return
  79. }
  80. ce.User.Connect(true)
  81. ce.User.Login(ce.RoomID)
  82. }
  83. const cmdLogoutHelp = `logout - Logout from WhatsApp`
  84. // CommandLogout handles !logout command
  85. func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
  86. if ce.User.Session == nil {
  87. ce.Reply("You're not logged in.")
  88. return
  89. }
  90. err := ce.User.Conn.Logout()
  91. if err != nil {
  92. ce.User.log.Warnln("Error while logging out:", err)
  93. ce.Reply("Error while logging out (see logs for details)")
  94. return
  95. }
  96. ce.User.Conn = nil
  97. ce.User.Session = nil
  98. ce.User.Update()
  99. ce.Reply("Logged out successfully.")
  100. }
  101. const cmdHelpHelp = `help - Prints this help`
  102. // CommandHelp handles help command
  103. func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
  104. cmdPrefix := handler.bridge.Config.Bridge.CommandPrefix + " "
  105. ce.Reply(strings.Join([]string{
  106. cmdPrefix + cmdHelpHelp,
  107. cmdPrefix + cmdLoginHelp,
  108. cmdPrefix + cmdLogoutHelp,
  109. }, "\n"))
  110. }