irc/plugins/auth/auth.go

76 lines
1.8 KiB
Go
Raw Normal View History

2024-05-14 05:51:42 +00:00
package auth
import (
"encoding/base64"
"tuxpa.in/a/irc/pkg/ircv3"
)
type SaslPlain struct {
Username string
Password string
}
2024-05-15 09:28:02 +00:00
func (saslplain *SaslPlain) Handle(w ircv3.MessageWriter, e *ircv3.Event) bool {
if e.Type == ircv3.EventTypeCONTROL && e.Msg.Command == "/EVENT_ON_SERVE" {
w.WriteMessage(ircv3.NewMessage("CAP", "REQ", "sasl"))
return false
}
m, _ := e.Msg, e.Context()
if m.Command == "CAP" && m.Param(0) == "*" && m.Param(1) == "ACK" && m.Param(2) == "sasl" {
w.WriteMessage(ircv3.NewMessage("AUTHENTICATE", "PLAIN"))
return false
}
if m.Command == "AUTHENTICATE" && m.Param(0) == "+" {
w.WriteMessage(ircv3.NewMessage("AUTHENTICATE", base64.StdEncoding.EncodeToString([]byte(
saslplain.Username+string([]byte{0})+
2024-05-14 05:51:42 +00:00
saslplain.Username+string([]byte{0})+
2024-05-15 09:28:02 +00:00
saslplain.Password,
))))
return false
}
switch m.Command {
case "903", "904", "905", "906", "907":
return true
}
return false
}
func (saslplain *SaslPlain) Middleware(next ircv3.Handler) ircv3.Handler {
// nothing to do
return next
2024-05-14 05:51:42 +00:00
}
type User struct {
Username string
Realname string
Hostname string
Server string
}
2024-05-15 09:28:02 +00:00
func (u *User) Handle(w ircv3.MessageWriter, e *ircv3.Event) bool {
if e.Type == ircv3.EventTypeCONTROL && e.Msg.Command == "/EVENT_ON_SERVE" {
w.WriteMessage(ircv3.NewMessage("USER", u.Username, u.Hostname, u.Server, u.Realname))
}
return true
}
2024-05-14 05:51:42 +00:00
func (u *User) Middleware(next ircv3.Handler) ircv3.Handler {
2024-05-15 09:28:02 +00:00
// nothing to do
return next
2024-05-14 05:51:42 +00:00
}
type Nick struct {
Nick string
}
2024-05-15 09:28:02 +00:00
func (u *Nick) Handle(w ircv3.MessageWriter, e *ircv3.Event) bool {
if e.Type == ircv3.EventTypeCONTROL && e.Msg.Command == "/EVENT_ON_SERVE" {
w.WriteMessage(ircv3.NewMessage("NICK", u.Nick))
}
return true
}
2024-05-14 05:51:42 +00:00
func (u *Nick) Middleware(next ircv3.Handler) ircv3.Handler {
2024-05-15 09:28:02 +00:00
return next
2024-05-14 05:51:42 +00:00
}