Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
remove search by join messages as unreliable
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Oct 29, 2022
1 parent 8b265a4 commit 789cdc5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 86 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ Currently, the only filter is by the join time to kill the bot hoards.

## CLI parameters

| Command line | Default | Description |
|------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------|
| appid | | AppID, _required_ |
| apphash | | AppHash, _required_ |
| phone | | Telegram phone of the channel admin, _required_ |
| password | `` | password, if set for the admin, _optional_ |
| channel_id | | channel or supergroup id, without -100 part, _required_ |
| ban_to_timestamp | | the end of the time from which newly joined users will be banned, _required for search_ |
| ban_search_duration | | amount of time before the ban_to for which we need to ban users, _required for search_ |
| ban_search_offset | `0` | starting offset of search, useful if you banned the offenders in first N users already. If provided, slower user search is used. |
| ban_search_limit | `0` | limit of users to check for ban, 0 is unlimited |
| ban_and_kick_filepath | | set this option to path to text file with users clean up their messages, ban and kick them |
| search_ignore_messages | `false` | do not retrieve messages when searching for users to ban |
| dbg | `false` | debug mode |
| Command line | Default | Description |
|------------------------|---------|--------------------------------------------------------------------------------------------|
| appid | | AppID, _required_ |
| apphash | | AppHash, _required_ |
| phone | | Telegram phone of the channel admin, _required_ |
| password | `` | password, if set for the admin, _optional_ |
| channel_id | | channel or supergroup id, without -100 part, _required_ |
| ban_to_timestamp | | the end of the time from which newly joined users will be banned, _required for search_ |
| ban_search_duration | | amount of time before the ban_to for which we need to ban users, _required for search_ |
| ban_search_offset | `0` | starting offset of search, useful if you banned the offenders in first N users already |
| ban_search_limit | `0` | limit of users to check for ban, 0 is unlimited |
| ban_and_kick_filepath | | set this option to path to text file with users clean up their messages, ban and kick them |
| search_ignore_messages | `false` | do not retrieve messages when searching for users to ban |
| dbg | `false` | debug mode |


## How to run
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type options struct {
ChannelID int64 `long:"channel_id" description:"channel or supergroup id, without -100 part, https://gist.github.com/mraaroncruz/e76d19f7d61d59419002db54030ebe35" required:"true"`
BanToTimestamp int64 `long:"ban_to_timestamp" description:"the end of the time from which newly joined users will be banned, unix timestamp"`
BanSearchDuration time.Duration `long:"ban_search_duration" description:"amount of time before the ban_to for which we need to ban users"`
BanSearchOffset int `long:"ban_search_offset" description:"starting offset of search, useful if you banned the offenders in first N users already. If provided, slower user search is used."`
BanSearchOffset int `long:"ban_search_offset" description:"starting offset of search, useful if you banned the offenders in first N users already"`
BanSearchLimit int `long:"ban_search_limit" description:"limit of users to check for ban, 0 is unlimited"`
SearchIgnoreMessages bool `long:"search_ignore_messages" description:"do not retrieve messages when searching for users to ban"`
BanAndKickFilePath string `long:"ban_and_kick_filepath" description:"set this option to path to text file with users clean up their messages, ban and kick them"`
Expand Down
72 changes: 1 addition & 71 deletions retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ func searchAndStoreUsersToBan(ctx context.Context, api *tg.Client, channel *tg.C

// Buffered channel with users to ban
nottyList := make(chan channelParticipantInfo, requestLimit)
if params.offset != 0 {
go getChannelMembersWithinTimeframe(ctx, api, channel, banFrom, banTo, params.offset, params.limit, nottyList)
} else {
go getChannelMembersByJoinMessage(ctx, api, channel, banFrom, banTo, params.limit, nottyList)
}
go getChannelMembersWithinTimeframe(ctx, api, channel, banFrom, banTo, params.offset, params.limit, nottyList)

fileName := fmt.Sprintf("./ban/%s.users.csv", time.Now().Format("2006-01-02T15-04-05"))

Expand Down Expand Up @@ -114,72 +110,6 @@ func getChannelMembersWithinTimeframe(ctx context.Context, api *tg.Client, chann
}
}

// getSingleUserStoreInfo retrieves extended user info for all users who joined in the given period,
// closes provided channel before returning, supposed to be run in goroutine.
func getChannelMembersByJoinMessage(ctx context.Context, api *tg.Client, channel *tg.Channel, banFrom, banTo time.Time, searchLimit int, users chan<- channelParticipantInfo) {
defer close(users)
var offsetID int
var processed int
for {
if searchLimit != 0 && processed >= searchLimit {
break
}
messages, err := api.MessagesSearch(ctx, &tg.MessagesSearchRequest{
Peer: channel.AsInputPeer(),
Filter: &tg.InputMessagesFilterEmpty{},
MinDate: int(banFrom.Unix()),
MaxDate: int(banTo.Unix()),
Limit: requestLimit,
OffsetID: offsetID,
})
if err != nil {
log.Printf("[ERROR] Error retrieving messages: %v", err)
break
}
if messages.Zero() {
break
}
processed += requestLimit
var rawMessages []tg.MessageClass
switch v := messages.(type) {
case *tg.MessagesMessages:
rawMessages = v.Messages
case *tg.MessagesMessagesSlice:
rawMessages = v.Messages
case *tg.MessagesChannelMessages:
rawMessages = v.Messages
}

for _, message := range rawMessages {
offsetID = message.GetID()
if m, ok := message.(*tg.MessageService); ok {
if peer, okM := m.GetFromID(); okM {
if u, okU := peer.(*tg.PeerUser); okU {
participant, e := api.ChannelsGetParticipant(ctx, &tg.ChannelsGetParticipantRequest{
Channel: channel.AsInput(),
Participant: &tg.InputPeerUserFromMessage{
Peer: channel.AsInputPeer(),
MsgID: m.GetID(),
UserID: u.GetUserID(),
},
})
if e != nil || participant.Zero() {
continue
}
for _, pUser := range participant.Users {
if user, okCh := pUser.(*tg.User); okCh {
users <- channelParticipantInfo{info: user, participantInfo: &tg.ChannelParticipant{UserID: user.GetID(), Date: m.GetDate()}}
}
}

}
}
}
}
log.Printf("[INFO] Processed %d messages", processed)
}
}

// getUsersInfo retrieves extended user info for every user in given channel, as well as single message sent by such user
func getUsersInfo(ctx context.Context, api *tg.Client, channel *tg.Channel, users <-chan channelParticipantInfo, ignoreMessages bool) []banUserInfo {
var members []banUserInfo
Expand Down

0 comments on commit 789cdc5

Please sign in to comment.