// mmc -- Mattermost client // Copyright (C) 2023 Sergey Matveev // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . package main import ( "flag" "fmt" "io" "log" "os" "path" "time" "github.com/mattermost/mattermost-server/v6/model" "go.cypherpunks.ru/recfile" "go.stargrave.org/mmc" ) func printPost(m map[string][]string) { var tag string if len(m["Event"]) > 0 { switch m["Event"][0] { case model.WebsocketEventPostEdited: tag += "[EDIT] " case model.WebsocketEventPostDeleted: tag += "[DEL] " } } fmt.Printf("\a%s <%s> %s%s\n", m["Created"][0], m["Sender"][0], tag, m["Text"][0]) tag += "[FILE] " for i, fileId := range m["File"] { fmt.Printf( "\a%s <%s> %s%s (%s)\n", m["Created"][0], m["Sender"][0], tag, fileId, m["FileName"][i], ) } } func main() { lastNum := flag.Int("last", 10, "Only that number of messages") flag.Parse() where := flag.Arg(0) lockPth := path.Join(where, mmc.OutRecLock) unlock, err := mmc.Lock(lockPth) if err != nil { log.Fatalln(err) } pth := path.Join(where, mmc.OutRec) fd, err := os.Open(pth) if err != nil { log.Fatalln(err) } r := recfile.NewReader(fd) ms := make([]map[string][]string, 0) for { m, err := r.NextMapWithSlice() if err != nil { if err == io.EOF { break } log.Fatalln(err) } ms = append(ms, m) } unlock() if len(ms) > *lastNum { ms = ms[len(ms)-*lastNum:] } for _, m := range ms { printPost(m) } fi, err := fd.Stat() if err != nil { log.Fatalln(err) } size := fi.Size() for { time.Sleep(mmc.SleepTime) fi, err = os.Stat(pth) if fi.Size() == size { continue } unlock, err = mmc.Lock(lockPth) if err != nil { log.Fatalln(err) } r = recfile.NewReader(fd) for { m, err := r.NextMapWithSlice() if err != nil { if err == io.EOF { break } log.Fatalln(err) } printPost(m) } size = fi.Size() unlock() } }