summaryrefslogtreecommitdiffstats
path: root/bot/urltitle.go
blob: 4699148482f68f98d10cd7edd12865912411ac80 (plain)
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
// Copyright 2015 Robert S. Gerus. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package bot

import (
	"fmt"
	"log"
	"regexp"
	"strings"

	cfg "github.com/arachnist/gorepost/config"
	"github.com/arachnist/gorepost/irc"
)

var trimTitle *regexp.Regexp
var trimLink *regexp.Regexp

func getURLTitle(l string) string {
	title, err := httpGetXpath(l, "//head/title")
	if err == errElementNotFound {
		return "no title"
	} else if err != nil {
		return fmt.Sprint("error:", err)
	}

	return string(trimTitle.ReplaceAll([]byte(title), []byte{' '})[:])
}

func linktitle(output func(irc.Message), msg irc.Message) {
	var r []string

	for _, s := range strings.Split(msg.Trailing, " ") {
		b, err := regexp.Match("https?://", []byte(s))
		if err != nil {
			log.Println("Context:", msg.Context, "linktitle regex error:", err)
			return
		}

		s = string(trimLink.ReplaceAll([]byte(s), []byte("http"))[:])

		if b {
			t := getURLTitle(s)
			if t != "no title" {
				r = append(r, t)
			}
		}
	}

	if len(r) > 0 {
		t := cfg.LookupString(msg.Context, "LinkTitlePrefix") + strings.Join(r, cfg.LookupString(msg.Context, "LinkTitleDelimiter"))

		output(reply(msg, t))
	}
}

func init() {
	trimTitle, _ = regexp.Compile("[\\s]+")
	trimLink, _ = regexp.Compile("^.*?http")
	addCallback("PRIVMSG", "LINKTITLE", linktitle)
}