Send multiple bodies with support of xml:lang.
This commit is contained in:
parent
2f301ffc80
commit
a85f476600
24
main.go
24
main.go
|
|
@ -18,6 +18,7 @@ import (
|
|||
const (
|
||||
Version = "v0.5-dev"
|
||||
configurationFilePath = "http-auth/httpAuth.conf"
|
||||
langFilePath = "http-auth/messages.lang"
|
||||
PathConfEnvVariable = "XDG_CONFIG_DIRS"
|
||||
DefaultXdgConfigDirs = "/etc/xdg"
|
||||
)
|
||||
|
|
@ -32,6 +33,7 @@ func init() {
|
|||
if !loadConfigFile() {
|
||||
log.Fatal("Failed to load configuration file.")
|
||||
}
|
||||
loadLangFile()
|
||||
|
||||
// HTTP config
|
||||
httpTimeout, err := strconv.Atoi(mapConfig["http_timeout_sec"])
|
||||
|
|
@ -98,6 +100,28 @@ func loadConfigFile() bool {
|
|||
return ret
|
||||
}
|
||||
|
||||
func loadLangFile() bool {
|
||||
ret := false
|
||||
envVariable := os.Getenv(PathConfEnvVariable)
|
||||
if envVariable == "" {
|
||||
envVariable = DefaultXdgConfigDirs
|
||||
}
|
||||
for _, path := range strings.Split(envVariable, ":") {
|
||||
log.Println("Try to find messages lang file into " + path)
|
||||
langFile := path + "/" + langFilePath
|
||||
if _, err := os.Stat(langFile); err == nil {
|
||||
// The config file exist
|
||||
if cfg.Load(langFile, xmpp.MapLangs) == nil {
|
||||
// And has been loaded succesfully
|
||||
log.Println("Find messages lang file at " + langFile)
|
||||
ret = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
go http.Run()
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -14,6 +15,15 @@ const (
|
|||
|
||||
TYPE_SEND_MESSAGE = "type_send_message"
|
||||
TYPE_SEND_IQ = "type_send_iq"
|
||||
|
||||
TEMPLATE_DOMAIN = "_DOMAIN_"
|
||||
TEMPLATE_METHOD = "_METHOD_"
|
||||
TEMPLATE_VALIDATION_CODE = "_VALIDE_CODE_"
|
||||
DEFAULT_MESSAGE = "_DOMAIN_ (with method _METHOD_) need to validate your identity, do you agree ?\nValidation code : _VALIDE_CODE_\nPlease check that this code is the same as on _DOMAIN_.\n\nIf your client doesn't support that functionnality, please send back the validation code to confirm the request."
|
||||
)
|
||||
|
||||
var (
|
||||
MapLangs = make(map[string]string)
|
||||
)
|
||||
|
||||
type Confirmation struct {
|
||||
|
|
@ -53,12 +63,8 @@ func (confirmation *Confirmation) askViaIQ() {
|
|||
|
||||
func (confirmation *Confirmation) askViaMessage() {
|
||||
m := xmpp.Message{From: jid.Full(), 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
|
||||
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."
|
||||
confirmation.setBodies(&m)
|
||||
m.Confir = &xmpp.Confirm{Id: confirmation.Transaction, Method: confirmation.Method, URL: confirmation.Domain}
|
||||
|
||||
log.Printf("%sSend message %v", LogInfo, m)
|
||||
|
|
@ -68,3 +74,24 @@ func (confirmation *Confirmation) askViaMessage() {
|
|||
confirmation.TypeSend = TYPE_SEND_MESSAGE
|
||||
confirmation.IdMap = confirmation.Transaction
|
||||
}
|
||||
|
||||
func (confirmation *Confirmation) setBodies(message *xmpp.Message) {
|
||||
msg := DEFAULT_MESSAGE
|
||||
if len(MapLangs) == 0 {
|
||||
msg = strings.Replace(msg, TEMPLATE_DOMAIN, confirmation.Domain, -1)
|
||||
msg = strings.Replace(msg, TEMPLATE_METHOD, confirmation.Method, -1)
|
||||
msg = strings.Replace(msg, TEMPLATE_VALIDATION_CODE, confirmation.Transaction, -1)
|
||||
message.Body = append(message.Body, xmpp.MessageBody{Lang: "en", Value: msg})
|
||||
} else {
|
||||
for key, val := range MapLangs {
|
||||
msg = val
|
||||
msg = strings.Replace(msg, TEMPLATE_DOMAIN, confirmation.Domain, -1)
|
||||
msg = strings.Replace(msg, TEMPLATE_METHOD, confirmation.Method, -1)
|
||||
msg = strings.Replace(msg, TEMPLATE_VALIDATION_CODE, confirmation.Transaction, -1)
|
||||
msg = strings.Replace(msg, "\\n", "\n", -1)
|
||||
msg = strings.Replace(msg, "\\r", "\r", -1)
|
||||
msg = strings.Replace(msg, "\\t", "\t", -1)
|
||||
message.Body = append(message.Body, xmpp.MessageBody{Lang: key, Value: msg})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,12 +100,14 @@ func mainXMPP() {
|
|||
} else {
|
||||
// If body is the confirmation id, it will be considerated as accepted.
|
||||
// In order to be compatible with all confirmations.
|
||||
confirmation := WaitMessageAnswers[v.Body]
|
||||
if len(v.Body) > 0 {
|
||||
confirmation := WaitMessageAnswers[v.Body[0].Value]
|
||||
jidFrom, _ := xmpp.ParseJID(v.From)
|
||||
if confirmation != nil && confirmation.JID == jidFrom.Bare() {
|
||||
processConfirm(v, confirmation)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case *xmpp.Iq:
|
||||
switch v.PayloadName().Space {
|
||||
|
|
|
|||
Loading…
Reference in New Issue