-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
475 lines (413 loc) · 10 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package main
import (
"./telegrambot"
"./xmpp"
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/gorilla/mux"
"io/ioutil"
"log"
"net/http"
"path"
"regexp"
"runtime/debug"
"strconv"
"strings"
"time"
)
var (
config = flag.String("config", "settings.json", "configuration file")
conf Configuration
bot *telegrambot.Bot
users = make(map[int]*User)
currentUpdateId = 0
)
const (
MAX_ACCOUNTS = 2
GREETING = `
Hi, this is a jabber client for Telegram.
Start the work with ` + "`" + "/connect" + "`" + `command.
For now it can only get messages and supports replying to them.`
)
const (
CMD_UNDEFINED = iota
CMD_CONNECT = iota
CMD_CHECK = iota
CMD_DISCONNECT = iota
CMD_BOT_MESSAGE = iota
CMD_MESSAGE = iota
CMD_START = iota
)
const (
STATUS_CONNECTING = iota
STATUS_CONNECTED = iota
STATUS_DISCONNECTED = iota
)
/* Contains user details */
type User struct {
accounts map[string]*Account
command *Command
}
/* Account represents connection on this level,
UserId - id of telegram user
Jid, Host, Port - connection params
For security reasons we do not keep password here
messageJids - used for messages reply, when we get some message we keep
its id and sender, and when user wants to reply, we can determine receiver
of reply
client - xmpp connection
*/
type Account struct {
UserId int
Jid string
Host string
Port uint16
messageJids map[int]string
client *xmpp.Client
status int
}
/* Configuration, filled from settings file */
type Configuration struct {
Listen int `json:"listen"`
Token string `json:"token"`
BaseDomain string `json:"base_domain"`
HookPath string `json:"hook_path"`
AdminUserId int `json:"admin_user_id"`
Debug bool `json:"debug"`
}
/* Messages to bot parsed to this struct if they has some meaning */
type Command struct {
Cmd int
Jid string
Host string
Port uint16
From int
UserId int
Message string
/* Used in multi-step commands */
Step int
MsgId int
}
func (c *Command) Next(msg string) {
c.MsgId = bot.SendReplyMessage(c.From, msg)
c.Step += 1
}
func (c *Command) Again(msg string) {
c.MsgId = bot.SendReplyMessage(c.From, msg)
}
/* Load configuration from specified file and connect to database */
func loadConfiguration() {
flag.Parse()
confData, err := ioutil.ReadFile(*config)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(confData, &conf)
if err != nil {
log.Fatal("Configuration decoding error: ", err)
}
validToken := regexp.MustCompile(`^\d+:[\w\-]+$`)
if !validToken.MatchString(conf.Token) {
log.Fatal("Invalid token format")
}
xmpp.Init()
}
/* Creates bot, checks it and shows in console hook path */
func setupBot() {
bot = &telegrambot.Bot{Token: conf.Token}
ok, info := bot.GetMe()
if !ok {
log.Fatal("Bot setup error")
}
log.Printf("Bot username is: %s", info["username"].(string))
hookPath := "https://" + path.Join(conf.BaseDomain, conf.HookPath)
log.Println("Hook expected on: ", hookPath)
go func() {
time.Sleep(2 * time.Second)
ok := bot.SetWebhook(hookPath)
if ok {
log.Println("Bot setWebhook: ok")
} else {
log.Fatal("Bot setWebhook: error")
}
}()
}
/* Creates xmpp connection
User can have more than one connection, it all is keeped in `accounts`
First key is user_id, second - jid
It creates client, start client listening, and start own goroutine
that listens messages from client channel
*/
func Connect(user_id int, jid string, password string,
host string, port uint16) error {
var account *Account
var found bool
user_accounts := users[user_id].accounts
if account, found = user_accounts[jid]; !found {
if len(user_accounts) >= MAX_ACCOUNTS {
return errors.New("Accounts limit exceeded")
}
account = &Account{
Jid: jid,
client: &xmpp.Client{Jid: jid},
status: STATUS_DISCONNECTED,
}
user_accounts[jid] = account
}
if account.status == STATUS_CONNECTED {
return errors.New("Account already connected")
} else if account.status == STATUS_CONNECTING {
return errors.New("Account is already trying to connect")
}
account.Host = host
account.Port = port
account.status = STATUS_CONNECTING
account.messageJids = make(map[int]string)
client := account.client
err := client.Connect(password, host, port)
if err != nil {
log.Println("Connection error: ", err)
return errors.New("Connection error")
}
client.Listen()
// start listening messages from jabber
go func() {
defer func() {
if err := recover(); err != nil {
log.Println("Messages listening error: ", err)
if conf.Debug {
debug.PrintStack()
}
}
account.status = STATUS_DISCONNECTED
SendMessage(user_id, fmt.Sprintf("%s disconnected (or connection failed)", jid))
}()
for event := range client.Channel {
switch event.EventType {
case xmpp.XMPP_CONN_CONNECT:
{
SendMessage(user_id, fmt.Sprintf("%s connected", jid))
account.status = STATUS_CONNECTED
}
case xmpp.XMPP_CONN_DISCONNECT:
{
SendMessage(user_id, fmt.Sprintf("%s disconnected", jid))
}
case xmpp.XMPP_CONN_FAIL:
{
SendMessage(user_id, fmt.Sprintf("%s connection fail", jid))
}
case xmpp.XMPP_MESSAGE:
{
msg := event.Msg
msg_jid := strings.Split(msg.From, "/")[0]
text := fmt.Sprintf("%s %s", msg_jid, msg.Text)
message_id := SendMessage(user_id, text)
if message_id > 0 {
account.messageJids[message_id] = msg_jid
}
}
}
}
}()
return nil
}
func Disconnect(user_id int) {
if user, ok := users[user_id]; ok {
for _, account := range user.accounts {
account.client.Disconnect()
}
}
}
func SendMessage(user_id int, text string) int {
return bot.SendMessage(user_id, text)
}
func parseCommand(message *telegrambot.Message) (*Command, error) {
defer func() {
if err := recover(); err != nil {
log.Println("Command parse error", message.Text)
}
}()
text := message.Text
command := &Command{
From: message.From.Id,
Step: 0,
Cmd: CMD_UNDEFINED,
}
if command.From == 0 {
log.Println("Something went wrong (command.From == 0)")
return nil, errors.New("Internal error")
}
if len(text) > 5000 {
return nil, errors.New("Too long command")
}
parts := strings.Split(strings.Trim(text, " "), " ")
cmd := parts[0]
if cmd == "/connect" {
command.Cmd = CMD_CONNECT
} else if cmd == "/check" && conf.AdminUserId == command.From {
command.Cmd = CMD_CHECK
} else if cmd == "/disconnect" {
command.Cmd = CMD_DISCONNECT
} else if cmd == "/start" {
command.Cmd = CMD_START
} else if cmd == "/bot_message" && conf.AdminUserId == command.From {
user_id, err := strconv.Atoi(parts[1])
if err == nil {
command.Cmd = CMD_BOT_MESSAGE
command.Message = strings.Join(parts[2:], " ")
command.UserId = user_id
}
} else if cmd == "/message" {
// todo: add support message to anybody
command.Cmd = CMD_MESSAGE
}
/*
* If command can't be parsed, try to send it to admin.
* It can be just an error
*/
if command.Cmd == CMD_UNDEFINED {
if conf.AdminUserId > 0 {
text := fmt.Sprintf("%d %s %s %s",
message.From.Id,
message.From.FirstName,
message.From.Username,
message.Text)
SendMessage(conf.AdminUserId, text)
}
return nil, errors.New("Unknown command")
}
return command, nil
}
func onUpdate(update *telegrambot.Update) {
var command *Command
var user *User
var err error
defer func() {
if err := recover(); err != nil {
log.Println("Update handling error: ", err)
if conf.Debug {
debug.PrintStack()
}
}
}()
if currentUpdateId > 0 && update.Id < currentUpdateId {
return
}
currentUpdateId = update.Id
message := &update.Msg
user_id := message.From.Id
// there is no message
if user_id == 0 {
return
}
// only private chats allowed
if user_id != message.Chat.Id {
return
}
// skip forwared messages
if message.ForwardDate > 0 {
return
}
/* adding to users map */
if _, ok := users[user_id]; !ok {
users[user_id] = &User{
command: nil,
accounts: make(map[string]*Account),
}
}
command = nil
reply_to_id := message.ReplyTo.MessageId
user = users[user_id]
if reply_to_id > 0 {
// is reply
reply_failed := true
if user.command != nil && user.command.MsgId == reply_to_id {
command = user.command
reply_failed = false
} else {
user_accounts := user.accounts
for _, account := range user_accounts {
if jid, ok := account.messageJids[reply_to_id]; ok {
account.client.SendMessage(jid, message.Text)
return
}
}
}
if reply_failed {
SendMessage(user_id, "Error. Reply wasn't sent")
return
}
}
if command == nil {
command, err = parseCommand(message)
if err != nil {
SendMessage(message.From.Id, err.Error())
return
}
}
switch command.Cmd {
case CMD_CONNECT:
{
switch command.Step {
case 0:
{
command.Next("Enter your jabber id:")
user.command = command
}
case 1:
{
if !EmailIsValid(message.Text) {
command.Again("Invalid jabber id. Try again")
break
}
command.Next("Enter your password:")
command.Jid = message.Text
}
case 2:
{
err = Connect(command.From, command.Jid, message.Text,
command.Host, command.Port)
if err != nil {
SendMessage(command.From, err.Error())
}
}
default:
log.Println("Something went wrong in connecting")
}
}
case CMD_DISCONNECT:
Disconnect(command.From)
case CMD_BOT_MESSAGE:
SendMessage(command.UserId, command.Message)
case CMD_START:
SendMessage(command.From, GREETING)
case CMD_CHECK:
text := `
I'm alive.
Users count: %d.
`
SendMessage(conf.AdminUserId, fmt.Sprintf(text, len(users)))
}
}
func listen() {
routes := mux.NewRouter()
bot.OnUpdate = onUpdate
routes.HandleFunc(conf.HookPath, bot.Hook)
s := &http.Server{
Addr: fmt.Sprintf(":%d", conf.Listen),
Handler: routes,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Fatal(s.ListenAndServe())
}
func main() {
log.Println("Server started")
loadConfiguration()
setupBot()
listen()
}