|
1 | 1 | // Copyright 2020 the commonlispbr authors. All rights reserved |
2 | 2 | // Use of this source code is governed by a BSD-style |
3 | 3 | // license that can be found in the LICENSE file. |
4 | | - |
5 | 4 | package main |
6 | 5 |
|
7 | 6 | import ( |
@@ -39,6 +38,14 @@ var trollGroups = []string{ |
39 | 38 | "@mlbrasil", |
40 | 39 | } |
41 | 40 |
|
| 41 | +var passList = []string{} |
| 42 | + |
| 43 | +var admins = []string{ |
| 44 | + "lerax", |
| 45 | + "luksamuk", |
| 46 | + "perkunos", |
| 47 | +} |
| 48 | + |
42 | 49 | const logfile = "troll-shield.log" |
43 | 50 | const killsFile = "kills.txt" |
44 | 51 |
|
@@ -192,7 +199,7 @@ func setupBot(envVar string) (*telegram.BotAPI, error) { |
192 | 199 | bot, err := telegram.NewBotAPI(token) |
193 | 200 |
|
194 | 201 | if err != nil { |
195 | | - return nil, fmt.Errorf("Setup %v failed with: %v", envVar, err) |
| 202 | + return nil, fmt.Errorf("setup %v failed with: %v", envVar, err) |
196 | 203 | } |
197 | 204 |
|
198 | 205 | bot.Debug = true |
@@ -266,3 +273,61 @@ func reportKills(bot TrollShieldBot, update *telegram.Update, kills int64) { |
266 | 273 | } |
267 | 274 | reply(bot, update, txt) |
268 | 275 | } |
| 276 | + |
| 277 | +// parse: |
| 278 | +// - /pass <@username> |
| 279 | +// - /pass <FirstName [LastName]> |
| 280 | +// and return what is available |
| 281 | +func extractPassUserName(command string) string { |
| 282 | + tokens := strings.Split(command, " ") |
| 283 | + n := len(tokens) |
| 284 | + return strings.Join(tokens[1:n], " ") |
| 285 | +} |
| 286 | + |
| 287 | +// If has pass, return true and and return the matched pass |
| 288 | +func hasPass(user telegram.User) (string, bool) { |
| 289 | + userName := getUserName(user) |
| 290 | + for _, pass := range passList { |
| 291 | + if strings.HasPrefix(userName, pass) || user.FirstName == pass { |
| 292 | + return pass, true |
| 293 | + } |
| 294 | + } |
| 295 | + return "", false |
| 296 | +} |
| 297 | + |
| 298 | +// remove pass list and reply the consumed pass list |
| 299 | +func removePassList(bot TrollShieldBot, update *telegram.Update, pass string) { |
| 300 | + // Remove the element at index i from a. |
| 301 | + n := len(passList) |
| 302 | + for i, p := range passList { |
| 303 | + if p == pass { |
| 304 | + // remove pass from passList |
| 305 | + passList[i] = passList[n-1] // Copy last element to index i. |
| 306 | + passList[n-1] = "" // Erase last element (write zero value). |
| 307 | + passList = passList[:n-1] // Truncate slice. |
| 308 | + } |
| 309 | + } |
| 310 | + reply(bot, update, fmt.Sprintf("O passe para %q foi consumido.", pass)) |
| 311 | +} |
| 312 | + |
| 313 | +// check if a message cames from a @commonlispbr admin |
| 314 | +func fromAdminEvent(update *telegram.Update) bool { |
| 315 | + if update.Message.From == nil { |
| 316 | + return false |
| 317 | + } |
| 318 | + fromUserName := update.Message.From.UserName |
| 319 | + for _, admin := range admins { |
| 320 | + if admin == fromUserName { |
| 321 | + return true |
| 322 | + } |
| 323 | + } |
| 324 | + |
| 325 | + return false |
| 326 | +} |
| 327 | + |
| 328 | +// addPass to passList and send a message |
| 329 | +func addPassList(bot TrollShieldBot, update *telegram.Update) { |
| 330 | + userName := extractPassUserName(update.Message.Text) |
| 331 | + passList = append(passList, userName) |
| 332 | + reply(bot, update, fmt.Sprintf("O passe para %q foi adicionado.", userName)) |
| 333 | +} |
0 commit comments