aboutsummaryrefslogtreecommitdiff
path: root/chatcommands.go
blob: 3f4c2333bc82dce670cfd23b709ee54713c23662 (plain) (blame)
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 (
	"fmt"
	"os"
	"strings"

	"github.com/HimbeerserverDE/mt-multiserver-proxy"
)

func init() {
	proxy.RegisterChatCmd(proxy.ChatCmd{
		Name: "shutdown",
		Perm: "cmd_shutdown",
		Handler: func(cc *proxy.ClientConn, args ...string) string {
			proc, err := os.FindProcess(os.Getpid())
			if err != nil {
				return "Could not find process: " + err.Error()
			}

			proc.Signal(os.Interrupt)
			return ""
		},
	})
	proxy.RegisterChatCmd(proxy.ChatCmd{
		Name: "reload",
		Perm: "cmd_reload",
		Handler: func(cc *proxy.ClientConn, args ...string) string {
			if err := proxy.LoadConfig(); err != nil {
				return "Configuration could not be reloaded. Old config is still active. Error: " + err.Error()
			}

			return "Configuration reloaded. You should restart if you encounter any problems."
		},
	})
	proxy.RegisterChatCmd(proxy.ChatCmd{
		Name: "perms",
		Perm: "cmd_perms",
		Handler: func(cc *proxy.ClientConn, args ...string) string {
			return "Your permissions: " + strings.Join(cc.Perms(), " ")
		},
	})
	proxy.RegisterChatCmd(proxy.ChatCmd{
		Name: "server",
		Perm: "cmd_server",
		Handler: func(cc *proxy.ClientConn, args ...string) string {
			if len(args) == 0 {
				srvs := make([]string, len(proxy.Conf().Servers))
				for i, srv := range proxy.Conf().Servers {
					srvs[i] = srv.Name
				}

				return fmt.Sprintf("Connected to: %s | Servers: %s", cc.ServerName(), strings.Join(srvs, " "))
			}

			srv := strings.Join(args, " ")
			if cc.ServerName() == srv {
				return "Already connected to this server."
			}

			if err := cc.Hop(srv); err != nil {
				return "Could not switch servers. Reconnect if you encounter any problems. Error: " + err.Error()
			}

			return ""
		},
	})
}