forked from iyear/E5SubBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.go
216 lines (204 loc) · 6.18 KB
/
control.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
package main
import (
"errors"
"fmt"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
tb "gopkg.in/tucnak/telebot.v2"
"io"
"log"
"os"
"strconv"
"strings"
"time"
)
var SignOk map[int64]int
//If Successfully return "",else return error information
func BindUser(m *tb.Message, cid, cse string) error {
logger.Printf("%d Begin Bind\n", m.Chat.ID)
tmp := strings.Split(m.Text, " ")
if len(tmp) != 2 {
logger.Printf("%d Bind error:Wrong Bind Format\n", m.Chat.ID)
return errors.New("绑定格式错误")
}
logger.Println("alias: " + tmp[1])
alias := tmp[1]
code := GetURLValue(tmp[0], "code")
//fmt.Println(code)
access, refresh, err := MSFirGetToken(code, cid, cse)
if err != nil {
logger.Printf("%d Bind error:GetRefreshToken %s \n", m.Chat.ID, err.Error())
return err
}
//token has gotten
bot.Send(m.Chat, "Token获取成功!")
info, err := MSGetUserInfo(access)
//fmt.Printf("TGID:%d Refresh Token: %s\n", m.Chat.ID, refresh)
if err != nil {
logger.Printf("%d Bind error:Getinfo %s \n", m.Chat.ID, err.Error())
return err
}
var u MSData
u.tgId = m.Chat.ID
u.refreshToken = refresh
//TG的Data传递最高64bytes,一些msid超过了报错BUTTON_DATA_INVALID (0),采取md5
u.msId = Get16MD5Encode(gjson.Get(info, "id").String())
u.uptime = time.Now().Unix()
logger.Println(u.uptime)
u.alias = alias
u.clientId = cid
u.clientSecret = cse
u.other = ""
//MS User Is Exist
if MSAppIsExist(u.tgId, u.clientId) {
logger.Printf("%d Bind error:MSUserHasExisted\n", m.Chat.ID)
return errors.New("该应用已经绑定过了,无需重复绑定")
}
//MS information has gotten
bot.Send(m.Chat, "MS_ID(MD5): "+u.msId+"\nuserPrincipalName: "+gjson.Get(info, "userPrincipalName").String()+"\ndisplayName: "+gjson.Get(info, "displayName").String()+"\n")
if ok, err := AddData(u); !ok {
logger.Printf("%d Bind error: %s\n", m.Chat.ID, err)
return err
}
logger.Printf("%d Bind Successfully!\n", m.Chat.ID)
return nil
}
//get bind num
func GetBindNum(tgId int64) int {
data := QueryDataByTG(tgId)
return len(data)
}
//return true => exist
func MSAppIsExist(tgId int64, clientId string) bool {
data := QueryDataByTG(tgId)
var res MSData
for _, res = range data {
if res.clientId == clientId {
return true
}
}
return false
}
//SignTask
func SignTask() {
var (
SignOk map[int64]int
SignErr []string
UnbindUser []string
num, signOk int
)
SignOk = make(map[int64]int)
fmt.Println("----Task Begin----")
fmt.Println("Time:" + time.Now().Format("2006-01-02 15:04:05"))
data := QueryDataAll()
num = len(data)
fmt.Println("Start Sign")
//签到任务
for _, u := range data {
pre := "您的账户: " + u.alias + "\n在任务执行时出现了错误!\n错误:"
access, err := MSGetToken(u.refreshToken, u.clientId, u.clientSecret)
chat, _ := bot.ChatByID(strconv.FormatInt(u.tgId, 10))
//生成解绑按钮
var inlineKeys [][]tb.InlineButton
UnBindBtn := tb.InlineButton{Unique: "un" + u.msId, Text: "点击解绑该账户", Data: u.msId}
bot.Handle(&UnBindBtn, bUnBindInlineBtn)
inlineKeys = append(inlineKeys, []tb.InlineButton{UnBindBtn})
tmpBtn := &tb.ReplyMarkup{InlineKeyboard: inlineKeys}
se := u.msId + " ( @" + chat.Username + " )"
if err != nil {
logger.Println(u.msId+" ", err)
bot.Send(chat, pre+gjson.Get(err.Error(), "error").String(), tmpBtn)
SignErr = append(SignErr, se)
ErrorTimes[u.msId]++
continue
}
if ok, err := OutLookGetMails(access); !ok {
logger.Println(u.msId+" ", err)
bot.Send(chat, pre+gjson.Get(err.Error(), "error").String(), tmpBtn)
ErrorTimes[u.msId]++
SignErr = append(SignErr, se)
continue
}
u.uptime = time.Now().Unix()
if ok, err := UpdateData(u); !ok {
logger.Println(u.msId+" ", err)
bot.Send(chat, pre+err.Error(), tmpBtn)
SignErr = append(SignErr, se)
ErrorTimes[u.msId]++
continue
}
fmt.Println(u.msId + " Sign OK!")
SignOk[u.tgId]++
signOk++
}
fmt.Println("Sign End,Start Send")
var isSend map[int64]bool
isSend = make(map[int64]bool)
//用户任务反馈
for _, u := range data {
chat, err := bot.ChatByID(strconv.FormatInt(u.tgId, 10))
if err != nil {
logger.Println("Send Result ERROR: ", err)
continue
}
//错误上限账户清退
if ErrorTimes[u.msId] == ErrMaxTimes {
logger.Println(u.msId + " Error Limit")
if ok, err := DelData(u.msId); !ok {
logger.Println(err)
} else {
UnbindUser = append(UnbindUser, u.msId+" ( @"+chat.Username+" )")
_, err = bot.Send(chat, "您的账户因达到错误上限而被自动解绑\n后会有期!\n\n别名: "+u.alias+"\nclient_id: "+u.clientId+"\nclient_secret: "+u.clientSecret)
if err != nil {
logger.Println(err)
}
}
}
if !isSend[u.tgId] {
//静默发送,过多消息很烦
_, err = bot.Send(chat, "任务反馈\n时间: "+time.Now().Format("2006-01-02 15:04:05")+"\n结果: "+strconv.Itoa(SignOk[u.tgId])+"/"+strconv.Itoa(GetBindNum(u.tgId)), &tb.SendOptions{DisableNotification: true})
if err != nil {
logger.Println(err)
}
isSend[u.tgId] = true
}
}
//管理员任务反馈
var ErrUserStr string
var UnbindUserStr string
for _, eu := range SignErr {
ErrUserStr = ErrUserStr + eu + "\n"
}
for _, ubu := range UnbindUser {
UnbindUserStr = UnbindUserStr + ubu + "\n"
}
for _, a := range admin {
chat, _ := bot.ChatByID(strconv.FormatInt(a, 10))
bot.Send(chat, "任务反馈(管理员)\n完成时间: "+time.Now().Format("2006-01-02 15:04:05")+"\n结果: "+strconv.Itoa(signOk)+"/"+strconv.Itoa(num)+"\n错误账户:\n"+ErrUserStr+"\n清退账户:\n"+UnbindUserStr)
}
fmt.Println("----Task End----")
}
func GetAdmin() []int64 {
var result []int64
admins := strings.Split(viper.GetString("admin"), ",")
for _, v := range admins {
id, _ := strconv.ParseInt(v, 10, 64)
result = append(result, id)
}
return result
}
func InitLogger() {
if !PathExists(bLogBasePath) {
os.Mkdir(bLogBasePath, 0773)
}
path := bLogBasePath + time.Now().Format("2006-01-02") + ".log"
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0773)
if err != nil {
logger.Println(err)
}
writers := []io.Writer{
f,
os.Stdout}
faoWriter := io.MultiWriter(writers...)
logger = log.New(faoWriter, "【E5Sub】", log.Ldate|log.Ltime|log.Lshortfile)
}