2016-12-04 00:34:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/s-rah/go-ricochet"
|
2017-08-12 00:10:41 +00:00
|
|
|
"github.com/s-rah/go-ricochet/channels"
|
|
|
|
"github.com/s-rah/go-ricochet/connection"
|
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
2016-12-04 00:34:58 +00:00
|
|
|
"log"
|
2017-08-12 00:10:41 +00:00
|
|
|
"time"
|
2016-12-04 00:34:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EchoBotService is an example service which simply echoes back what a client
|
|
|
|
// sends it.
|
2017-08-12 00:10:41 +00:00
|
|
|
type RicochetEchoBot struct {
|
|
|
|
connection.AutoConnectionHandler
|
|
|
|
messages chan string
|
2016-12-04 00:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 00:10:41 +00:00
|
|
|
func (echobot *RicochetEchoBot) ContactRequest(name string, message string) string {
|
|
|
|
return "Pending"
|
2016-12-04 00:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 00:10:41 +00:00
|
|
|
func (echobot *RicochetEchoBot) ContactRequestRejected() {
|
|
|
|
}
|
|
|
|
func (echobot *RicochetEchoBot) ContactRequestAccepted() {
|
|
|
|
}
|
|
|
|
func (echobot *RicochetEchoBot) ContactRequestError() {
|
2016-12-04 00:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 00:10:41 +00:00
|
|
|
func (echobot *RicochetEchoBot) ChatMessage(messageID uint32, when time.Time, message string) bool {
|
|
|
|
echobot.messages <- message
|
2016-12-04 00:34:58 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-08-12 00:10:41 +00:00
|
|
|
func (echobot *RicochetEchoBot) ChatMessageAck(messageID uint32) {
|
|
|
|
|
2016-12-04 00:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 00:10:41 +00:00
|
|
|
func (echobot *RicochetEchoBot) Connect(privateKeyFile string, hostname string) {
|
|
|
|
|
|
|
|
privateKey, _ := utils.LoadPrivateKeyFromFile(privateKeyFile)
|
|
|
|
echobot.messages = make(chan string)
|
|
|
|
|
|
|
|
echobot.Init()
|
|
|
|
echobot.RegisterChannelHandler("im.ricochet.contact.request", func() channels.Handler {
|
|
|
|
contact := new(channels.ContactRequestChannel)
|
|
|
|
contact.Handler = echobot
|
|
|
|
return contact
|
|
|
|
})
|
|
|
|
echobot.RegisterChannelHandler("im.ricochet.chat", func() channels.Handler {
|
|
|
|
chat := new(channels.ChatChannel)
|
|
|
|
chat.Handler = echobot
|
|
|
|
return chat
|
|
|
|
})
|
|
|
|
|
|
|
|
rc, _ := goricochet.Open(hostname)
|
|
|
|
known, err := connection.HandleOutboundConnection(rc).ProcessAuthAsClient(privateKey)
|
|
|
|
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
|
|
|
|
})
|
|
|
|
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
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
if ok {
|
|
|
|
chatchannel.SendMessage(message)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("Could not find chat channel")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2016-12-04 00:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-08-12 00:10:41 +00:00
|
|
|
echoBot := new(RicochetEchoBot)
|
|
|
|
echoBot.Connect("private_key", "oqf7z4ot6kuejgam")
|
2016-12-04 00:34:58 +00:00
|
|
|
}
|