contact.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package whatsapp
  2. import (
  3. "fmt"
  4. "github.com/Rhymen/go-whatsapp/binary"
  5. "strconv"
  6. "time"
  7. )
  8. type Presence string
  9. const (
  10. PresenceAvailable = "available"
  11. PresenceUnavailable = "unavailable"
  12. PresenceComposing = "composing"
  13. )
  14. //TODO: filename? WhatsApp uses Store.Contacts for these functions
  15. //TODO: functions probably shouldn't return a string, maybe build a struct / return json
  16. //TODO: check for further queries
  17. func (wac *Conn) GetProfilePicThumb(jid string) (<-chan string, error) {
  18. data := []interface{}{"query", "ProfilePicThumb", jid}
  19. return wac.write(data)
  20. }
  21. func (wac *Conn) GetStatus(jid string) (<-chan string, error) {
  22. data := []interface{}{"query", "Status", jid}
  23. return wac.write(data)
  24. }
  25. func (wac *Conn) GetGroupMetaData(jid string) (<-chan string, error) {
  26. data := []interface{}{"query", "GroupMetadata", jid}
  27. return wac.write(data)
  28. }
  29. func (wac *Conn) SubscribePresence(jid string) (<-chan string, error) {
  30. data := []interface{}{"action", "presence", "subscribe", jid}
  31. return wac.write(data)
  32. }
  33. func (wac *Conn) CreateGroup(subject string, participants []string) (<-chan string, error) {
  34. return wac.setGroup("create", "", subject, participants)
  35. }
  36. func (wac *Conn) UpdateGroupSubject(subject string, jid string) (<-chan string, error) {
  37. return wac.setGroup("subject", jid, subject, nil)
  38. }
  39. func (wac *Conn) SetAdmin(jid string, participants []string) (<-chan string, error) {
  40. return wac.setGroup("promote", jid, "", participants)
  41. }
  42. func (wac *Conn) RemoveAdmin(jid string, participants []string) (<-chan string, error) {
  43. return wac.setGroup("demote", jid, "", participants)
  44. }
  45. func (wac *Conn) AddMember(jid string, participants []string) (<-chan string, error) {
  46. return wac.setGroup("add", jid, "", participants)
  47. }
  48. func (wac *Conn) RemoveMember(jid string, participants []string) (<-chan string, error) {
  49. return wac.setGroup("remove", jid, "", participants)
  50. }
  51. func (wac *Conn) LeaveGroup(jid string) (<-chan string, error) {
  52. return wac.setGroup("leave", jid, "", nil)
  53. }
  54. func (wac *Conn) Search(search string, count, page int) (*binary.Node, error) {
  55. return wac.query("search", "", "", "", "", search, count, page)
  56. }
  57. func (wac *Conn) LoadMessages(jid, messageId string, count int) (*binary.Node, error) {
  58. return wac.query("message", jid, "", "before", "true", "", count, 0)
  59. }
  60. func (wac *Conn) LoadMessagesBefore(jid, messageId string, count int) (*binary.Node, error) {
  61. return wac.query("message", jid, messageId, "before", "true", "", count, 0)
  62. }
  63. func (wac *Conn) LoadMessagesAfter(jid, messageId string, count int) (*binary.Node, error) {
  64. return wac.query("message", jid, messageId, "after", "true", "", count, 0)
  65. }
  66. func (wac *Conn) Presence(jid string, presence Presence) (<-chan string, error) {
  67. ts := time.Now().Unix()
  68. tag := fmt.Sprintf("%d.--%d", ts, wac.msgCount)
  69. n := binary.Node{
  70. Description: "action",
  71. Attributes: map[string]string{
  72. "type": "set",
  73. "epoch": strconv.Itoa(wac.msgCount),
  74. },
  75. Content: []interface{}{binary.Node{
  76. Description: "presence",
  77. Attributes: map[string]string{
  78. "type": string(presence),
  79. },
  80. }},
  81. }
  82. return wac.writeBinary(n, group, ignore, tag)
  83. }
  84. func (wac *Conn) Emoji() (*binary.Node, error) {
  85. return wac.query("emoji", "", "", "", "", "", 0, 0)
  86. }
  87. func (wac *Conn) Contacts() (*binary.Node, error) {
  88. return wac.query("contacts", "", "", "", "", "", 0, 0)
  89. }
  90. func (wac *Conn) Chats() (*binary.Node, error) {
  91. return wac.query("chat", "", "", "", "", "", 0, 0)
  92. }
  93. func (wac *Conn) Read(jid, id string) (<-chan string, error) {
  94. ts := time.Now().Unix()
  95. tag := fmt.Sprintf("%d.--%d", ts, wac.msgCount)
  96. n := binary.Node{
  97. Description: "action",
  98. Attributes: map[string]string{
  99. "type": "set",
  100. "epoch": strconv.Itoa(wac.msgCount),
  101. },
  102. Content: []interface{}{binary.Node{
  103. Description: "read",
  104. Attributes: map[string]string{
  105. "count": "1",
  106. "index": id,
  107. "jid": jid,
  108. "owner": "false",
  109. },
  110. }},
  111. }
  112. return wac.writeBinary(n, group, ignore, tag)
  113. }
  114. func (wac *Conn) query(t, jid, messageId, kind, owner, search string, count, page int) (*binary.Node, error) {
  115. ts := time.Now().Unix()
  116. tag := fmt.Sprintf("%d.--%d", ts, wac.msgCount)
  117. n := binary.Node{
  118. Description: "query",
  119. Attributes: map[string]string{
  120. "type": t,
  121. "epoch": strconv.Itoa(wac.msgCount),
  122. },
  123. }
  124. if jid != "" {
  125. n.Attributes["jid"] = jid
  126. }
  127. if messageId != "" {
  128. n.Attributes["index"] = messageId
  129. }
  130. if kind != "" {
  131. n.Attributes["kind"] = kind
  132. }
  133. if owner != "" {
  134. n.Attributes["owner"] = owner
  135. }
  136. if search != "" {
  137. n.Attributes["search"] = search
  138. }
  139. if count != 0 {
  140. n.Attributes["count"] = strconv.Itoa(count)
  141. }
  142. if page != 0 {
  143. n.Attributes["page"] = strconv.Itoa(page)
  144. }
  145. ch, err := wac.writeBinary(n, group, ignore, tag)
  146. if err != nil {
  147. return nil, err
  148. }
  149. msg, err := wac.decryptBinaryMessage([]byte(<-ch))
  150. if err != nil {
  151. return nil, err
  152. }
  153. //TODO: use parseProtoMessage
  154. return msg, nil
  155. }
  156. func (wac *Conn) setGroup(t, jid, subject string, participants []string) (<-chan string, error) {
  157. ts := time.Now().Unix()
  158. tag := fmt.Sprintf("%d.--%d", ts, wac.msgCount)
  159. //TODO: get proto or improve encoder to handle []interface{}
  160. p := buildParticipantNodes(participants)
  161. g := binary.Node{
  162. Description: "group",
  163. Attributes: map[string]string{
  164. "author": wac.session.Wid,
  165. "id": tag,
  166. "type": t,
  167. },
  168. Content: p,
  169. }
  170. if jid != "" {
  171. g.Attributes["jid"] = jid
  172. }
  173. if subject != "" {
  174. g.Attributes["subject"] = subject
  175. }
  176. n := binary.Node{
  177. Description: "action",
  178. Attributes: map[string]string{
  179. "type": "set",
  180. "epoch": strconv.Itoa(wac.msgCount),
  181. },
  182. Content: []interface{}{g},
  183. }
  184. return wac.writeBinary(n, group, ignore, tag)
  185. }
  186. func buildParticipantNodes(participants []string) []binary.Node {
  187. l := len(participants)
  188. if participants == nil || l == 0 {
  189. return nil
  190. }
  191. p := make([]binary.Node, len(participants))
  192. for i, participant := range participants {
  193. p[i] = binary.Node{
  194. Description: "participant",
  195. Attributes: map[string]string{
  196. "jid": participant,
  197. },
  198. }
  199. }
  200. return p
  201. }