commands.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "github.com/Rhymen/go-whatsapp"
  20. "maunium.net/go/maulogger"
  21. "maunium.net/go/mautrix-appservice"
  22. "maunium.net/go/mautrix-whatsapp/database"
  23. "maunium.net/go/mautrix-whatsapp/types"
  24. "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
  25. )
  26. type CommandHandler struct {
  27. bridge *Bridge
  28. log maulogger.Logger
  29. }
  30. // NewCommandHandler creates a CommandHandler
  31. func NewCommandHandler(bridge *Bridge) *CommandHandler {
  32. return &CommandHandler{
  33. bridge: bridge,
  34. log: bridge.Log.Sub("Command handler"),
  35. }
  36. }
  37. // CommandEvent stores all data which might be used to handle commands
  38. type CommandEvent struct {
  39. Bot *appservice.IntentAPI
  40. Bridge *Bridge
  41. Handler *CommandHandler
  42. RoomID types.MatrixRoomID
  43. User *User
  44. Args []string
  45. }
  46. // Reply sends a reply to command as notice
  47. func (ce *CommandEvent) Reply(msg string) {
  48. _, err := ce.Bot.SendNotice(string(ce.User.ManagementRoom), msg)
  49. if err != nil {
  50. ce.Handler.log.Warnfln("Failed to reply to command from %s: %v", ce.User.MXID, err)
  51. }
  52. }
  53. // Handle handles messages to the bridge
  54. func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, message string) {
  55. args := strings.Split(message, " ")
  56. cmd := strings.ToLower(args[0])
  57. ce := &CommandEvent{
  58. Bot: handler.bridge.Bot,
  59. Bridge: handler.bridge,
  60. Handler: handler,
  61. RoomID: roomID,
  62. User: user,
  63. Args: args[1:],
  64. }
  65. switch cmd {
  66. case "login":
  67. handler.CommandLogin(ce)
  68. case "logout":
  69. handler.CommandLogout(ce)
  70. case "help":
  71. handler.CommandHelp(ce)
  72. case "import":
  73. handler.CommandImport(ce)
  74. default:
  75. ce.Reply("Unknown Command")
  76. }
  77. }
  78. const cmdLoginHelp = `login - Authenticate this Bridge as WhatsApp Web Client`
  79. // CommandLogin handles login command
  80. func (handler *CommandHandler) CommandLogin(ce *CommandEvent) {
  81. if ce.User.Session != nil {
  82. ce.Reply("You're already logged in.")
  83. return
  84. }
  85. ce.User.Connect(true)
  86. ce.User.Login(ce.RoomID)
  87. }
  88. const cmdLogoutHelp = `logout - Logout from WhatsApp`
  89. // CommandLogout handles !logout command
  90. func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
  91. if ce.User.Session == nil {
  92. ce.Reply("You're not logged in.")
  93. return
  94. }
  95. err := ce.User.Conn.Logout()
  96. if err != nil {
  97. ce.User.log.Warnln("Error while logging out:", err)
  98. ce.Reply("Error while logging out (see logs for details)")
  99. return
  100. }
  101. ce.User.Conn = nil
  102. ce.User.Session = nil
  103. ce.User.Update()
  104. ce.Reply("Logged out successfully.")
  105. }
  106. const cmdHelpHelp = `help - Prints this help`
  107. // CommandHelp handles help command
  108. func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
  109. cmdPrefix := ""
  110. if ce.User.ManagementRoom != ce.RoomID {
  111. cmdPrefix = handler.bridge.Config.Bridge.CommandPrefix + " "
  112. }
  113. ce.Reply(strings.Join([]string{
  114. cmdPrefix + cmdHelpHelp,
  115. cmdPrefix + cmdLoginHelp,
  116. cmdPrefix + cmdLogoutHelp,
  117. cmdPrefix + cmdImportHelp,
  118. }, "\n"))
  119. }
  120. const cmdImportHelp = `import <jid>|contacts - Open up a room for JID or for each WhatsApp contact`
  121. // CommandImport handles import command
  122. func (handler *CommandHandler) CommandImport(ce *CommandEvent) {
  123. // ensure all messages go to the management room
  124. ce.RoomID = ce.User.ManagementRoom
  125. if len(ce.Args) == 0 {
  126. ce.Reply("Usage: import <jid>|contacts")
  127. return
  128. }
  129. user := ce.User
  130. if ce.Args[0] == "contacts" {
  131. handler.log.Debugln("Importing all contacts of", user)
  132. _, err := user.Conn.Contacts()
  133. if err != nil {
  134. handler.log.Errorln("Error on update of contacts of user", user, ":", err)
  135. return
  136. }
  137. for jid, contact := range user.Conn.Store.Contacts {
  138. if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) {
  139. puppet := user.bridge.GetPuppetByJID(contact.Jid)
  140. puppet.Sync(user, contact)
  141. } else {
  142. portal := user.bridge.GetPortalByJID(database.GroupPortalKey(contact.Jid))
  143. portal.Sync(user, contact)
  144. }
  145. }
  146. ce.Reply("Importing all contacts done")
  147. } else {
  148. jid := ce.Args[0] + whatsappExt.NewUserSuffix
  149. handler.log.Debugln("Importing", jid, "for", user)
  150. puppet := user.bridge.GetPuppetByJID(jid)
  151. contact := whatsapp.Contact { Jid: jid }
  152. puppet.Sync(user, contact)
  153. }
  154. }