-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:thoj/go-ircevent
- Loading branch information
Showing
4 changed files
with
128 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package irc | ||
|
||
import ( | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type SASLResult struct { | ||
Failed bool | ||
Err error | ||
} | ||
|
||
func (irc *Connection) setupSASLCallbacks(result chan<- *SASLResult) { | ||
irc.AddCallback("CAP", func(e *Event) { | ||
if len(e.Arguments) == 3 { | ||
if e.Arguments[1] == "LS" { | ||
if !strings.Contains(e.Arguments[2], "sasl") { | ||
result <- &SASLResult{true, errors.New("no SASL capability " + e.Arguments[2])} | ||
} | ||
} | ||
if e.Arguments[1] == "ACK" { | ||
if irc.SASLMech != "PLAIN" { | ||
result <- &SASLResult{true, errors.New("only PLAIN is supported")} | ||
} | ||
irc.SendRaw("AUTHENTICATE " + irc.SASLMech) | ||
} | ||
} | ||
}) | ||
irc.AddCallback("AUTHENTICATE", func(e *Event) { | ||
str := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s\x00%s\x00%s", irc.SASLLogin, irc.SASLLogin, irc.SASLPassword))) | ||
irc.SendRaw("AUTHENTICATE " + str) | ||
}) | ||
irc.AddCallback("901", func(e *Event) { | ||
irc.SendRaw("CAP END") | ||
irc.SendRaw("QUIT") | ||
result <- &SASLResult{true, errors.New(e.Arguments[1])} | ||
}) | ||
irc.AddCallback("902", func(e *Event) { | ||
irc.SendRaw("CAP END") | ||
irc.SendRaw("QUIT") | ||
result <- &SASLResult{true, errors.New(e.Arguments[1])} | ||
}) | ||
irc.AddCallback("903", func(e *Event) { | ||
irc.SendRaw("CAP END") | ||
result <- &SASLResult{false, nil} | ||
}) | ||
irc.AddCallback("904", func(e *Event) { | ||
irc.SendRaw("CAP END") | ||
irc.SendRaw("QUIT") | ||
result <- &SASLResult{true, errors.New(e.Arguments[1])} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package irc | ||
|
||
import ( | ||
"crypto/tls" | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// set SASLLogin and SASLPassword environment variables before testing | ||
func TestConnectionSASL(t *testing.T) { | ||
SASLServer := "irc.freenode.net:7000" | ||
SASLLogin := os.Getenv("SASLLogin") | ||
SASLPassword := os.Getenv("SASLPassword") | ||
|
||
if SASLLogin == "" { | ||
t.SkipNow() | ||
} | ||
irccon := IRC("go-eventirc", "go-eventirc") | ||
irccon.VerboseCallbackHandler = true | ||
irccon.Debug = true | ||
irccon.UseTLS = true | ||
irccon.UseSASL = true | ||
irccon.SASLLogin = SASLLogin | ||
irccon.SASLPassword = SASLPassword | ||
irccon.TLSConfig = &tls.Config{InsecureSkipVerify: true} | ||
irccon.AddCallback("001", func(e *Event) { irccon.Join("#go-eventirc") }) | ||
|
||
irccon.AddCallback("366", func(e *Event) { | ||
irccon.Privmsg("#go-eventirc", "Test Message SASL\n") | ||
time.Sleep(2 * time.Second) | ||
irccon.Quit() | ||
}) | ||
|
||
err := irccon.Connect(SASLServer) | ||
if err != nil { | ||
t.Fatal("SASL failed") | ||
} | ||
irccon.Loop() | ||
} |