-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
304 lines (283 loc) · 9.43 KB
/
session.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
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
package store
/*
Sessions are unusual because they aren't all stored as rows in the "session"
database table. The set of sessions exposed by the "store" package is the union
of those stored in the database table and the set of future sessions defined by
the configuration. The fact that some of them are not realized in the database
is an internal implementation detail.
*/
import (
"database/sql"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/rothskeller/packet/envelope"
"github.com/rothskeller/packet/message"
"github.com/rothskeller/wppsvr/interval"
)
// A Session defines the parameters of a single session instance.
type Session struct {
ID int `yaml:"id"`
CallSign string `yaml:"callSign"`
Name string `yaml:"name"`
Prefix string `yaml:"prefix"`
Start time.Time `yaml:"start"`
End time.Time `yaml:"end"`
ReportToText []string `yaml:"-"`
ReportToHTML []string `yaml:"-"`
ToBBSes []string `yaml:"toBBSes"`
DownBBSes []string `yaml:"downBBSes"`
Retrieve []*Retrieval `yaml:"retrieve"`
MessageTypes []string `yaml:"messageTypes"`
ModelMessage string `yaml:"modelMessage"`
Instructions string `yaml:"instructions"`
RetrieveAt string `yaml:"retrieveAt"`
Report string `yaml:"-"`
Flags SessionFlags `yaml:"flags"`
ModelMsg message.Message `yaml:"-"`
RetrieveInterval interval.Interval `yaml:"-"`
}
// A Retrieval describes a single scheduled retrieval for a session.
type Retrieval struct {
BBS string `yaml:"bbs"`
LastRun time.Time `yaml:"lastRun"`
}
// SessionFlags is a collection of flags describing a session.
type SessionFlags uint8
// Values for SessionFlags
const (
Running SessionFlags = (1 << iota)
ExcludeFromWeek
DontKillMessages
DontSendResponses
Imported
Modified
ReportToSenders
)
// GetRunningSessions returns the (unordered) list of all running sessions.
func (s *Store) GetRunningSessions() (list []*Session) {
// Running sessions are always realized in the database, because the act
// of setting their running flag causes them to be realized. So this is
// just a database query.
return s.getSessionsWhere("flags&1")
}
// ExistSessions returns whether any sessions exist in the specified time range
// (inclusive start, exclusive end).
func (s *Store) ExistSessions(start, end time.Time) bool {
var (
dummy int
)
s.mutex.RLock()
defer s.mutex.RUnlock()
switch err := s.dbh.QueryRow(`SELECT 1 FROM session WHERE end>=? AND end <? LIMIT 1`, start, end).Scan(&dummy); err {
case nil:
return true
case sql.ErrNoRows:
return false
default:
panic(err)
}
}
// GetSession returns the session with the specified ID, or nil if there is
// none.
func (s *Store) GetSession(id int) *Session {
if list := s.getSessionsWhere("id=?", id); len(list) != 0 {
return list[0]
}
return nil
}
// GetSessions returns the set of sessions that end during the specified
// time range (inclusive start, exclusive end). The sessions are sorted by end
// time, then by call sign.
func (s *Store) GetSessions(start, end time.Time) (list []*Session) {
return s.getSessionsWhere("end>=? AND end<? ORDER BY end, callsign", start, end)
}
// getSessionsWhere returns the (unordered) list of sessions matching the
// specified criteria.
func (s *Store) getSessionsWhere(where string, args ...interface{}) (list []*Session) {
var (
rows *sql.Rows
err error
)
s.mutex.RLock()
defer s.mutex.RUnlock()
rows, err = s.dbh.Query("SELECT id, callsign, name, prefix, start, end, reporttotext, reporttohtml, tobbses, downbbses, messagetypes, modelmessage, instructions, retrieveat, report, flags FROM session WHERE "+where, args...)
if err != nil {
panic(err)
}
for rows.Next() {
var (
session Session
reporttotext string
reporttohtml string
tobbses string
downbbses string
messagetypes string
rows2 *sql.Rows
)
err = rows.Scan(&session.ID, &session.CallSign, &session.Name, &session.Prefix, &session.Start, &session.End, &reporttotext, &reporttohtml, &tobbses, &downbbses, &messagetypes, &session.ModelMessage, &session.Instructions, &session.RetrieveAt, &session.Report, &session.Flags)
if err != nil {
panic(err)
}
session.ReportToText = split(reporttotext)
session.ReportToHTML = split(reporttohtml)
session.ToBBSes = split(tobbses)
session.DownBBSes = split(downbbses)
session.MessageTypes = split(messagetypes)
session.RetrieveInterval = interval.Parse(session.RetrieveAt)
if session.ModelMessage != "" {
if env, body, err := envelope.ParseSaved(session.ModelMessage); err == nil {
session.ModelMsg = message.Decode(env.SubjectLine, body)
} else {
panic(err)
}
}
rows2, err = s.dbh.Query(`SELECT bbs, lastrun FROM retrieval WHERE session=?`, session.ID)
if err != nil {
panic(err)
}
for rows2.Next() {
var r Retrieval
if err = rows2.Scan(&r.BBS, &r.LastRun); err != nil {
panic(err)
}
session.Retrieve = append(session.Retrieve, &r)
}
if err = rows2.Err(); err != nil {
panic(err)
}
list = append(list, &session)
}
if err = rows.Err(); err != nil {
panic(err)
}
return list
}
// CreateSession creates a new session.
func (s *Store) CreateSession(session *Session) {
var (
tx *sql.Tx
result sql.Result
id int64
err error
)
s.mutex.Lock()
defer s.mutex.Unlock()
tx, err = s.dbh.Begin()
if err != nil {
panic(err)
}
result, err = tx.Exec("INSERT INTO session (callsign, name, prefix, start, end, reporttotext, reporttohtml, tobbses, downbbses, messagetypes, modelmessage, instructions, retrieveat, report, flags) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
session.CallSign, session.Name, session.Prefix, session.Start, session.End,
strings.Join(session.ReportToText, ";"), strings.Join(session.ReportToHTML, ";"), strings.Join(session.ToBBSes, ";"),
strings.Join(session.DownBBSes, ";"), strings.Join(session.MessageTypes, ";"), session.ModelMessage,
session.Instructions, session.RetrieveAt, session.Report, session.Flags)
if err != nil {
panic(err)
}
id, err = result.LastInsertId()
if err != nil {
panic(err)
}
session.ID = int(id)
for _, r := range session.Retrieve {
_, err = tx.Exec("INSERT INTO retrieval (session, bbs, lastrun) VALUES (?,?,?)",
session.ID, r.BBS, r.LastRun)
if err != nil {
panic(err)
}
}
if err = tx.Commit(); err != nil {
panic(err)
}
}
// UpdateSession updates an existing session.
func (s *Store) UpdateSession(session *Session) {
var (
tx *sql.Tx
err error
)
if session.ID == 0 {
// This is an unrealized session; we actually need to create it.
s.CreateSession(session)
return
}
s.mutex.Lock()
defer s.mutex.Unlock()
if tx, err = s.dbh.Begin(); err != nil {
panic(err)
}
_, err = tx.Exec("UPDATE session SET (callsign, name, prefix, start, end, reporttotext, reporttohtml, tobbses, downbbses, messagetypes, modelmessage, instructions, retrieveat, report, flags) = (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) WHERE id=?",
session.CallSign, session.Name, session.Prefix, session.Start, session.End,
strings.Join(session.ReportToText, ";"), strings.Join(session.ReportToHTML, ";"), strings.Join(session.ToBBSes, ";"),
strings.Join(session.DownBBSes, ";"), strings.Join(session.MessageTypes, ";"), session.ModelMessage,
session.Instructions, session.RetrieveAt, session.Report, session.Flags, session.ID)
if err != nil {
panic(err)
}
if _, err = tx.Exec(`DELETE FROM retrieval WHERE session=?`, session.ID); err != nil {
panic(err)
}
for _, r := range session.Retrieve {
_, err = tx.Exec("INSERT INTO retrieval (session, bbs, lastrun) VALUES (?,?,?)",
session.ID, r.BBS, r.LastRun)
if err != nil {
panic(err)
}
}
if err = tx.Commit(); err != nil {
panic(err)
}
}
// DeleteSession deletes a session.
func (s *Store) DeleteSession(session *Session) {
s.mutex.Lock()
defer s.mutex.Unlock()
if _, err := s.dbh.Exec(`DELETE FROM session WHERE id=?`, session.ID); err != nil {
panic(err)
}
}
// ModelImageCount returns the number of model images associated with the
// session. The images use 1-based numbering, so they are numbered 1 through
// the return value of this function, inclusive.
func (s *Store) ModelImageCount(sid int) (count int) {
prefix := fmt.Sprintf("s%d", sid)
matches, _ := filepath.Glob(prefix + "p*.*")
for _, match := range matches {
pstr := match[len(prefix)+1 : len(match)-len(filepath.Ext(match))]
if pnum, err := strconv.Atoi(pstr); err == nil && pnum > count {
count = pnum
}
}
return count
}
// ModelImage returns an open file handle to the specified model image page
// number, or nil if there is no such image. Model image page numbers start at
// 1. It is the caller's responsibility to close the handle.
func (s *Store) ModelImage(sid int, pnum int) (fh *os.File) {
matches, _ := filepath.Glob(fmt.Sprintf("s%dp%d.*", sid, pnum))
if len(matches) == 1 {
fh, _ = os.Open(matches[0])
}
return fh
}
// DeleteModelImages removes all model images for the specified session.
func (s *Store) DeleteModelImages(sid int) {
prefix := fmt.Sprintf("s%d", sid)
matches, _ := filepath.Glob(prefix + "p*.*")
for _, match := range matches {
os.Remove(match)
}
}
// SaveModelImage saves the specified model image for the specified session.
func (s *Store) SaveModelImage(sid int, pnum int, name string, body io.Reader) {
fname := fmt.Sprintf("s%dp%d%s", sid, pnum, filepath.Ext(name))
if fh, err := os.Create(fname); err == nil {
io.Copy(fh, body)
fh.Close()
}
}