File size: 14,827 Bytes
f691796
 
 
 
 
 
 
 
 
dfca837
f691796
 
 
 
dfca837
 
 
 
 
f691796
76a45e0
 
 
 
 
 
 
 
 
 
f691796
 
dfca837
f691796
 
 
76a45e0
 
f691796
 
 
 
 
 
 
 
76a45e0
 
 
 
 
 
 
 
dfca837
 
 
 
 
 
 
76a45e0
f691796
 
 
76a45e0
 
 
f691796
76a45e0
f691796
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76a45e0
 
 
 
 
f691796
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76a45e0
 
f691796
 
dfca837
 
 
 
76a45e0
dfca837
 
 
 
 
 
 
76a45e0
f691796
 
dfca837
 
 
 
 
 
 
f691796
 
dfca837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76a45e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f691796
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76a45e0
 
 
 
 
 
 
 
 
 
 
 
 
 
f691796
 
 
 
 
 
 
 
76a45e0
 
 
f691796
 
 
 
 
 
76a45e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f691796
 
 
76a45e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f691796
76a45e0
 
f691796
 
 
 
76a45e0
 
f691796
76a45e0
f691796
76a45e0
f691796
76a45e0
 
 
 
 
 
 
 
 
 
f691796
 
76a45e0
f691796
 
 
 
 
 
 
 
dfca837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76a45e0
 
 
 
 
 
 
 
 
f691796
 
 
 
 
 
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"""Chase the group for admin rights, or leave.

Anyone can add this bot to any group. Without delete+ban rights it can neither
remove nor restrict anything — yet the old code still sent every single group
message through the AI judge, only to discover afterwards that it had no way to
act. A real incident: one unknown group produced 447 "detected spam but no
permission" lines in 24 hours, every one of them a wasted AI call. That is a
free quota-drain: add the bot to a busy group, withhold rights, watch it burn.

Four layers:
  1. No rights -> no AI judging at all (has_rights, cached) — kills the cost
  2. After being added, nag at 1/2/3/5/10 minutes, escalating in tone
  3. Still nothing at 10 minutes: explain, leave, and blocklist the chat, so a
     re-invite is refused instantly instead of restarting the whole cycle
  4. Every 6 hours, sweep every known chat and feed the rights-less ones into
     layer 2. This is what turns the first three layers from "handles new chats"
     into "handles all chats", with nobody doing anything by hand. It covers
     three cases at once: chats that predate this feature, nags lost to a
     restart (the JobQueue is in memory), and rights revoked later on.

"Couldn't find out" is not "no rights" — that is the central rule here. A
getChatMember timeout, a Telegram 5xx or a flood-wait all raise, and treating
every raise as a negative means one network blip can make the bot leave a fully
authorised group and blocklist it, irreversibly. So probe_rights is tri-state:
True / False / None, and only a confirmed False may trigger a leave.

Blocklist entries expire after BLOCK_DAYS. An attacker has to redo the whole
setup every week for a payoff of five messages, while an honest mistake heals
itself without anyone editing the database by hand. A super-admin can also clear
one instantly with /groups unblock <chat_id>.
"""

import asyncio
import logging
import time

from telegram.error import BadRequest, Forbidden

from modules.db import shared_db

logger = logging.getLogger(__name__)

# Nag schedule in seconds. The last entry leaves the group.
REMIND_AT = (60, 120, 180, 300, 600)
GIVE_UP_AFTER = REMIND_AT[-1]

# When the rights lookup itself fails: how long to wait, how many times to retry.
# No leaving happens while retrying — an unknown answer is not evidence.
RETRY_AFTER = 60
MAX_RETRIES = 3

# How long a blocklist entry stays in force.
BLOCK_DAYS = 7

# Full sweep: how often, how long after startup the first one runs, and how long
# to pause between chats. The pause matters because this is a loop over every
# chat we know — without it a large deployment would flood-limit itself.
SWEEP_INTERVAL = 6 * 3600
SWEEP_FIRST = 300
SWEEP_PACE = 0.15

# Do we hold enforcement rights in this chat: chat_id -> (has_rights, checked_at, ttl)
# Calling getChatMember per message is expensive, and rights changes arrive via
# my_chat_member anyway; the cache only covers the case where that event is
# missed, so the TTL can be generous.
# A False derived from a failed lookup is cached only briefly (_UNKNOWN_TTL) —
# otherwise one blip silences the whole group for five minutes, during which every
# message skips judging and nothing ever arrives to clear the cache.
_RIGHTS_TTL = 300
_UNKNOWN_TTL = 20
_rights_cache = {}


async def ensure_tables():
    async with shared_db() as db:
        await db.execute(
            "CREATE TABLE IF NOT EXISTS group_blocklist ("
            "chat_id INTEGER PRIMARY KEY, title TEXT, reason TEXT, "
            "blocked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"
        )
        await db.commit()


async def is_blocked(chat_id):
    try:
        async with shared_db() as db:
            cur = await db.execute(
                "SELECT 1 FROM group_blocklist WHERE chat_id=? "
                "AND blocked_at > datetime('now', ?)",
                (chat_id, "-" + str(BLOCK_DAYS) + " days")
            )
            return await cur.fetchone() is not None
    except Exception as e:
        logger.warning("blocklist lookup failed (allowing): " + str(e))
        return False


async def block(chat_id, title="", reason="no_admin_rights"):
    async with shared_db() as db:
        await db.execute(
            "INSERT OR REPLACE INTO group_blocklist (chat_id, title, reason) VALUES (?,?,?)",
            (chat_id, title or "", reason)
        )
        await db.commit()


async def unblock(chat_id):
    async with shared_db() as db:
        await db.execute("DELETE FROM group_blocklist WHERE chat_id=?", (chat_id,))
        await db.commit()
    _rights_cache.pop(chat_id, None)


def forget_rights(chat_id):
    """Invalidate the cache when rights may have changed."""
    _rights_cache.pop(chat_id, None)


def note_rights(chat_id, has_rights, ttl=_RIGHTS_TTL):
    _rights_cache[chat_id] = (bool(has_rights), time.time(), ttl)


def _has_both(member):
    return (member.status == "administrator"
            and bool(getattr(member, "can_delete_messages", False))
            and bool(getattr(member, "can_restrict_members", False)))


async def member_state(context, chat_id):
    """Where we stand in a chat: 'ok' / 'no' / 'gone' (not a member) / 'unknown'.

    'gone' has to be its own answer: _has_both is False for left/kicked too, and
    without the distinction the sweep would open a nag cycle against a chat we
    already left, fail to send, and run the whole leave path again.
    """
    try:
        me = await context.bot.get_chat_member(chat_id, context.bot.id)
    except Forbidden:
        return "gone"
    except BadRequest as e:
        if "chat not found" in str(e).lower():
            return "gone"
        logger.warning("own-rights lookup failed chat=" + str(chat_id) + ": " + str(e))
        return "unknown"
    except Exception as e:
        logger.warning("own-rights lookup failed chat=" + str(chat_id) + ": " + str(e))
        return "unknown"
    if me.status in ("left", "kicked"):
        return "gone"
    return "ok" if _has_both(me) else "no"


async def probe_rights(context, chat_id):
    """Tri-state: True (have them) / False (confirmed not) / None (couldn't tell).

    None must never collapse into False. Anything irreversible — leaving,
    blocklisting — may only act on a confirmed False. 'gone' folds into None too:
    there is nothing to leave or blocklist in a chat we are not in. Writes no
    cache; the caller decides how to record the answer.
    """
    st = await member_state(context, chat_id)
    if st == "ok":
        return True
    if st == "no":
        return False
    return None


async def has_rights(context, chat_id, use_cache=True):
    """Can we delete messages AND ban users here. Both, not either.

    For the message path: an unknown answer counts as False (better to judge
    nothing than to burn calls in a chat we were never authorised in), but it is
    cached only briefly. Use probe_rights for anything that leads to a leave.
    """
    if use_cache:
        hit = _rights_cache.get(chat_id)
        if hit and (time.time() - hit[1]) < hit[2]:
            return hit[0]
    ok = await probe_rights(context, chat_id)
    if ok is None:
        note_rights(chat_id, False, _UNKNOWN_TTL)
        return False
    note_rights(chat_id, ok)
    return ok


def _job_name(chat_id):
    return "perm_nag_" + str(chat_id)


def cancel(context, chat_id):
    """Drop any pending nags for this chat (rights granted, or we left)."""
    jq = getattr(context, "job_queue", None)
    if not jq:
        return 0
    jobs = jq.get_jobs_by_name(_job_name(chat_id))
    for j in jobs:
        j.schedule_removal()
    return len(jobs)


def schedule(context, chat_id, title=""):
    """Queue the 1/2/3/5/10 minute nags after being added to a group."""
    jq = getattr(context, "job_queue", None)
    if not jq:
        logger.warning("no job_queue, skipping rights nag for chat=" + str(chat_id))
        return False
    cancel(context, chat_id)
    for sec in REMIND_AT:
        jq.run_once(
            _nag,
            sec,
            data={"chat_id": chat_id, "title": title, "at": sec},
            name=_job_name(chat_id),
        )
    return True


def _requeue(context, chat_id, title, at, tries):
    """Couldn't read rights for this slot — redo it in a minute, advance nothing."""
    jq = getattr(context, "job_queue", None)
    if not jq:
        return False
    jq.run_once(
        _nag,
        RETRY_AFTER,
        data={"chat_id": chat_id, "title": title, "at": at, "tries": tries},
        name=_job_name(chat_id),
    )
    return True


TEXTS = {
    60:  "I don't have admin rights yet, so I can't delete ads or ban anyone.\n"
         "Tap the group name -> Administrators -> Add Admin -> pick me -> "
         "enable Delete Messages and Ban Users.",
    120: "Still no rights. Without them I can only watch — I can't help.",
    180: "Third reminder: still not authorised.",
    300: "⚠️ Without rights I will leave this group in 5 minutes.",
}
GENERIC = ("I don't have admin rights yet (Delete Messages + Ban Users), so I "
           "can't do anything here.\nTap the group name -> Administrators -> "
           "Add Admin -> pick me -> enable both.")
GOODBYE = ("No admin rights after 10 minutes, so I'm leaving.\n"
           "Without Delete Messages and Ban Users I can't do anything here, and "
           "staying would just waste resources.\n\n"
           "Grant the rights first, then invite me again whenever you like.")


def _is_fatal_send_error(e):
    """Is this "we can never speak here" or "the network hiccuped"?

    Only the former may lead to a leave. Forbidden means kicked/muted/blocked and
    is definitive; among BadRequests only the chat-is-gone family is. Everything
    else (timeouts, 5xx, flood-wait) is treated as transient so the remaining
    nags still get their chance.
    """
    if isinstance(e, Forbidden):
        return True
    if isinstance(e, BadRequest):
        t = str(e).lower()
        return ("chat not found" in t or "chat_id is empty" in t
                or "group chat was deactivated" in t or "chat was upgraded" in t)
    return False


async def _nag(context):
    d = context.job.data
    chat_id, title, at = d["chat_id"], d.get("title", ""), d["at"]
    tries = d.get("tries", 0)

    ok = await probe_rights(context, chat_id)

    if ok is None:
        # Unknown is not a negative. Never leave on this — retry the same slot.
        if tries < MAX_RETRIES:
            logger.warning("rights lookup failed, retrying in " + str(RETRY_AFTER)
                           + "s chat=" + str(chat_id) + " at=" + str(at)
                           + " tries=" + str(tries + 1))
            _requeue(context, chat_id, title, at, tries + 1)
        else:
            # Repeatedly unanswerable: either a long outage or the chat is gone.
            # Bow out quietly — no leave, no blocklist. A rights-less chat costs
            # nothing on the message path anyway.
            logger.warning("rights lookup failed " + str(MAX_RETRIES)
                           + "x, giving up on nags (not leaving) chat=" + str(chat_id))
            cancel(context, chat_id)
        return

    if ok:
        note_rights(chat_id, True)
        cancel(context, chat_id)
        logger.info("rights granted, stopping nags for chat=" + str(chat_id))
        return

    note_rights(chat_id, False)

    if at < GIVE_UP_AFTER:
        text = TEXTS.get(at, GENERIC)
        try:
            await context.bot.send_message(chat_id, text)
        except Exception as e:
            if _is_fatal_send_error(e):
                # We genuinely cannot speak here (kicked/muted) — nagging is moot.
                logger.warning("nag undeliverable chat=" + str(chat_id) + ": "
                               + str(e) + ", leaving")
                cancel(context, chat_id)
                await _leave(context, chat_id, title, "cannot_send", blocklist=False)
            else:
                # Transient — do nothing, the later slots still run.
                logger.warning("nag send failed (transient) chat=" + str(chat_id)
                               + ": " + str(e))
        return

    # Final slot: confirmed no rights, so leave and blocklist.
    try:
        await context.bot.send_message(chat_id, GOODBYE)
    except Exception:
        pass
    cancel(context, chat_id)
    await _leave(context, chat_id, title, "no_admin_rights")


async def _known_groups():
    async with shared_db() as db:
        cur = await db.execute("SELECT chat_id, title FROM groups")
        return await cur.fetchall()


async def sweep(context):
    """Walk every known chat and start the nag cycle wherever we hold no rights.

    Layers 2 and 3 only attach at the moment we are added, so they miss chats
    that predate the feature and nags that a restart dropped (the JobQueue lives
    in memory). This is the rule that closes both — nobody has to leave a group
    by hand.
    """
    jq = getattr(context, "job_queue", None)
    try:
        rows = await _known_groups()
    except Exception as e:
        logger.warning("rights sweep could not read the chat list: " + str(e))
        return
    stat = {"ok": 0, "no": 0, "gone": 0, "unknown": 0, "busy": 0}
    for chat_id, title in rows:
        # Already being nagged: leave it alone, or every sweep would reset the
        # countdown to zero and the chat would never actually be left.
        if jq and jq.get_jobs_by_name(_job_name(chat_id)):
            stat["busy"] += 1
            continue
        st = await member_state(context, chat_id)
        stat[st] += 1
        if st == "ok":
            note_rights(chat_id, True)
        elif st == "no":
            note_rights(chat_id, False)
            schedule(context, chat_id, title or "")
        await asyncio.sleep(SWEEP_PACE)
    logger.info("rights sweep done: total=" + str(len(rows)) + " ok=" + str(stat["ok"])
                + " nag_started=" + str(stat["no"]) + " already_nagging=" + str(stat["busy"])
                + " gone=" + str(stat["gone"]) + " unknown=" + str(stat["unknown"]))


async def _leave(context, chat_id, title, reason, blocklist=True):
    """Leave. blocklist=False is for "they kicked or muted us" — that was their
    call, and holding it against the chat would block a re-invite they are
    perfectly entitled to make."""
    if blocklist:
        try:
            await block(chat_id, title, reason)
        except Exception as e:
            logger.warning("blocklist write failed chat=" + str(chat_id) + ": " + str(e))
    try:
        await context.bot.leave_chat(chat_id)
        logger.info("left unauthorised chat=" + str(chat_id) + " reason=" + reason)
    except Exception as e:
        logger.warning("leave failed chat=" + str(chat_id) + ": " + str(e))
    _rights_cache.pop(chat_id, None)