whatsapp.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "fmt"
  20. "io"
  21. "io/ioutil"
  22. "net/http"
  23. "strings"
  24. "github.com/Rhymen/go-whatsapp"
  25. )
  26. const (
  27. OldUserSuffix = "@c.us"
  28. NewUserSuffix = "@s.whatsapp.net"
  29. )
  30. type ExtendedConn struct {
  31. *whatsapp.Conn
  32. handlers []whatsapp.Handler
  33. }
  34. func ExtendConn(conn *whatsapp.Conn) *ExtendedConn {
  35. ext := &ExtendedConn{
  36. Conn: conn,
  37. }
  38. ext.Conn.AddHandler(ext)
  39. return ext
  40. }
  41. func (ext *ExtendedConn) shouldCallSynchronously(handler whatsapp.Handler) bool {
  42. sh, ok := handler.(whatsapp.SyncHandler)
  43. return ok && sh.ShouldCallSynchronously()
  44. }
  45. func (ext *ExtendedConn) ShouldCallSynchronously() bool {
  46. return true
  47. }
  48. type GroupInfo struct {
  49. JID string `json:"jid"`
  50. OwnerJID string `json:"owner"`
  51. Name string `json:"subject"`
  52. NameSetTime int64 `json:"subjectTime"`
  53. NameSetBy string `json:"subjectOwner"`
  54. Topic string `json:"desc"`
  55. TopicID string `json:"descId"`
  56. TopicSetAt int64 `json:"descTime"`
  57. TopicSetBy string `json:"descOwner"`
  58. GroupCreated int64 `json:"creation"`
  59. Status int16 `json:"status"`
  60. Participants []struct {
  61. JID string `json:"id"`
  62. IsAdmin bool `json:"isAdmin"`
  63. IsSuperAdmin bool `json:"isSuperAdmin"`
  64. } `json:"participants"`
  65. }
  66. func (ext *ExtendedConn) GetGroupMetaData(jid string) (*GroupInfo, error) {
  67. data, err := ext.Conn.GetGroupMetaData(jid)
  68. if err != nil {
  69. return nil, fmt.Errorf("failed to get group metadata: %v", err)
  70. }
  71. content := <-data
  72. info := &GroupInfo{}
  73. err = json.Unmarshal([]byte(content), info)
  74. if err != nil {
  75. return info, fmt.Errorf("failed to unmarshal group metadata: %v", err)
  76. }
  77. for index, participant := range info.Participants {
  78. info.Participants[index].JID = strings.Replace(participant.JID, OldUserSuffix, NewUserSuffix, 1)
  79. }
  80. info.NameSetBy = strings.Replace(info.NameSetBy, OldUserSuffix, NewUserSuffix, 1)
  81. info.TopicSetBy = strings.Replace(info.TopicSetBy, OldUserSuffix, NewUserSuffix, 1)
  82. return info, nil
  83. }
  84. type ProfilePicInfo struct {
  85. URL string `json:"eurl"`
  86. Tag string `json:"tag"`
  87. Status int16 `json:"status"`
  88. }
  89. func (ppi *ProfilePicInfo) Download() (io.ReadCloser, error) {
  90. resp, err := http.Get(ppi.URL)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return resp.Body, nil
  95. }
  96. func (ppi *ProfilePicInfo) DownloadBytes() ([]byte, error) {
  97. body, err := ppi.Download()
  98. if err != nil {
  99. return nil, err
  100. }
  101. defer body.Close()
  102. data, err := ioutil.ReadAll(body)
  103. return data, err
  104. }
  105. func (ext *ExtendedConn) GetProfilePicThumb(jid string) (*ProfilePicInfo, error) {
  106. data, err := ext.Conn.GetProfilePicThumb(jid)
  107. if err != nil {
  108. return nil, fmt.Errorf("failed to get avatar: %v", err)
  109. }
  110. content := <-data
  111. info := &ProfilePicInfo{}
  112. err = json.Unmarshal([]byte(content), info)
  113. if err != nil {
  114. return info, fmt.Errorf("failed to unmarshal avatar info: %v", err)
  115. }
  116. return info, nil
  117. }