cmd.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. Raw json.RawMessage `json:"-"`
  33. }
  34. type CommandHandler interface {
  35. whatsapp.Handler
  36. HandleCommand(Command)
  37. }
  38. func (ext *ExtendedConn) handleMessageCommand(message []byte) {
  39. var event Command
  40. err := json.Unmarshal(message, &event)
  41. if err != nil {
  42. ext.jsonParseError(err)
  43. return
  44. }
  45. event.Raw = message
  46. event.JID = strings.Replace(event.JID, OldUserSuffix, NewUserSuffix, 1)
  47. for _, handler := range ext.handlers {
  48. commandHandler, ok := handler.(CommandHandler)
  49. if !ok {
  50. continue
  51. }
  52. go commandHandler.HandleCommand(event)
  53. }
  54. }