-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
301 lines (255 loc) · 6.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"context"
"flag"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"sort"
"strconv"
"strings"
"syscall"
"time"
"github.com/fatih/color"
"github.com/gotd/td/telegram"
"github.com/joho/godotenv"
"github.com/rusq/dlog"
"github.com/rusq/osenv/v2"
"github.com/rusq/tracer"
"github.com/schollz/progressbar/v3"
mtp "github.com/rusq/mtpwrap"
"github.com/rusq/mtpwrap/authflow"
"github.com/rusq/wipemychat/internal/session"
"github.com/rusq/wipemychat/internal/tui"
"github.com/rusq/wipemychat/internal/waipu"
)
const cacheDirName = "tgmsg_revoker"
const AppName = "Wipe My Chat for Telegram"
var (
version = "dev"
date = "just now"
commit = ""
versionSig = fmt.Sprintf("%s %s (built %s)", AppName, version, date)
)
var _ = godotenv.Load() // load environment variables from .env, if present
type Params struct {
CacheDirName string
ApiID int
ApiHash string
Phone string
// Reset requests removal of the session and API credentials files.
Reset bool
// Logout requests removal of the session file.
Logout bool
List bool
Batch chatIDs
Version bool
Verbose bool
Trace string
cacheDir string
}
func main() {
p, err := parseCmdLine()
if err != nil {
dlog.Fatal(err)
}
if p.Version {
ver(os.Stdout)
return
}
dlog.SetDebug(p.Verbose)
if err := p.initCacheDir(cacheDirName); err != nil {
dlog.Fatalf("failed to create cache directory: %s", err)
}
dlog.SetDebug(p.Verbose)
if err := run(context.Background(), p); err != nil {
dlog.Fatal(err)
}
}
type chatIDs []int64
func (c *chatIDs) Set(val string) error {
ss := strings.Split(val, ",")
ids := make([]int64, 0, len(ss))
for _, sID := range ss {
id, err := strconv.ParseInt(sID, 10, 64)
if err != nil {
return err
}
ids = append(ids, id)
}
*c = ids
return nil
}
func (c *chatIDs) String() string {
return fmt.Sprint([]int64(*c))
}
func parseCmdLine() (Params, error) {
p := Params{CacheDirName: cacheDirName}
{
// auth options
flag.IntVar(&p.ApiID, "api-id", osenv.Secret("APP_ID", 0), "Telegram API ID")
flag.StringVar(&p.ApiHash, "api-token", osenv.Secret("APP_HASH", ""), "Telegram API token")
flag.StringVar(&p.Phone, "phone", osenv.Value("PHONE", ""), "phone `number` in international format for authentication (optional)")
// reset options
flag.BoolVar(&p.Reset, "reset", false, "reset authentication (logout and remove credentials)")
flag.BoolVar(&p.Logout, "logout", false, "logout current account, use this to login as another user with the same API ID")
// batch mode
flag.BoolVar(&p.List, "list", false, "list channels and their IDs")
flag.Var(&p.Batch, "wipe", "batch mode, specify comma separated chat IDs on the command line")
// sundry
flag.BoolVar(&p.Version, "v", false, "print version and exit")
flag.BoolVar(&p.Verbose, "verbose", osenv.Value("DEBUG", "") != "", "verbose output")
flag.StringVar(&p.Trace, "trace", osenv.Value("TRACE_FILE", ""), "trace `filename`")
flag.Parse()
}
return p, nil
}
func (p *Params) initCacheDir(appName string) error {
cacheDir, err := os.UserCacheDir()
if err != nil {
return err
}
cacheDir = filepath.Join(cacheDir, appName)
if err := os.MkdirAll(cacheDir, 0o700); err != nil {
return err
}
p.cacheDir = cacheDir
return nil
}
func unlink(path string) error {
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
func run(ctx context.Context, p Params) error {
if p.Trace != "" {
tr := tracer.New(p.Trace)
if err := tr.Start(); err != nil {
return err
}
defer tr.End()
}
header(os.Stdout)
sessfile := filepath.Join(p.cacheDir, "session.dat")
if migrated, err := migratev120(sessfile); err != nil {
return err
} else if migrated {
fmt.Fprintln(os.Stdout, "session file was migrated to new format")
}
sessStorage := session.FileStorage{Path: filepath.Join(p.cacheDir, "session.dat")}
apiCredsFile := filepath.Join(p.cacheDir, "telegram.dat")
if p.Logout {
if err := unlink(sessStorage.Path); err != nil {
return err
} else {
fmt.Fprintln(os.Stdout, "you were logged out")
}
os.Exit(0)
}
if p.Reset {
for _, file := range []string{sessStorage.Path, apiCredsFile} {
if err := unlink(file); err != nil {
if os.IsNotExist(err) {
continue
}
return fmt.Errorf("error deleting %s: %w", file, err)
}
}
fmt.Fprintln(os.Stdout, "logged out and credentials removed")
os.Exit(0)
}
opts := telegram.Options{
SessionStorage: &sessStorage,
}
cl, err := mtp.New(ctx, p.ApiID, p.ApiHash,
mtp.WithAuth(authflow.NewTermAuth(p.Phone)),
mtp.WithApiCredsFile(apiCredsFile),
mtp.WithMTPOptions(opts),
mtp.WithDebug(p.Verbose),
)
if err != nil {
return err
}
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
dlog.Println("Connecting to telegram . . .")
if err := cl.Start(ctx); err != nil {
return err
}
defer func() {
if err := cl.Stop(); err != nil {
dlog.Printf("stop error: %s", err)
}
}()
if p.List {
return waipu.List(ctx, os.Stdout, cl)
} else if len(p.Batch) > 0 {
return waipu.Batch(ctx, cl, []int64(p.Batch))
} else {
// run UI
done, finished := fakeProgress("Getting chats . . .", 0)
chats, err := cl.GetChats(ctx)
close(done)
<-finished
if err != nil {
return err
}
sort.Slice(chats, func(i, j int) bool {
return chats[i].GetTitle() < chats[j].GetTitle()
})
dlog.Printf("got %d chats", len(chats))
tva := tui.New(ctx, cl)
if err := tva.Run(ctx, chats); err != nil {
return err
}
}
return nil
}
// fakeProgress starts a fake spinner and returns a channel that must be closed
// once the operation completes. interval is interval between iterations. If not
// set, will default to 50ms.
func fakeProgress(title string, interval time.Duration) (chan<- struct{}, <-chan struct{}) {
if interval == 0 {
interval = 50 * time.Millisecond
}
done := make(chan struct{})
finished := make(chan struct{})
go func() {
bar := progressbar.NewOptions(
-1,
progressbar.OptionSetDescription(title),
progressbar.OptionSetPredictTime(false),
progressbar.OptionSpinnerType(9),
)
t := time.NewTicker(interval)
defer t.Stop()
for {
select {
case <-done:
bar.Finish()
fmt.Println()
close(finished)
return
case <-t.C:
bar.Add(1)
}
}
}()
return done, finished
}
func header(w io.Writer) {
fmt.Fprintf(w,
"%s\n%s\n%s\n", versionSig, strings.Repeat("-", len(versionSig)),
color.New(color.Italic).Sprint("In loving memory of V. Gorban, 1967-2022."),
)
fmt.Fprintln(w)
}
func ver(w io.Writer) {
header(w)
if commit != "" {
fmt.Fprintf(w, "commit: %s\n", commit)
}
}