aboutsummaryrefslogtreecommitdiff
path: root/cmd/_old/cadeybot/znc/znc.go
blob: 31997cf3a05bd1ea171f9eb6a3480694fb349fd0 (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
63
package znc

import (
	"bufio"
	"errors"
	"strings"
	"time"
)

const (
	dateFormat = `2006-01-02`
	timeFormat = `[15:04:05]`

	status = `***`
)

// LogMessage is an individual log message scraped from ZNC logs.
type LogMessage struct {
	Sent   time.Time
	Sender string
	Body   string
}

// Reader reads IRC log lines from a given io.Reader.
type Reader struct {
	S *bufio.Scanner
}

var (
	// ErrFailedFilter is returned when the znc message fails the matching filter.
	ErrFailedFilter = errors.New("znc: failed filter function")
)

// ReadOldLine does what you'd expect. Expects old style ZNC logs.
func (r Reader) ReadOldLine() (*LogMessage, error) {
	if !r.S.Scan() {
		return nil, r.S.Err()
	}

	result := LogMessage{}

	line := r.S.Text()
	sp := strings.SplitN(line, " ", 3)
	timeDate := sp[0] + " " + sp[1]
	t, err := time.Parse(dateFormat+" "+timeFormat, timeDate)
	if err != nil {
		return nil, err
	}
	result.Sent = t
	rest := sp[2]

	switch rest[0] {
	case '*':
		result.Sender = status
		result.Body = rest
	case '<':
		split := strings.SplitN(rest, " ", 2)
		result.Sender = split[0][1 : len(split[0])-1]
		result.Body = split[1]
	}

	return &result, nil
}