irc/pkg/capability/cap_servertime/capability.go

47 lines
1.1 KiB
Go
Raw Normal View History

2024-05-15 09:28:02 +00:00
package cap_servertime
import (
"strings"
"tuxpa.in/a/irc/pkg/ircv3"
)
type Capability struct {
supported bool
}
func (c *Capability) Middleware(next ircv3.Handler) ircv3.Handler {
return ircv3.HandlerFunc(func(w ircv3.MessageWriter, e *ircv3.Event) {
if c.supported {
// tString := e.Msg.Tags.Get("time")
// if tString != "" {
// parsedTime, err := time.Parse("2006-01-02T15:04:05.000Z", tString)
// if err == nil {
// ctx = context.WithValue(ctx, keyServerTime, parsedTime)
// }
// }
}
next.Handle(w, e)
})
}
func (c *Capability) Handle(w ircv3.MessageWriter, e *ircv3.Event) bool {
// dont have the capability? nothing to do. registered.
if e.Msg.Command == "CAP" && e.Msg.Param(0) == "*" && e.Msg.Param(1) == "LS" {
for _, v := range strings.Fields(e.Msg.Param(2)) {
if v == "server-time" {
w.WriteMessage(ircv3.NewMessage("CAP", "REQ", "server-time"))
return true
}
}
}
if e.Msg.Command == "CAP" && e.Msg.Param(2) == "server-time" {
if e.Msg.Param(1) == "ACK" {
c.supported = true
} else {
c.supported = false
}
}
return false
}