cmd.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 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. )
  26. type Command struct {
  27. Type CommandType `json:"type"`
  28. JID string `json:"jid"`
  29. *ProfilePicInfo
  30. }
  31. type CommandHandler interface {
  32. whatsapp.Handler
  33. HandleCommand(Command)
  34. }
  35. func (ext *ExtendedConn) handleMessageCommand(message []byte) {
  36. var event Command
  37. err := json.Unmarshal(message, &event)
  38. if err != nil {
  39. ext.jsonParseError(err)
  40. return
  41. }
  42. event.JID = strings.Replace(event.JID, OldUserSuffix, NewUserSuffix, 1)
  43. for _, handler := range ext.handlers {
  44. commandHandler, ok := handler.(CommandHandler)
  45. if !ok {
  46. continue
  47. }
  48. go commandHandler.HandleCommand(event)
  49. }
  50. }