contact.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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) Exist(jid string) (<-chan string, error) {
  85. data := []interface{}{"query", "exist", jid}
  86. return wac.write(data)
  87. }
  88. func (wac *Conn) Emoji() (*binary.Node, error) {
  89. return wac.query("emoji", "", "", "", "", "", 0, 0)
  90. }
  91. func (wac *Conn) Contacts() (*binary.Node, error) {
  92. return wac.query("contacts", "", "", "", "", "", 0, 0)
  93. }
  94. func (wac *Conn) Chats() (*binary.Node, error) {
  95. return wac.query("chat", "", "", "", "", "", 0, 0)
  96. }
  97. func (wac *Conn) Read(jid, id string) (<-chan string, error) {
  98. ts := time.Now().Unix()
  99. tag := fmt.Sprintf("%d.--%d", ts, wac.msgCount)
  100. n := binary.Node{
  101. Description: "action",
  102. Attributes: map[string]string{
  103. "type": "set",
  104. "epoch": strconv.Itoa(wac.msgCount),
  105. },
  106. Content: []interface{}{binary.Node{
  107. Description: "read",
  108. Attributes: map[string]string{
  109. "count": "1",
  110. "index": id,
  111. "jid": jid,
  112. "owner": "false",
  113. },
  114. }},
  115. }
  116. return wac.writeBinary(n, group, ignore, tag)
  117. }
  118. func (wac *Conn) query(t, jid, messageId, kind, owner, search string, count, page int) (*binary.Node, error) {
  119. ts := time.Now().Unix()
  120. tag := fmt.Sprintf("%d.--%d", ts, wac.msgCount)
  121. n := binary.Node{
  122. Description: "query",
  123. Attributes: map[string]string{
  124. "type": t,
  125. "epoch": strconv.Itoa(wac.msgCount),
  126. },
  127. }
  128. if jid != "" {
  129. n.Attributes["jid"] = jid
  130. }
  131. if messageId != "" {
  132. n.Attributes["index"] = messageId
  133. }
  134. if kind != "" {
  135. n.Attributes["kind"] = kind
  136. }
  137. if owner != "" {
  138. n.Attributes["owner"] = owner
  139. }
  140. if search != "" {
  141. n.Attributes["search"] = search
  142. }
  143. if count != 0 {
  144. n.Attributes["count"] = strconv.Itoa(count)
  145. }
  146. if page != 0 {
  147. n.Attributes["page"] = strconv.Itoa(page)
  148. }
  149. ch, err := wac.writeBinary(n, group, ignore, tag)
  150. if err != nil {
  151. return nil, err
  152. }
  153. msg, err := wac.decryptBinaryMessage([]byte(<-ch))
  154. if err != nil {
  155. return nil, err
  156. }
  157. //TODO: use parseProtoMessage
  158. return msg, nil
  159. }
  160. func (wac *Conn) setGroup(t, jid, subject string, participants []string) (<-chan string, error) {
  161. ts := time.Now().Unix()
  162. tag := fmt.Sprintf("%d.--%d", ts, wac.msgCount)
  163. //TODO: get proto or improve encoder to handle []interface{}
  164. p := buildParticipantNodes(participants)
  165. g := binary.Node{
  166. Description: "group",
  167. Attributes: map[string]string{
  168. "author": wac.session.Wid,
  169. "id": tag,
  170. "type": t,
  171. },
  172. Content: p,
  173. }
  174. if jid != "" {
  175. g.Attributes["jid"] = jid
  176. }
  177. if subject != "" {
  178. g.Attributes["subject"] = subject
  179. }
  180. n := binary.Node{
  181. Description: "action",
  182. Attributes: map[string]string{
  183. "type": "set",
  184. "epoch": strconv.Itoa(wac.msgCount),
  185. },
  186. Content: []interface{}{g},
  187. }
  188. return wac.writeBinary(n, group, ignore, tag)
  189. }
  190. func buildParticipantNodes(participants []string) []binary.Node {
  191. l := len(participants)
  192. if participants == nil || l == 0 {
  193. return nil
  194. }
  195. p := make([]binary.Node, len(participants))
  196. for i, participant := range participants {
  197. p[i] = binary.Node{
  198. Description: "participant",
  199. Attributes: map[string]string{
  200. "jid": participant,
  201. },
  202. }
  203. }
  204. return p
  205. }