cwtchbot/cmd/echobot/main.go

47 lines
1.6 KiB
Go
Raw Normal View History

2020-11-09 21:45:13 +00:00
package main
2019-10-28 02:09:02 +00:00
import (
"cwtch.im/cwtch/event"
2020-11-09 21:45:13 +00:00
"cwtch.im/cwtch/model"
"cwtch.im/cwtch/protocol/connections"
"git.openprivacy.ca/openprivacy/log"
2019-10-28 02:09:02 +00:00
"git.openprivacy.ca/sarah/cwtchbot"
"os/user"
"path"
)
func main() {
user, _ := user.Current()
log.SetLevel(log.LevelInfo)
cwtchbot := bot.NewCwtchBot(path.Join(user.HomeDir, "/.echobot/"), "echobot")
2020-11-09 21:45:13 +00:00
2019-10-28 02:09:02 +00:00
cwtchbot.Launch()
for {
log.Infof("Process.....\n")
message := cwtchbot.Queue.Next()
switch message.EventType {
case event.NewMessageFromGroup:
2020-11-09 21:45:13 +00:00
if message.Data[event.RemotePeer] != cwtchbot.Peer.GetOnion() {
2019-10-28 02:09:02 +00:00
log.Infof("New Message: %v\v", message.Data[event.Data])
2021-05-13 20:02:41 +00:00
cwtchbot.Peer.SendMessageToGroupTracked(message.Data[event.GroupID], message.Data[event.Data])
2019-10-28 02:09:02 +00:00
}
2020-11-09 21:45:13 +00:00
case event.NewMessageFromPeer:
log.Infof("New Event: %v", message)
cwtchbot.Queue.Publish(event.NewEvent(event.PeerAcknowledgement, map[event.Field]string{event.EventID: message.EventID, event.RemotePeer: message.Data[event.RemotePeer]}))
msg := cwtchbot.UnpackMessage(message.Data[event.Data])
log.Infof("Message: %v", msg)
reply := string(cwtchbot.PackMessage(msg.Overlay, msg.Data))
cwtchbot.Peer.SendMessageToPeer(message.Data[event.RemotePeer], reply)
2020-11-09 21:45:13 +00:00
case event.PeerStateChange:
state := message.Data[event.ConnectionState]
if state == connections.ConnectionStateName[connections.AUTHENTICATED] {
log.Infof("Auto approving stranger %v", message.Data[event.RemotePeer])
cwtchbot.Peer.AddContact("stranger", message.Data[event.RemotePeer], model.AuthApproved)
}
2019-10-28 02:09:02 +00:00
default:
log.Infof("New Event: %v", message)
}
}
}