diff --git a/main.go b/main.go index 6e3dbf2..5c0703c 100644 --- a/main.go +++ b/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() diff --git a/xmpp/confirmation.go b/xmpp/confirmation.go index def70cb..a7b2ffb 100644 --- a/xmpp/confirmation.go +++ b/xmpp/confirmation.go @@ -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}) + } + } +} diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 4d82cc2..841cc78 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -100,10 +100,12 @@ 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] - jidFrom, _ := xmpp.ParseJID(v.From) - if confirmation != nil && confirmation.JID == jidFrom.Bare() { - processConfirm(v, confirmation) + 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) + } } }