2012-10-11 15:00:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-08-13 14:45:40 +00:00
|
|
|
"github.com/derlaft/xmpp"
|
2012-10-11 15:00:43 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2015-08-18 15:06:25 +00:00
|
|
|
"os"
|
2012-10-11 15:00:43 +00:00
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-08-13 14:45:40 +00:00
|
|
|
const (
|
|
|
|
room = "room@conference.example.com"
|
|
|
|
name = "botname"
|
|
|
|
server = "example.com"
|
|
|
|
me = name + "@" + server
|
|
|
|
id = name
|
|
|
|
password = "password"
|
|
|
|
resource = "resource"
|
|
|
|
connect = "xmpp.example.com:5222"
|
|
|
|
)
|
2012-10-11 15:00:43 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
admin []string
|
2012-10-18 10:41:33 +00:00
|
|
|
cs = make(chan xmpp.Stanza)
|
2015-08-18 15:06:25 +00:00
|
|
|
stop chan struct{}
|
2012-10-18 10:41:33 +00:00
|
|
|
next xmpp.Stanza
|
2012-10-11 15:00:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2014-02-25 12:45:58 +00:00
|
|
|
var (
|
|
|
|
Conn *xmpp.Conn
|
|
|
|
err error
|
|
|
|
)
|
2015-08-18 11:15:54 +00:00
|
|
|
|
|
|
|
START:
|
2015-08-13 14:45:40 +00:00
|
|
|
for {
|
2015-08-18 15:06:25 +00:00
|
|
|
stop = make(chan struct{})
|
|
|
|
if(err != nil) {
|
|
|
|
admin = admin[:0]
|
|
|
|
if Conn != nil {
|
|
|
|
log.Println("Conn check:", Conn.Close())
|
|
|
|
}
|
2015-08-13 14:45:40 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
2014-02-25 12:45:30 +00:00
|
|
|
|
2015-08-18 11:15:54 +00:00
|
|
|
Conn, err = xmpp.Dial(connect, id, server, password, resource, nil)
|
2015-08-13 14:45:40 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Conn", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := Conn.SignalPresence("dnd", "is there some food in this world?", 12); err != nil {
|
|
|
|
log.Println("Signal", err)
|
|
|
|
continue
|
|
|
|
}
|
2015-08-18 15:06:25 +00:00
|
|
|
if err := Conn.SendPresence(room + "/" + name, ""); err != nil {
|
2015-08-13 14:45:40 +00:00
|
|
|
log.Println("Presence", err)
|
|
|
|
continue
|
|
|
|
}
|
2013-01-16 12:35:43 +00:00
|
|
|
|
2015-08-18 15:06:25 +00:00
|
|
|
go func(Conn *xmpp.Conn, stop chan struct{}) {
|
2015-08-13 14:45:40 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.After(60 * time.Second):
|
|
|
|
Conn.SendIQ(server, "set", "<keepalive xmlns='urn:xmpp:keepalive:0'> <interval>60</interval> </keepalive>")
|
|
|
|
if _, _, err = Conn.SendIQ(server, "get", "<ping xmlns='urn:xmpp:ping'/>"); err != nil {
|
2015-08-18 15:06:25 +00:00
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
default:
|
|
|
|
log.Println("KeepAlive err:", err)
|
|
|
|
close(stop)
|
|
|
|
}
|
2015-08-13 14:45:40 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-18 15:06:25 +00:00
|
|
|
|
|
|
|
case <-stop:
|
|
|
|
return
|
2015-08-13 14:45:40 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-18 15:06:25 +00:00
|
|
|
}(Conn, stop)
|
2015-08-13 14:45:40 +00:00
|
|
|
|
2015-08-18 15:06:25 +00:00
|
|
|
go func(Conn *xmpp.Conn, stop chan struct{}) {
|
2015-08-13 14:45:40 +00:00
|
|
|
for {
|
|
|
|
next, err := Conn.Next()
|
|
|
|
if err != nil {
|
2015-08-18 15:06:25 +00:00
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
default:
|
|
|
|
log.Println("KeepAlive err:", err)
|
|
|
|
close(stop)
|
|
|
|
}
|
2012-10-18 10:41:33 +00:00
|
|
|
return
|
2012-10-11 15:00:43 +00:00
|
|
|
}
|
2015-08-13 14:45:40 +00:00
|
|
|
cs <- next
|
2012-10-18 10:41:33 +00:00
|
|
|
}
|
2015-08-18 15:06:25 +00:00
|
|
|
}(Conn, stop)
|
2012-10-18 10:41:33 +00:00
|
|
|
|
|
|
|
for {
|
2015-08-13 14:45:40 +00:00
|
|
|
select {
|
|
|
|
case next = <-cs:
|
2015-08-18 15:06:25 +00:00
|
|
|
|
|
|
|
case <-stop:
|
|
|
|
Conn.Close()
|
|
|
|
Conn = nil
|
|
|
|
continue START
|
|
|
|
|
2015-08-13 14:45:40 +00:00
|
|
|
case <-time.After(65 * time.Second):
|
|
|
|
log.Println(Conn.Close(), "\n\t", "closed after 65 seconds of inactivity")
|
2015-08-18 15:06:25 +00:00
|
|
|
close(stop)
|
|
|
|
Conn = nil
|
2015-08-18 11:15:54 +00:00
|
|
|
continue START
|
2012-10-18 10:41:33 +00:00
|
|
|
}
|
2015-08-18 15:06:25 +00:00
|
|
|
|
2015-08-13 14:45:40 +00:00
|
|
|
switch t := next.Value.(type) {
|
|
|
|
case *xmpp.ClientPresence:
|
|
|
|
PresenceHandler(Conn, t)
|
2015-08-18 15:06:25 +00:00
|
|
|
|
2015-08-13 14:45:40 +00:00
|
|
|
case *xmpp.ClientMessage:
|
|
|
|
if len(t.Delay.Stamp) == 0 && len(t.Subject) == 0 {
|
|
|
|
log.Println(t)
|
|
|
|
if GetNick(t.From) != name {
|
|
|
|
if t.Type == "groupchat" {
|
|
|
|
go MessageHandler(Conn, t)
|
|
|
|
} else if xmpp.RemoveResourceFromJid(strings.ToLower(t.From)) == me {
|
|
|
|
go SelfHandler(Conn, t)
|
|
|
|
}
|
2013-05-16 07:31:23 +00:00
|
|
|
}
|
2012-10-11 15:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 14:45:40 +00:00
|
|
|
log.Println(Conn.Close(), "\n\t", "wtf am I doing here?")
|
2012-10-11 15:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SelfHandler(Conn *xmpp.Conn, Msg *xmpp.ClientMessage) {
|
|
|
|
Msg.Body = strings.TrimSpace(Msg.Body)
|
2015-08-18 15:06:25 +00:00
|
|
|
if(!strings.HasPrefix(Msg.Body, "!")) {
|
|
|
|
Conn.Send(room, "groupchat", Msg.Body)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
command, err := GetCommand(Msg.Body, Msg.From, "./plugins/")
|
|
|
|
if(err != nil) {
|
|
|
|
Conn.Send(Msg.From, "chat", err.Error())
|
2013-01-29 19:54:05 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-18 15:06:25 +00:00
|
|
|
out, err := command.CombinedOutput()
|
|
|
|
if err != nil {
|
2012-10-11 15:00:43 +00:00
|
|
|
log.Println(err)
|
2015-08-18 15:06:25 +00:00
|
|
|
Conn.Send(Msg.From, "chat", err.Error())
|
2012-10-11 15:00:43 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-19 17:53:56 +00:00
|
|
|
Conn.Send(Msg.From, "chat", strings.TrimRight(string(out), " \t\n"))
|
2012-10-11 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 15:06:25 +00:00
|
|
|
var call = regexp.MustCompile("^" + name + "[:,]")
|
2012-10-11 15:00:43 +00:00
|
|
|
func MessageHandler(Conn *xmpp.Conn, Msg *xmpp.ClientMessage) {
|
|
|
|
switch {
|
2015-08-18 15:06:25 +00:00
|
|
|
case strings.HasPrefix(Msg.Body, "!megakick "):
|
|
|
|
s := strings.Split(Msg.Body, "!megakick ")
|
2013-01-14 14:26:18 +00:00
|
|
|
if in(admin, Msg.From) {
|
2013-01-16 12:35:43 +00:00
|
|
|
Conn.ModUse(room, s[1], "none", "")
|
|
|
|
} else {
|
|
|
|
Conn.Send(room, "groupchat", fmt.Sprintf("%s: GTFO", GetNick(Msg.From)))
|
2013-01-14 14:26:18 +00:00
|
|
|
}
|
2015-08-18 15:06:25 +00:00
|
|
|
|
|
|
|
case strings.HasPrefix(Msg.Body, "!"): //any external command
|
|
|
|
cmd, err := GetCommand(Msg.Body, Msg.From, "./plugins/")
|
|
|
|
if(err != nil) {
|
|
|
|
Conn.Send(room, "groupchat", fmt.Sprintf("%s: WAT", GetNick(Msg.From)))
|
|
|
|
if(in(admin, Msg.From)) {
|
|
|
|
Conn.Send(Msg.From, "chat", err.Error())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2012-10-11 15:00:43 +00:00
|
|
|
stdout, _ := cmd.StdoutPipe()
|
|
|
|
stderr, _ := cmd.StderrPipe()
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
log.Println(err)
|
2015-08-18 15:06:25 +00:00
|
|
|
Conn.Send(room, "groupchat", fmt.Sprintf("%s: WAT", GetNick(Msg.From)))
|
|
|
|
if(in(admin, Msg.From)) {
|
|
|
|
Conn.Send(Msg.From, "chat", err.Error())
|
|
|
|
}
|
2012-10-11 15:00:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
out, _ := ioutil.ReadAll(stdout)
|
|
|
|
outerr, _ := ioutil.ReadAll(stderr)
|
2015-08-18 15:06:25 +00:00
|
|
|
cmd.Wait()
|
2012-10-11 15:00:43 +00:00
|
|
|
if len(outerr) != 0 && in(admin, Msg.From) {
|
2015-08-19 17:53:56 +00:00
|
|
|
Conn.Send(Msg.From, "chat", strings.TrimRight(string(outerr), " \t\n"))
|
2012-10-11 15:00:43 +00:00
|
|
|
}
|
2015-08-19 17:53:56 +00:00
|
|
|
Conn.Send(room, "groupchat", strings.TrimRight(string(out), " \t\n"))
|
2015-08-18 15:06:25 +00:00
|
|
|
|
|
|
|
case call.MatchString(Msg.Body): //chat
|
|
|
|
command, err := GetCommand(call.ReplaceAllString(Msg.Body, "!answer"), Msg.From, "./chat/")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out, err := command.CombinedOutput()
|
2012-10-11 15:00:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2015-08-18 15:06:25 +00:00
|
|
|
if(in(admin, Msg.From)) {
|
|
|
|
Conn.Send(Msg.From, "chat", err.Error())
|
|
|
|
}
|
2012-10-11 15:00:43 +00:00
|
|
|
return
|
|
|
|
}
|
2015-08-19 17:53:56 +00:00
|
|
|
Conn.Send(room, "groupchat", strings.TrimRight(string(out), " \t\n"))
|
2012-10-11 15:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PresenceHandler(Conn *xmpp.Conn, Prs *xmpp.ClientPresence) {
|
2015-08-18 15:06:25 +00:00
|
|
|
if(Prs.From == room + "/" + name && Prs.Item.Role == "none") {
|
|
|
|
log.Println("was kicked")
|
|
|
|
close(stop)
|
|
|
|
return
|
|
|
|
}
|
2012-10-11 15:00:43 +00:00
|
|
|
switch Prs.Item.Affiliation {
|
|
|
|
case "owner":
|
|
|
|
fallthrough
|
|
|
|
case "admin":
|
|
|
|
if Prs.Item.Role != "none" {
|
|
|
|
if !in(admin, Prs.From) {
|
|
|
|
admin = append(admin, Prs.From)
|
|
|
|
}
|
2013-06-06 10:40:25 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if in(admin, Prs.From) {
|
2012-10-11 15:00:43 +00:00
|
|
|
admin = del(admin, Prs.From)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 15:06:25 +00:00
|
|
|
//letter(ASCII or cyrillic), number, underscore only
|
|
|
|
var cmd_validator = regexp.MustCompile("^!(\\w|\\p{Cyrillic})*$")
|
|
|
|
func GetCommand(body, from, dir string) (*exec.Cmd, error) {
|
2012-10-11 15:00:43 +00:00
|
|
|
split := strings.SplitAfterN(body, " ", 2)
|
2015-08-18 15:06:25 +00:00
|
|
|
cmd := strings.TrimSpace(split[0])
|
|
|
|
|
|
|
|
if(!cmd_validator.MatchString(cmd)) { return nil, fmt.Errorf("Bad command \"%s\"", cmd) }
|
|
|
|
|
|
|
|
var (
|
|
|
|
info os.FileInfo
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
path := dir + Strip(cmd[1:])
|
|
|
|
if info, err = os.Stat(path); err != nil { return nil, err }
|
|
|
|
if info.IsDir() || info.Mode() & 0111 == 0 { return nil, fmt.Errorf("\"%s\" isn't executable", path) }
|
|
|
|
|
|
|
|
args := []string{ Strip(GetNick(from)), strconv.FormatBool(in(admin, from)) }
|
|
|
|
if(len(split) > 1) { args = append(args, Strip(split[1])) }
|
|
|
|
return exec.Command(path, args...), nil
|
2012-10-11 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 15:06:25 +00:00
|
|
|
var strip_regexp = regexp.MustCompile("(`|\\$|\\.\\.)")
|
|
|
|
var quote_regexp = regexp.MustCompile("(\"|')")
|
|
|
|
func Strip(s string) string {
|
|
|
|
return quote_regexp.ReplaceAllString(strip_regexp.ReplaceAllString(s, ""), "“")
|
2012-10-11 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetNick(s string) string {
|
|
|
|
slash := strings.Index(s, "/")
|
|
|
|
if slash != -1 {
|
|
|
|
return s[slash+1:]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func in(slice []string, value string) bool {
|
|
|
|
for _, v := range slice {
|
|
|
|
if v == value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func pos(slice []string, value string) int {
|
|
|
|
for p, v := range slice {
|
|
|
|
if v == value {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func del(slice []string, value string) []string {
|
|
|
|
if i := pos(slice, value); i >= 0 {
|
|
|
|
return append(slice[:i], slice[i+1:]...)
|
|
|
|
}
|
|
|
|
return slice
|
|
|
|
}
|