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

154 lines
4.1 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"
"fmt"
2017-12-16 23:20:46 +01:00
"github.com/genofire/yaja/database"
2017-12-14 21:30:07 +01:00
"github.com/genofire/yaja/messages"
"github.com/genofire/yaja/model"
2017-12-16 23:20:46 +01:00
"github.com/genofire/yaja/server/state"
"github.com/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-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-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
}