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"
|
2023-02-07 21:42:08 +00:00
|
|
|
_ "github.com/mutecomm/go-sqlcipher/v4"
|
2019-10-28 02:09:02 +00:00
|
|
|
"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()
|
|
|
|
|
2023-02-07 21:42:08 +00:00
|
|
|
log.Infof("echbot address: %v", cwtchbot.Peer.GetOnion())
|
|
|
|
|
2019-10-28 02:09:02 +00:00
|
|
|
for {
|
|
|
|
log.Infof("Process.....\n")
|
|
|
|
message := cwtchbot.Queue.Next()
|
2023-02-07 21:42:08 +00:00
|
|
|
cid, _ := cwtchbot.Peer.FetchConversationInfo(message.Data[event.RemotePeer])
|
2019-10-28 02:09:02 +00:00
|
|
|
switch message.EventType {
|
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]}))
|
2021-04-29 18:46:40 +00:00
|
|
|
msg := cwtchbot.UnpackMessage(message.Data[event.Data])
|
|
|
|
log.Infof("Message: %v", msg)
|
|
|
|
reply := string(cwtchbot.PackMessage(msg.Overlay, msg.Data))
|
2023-02-07 21:42:08 +00:00
|
|
|
cwtchbot.Peer.SendMessage(cid.ID, 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])
|
2023-02-07 21:42:08 +00:00
|
|
|
// accept the stranger as a new contact
|
|
|
|
cwtchbot.Peer.NewContactConversation("stranger", model.DefaultP2PAccessControl(), true)
|
2020-11-09 21:45:13 +00:00
|
|
|
}
|
2019-10-28 02:09:02 +00:00
|
|
|
default:
|
|
|
|
log.Infof("New Event: %v", message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|