whatsapp.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. type GroupInfo struct {
  42. JID string `json:"jid"`
  43. OwnerJID string `json:"owner"`
  44. Name string `json:"subject"`
  45. NameSetTime int64 `json:"subjectTime"`
  46. NameSetBy string `json:"subjectOwner"`
  47. Topic string `json:"desc"`
  48. TopicID string `json:"descId"`
  49. TopicSetAt int64 `json:"descTime"`
  50. TopicSetBy string `json:"descOwner"`
  51. GroupCreated int64 `json:"creation"`
  52. Status int16 `json:"status"`
  53. Participants []struct {
  54. JID string `json:"id"`
  55. IsAdmin bool `json:"isAdmin"`
  56. IsSuperAdmin bool `json:"isSuperAdmin"`
  57. } `json:"participants"`
  58. }
  59. func (ext *ExtendedConn) GetGroupMetaData(jid string) (*GroupInfo, error) {
  60. data, err := ext.Conn.GetGroupMetaData(jid)
  61. if err != nil {
  62. return nil, fmt.Errorf("failed to get group metadata: %v", err)
  63. }
  64. content := <-data
  65. info := &GroupInfo{}
  66. err = json.Unmarshal([]byte(content), info)
  67. if err != nil {
  68. return info, fmt.Errorf("failed to unmarshal group metadata: %v", err)
  69. }
  70. for index, participant := range info.Participants {
  71. info.Participants[index].JID = strings.Replace(participant.JID, OldUserSuffix, NewUserSuffix, 1)
  72. }
  73. info.NameSetBy = strings.Replace(info.NameSetBy, OldUserSuffix, NewUserSuffix, 1)
  74. info.TopicSetBy = strings.Replace(info.TopicSetBy, OldUserSuffix, NewUserSuffix, 1)
  75. return info, nil
  76. }
  77. type ProfilePicInfo struct {
  78. URL string `json:"eurl"`
  79. Tag string `json:"tag"`
  80. Status int16 `json:"status"`
  81. }
  82. func (ppi *ProfilePicInfo) Download() (io.ReadCloser, error) {
  83. resp, err := http.Get(ppi.URL)
  84. if err != nil {
  85. return nil, err
  86. }
  87. return resp.Body, nil
  88. }
  89. func (ppi *ProfilePicInfo) DownloadBytes() ([]byte, error) {
  90. body, err := ppi.Download()
  91. if err != nil {
  92. return nil, err
  93. }
  94. defer body.Close()
  95. data, err := ioutil.ReadAll(body)
  96. return data, err
  97. }
  98. func (ext *ExtendedConn) GetProfilePicThumb(jid string) (*ProfilePicInfo, error) {
  99. data, err := ext.Conn.GetProfilePicThumb(jid)
  100. if err != nil {
  101. return nil, fmt.Errorf("failed to get avatar: %v", err)
  102. }
  103. content := <-data
  104. info := &ProfilePicInfo{}
  105. err = json.Unmarshal([]byte(content), info)
  106. if err != nil {
  107. return info, fmt.Errorf("failed to unmarshal avatar info: %v", err)
  108. }
  109. return info, nil
  110. }