persistence

This commit is contained in:
Cadey Ratio 2016-06-01 11:57:58 -07:00
parent a707bdca62
commit ea486916a1
2 changed files with 30 additions and 8 deletions

View File

@ -13,7 +13,6 @@ import (
"github.com/asdine/storm"
"gopkg.in/telegram-bot-api.v4"
"xeserv.us/r1459"
)
@ -138,8 +137,7 @@ func handleClient(c *Client) {
if c.DB == nil {
db, err := storm.Open("var/" + c.Nick + ".db")
if err != nil {
c.KillClient(err)
log.Fatal(err)
return
}
@ -179,6 +177,14 @@ func handleClient(c *Client) {
c.TGBot = bot
db, err := storm.Open("var/" + me.UserName + ".db")
if err != nil {
log.Fatal(err)
return
}
c.DB = db
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
@ -189,12 +195,24 @@ func handleClient(c *Client) {
go func(c *Client) {
for update := range updates {
if update.Message == nil {
continue
}
id := update.Message.Chat.ID
r := &Room{}
c.DB.One("RoomID", id, r)
r.client = c
err := c.DB.One("RoomID", id, r)
if err != nil {
c.SendLine(&r1459.RawLine{
Source: "xeserv.us",
Verb: "PRIVMSG",
Args: []string{
c.Nick,
fmt.Sprintf("Unknown room ID %v message %#v", update.Message.Chat.ID, update.Message.Chat),
},
})
}
if update.Message.Text != "" {
r.RecieveMessage(update)
@ -222,7 +240,7 @@ func handleClient(c *Client) {
Verb: "NOTICE",
Source: "xeserv.us",
Args: []string{
c.Nick, "JOIN <shortname> <id>",
c.Nick, "ASSOCIATE <shortname> <id>",
},
})

View File

@ -126,6 +126,10 @@ func (r *Room) Burst() []*r1459.RawLine {
func (r Room) RecieveMessage(update tgbotapi.Update) {
c := r.client
if c == nil {
return
}
log.Printf("[#%v] %s %s", r.Shortname, update.Message.From.UserName, update.Message.Text)
found := false
@ -151,7 +155,7 @@ func (r Room) RecieveMessage(update tgbotapi.Update) {
})
}
for _, msgLine := range strings.Split(update.Message.Text, " ") {
for _, msgLine := range strings.Split(update.Message.Text, "\n") {
c.SendLine(&r1459.RawLine{
Verb: "PRIVMSG",
Source: Mask(update.Message.From.UserName),