diff --git a/ban.go b/ban.go index e9dd0b3..6835cba 100644 --- a/ban.go +++ b/ban.go @@ -7,15 +7,14 @@ import ( "io" "os" "strconv" - "time" log "github.com/go-pkgz/lgr" "github.com/gotd/td/tg" ) -// bans users from given file, cleans up their messages and kicks them afterwards +// bans users from given file, cleans up their messages and kicks them afterwards. +// in case of errors during the run, writes unprocessed errors back to the same file. func banAndKickUsers(ctx context.Context, api *tg.Client, channel *tg.Channel, filePath string) { - users, err := readUserIDsFromCSV(filePath) if err != nil { log.Printf("[ERROR] error reading users from the file %s: %v", filePath, err) @@ -31,21 +30,18 @@ func banAndKickUsers(ctx context.Context, api *tg.Client, channel *tg.Channel, f if stoppedIndex == 0 { log.Printf("[INFO] Canceled without processing any entries, restart the same command to ban users") } - fileName := fmt.Sprintf("./ban/%s.unprocessed.users.csv", time.Now().Format("2006-01-02T15-04-05")) usersToBan := make([]banUserInfo, len(users[stoppedIndex:])) for i, user := range users[stoppedIndex:] { usersToBan[i] = banUserInfo{userID: user.UserID, accessHash: user.AccessHash} } - if e := writeUsersToFile(usersToBan, fileName); err != nil { + if e := writeUsersToFile(usersToBan, filePath); err != nil { log.Printf("[ERROR] Error writing rest of users to ban after context cancel to file: %v", e) } else { - log.Printf("[INFO] Success, rest of users (%d-%d) to ban after context cancel written to %s", + log.Printf("[INFO] Success, rest of users (%d-%d) to ban after context cancel written to the same file %s, restart the same command to ban users", stoppedIndex, len(users), - fileName) - log.Printf("[INFO] To continue, run the same command with the new filename:") - log.Printf("[INFO] --ban_and_kick_filepath %s", fileName) + filePath) } } diff --git a/main.go b/main.go index c398d86..0389b32 100644 --- a/main.go +++ b/main.go @@ -86,6 +86,7 @@ func main() { } }() + // credentials are stored per phone number telegramOptions := telegram.Options{ Middlewares: []telegram.Middleware{waiter}, SessionStorage: &telegram.FileSessionStorage{Path: fmt.Sprintf("./ban/%s.json", opts.Phone)}, @@ -123,25 +124,28 @@ func main() { return err } - if opts.BanAndKickFilePath == "" { - if opts.BanToTimestamp == 0 { - log.Printf("[ERROR] ban_to must be set when searching for users") - return nil - } - if opts.BanSearchDuration.Seconds() <= 0 { - log.Printf("[ERROR] ban_search_duration must be non-zero when searching for users") - return nil - } - searchAndStoreUsersToBan(ctx, api, channel, searchParams{ - endUnixTime: opts.BanToTimestamp, - duration: opts.BanSearchDuration, - offset: opts.BanSearchOffset, - limit: opts.BanSearchLimit, - ignoreMessages: opts.SearchIgnoreMessages, - }) - } else { + // ban users case + if opts.BanAndKickFilePath != "" { banAndKickUsers(ctx, api, channel, opts.BanAndKickFilePath) + return nil + } + + // retrieve users to ban case + if opts.BanToTimestamp == 0 { + log.Printf("[ERROR] ban_to must be set when searching for users") + return nil + } + if opts.BanSearchDuration.Seconds() <= 0 { + log.Printf("[ERROR] ban_search_duration must be non-zero when searching for users") + return nil } + searchAndStoreUsersToBan(ctx, api, channel, searchParams{ + endUnixTime: opts.BanToTimestamp, + duration: opts.BanSearchDuration, + offset: opts.BanSearchOffset, + limit: opts.BanSearchLimit, + ignoreMessages: opts.SearchIgnoreMessages, + }) return nil }); err != nil { @@ -224,7 +228,7 @@ func writeUsersToFile(users []banUserInfo, fileName string) error { }() } - data := [][]string{{"joined", "userID", "access_hash", "username", "firstName", "lastName", "langCode", "message"}} + data := [][]string{{"joined", "userID", "access_hash", "username", "firstName", "lastName", "message"}} for _, user := range users { data = append(data, []string{ @@ -234,7 +238,6 @@ func writeUsersToFile(users []banUserInfo, fileName string) error { user.username, // username strings.ReplaceAll(user.firstName, "\t", " "), // firstName strings.ReplaceAll(user.lastName, "\t", " "), // lastName - user.langCode, // langCode strings.ReplaceAll(user.message, "\t", " "), // message }) } diff --git a/retrieve.go b/retrieve.go index 2b28a9d..c58b2dc 100644 --- a/retrieve.go +++ b/retrieve.go @@ -22,7 +22,6 @@ type banUserInfo struct { username string firstName string lastName string - langCode string } type channelParticipantInfo struct { @@ -158,7 +157,6 @@ func getSingleUserStoreInfo(ctx context.Context, api *tg.Client, channel *tg.Cha userInfoToStore.username = userToBan.info.Username userInfoToStore.firstName = userToBan.info.FirstName userInfoToStore.lastName = userToBan.info.LastName - userInfoToStore.langCode = userToBan.info.LangCode userInfoToStore.accessHash = userToBan.info.AccessHash var message string