This repository has been archived by the owner on Apr 13, 2018. It is now read-only.
/
sysinput.go
executable file
·68 lines (60 loc) · 1.71 KB
/
sysinput.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"bufio"
"bytes"
"github.com/tucnak/telebot"
log "maunium.net/go/maulogger"
"maunium.net/go/ranssibot/config"
"maunium.net/go/ranssibot/util"
"os"
"strings"
)
func listen(bot *telebot.Bot) {
reader := bufio.NewReader(os.Stdin)
for {
text, _ := reader.ReadString('\n')
text = text[:len(text)-1]
log.Debugf("[Syscmds] Received %s", text)
args := strings.Split(text, " ")
onCommand(bot, strings.ToLower(args[0]), args[1:])
}
}
func onCommand(bot *telebot.Bot, command string, args []string) {
if command == "msg" && len(args) > 1 {
user := config.GetUser(args[0])
if user.UID == config.NilUser.UID {
log.Errorf("[Syscmds] Couldn't get an user with the name or UID %s", args[0])
}
msg := connect(args[1:])
bot.SendMessage(user, "*[Sysadmin]* "+msg, util.Markdown)
log.Infof("[Syscmds] Sent message %[1]s to %[2]s", msg, user.Name)
} else if command == "broadcast" && len(args) > 0 {
msg := connect(args)
for _, user := range config.GetAllUsers() {
bot.SendMessage(user, "*[Sysadmin Broadcast]* "+msg, util.Markdown)
}
log.Infof("[Syscmds] Broadcasted message %[1]s", msg)
} else if command == "config" && len(args) > 0 {
if strings.EqualFold(args[0], "save") {
if !config.IndentConfig && len(args) > 1 && strings.EqualFold(args[0], "pretty") {
config.IndentConfig = true
config.Save()
config.IndentConfig = false
} else {
config.Save()
}
} else if strings.EqualFold(args[0], "load") {
config.Load()
}
} else if command == "stop" {
Shutdown("Sysadmin")
}
}
func connect(array []string) string {
var buffer bytes.Buffer
for _, val := range array {
buffer.WriteString(" ")
buffer.WriteString(val)
}
return buffer.String()[1:]
}