whatsapp.go 4.1 KB

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