package main

import (
	"bytes"
	"log"
	"os/exec"
	"strings"

	"github.com/shizeeg/xmpp"
)

// RunPlugin is a generic interface for external commands.
func (s *Session) RunPlugin(stanza xmpp.Stanza, filename string, tonick bool, params ...string) {
	message, ok := stanza.Value.(*xmpp.ClientMessage)
	if !ok {
		log.Println("Wrong Stanza type!")
		return
	}
	lang := "-lang=en"
	if len(message.Lang) >= 2 {
		lang = "-lang=" + message.Lang[:2]
	}
	plugin := exec.Command(filename, lang)
	plugin.Args = append(plugin.Args, params...)

	var out bytes.Buffer
	plugin.Stdout = &out
	if err := plugin.Run(); err != nil {
		log.Println(err)
		return
	}
	s.Say(stanza, strings.TrimSpace(out.String()), tonick, false)
}

package main

import (
	"bytes"
	"log"
	"os/exec"

	"github.com/shizeeg/xmpp"
)

// RunPlugin is a generic interface for external commands.
func (s *Session) RunPlugin(stanza xmpp.Stanza, filename string, tonick bool, params ...string) {
	message, ok := stanza.Value.(*xmpp.ClientMessage)
	if !ok {
		log.Println("Wrong Stanza type!")
		return
	}
	lang := "-lang=en"
	if len(message.Lang) > 0 {
		lang = "-lang=" + message.Lang
	}
	plugin := exec.Command(filename, lang)
	plugin.Args = append(plugin.Args, params...)

	var out bytes.Buffer
	plugin.Stdout = &out
	if err := plugin.Run(); err != nil {
		log.Println(err)
		return
	}
	s.Say(stanza, out.String(), tonick, false)
}

--- 
+++ 
@@ -4,6 +4,7 @@
 	"bytes"
 	"log"
 	"os/exec"
+	"strings"
 
 	"github.com/shizeeg/xmpp"
 )
@@ -16,8 +17,8 @@
 		return
 	}
 	lang := "-lang=en"
-	if len(message.Lang) > 0 {
-		lang = "-lang=" + message.Lang
+	if len(message.Lang) >= 2 {
+		lang = "-lang=" + message.Lang[:2]
 	}
 	plugin := exec.Command(filename, lang)
 	plugin.Args = append(plugin.Args, params...)
@@ -28,5 +29,5 @@
 		log.Println(err)
 		return
 	}
-	s.Say(stanza, out.String(), tonick, false)
+	s.Say(stanza, strings.TrimSpace(out.String()), tonick, false)
 }
FIX language detection. Some clients send incorrect data in xml:lang="en-US" Deal with that
