libricochet-go/examples/echobot/main.go

116 lines
2.9 KiB
Go
Raw Normal View History

2016-01-02 02:08:28 +00:00
package main
import (
2018-06-08 21:54:31 +00:00
"git.openprivacy.ca/openprivacy/libricochet-go"
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
"git.openprivacy.ca/openprivacy/libricochet-go/connection"
"git.openprivacy.ca/openprivacy/libricochet-go/identity"
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
"log"
2017-05-02 23:33:51 +00:00
"time"
2016-01-02 02:08:28 +00:00
)
2016-11-08 23:12:50 +00:00
// EchoBotService is an example service which simply echoes back what a client
// sends it.
2017-05-02 23:33:51 +00:00
type RicochetEchoBot struct {
connection.AutoConnectionHandler
messages chan string
}
2016-01-02 02:08:28 +00:00
2017-05-02 23:33:51 +00:00
func (echobot *RicochetEchoBot) ContactRequest(name string, message string) string {
return "Pending"
}
2017-05-02 23:33:51 +00:00
func (echobot *RicochetEchoBot) ContactRequestRejected() {
}
func (echobot *RicochetEchoBot) ContactRequestAccepted() {
}
func (echobot *RicochetEchoBot) ContactRequestError() {
}
func (echobot *RicochetEchoBot) ChatMessage(messageID uint32, when time.Time, message string) bool {
echobot.messages <- message
return true
}
2016-01-02 02:08:28 +00:00
func (echobot *RicochetEchoBot) OpenInbound() {
}
func (echobot *RicochetEchoBot) ChatMessageAck(messageID uint32, accepted bool) {
2017-05-02 23:33:51 +00:00
}
2016-01-02 02:08:28 +00:00
2017-05-02 23:33:51 +00:00
func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string) {
privateKey, _ := utils.LoadPrivateKeyFromFile(privateKeyFile)
echobot.messages = make(chan string)
echobot.Init()
2017-05-02 23:33:51 +00:00
echobot.RegisterChannelHandler("im.ricochet.contact.request", func() channels.Handler {
contact := new(channels.ContactRequestChannel)
contact.Handler = echobot
return contact
})
2017-05-02 23:33:51 +00:00
echobot.RegisterChannelHandler("im.ricochet.chat", func() channels.Handler {
chat := new(channels.ChatChannel)
chat.Handler = echobot
return chat
})
rc, err := goricochet.Open(hostname)
if err != nil {
log.Fatalf("could not connect to %s: %v", hostname, err)
}
known, err := connection.HandleOutboundConnection(rc).ProcessAuthAsClient(identity.Initialize("echobot", privateKey))
2017-05-02 23:33:51 +00:00
if err == nil {
go rc.Process(echobot)
if !known {
err := rc.Do(func() error {
_, err := rc.RequestOpenChannel("im.ricochet.contact.request",
&channels.ContactRequestChannel{
Handler: echobot,
Name: "EchoBot",
Message: "I LIVE 😈😈!!!!",
})
return err
})
2017-05-02 23:33:51 +00:00
if err != nil {
log.Printf("could not contact %s", err)
}
}
rc.Do(func() error {
_, err := rc.RequestOpenChannel("im.ricochet.chat", &channels.ChatChannel{Handler: echobot})
return err
})
2017-05-02 23:33:51 +00:00
for {
message := <-echobot.messages
log.Printf("Received Message: %s", message)
rc.Do(func() error {
log.Printf("Finding Chat Channel")
channel := rc.Channel("im.ricochet.chat", channels.Outbound)
if channel != nil {
log.Printf("Found Chat Channel")
chatchannel, ok := channel.Handler.(*channels.ChatChannel)
2017-05-02 23:33:51 +00:00
if ok {
chatchannel.SendMessage(message)
}
} else {
log.Printf("Could not find chat channel")
}
return nil
})
}
2016-01-02 02:08:28 +00:00
}
}
func main() {
2017-05-02 23:33:51 +00:00
echoBot := new(RicochetEchoBot)
echoBot.Connect("private_key", "flkjmgvjloyyzlpe")
2016-01-02 02:08:28 +00:00
}