101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
package xmpp
|
||
|
||
import (
|
||
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
|
||
|
||
"crypto/rand"
|
||
"log"
|
||
"strconv"
|
||
)
|
||
|
||
const (
|
||
dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||
|
||
REPLY_UNREACHABLE = "reply_unreachable"
|
||
REPLY_DENY = "reply_deny"
|
||
REPLY_OK = "reply_ok"
|
||
|
||
TYPE_SEND_MESSAGE = "type_send_message"
|
||
TYPE_SEND_IQ = "type_send_iq"
|
||
)
|
||
|
||
type Confirmation struct {
|
||
JID string
|
||
Method string
|
||
Domain string
|
||
Transaction string
|
||
|
||
TypeSend string
|
||
IdMap string
|
||
|
||
ChanReply chan string
|
||
}
|
||
|
||
/*
|
||
type Transport interface {
|
||
Run()
|
||
Out(chan interface{})
|
||
}
|
||
*/
|
||
|
||
func (confirmation *Confirmation) SendConfirmation() {
|
||
log.Printf("%sQuery JID %s", LogInfo, confirmation.JID)
|
||
isAutoGeneratedTranctionID := false
|
||
if confirmation.Transaction == "" {
|
||
// Random transaction ID generation
|
||
confirmation.Transaction = TransactionID()
|
||
isAutoGeneratedTranctionID = true
|
||
}
|
||
clientJID, _ := xmpp.ParseJID(confirmation.JID)
|
||
if clientJID.Resource == "" {
|
||
confirmation.askViaMessage(isAutoGeneratedTranctionID)
|
||
} else {
|
||
confirmation.askViaIQ()
|
||
}
|
||
}
|
||
|
||
func (confirmation *Confirmation) askViaIQ() {
|
||
stanzaID++
|
||
stanzaIDstr := strconv.Itoa(stanzaID)
|
||
m := xmpp.Iq{Type: xmpp.IQTypeGet, To: confirmation.JID, From: jid.Domain, Id: stanzaIDstr}
|
||
confirm := &xmpp.Confirm{Id: confirmation.Transaction, Method: confirmation.Method, URL: confirmation.Domain}
|
||
m.PayloadEncode(confirm)
|
||
WaitIqMessages[stanzaIDstr] = confirmation
|
||
comp.Out <- m
|
||
|
||
confirmation.TypeSend = TYPE_SEND_IQ
|
||
confirmation.IdMap = stanzaIDstr
|
||
}
|
||
|
||
func (confirmation *Confirmation) askViaMessage(isAutoGeneratedTranctionID bool) {
|
||
m := xmpp.Message{From: jid.Domain, To: confirmation.JID, Type: xmpp.MessageTypeNormal}
|
||
|
||
m.Thread = xmpp.SessionID()
|
||
m.Body = confirmation.Domain + " (with method " + confirmation.Method + ") need to validate your identity, do you agree ?"
|
||
m.Body += "\nValidation code : " + confirmation.Transaction
|
||
if !isAutoGeneratedTranctionID {
|
||
// Send only if the transaction ID is not autogenerated
|
||
m.Body += "\nPlease check that this code is the same as on " + confirmation.Domain
|
||
}
|
||
m.Body += "\n\nIf your client doesn't support that functionnality, please send back the validation code to confirm the request."
|
||
m.Confir = &xmpp.Confirm{Id: confirmation.Transaction, Method: confirmation.Method, URL: confirmation.Domain}
|
||
|
||
log.Printf("%sSend message %v", LogInfo, m)
|
||
WaitMessageAnswers[confirmation.Transaction] = confirmation
|
||
comp.Out <- m
|
||
|
||
confirmation.TypeSend = TYPE_SEND_MESSAGE
|
||
confirmation.IdMap = confirmation.Transaction
|
||
}
|
||
|
||
func TransactionID() string {
|
||
var bytes = make([]byte, 8)
|
||
if _, err := rand.Read(bytes); err != nil {
|
||
panic(err)
|
||
}
|
||
for k, v := range bytes {
|
||
bytes[k] = dictionary[v%byte(len(dictionary))]
|
||
}
|
||
return string(bytes)
|
||
}
|