cmd.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2019 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 whatsappExt
  17. import (
  18. "encoding/json"
  19. "strings"
  20. "github.com/Rhymen/go-whatsapp"
  21. )
  22. type CommandType string
  23. const (
  24. CommandPicture CommandType = "picture"
  25. CommandDisconnect CommandType = "disconnect"
  26. )
  27. type Command struct {
  28. Type CommandType `json:"type"`
  29. JID string `json:"jid"`
  30. *ProfilePicInfo
  31. Kind string `json:"kind"`
  32. }
  33. type CommandHandler interface {
  34. whatsapp.Handler
  35. HandleCommand(Command)
  36. }
  37. func (ext *ExtendedConn) handleMessageCommand(message []byte) {
  38. var event Command
  39. err := json.Unmarshal(message, &event)
  40. if err != nil {
  41. ext.jsonParseError(err)
  42. return
  43. }
  44. event.JID = strings.Replace(event.JID, OldUserSuffix, NewUserSuffix, 1)
  45. for _, handler := range ext.handlers {
  46. commandHandler, ok := handler.(CommandHandler)
  47. if !ok {
  48. continue
  49. }
  50. go commandHandler.HandleCommand(event)
  51. }
  52. }