-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
411 lines (334 loc) · 8.93 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package main
// go get github.com/mattn/go-sqlite3 github.com/go-telegram-bot-api/telegram-bot-api mvdan.cc/xurls github.com/sirupsen/logrus
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"os/user"
"path/filepath"
"syscall"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
_ "github.com/mattn/go-sqlite3"
logrus "github.com/sirupsen/logrus"
xurls "mvdan.cc/xurls"
)
var log *logrus.Logger
var signalCh chan os.Signal
var ctx context.Context
var cancel context.CancelFunc
func init() {
log = logrus.New()
log.Out = os.Stdout
botInfo.readConfig()
log.Info("Init")
signalCh = make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, os.Kill,
syscall.SIGINT,
syscall.SIGTERM)
ctx, cancel = context.WithCancel(context.Background())
}
type BotInfo struct {
Token string `json:"token"`
Site string `json:"wallabag_site"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Username string `json:"username"`
Password string `json:"password"`
FilterUsers []string `json:"filter_users"`
}
var botInfo BotInfo
func (b *BotInfo) readConfig() {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
configFile := filepath.Join(usr.HomeDir, ".config", "t.me", "wallabag.json")
if _, err := os.Stat(configFile); err != nil {
log.Fatalf("Fail to open config file %s with error: %v", configFile, err)
}
jsonFile, err := os.Open(configFile)
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
var content []byte
if content, err = ioutil.ReadAll(jsonFile); err != nil {
log.Fatalf("Fail to read bot config: %v", err)
}
if err := json.Unmarshal(content, &b); err != nil {
log.Fatalf("Fail to parse bot config: %v", err)
}
if b.Token == "" || b.Site == "" || b.ClientID == "" || b.ClientSecret == "" || b.Username == "" || b.Password == "" {
log.Fatalf("Fail to parse bot token and wallabag credentials")
}
}
type saveURLRequest struct {
URL string
ChatID int64
MessageID int
}
const rescanInterval = 3600
func sqlite3Handler(diskQueue, reqQueue, diskAckQueue, ackQueue chan saveURLRequest) {
var database *sql.DB
var statement *sql.Stmt
var err error
database, err = sql.Open("sqlite3", "./wallabag.db")
if err != nil {
log.Fatalf("%v", err)
}
if statement, err = database.Prepare(`CREATE TABLE IF NOT EXISTS Requests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
URL TEXT,
ChatID INTEGER,
MessageID INTEGER,
saved INTEGER)`); err != nil {
log.Fatalf("%v", err)
}
if _, err = statement.Exec(); err != nil {
log.Fatalf("%v", err)
}
if statement, err = database.Prepare(`CREATE INDEX IF NOT EXISTS URLIndex ON Requests(URL);`); err != nil {
log.Fatalf("%v", err)
}
if _, err = statement.Exec(); err != nil {
log.Fatalf("%v", err)
}
go func() {
for {
timer := time.NewTimer(rescanInterval * time.Second)
var rows *sql.Rows
if rows, err = database.Query(`
SELECT URL, ChatID, MessageID
FROM Requests
WHERE saved == 0
`); err != nil {
log.Fatalf("%v", err)
}
var URL string
var ChatID int64
var MessageID int
for rows.Next() {
if err := rows.Scan(&URL, &ChatID, &MessageID); err != nil {
log.Error("Cannot read url from database")
continue
}
log.Infof("Unfinished %s, %d, %d", URL, ChatID, MessageID)
reqQueue <- saveURLRequest{URL: URL, ChatID: ChatID, MessageID: MessageID}
}
select {
case <-ctx.Done():
return
case <-timer.C:
}
}
}()
go func() {
for {
var r saveURLRequest
select {
case r = <-diskQueue:
case <-ctx.Done():
return
}
var count int
row := database.QueryRow("SELECT COUNT(*) FROM Requests WHERE URL = ?", r.URL)
err := row.Scan(&count)
if err != nil {
log.Errorf("Fail to get count of URL: %v", err)
continue
}
if count != 0 {
log.Infof("Skip existing URL: %s", r.URL)
continue
}
log.Infof("Saving request to disk first: %s, %d, %d", r.URL, r.ChatID, r.MessageID)
statement, err := database.Prepare("INSERT INTO Requests (URL, ChatID, MessageID, saved) VALUES (?, ?, ?, 0)")
if err != nil {
log.Errorf("Fail to insert request to SQLite: %v", err)
continue
}
_, err = statement.Exec(r.URL, r.ChatID, r.MessageID)
if err != nil {
log.Errorf("Fail to insert request to SQLite: %v", err)
continue
}
reqQueue <- r
}
}()
go func() {
for {
var r saveURLRequest
select {
case r = <-diskAckQueue:
case <-ctx.Done():
return
}
log.Infof("Update URL as saved: %s, %d, %d", r.URL, r.ChatID, r.MessageID)
var count int
row := database.QueryRow("SELECT COUNT(*) FROM Requests WHERE URL = ?", r.URL)
err := row.Scan(&count)
if err != nil {
log.Errorf("Fail to get count of URL: %v", err)
continue
}
if count == 0 {
log.Errorf("This URL should exist: %s", r.URL)
}
statement, err := database.Prepare("UPDATE Requests SET saved = 1 WHERE URL = ?")
if err != nil {
log.Errorf("Fail to update request to SQLite: %v", err)
continue
}
_, err = statement.Exec(r.URL)
if err != nil {
log.Errorf("Fail to update request to SQLite: %v", err)
continue
}
ackQueue <- r
}
}()
}
type wallabagTokenResp struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
}
func wallabagHandler(reqQueue, diskAckQueue chan saveURLRequest) {
bearer := ""
bearerExpire := time.Now()
go func() {
for {
var r saveURLRequest
select {
case r = <-reqQueue:
case <-ctx.Done():
return
}
if time.Now().After(bearerExpire) || bearer == "" {
requestURL := fmt.Sprintf(
"https://%s/oauth/v2/token?grant_type=password&client_id=%s&client_secret=%s&username=%s&password=%s",
botInfo.Site,
botInfo.ClientID,
botInfo.ClientSecret,
botInfo.Username,
botInfo.Password,
)
resp, err := http.Get(requestURL)
if err != nil {
log.Errorf("Fail to get token %v", err)
continue
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Errorf("Fail to get token %+v", resp.Header)
continue
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err)
continue
}
tokenResp := wallabagTokenResp{}
if err = json.Unmarshal(bodyBytes, &tokenResp); err != nil {
log.Error(err)
continue
}
bearerExpire = time.Now().Local().Add(time.Second * time.Duration(tokenResp.ExpiresIn))
bearer = tokenResp.AccessToken
log.Infof("New token fetched: %s", bearer)
}
req, err := http.NewRequest(
"POST",
fmt.Sprintf("https://%s/api/entries.json", botInfo.Site),
bytes.NewBuffer([]byte(fmt.Sprintf(`{ "url": "%s" }`, r.URL))))
if err != nil {
log.Error(err)
continue
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearer))
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Error(err)
continue
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Errorf("Wallabag returns error code: %v", resp.StatusCode)
continue
}
log.Infof("Wallabag says it is saved: %s, %d, %d", r.URL, r.ChatID, r.MessageID)
diskAckQueue <- r
}
}()
}
const timeOut = 60
func main() {
diskQueue := make(chan saveURLRequest, 100)
reqQueue := make(chan saveURLRequest, 100)
diskAckQueue := make(chan saveURLRequest, 100)
ackQueue := make(chan saveURLRequest, 100)
wallabagHandler(reqQueue, diskAckQueue)
sqlite3Handler(diskQueue, reqQueue, diskAckQueue, ackQueue)
filterUsers := map[string]bool{}
for _, s := range botInfo.FilterUsers {
filterUsers[s] = true
}
bot, err := tgbotapi.NewBotAPI(botInfo.Token)
if err != nil {
log.Panic(err)
}
bot.Debug = false
log.Infof("Authorized on account %s", bot.Self.UserName)
go func() {
for {
select {
case ackMsg := <-ackQueue:
log.Info("Sending")
msg := tgbotapi.NewMessage(ackMsg.ChatID, ackMsg.URL)
bot.Send(msg)
case <-ctx.Done():
return
}
}
}()
rxStrict := xurls.Strict()
offset := 0
for {
u := tgbotapi.NewUpdate(offset)
updates, err := bot.GetUpdates(u)
if err != nil {
log.Error(err)
}
timer := time.NewTimer(timeOut * time.Second)
for _, update := range updates {
offset = 1 + update.UpdateID
if update.Message == nil { // ignore any non-Message Updates
continue
}
log.Infof("Telegram received: %s", update.Message.Text)
if _, ok := filterUsers[update.Message.From.UserName]; !ok {
log.Infof("Telegram discards as it is from user: %s", update.Message.From.UserName)
continue
}
for _, r := range rxStrict.FindAllString(update.Message.Text, -1) {
log.Infof("Found URL: %s", r)
diskQueue <- saveURLRequest{URL: r, ChatID: update.Message.Chat.ID}
}
}
select {
case <-signalCh:
cancel()
os.Exit(0)
case <-timer.C:
break
}
}
}