summaryrefslogtreecommitdiff
path: root/cmd/generate/forge_pipermail.go
blob: e015bb5da543bf0bab2ef1f4094db8b806b1e687 (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
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
package main

import (
	"fmt"
	"net/url"
	"regexp"
	"strconv"
	"strings"
	"time"
)

var (
	rePiperMailMessage = regexp.MustCompile(`^(https?://.*/pipermail/.*/)([0-4]{4}-(?:January|February|March|April|May|June|July|August|September|October|November|December))/([0-9]+)\.html$`)
	rePiperMailDate    = regexp.MustCompile(`^\s*<I>([^<]+)</I>\s*$`)
	rePiperMailReply   = regexp.MustCompile(`^\s*<LINK REL="made" HREF="(.*)">\s$`)
)

type PiperMail struct{}

var _ Forge = PiperMail{}

func (PiperMail) FetchStatus(urls []string) (string, error) {
	return "", nil
}

func (PiperMail) FetchSubmittedAt(urls []string) (time.Time, error) {
	for _, u := range urls {
		if !rePiperMailMessage.MatchString(u) {
			continue
		}
		htmlStr, err := httpGet(u, nil)
		if err != nil {
			return time.Time{}, err
		}
		for _, line := range strings.Split(htmlStr, "\n") {
			if m := rePiperMailDate.FindStringSubmatch(line); m != nil {
				return time.Parse(time.UnixDate, m[1])
			}
		}
	}
	return time.Time{}, nil
}

func (PiperMail) nextMonth(ym string) string {
	yStr, mStr, ok := strings.Cut(ym, "-")
	if !ok {
		panic(fmt.Errorf("invalid year-month: %q", ym))
	}
	switch mStr {
	case "January":
		return yStr + "-February"
	case "February":
		return yStr + "-March"
	case "March":
		return yStr + "-April"
	case "April":
		return yStr + "-May"
	case "May":
		return yStr + "-June"
	case "June":
		return yStr + "-July"
	case "July":
		return yStr + "-August"
	case "August":
		return yStr + "-September"
	case "September":
		return yStr + "-October"
	case "October":
		return yStr + "-November"
	case "November":
		return yStr + "-December"
	case "December":
		y, _ := strconv.Atoi(yStr)
		return fmt.Sprintf("%d-January", y+1)
	default:
		panic(fmt.Errorf("invalid year-month: %q", ym))
	}
}

func (PiperMail) messageID(u string) (string, error) {
}


func (p PiperMail) FetchLastUpdated(urls []string) (time.Time, User, error) {
	for _, u := range urls {
		m := rePiperMailMessage.FindStringSubmatch(u)
		if m == nil {
			continue
		}
		uBase := m[1]
		uYM := m[2]
		//uInt := m[3]

		htmlStr, err := httpGet(u, nil)
		if err != nil {
			return time.Time{}, User{}, err
		}
		var msgid string
		for _, line := range strings.Split(htmlStr, "\n") {
			if m := rePiperMailReply.FindStringSubmatch(line); m != nil {
				ru, err := url.Parse(m[1])
				if err != nil {
					continue
				}
				if msgid = ru.Query().Get("In-Reply-To"); msgid != "" {
					break
				}
			}
		}
		if msgid == "" {
			continue
		}
		mboxStr, err := httpGet(uBase+uYM+".txt.gz", nil)
		if err != nil {
			return time.Time{}, User{}, err
		}
	}
	return time.Time{}, User{}, nil
}