From 6ba60c89c4fb949965624a9ea2c1374c5999f560 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Tue, 30 Dec 2025 23:33:48 -0500 Subject: [PATCH] move mysql serialization tools to shared pkgs --- irc/history/serialization.go | 19 +++++++++++++++++++ irc/mysql/history.go | 12 ++++++------ irc/mysql/serialization.go | 23 ----------------------- irc/utils/crypto.go | 5 +++++ 4 files changed, 30 insertions(+), 29 deletions(-) create mode 100644 irc/history/serialization.go delete mode 100644 irc/mysql/serialization.go diff --git a/irc/history/serialization.go b/irc/history/serialization.go new file mode 100644 index 00000000..4b7b5585 --- /dev/null +++ b/irc/history/serialization.go @@ -0,0 +1,19 @@ +// Copyright (c) 2020 Shivaram Lingamneni +// released under the MIT license + +package history + +import ( + "encoding/json" +) + +// 123 / '{' is the magic number that means JSON; +// if we want to do a binary encoding later, we just have to add different magic version numbers + +func MarshalItem(item *Item) (result []byte, err error) { + return json.Marshal(item) +} + +func UnmarshalItem(data []byte, result *Item) (err error) { + return json.Unmarshal(data, result) +} diff --git a/irc/mysql/history.go b/irc/mysql/history.go index 7a6988dd..16749347 100644 --- a/irc/mysql/history.go +++ b/irc/mysql/history.go @@ -652,12 +652,12 @@ func (mysql *MySQL) insertCorrespondentsEntry(ctx context.Context, target, corre } func (mysql *MySQL) insertBase(ctx context.Context, item history.Item) (id int64, err error) { - value, err := marshalItem(&item) + value, err := history.MarshalItem(&item) if err != nil { return 0, fmt.Errorf("could not marshal item: %w", err) } - msgidBytes, err := decodeMsgid(item.Message.Msgid) + msgidBytes, err := utils.DecodeSecretToken(item.Message.Msgid) if err != nil { return 0, fmt.Errorf("could not decode msgid: %w", err) } @@ -754,7 +754,7 @@ func (mysql *MySQL) DeleteMsgid(msgid, accountName string) (err error) { if accountName != "*" { var item history.Item - err = unmarshalItem(data, &item) + err = history.UnmarshalItem(data, &item) // delete if the entry is corrupt if err == nil && item.AccountName != accountName { return ErrDisallowed @@ -800,7 +800,7 @@ func (mysql *MySQL) Export(account string, writer io.Writer) { if err != nil { return } - err = unmarshalItem(blob, &item) + err = history.UnmarshalItem(blob, &item) if err != nil { return } @@ -828,7 +828,7 @@ func (mysql *MySQL) Export(account string, writer io.Writer) { } func (mysql *MySQL) lookupMsgid(ctx context.Context, msgid string, includeData bool) (result time.Time, id uint64, data []byte, err error) { - decoded, err := decodeMsgid(msgid) + decoded, err := utils.DecodeSecretToken(msgid) if err != nil { return } @@ -886,7 +886,7 @@ func (mysql *MySQL) selectItems(ctx context.Context, query string, args ...inter if err != nil { return nil, fmt.Errorf("could not scan history item: %w", err) } - err = unmarshalItem(blob, &item) + err = history.UnmarshalItem(blob, &item) if err != nil { return nil, fmt.Errorf("could not unmarshal history item: %w", err) } diff --git a/irc/mysql/serialization.go b/irc/mysql/serialization.go deleted file mode 100644 index 5226a1dd..00000000 --- a/irc/mysql/serialization.go +++ /dev/null @@ -1,23 +0,0 @@ -package mysql - -import ( - "encoding/json" - - "github.com/ergochat/ergo/irc/history" - "github.com/ergochat/ergo/irc/utils" -) - -// 123 / '{' is the magic number that means JSON; -// if we want to do a binary encoding later, we just have to add different magic version numbers - -func marshalItem(item *history.Item) (result []byte, err error) { - return json.Marshal(item) -} - -func unmarshalItem(data []byte, result *history.Item) (err error) { - return json.Unmarshal(data, result) -} - -func decodeMsgid(msgid string) ([]byte, error) { - return utils.B32Encoder.DecodeString(msgid) -} diff --git a/irc/utils/crypto.go b/irc/utils/crypto.go index a479b96d..7965b6eb 100644 --- a/irc/utils/crypto.go +++ b/irc/utils/crypto.go @@ -42,6 +42,11 @@ func GenerateSecretToken() string { return B32Encoder.EncodeToString(buf[:]) } +// return a compact representation of a token generated by GenerateSecretToken() +func DecodeSecretToken(t string) ([]byte, error) { + return B32Encoder.DecodeString(t) +} + // securely check if a supplied token matches a stored token func SecretTokensMatch(storedToken string, suppliedToken string) bool { // XXX fix a potential gotcha: if the stored token is uninitialized,