Simplify. Remove unused/unreachable code.
This commit is contained in:
parent
9a399bd692
commit
19c81df5d6
|
|
@ -93,14 +93,13 @@ func startClient(stream *Stream, jid JID) error {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if rstart, err := stream.SendStart(&start); err != nil {
|
rstart, err := stream.SendStart(&start)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
|
||||||
if rstart.Name != (xml.Name{nsStreams, "stream"}) {
|
|
||||||
return fmt.Errorf("unexpected start element: %s", rstart.Name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if rstart.Name != (xml.Name{nsStreams, "stream"}) {
|
||||||
|
return fmt.Errorf("unexpected start element: %s", rstart.Name)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -158,26 +157,26 @@ func authenticatePlain(stream *Stream, user, password string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func authenticateResponse(stream *Stream) error {
|
func authenticateResponse(stream *Stream) error {
|
||||||
if se, err := stream.Next(); err != nil {
|
se, err := stream.Next()
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
|
||||||
switch se.Name.Local {
|
|
||||||
case "success":
|
|
||||||
if err := stream.Skip(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
case "failure":
|
|
||||||
f := new(saslFailure)
|
|
||||||
if err := stream.Decode(f, se); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return fmt.Errorf("Authentication failed: %s", f.Reason.Local)
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("Unexpected: %s", se.Name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
panic("unreachable")
|
|
||||||
|
switch se.Name.Local {
|
||||||
|
case "success":
|
||||||
|
if err := stream.Skip(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case "failure":
|
||||||
|
f := new(saslFailure)
|
||||||
|
if err := stream.Decode(f, se); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return fmt.Errorf("Authentication failed: %s", f.Reason.Local)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("Unexpected: %s", se.Name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type saslAuth struct {
|
type saslAuth struct {
|
||||||
|
|
|
||||||
|
|
@ -35,21 +35,20 @@ func startComponent(stream *Stream, jid JID) (string, error) {
|
||||||
|
|
||||||
var streamId string
|
var streamId string
|
||||||
|
|
||||||
if rstart, err := stream.SendStart(&start); err != nil {
|
rstart, err := stream.SendStart(&start)
|
||||||
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
} else {
|
}
|
||||||
if rstart.Name != (xml.Name{nsStreams, "stream"}) {
|
if rstart.Name != (xml.Name{nsStreams, "stream"}) {
|
||||||
return "", fmt.Errorf("unexpected start element: %s", rstart.Name)
|
return "", fmt.Errorf("unexpected start element: %s", rstart.Name)
|
||||||
}
|
}
|
||||||
// Find the stream id.
|
// Find the stream id.
|
||||||
for _, attr := range rstart.Attr {
|
for _, attr := range rstart.Attr {
|
||||||
if attr.Name.Local == "id" {
|
if attr.Name.Local == "id" {
|
||||||
streamId = attr.Value
|
streamId = attr.Value
|
||||||
break
|
break
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if streamId == "" {
|
if streamId == "" {
|
||||||
return "", errors.New("Missing stream id")
|
return "", errors.New("Missing stream id")
|
||||||
}
|
}
|
||||||
|
|
@ -70,18 +69,14 @@ func handshake(stream *Stream, streamId, secret string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get handshake response.
|
// Get handshake response.
|
||||||
if start, err := stream.Next(); err != nil {
|
start, err := stream.Next()
|
||||||
return err
|
if err != nil {
|
||||||
} else {
|
|
||||||
if start.Name != (xml.Name{nsComponentAccept, "handshake"}) {
|
|
||||||
return fmt.Errorf("Expected <handshake/>, for %s", start.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := stream.Skip(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if start.Name != (xml.Name{nsComponentAccept, "handshake"}) {
|
||||||
return nil
|
return fmt.Errorf("Expected <handshake/>, for %s", start.Name)
|
||||||
|
}
|
||||||
|
return stream.Skip()
|
||||||
}
|
}
|
||||||
|
|
||||||
type saslHandshake struct {
|
type saslHandshake struct {
|
||||||
|
|
|
||||||
|
|
@ -115,12 +115,11 @@ type Error struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Error) Error() string {
|
func (e Error) Error() string {
|
||||||
if text := e.Text(); text == "" {
|
text := e.Text()
|
||||||
|
if text == "" {
|
||||||
return fmt.Sprintf("[%s] %s", e.Type, e.Condition().Local)
|
return fmt.Sprintf("[%s] %s", e.Type, e.Condition().Local)
|
||||||
} else {
|
|
||||||
return fmt.Sprintf("[%s] %s, %s", e.Type, e.Condition().Local, text)
|
|
||||||
}
|
}
|
||||||
panic("unreachable")
|
return fmt.Sprintf("[%s] %s, %s", e.Type, e.Condition().Local, text)
|
||||||
}
|
}
|
||||||
|
|
||||||
type errorText struct {
|
type errorText struct {
|
||||||
|
|
|
||||||
|
|
@ -148,11 +148,11 @@ func (stream *Stream) Next() (*xml.StartElement, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if stream.config.LogStanzas {
|
if stream.config.LogStanzas {
|
||||||
if xml, err := collectElement(stream.dec, start, stream.incomingNamespace); err != nil {
|
xml, err := collectElement(stream.dec, start, stream.incomingNamespace)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
|
||||||
stream.stanzaBuf = xml
|
|
||||||
}
|
}
|
||||||
|
stream.stanzaBuf = xml
|
||||||
log.Println("recv:", stream.stanzaBuf)
|
log.Println("recv:", stream.stanzaBuf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -170,7 +170,7 @@ func nextStartElement(dec *xml.Decoder) (*xml.StartElement, error) {
|
||||||
}
|
}
|
||||||
switch e := t.(type) {
|
switch e := t.(type) {
|
||||||
case xml.StartElement:
|
case xml.StartElement:
|
||||||
for i, _ := range e.Attr {
|
for i := range e.Attr {
|
||||||
// Replace URL namespace to xml in order to avoid error on Unmarshal
|
// Replace URL namespace to xml in order to avoid error on Unmarshal
|
||||||
// It's quite ugly, but working for now
|
// It's quite ugly, but working for now
|
||||||
if e.Attr[i].Name.Space == "http://www.w3.org/XML/1998/namespace" {
|
if e.Attr[i].Name.Space == "http://www.w3.org/XML/1998/namespace" {
|
||||||
|
|
@ -183,7 +183,6 @@ func nextStartElement(dec *xml.Decoder) (*xml.StartElement, error) {
|
||||||
return nil, io.EOF
|
return nil, io.EOF
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic("Unreachable")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip reads tokens until it reaches the end element of the most recent start
|
// Skip reads tokens until it reaches the end element of the most recent start
|
||||||
|
|
@ -205,11 +204,11 @@ func (stream *Stream) Decode(v interface{}, start *xml.StartElement) error {
|
||||||
// Explicity lookup next start element to ensure stream is validated,
|
// Explicity lookup next start element to ensure stream is validated,
|
||||||
// stanza is logged, etc.
|
// stanza is logged, etc.
|
||||||
if start == nil {
|
if start == nil {
|
||||||
if se, err := stream.Next(); err != nil {
|
se, err := stream.Next()
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
|
||||||
start = se
|
|
||||||
}
|
}
|
||||||
|
start = se
|
||||||
}
|
}
|
||||||
|
|
||||||
if stream.config.LogStanzas {
|
if stream.config.LogStanzas {
|
||||||
|
|
|
||||||
|
|
@ -76,14 +76,13 @@ func writeXMLAttr(w io.Writer, attr xml.Attr) error {
|
||||||
func startElementIter(dec *xml.Decoder) func() *xml.StartElement {
|
func startElementIter(dec *xml.Decoder) func() *xml.StartElement {
|
||||||
return func() *xml.StartElement {
|
return func() *xml.StartElement {
|
||||||
for {
|
for {
|
||||||
if tok, err := dec.Token(); err != nil {
|
tok, err := dec.Token()
|
||||||
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
} else {
|
}
|
||||||
if start, ok := tok.(xml.StartElement); ok {
|
if start, ok := tok.(xml.StartElement); ok {
|
||||||
return &start
|
return &start
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue