-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgshell.go
164 lines (150 loc) · 3.35 KB
/
tgshell.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"
"github.com/codeskyblue/go-sh"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func handle_guest(m *tgbotapi.Message) {
id := m.From.ID
name := m.From.String()
var reply string
if !acl_exists(id) {
reply = fmt.Sprintf("Hello, %s! I will memorize you", name)
acl_touch(id, name)
} else {
reply = fmt.Sprintf("I know you, %s.", name)
}
send_reply(m, false, reply)
}
func handle_uptime(m *tgbotapi.Message) {
send_reply(m, false, "UPTIME NIY")
}
func dispatch(m *tgbotapi.Message) bool {
id := m.From.ID
name := m.From.String()
cmd := strings.ToUpper(GetFirstToken(m.Text))
handler, ok := handlers[cmd]
if ok {
if acl_can(id, handler.perm) {
go handler.proc(m)
} else {
send_reply(m, true, fmt.Sprintf("[%v]%v cannot into %s",
id, name, handler.perm))
}
return true
}
if m.Chat.Type == "private" {
send_reply(m, true, fmt.Sprintf("I don't understood, what %v is", cmd))
}
return false
}
func register_all() {
register_base()
register_exec()
register_sh()
register_files()
register_screenshot()
register_acl()
register_lock()
register_halt()
}
func workSession() {
log.Println("start work session")
var err error
for {
bot, err = tgbotapi.NewBotAPI(config.Token)
if err == nil {
break
}
log.Println("tgbotapi.NewBotAPI() got error", err, ". sleep and re-init")
time.Sleep(RECONNECT_INTERVAL)
}
//bot.Debug = true
sigchan = make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
signal.Notify(sigchan, syscall.SIGTERM)
//defer Stop(sigchan)
go func() {
sig := <-sigchan
inform(fmt.Sprintf("Got signal <%s>.\nExecution terminated", sig))
os.Exit(exitcode)
}()
shell = sh.NewSession()
log.Printf("authiorized as %s", bot.Self.UserName)
inform_at_start()
defer inform_at_stop()
register_all()
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
var updates <-chan tgbotapi.Update
for {
updates, err = bot.GetUpdatesChan(u)
if err == nil {
break
}
log.Printf("GetUpdatesChan() got error %s\nsleep and try again", err)
time.Sleep(RECONNECT_INTERVAL)
}
for update := range updates {
m := update.Message
log.Printf("got message:\n%s", ppj(m))
if m == nil {
continue // we have no deals with malformed messages
}
from := m.From
if from == nil {
continue // we have no deals with anonymous messages
}
acl_touch(from.ID, from.String()) // remember/refresh acl
if !dispatch(m) {
if m.Chat.Type == "private" {
msg := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf(
"U said: <<%s>>", m.Text))
msg.ReplyToMessageID = m.MessageID
bot.Send(msg)
}
}
}
log.Println("finish work session")
}
func fg_main() {
}
var fg = flag.Bool("fg", false, "start in foreground mode")
func start_daemon() {
args := append(append([]string(nil), "-fg"), os.Args[1:]...)
cmd := exec.Command(os.Args[0], args...)
cmd.Start()
fmt.Println("[PID]", cmd.Process.Pid)
os.Exit(0)
}
func main() {
flag.Parse()
if !*fg {
start_daemon()
}
SetupLogger()
if err := LoadConfig(); err != nil {
fmt.Println(err.Error())
if err := SaveConfig(); err != nil {
fmt.Println(err.Error())
}
os.Exit(1)
}
if err := ValidateConfig(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
for {
workSession()
log.Println("delay and return to work")
time.Sleep(RECONNECT_INTERVAL)
}
}