-
Notifications
You must be signed in to change notification settings - Fork 7
/
topicchanger.go
176 lines (151 loc) · 3.48 KB
/
topicchanger.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
package main
import (
"database/sql"
"fmt"
"log"
"strings"
"time"
"gopkg.in/sorcix/irc.v2"
_ "github.com/lib/pq"
)
// I would prefer 🕖, but it’s not available in most fonts
const RobotBlockIdentifier = "ꜰ"
func TopicChanger() {
for {
Post("TOPIC #chaos-hd") // TODO: configurable channel
time.Sleep(5 * time.Minute)
}
}
func runnerTopicChanger(msg *irc.Message) error {
var topic string
channel := "#chaos-hd" // TODO: configurable channel
switch msg.Command {
// A user changed the topic
case irc.TOPIC:
if len(msg.Params) < 1 || msg.Params[0] != channel {
return nil
}
return updateTopic(channel, msg.Trailing())
// We received a reply to our periodic TOPIC command.
case irc.RPL_TOPIC:
topic = msg.Trailing()
fallthrough
case irc.RPL_NOTOPIC:
if len(msg.Params) < 2 || msg.Params[1] != channel {
return nil
}
return updateTopic(channel, topic)
}
return nil
}
const separator = "|"
func replaceTopic(current string) (string, error) {
nextEvent, err := getNextEvent()
if err != nil {
return "", err
}
nextEventPart := fmt.Sprintf(" %s %v ", RobotBlockIdentifier, nextEvent)
parts := strings.Split(current+" ", separator)
for idx, part := range parts {
if strings.Contains(part, RobotBlockIdentifier) {
parts[idx] = nextEventPart
}
}
if !strings.Contains(current, RobotBlockIdentifier) {
parts = append(parts, nextEventPart)
}
return strings.TrimSpace(strings.Join(parts, separator)), nil
}
func updateTopic(channel, currentTopic string) error {
newTopic, err := replaceTopic(currentTopic)
if err != nil {
return err
}
if strings.TrimSpace(currentTopic) != strings.TrimSpace(newTopic) {
log.Printf("updating topic from %q to %q", currentTopic, newTopic)
Post("TOPIC " + channel + " :" + newTopic)
}
return nil
}
type event struct {
stammtisch bool
override string
location sql.NullString
date time.Time
topic sql.NullString
speaker sql.NullString
}
func (evt *event) String() string {
t := ""
date := evt.date.Format("2006-01-02")
dateToday := time.Now().Format("2006-01-02")
dateTomorrow := time.Now().Add(24 * time.Hour).Format("2006-01-02")
switch date {
case dateToday:
t += "HEUTE (" + evt.date.Format("02.Jan") + ")"
case dateTomorrow:
t += "MORGEN (" + evt.date.Format("02.Jan") + ")"
default:
t += date
}
t += ": "
if evt.override != "" {
t += "Ausnahmsweise: " + evt.override
} else if evt.stammtisch {
t += "Stammtisch @ "
if evt.location.Valid {
t += evt.location.String
} else {
t += "TBA"
}
t += " https://www.noname-ev.de/yarpnarp.html"
t += " bitte zu/absagen"
} else {
t += "c¼h: "
if evt.topic.Valid {
t += evt.topic.String
if evt.speaker.Valid {
t += " von " + evt.speaker.String
}
} else {
t += "noch keine ◉︵◉"
}
}
return strings.TrimSpace(t)
}
var getNextEvent = func() (*event, error) {
const nextEventQuery = `
SELECT
stammtisch,
override,
location,
termine.date,
topic,
speaker
FROM termine
LEFT JOIN vortraege
ON termine.date = vortraege.date
WHERE termine.date >= NOW()::date
ORDER BY termine.date ASC
LIMIT 1
`
db, err := sql.Open("postgres", "dbname=nnev user=anon host=/var/run/postgresql sslmode=disable")
if err != nil {
return nil, err
}
defer db.Close()
var e event
if err := db.QueryRow(nextEventQuery).Scan(
&e.stammtisch,
&e.override,
&e.location,
&e.date,
&e.topic,
&e.speaker); err != nil {
return nil, err
}
if *verbose {
log.Printf("event from SQL: %#v", e)
}
return &e, nil
}