-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobber.py
More file actions
1362 lines (1234 loc) · 58.5 KB
/
Copy pathRobber.py
File metadata and controls
1362 lines (1234 loc) · 58.5 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# CopyLeft 2026 github.com/i-execute
# Author: I_execute.t.me
# Licensed under AGPLv3.
__version__ = (2, 0, 0)
# meta developer: Execute_forge.t.me
# meta banner: https://raw.githubusercontent.com/i-execute/Modules/main/Storage/Robber/MetaBanner.jpeg
import logging
import asyncio
import os
import re
import tempfile
import shutil
import time
import zipfile
import io
import base64
import ipaddress
import struct
import sqlite3
from telethon import TelegramClient
from telethon.sessions import StringSession
from telethon.tl import functions, types
from telethon.tl.types import (
MessageMediaWebPage,
MessageMediaContact,
MessageMediaGeo,
MessageMediaGeoLive,
MessageMediaPoll,
)
from telethon.errors import FloodWaitError, AuthKeyUnregisteredError, UserDeactivatedBanError
from .. import loader, utils
from ..inline.types import InlineCall
logger = logging.getLogger(__name__)
STORY_LINK_RE = re.compile(r"t\.me/([^/]+)/s/(\d+)")
POST_LINK_RE = re.compile(r"t\.me/(?:c/)?([^/]+)/(\d+)")
STRING_SESSION_PATTERN = re.compile(r"1[A-Za-z0-9_-]{200,}={0,2}")
HEX_KEY_PATTERN = re.compile(r"[0-9a-fA-F]{512}")
SKIP_MEDIA_TYPES = (
MessageMediaContact,
MessageMediaGeo,
MessageMediaGeoLive,
MessageMediaPoll,
)
BATCH_SIZE = 100
FLOOD_EXTRA = 5
PAUSE_EVERY = 100
PAUSE_DURATION = 5
MAX_ACCOUNTS = 5
DC_IP_MAP = {
1: "149.154.175.53",
2: "149.154.167.51",
3: "149.154.175.100",
4: "149.154.167.91",
5: "91.108.56.130",
}
def _safe_disconnect(client):
if client:
try:
asyncio.ensure_future(client.disconnect())
except Exception:
pass
@loader.tds
class Robber(loader.Module):
"""Fully steal channels and stories, support for private channels and multi-account"""
strings = {
"name": "Robber",
"main_menu": "<b>Robber - Channel & Content Stealer</b>\nSelect operation:",
"account_select": "<b>Choose account to work with:</b>",
"connected_select": "<b>Select connected account:</b>",
"btn_double": "Copy Channel",
"btn_steal": "Steal Post",
"btn_sdl": "Download Story",
"btn_back": "Back to Menu",
"btn_close": "Close",
"btn_owner": "Owner",
"btn_connected": "Connected",
"connect_menu": "<b>Connect Session</b>\nDetected: <code>{type}</code>\nConnect this session?",
"connect_select_dc": "<b>Select DC for HEX session:</b>",
"connecting": "<b>Connecting session...</b>",
"connect_ok": "<b>Connected!</b>\nSlot: <code>{slot}</code>\nUser: {user}\nID: <code>{uid}</code>",
"connect_fail_invalid": "<b>Error:</b> Session is invalid or not authorized",
"connect_fail_banned": "<b>Error:</b> Account is banned",
"connect_fail_revoked": "<b>Error:</b> Session revoked",
"connect_fail_error": "<b>Error:</b> <code>{err}</code>",
"connect_slots_full": "<b>Error:</b> Max 5 accounts connected",
"slot_info": "Slot {slot} | {name} | ID: {uid}",
"double_start": "<b>Starting copy...</b>\nFetching messages...",
"double_progress": "<b>Copying...</b>\nProcessed: {done} / {total}\nSkipped: {skipped}\nAlbums: {albums}",
"double_done": "<b>Copy complete!</b>\nProcessed: {done}\nSkipped: {skipped}\nAlbums: {albums}\nLink: {link}",
"double_no_id": "<b>Error:</b> Provide channel ID or username",
"double_error": "<b>Error:</b> <code>{err}</code>",
"double_send_error": "<b>Copying...</b>\nProcessed: {done} / {total}\nSkipped: {skipped}\nAlbums: {albums}\n<b>Send error on msg {msg_id}:</b>\n<code>{err}</code>",
"input_channel": "Enter channel ID or username:",
"input_post": "Enter post link (e.g., https://t.me/c/3872609933/8):",
"input_story": "Enter username, user ID or story link (e.g., https://t.me/username/s/53):",
"steal_bad_link": "<b>Error:</b> Cannot parse post link",
"steal_not_found": "<b>Error:</b> Post not found",
"steal_error": "<b>Error:</b> <code>{err}</code>",
"steal_processing": "<b>Stealing post...</b>",
"steal_done": "<b>Post stolen successfully!</b>",
"sdl_bad_input": "<b>Error:</b> Cannot parse input",
"sdl_not_found": "<b>Error:</b> Story not found or has no media",
"sdl_download_fail": "<b>Error:</b> Download failed",
"sdl_processing": "<b>Downloading story...</b>",
"sdl_done": "<b>Story downloaded successfully!</b>",
"sdl_select_mode": "<b>Select download mode:</b>\nUser: {user}",
"sdl_select_export": "<b>Select export format:</b>",
"btn_sdl_one": "One Story (by link)",
"btn_sdl_all": "All Stories",
"btn_sdl_export_one": "Send one by one",
"btn_sdl_export_zip": "ZIP archive",
"sdl_progress_dl": "<b>Downloading stories...</b>\n{done} / {total} downloaded",
"sdl_progress_zip": "<b>Creating ZIP archive...</b>\n{done} / {total} files",
"sdl_progress_upload": "<b>Uploading to Telegram...</b>\n{cur:.1f} / {total:.1f} MB",
"sdl_done_all": "<b>Done!</b>\n{count} stories sent",
"sdl_done_zip": "<b>Done!</b>\nArchive sent: {name}",
"sdl_input_story_link": "Enter story link:",
"sdl_no_stories": "<b>No stories found</b>",
"btn_dc_1": "DC 1",
"btn_dc_2": "DC 2",
"btn_dc_3": "DC 3",
"btn_dc_4": "DC 4",
"btn_dc_5": "DC 5",
}
strings_ru = {
"main_menu": "<b>Robber - Копировщик каналов и контента</b>\nВыберите операцию:",
"account_select": "<b>Выберите аккаунт для работы:</b>",
"connected_select": "<b>Выберите подключённый аккаунт:</b>",
"btn_double": "Копировать канал",
"btn_steal": "Украсть пост",
"btn_sdl": "Скачать историю",
"btn_back": "Назад в меню",
"btn_close": "Закрыть",
"btn_owner": "Владелец",
"btn_connected": "Подключённые",
"connect_menu": "<b>Подключение сессии</b>\nОбнаружено: <code>{type}</code>\nПодключить эту сессию?",
"connect_select_dc": "<b>Выберите DC для HEX сессии:</b>",
"connecting": "<b>Подключение сессии...</b>",
"connect_ok": "<b>Подключено!</b>\nСлот: <code>{slot}</code>\nПользователь: {user}\nID: <code>{uid}</code>",
"connect_fail_invalid": "<b>Ошибка:</b> Сессия невалидна или не авторизована",
"connect_fail_banned": "<b>Ошибка:</b> Аккаунт заблокирован",
"connect_fail_revoked": "<b>Ошибка:</b> Сессия отозвана",
"connect_fail_error": "<b>Ошибка:</b> <code>{err}</code>",
"connect_slots_full": "<b>Ошибка:</b> Максимум 5 аккаунтов подключено",
"slot_info": "Слот {slot} | {name} | ID: {uid}",
"double_start": "<b>Начинаю копирование...</b>\nПолучаю сообщения...",
"double_progress": "<b>Копирую...</b>\nОбработано: {done} / {total}\nПропущено: {skipped}\nАльбомы: {albums}",
"double_done": "<b>Копирование завершено!</b>\nОбработано: {done}\nПропущено: {skipped}\nАльбомы: {albums}\nСсылка: {link}",
"double_no_id": "<b>Ошибка:</b> Укажите ID или юзернейм канала",
"double_error": "<b>Ошибка:</b> <code>{err}</code>",
"double_send_error": "<b>Копирую...</b>\nОбработано: {done} / {total}\nПропущено: {skipped}\nАльбомы: {albums}\n<b>Ошибка отправки сообщения {msg_id}:</b>\n<code>{err}</code>",
"input_channel": "Введите ID или юзернейм канала:",
"input_post": "Введите ссылку на пост (например, https://t.me/c/3872609933/8):",
"input_story": "Введите юзернейм, ID пользователя или ссылку на историю:",
"steal_bad_link": "<b>Ошибка:</b> Не могу распарсить ссылку на пост",
"steal_not_found": "<b>Ошибка:</b> Пост не найден",
"steal_error": "<b>Ошибка:</b> <code>{err}</code>",
"steal_processing": "<b>Крадем пост...</b>",
"steal_done": "<b>Пост успешно украден!</b>",
"sdl_bad_input": "<b>Ошибка:</b> Не могу распарсить ввод",
"sdl_not_found": "<b>Ошибка:</b> История не найдена или без медиа",
"sdl_download_fail": "<b>Ошибка:</b> Не удалось скачать",
"sdl_processing": "<b>Скачиваю историю...</b>",
"sdl_done": "<b>История успешно скачана!</b>",
"sdl_select_mode": "<b>Выберите режим скачивания:</b>\nПользователь: {user}",
"sdl_select_export": "<b>Выберите формат экспорта:</b>",
"btn_sdl_one": "Одна история (по ссылке)",
"btn_sdl_all": "Все истории",
"btn_sdl_export_one": "Отправить по одной",
"btn_sdl_export_zip": "ZIP архив",
"sdl_progress_dl": "<b>Скачиваю истории...</b>\n{done} / {total} скачано",
"sdl_progress_zip": "<b>Создаю ZIP архив...</b>\n{done} / {total} файлов",
"sdl_progress_upload": "<b>Выгружаю в Telegram...</b>\n{cur:.1f} / {total:.1f} МБ",
"sdl_done_all": "<b>Готово!</b>\n{count} историй отправлено",
"sdl_done_zip": "<b>Готово!</b>\nАрхив отправлен: {name}",
"sdl_input_story_link": "Введите ссылку на историю:",
"sdl_no_stories": "<b>Историй не найдено</b>",
"btn_dc_1": "DC 1",
"btn_dc_2": "DC 2",
"btn_dc_3": "DC 3",
"btn_dc_4": "DC 4",
"btn_dc_5": "DC 5",
}
def __init__(self):
self.config = loader.ModuleConfig(
loader.ConfigValue(
"CREATE_CHANNEL",
True,
"If True creates channel, if False creates group",
validator=loader.validators.Boolean(),
),
loader.ConfigValue(
"API_ID",
2040,
"Telegram API ID for session connections",
validator=loader.validators.Integer(),
),
loader.ConfigValue(
"API_HASH",
"b18441a1ff607e10a989891a5462e627",
"Telegram API Hash",
validator=loader.validators.Hidden(),
),
)
self._temp_dir = None
self._client = None
self._db = None
self._pending_connect = {}
async def client_ready(self, client, db):
self._client = client
self._db = db
self._temp_dir = os.path.join(tempfile.gettempdir(), "robber_tmp")
os.makedirs(self._temp_dir, exist_ok=True)
async def on_unload(self):
self._wipe_temp()
slots = self._load_slots()
for slot_data in slots.values():
client = slot_data.get("_client")
if client:
_safe_disconnect(client)
def _wipe_temp(self):
if self._temp_dir and os.path.exists(self._temp_dir):
try:
shutil.rmtree(self._temp_dir)
except Exception:
pass
def _tmp_path(self, name):
return os.path.join(self._temp_dir, name)
def _load_slots(self) -> dict:
raw = self._db.get("Robber", "slots", {})
return raw if isinstance(raw, dict) else {}
def _save_slots(self, slots: dict):
save = {k: {sk: sv for sk, sv in v.items() if sk != "_client"} for k, v in slots.items()}
self._db.set("Robber", "slots", save)
def _next_slot(self, slots: dict) -> int | None:
for i in range(1, MAX_ACCOUNTS + 1):
if str(i) not in slots:
return i
return None
def _find_string_session(self, text):
if not text:
return None
m = STRING_SESSION_PATTERN.search(text)
return m.group(0) if m else None
def _find_hex_key(self, text):
if not text:
return None
m = HEX_KEY_PATTERN.search(text)
return m.group(0) if m else None
def _parse_string_session(self, s):
try:
s = s.strip()
if not s.startswith("1"):
return None
raw = s[1:]
raw += "=" * (-len(raw) % 4)
data = base64.urlsafe_b64decode(raw)
if len(data) == 263:
dc_id, ip_b, port, auth_key = struct.unpack(">B4sH256s", data)
ip = str(ipaddress.IPv4Address(ip_b))
elif len(data) == 275:
dc_id, ip_b, port, auth_key = struct.unpack(">B16sH256s", data)
ip = str(ipaddress.IPv6Address(ip_b))
else:
return None
return {"dc_id": dc_id, "ip": ip, "port": port, "auth_key": auth_key}
except Exception:
return None
def _build_string_session(self, dc_id, auth_key):
try:
if dc_id not in DC_IP_MAP:
return None
if isinstance(auth_key, str):
auth_key = auth_key.encode("latin-1")
if len(auth_key) != 256:
return None
ip = ipaddress.IPv4Address(DC_IP_MAP[dc_id])
data = struct.pack(">B4sH256s", dc_id, ip.packed, 443, auth_key)
return "1" + base64.urlsafe_b64encode(data).decode()
except Exception:
return None
async def _read_session_file(self, path):
try:
conn = sqlite3.connect(path)
cur = conn.cursor()
cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='sessions'")
if not cur.fetchone():
conn.close()
return None
cur.execute("SELECT dc_id, auth_key FROM sessions LIMIT 1")
row = cur.fetchone()
conn.close()
if row:
dc_id, auth_key = row
if isinstance(auth_key, str):
auth_key = auth_key.encode("latin-1")
if not auth_key or len(auth_key) != 256:
return None
return {"dc_id": dc_id, "auth_key": auth_key}
return None
except Exception:
return None
async def _connect_and_test(self, string_session: str):
client = TelegramClient(
StringSession(string_session),
int(self.config["API_ID"]),
self.config["API_HASH"],
device_model="RobberModule",
system_version="By @I_execute",
app_version=f"v{'.'.join(map(str, __version__))}",
)
await asyncio.wait_for(client.connect(), timeout=15)
me = await asyncio.wait_for(client.get_me(), timeout=10)
return client, me
async def _get_slot_client(self, slot: str):
slots = self._load_slots()
data = slots.get(slot)
if not data:
return None
if data.get("_client"):
return data["_client"]
string_session = data.get("string")
if not string_session:
return None
try:
client, me = await self._connect_and_test(string_session)
slots[slot]["_client"] = client
return client
except Exception:
return None
def _get_working_client(self, slot: str | None):
if slot is None:
return self._client
slots = self._load_slots()
data = slots.get(slot)
if data and data.get("_client"):
return data["_client"]
return self._client
async def _safe(self, coro, client=None):
c = client or self._client
while True:
try:
return await coro
except FloodWaitError as e:
await asyncio.sleep(e.seconds + FLOOD_EXTRA)
except Exception:
raise
async def _is_forum_chat(self, message):
if getattr(message, "is_private", False):
return False
try:
chat = await message.get_chat()
return getattr(chat, "forum", False)
except Exception:
return False
def _get_topic_id(self, message):
reply_to = getattr(message, "reply_to", None)
if reply_to:
top_id = getattr(reply_to, "reply_to_top_id", None)
if top_id:
return top_id
msg_id = getattr(reply_to, "reply_to_msg_id", None)
if msg_id and getattr(reply_to, "forum_topic", False):
return msg_id
return None
def _get_html(self, msg):
if not msg.message:
return None
try:
if msg.entities:
from telethon.extensions import html
return html.unparse(msg.message, msg.entities)
except Exception:
pass
return msg.message
async def _dl(self, media, fname, client=None):
c = client or self._client
path = self._tmp_path(fname)
try:
result = await c.download_media(media, file=path)
if result and os.path.exists(result):
return result
except Exception as e:
logger.warning(f"[Robber] dl failed: {e}")
return None
async def _create_target(self, title, about, client=None):
c = client or self._client
as_channel = self.config["CREATE_CHANNEL"]
result = await self._safe(
c(functions.channels.CreateChannelRequest(
title=title, about=about,
broadcast=as_channel, megagroup=not as_channel,
)),
client=c,
)
return result.chats[0]
async def _set_avatar(self, channel, src_entity, client=None):
c = client or self._client
try:
av_path = self._tmp_path(f"av_{channel.id}")
downloaded = await c.download_profile_photo(src_entity, file=av_path)
if downloaded and os.path.exists(downloaded):
try:
photo = await self._safe(c.upload_file(downloaded), client=c)
await self._safe(
c(functions.channels.EditPhotoRequest(
channel=channel,
photo=types.InputChatUploadedPhoto(file=photo),
)),
client=c,
)
finally:
try:
os.remove(downloaded)
except Exception:
pass
except Exception as e:
logger.warning(f"[Robber] avatar failed: {e}")
async def _send_msg(self, target, msg, id_map, client=None):
c = client or self._client
if msg.action is not None:
return None
media = msg.media
text = self._get_html(msg)
reply_to = None
if msg.reply_to and hasattr(msg.reply_to, "reply_to_msg_id"):
reply_to = id_map.get(msg.reply_to.reply_to_msg_id)
if media and isinstance(media, SKIP_MEDIA_TYPES):
label = type(media).__name__.replace("MessageMedia", "")
fallback = f"[{label}]"
if text:
fallback = f"{text}\n\n[{label}]"
return await self._safe(c.send_message(target, fallback, reply_to=reply_to), client=c)
if media and isinstance(media, MessageMediaWebPage):
media = None
if media is not None:
fname = f"media_{msg.id}"
path = await self._dl(media, fname, client=c)
if path:
try:
return await self._safe(
c.send_file(target, path, caption=text, parse_mode="html", reply_to=reply_to, force_document=False),
client=c,
)
finally:
try:
os.remove(path)
except Exception:
pass
else:
if text:
return await self._safe(c.send_message(target, text, parse_mode="html", reply_to=reply_to), client=c)
return None
if text:
return await self._safe(c.send_message(target, text, parse_mode="html", reply_to=reply_to), client=c)
return None
async def _send_album(self, target, album_msgs, id_map, client=None):
c = client or self._client
first = album_msgs[0]
reply_to = None
if first.reply_to and hasattr(first.reply_to, "reply_to_msg_id"):
reply_to = id_map.get(first.reply_to.reply_to_msg_id)
paths = []
captions = []
try:
for i, am in enumerate(album_msgs):
if am.media and not isinstance(am.media, (MessageMediaWebPage,) + SKIP_MEDIA_TYPES):
path = await self._dl(am.media, f"album_{am.id}_{i}", client=c)
if path:
paths.append(path)
captions.append(self._get_html(am) or "")
if not paths:
return []
sent = await self._safe(
c.send_file(target, paths, caption=captions, parse_mode="html", reply_to=reply_to),
client=c,
)
if not isinstance(sent, list):
sent = [sent]
return sent
finally:
for p in paths:
try:
os.remove(p)
except Exception:
pass
async def _resolve_post_link(self, link, client=None):
c = client or self._client
m = POST_LINK_RE.search(link)
if not m:
return None, None
chat_part = m.group(1)
msg_id = int(m.group(2))
try:
if chat_part.isdigit():
entity = await c.get_entity(int(f"-100{chat_part}"))
else:
entity = await c.get_entity(chat_part)
except Exception as e:
raise Exception(f"Cannot resolve chat: {e}")
return entity, msg_id
async def _fetch_album(self, entity, anchor_msg, client=None):
c = client or self._client
if not anchor_msg.grouped_id:
return [anchor_msg]
group_id = anchor_msg.grouped_id
album = []
async for msg in c.iter_messages(entity, min_id=anchor_msg.id - 20, max_id=anchor_msg.id + 20):
if msg.grouped_id == group_id:
album.append(msg)
if not album:
album = [anchor_msg]
album.sort(key=lambda m: m.id)
return album
async def _resolve_story_user(self, inp: str, client=None):
c = client or self._client
inp = inp.strip()
m = STORY_LINK_RE.search(inp)
if m:
username = m.group(1)
story_id = int(m.group(2))
peer = (await c(functions.contacts.ResolveUsernameRequest(username))).peer
return peer, story_id
if inp.lstrip("-").isdigit():
uid = int(inp)
try:
entity = await c.get_entity(uid)
peer = await c.get_input_entity(entity)
return peer, None
except Exception as e:
raise Exception(f"Cannot resolve user ID: {e}")
try:
peer = (await c(functions.contacts.ResolveUsernameRequest(inp.lstrip("@")))).peer
return peer, None
except Exception as e:
raise Exception(f"Cannot resolve username: {e}")
async def _fetch_all_stories(self, peer, client=None):
c = client or self._client
seen_ids = set()
stories = []
try:
result = await c(functions.stories.GetPeerStoriesRequest(peer=peer))
for s in result.stories.stories:
if s.id not in seen_ids:
seen_ids.add(s.id)
stories.append(s)
except Exception as e:
logger.warning(f"[Robber] GetPeerStoriesRequest failed: {e}")
offset_id = 0
while True:
try:
result = await c(functions.stories.GetPinnedStoriesRequest(
peer=peer, offset_id=offset_id, limit=100
))
if not result.stories:
break
for s in result.stories:
if s.id not in seen_ids:
seen_ids.add(s.id)
stories.append(s)
if len(result.stories) < 100:
break
offset_id = result.stories[-1].id
except Exception as e:
logger.warning(f"[Robber] GetPinnedStoriesRequest failed: {e}")
break
return stories
async def _download_story_media(self, story, idx: int, client=None):
c = client or self._client
media = getattr(story, "media", None)
if not media:
return None
is_video = isinstance(media, types.MessageMediaDocument)
ext = ".mp4" if is_video else ".jpg"
fname = f"story_{idx}{ext}"
path = self._tmp_path(fname)
if os.path.exists(path):
os.remove(path)
try:
downloaded = await c.download_media(media, file=path)
if downloaded and os.path.exists(downloaded):
return downloaded
except Exception as e:
logger.warning(f"[Robber] story dl failed: {e}")
return None
def _get_main_markup(self, slot: str | None = None):
return [
[{"text": self.strings["btn_double"], "callback": self._cb_double_menu, "args": (slot,), "style": "primary"}],
[{"text": self.strings["btn_steal"], "callback": self._cb_steal_menu, "args": (slot,), "style": "primary"}],
[{"text": self.strings["btn_sdl"], "callback": self._cb_sdl_menu, "args": (slot,), "style": "primary"}],
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}],
]
def _slot_label(self, slot: str, data: dict) -> str:
name = data.get("name", "?")
uid = data.get("uid", "?")
return f"{slot}. {name} | {uid}"
def _get_account_select_markup(self, slots: dict):
connected_btn = [{"text": self.strings["btn_connected"], "callback": self._cb_connected_select, "style": "success"}]
markup = [
[{"text": self.strings["btn_owner"], "callback": self._cb_owner_selected, "style": "primary"}],
connected_btn,
[{"text": self.strings["btn_close"], "callback": self._cb_close, "style": "danger"}],
]
return markup
def _get_connected_markup(self, slots: dict):
markup = []
styles = ["primary", "success", "primary", "success", "primary"]
for i, (slot, data) in enumerate(slots.items()):
label = self._slot_label(slot, data)
markup.append([{
"text": label,
"callback": self._cb_slot_selected,
"args": (slot,),
"style": styles[i % len(styles)],
}])
markup.append([{"text": self.strings["btn_back"], "callback": self._cb_account_select, "style": "danger"}])
return markup
async def _cb_account_select(self, call: InlineCall):
slots = self._load_slots()
if not slots:
await call.edit(self.strings["main_menu"], reply_markup=self._get_main_markup(None))
return
await call.edit(self.strings["account_select"], reply_markup=self._get_account_select_markup(slots))
async def _cb_owner_selected(self, call: InlineCall):
await call.edit(self.strings["main_menu"], reply_markup=self._get_main_markup(None))
async def _cb_connected_select(self, call: InlineCall):
slots = self._load_slots()
if not slots:
await call.edit(self.strings["main_menu"], reply_markup=self._get_main_markup(None))
return
await call.edit(self.strings["connected_select"], reply_markup=self._get_connected_markup(slots))
async def _cb_slot_selected(self, call: InlineCall, slot: str):
slots = self._load_slots()
data = slots.get(slot, {})
name = data.get("name", "?")
uid = data.get("uid", "?")
text = f"{self.strings['main_menu']}\n<blockquote>Account: {name} | ID: <code>{uid}</code></blockquote>"
await call.edit(text, reply_markup=self._get_main_markup(slot))
async def _cb_connect_string(self, call: InlineCall, string_session: str):
await call.edit(self.strings["connecting"])
slots = self._load_slots()
slot = self._next_slot(slots)
if slot is None:
await call.edit(self.strings["connect_slots_full"])
return
try:
client, me = await self._connect_and_test(string_session)
if me is None:
_safe_disconnect(client)
await call.edit(self.strings["connect_fail_invalid"])
return
first = getattr(me, "first_name", "") or ""
last = getattr(me, "last_name", "") or ""
name = f"{first} {last}".strip() or "Unknown"
slots[str(slot)] = {
"string": string_session,
"name": name,
"uid": me.id,
"_client": client,
}
self._save_slots(slots)
await call.edit(self.strings["connect_ok"].format(slot=slot, user=name, uid=me.id))
except AuthKeyUnregisteredError:
await call.edit(self.strings["connect_fail_revoked"])
except UserDeactivatedBanError:
await call.edit(self.strings["connect_fail_banned"])
except Exception as e:
await call.edit(self.strings["connect_fail_error"].format(err=str(e)))
async def _cb_connect_hex_dc(self, call: InlineCall, hex_key: str, dc_id: int):
auth_key = bytes.fromhex(hex_key)
string_session = self._build_string_session(dc_id, auth_key)
if not string_session:
await call.edit(self.strings["connect_fail_invalid"])
return
await self._cb_connect_string(call, string_session)
async def _cb_connect_hex_select_dc(self, call: InlineCall, hex_key: str):
await call.edit(
self.strings["connect_select_dc"],
reply_markup=[
[
{"text": self.strings["btn_dc_1"], "callback": self._cb_connect_hex_dc, "args": (hex_key, 1), "style": "primary"},
{"text": self.strings["btn_dc_2"], "callback": self._cb_connect_hex_dc, "args": (hex_key, 2), "style": "primary"},
{"text": self.strings["btn_dc_3"], "callback": self._cb_connect_hex_dc, "args": (hex_key, 3), "style": "primary"},
],
[
{"text": self.strings["btn_dc_4"], "callback": self._cb_connect_hex_dc, "args": (hex_key, 4), "style": "primary"},
{"text": self.strings["btn_dc_5"], "callback": self._cb_connect_hex_dc, "args": (hex_key, 5), "style": "primary"},
],
],
)
async def _cb_main_menu(self, call: InlineCall, slot: str | None = None):
slots = self._load_slots()
if slots:
await call.edit(self.strings["account_select"], reply_markup=self._get_account_select_markup(slots))
else:
await call.edit(self.strings["main_menu"], reply_markup=self._get_main_markup(None))
async def _cb_double_menu(self, call: InlineCall, slot: str | None = None):
await call.edit(
self.strings["main_menu"],
reply_markup=[
[{"text": self.strings["btn_double"], "input": self.strings["input_channel"], "handler": self._cb_double_execute, "args": (slot,), "style": "success"}],
[{"text": self.strings["btn_back"], "callback": self._cb_main_menu, "args": (slot,), "style": "danger"}],
],
)
async def _cb_steal_menu(self, call: InlineCall, slot: str | None = None):
await call.edit(
self.strings["main_menu"],
reply_markup=[
[{"text": self.strings["btn_steal"], "input": self.strings["input_post"], "handler": self._cb_steal_execute, "args": (slot,), "style": "success"}],
[{"text": self.strings["btn_back"], "callback": self._cb_main_menu, "args": (slot,), "style": "danger"}],
],
)
async def _cb_sdl_menu(self, call: InlineCall, slot: str | None = None):
await call.edit(
self.strings["main_menu"],
reply_markup=[
[{"text": self.strings["btn_sdl"], "input": self.strings["input_story"], "handler": self._cb_sdl_input, "args": (slot,), "style": "success"}],
[{"text": self.strings["btn_back"], "callback": self._cb_main_menu, "args": (slot,), "style": "danger"}],
],
)
async def _cb_double_execute(self, call: InlineCall, channel_input: str, slot: str | None = None):
channel_input = channel_input.strip()
if not channel_input:
await call.answer(self.strings["double_no_id"], show_alert=True)
return
client = await self._get_slot_client(slot) if slot else self._client
if client is None:
client = self._client
await call.edit(self.strings["double_start"])
try:
try:
src = await client.get_entity(int(channel_input))
except ValueError:
src = await client.get_entity(channel_input)
src_title = getattr(src, "title", "Copied")
src_about = ""
try:
full = await client(functions.channels.GetFullChannelRequest(src))
src_about = full.full_chat.about or ""
except Exception:
pass
new_channel = await self._create_target(src_title, src_about, client=client)
await self._set_avatar(new_channel, src, client=client)
new_entity = await client.get_entity(new_channel.id)
all_ids = []
async for msg in client.iter_messages(src, reverse=True):
all_ids.append(msg.id)
total = len(all_ids)
done = 0
skipped = 0
albums = 0
id_map = {}
last_edit_time = 0
edit_interval = 3
async def _update_progress():
nonlocal last_edit_time
now = time.time()
if now - last_edit_time >= edit_interval:
last_edit_time = now
await call.edit(
self.strings["double_progress"].format(
done=done, total=total, skipped=skipped, albums=albums
)
)
i = 0
while i < len(all_ids):
batch_ids = all_ids[i: i + BATCH_SIZE]
msgs = await self._safe(client.get_messages(src, ids=batch_ids), client=client)
msgs = [m for m in msgs if m is not None]
msgs.sort(key=lambda m: m.id)
j = 0
while j < len(msgs):
msg = msgs[j]
if msg.grouped_id:
group_id = msg.grouped_id
album = [msg]
k = j + 1
while k < len(msgs) and msgs[k].grouped_id == group_id:
album.append(msgs[k])
k += 1
try:
sent_list = await self._send_album(new_entity, album, id_map, client=client)
if sent_list:
for orig, sent in zip(album, sent_list):
id_map[orig.id] = sent.id
albums += 1
done += len(album)
else:
skipped += len(album)
done += len(album)
except Exception as e:
skipped += len(album)
done += len(album)
await call.edit(
self.strings["double_send_error"].format(
done=done, total=total, skipped=skipped,
albums=albums, msg_id=msg.id, err=str(e),
)
)
await asyncio.sleep(2)
j = k
else:
try:
sent = await self._send_msg(new_entity, msg, id_map, client=client)
if sent:
id_map[msg.id] = sent.id
done += 1
else:
if msg.action is None:
skipped += 1
done += 1
except Exception as e:
skipped += 1
done += 1
await call.edit(
self.strings["double_send_error"].format(
done=done, total=total, skipped=skipped,
albums=albums, msg_id=msg.id, err=str(e),
)
)
await asyncio.sleep(2)
j += 1
await _update_progress()
if done > 0 and done % PAUSE_EVERY == 0:
await asyncio.sleep(PAUSE_DURATION)
i += BATCH_SIZE
try:
uname = getattr(new_entity, "username", None)
link = f"@{uname}" if uname else f"<code>-100{new_channel.id}</code>"
except Exception:
link = f"<code>-100{new_channel.id}</code>"
await call.edit(
self.strings["double_done"].format(done=done, skipped=skipped, albums=albums, link=link),
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_main_menu, "args": (slot,), "style": "danger"}]],
)
except Exception as e:
logger.error(f"[Robber] double error: {e}", exc_info=True)
await call.edit(
self.strings["double_error"].format(err=str(e)),
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_main_menu, "args": (slot,), "style": "danger"}]],
)
async def _cb_steal_execute(self, call: InlineCall, post_link: str, slot: str | None = None):
post_link = post_link.strip()
if not POST_LINK_RE.search(post_link):
await call.answer(self.strings["steal_bad_link"], show_alert=True)
return
client = await self._get_slot_client(slot) if slot else self._client
if client is None:
client = self._client
await call.edit(self.strings["steal_processing"])
try:
entity, msg_id = await self._resolve_post_link(post_link, client=client)
anchor = await client.get_messages(entity, ids=[msg_id])
anchor = anchor[0] if anchor else None
if not anchor:
await call.edit(
self.strings["steal_not_found"],
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_main_menu, "args": (slot,), "style": "danger"}]],
)
return
target_chat = call.form["chat"]
if anchor.grouped_id:
album = await self._fetch_album(entity, anchor, client=client)
await self._send_album(target_chat, album, {}, client=client)
else:
await self._send_msg(target_chat, anchor, {}, client=client)
await call.edit(
self.strings["steal_done"],
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_main_menu, "args": (slot,), "style": "danger"}]],
)
except Exception as e:
logger.error(f"[Robber] steal error: {e}", exc_info=True)
await call.edit(
self.strings["steal_error"].format(err=str(e)),
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_main_menu, "args": (slot,), "style": "danger"}]],
)
async def _cb_sdl_input(self, call: InlineCall, user_input: str, slot: str | None = None):
user_input = user_input.strip()
client = await self._get_slot_client(slot) if slot else self._client
if client is None:
client = self._client
await call.edit(self.strings["sdl_processing"])
try:
peer, story_id = await self._resolve_story_user(user_input, client=client)
except Exception as e:
await call.edit(
self.strings["sdl_bad_input"],
reply_markup=[[{"text": self.strings["btn_back"], "callback": self._cb_main_menu, "args": (slot,), "style": "danger"}]],
)
return
try:
entity = await client.get_entity(peer)
first = getattr(entity, "first_name", "") or getattr(entity, "title", "") or ""
last = getattr(entity, "last_name", "") or ""
user_label = f"{first} {last}".strip() or str(getattr(entity, "id", "?"))
except Exception:
user_label = "?"
if story_id is not None:
await call.edit(
self.strings["sdl_select_export"],
reply_markup=[
[{"text": self.strings["btn_sdl_export_one"], "callback": self._cb_sdl_one_by_one, "args": (peer, [story_id], slot), "style": "primary"}],
[{"text": self.strings["btn_sdl_export_zip"], "callback": self._cb_sdl_zip, "args": (peer, [story_id], slot), "style": "success"}],
[{"text": self.strings["btn_back"], "callback": self._cb_main_menu, "args": (slot,), "style": "danger"}],
],
)
else:
await call.edit(
self.strings["sdl_select_mode"].format(user=user_label),