aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: 6a078a60f6b17684f9b6311ca9d42b03090eedad (plain) (blame)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Telebonk is a reposter from Honk to Telegram.
package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"net/url"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"time"

	"git.aaoth.xyz/la-ninpre/telebonk/config"
)

// A Honk is a post from honk.
type Honk struct {
	ID     int       // unique id of a post
	What   string    // type of an action (post, repost, reply)
	Oondle string    // e-mail style handle of the original author of a post
	Oonker string    // url of the original author of a post
	XID    string    // url of a post, also unique
	RID    string    // url of a post that current post is replying to
	Date   time.Time // datetime of a post
	Precis string    // post summary
	Noise  string    // contents of a post
	Onts   []string  // a slice of tags in a post
	Donks  []*Donk   // a slice of attachments to a post

	MessID    int // telegram message_id
	ReplyToID int // telegram message_id of a message to reply to

	Action HonkAction
}

// A HonkAction tells what to do with the saved honk.
type HonkAction int

const (
	HonkNotSet HonkAction = iota
	HonkIgnore
	HonkSend
	HonkEdit
)

// A Donk stores metadata for media files.
type Donk struct {
	URL   string
	Media string // mime-type of an attachment
	Desc  string
}

// Check performs some checks on a Honk to filter out what's not going to be posted.
//
// It rejects honk if if falls into one of these categories:
//   - it is posted before the telebonk started
//   - it is replying to a honk that's not posted by telebonk (either a remote honk or old honk)
//   - it is of unsupported type (not a regular honk, reply or bonk)
//   - it contains a `#notg` tag.
//   - it is empty
func (h *Honk) Check() error {
	log.Print("check: checking honk #", h.ID) // info

	switch h.What {
	case "honked", "bonked":
		break
	case "honked back":
		hi, ok := honkMap[h.RID]
		if !ok {
			return fmt.Errorf("cannot reply to nonexisting telebonk")
		}
		h.ReplyToID = hi.MessID
	default:
		return fmt.Errorf("unsupported honk type: %s", h.What)
	}

	for _, ont := range h.Onts {
		if strings.ToLower(ont) == "#notg" {
			return fmt.Errorf("skipping #notg honk")
		}
	}

	if h.Noise == emptyNoise && len(h.Donks) == 0 {
		return fmt.Errorf("empty honk")
	}
	return nil
}

// Decide sets the Action of a Honk.
//
// It sets HonkIgnore to those honks that are: 1) old; 2) already sent and aren't edits.
func (h *Honk) Decide() {
	oldhonk, ok := honkMap[h.XID]
	if ok {
		if oldhonk.MessID == 0 || h.Date.Equal(oldhonk.Date) {
			h.Action = HonkIgnore
			h.save(oldhonk.MessID)
			return
		}
		log.Print("decide: honk #", h.XID, " is to be edited")
		h.Action = HonkEdit
		h.MessID = oldhonk.MessID
		return
	}
	if h.Date.Before(now) {
		h.Action = HonkIgnore
		h.save(0)
		return
	}
	log.Print("decide: honk #", h.ID, " is to be sent")
	h.Action = HonkSend
}

// save records a Honk to the honkMap
func (h *Honk) save(messID int) {
	h.MessID = messID
	honkMap[h.XID] = h
}

// forget unchecks a Honk from the honkMap
func (h *Honk) forget() {
	oldhonk, ok := honkMap[h.XID]
	if !ok {
		return
	}
	oldhonk.MessID = 0
	oldhonk.ReplyToID = 0
	honkMap[oldhonk.XID] = oldhonk
}

// A Mess holds data for a message to be sent to Telegram.
type Mess struct {
	Text             string `json:"text"`
	ChatID           string `json:"chat_id"`
	ParseMode        string `json:"parse_mode,omitempty"`
	MessageID        int    `json:"message_id,omitempty"`
	ReplyToMessageID int    `json:"reply_to_message_id,omitempty"`

	Document string `json:"document,omitempty"`
	Photo    string `json:"photo,omitempty"`
	Caption  string `json:"caption,omitempty"`

	kind messKind
}

// A TelegramResponse is a response from Telegram API.
type TelegramResponse struct {
	Ok          bool
	Description string
	Result      struct {
		MessageID int `json:"message_id"`
	}
}

// NewMess creates and populates a new Mess with default values.
func NewMess(parseMode, chatID string) *Mess {
	return &Mess{
		ParseMode: parseMode,
		ChatID:    chatID,
		kind:      messHonk,
	}
}

// NewMessFromHonk creates a slice of Mess objects from existing Honk.
func NewMessFromHonk(honk *Honk) []*Mess {
	var truncateWith = "...\n\nfull honk: " + honk.XID // hardcoded == bad
	// donks should be sent as a separate messages, so need to create all of 'em
	// cap(messes) = 1 for honk + 1 for each donk
	var messes = make([]*Mess, 0, 1+len(honk.Donks))

	messes = append(messes, NewMess("html"))
	for _, donk := range honk.Donks {
		donkMess := NewMess("") // donks don't contain html
		donkMess.Caption = TruncateNoise(donk.Desc, truncateWith, 1024)
		switch {
		case strings.HasPrefix(donk.Media, "image/"):
			donkMess.Photo = donk.URL
			donkMess.kind = messDonkPht
		case donk.Media == "application/pdf", donk.Media == "text/plain":
			donkMess.Document = donk.URL
			donkMess.kind = messDonkDoc
		}
		messes = append(messes, donkMess)
	}
	if honk.Noise == emptyNoise {
		messes = messes[1:] // just donks
	}

	if honk.Action == HonkEdit {
		// TODO: implement editing documents and photos
		messes[0].kind = messEdit
		messes[0].MessageID = honk.MessID
		messes = messes[:1] // don't donk if editing
	}

	var text = CalmNoise(honk.Noise)
	text = TruncateNoise(text, truncateWith, 4096)
	// bonk, then honk back - ok
	// honk back, then bonk - not gonna sync, is it ok?
	// upd: bonks work really confusing
	switch honk.What {
	case "honked":
		break
	case "honked back":
		messes[0].ReplyToMessageID = honk.ReplyToID
	case "bonked":
		oonker := fmt.Sprintf("<a href=\"%s\">%s</a>:", honk.Oonker, honk.Oondle)
		text = oonker + "\n" + text
	}

	// danger zone handling
	if strings.HasPrefix(honk.Precis, "DZ:") {
		text = strings.Join([]string{"<tg-spoiler>", "</tg-spoiler>"}, text)
		text = honk.Precis + "\n" + text
	}
	messes[0].Text = text

	return messes
}

// Send sends a Mess to Telegram.
func (m *Mess) Send() (*TelegramResponse, error) {
	var apiURL = botAPIMethod(tgSendMessage)

	switch m.kind {
	case messHonk:
		// noop
	case messEdit:
		apiURL = botAPIMethod(tgEditMessageText)
	case messDonkPht:
		apiURL = botAPIMethod(tgSendPhoto)
	case messDonkDoc:
		apiURL = botAPIMethod(tgSendDocument)
	}

	junk, err := json.Marshal(m)
	if err != nil {
		return nil, err
	}
	buf := bytes.NewBuffer(junk)
	req, err := http.NewRequest("POST", apiURL, buf)
	if err != nil {
		return nil, err
	}
	req.Header.Add("Content-type", "application/json")
	req.Header.Add("Content-length", strconv.Itoa(buf.Len()))

	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var res TelegramResponse
	json.NewDecoder(resp.Body).Decode(&res)
	if !res.Ok {
		return nil, fmt.Errorf("mess send: %s", res.Description)
	}

	return &res, nil
}

type messKind int

const (
	messHonk messKind = iota
	messEdit
	messDonkPht
	messDonkDoc
)

// XXX: figure out how to retreive these args without passing config object thousand times
func botAPIMethod(url, token, method string) string {
	return fmt.Sprintf("%s/bot%s/%s", url, token, method)
}

func checkTgAPI() error {
	var apiURL = botAPIMethod(tgGetMe)
	resp, err := client.Get(apiURL)
	if err != nil {
		return err
	}
	if resp.StatusCode != 200 {
		status, _ := io.ReadAll(resp.Body)
		return fmt.Errorf("status: %d: %s", resp.StatusCode, status)
	}
	return nil
}

// Telegram Bot API methods
const (
	tgGetMe           = "getMe"
	tgSendMessage     = "sendMessage"
	tgEditMessageText = "editMessageText"
	tgSendPhoto       = "sendPhoto"
	tgSendDocument    = "sendDocument"
)

// getHonks receives and unmarshals some honks from a Honk instance.
func getHonks(from, page, token string, after int) ([]*Honk, error) {
	query := url.Values{}
	query.Set("action", "gethonks")
	query.Set("page", page)
	query.Set("after", strconv.Itoa(after))
	apiurl := from + "/api?" + query.Encode()

	req, err := http.NewRequest("GET", apiurl, nil)
	if err != nil {
		return nil, err
	}
	req.Header.Add("Authorization", "Bearer "+token)

	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	// honk outputs junk like `{ "honks": [ ... ] }`, need to get into the list
	var honkJunk map[string][]*Honk
	err = json.NewDecoder(resp.Body).Decode(&honkJunk)
	if err != nil {
		// XXX: honk tokens last for a week or so. when one expires, shouldn't this say something meaningful instead of `unexpected v in blah-blah'?
		log.Print("gethonks: ", resp.Status)
		return nil, err
	}

	honks := honkJunk["honks"]
	// honk.ID monotonically increases, so it can be used to sort honks
	sort.Slice(honks, func(i, j int) bool { return honks[i].ID < honks[j].ID })

	return honks, nil
}

var (
	rePTags   = regexp.MustCompile(`<\/?p>`)
	reHlTags  = regexp.MustCompile(`<\/?span( class="[a-z]{2}")?>`)
	reBrTags  = regexp.MustCompile(`<br>`)
	reImgTags = regexp.MustCompile(`<img .*src="(.*)">`)
	reUlTags  = regexp.MustCompile(`<\/?ul>`)
	reTbTags  = regexp.MustCompile(`<table>.*</table>`)

	reLiTags = regexp.MustCompile(`<li>([^<>\/]*)<\/li>`)
	reHnTags = regexp.MustCompile(`<h[1-6]>(.*)</h[1-6]>`)
	reHrTags = regexp.MustCompile(`<hr>.*<\/hr>`)
	reBqTags = regexp.MustCompile(`<blockquote>(.*)<\/blockquote>`)
)

const emptyNoise = "<p></p>\n"

// CalmNoise erases and rewrites html tags that are not supported by Telegram.
func CalmNoise(s string) string {
	// delete these
	s = rePTags.ReplaceAllString(s, "")
	s = reHlTags.ReplaceAllString(s, "")
	s = reBrTags.ReplaceAllString(s, "")
	s = reImgTags.ReplaceAllString(s, "")
	s = reUlTags.ReplaceAllString(s, "")
	s = reTbTags.ReplaceAllString(s, "")

	// these can be repurposed
	s = reHrTags.ReplaceAllString(s, "---\n")
	s = reHnTags.ReplaceAllString(s, "<b>$1</b>\n\n")
	s = reBqTags.ReplaceAllString(s, "| <i>$1</i>")
	s = reLiTags.ReplaceAllString(s, "* $1\n")

	return strings.TrimSpace(s)
}

// TruncateNoise truncates a string up to `length - len(with)` characters long and adds `with` to the end.
func TruncateNoise(s, with string, length int) string {
	// telegram can handle posts no longer than 4096 (or 1024) characters _after_ the parsing of entities.
	// we could be clever and calculate the true length of text, but let's keep it simple and stupid.
	if len(s) <= length {
		return s
	}

	var b strings.Builder
	b.Grow(length)
	var end = length - 1 - len(with)
	for i, r := range s {
		if i >= end {
			break
		}
		b.WriteRune(r)
	}
	b.WriteString(with)
	return b.String()
}

// XXX: global and mutable
var conf = &config.Config{}

func init() {
	flag.StringVar(&conf.TgBotToken, "bot_token", "", "Telegram bot token")
	flag.StringVar(&conf.TgChatID, "chat_id", "", "Telegram chat_id")
	flag.StringVar(&conf.TgApiURL, "tgapi_url", "https://api.telegram.org", "Telegram API URL")
	flag.StringVar(&conf.HonkAuthToken, "honk_token", "", "Honk auth token")
	flag.StringVar(&conf.HonkPage, "honk_page", "myhonks", "Page to get honks from. Should be one of [atme, longago, home, myhonks]")
	flag.StringVar(&conf.HonkURL, "honk_url", "", "URL of a Honk instance")

	flag.Parse()

	if err := conf.Check(); err != nil {
		log.Fatal("conf:", err) // fail
	}
	conf.TgApiURL = strings.TrimRight(conf.TgApiURL, "/")
	if err := checkTgAPI(); err != nil {
		log.Fatal("tgAPI:", err) // fail
	}
}

var client = http.DefaultClient
var honkMap = make(map[string]*Honk) // XXX: not safe for use by multiple goroutines!
var now = time.Now()

func main() {
	var retry = 5

	log.Print("starting telebonk") // info
	for {
		honks, err := getHonks(conf.HonkURL, conf.HonkPage, conf.HonkAuthToken, 0)
		if err != nil {
			log.Print("gethonks:", err) // error
			retry--
			if retry == 0 {
				log.Fatal("gethonks: giving up") // fail
			}
			time.Sleep(5 * time.Second)
			continue
		}
	HonkLoop:
		for _, honk := range honks {
			honk.Decide()
			switch honk.Action {
			case HonkIgnore:
				continue
			case HonkSend, HonkEdit:
				if err := honk.Check(); err != nil {
					log.Print("honk check:", err) // error
					continue
				}
			}
			messes := NewMessFromHonk(honk)
			// messes[0] is a honk or a donk to be sent
			resp, err := messes[0].Send()
			if err != nil {
				log.Print("mess send", err) // error
				honk.forget()               // retry
				continue
			}
			// remember only the first mess' response
			honk.save(resp.Result.MessageID)
			for _, mess := range messes[1:] {
				if _, err := mess.Send(); err != nil {
					log.Print("mess send", err) // error
					continue HonkLoop
				}
			}
		}
		time.Sleep(30 * time.Second)
	}
}