Skip to content

Commit

Permalink
feat: add protobuf-sending of messages
Browse files Browse the repository at this point in the history
feat: enable getGroupInviteLink now returns the link
feat: add getGroupInfo
  • Loading branch information
Labfox committed Aug 27, 2024
1 parent e0f94e1 commit 36a8be4
Show file tree
Hide file tree
Showing 99 changed files with 7,187 additions and 22 deletions.
3 changes: 2 additions & 1 deletion dev-requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ aiosqlite
setuptools
requests
mkdocs
qrcode
qrcode
protobuf
4 changes: 4 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pluggy==1.5.0
# via pytest
pre-commit==3.8.0
# via -r dev-requirements.in
protobuf==5.27.3
# via
# -r dev-requirements.in
# -r requirements.in
pypng==0.20220715.0
# via qrcode
pytest==8.3.2
Expand Down
3 changes: 2 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ types-PyYAML
setuptools
requests
mkdocs
qrcode
qrcode
protobuf
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pathspec==0.12.1
# via mkdocs
platformdirs==4.2.2
# via mkdocs-get-deps
protobuf==5.27.3
# via -r requirements.in
pypng==0.20220715.0
# via qrcode
python-dateutil==2.9.0.post0
Expand Down
86 changes: 74 additions & 12 deletions whatsfly/dependencies/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

// #include "wapp.h"
// #include <string.h>
// #include <stdlib.h>
// #include <stdint.h>
import "C"

import (
Expand Down Expand Up @@ -444,13 +447,11 @@ func (w *WhatsAppClient) Disconnect(c2 *whatsmeow.Client) {
w.runMessageThread = false
}

func (w *WhatsAppClient) SendMessage(number string, message string, is_group bool) int {
func (w *WhatsAppClient) SendMessage(number string, message *waProto.Message, is_group bool) int {
var numberObj types.JID = getJid(number, is_group)

messageObj := &waProto.Message{
Conversation: proto.String(""),
}
messageObj.Conversation = proto.String(message)
messageObj := message


// Check if the client is connected
if !w.wpClient.IsConnected() {
Expand Down Expand Up @@ -688,7 +689,7 @@ func (w *WhatsAppClient) SendDocument(number string, documentPath string, captio
return 0
}

func (w *WhatsAppClient) GetGroupInviteLink(group string, reset bool) int {
func (w *WhatsAppClient) GetGroupInviteLink(group string, reset bool, returnid string) int {
numberObj := getJid(group, true)

if !w.wpClient.IsConnected() {
Expand All @@ -699,7 +700,8 @@ func (w *WhatsAppClient) GetGroupInviteLink(group string, reset bool) int {
}

link, err := w.wpClient.GetGroupInviteLink(numberObj, reset)
w.addEventToQueue("{\"eventType\":\"groupInviteLink\",\"group\": \""+group+"\" \"link\":" + link + "}")
w.addEventToQueue("{\"eventType\":\"groupInviteLink\",\"group\": \""+group+"\", \"link\":\"" + link + "\"}")
w.addEventToQueue("{\"eventType\":\"methodReturn\",\"return\": \""+link+"\", \"callid\":\"" + returnid + "\"}")
if err != nil {
return 1
}
Expand Down Expand Up @@ -789,6 +791,32 @@ func (w *WhatsAppClient) SetGroupTopic(group string, topic string) int {
return 0
}

func (w *WhatsAppClient) GetGroupInfo(group string, return_id string) int {
numberObj := getJid(group, true)

if !w.wpClient.IsConnected() {
err := w.wpClient.Connect()
if err != nil {
return 1
}
}

groupinfo, err := w.wpClient.GetGroupInfo(numberObj)
if err != nil {
return 1
}

b, err := json.Marshal(&groupinfo)
if err != nil {
return 1
}

w.addEventToQueue("{\"eventType\":\"methodReturn\",\"return\": "+string(b)+", \"callid\":\"" + return_id + "\"}")

return 0
}



//export NewWhatsAppClientWrapper
func NewWhatsAppClientWrapper(c_phone_number *C.char, c_media_path *C.char, fn_disconnect_callback C.ptr_to_pyfunc, fn_event_callback C.ptr_to_pyfunc_str) C.int {
Expand Down Expand Up @@ -819,16 +847,38 @@ func MessageThreadWrapper(id C.int) {
w.MessageThread()
}

//export SendMessageWrapper
func SendMessageWrapper(id C.int, c_phone_number *C.char, c_message *C.char, c_is_group C.bool) C.int {
//export SendMessageProtobufWrapper
func SendMessageProtobufWrapper(id C.int, c_phone_number *C.char, c_message *C.char, c_is_group C.bool) C.int {
phone_number := C.GoString(c_phone_number)
message := C.GoString(c_message)

message := &waProto.Message{}

length := C.strlen(c_message)

goBytes := C.GoBytes(unsafe.Pointer(c_message), C.int(length))

proto.Unmarshal(goBytes, message)
is_group := bool(c_is_group)

w := handles[int(id)]
return C.int(w.SendMessage(phone_number, message, is_group))
}

//export SendMessageWrapper
func SendMessageWrapper(id C.int, c_phone_number *C.char, c_message *C.char, c_is_group C.bool) C.int {
message_str := C.GoString(c_message)

message := &waProto.Message{
Conversation: &message_str,
}

marshalled, _ := proto.Marshal(message)

message_encoded := C.CString(string(marshalled))

return SendMessageProtobufWrapper(id, c_phone_number, message_encoded, c_is_group)
}

//export SendImageWrapper
func SendImageWrapper(id C.int, c_phone_number *C.char, c_image_path *C.char, c_caption *C.char, c_is_group C.bool) C.int {
phone_number := C.GoString(c_phone_number)
Expand Down Expand Up @@ -873,13 +923,14 @@ func SendDocumentWrapper(id C.int, c_phone_number *C.char, c_document_path *C.ch
}

//export GetGroupInviteLinkWrapper
func GetGroupInviteLinkWrapper(id C.int, c_jid *C.char, c_reset C.bool) C.int {
func GetGroupInviteLinkWrapper(id C.int, c_jid *C.char, c_reset C.bool, c_return_id *C.char) C.int {
jid := C.GoString(c_jid)
reset := bool(c_reset)
return_id := C.GoString(c_return_id)

w := handles[int(id)]

return C.int(w.GetGroupInviteLink(jid, reset))
return C.int(w.GetGroupInviteLink(jid, reset, return_id))
}

//export JoinGroupWithInviteLinkWrapper
Expand Down Expand Up @@ -931,6 +982,17 @@ func SetGroupTopicWrapper(id C.int, c_jid *C.char, c_topic *C.char) C.int {
return C.int(w.SetGroupTopic(jid, topic))
}

//export GetGroupInfoWrapper
func GetGroupInfoWrapper(id C.int, c_jid *C.char, c_return_id *C.char) C.int {
jid := C.GoString(c_jid)
return_id := C.GoString(c_return_id)

w := handles[int(id)]

return C.int(w.GetGroupInfo(jid, return_id))
}


//export Version
func Version() C.int {
return C.int(012)
Expand Down
4 changes: 3 additions & 1 deletion whatsfly/dependencies/wapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ extern "C" {
extern void DisconnectWrapper(int id);
extern void MessageThreadWrapper(int id);
extern int SendMessageWrapper(int id, char* c_number, char* c_msg, bool is_group);
extern int SendMessageProtobufWrapper(int id, char* c_number, char* c_msg, bool is_group);
extern int SendImageWrapper(int id, char* c_number, char* c_image_path, char* c_caption, bool is_group);
extern int SendVideoWrapper(int id, char* c_number, char* c_video_path, char* c_caption, bool is_group);
extern int SendAudioWrapper(int id, char* c_number, char* c_audio_path, bool is_group);
extern int SendDocumentWrapper(int id, char* c_number, char* c_video_path, char* c_caption, bool is_group);
extern int GetGroupInviteLinkWrapper(int id, char* c_jid, bool reset);
extern int GetGroupInviteLinkWrapper(int id, char* c_jid, bool reset, char* return_id);
extern int JoinGroupWithInviteLinkWrapper(int id, char* c_link);
extern int SetGroupAnnounceWrapper(int id, char* c_jid, bool announce);
extern int SetGroupLockedWrapper(int id, char* c_jid, bool locked);
extern int SetGroupNameWrapper(int id, char* c_jid, char* name);
extern int SetGroupTopicWrapper(int id, char* c_jid, char* topic);
extern int GetGroupInfoWrapper(int id, char* c_jid, char* return_id);
extern int Version();

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions whatsfly/proto/generate_protos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
protoc --proto_path=. --python_out=. */*.proto
Binary file added whatsfly/proto/waAdv/WAAdv.pb.raw
Binary file not shown.
43 changes: 43 additions & 0 deletions whatsfly/proto/waAdv/WAAdv.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
syntax = "proto2";
package WAAdv;
option go_package = "go.mau.fi/whatsmeow/proto/waAdv";

enum ADVEncryptionType {
E2EE = 0;
HOSTED = 1;
}

message ADVKeyIndexList {
optional uint32 rawID = 1;
optional uint64 timestamp = 2;
optional uint32 currentIndex = 3;
repeated uint32 validIndexes = 4 [packed=true];
optional ADVEncryptionType accountType = 5;
}

message ADVSignedKeyIndexList {
optional bytes details = 1;
optional bytes accountSignature = 2;
optional bytes accountSignatureKey = 3;
}

message ADVDeviceIdentity {
optional uint32 rawID = 1;
optional uint64 timestamp = 2;
optional uint32 keyIndex = 3;
optional ADVEncryptionType accountType = 4;
optional ADVEncryptionType deviceType = 5;
}

message ADVSignedDeviceIdentity {
optional bytes details = 1;
optional bytes accountSignatureKey = 2;
optional bytes accountSignature = 3;
optional bytes deviceSignature = 4;
}

message ADVSignedDeviceIdentityHMAC {
optional bytes details = 1;
optional bytes HMAC = 2;
optional ADVEncryptionType accountType = 3;
}
38 changes: 38 additions & 0 deletions whatsfly/proto/waAdv/WAAdv_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Loading

0 comments on commit 36a8be4

Please sign in to comment.