plz audit this commit, what did i fuck up?
This commit is contained in:
parent
49ae78f98b
commit
330bd96cdf
96
main.go
96
main.go
@ -169,7 +169,7 @@ func handleConn(conn net.Conn) {
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
//fmt.Printf(" %s\n", line)
|
||||
fmt.Printf(" %s\n", line)
|
||||
switch strings.ToUpper(msg.Command) {
|
||||
case "NICK":
|
||||
if len(msg.Params) > 0 {
|
||||
@ -515,16 +515,15 @@ func (c *Client) serveHistory(subcmd, target string, count int, params []string)
|
||||
} else {
|
||||
extra := count - len(ch.history)
|
||||
rows, err := db.Query(`SELECT msgid, timestamp, prefix, command, msg
|
||||
FROM messages
|
||||
WHERE channel=?
|
||||
ORDER BY timestamp DESC LIMIT ?`,
|
||||
FROM messages
|
||||
WHERE channel=?
|
||||
ORDER BY timestamp DESC LIMIT ?`,
|
||||
target, extra)
|
||||
if err != nil {
|
||||
c.fwrite(":server 718 %s %s :No history available\r\n", c.nick, target)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
dbMsgs := scanRows(rows, target)
|
||||
msgs = append(dbMsgs, ch.history...)
|
||||
}
|
||||
@ -535,18 +534,16 @@ func (c *Client) serveHistory(subcmd, target string, count int, params []string)
|
||||
return
|
||||
}
|
||||
msgid := strings.TrimPrefix(params[2], "msgid=")
|
||||
|
||||
rows, err := db.Query(`SELECT msgid, timestamp, prefix, command, msg
|
||||
FROM messages
|
||||
WHERE channel=? AND msgid < ?
|
||||
ORDER BY timestamp DESC LIMIT ?`,
|
||||
FROM messages
|
||||
WHERE channel=? AND msgid < ?
|
||||
ORDER BY timestamp DESC LIMIT ?`,
|
||||
target, msgid, count)
|
||||
if err != nil {
|
||||
c.fwrite(":server 718 %s %s :No history available\r\n", c.nick, target)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
msgs = scanRows(rows, target)
|
||||
|
||||
case "AFTER":
|
||||
@ -554,19 +551,41 @@ func (c *Client) serveHistory(subcmd, target string, count int, params []string)
|
||||
c.fwrite(":server 461 %s CHATHISTORY :Not enough parameters\r\n", c.nick)
|
||||
return
|
||||
}
|
||||
msgid := strings.TrimPrefix(params[2], "msgid=")
|
||||
|
||||
rows, err := db.Query(`SELECT msgid, timestamp, prefix, command, msg
|
||||
FROM messages
|
||||
WHERE channel=? AND msgid > ?
|
||||
ORDER BY timestamp ASC LIMIT ?`,
|
||||
target, msgid, count)
|
||||
arg := params[2]
|
||||
var rows *sql.Rows
|
||||
var err error
|
||||
|
||||
if strings.HasPrefix(arg, "msgid=") {
|
||||
msgid := strings.TrimPrefix(arg, "msgid=")
|
||||
rows, err = db.Query(`SELECT msgid, timestamp, prefix, command, msg
|
||||
FROM messages
|
||||
WHERE channel=? AND msgid > ?
|
||||
ORDER BY timestamp ASC LIMIT ?`,
|
||||
target, msgid, count)
|
||||
} else if strings.HasPrefix(arg, "timestamp=") {
|
||||
tsStr := strings.TrimPrefix(arg, "timestamp=")
|
||||
t, parseErr := time.Parse(time.RFC3339Nano, tsStr)
|
||||
if parseErr != nil {
|
||||
c.fwrite(":server 461 %s CHATHISTORY :Invalid timestamp\r\n", c.nick)
|
||||
return
|
||||
}
|
||||
rows, err = db.Query(`SELECT msgid, nick, timestamp, channel, prefix, command, msg
|
||||
FROM messages
|
||||
WHERE channel=? AND timestamp > ?
|
||||
ORDER BY timestamp ASC LIMIT ?`,
|
||||
target, t.UnixNano(), count)
|
||||
|
||||
} else {
|
||||
c.fwrite(":server 461 %s CHATHISTORY :Invalid anchor parameter\r\n", c.nick)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.fwrite(":server 718 %s %s :No history available\r\n", c.nick, target)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
msgs = scanRows(rows, target)
|
||||
|
||||
case "AROUND":
|
||||
@ -577,9 +596,9 @@ func (c *Client) serveHistory(subcmd, target string, count int, params []string)
|
||||
msgid := strings.TrimPrefix(params[2], "msgid=")
|
||||
half := count / 2
|
||||
beforeRows, _ := db.Query(`SELECT msgid, timestamp, prefix, command, msg
|
||||
FROM messages
|
||||
WHERE channel=? AND msgid < ?
|
||||
ORDER BY timestamp DESC LIMIT ?`,
|
||||
FROM messages
|
||||
WHERE channel=? AND msgid < ?
|
||||
ORDER BY timestamp DESC LIMIT ?`,
|
||||
target, msgid, half)
|
||||
beforeMsgs := scanRows(beforeRows, target)
|
||||
anchor, err := findMsgIndex(target, msgid)
|
||||
@ -588,22 +607,23 @@ func (c *Client) serveHistory(subcmd, target string, count int, params []string)
|
||||
anchorMsg = []HistoryMessage{*anchor}
|
||||
}
|
||||
afterRows, _ := db.Query(`SELECT msgid, timestamp, prefix, command, msg
|
||||
FROM messages
|
||||
WHERE channel=? AND msgid > ?
|
||||
ORDER BY timestamp ASC LIMIT ?`,
|
||||
FROM messages
|
||||
WHERE channel=? AND msgid > ?
|
||||
ORDER BY timestamp ASC LIMIT ?`,
|
||||
target, msgid, half)
|
||||
afterMsgs := scanRows(afterRows, target)
|
||||
|
||||
msgs = append(beforeMsgs, anchorMsg...)
|
||||
msgs = append(msgs, afterMsgs...)
|
||||
}
|
||||
|
||||
batchID := fmt.Sprintf("hist-%d", time.Now().UnixNano())
|
||||
c.fwrite(":server BATCH +%s chathistory %s\r\n", batchID, target)
|
||||
|
||||
for _, m := range msgs {
|
||||
tags := fmt.Sprintf("@msgid=%s;time=%s",
|
||||
m.MsgID,
|
||||
m.Time.UTC().Format(time.RFC3339))
|
||||
m.Time.UTC().Format(time.RFC3339Nano))
|
||||
c.fwrite("%s :%s %s %s :%s\r\n",
|
||||
tags,
|
||||
m.Prefix,
|
||||
@ -620,12 +640,12 @@ func findMsgIndex(channel, msgid string) (*HistoryMessage, error) {
|
||||
WHERE channel=? AND msgid=?`, channel, msgid)
|
||||
|
||||
var m HistoryMessage
|
||||
var ts string
|
||||
err := row.Scan(&m.MsgID, &ts, &m.Prefix, &m.Command, &m.Text)
|
||||
var tsInt int64
|
||||
err := row.Scan(&m.MsgID, &tsInt, &m.Prefix, &m.Command, &m.Text)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.Time, _ = time.Parse(time.RFC3339, ts)
|
||||
m.Time = time.Unix(0, tsInt).UTC()
|
||||
m.Params = []string{channel}
|
||||
return &m, nil
|
||||
}
|
||||
@ -926,7 +946,7 @@ func storeMessage(ch *Channel, sender *Client, target, text string) {
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?)`,
|
||||
msg.MsgID,
|
||||
sender.nick,
|
||||
msg.Time.UTC().Format(time.RFC3339),
|
||||
msg.Time.UnixNano(),
|
||||
target,
|
||||
msg.Prefix,
|
||||
msg.Command,
|
||||
@ -1038,20 +1058,10 @@ func sqlinitDB() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
/*
|
||||
msg := HistoryMessage{
|
||||
MsgID: fmt.Sprintf("%d", time.Now().UnixNano()),
|
||||
Time: time.Now(),
|
||||
Prefix: fmt.Sprintf("%s!%s@%s", sender.nick, sender.user, sender.host),
|
||||
Command: "PRIVMSG",
|
||||
Params: []string{target},
|
||||
Text: text,
|
||||
}
|
||||
*/
|
||||
stmt := `CREATE TABLE IF NOT EXISTS messages(
|
||||
msgid TEXT PRIMARY KEY,
|
||||
nick TEXT,
|
||||
timestamp TEXT,
|
||||
timestamp INTEGER,
|
||||
channel TEXT,
|
||||
prefix TEXT,
|
||||
command TEXT,
|
||||
@ -1064,9 +1074,11 @@ func scanRows(rows *sql.Rows, channel string) []HistoryMessage {
|
||||
var msgs []HistoryMessage
|
||||
for rows.Next() {
|
||||
var m HistoryMessage
|
||||
var ts string
|
||||
rows.Scan(&m.MsgID, &ts, &m.Prefix, &m.Command, &m.Text)
|
||||
m.Time, _ = time.Parse(time.RFC3339, ts)
|
||||
var tsInt int64
|
||||
if err := rows.Scan(&m.MsgID, &tsInt, &m.Prefix, &m.Command, &m.Text); err != nil {
|
||||
continue
|
||||
}
|
||||
m.Time = time.Unix(0, tsInt).UTC()
|
||||
m.Params = []string{channel}
|
||||
msgs = append(msgs, m)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user