plz audit this commit, what did i fuck up?

This commit is contained in:
meera 2026-07-18 22:19:33 +03:00
parent 49ae78f98b
commit 330bd96cdf

64
main.go
View File

@ -169,7 +169,7 @@ func handleConn(conn net.Conn) {
if err != nil { if err != nil {
continue continue
} }
//fmt.Printf(" %s\n", line) fmt.Printf(" %s\n", line)
switch strings.ToUpper(msg.Command) { switch strings.ToUpper(msg.Command) {
case "NICK": case "NICK":
if len(msg.Params) > 0 { if len(msg.Params) > 0 {
@ -524,7 +524,6 @@ func (c *Client) serveHistory(subcmd, target string, count int, params []string)
return return
} }
defer rows.Close() defer rows.Close()
dbMsgs := scanRows(rows, target) dbMsgs := scanRows(rows, target)
msgs = append(dbMsgs, ch.history...) msgs = append(dbMsgs, ch.history...)
} }
@ -535,7 +534,6 @@ func (c *Client) serveHistory(subcmd, target string, count int, params []string)
return return
} }
msgid := strings.TrimPrefix(params[2], "msgid=") msgid := strings.TrimPrefix(params[2], "msgid=")
rows, err := db.Query(`SELECT msgid, timestamp, prefix, command, msg rows, err := db.Query(`SELECT msgid, timestamp, prefix, command, msg
FROM messages FROM messages
WHERE channel=? AND msgid < ? WHERE channel=? AND msgid < ?
@ -546,7 +544,6 @@ func (c *Client) serveHistory(subcmd, target string, count int, params []string)
return return
} }
defer rows.Close() defer rows.Close()
msgs = scanRows(rows, target) msgs = scanRows(rows, target)
case "AFTER": 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) c.fwrite(":server 461 %s CHATHISTORY :Not enough parameters\r\n", c.nick)
return return
} }
msgid := strings.TrimPrefix(params[2], "msgid=")
rows, err := db.Query(`SELECT msgid, timestamp, prefix, command, msg 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 FROM messages
WHERE channel=? AND msgid > ? WHERE channel=? AND msgid > ?
ORDER BY timestamp ASC LIMIT ?`, ORDER BY timestamp ASC LIMIT ?`,
target, msgid, count) 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 { if err != nil {
c.fwrite(":server 718 %s %s :No history available\r\n", c.nick, target) c.fwrite(":server 718 %s %s :No history available\r\n", c.nick, target)
return return
} }
defer rows.Close() defer rows.Close()
msgs = scanRows(rows, target) msgs = scanRows(rows, target)
case "AROUND": case "AROUND":
@ -597,13 +616,14 @@ func (c *Client) serveHistory(subcmd, target string, count int, params []string)
msgs = append(beforeMsgs, anchorMsg...) msgs = append(beforeMsgs, anchorMsg...)
msgs = append(msgs, afterMsgs...) msgs = append(msgs, afterMsgs...)
} }
batchID := fmt.Sprintf("hist-%d", time.Now().UnixNano()) batchID := fmt.Sprintf("hist-%d", time.Now().UnixNano())
c.fwrite(":server BATCH +%s chathistory %s\r\n", batchID, target) c.fwrite(":server BATCH +%s chathistory %s\r\n", batchID, target)
for _, m := range msgs { for _, m := range msgs {
tags := fmt.Sprintf("@msgid=%s;time=%s", tags := fmt.Sprintf("@msgid=%s;time=%s",
m.MsgID, m.MsgID,
m.Time.UTC().Format(time.RFC3339)) m.Time.UTC().Format(time.RFC3339Nano))
c.fwrite("%s :%s %s %s :%s\r\n", c.fwrite("%s :%s %s %s :%s\r\n",
tags, tags,
m.Prefix, m.Prefix,
@ -620,12 +640,12 @@ func findMsgIndex(channel, msgid string) (*HistoryMessage, error) {
WHERE channel=? AND msgid=?`, channel, msgid) WHERE channel=? AND msgid=?`, channel, msgid)
var m HistoryMessage var m HistoryMessage
var ts string var tsInt int64
err := row.Scan(&m.MsgID, &ts, &m.Prefix, &m.Command, &m.Text) err := row.Scan(&m.MsgID, &tsInt, &m.Prefix, &m.Command, &m.Text)
if err != nil { if err != nil {
return nil, err return nil, err
} }
m.Time, _ = time.Parse(time.RFC3339, ts) m.Time = time.Unix(0, tsInt).UTC()
m.Params = []string{channel} m.Params = []string{channel}
return &m, nil return &m, nil
} }
@ -926,7 +946,7 @@ func storeMessage(ch *Channel, sender *Client, target, text string) {
VALUES(?, ?, ?, ?, ?, ?, ?)`, VALUES(?, ?, ?, ?, ?, ?, ?)`,
msg.MsgID, msg.MsgID,
sender.nick, sender.nick,
msg.Time.UTC().Format(time.RFC3339), msg.Time.UnixNano(),
target, target,
msg.Prefix, msg.Prefix,
msg.Command, msg.Command,
@ -1038,20 +1058,10 @@ func sqlinitDB() error {
if err != nil { if err != nil {
return err 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( stmt := `CREATE TABLE IF NOT EXISTS messages(
msgid TEXT PRIMARY KEY, msgid TEXT PRIMARY KEY,
nick TEXT, nick TEXT,
timestamp TEXT, timestamp INTEGER,
channel TEXT, channel TEXT,
prefix TEXT, prefix TEXT,
command TEXT, command TEXT,
@ -1064,9 +1074,11 @@ func scanRows(rows *sql.Rows, channel string) []HistoryMessage {
var msgs []HistoryMessage var msgs []HistoryMessage
for rows.Next() { for rows.Next() {
var m HistoryMessage var m HistoryMessage
var ts string var tsInt int64
rows.Scan(&m.MsgID, &ts, &m.Prefix, &m.Command, &m.Text) if err := rows.Scan(&m.MsgID, &tsInt, &m.Prefix, &m.Command, &m.Text); err != nil {
m.Time, _ = time.Parse(time.RFC3339, ts) continue
}
m.Time = time.Unix(0, tsInt).UTC()
m.Params = []string{channel} m.Params = []string{channel}
msgs = append(msgs, m) msgs = append(msgs, m)
} }