2017-12-16 23:20:46 +01:00
|
|
|
package toclient
|
2017-12-14 21:30:07 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
|
2018-02-07 15:34:18 +01:00
|
|
|
"dev.sum7.eu/genofire/yaja/database"
|
|
|
|
"dev.sum7.eu/genofire/yaja/messages"
|
|
|
|
"dev.sum7.eu/genofire/yaja/model"
|
|
|
|
"dev.sum7.eu/genofire/yaja/server/state"
|
|
|
|
"dev.sum7.eu/genofire/yaja/server/utils"
|
2017-12-14 21:30:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type RegisterFormRequest struct {
|
2017-12-16 23:20:46 +01:00
|
|
|
Next state.State
|
2017-12-17 17:50:51 +01:00
|
|
|
Client *utils.Client
|
2017-12-16 23:20:46 +01:00
|
|
|
domainRegisterAllowed utils.DomainRegisterAllowed
|
|
|
|
element *xml.StartElement
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process message
|
2017-12-17 17:50:51 +01:00
|
|
|
func (state *RegisterFormRequest) Process() state.State {
|
|
|
|
state.Client.Log = state.Client.Log.WithField("state", "register form request")
|
|
|
|
state.Client.Log.Debug("running")
|
|
|
|
defer state.Client.Log.Debug("leave")
|
2017-12-14 21:30:07 +01:00
|
|
|
|
2017-12-17 17:50:51 +01:00
|
|
|
if !state.domainRegisterAllowed(state.Client.JID) {
|
|
|
|
state.Client.Log.Error("unpossible to reach this state, register on this domain is not allowed")
|
|
|
|
return nil
|
2017-12-15 22:24:42 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 21:30:07 +01:00
|
|
|
var msg messages.IQ
|
2017-12-17 17:50:51 +01:00
|
|
|
if err := state.Client.In.DecodeElement(&msg, state.element); err != nil {
|
|
|
|
state.Client.Log.Warn("is no iq: ", err)
|
|
|
|
return state
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
|
|
|
if msg.Type != messages.IQTypeGet {
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Log.Warn("is no get iq")
|
|
|
|
return state
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
|
|
|
if msg.Error != nil {
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Log.Warn("iq with error: ", msg.Error.Code)
|
|
|
|
return state
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
|
|
|
type query struct {
|
|
|
|
XMLName xml.Name `xml:"query"`
|
|
|
|
}
|
|
|
|
q := &query{}
|
|
|
|
err := xml.Unmarshal(msg.Body, q)
|
|
|
|
|
|
|
|
if q.XMLName.Space != messages.NSIQRegister || err != nil {
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Log.Warn("is no iq register: ", err)
|
|
|
|
return nil
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Out.Encode(&messages.IQ{
|
2017-12-14 21:30:07 +01:00
|
|
|
Type: messages.IQTypeResult,
|
2017-12-17 17:50:51 +01:00
|
|
|
To: state.Client.JID.String(),
|
|
|
|
From: state.Client.JID.Domain,
|
2017-12-14 21:30:07 +01:00
|
|
|
ID: msg.ID,
|
|
|
|
Body: []byte(fmt.Sprintf(`<query xmlns='%s'><instructions>
|
|
|
|
Choose a username and password for use with this service.
|
|
|
|
</instructions>
|
|
|
|
<username/>
|
|
|
|
<password/>
|
|
|
|
</query>`, messages.NSIQRegister)),
|
|
|
|
})
|
2017-12-17 17:50:51 +01:00
|
|
|
return state.Next
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type RegisterRequest struct {
|
2017-12-16 23:20:46 +01:00
|
|
|
Next state.State
|
2017-12-17 17:50:51 +01:00
|
|
|
Client *utils.Client
|
2017-12-16 23:20:46 +01:00
|
|
|
database *database.State
|
|
|
|
domainRegisterAllowed utils.DomainRegisterAllowed
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process message
|
2017-12-17 17:50:51 +01:00
|
|
|
func (state *RegisterRequest) Process() state.State {
|
|
|
|
state.Client.Log = state.Client.Log.WithField("state", "register request")
|
|
|
|
state.Client.Log.Debug("running")
|
|
|
|
defer state.Client.Log.Debug("leave")
|
2017-12-14 21:30:07 +01:00
|
|
|
|
2017-12-17 17:50:51 +01:00
|
|
|
if !state.domainRegisterAllowed(state.Client.JID) {
|
|
|
|
state.Client.Log.Error("unpossible to reach this state, register on this domain is not allowed")
|
|
|
|
return nil
|
2017-12-15 22:24:42 +01:00
|
|
|
}
|
|
|
|
|
2017-12-17 17:50:51 +01:00
|
|
|
element, err := state.Client.Read()
|
2017-12-14 21:30:07 +01:00
|
|
|
if err != nil {
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Log.Warn("unable to read: ", err)
|
|
|
|
return nil
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
|
|
|
var msg messages.IQ
|
2017-12-17 17:50:51 +01:00
|
|
|
if err = state.Client.In.DecodeElement(&msg, element); err != nil {
|
|
|
|
state.Client.Log.Warn("is no iq: ", err)
|
|
|
|
return state
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
|
|
|
if msg.Type != messages.IQTypeGet {
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Log.Warn("is no get iq")
|
|
|
|
return state
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
|
|
|
if msg.Error != nil {
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Log.Warn("iq with error: ", msg.Error.Code)
|
|
|
|
return state
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
|
|
|
type query struct {
|
|
|
|
XMLName xml.Name `xml:"query"`
|
|
|
|
Username string `xml:"username"`
|
|
|
|
Password string `xml:"password"`
|
|
|
|
}
|
|
|
|
q := &query{}
|
|
|
|
err = xml.Unmarshal(msg.Body, q)
|
|
|
|
if err != nil {
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Log.Warn("is no iq register: ", err)
|
|
|
|
return nil
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
|
|
|
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.JID.Local = q.Username
|
|
|
|
state.Client.Log = state.Client.Log.WithField("jid", state.Client.JID.Full())
|
|
|
|
account := model.NewAccount(state.Client.JID, q.Password)
|
2017-12-16 23:20:46 +01:00
|
|
|
err = state.database.AddAccount(account)
|
2017-12-14 21:30:07 +01:00
|
|
|
if err != nil {
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Out.Encode(&messages.IQ{
|
2017-12-14 21:30:07 +01:00
|
|
|
Type: messages.IQTypeResult,
|
2017-12-17 17:50:51 +01:00
|
|
|
To: state.Client.JID.String(),
|
|
|
|
From: state.Client.JID.Domain,
|
2017-12-14 21:30:07 +01:00
|
|
|
ID: msg.ID,
|
|
|
|
Body: []byte(fmt.Sprintf(`<query xmlns='%s'>
|
|
|
|
<username>%s</username>
|
|
|
|
<password>%s</password>
|
|
|
|
</query>`, messages.NSIQRegister, q.Username, q.Password)),
|
|
|
|
Error: &messages.Error{
|
|
|
|
Code: "409",
|
|
|
|
Type: "cancel",
|
|
|
|
Any: xml.Name{
|
|
|
|
Local: "conflict",
|
|
|
|
Space: "urn:ietf:params:xml:ns:xmpp-stanzas",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Log.Warn("database error: ", err)
|
|
|
|
return state
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Out.Encode(&messages.IQ{
|
2017-12-14 21:30:07 +01:00
|
|
|
Type: messages.IQTypeResult,
|
2017-12-17 17:50:51 +01:00
|
|
|
To: state.Client.JID.String(),
|
|
|
|
From: state.Client.JID.Domain,
|
2017-12-14 21:30:07 +01:00
|
|
|
ID: msg.ID,
|
|
|
|
})
|
|
|
|
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Log.Infof("registered client %s", state.Client.JID.Bare())
|
|
|
|
return state.Next
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|