]> Sergey Matveev's repositories - mmc.git/blob - cmd/rd/main.go
Show filename
[mmc.git] / cmd / rd / main.go
1 // mmc -- Mattermost client
2 // Copyright (C) 2023 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package main
18
19 import (
20         "flag"
21         "fmt"
22         "io"
23         "log"
24         "os"
25         "path"
26         "time"
27
28         "github.com/mattermost/mattermost-server/v6/model"
29         "go.cypherpunks.ru/recfile"
30         "go.stargrave.org/mmc"
31 )
32
33 func printPost(m map[string][]string) {
34         var tag string
35         if len(m["Event"]) > 0 {
36                 switch m["Event"][0] {
37                 case model.WebsocketEventPostEdited:
38                         tag += "[EDIT] "
39                 case model.WebsocketEventPostDeleted:
40                         tag += "[DEL] "
41                 }
42         }
43         fmt.Printf("\a%s <%s> %s%s\n", m["Created"][0], m["Sender"][0], tag, m["Text"][0])
44         tag += "[FILE] "
45         for i, fileId := range m["File"] {
46                 fmt.Printf(
47                         "\a%s <%s> %s%s (%s)\n",
48                         m["Created"][0], m["Sender"][0], tag, fileId, m["FileName"][i],
49                 )
50         }
51 }
52
53 func main() {
54         lastNum := flag.Int("last", 10, "Only that number of messages")
55         flag.Parse()
56         where := flag.Arg(0)
57         lockPth := path.Join(where, mmc.OutRecLock)
58         unlock, err := mmc.Lock(lockPth)
59         if err != nil {
60                 log.Fatalln(err)
61         }
62         pth := path.Join(where, mmc.OutRec)
63         fd, err := os.Open(pth)
64         if err != nil {
65                 log.Fatalln(err)
66         }
67
68         r := recfile.NewReader(fd)
69         ms := make([]map[string][]string, 0)
70         for {
71                 m, err := r.NextMapWithSlice()
72                 if err != nil {
73                         if err == io.EOF {
74                                 break
75                         }
76                         log.Fatalln(err)
77                 }
78                 ms = append(ms, m)
79         }
80         unlock()
81         if len(ms) > *lastNum {
82                 ms = ms[len(ms)-*lastNum:]
83         }
84         for _, m := range ms {
85                 printPost(m)
86         }
87         fi, err := fd.Stat()
88         if err != nil {
89                 log.Fatalln(err)
90         }
91         size := fi.Size()
92         for {
93                 time.Sleep(mmc.SleepTime)
94                 fi, err = os.Stat(pth)
95                 if fi.Size() == size {
96                         continue
97                 }
98                 unlock, err = mmc.Lock(lockPth)
99                 if err != nil {
100                         log.Fatalln(err)
101                 }
102                 r = recfile.NewReader(fd)
103                 for {
104                         m, err := r.NextMapWithSlice()
105                         if err != nil {
106                                 if err == io.EOF {
107                                         break
108                                 }
109                                 log.Fatalln(err)
110                         }
111                         printPost(m)
112                 }
113                 size = fi.Size()
114                 unlock()
115         }
116 }