sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/server/toclient/register.go

136 lines
3.8 KiB
Go
Raw Normal View History

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
}
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
}
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,
2018-02-13 20:05:18 +01:00
From: messages.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-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.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 {
state.Client.Out.Encode(&messages.IQClient{
2018-02-10 13:34:42 +01:00
Type: messages.IQTypeResult,
To: state.Client.JID,
2018-02-13 20:05:18 +01:00
From: messages.NewJID(state.Client.JID.Domain),
2018-02-10 13:34:42 +01:00
ID: msg.ID,
PrivateRegister: msg.PrivateRegister,
Error: &messages.ErrorClient{
2017-12-14 21:30:07 +01:00
Code: "409",
Type: "cancel",
2018-02-11 15:11:52 +01:00
StanzaErrorGroup: messages.StanzaErrorGroup{
Conflict: &xml.Name{},
2017-12-14 21:30:07 +01:00
},
},
})
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
}
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,
2018-02-13 20:05:18 +01:00
From: messages.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
}