2017-12-23 17:03:26 +04:00
// i2_bot – Instinct PokememBro Bot
2018-04-04 21:33:30 +04:00
// Copyright (c) 2017-2018 Vladimir "fat0troll" Hodakov
2017-12-23 17:03:26 +04:00
package chatter
import (
"strconv"
"strings"
2018-03-31 17:44:12 +04:00
"github.com/go-telegram-bot-api/telegram-bot-api"
2018-05-19 12:14:25 +04:00
"github.com/fat0troll/i2_bot/lib/dbmapping"
2017-12-23 17:03:26 +04:00
)
func ( ct * Chatter ) userPrivilegesCheck ( update * tgbotapi . Update , user * tgbotapi . User ) bool {
// There are two special chats, pointed by config, where any member of league may be
2017-12-23 21:08:57 +04:00
defaultChatID , _ := strconv . ParseInt ( c . Cfg . SpecialChats . DefaultID , 10 , 64 )
2017-12-23 17:03:26 +04:00
bastionChatID , _ := strconv . ParseInt ( c . Cfg . SpecialChats . BastionID , 10 , 64 )
academyChatID , _ := strconv . ParseInt ( c . Cfg . SpecialChats . AcademyID , 10 , 64 )
2017-12-24 04:36:23 +04:00
hqChatID , _ := strconv . ParseInt ( c . Cfg . SpecialChats . HeadquartersID , 10 , 64 )
2018-04-04 21:33:30 +04:00
gamesChatID , _ := strconv . ParseInt ( c . Cfg . SpecialChats . GamesID , 10 , 64 )
2017-12-23 21:08:57 +04:00
2017-12-24 04:36:23 +04:00
if update . Message . Chat . ID == defaultChatID || update . Message . Chat . ID == hqChatID {
2017-12-23 21:08:57 +04:00
return true
}
2017-12-23 17:03:26 +04:00
// There are special users, which will bypass these checks
2018-04-08 22:32:51 +04:00
specialUsers := [ ] string { "gantz_yaka" , "agentpb" , "pbhelp" , "i2_bot" , "i2_dev_bot" , "grstatsbot" }
2017-12-23 17:03:26 +04:00
for j := range specialUsers {
2018-04-08 22:32:51 +04:00
if strings . Contains ( strings . ToLower ( user . UserName ) , specialUsers [ j ] ) {
2017-12-23 17:03:26 +04:00
// This is for PokememBro admins, they can join any chat at any time
return true
}
}
2018-04-07 17:22:00 +04:00
switch update . Message . Chat . ID {
case academyChatID :
c . Log . Debug ( "Checking user rights in academy chat..." )
case bastionChatID :
c . Log . Debug ( "Checking user rights in bastion chat..." )
case gamesChatID :
c . Log . Debug ( "Checking user rights in games chat..." )
case hqChatID :
c . Log . Debug ( "Checking user rights in headquarters chat..." )
}
2018-04-08 22:32:51 +04:00
if update . Message . Chat . ID == gamesChatID && user . IsBot {
2018-04-07 17:26:18 +04:00
c . Log . Debug ( "Game bot with username @" + user . UserName + " passed filtration" )
2018-04-04 21:33:30 +04:00
return true
}
2018-01-29 23:50:25 +04:00
playerRaw , err := c . DataCache . GetPlayerByTelegramID ( user . ID )
if err != nil {
c . Log . Error ( err . Error ( ) )
2017-12-23 17:03:26 +04:00
return false
}
2018-01-29 23:50:25 +04:00
if c . Users . PlayerBetterThan ( playerRaw , "admin" ) {
2017-12-24 04:36:23 +04:00
return true
}
2017-12-23 17:03:26 +04:00
// So, user is not a PokememBro admin. For Bastion and Academy she needs to be league player
switch update . Message . Chat . ID {
2018-04-04 21:33:30 +04:00
case academyChatID , bastionChatID , gamesChatID :
2018-02-17 07:03:58 +04:00
if playerRaw . LeagueID == 1 && playerRaw . Status != "spy" && playerRaw . Status != "league_changed" && playerRaw . Status != "banned" {
2017-12-23 17:03:26 +04:00
return true
}
default :
2018-02-17 07:03:58 +04:00
availableChatsForUser := c . DataCache . GetAvailableSquadsChatsForUser ( playerRaw . ID )
2017-12-23 17:03:26 +04:00
for i := range availableChatsForUser {
if update . Message . Chat . ID == availableChatsForUser [ i ] . TelegramID {
return true
}
}
}
2018-04-07 17:22:00 +04:00
c . Log . Debug ( "User failed to prove identity. Ban sequence arrived." )
2017-12-23 17:03:26 +04:00
return false
}
// BanUserFromChat removes user from chat
func ( ct * Chatter ) BanUserFromChat ( user * tgbotapi . User , chatRaw * dbmapping . Chat ) {
chatUserConfig := tgbotapi . ChatMemberConfig {
ChatID : chatRaw . TelegramID ,
UserID : user . ID ,
}
kickConfig := tgbotapi . KickChatMemberConfig {
ChatMemberConfig : chatUserConfig ,
UntilDate : 1893456000 ,
}
c . Log . Info ( "Trying to ban user..." )
_ , err := c . Bot . KickChatMember ( kickConfig )
if err != nil {
c . Log . Error ( err . Error ( ) )
}
bastionChatID , _ := strconv . ParseInt ( c . Cfg . SpecialChats . BastionID , 10 , 64 )
academyChatID , _ := strconv . ParseInt ( c . Cfg . SpecialChats . AcademyID , 10 , 64 )
hqChatID , _ := strconv . ParseInt ( c . Cfg . SpecialChats . HeadquartersID , 10 , 64 )
if ( chatRaw . TelegramID != bastionChatID ) || ( chatRaw . TelegramID != academyChatID ) {
2018-02-17 07:03:58 +04:00
squad , err := c . DataCache . GetSquadByChatID ( chatRaw . ID )
if err != nil {
c . Log . Error ( err . Error ( ) )
} else {
// In Bastion notifications are public in default chat
commanders := c . DataCache . GetCommandersForSquad ( squad . ID )
2017-12-23 17:03:26 +04:00
for i := range commanders {
message := "Некто " + c . Users . GetPrettyName ( user ) + " попытался зайти в чат _" + chatRaw . Name + "_ и был изгнан ботом, так как не имеет права посещать этот чат."
msg := tgbotapi . NewMessage ( int64 ( commanders [ i ] . TelegramID ) , message )
msg . ParseMode = "Markdown"
c . Bot . Send ( msg )
}
}
} else {
message := "Некто " + c . Users . GetPrettyName ( user ) + " попытался зайти в один из общих чатов лиги и был изгнан ботом, так как не имеет права посещать этот чат."
msg := tgbotapi . NewMessage ( hqChatID , message )
msg . ParseMode = "Markdown"
c . Bot . Send ( msg )
}
}
// ProtectChat protects chats from unauthorized access
// Returns "protection_passed" if all protection checks passed
func ( ct * Chatter ) ProtectChat ( update * tgbotapi . Update , playerRaw * dbmapping . Player , chatRaw * dbmapping . Chat ) string {
// Check on new user addition
if update . Message . NewChatMembers != nil {
newUsers := * update . Message . NewChatMembers
if len ( newUsers ) > 0 {
2018-03-31 17:44:12 +04:00
c . Log . Debug ( "New users joined/added to chat. Checking rights for them." )
2017-12-23 17:03:26 +04:00
for i := range newUsers {
newUserPassed := ct . userPrivilegesCheck ( update , & newUsers [ i ] )
if ! newUserPassed {
2018-03-31 17:44:12 +04:00
c . Log . Debug ( "This user can't be here: removing from chat..." )
2017-12-23 17:03:26 +04:00
ct . BanUserFromChat ( & newUsers [ i ] , chatRaw )
}
}
}
}
existingUserPassed := ct . userPrivilegesCheck ( update , update . Message . From )
if ! existingUserPassed {
2018-03-31 17:44:12 +04:00
c . Log . Debug ( "Existing chat user can't be here. Vanishing..." )
2017-12-23 17:03:26 +04:00
ct . BanUserFromChat ( update . Message . From , chatRaw )
return "fail"
}
2018-02-17 07:03:58 +04:00
return "ok"
2017-12-23 17:03:26 +04:00
}