forked from drakenot/gramarr
/
handler_auth.go
94 lines (80 loc) · 2.26 KB
/
handler_auth.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
package main
import (
"fmt"
"strings"
tb "gopkg.in/tucnak/telebot.v2"
)
func (e *Env) HandleAuth(m *tb.Message) {
var msg []string
pass := m.Payload
user, exists := e.Users.User(m.Sender.ID)
// Empty Password?
if pass == "" {
Send(e.Bot, m.Sender, "Usage: `/auth [password]`")
return
}
// Is User Already Admin?
if exists && user.IsAdmin() {
// Notify User
msg = append(msg, "You're already authorized.")
msg = append(msg, "Type /start to begin.")
Send(e.Bot, m.Sender, strings.Join(msg, "\n"))
return
}
// Check if pass is Admin Password
if pass == e.Config.Bot.AdminPassword {
if exists {
user.Access = UAAdmin
e.Users.Update(user)
} else {
newUser := User{
ID: m.Sender.ID,
FirstName: m.Sender.FirstName,
LastName: m.Sender.LastName,
Username: m.Sender.Username,
Access: UAAdmin,
}
e.Users.Create(newUser)
}
// Notify User
msg = append(msg, "You have been authorized as an *admin*.")
msg = append(msg, "Type /start to begin.")
Send(e.Bot, m.Sender, strings.Join(msg, "\n"))
// Notify Admin
adminMsg := fmt.Sprintf("%s has been granted admin access.", DisplayName(m.Sender))
SendAdmin(e.Bot, e.Users.Admins(), adminMsg)
return
}
// Check if pass is User Password
if pass == e.Config.Bot.Password {
if exists {
// Notify User
msg = append(msg, "You're already authorized.")
msg = append(msg, "Type /start to begin.")
Send(e.Bot, m.Sender, strings.Join(msg, "\n"))
return
}
newUser := User{
ID: m.Sender.ID,
Username: m.Sender.Username,
FirstName: m.Sender.FirstName,
LastName: m.Sender.LastName,
Access: UAMember,
}
e.Users.Create(newUser)
// Notify User
msg = append(msg, "You have been authorized.")
msg = append(msg, "Type /start to begin.")
Send(e.Bot, m.Sender, strings.Join(msg, "\n"))
// Notify Admin
adminMsg := fmt.Sprintf("%s has been granted acccess.", DisplayName(m.Sender))
SendAdmin(e.Bot, e.Users.Admins(), adminMsg)
return
}
// Notify User
SendError(e.Bot, m.Sender, "Your password is invalid.")
// Notify Admin
adminMsg := "%s made an invalid auth request with password: %s"
adminMsg = fmt.Sprintf(adminMsg, DisplayName(m.Sender), EscapeMarkdown(m.Payload))
SendAdmin(e.Bot, e.Users.Admins(), adminMsg)
}