def quickban(bot, update): query = update.callback_query i18n(update).loads.install(True) callback = callabck_parse.callback_parse(query.data) user_id, msg_id = callback.qact.split(':') tags = callback.qdata if sage.michael(query.from_user.id) == False and sage.lucifer( query.from_user.id) == False: query.answer('權限不夠。') return if sage.in_shield(int(user_id)): text = query.message.text_html + '\n被保護ㄉ狀態。' query.edit_message_text(text, parse_mode='html') return try: sent = update.message.forward(config.getint('log', 'evidence')) except BaseException: sent = 2 days = druation([tags]) if days != 0: until = int( (datetime.now(taiwan_country) + timedelta(days=days)).timestamp()) else: until = 0 excalibur(bot, update, int(user_id), [tags], query.from_user.id, until=until, reason=tags) text = query.message.text_html + \ f'\n{"="*23}' + \ _(f'\n處刑人:{query.from_user.mention_html()}\n') + \ _(f'標籤:<code>{tags}</code>') query.edit_message_text(text, parse_mode='html')
def extend_links(bot, update, inherit, cmd=None): # tinyurl, t.cn, bit.ly return False locales.i18n(update).loads.install(True) pattern = r'(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)(tinyurl\.com|bit\.ly|t\.cn)(\/[a-z0-9]+)' result = re.findall(pattern, unescape(update.message.text_html), re.IGNORECASE) if len(result) == 0: return False extract_result = checker_result() extract_result.tags = [] extract_result.name = '' for x in result: url = ''.join(x) # binance_shortcut.ico req = requests.get(url, allow_redirects=False) try: real_url = req.headers['Location'] except Exception as e: logger.exception(e) ## Do All Checker ## for checker in check_list: result_ = checker(bot, update, real_url) if result_.name: extract_result.name += result_.name extract_result.tags.extend(result_.tags) else: return ## Do All Checker ## if sage.in_shield(update.message.from_user.id): return if is_participate_white(bot, update): return if is_admin(bot, update): return if extract_result.tags == []: logger.info('return with null tags') return day = druation(list(dict.fromkeys(extract_result.tags))) tags = list(dict.fromkeys(extract_result.tags)) mongo = db_tools.use_mongo() query_group = mongo.group.find_one({'chat.id': update.message.chat.id}) if query_group: group = db_parse.group() group.parse(query_group) else: return True if bool(set(group.config.sub_ban_list).intersection(list(to_emoji(tags)))): pass else: return evidence = 2 try: fwd = update.message.forward(config.getint('log', 'evidence')) except BadRequest as e: pass else: evidence = fwd.message_id if day == 0: until = 0 else: until = (datetime.now(taiwan_country) + timedelta(days=day)).timestamp() excalibur(bot, update, update.message.from_user.id, tags, bot.id, until=until, reason=f'hex auto.{extract_result.name}', evidence=evidence) right = False try: update.message.delete() except BaseException: right = True try: bot.restrict_chat_member(update.message.chat.id, update.message.from_user.id) except BaseException: right = True text = _('偵測到 <code>{checker}</code> 垃圾訊息\n').format( checker=extract_result.name) if right: text += _('⚠️權限不足無法操作⚠️\n') if extract_result.tags: text += _('依照契約內容已吞噬 <code>Sammer</code>\n') text += _('名字:{full_name}\n' + 'UID:<code>{user_id}</code>\n' + '標籤:<code>{tags}</code>').format( full_name=update.message.from_user.mention_html(), user_id=update.message.from_user.id, tags=', '.join(tags)) sent = update.message.reply_html(text).result() time.sleep(10) sent.delete() homicide(bot, update, update.message.from_user.id)
def xtext(bot, update): locales.i18n(update).loads.install(True) # Filters photo, documents if Filters.text(update.message): text = update.message.text_html else: text = update.message.caption_html if sage.in_shield(update.message.from_user.id): return if is_participate_white(bot, update): return if is_admin(bot, update): return cache = redis.lrange('xtext_filter', 0, -1) if len(cache) == 0: xfilter_refresh() cache = redis.lrange('xtext_filter', 0, -1) extend = extend_links(bot, update, text).result() excute = excute_() if not extend: for rule in cache: checker = re.findall(rule.decode(), text) if checker: query_xtext = mongo.xtext_filter.find_one( {'rule': rule.decode()}) rule_result = xtext_rules(query_xtext) if rule_result.match <= len(checker): try: evidence = update.message.forward( config.getint('log', 'evidence')).message_id except BaseException: evidence = 2 else: send_text = f'Sender: {update.message.from_user.mention_html()}\n' +\ f'ID: {update.message.from_user.id}' bot.send_message( config.getint('log', 'evidence'), send_text, reply_to_message_id=send_text.message_id) excute.parse(rule_result) else: return False else: return if excute.name == '': return day = druation(list(dict.fromkeys(excute.tags))) tags = list(list(dict.fromkeys(excute.tags))) action = to_emoji(list(dict.fromkeys(excute.action))) query_group = mongo.group.find_one({'chat.id': update.message.chat.id}) if query_group: group = db_parse.group() group.parse(query_group) if bool( set(group.config.sub_ban_list).intersection( list(to_emoji(tags)))): pass else: return else: return right = False if 'delete' in action or tags: try: update.message.delete() except BadRequest as e: if e.message == "Message can't be deleted": right = True if 'restrict' in action: try: bot.restrict_chat_member(update.message.chat.id, update.message.from_user.id) except BadRequest as e: if e.message == 'Not enough rights to restrict/unrestrict chat member': right = True if 'alert' in action: try: pass except BadRequest as e: text = f'#error #{__name__}\n' f'message: {e.message}\n' bot.send_message(config.getint('log', 'error')) text = _('偵測到 <code>{rule}</code> 規則漢堡罐頭\n'.format(rule=excute.name)) if right: text += _('⚠️權限不足無法操作⚠️\n') if excute.tags: text += _('依照契約內容已吞噬 Sammer\n') text += _('名字:{full_name}\n' + 'UID:<code>{user_id}</code>\n' + '標籤:<code>{tags}</code>').format( full_name=update.message.from_user.mention_html(), user_id=update.message.from_user.id, tags=', '.join(tags)) if day == 0: until = 0 else: until = (datetime.now(taiwan_country) + timedelta(days=day)).timestamp() excalibur(bot, update, update.message.from_user.id, tags, bot.id, until=until, reason=f'hex auto.{excute.name}', evidence=evidence) sent = update.message.reply_html(text).result() time.sleep(10) sent.delete() homicide(bot, update, update.message.from_user.id)
def hexban_long(bot, update): i18n(update).loads.install(True) if sage.is_sage(update.message.from_user.id) == False: try: update.message.delete() except BaseException: return sent = update.message.reply_text('騷年,你渴望力量ㄇ?').result() time.sleep(5) sent.delete() return if sage.elf(update.message.from_user.id): update.message.reply_text(_('等級不夠 🌚')) return # !hex ban u=123 t=spam day = search('ban={:d}d', update.message.text) reason = search('r={:S}', update.message.text) tags = search('t={:S}', update.message.text) uid = search('u={:d}', update.message.text) now = datetime.now(taiwan_country) if tags is None: update.message.reply_html(_('缺少 <code>標籤</code> 參數。')) return elif uid is None: update.message.reply_html(_('缺少 <code>UID</code> 參數。')) return if uid: uid = uid[0] if tags: tags = tags[0].split(',') if 'spam' not in tags: tags.append('spam') for tag in tags: if tag not in emojitags().emoji_dict.keys(): text = _(f'找不到 <code>{tag}</code> 標籤') update.message.reply_html(text) return if day: if day[0] == 0: until = 0 else: until = int((now + timedelta(days=day[0])).timestamp()) else: day_ = druation(tags) if day_ == 0: until = 0 else: until = int((now + timedelta(days=day_)).timestamp()) if sage.in_shield(uid): if sage.is_sage(uid): text = '精靈保護 🌚' else: text = '白色恐怖ㄉ力量 🌝' update.message.reply_text(text) return until_text = datetime.fromtimestamp(until).astimezone(taiwan_country) opid = update.message.from_user.id tags_text = ', '.join(tags) evidence = 2 opid = update.message.from_user.id if reason: reason = reason[0] else: reason = tags_text query_user = mongo.user.find_one({'chat.id': uid}) excalibur(bot, update, uid=uid, tags=tags, opid=opid, until=until, reason=reason) text = '' if query_user: user = db_parse.user() user.parse(query_user) if user.fullname: if user.id < 0: text += _(f'頻道:<code>{user.fullname}</code>\n') else: text += _(f'名字:<code>{user.fullname}</code>\n') text += _(f'UID:<code>{uid}</code>\n') + \ _(f'標籤:<code>{tags_text}</code>\n') + \ _(f'證據:https://t.me/hexevidence/{evidence}\n') if until == 0: text += _(banyourwords.forever.format(reason=reason)) else: text += _(banyourwords.temp.format(reason=reason, date=until_text)) update.message.reply_html(text, web_page_preview=False)
def hexban_reply(bot, update): ''' !hex ban ads,scam !hex ban u=123 t=scam r=ㄏㄏ ''' i18n(update).loads.install(True) if sage.is_sage(update.message.from_user.id) == False: try: update.message.delete() except BaseException: return sent = update.message.reply_text('騷年,你渴望力量ㄇ?').result() time.sleep(5) sent.delete() if sage.elf(update.message.from_user.id): update.message.reply_text(_('等級不夠 🌚')) return args = update.message.text.split() if len(args) == 3: # !hex ban ads,scam tags = args[2].split(',') for tag in tags: if tag not in emojitags().emoji_dict.keys(): text = _(f'找不到 <code>{tag}</code> 標籤') update.message.reply_html(text) return if 'spam' not in tags: tags.append('spam') day = druation(tags) now = datetime.now(taiwan_country) if day == 0: until = 0 else: until = int((now + timedelta(days=day)).timestamp()) tags_text = ', '.join(tags) opid = update.message.from_user.id bang = [] if sage.in_shield( update.message.reply_to_message.from_user.id) == False: bang = [update.message.reply_to_message.from_user] if update.message.reply_to_message.forward_from: if sage.in_shield( update.message.reply_to_message.forward_from.id) == False: bang.append(update.message.reply_to_message.forward_from) if update.message.reply_to_message.forward_from_chat: if sage.in_shield( update.message.reply_to_message.forward_from_chat.id) == False: bang.append(update.message.reply_to_message.forward_from_chat) if bang: try: sent = bot.forward_message( config.getint( 'log', 'evidence'), update.message.reply_to_message.chat.id, update.message.reply_to_message.message_id) except BaseException: sent = None if sent: evidence = sent.message_id else: evidence = 2 else: text = '' if sage.in_shield(update.message.reply_to_message.from_user.id): if sage.is_sage(update.message.reply_to_message.from_user.id): text += _(f'<code>{update.message.reply_to_message.from_user.id}</code> 有精靈保護 🌚') else: text += _( f'<code>{update.message.reply_to_message.from_user.id}</code> 白色恐怖快逃R 🌚') if update.message.reply_to_message.forward_from: if sage.in_shield( update.message.reply_to_message.forward_from.id): if sage.is_sage( update.message.reply_to_message.forward_from.id): text += _( f'<code>{update.message.reply_to_message.forward_from.id}</code> 有精靈保護 🌚') else: text += _( f'<code>{update.message.reply_to_message.from_user.id}</code> 白色恐怖快逃R 🌚') if update.message.reply_to_message.forward_from_chat: if sage.in_shield( update.message.reply_to_message.forward_from_chat.id): if sage.is_sage( update.message.reply_to_message.forward_from_chat.id): text += _( f'<code>{update.message.reply_to_message.forward_from_chat.id}</code> 有精靈保護 🌚') else: text += _( f'<code>{update.message.reply_to_message.from_user.id}</code> 白色恐怖快逃R 🌚') update.message.reply_html(text) return if Filters.sticker(update.message.reply_to_message): if update.message.reply_to_message.sticker.set_name: update_sticker = { '$set': { 'sticker': { 'set_name': update.message.reply_to_message.sticker.set_name, 'tags': tags}, 'opid': update.message.from_user.id, 'reason': tags_text, 'evidence': evidence}} mongo.sticker.find_one_and_update( {'sticker.id': update.message.reply_to_message.sticker.file_id}, update_sticker, upsert=True) text = f'ID:<code>{update.message.reply_to_message.sticker.file_id}</code>\n' if update.message.reply_to_message.sticker.set_name: # https://t.me/addstickers/nichijou text += f'Set Name:https://t.me/addstickers/{update.message.reply_to_message.sticker.set_name}\n' text += _(f'標籤:<code>{tags_text}</code>') update.message.reply_html(text) sticker_judge.refresh() if Filters.photo(update.message.reply_to_message): file = bytes( update.message.reply_to_message.photo[-1].get_file().download_as_bytearray()) bio = io.BytesIO(file) i = imagehash.hashing(bio) hashing = i.phash() query_photo = mongo.xmedia.find_one({'photo.hash': hashing}) if query_photo: pass else: tmp_dict = {'photo': {'hash': hashing, 'tags': to_emoji(tags), 'indexing': i.indexing()}, 'evidence': evidence, 'opid': update.message.from_user.id, 'reason': tags_text} mongo.xmedia.insert_one(tmp_dict) text = f'hash:<code>{hashing}</code>\n' + \ _(f'標籤:<code>{tags_text}</code>\n') + \ _(f'標記:<code>{i.indexing()}</code>') update.message.reply_html(text) redis.lpush('photo_cache', hashing) for chat in bang: text = excalibur( bot, update, uid=chat.id, tags=tags, opid=opid, until=until, reason=tags_text, user=chat, evidence=evidence) update.message.reply_html(text, web_page_preview=False) update_user = {'$set': {'chat': chat.to_dict()}} mongo.user.find_one_and_update({'chat.id': chat.id}, update_user) homicide(bot, update, chat.id) # !hex ban t=ads r=臭 elif len(args) >= 4: day = search('ban={:d}d', update.message.text) reason = search('r={:S}', update.message.text) tags = search('t={:S}', update.message.text) uid = search('u={:d}', update.message.text) now = datetime.now(taiwan_country) if tags is None: text = f'缺少標籤參數。' update.message.reply_html(text) return else: tags = tags[0].split(',') if 'spam' not in tags: tags.append('spam') for tag in tags: if tag not in emojitags().emoji_dict.keys(): text = f'找不到 <code>{tag}</code> 標籤' update.message.reply_html(text) return if day: if day[0] == 0: until = 0 else: until = int((now + timedelta(days=day[0])).timestamp()) else: day_ = druation(tags) if day_ == 0: until = 0 else: until = int((now + timedelta(days=day_)).timestamp()) tags_text = ', '.join(tags) opid = update.message.from_user.id if reason: reason = reason[0] else: reason = tags_text if uid: uid = uid[0] try: sent = bot.forward_message( config.getint( 'log', 'evidence'), update.message.reply_to_message.chat.id, update.message.reply_to_message.message_id) except BaseException: sent = None if sent: evidence = sent.message_id else: evidence = 2 text = excalibur( bot, update, uid=uid, tags=tags, opid=opid, until=until, reason=reason, evidence=evidence) update.message.reply_html(text) return if sage.in_shield(update.message.reply_to_message.from_user.id): bang = [] else: bang = [update.message.reply_to_message.from_user] if update.message.reply_to_message.forward_from: if sage.in_shield( update.message.reply_to_message.forward_from.id) == False: bang.append(update.message.reply_to_message.forward_from) if update.message.reply_to_message.forward_from_chat: if sage.in_shield( update.message.reply_to_message.forward_from_chat.id) == False: bang.append(update.message.reply_to_message.forward_from_chat) if bang: try: sent = bot.forward_message( config.getint( 'log', 'evidence'), update.message.reply_to_message.chat.id, update.message.reply_to_message.message_id) except BaseException: sent = None if sent: evidence = sent.message_id else: evidence = 2 else: text = '' if update.message.reply_to_message.forward_from: if sage.is_sage( update.message.reply_to_message.forward_from.id): text += f'<code>{update.message.reply_to_message.forward_from.id}</code> 有精靈護體 🌚' else: text += f'<code>{update.message.reply_to_message.forward_from.id}</code> 白色恐怖快逃RRR 🌝' if update.message.reply_to_message.forward_from_chat: if sage.is_sage( update.message.reply_to_message.forward_from_chat): text += f'<code>{update.message.reply_to_message.forward_from_chat.id}</code> 有精靈護體 🌚' else: text += f'<code>{update.message.reply_to_message.forward_from_chat.id}</code> 白色恐怖快逃RRR 🌝' update.message.reply_html(text) if Filters.sticker(update.message.reply_to_message): if update.message.reply_to_message.sticker.set_name: update_sticker = { '$set': { 'sticker': { 'set_name': update.message.reply_to_message.sticker.set_name, 'tags': tags}, 'opid': update.message.from_user.id, 'reason': tags_text, 'evidence': evidence}} mongo.sticker.find_one_and_update( {'sticker.id': update.message.reply_to_message.sticker.file_id}, update_sticker, upsert=True) text = f'ID:<code>{update.message.reply_to_message.sticker.file_id}</code>\n' if update.message.reply_to_message.sticker.set_name: # https://t.me/addstickers/nichijou text += f'Set Name:https://t.me/addstickers/{update.message.reply_to_message.sticker.set_name}\n' text += _(f'標籤:<code>{tags_text}</code>') update.message.reply_html(text) sticker_judge.refresh() for chat in bang: update_user = {'$set': {'chat': chat.to_dict()}} mongo.user.find_one_and_update({'chat.id': chat.id}, update_user) text = excalibur( bot, update, uid=chat.id, opid=opid, until=until, reason=reason, tags=tags, user=chat, evidence=evidence) update.message.reply_html(text, web_page_preview=False)
def ban_sticker(bot, update): i18n(update).loads.install(True) if update.message.forward_from: chat = update.message.forward_from elif update.message.forward_from_chat: chat = update.message.forward_from_chat else: chat = update.message.from_user if update.message.sticker.set_name is None: return if sage.in_shield(update.message.from_user.id): return group = db_parse.group() query_group = mongo.group.find_one({'chat.id': update.message.chat.id}) group.parse(query_group) if group.white_participate: if update.message.from_user.id in group.white_participate: return if is_admin(bot, update, specfic=(update.message.chat.id, update.message.from_user.id)): return ban = sticker_judge.checker(bot, update, update.message.sticker.set_name) if ban: try: evidence = update.message.forward(config.getint('log', 'evidence')) except BaseException: evidence = 2 if update.message.sticker.set_name: query_stiker = mongo.sticker.find_one( {'sticker.set_name': update.message.sticker.set_name}) if query_stiker: sticker = db_parse.sticker() sticker.parse(query_stiker) query_user = mongo.user.find_one( {'chat.id': update.message.from_user.id}) user = db_parse.user() user.parse(query_user) if sticker.day == 0: until = 0 else: until = int((datetime.now(taiwan_country) + timedelta(days=sticker.day)).timestamp()) # check group sub with sticker ban check = bool( set(group.config.sub_ban_list).intersection(sticker.tags_list)) logger.debug( f'sticker compare with group sub is {check}, should ban') if not check: return if until == 0: pass ''' 當 A 子在別的地方得到 mark as spam (ads), 在 B 群沒有訂閱 (ads) 而有訂閱 p**n, 而 A 在貼了 mark as p**n 的貼圖。 A 子會被踢出去,並且記錄在 banned_participate, 但不更新 ban.current ''' single_ban = True if user.current and user.current.until != 0: single_ban = False update.message.from_user.fullname = update.message.from_user.full_name if evidence == 2: evidence_ = sticker.evidence else: evidence_ = evidence.message_id excalibur(bot, update, update.message.from_user.id, sticker.tags, sticker.opid, until=until, reason=sticker.reason, evidence=evidence_, user=update.message.from_user) announce_ban = _( announce(update.message.from_user.id, sticker.tags, sticker.opid, until=until, reason=sticker.reason, evidence=evidence_, query_user=update.message.from_user)) else: announce_ban = _( '名字:{fullname}\n' '傳送了 <code>{sticker}</code> 已被標記為 <code>{tags}</code> 的貼圖。' ).format(fullname=user.mention_html, sticker=sticker.set_name, tags=sticker.tags_text) try: update.message.delete() except BaseException: pass try: bot.restrict_chat_member(update.message.chat.id, update.message.from_user.id) except BadRequest as e: if e.message == 'User is an administrator of the chat': return elif e.message == 'Not enough rights to restrict/unrestrict chat member': text = _('⚠️權限不足⚠️\n') + announce_ban update.message.reply_html(text) return else: sent = update.message.reply_html(announce_ban).result() time.sleep(10) try: sent.delete() except BaseException: pass homicide(bot, update, update.message.from_user.id)