irc/plugins/auth/auth.go

76 lines
1.8 KiB
Go

package auth
import (
"encoding/base64"
"tuxpa.in/a/irc/pkg/ircv3"
)
type SaslPlain struct {
Username string
Password string
}
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})+
saslplain.Username+string([]byte{0})+
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
}
type User struct {
Username string
Realname string
Hostname string
Server string
}
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
}
func (u *User) Middleware(next ircv3.Handler) ircv3.Handler {
// nothing to do
return next
}
type Nick struct {
Nick string
}
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
}
func (u *Nick) Middleware(next ircv3.Handler) ircv3.Handler {
return next
}