2017-10-18 07:59:44 +00:00
|
|
|
package odClient
|
2017-09-07 15:11:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/s-rah/go-ricochet/connection"
|
|
|
|
"time"
|
|
|
|
"log"
|
|
|
|
"github.com/s-rah/go-ricochet/channels"
|
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
|
|
|
"github.com/s-rah/go-ricochet"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ODClient struct {
|
|
|
|
connection.AutoConnectionHandler
|
2017-09-14 05:48:40 +00:00
|
|
|
connection *connection.Connection
|
2017-09-07 15:11:23 +00:00
|
|
|
recvMessages chan string
|
|
|
|
deviceName string
|
|
|
|
deviceLevel int
|
|
|
|
batteryLevel string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (odClient *ODClient) Connect(privateKeyData string, serverAddr string) error {
|
|
|
|
log.Println("ODCLient.Connect()")
|
|
|
|
privateKey, err := utils.ParsePrivateKey([]byte(privateKeyData))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("error parsing private key: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
odClient.Init(privateKey, serverAddr)
|
|
|
|
|
|
|
|
odClient.RegisterChannelHandler("im.ricochet.contact.request", func() channels.Handler {
|
2017-09-14 05:48:40 +00:00
|
|
|
log.Println("handler for contact.request")
|
2017-09-07 15:11:23 +00:00
|
|
|
contact := new(channels.ContactRequestChannel)
|
|
|
|
contact.Handler = odClient
|
|
|
|
return contact
|
|
|
|
})
|
|
|
|
|
|
|
|
odClient.RegisterChannelHandler("im.ricochet.chat", func() channels.Handler {
|
2017-09-14 05:48:40 +00:00
|
|
|
log.Println("handler for chat")
|
2017-09-07 15:11:23 +00:00
|
|
|
chat := new(channels.ChatChannel)
|
|
|
|
chat.Handler = odClient
|
|
|
|
return chat
|
|
|
|
})
|
|
|
|
|
|
|
|
odClient.recvMessages = make(chan string)
|
|
|
|
|
|
|
|
log.Println("ODClient connecting...")
|
2017-09-14 05:48:40 +00:00
|
|
|
odClient.connection, err = goricochet.Open(serverAddr)
|
2017-09-07 15:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error connecting %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Println("ODCleint connected!")
|
2017-09-23 16:16:14 +00:00
|
|
|
log.Println("Starting auth...")
|
2017-09-14 05:48:40 +00:00
|
|
|
known, err := connection.HandleOutboundConnection(odClient.connection).ProcessAuthAsClient(privateKey)
|
2017-09-07 15:11:23 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error handling auth: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-09-14 05:48:40 +00:00
|
|
|
|
|
|
|
log.Println("go Process")
|
|
|
|
go odClient.connection.Process(odClient)
|
|
|
|
|
|
|
|
if !known {
|
|
|
|
err := odClient.connection.RequestOpenChannel("im.ricochet.contact.request", odClient)
|
|
|
|
if err != nil {
|
2017-09-23 16:16:14 +00:00
|
|
|
log.Printf("Could not auth with server", err)
|
2017-09-14 05:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("ODClient: Authenticated")
|
|
|
|
|
|
|
|
log.Println("RequestOpenChanel chat")
|
|
|
|
err = odClient.connection.RequestOpenChannel("im.ricochet.chat", odClient)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2017-09-23 16:16:14 +00:00
|
|
|
log.Println("Connection fully initiated, chat channel open!")
|
2017-09-07 15:11:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-23 16:05:09 +00:00
|
|
|
func (odClient *ODClient) Disconnect() {
|
|
|
|
log.Println("Break()...")
|
|
|
|
odClient.connection.Break()
|
|
|
|
log.Println("Conn.Close()...")
|
|
|
|
odClient.connection.Conn.Close()
|
|
|
|
}
|
|
|
|
|
2017-09-14 05:48:40 +00:00
|
|
|
/*func (odClient *ODClient) RequestContact() {
|
|
|
|
odClient.connection.Do(func() error {
|
|
|
|
channel := odClient.connection.Channel("im.ricochet.contact.request", channels.Outbound)
|
|
|
|
if channel != nil {
|
|
|
|
contactRequestChannel, ok := (*channel.Handler).(*channels.ContactRequestChannel)
|
|
|
|
if ok {
|
|
|
|
//contactRequestChannel.Handler
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
log.Println("ERROR: failed to find chat channel")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}*/
|
|
|
|
|
|
|
|
func (odClient *ODClient) SendMessage(message string) {
|
|
|
|
odClient.connection.Do(func() error {
|
|
|
|
channel := odClient.connection.Channel("im.ricochet.chat", channels.Outbound)
|
|
|
|
if channel != nil {
|
|
|
|
chatchannel, ok := (*channel.Handler).(*channels.ChatChannel)
|
|
|
|
if ok {
|
|
|
|
chatchannel.SendMessage(message)
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
log.Println("ERROR: failed to find chat channel")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-18 15:35:24 +00:00
|
|
|
func (odClient *ODClient) GetMessage() string {
|
|
|
|
message := <-odClient.recvMessages
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:11:23 +00:00
|
|
|
/************* Chat Channel Handler ********/
|
|
|
|
|
|
|
|
// ChatMessage passes the response to recvMessages.
|
|
|
|
func (odc *ODClient) ChatMessage(messageID uint32, when time.Time, message string) bool {
|
2017-09-23 16:16:14 +00:00
|
|
|
//log.Printf("Received Message: %s", message)
|
2017-09-07 15:11:23 +00:00
|
|
|
odc.recvMessages <- message
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChatMessageAck does nothing.
|
|
|
|
func (odc *ODClient) ChatMessageAck(messageID uint32) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/************* Contact Channel Handler ********/
|
|
|
|
|
|
|
|
// GetContactDetails is purposely empty
|
|
|
|
func (odc *ODClient) GetContactDetails() (string, string) {
|
2017-09-14 05:48:40 +00:00
|
|
|
return "AndroidOD Client", ""
|
2017-09-07 15:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContactRequest denies any contact request.
|
|
|
|
func (odc *ODClient) ContactRequest(name string, message string) string {
|
|
|
|
return "Rejected"
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContactRequestRejected purposly does nothing.
|
|
|
|
func (odc *ODClient) ContactRequestRejected() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContactRequestAccepted purposly does nothing.
|
|
|
|
func (odc *ODClient) ContactRequestAccepted() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContactRequestError purposly does nothing.
|
|
|
|
func (odc *ODClient) ContactRequestError() {
|
|
|
|
}
|