2017-12-16 23:20:46 +01:00
|
|
|
package toclient
|
2017-12-14 21:30:07 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-07 19:32:11 +01:00
|
|
|
var msg messages.IQClient
|
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
|
|
|
}
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
if msg.PrivateRegister == nil {
|
|
|
|
state.Client.Log.Warn("is no iq register")
|
2017-12-17 17:50:51 +01:00
|
|
|
return nil
|
2017-12-14 21:30:07 +01:00
|
|
|
}
|
2018-02-07 19:32:11 +01:00
|
|
|
state.Client.Out.Encode(&messages.IQClient{
|
2017-12-14 21:30:07 +01:00
|
|
|
Type: messages.IQTypeResult,
|
2018-02-10 13:34:42 +01:00
|
|
|
To: state.Client.JID,
|
|
|
|
From: model.NewJID(state.Client.JID.Domain),
|
2017-12-14 21:30:07 +01:00
|
|
|
ID: msg.ID,
|
2018-02-10 13:34:42 +01:00
|
|
|
PrivateRegister: &messages.IQPrivateRegister{
|
|
|
|
Instructions: "Choose a username and password for use with this service.",
|
|
|
|
Username: "",
|
|
|
|
Password: "",
|
|
|
|
},
|
2017-12-14 21:30:07 +01:00
|
|
|
})
|
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
|
|
|
}
|
2018-02-07 19:32:11 +01:00
|
|
|
var msg messages.IQClient
|
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
|
|
|
}
|
2018-02-10 13:34:42 +01:00
|
|
|
if msg.PrivateRegister == 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
|
|
|
}
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
state.Client.JID.Local = msg.PrivateRegister.Username
|
2017-12-17 17:50:51 +01:00
|
|
|
state.Client.Log = state.Client.Log.WithField("jid", state.Client.JID.Full())
|
2018-02-10 13:34:42 +01:00
|
|
|
account := model.NewAccount(state.Client.JID, msg.PrivateRegister.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 {
|
2018-02-07 19:32:11 +01:00
|
|
|
state.Client.Out.Encode(&messages.IQClient{
|
2018-02-10 13:34:42 +01:00
|
|
|
Type: messages.IQTypeResult,
|
|
|
|
To: state.Client.JID,
|
|
|
|
From: model.NewJID(state.Client.JID.Domain),
|
|
|
|
ID: msg.ID,
|
|
|
|
PrivateRegister: msg.PrivateRegister,
|
2018-02-07 19:32:11 +01:00
|
|
|
Error: &messages.ErrorClient{
|
2017-12-14 21:30:07 +01:00
|
|
|
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
|
|
|
}
|
2018-02-07 19:32:11 +01:00
|
|
|
state.Client.Out.Encode(&messages.IQClient{
|
2017-12-14 21:30:07 +01:00
|
|
|
Type: messages.IQTypeResult,
|
2018-02-10 13:34:42 +01:00
|
|
|
To: state.Client.JID,
|
|
|
|
From: model.NewJID(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
|
|
|
}
|