From 71a0a2483d0f11491aa97fef2bd069843208549a Mon Sep 17 00:00:00 2001 From: V-inz Date: Thu, 5 Jan 2017 00:39:45 +0100 Subject: [PATCH 1/7] auto-answer TextMessage, avoid recursive qoutes --- main.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.go b/main.go index 0a8665a..a194139 100644 --- a/main.go +++ b/main.go @@ -107,6 +107,12 @@ func main() { // play the audio if you like case o3.TextMessage: // respond with a quote of what was send to us. + + // but only if it's no a message we sent to ourselves, avoid recursive neverending qoutes + if (tid.String() == msg.Sender().String()) { + continue + } + // to make the quote render nicely in the app we use "markdown" // of the form "> PERSONWEQUOTE: Text of qoute\nSomething we wanna add." qoute := fmt.Sprintf("> %s: %s\n%s", msg.Sender(), msg.Text(), "Exactly!") From bffc8ce20b6a8f9cd36ecc0901b038c2e660ac84 Mon Sep 17 00:00:00 2001 From: kolAflash Date: Fri, 17 Feb 2017 00:14:55 +0100 Subject: [PATCH 2/7] Moved code into seperate functions (initialise, sendTestMsg, sendMsgChan) and fixed https://github.com/o3ma/o3demo/issues/7 by putting sendTestMsg into a separate thread. --- main.go | 49 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index a194139..708fb05 100644 --- a/main.go +++ b/main.go @@ -2,25 +2,38 @@ package main import ( - "fmt" "log" + "fmt" "os" "github.com/o3ma/o3" ) -func main() { +func main() { var ( pass = []byte{0xA, 0xB, 0xC, 0xD, 0xE} - tr o3.ThreemaRest idpath = "threema.id" abpath = "address.book" - tid o3.ThreemaID pubnick = "parrot" rid = "ZX9TZZ7P" + testMsg = "Say something!" ) + tr, tid, ctx, receiveMsgChan, sendMsgChan := initialise(pass, idpath, abpath, pubnick) + + go sendTestMsg(tr, abpath, rid, testMsg, ctx, sendMsgChan) + + receiveLoop(tid, ctx, receiveMsgChan, sendMsgChan) +} + + +func initialise(pass []byte, idpath string, abpath string, pubnick string) (o3.ThreemaRest, o3.ThreemaID, o3.SessionContext, <-chan o3.ReceivedMsg, chan<- o3.Message) { + var ( + tr o3.ThreemaRest + tid o3.ThreemaID + ) + // check whether an id file exists or else create a new one if _, err := os.Stat(idpath); err != nil { var err error @@ -58,6 +71,18 @@ func main() { } } + // let the session begin + fmt.Println("Starting session") + sendMsgChan, receiveMsgChan, err := ctx.Run() + if err != nil { + log.Fatal(err) + } + + return tr, tid, ctx, receiveMsgChan, sendMsgChan +} + + +func sendTestMsg(tr o3.ThreemaRest, abpath string, rid string, testMsg string, ctx o3.SessionContext, sendMsgChan chan<- o3.Message) { // check if we know the remote ID for // (just demonstration purposes \bc sending and receiving functions do this lookup for us) if _, b := ctx.ID.Contacts.Get(rid); b == false { @@ -80,19 +105,16 @@ func main() { } } - // let the session begin - fmt.Println("Starting session") - sendMsgChan, receiveMsgChan, err := ctx.Run() - if err != nil { - log.Fatal(err) - } - // send our initial message to our recipient fmt.Println("Sending initial message") - err = ctx.SendTextMessage(rid, "Say something!", sendMsgChan) + err := ctx.SendTextMessage(rid, testMsg, sendMsgChan) if err != nil { log.Fatal(err) } +} + + +func receiveLoop(tid o3.ThreemaID, ctx o3.SessionContext, receiveMsgChan <-chan o3.ReceivedMsg, sendMsgChan chan<- o3.Message) { // handle incoming messages for receivedMessage := range receiveMsgChan { @@ -117,7 +139,7 @@ func main() { // of the form "> PERSONWEQUOTE: Text of qoute\nSomething we wanna add." qoute := fmt.Sprintf("> %s: %s\n%s", msg.Sender(), msg.Text(), "Exactly!") // we use the convinient "SendTextMessage" function to send - err = ctx.SendTextMessage(msg.Sender().String(), qoute, sendMsgChan) + err := ctx.SendTextMessage(msg.Sender().String(), qoute, sendMsgChan) if err != nil { log.Fatal(err) } @@ -150,5 +172,4 @@ func main() { fmt.Printf("Unknown message type from: %s\nContent: %#v", msg.Sender(), msg) } } - } From 3282d3fd29496e5f37ebf7fc7d291c6d4a74c218 Mon Sep 17 00:00:00 2001 From: kolAflash Date: Fri, 17 Feb 2017 00:16:00 +0100 Subject: [PATCH 3/7] Added a comment, made sending a message more verbose and printing received messages. --- main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 708fb05..c1ea271 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ func main() { idpath = "threema.id" abpath = "address.book" pubnick = "parrot" - rid = "ZX9TZZ7P" + rid = "ZX9TZZ7P" // e.g. ZX9TZZ7P testMsg = "Say something!" ) @@ -106,7 +106,7 @@ func sendTestMsg(tr o3.ThreemaRest, abpath string, rid string, testMsg string, c } // send our initial message to our recipient - fmt.Println("Sending initial message") + fmt.Println("Sending initial message to " + rid + ": " + testMsg) err := ctx.SendTextMessage(rid, testMsg, sendMsgChan) if err != nil { log.Fatal(err) @@ -129,6 +129,7 @@ func receiveLoop(tid o3.ThreemaID, ctx o3.SessionContext, receiveMsgChan <-chan // play the audio if you like case o3.TextMessage: // respond with a quote of what was send to us. + fmt.Printf("---- Received Message from: %s ----\n%s\n-----------------------------------------\n", msg.Sender(), msg.Text()) // but only if it's no a message we sent to ourselves, avoid recursive neverending qoutes if (tid.String() == msg.Sender().String()) { From 3b028010412b57190f88e912d1a0b6e6654f2c76 Mon Sep 17 00:00:00 2001 From: kolAflash Date: Fri, 17 Feb 2017 00:16:38 +0100 Subject: [PATCH 4/7] Changed password from something non-ASCII to 01234567. --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index c1ea271..c5dbc74 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ import ( func main() { var ( - pass = []byte{0xA, 0xB, 0xC, 0xD, 0xE} + pass = []byte{0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37} // 01234567 idpath = "threema.id" abpath = "address.book" pubnick = "parrot" From 266595b407baefa7d93e559d2b1b4f129bc37ebb Mon Sep 17 00:00:00 2001 From: kolAflash Date: Sat, 18 Feb 2017 03:16:22 +0100 Subject: [PATCH 5/7] Using o3/testing functions to display message-id when sending. --- main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 5af8ec1..d77af1b 100644 --- a/main.go +++ b/main.go @@ -117,8 +117,8 @@ func sendTestMsg(tr o3.ThreemaRest, abpath string, rid string, testMsg string, c } // send our initial message to our recipient - fmt.Println("Sending initial message to " + rid + ": " + testMsg) - err := ctx.SendTextMessage(rid, testMsg, sendMsgChan) + err, tm := ctx.SendTextMessage(rid, testMsg, sendMsgChan) + fmt.Println("Sending initial message [" + fmt.Sprintf("%x", tm.ID()) + "] to " + rid + ": " + testMsg + "\n--------------------\n") if err != nil { log.Fatal(err) } @@ -140,7 +140,7 @@ func receiveLoop(tid o3.ThreemaID, gdpath string, ctx o3.SessionContext, receive // play the audio if you like case o3.TextMessage: // respond with a quote of what was send to us. - fmt.Printf("---- Received Message from: %s ----\n%s\n-----------------------------------------\n", msg.Sender(), msg.Text()) + fmt.Printf("\nMessage from %s: %s\n--------------------\n\n", msg.Sender(), msg.Text()) // but only if it's no a message we sent to ourselves, avoid recursive neverending qoutes if (tid.String() == msg.Sender().String()) { @@ -151,7 +151,7 @@ func receiveLoop(tid o3.ThreemaID, gdpath string, ctx o3.SessionContext, receive // of the form "> PERSONWEQUOTE: Text of qoute\nSomething we wanna add." qoute := fmt.Sprintf("> %s: %s\n%s", msg.Sender(), msg.Text(), "Exactly!") // we use the convinient "SendTextMessage" function to send - err := ctx.SendTextMessage(msg.Sender().String(), qoute, sendMsgChan) + err, _ := ctx.SendTextMessage(msg.Sender().String(), qoute, sendMsgChan) if err != nil { log.Fatal(err) } From 7022096dec5345e535c10b8001e015b74ddfc1be Mon Sep 17 00:00:00 2001 From: kolAflash Date: Sat, 18 Feb 2017 03:21:00 +0100 Subject: [PATCH 6/7] Minor spacing fix. --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index d77af1b..1317538 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,7 @@ func main() { pass = []byte{0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37} // 01234567 idpath = "threema.id" abpath = "address.book" - gdpath = "group.directory" + gdpath = "group.directory" pubnick = "parrot" rid = "ZX9TZZ7P" // e.g. ZX9TZZ7P testMsg = "Say something!" From b2906544a9f3c4fc46042653e39ceb24aff9f6ab Mon Sep 17 00:00:00 2001 From: kolAflash Date: Sat, 18 Feb 2017 13:29:38 +0100 Subject: [PATCH 7/7] Started using pointers instead of copying all sorts of stuff in function calls. --- main.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 1317538..6e130f2 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ func main() { } -func initialise(pass []byte, idpath string, abpath string, gdpath string, pubnick string) (o3.ThreemaRest, o3.ThreemaID, o3.SessionContext, <-chan o3.ReceivedMsg, chan<- o3.Message) { +func initialise(pass []byte, idpath string, abpath string, gdpath string, pubnick string) (*o3.ThreemaRest, *o3.ThreemaID, *o3.SessionContext, <-chan o3.ReceivedMsg, chan<- o3.Message) { var ( tr o3.ThreemaRest tid o3.ThreemaID @@ -89,11 +89,11 @@ func initialise(pass []byte, idpath string, abpath string, gdpath string, pubnic log.Fatal(err) } - return tr, tid, ctx, receiveMsgChan, sendMsgChan + return &tr, &tid, &ctx, receiveMsgChan, sendMsgChan } -func sendTestMsg(tr o3.ThreemaRest, abpath string, rid string, testMsg string, ctx o3.SessionContext, sendMsgChan chan<- o3.Message) { +func sendTestMsg(tr *o3.ThreemaRest, abpath string, rid string, testMsg string, ctx *o3.SessionContext, sendMsgChan chan<- o3.Message) { // check if we know the remote ID for // (just demonstration purposes \bc sending and receiving functions do this lookup for us) if _, b := ctx.ID.Contacts.Get(rid); b == false { @@ -125,7 +125,7 @@ func sendTestMsg(tr o3.ThreemaRest, abpath string, rid string, testMsg string, c } -func receiveLoop(tid o3.ThreemaID, gdpath string, ctx o3.SessionContext, receiveMsgChan <-chan o3.ReceivedMsg, sendMsgChan chan<- o3.Message) { +func receiveLoop(tid *o3.ThreemaID, gdpath string, ctx *o3.SessionContext, receiveMsgChan <-chan o3.ReceivedMsg, sendMsgChan chan<- o3.Message) { // handle incoming messages for receivedMessage := range receiveMsgChan { @@ -157,13 +157,13 @@ func receiveLoop(tid o3.ThreemaID, gdpath string, ctx o3.SessionContext, receive } // confirm to the sender that we received the message // this is how one can send messages manually without helper functions like "SendTextMessage" - drm, err := o3.NewDeliveryReceiptMessage(&ctx, msg.Sender().String(), msg.ID(), o3.MSGDELIVERED) + drm, err := o3.NewDeliveryReceiptMessage(ctx, msg.Sender().String(), msg.ID(), o3.MSGDELIVERED) if err != nil { log.Fatal(err) } sendMsgChan <- drm // give a thumbs up - upm, err := o3.NewDeliveryReceiptMessage(&ctx, msg.Sender().String(), msg.ID(), o3.MSGAPPROVED) + upm, err := o3.NewDeliveryReceiptMessage(ctx, msg.Sender().String(), msg.ID(), o3.MSGAPPROVED) if err != nil { log.Fatal(err) }