From 120dc5f468d1b6fb9f7450beae1dd8dbc7ae00c5 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 14 Jan 2021 15:34:08 -0800 Subject: [PATCH] add GetMessages --- .gitignore | 4 +++ README.md | 12 ++++++-- lib.go | 87 +++++++++++++++++++++++++++++++----------------------- 3 files changed, 64 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 485dee6..c24322b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ .idea +cwtch-sources.jar +cwtch.aar +libCwtch.h +libCwtch.so diff --git a/README.md b/README.md index 4bb7050..2a98146 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,14 @@ # Build Instructions... - go build -buildmode c-shared -o libCwtch.so + make linux + make android # Using -LD_LIBRARY_PATH set to point to libCwtch.so +## Linux Desktop: + + - `LD_LIBRARY_PATH` set to point to `libCwtch.so` + - or drop a symlink into `/usr/lib` + +## Android + +- copy `cwtch.aar` into `flutter_app/android/cwtch` diff --git a/lib.go b/lib.go index ba12705..841be96 100644 --- a/lib.go +++ b/lib.go @@ -32,48 +32,47 @@ func c_StartCwtch(dir_c *C.char, len C.int, tor_c *C.char, torLen C.int) { } func StartCwtch(appDir string, torPath string) { - //go func (appDir string, torPath string) { - log.SetLevel(log.LevelDebug) + log.SetLevel(log.LevelDebug) - log.Infof("Loading Cwtch Directory %v and tor path: %v", appDir, torPath) + log.Infof("Loading Cwtch Directory %v and tor path: %v", appDir, torPath) - mrand.Seed(int64(time.Now().Nanosecond())) - port := mrand.Intn(1000) + 9600 - controlPort := port + 1 + mrand.Seed(int64(time.Now().Nanosecond())) + port := mrand.Intn(1000) + 9600 + controlPort := port + 1 - // generate a random password (actually random, stored in memory, for the control port) - key := make([]byte, 64) - _, err := rand.Read(key) - if err != nil { - panic(err) - } + // generate a random password (actually random, stored in memory, for the control port) + key := make([]byte, 64) + _, err := rand.Read(key) + if err != nil { + panic(err) + } - log.Infof("making directory %v", appDir) - os.MkdirAll(path.Join(appDir, "/.tor", "tor"), 0700) - tor.NewTorrc().WithSocksPort(port).WithOnionTrafficOnly().WithControlPort(controlPort).WithHashedPassword(base64.StdEncoding.EncodeToString(key)).Build(filepath.Join(appDir, ".tor", "tor", "torrc")) - acn, err := tor.NewTorACNWithAuth(path.Join(appDir, "/.tor"), torPath, controlPort, tor.HashedPasswordAuthenticator{base64.StdEncoding.EncodeToString(key)}) - if err != nil { - log.Errorf("\nError connecting to Tor: %v\n", err) - } - //acn.WaitTillBootstrapped() + log.Infof("making directory %v", appDir) + os.MkdirAll(path.Join(appDir, "/.tor", "tor"), 0700) + tor.NewTorrc().WithSocksPort(port).WithOnionTrafficOnly().WithControlPort(controlPort).WithHashedPassword(base64.StdEncoding.EncodeToString(key)).Build(filepath.Join(appDir, ".tor", "tor", "torrc")) + acn, err := tor.NewTorACNWithAuth(path.Join(appDir, "/.tor"), torPath, controlPort, tor.HashedPasswordAuthenticator{base64.StdEncoding.EncodeToString(key)}) + if err != nil { + log.Errorf("\nError connecting to Tor: %v\n", err) + } + //acn.WaitTillBootstrapped() - newApp := app.NewApp(acn, appDir) - acnQueue = event.NewQueue() - newApp.GetPrimaryBus().Subscribe(event.ACNStatus, acnQueue) - peer.DefaultEventsToHandle = []event.Type{ - event.EncryptedGroupMessage, - event.NewMessageFromPeer, - event.PeerAcknowledgement, - event.NewGroupInvite, - event.PeerError, - event.SendMessageToGroupError, - event.NewGetValMessageFromPeer, - event.PeerStateChange, - } - newApp.LoadProfiles("be gay do crime") - newApp.LaunchPeers() - application = newApp - //}(appDir, torPath) + newApp := app.NewApp(acn, appDir) + acnQueue = event.NewQueue() + newApp.GetPrimaryBus().Subscribe(event.ACNStatus, acnQueue) + peer.DefaultEventsToHandle = []event.Type{ + event.EncryptedGroupMessage, + event.NewMessageFromPeer, + event.PeerAcknowledgement, + event.NewGroupInvite, + event.PeerError, + event.SendMessageToGroupError, + event.NewGetValMessageFromPeer, + event.PeerStateChange, + } + newApp.LoadProfiles("be gay do crime") + newApp.LaunchPeers() + application = newApp + log.Infof("libcwtch-go application SET\n") } //export c_ACNEvents @@ -179,6 +178,7 @@ func c_GetMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, ha return C.CString(GetMessage(profile, handle, int(message_index))) } +// Deprecate - 2021.01.14 - not used func GetMessage(profile, handle string, message_index int) string { message := application.GetPeer(profile).GetContact(handle).Timeline.Messages[message_index] bytes,_ := json.Marshal(message) @@ -186,5 +186,18 @@ func GetMessage(profile, handle string, message_index int) string { return string(bytes) } +//export c_GetMessages +func c_GetMessages(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, start C.int, end C.int) *C.char { + profile := C.GoStringN(profile_ptr, profile_len) + handle := C.GoStringN(handle_ptr, handle_len) + return C.CString(GetMessages(profile, handle, int(start), int(end))) +} + +func GetMessages(profile, handle string, start, end int) string { + messages := application.GetPeer(profile).GetContact(handle).Timeline.Messages[start:end] + bytes,_ := json.Marshal(messages) + return string(bytes) +} + // Leave as is, needed by ffi //func main() {} \ No newline at end of file