1 | package soju
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "fmt"
|
---|
5 | "strconv"
|
---|
6 | "strings"
|
---|
7 | "time"
|
---|
8 |
|
---|
9 | "gopkg.in/irc.v3"
|
---|
10 | )
|
---|
11 |
|
---|
12 | // messageStore is a per-user store for IRC messages.
|
---|
13 | type messageStore interface {
|
---|
14 | Close() error
|
---|
15 | // LastMsgID queries the last message ID for the given network, entity and
|
---|
16 | // date. The message ID returned may not refer to a valid message, but can be
|
---|
17 | // used in history queries.
|
---|
18 | LastMsgID(network *network, entity string, t time.Time) (string, error)
|
---|
19 | LoadBeforeTime(network *network, entity string, t time.Time, limit int) ([]*irc.Message, error)
|
---|
20 | LoadAfterTime(network *network, entity string, t time.Time, limit int) ([]*irc.Message, error)
|
---|
21 | LoadLatestID(network *network, entity, id string, limit int) ([]*irc.Message, error)
|
---|
22 | Append(network *network, entity string, msg *irc.Message) (id string, err error)
|
---|
23 | }
|
---|
24 |
|
---|
25 | func formatMsgID(netID int64, entity, extra string) string {
|
---|
26 | return fmt.Sprintf("%v %v %v", netID, entity, extra)
|
---|
27 | }
|
---|
28 |
|
---|
29 | func parseMsgID(s string) (netID int64, entity, extra string, err error) {
|
---|
30 | l := strings.SplitN(s, " ", 3)
|
---|
31 | if len(l) != 3 {
|
---|
32 | return 0, "", "", fmt.Errorf("invalid message ID %q: expected 3 fields", s)
|
---|
33 | }
|
---|
34 | netID, err = strconv.ParseInt(l[0], 10, 64)
|
---|
35 | if err != nil {
|
---|
36 | return 0, "", "", fmt.Errorf("invalid message ID %q: %v", s, err)
|
---|
37 | }
|
---|
38 | return netID, l[1], l[2], nil
|
---|
39 | }
|
---|