Compare commits

...

2 Commits

Author SHA1 Message Date
Chteufleur a85f476600 Send multiple bodies with support of xml:lang. 2016-11-13 20:27:32 +01:00
Chteufleur 2f301ffc80 Remove unused code 2016-11-11 18:06:33 +01:00
3 changed files with 61 additions and 31 deletions

24
main.go
View File

@ -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()

View File

@ -3,20 +3,27 @@ package xmpp
import (
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
"crypto/rand"
"log"
"strconv"
"strings"
)
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"
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 {
@ -33,15 +40,9 @@ type Confirmation struct {
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)
confirmation.askViaMessage()
} else {
confirmation.askViaIQ()
}
@ -60,17 +61,10 @@ func (confirmation *Confirmation) askViaIQ() {
confirmation.IdMap = stanzaIDstr
}
func (confirmation *Confirmation) askViaMessage(isAutoGeneratedTranctionID bool) {
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
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."
confirmation.setBodies(&m)
m.Confir = &xmpp.Confirm{Id: confirmation.Transaction, Method: confirmation.Method, URL: confirmation.Domain}
log.Printf("%sSend message %v", LogInfo, m)
@ -81,13 +75,23 @@ func (confirmation *Confirmation) askViaMessage(isAutoGeneratedTranctionID bool)
confirmation.IdMap = confirmation.Transaction
}
func TransactionID() string {
var bytes = make([]byte, 8)
if _, err := rand.Read(bytes); err != nil {
panic(err)
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})
}
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(bytes)
}

View File

@ -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 {