// 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" "log" "os" "time" "github.com/mattermost/mattermost-server/v6/model" "go.cypherpunks.ru/recfile" "go.cypherpunks.ru/netrc" "go.stargrave.org/mmc" ) func main() { entrypoint := flag.String("entrypoint", mmc.GetEntrypoint(), "Entrypoint") pageNum := flag.Int("page", 0, "Page number") whole := flag.Bool("whole", false, "Whole history") direct := flag.Bool("direct", false, "Direct channel") flag.Parse() log.SetFlags(log.Lshortfile) chId := flag.Arg(0) postId := flag.Arg(1) login, password := netrc.Find(*entrypoint) if login == "" || password == "" { log.Fatalln("no credentials found for:", *entrypoint) } c := model.NewAPIv4Client("https://" + *entrypoint) c.Login(login, password) me, _, err := c.GetMe("") if err != nil { log.Fatalln(err) } users, err := mmc.GetUsers(c, nil) if err != nil { log.Fatalln(err) } if *direct { dc, _, err := c.CreateDirectChannel(me.Id, chId) if err != nil { log.Fatalln(err) } chId = dc.Id } var slice []*model.Post if *whole { for n := 0; ; n++ { log.Println("page:", n) time.Sleep(mmc.SleepTime) list, _, err := c.GetPostsBefore(chId, postId, n, mmc.PerPage, "", false) if err != nil { log.Fatalln(err) } s := list.ToSlice() slice = append(slice, s...) if len(s) < mmc.PerPage { break } } } else { list, _, err := c.GetPostsBefore(chId, postId, *pageNum, mmc.PerPage, "", false) if err != nil { log.Fatalln(err) } slice = list.ToSlice() } w := recfile.NewWriter(os.Stdout) for i := len(slice) - 1; i >= 0; i-- { if err = mmc.PostToRec( w, users, mmc.Post{P: slice[i], E: model.WebsocketEventPosted}, ); err != nil { log.Fatalln(err) } } c.Logout() }