forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
114 lines (95 loc) · 3.38 KB
/
register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package xmpp implements the XMPP IM protocol, as specified in RFC 6120 and
// 6121.
package xmpp
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
"github.com/twstrike/coyim/xmpp/data"
"github.com/twstrike/coyim/xmpp/interfaces"
)
// Various errors signalled by the registration component
var (
ErrUsernameConflict = errors.New("xmpp: the username is not available for registration")
ErrMissingRequiredRegistrationInfo = errors.New("xmpp: missing required registration information")
ErrRegistrationFailed = errors.New("xmpp: account creation failed")
)
// XEP-0077
func (d *dialer) negotiateInBandRegistration(c interfaces.Conn) (bool, error) {
if c.Features().InBandRegistration == nil {
return false, nil
}
user := d.getJIDLocalpart()
return c.RegisterAccount(user, d.password)
}
func (c *conn) RegisterAccount(user, password string) (bool, error) {
if c.config.CreateCallback == nil {
return false, nil
}
err := c.createAccount(user, password)
if err != nil {
return false, err
}
return true, c.closeImmediately()
}
func (c *conn) createAccount(user, password string) error {
io.WriteString(c.config.GetLog(), "Attempting to create account\n")
fmt.Fprintf(c.out, "<iq type='get' id='create_1'><query xmlns='jabber:iq:register'/></iq>")
var iq data.ClientIQ
if err := c.in.DecodeElement(&iq, nil); err != nil {
return errors.New("unmarshal <iq>: " + err.Error())
}
if iq.Type != "result" {
return errors.New("xmpp: account creation failed")
}
var register data.RegisterQuery
if err := xml.NewDecoder(bytes.NewBuffer(iq.Query)).Decode(®ister); err != nil {
return err
}
if len(register.Form.Type) > 0 {
reply, err := processForm(®ister.Form, register.Datas, c.config.CreateCallback)
fmt.Fprintf(c.rawOut, "<iq type='set' id='create_2'><query xmlns='jabber:iq:register'>")
if err = xml.NewEncoder(c.rawOut).Encode(reply); err != nil {
return err
}
fmt.Fprintf(c.rawOut, "</query></iq>")
} else if register.Username != nil && register.Password != nil {
//TODO: make sure this only happens via SSL
//TODO: should generate form asking for username and password,
//and call processForm for consistency
// Try the old-style registration.
fmt.Fprintf(c.rawOut, "<iq type='set' id='create_2'><query xmlns='jabber:iq:register'><username>%s</username><password>%s</password></query></iq>", user, password)
}
iq2 := &data.ClientIQ{}
if err := c.in.DecodeElement(iq2, nil); err != nil {
return errors.New("unmarshal <iq>: " + err.Error())
}
if iq2.Type == "error" {
switch iq2.Error.Any.Local {
case "conflict":
// <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
return ErrUsernameConflict
case "not-acceptable":
// <not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
return ErrMissingRequiredRegistrationInfo
default:
return ErrRegistrationFailed
}
}
return nil
}
// CancelRegistration cancels the account registration with the server
func (c *conn) CancelRegistration() (reply chan data.Stanza, cookie data.Cookie, err error) {
// https://xmpp.org/extensions/xep-0077.html#usecases-cancel
registrationCancel := rawXML(`
<query xmlns='jabber:iq:register'>
<remove/>
</query>
`)
return c.SendIQ("", "set", registrationCancel)
}