Import blocked domains from csv

merge-requests/30/head
Bob Mottram 2023-02-10 12:02:10 +00:00
parent d863eb25e4
commit 50c47b89f8
29 changed files with 216 additions and 26 deletions

View File

@ -1460,3 +1460,101 @@ def get_cw_list_variable(list_name: str) -> str:
"""Returns the variable associated with a CW list
"""
return 'list' + list_name.replace(' ', '').replace("'", '')
def import_blocks(base_dir: str, nickname: str, domain: str,
filename: str) -> bool:
"""Imports blocked domains for a given account
"""
lines = []
try:
with open(filename, 'r', encoding='utf-8') as fp_blocks:
lines = fp_blocks.read().splitlines()
except OSError:
print('EX: unable to import blocked instances from file ' +
filename)
if not lines:
return False
if len(lines) < 2:
return False
if not lines[0].startswith('#domain,#') or \
'comment' not in lines[0]:
return False
fieldnames = lines[0].split(',')
comment_field_index = 0
for field_str in fieldnames:
if 'comment' in field_str:
break
comment_field_index += 1
if comment_field_index >= len(fieldnames):
return False
account_directory = acct_dir(base_dir, nickname, domain)
blocking_filename = \
account_directory + '/blocking.txt'
blocking_reasons_filename = \
account_directory + '/blocking_reasons.txt'
existing_lines = []
if os.path.isfile(blocking_filename):
try:
with open(blocking_filename, 'r', encoding='utf-8') as fp_blocks:
existing_lines = fp_blocks.read().splitlines()
except OSError:
print('EX: ' +
'unable to import existing blocked instances from file ' +
blocking_filename)
existing_reasons = []
if os.path.isfile(blocking_reasons_filename):
try:
with open(blocking_reasons_filename,
'r', encoding='utf-8') as fp_blocks:
existing_reasons = fp_blocks.read().splitlines()
except OSError:
print('EX: ' +
'unable to import existing ' +
'blocked instance reasons from file ' +
blocking_reasons_filename)
append_blocks = []
append_reasons = []
for line_str in lines:
if line_str.startswith('#'):
continue
block_fields = line_str.split(',')
blocked_domain_name = block_fields[0].strip()
if ' ' in blocked_domain_name or \
'.' not in blocked_domain_name:
continue
if blocked_domain_name in existing_lines:
# already blocked
continue
append_blocks.append(blocked_domain_name)
blocked_comment = block_fields[comment_field_index].strip()
if blocked_comment:
if blocked_comment not in existing_reasons:
append_reasons.append(blocked_domain_name + ' ' +
blocked_comment)
if not append_blocks:
return True
try:
with open(blocking_filename, 'a+', encoding='utf-8') as fp_blocks:
for new_block in append_blocks:
fp_blocks.write(new_block + '\n')
except OSError:
print('EX: ' +
'unable to append imported blocks to ' +
blocking_filename)
try:
with open(blocking_reasons_filename, 'a+',
encoding='utf-8') as fp_blocks:
for new_reason in append_reasons:
fp_blocks.write(new_reason + '\n')
except OSError:
print('EX: ' +
'unable to append imported block reasons to ' +
blocking_reasons_filename)
return True

View File

@ -147,6 +147,7 @@ from media import replace_twitter
from media import attach_media
from media import path_is_video
from media import path_is_audio
from blocking import import_blocks
from blocking import add_account_blocks
from blocking import get_cw_list_variable
from blocking import load_cw_lists
@ -6172,6 +6173,7 @@ class PubServer(BaseHTTPRequestHandler):
'instanceLogo',
'left_col_image', 'right_col_image',
'submitImportFollows',
'submitImportBlocks',
'submitImportTheme'
)
profile_media_types_uploaded = {}
@ -6220,6 +6222,10 @@ class PubServer(BaseHTTPRequestHandler):
filename_base = \
acct_dir(base_dir, nickname, domain) + \
'/import_following.csv'
elif m_type == 'submitImportBlocks':
filename_base = \
acct_dir(base_dir, nickname, domain) + \
'/import_blocks.csv'
else:
filename_base = \
acct_dir(base_dir, nickname, domain) + \
@ -6244,6 +6250,25 @@ class PubServer(BaseHTTPRequestHandler):
nickname)
continue
if m_type == 'submitImportBlocks':
if os.path.isfile(filename_base):
blocks_import_succeeded = False
if import_blocks(base_dir, nickname, domain,
filename_base):
print(nickname + ' imported blocks csv')
blocks_import_succeeded = True
try:
os.remove(filename_base)
except OSError:
print('EX: ' +
'unable to remove imported blocks file ' +
filename_base)
if blocks_import_succeeded:
continue
print('WARN: failed to import blocks from csv for ' +
nickname)
continue
if m_type == 'submitImportTheme':
if nickname == admin_nickname or \
is_artist(base_dir, nickname):

View File

@ -617,5 +617,7 @@
"Buy link": "رابط شراء",
"Buy links are allowed from the following domains": "روابط الشراء مسموح بها من المجالات التالية",
"Media license": "رخصة وسائل الإعلام",
"Media creator": "صانع الوسائط"
"Media creator": "صانع الوسائط",
"Import Blocks": "استيراد مثيلات محظورة",
"Export Blocks": "تصدير المثيلات المحظورة"
}

View File

@ -617,5 +617,7 @@
"Buy link": "সংযোগ কেনা",
"Buy links are allowed from the following domains": "নিম্নলিখিত ডোমেনগুলি থেকে লিঙ্কগুলি কেনার অনুমতি দেওয়া হয়েছে",
"Media license": "মিডিয়া লাইসেন্স",
"Media creator": "মিডিয়া নির্মাতা"
"Media creator": "মিডিয়া নির্মাতা",
"Import Blocks": "অবরুদ্ধ দৃষ্টান্ত আমদানি করুন",
"Export Blocks": "অবরুদ্ধ দৃষ্টান্ত রপ্তানি করুন"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Enllaç de compra",
"Buy links are allowed from the following domains": "Els enllaços de compra es permeten des dels dominis següents",
"Media license": "Llicència de mitjans",
"Media creator": "Creador de mitjans"
"Media creator": "Creador de mitjans",
"Import Blocks": "Importa instàncies bloquejades",
"Export Blocks": "Exporta instàncies bloquejades"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Prynu dolen",
"Buy links are allowed from the following domains": "Caniateir dolenni prynu o'r parthau canlynol",
"Media license": "Trwydded cyfryngau",
"Media creator": "Crëwr cyfryngau"
"Media creator": "Crëwr cyfryngau",
"Import Blocks": "Mewnforio Achosion wedi'u Rhwystro",
"Export Blocks": "Allforio Achosion wedi'u Rhwystro"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Link kaufen",
"Buy links are allowed from the following domains": "Kauflinks sind von den folgenden Domains erlaubt",
"Media license": "Medienlizenz",
"Media creator": "Mediengestalter"
"Media creator": "Mediengestalter",
"Import Blocks": "Blockierte Instanzen importieren",
"Export Blocks": "Blockierte Instanzen exportieren"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Σύνδεσμος αγοράς",
"Buy links are allowed from the following domains": "Οι σύνδεσμοι αγοράς επιτρέπονται από τους παρακάτω τομείς",
"Media license": "Άδεια ΜΜΕ",
"Media creator": "Δημιουργός πολυμέσων"
"Media creator": "Δημιουργός πολυμέσων",
"Import Blocks": "Εισαγωγή αποκλεισμένων παρουσιών",
"Export Blocks": "Εξαγωγή αποκλεισμένων παρουσιών"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Buy link",
"Buy links are allowed from the following domains": "Buy links are allowed from the following domains",
"Media license": "Media license",
"Media creator": "Media creator"
"Media creator": "Media creator",
"Import Blocks": "Import Blocks",
"Export Blocks": "Export Blocks"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Enlace de compra",
"Buy links are allowed from the following domains": "Se permiten enlaces de compra de los siguientes dominios",
"Media license": "Licencia de medios",
"Media creator": "Creadora de medios"
"Media creator": "Creadora de medios",
"Import Blocks": "Importar instancias bloqueadas",
"Export Blocks": "Exportar instancias bloqueadas"
}

View File

@ -617,5 +617,7 @@
"Buy link": "لینک خرید",
"Buy links are allowed from the following domains": "لینک خرید از دامنه های زیر مجاز است",
"Media license": "مجوز رسانه",
"Media creator": "سازنده رسانه"
"Media creator": "سازنده رسانه",
"Import Blocks": "وارد کردن موارد مسدود شده",
"Export Blocks": "نمونه های مسدود شده را صادر کنید"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Acheter un lien",
"Buy links are allowed from the following domains": "Les liens d'achat sont autorisés à partir des domaines suivants",
"Media license": "Licence média",
"Media creator": "Créateur de médias"
"Media creator": "Créateur de médias",
"Import Blocks": "Importer des instances bloquées",
"Export Blocks": "Exporter les instances bloquées"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Ceannaigh nasc",
"Buy links are allowed from the following domains": "Ceadaítear naisc cheannaigh ó na fearainn seo a leanas",
"Media license": "Ceadúnas meáin",
"Media creator": "Cruthaitheoir meáin"
"Media creator": "Cruthaitheoir meáin",
"Import Blocks": "Iompórtáil Cásanna Blocáilte",
"Export Blocks": "Easpórtáil Cásanna Blocáilte"
}

View File

@ -617,5 +617,7 @@
"Buy link": "लिंक खरीदें",
"Buy links are allowed from the following domains": "निम्नलिखित डोमेन से खरीदें लिंक की अनुमति है",
"Media license": "मीडिया लाइसेंस",
"Media creator": "मीडिया निर्माता"
"Media creator": "मीडिया निर्माता",
"Import Blocks": "अवरोधित उदाहरण आयात करें",
"Export Blocks": "निर्यात अवरुद्ध उदाहरण"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Link per l'acquisto",
"Buy links are allowed from the following domains": "I link di acquisto sono consentiti dai seguenti domini",
"Media license": "Licenza multimediale",
"Media creator": "Creatore multimediale"
"Media creator": "Creatore multimediale",
"Import Blocks": "Importa istanze bloccate",
"Export Blocks": "Esporta istanze bloccate"
}

View File

@ -617,5 +617,7 @@
"Buy link": "購入リンク",
"Buy links are allowed from the following domains": "次のドメインからの購入リンクが許可されています",
"Media license": "メディアライセンス",
"Media creator": "メディアクリエーター"
"Media creator": "メディアクリエーター",
"Import Blocks": "ブロックされたインスタンスのインポート",
"Export Blocks": "ブロックされたインスタンスのエクスポート"
}

View File

@ -617,5 +617,7 @@
"Buy link": "구매 링크",
"Buy links are allowed from the following domains": "다음 도메인에서 구매 링크가 허용됩니다.",
"Media license": "미디어 라이센스",
"Media creator": "미디어 크리에이터"
"Media creator": "미디어 크리에이터",
"Import Blocks": "차단된 인스턴스 가져오기",
"Export Blocks": "차단된 인스턴스 내보내기"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Girêdanê bikirin",
"Buy links are allowed from the following domains": "Zencîreyên kirînê ji domên jêrîn têne destûr kirin",
"Media license": "Lîsansa medyayê",
"Media creator": "Afirînerê medyayê"
"Media creator": "Afirînerê medyayê",
"Import Blocks": "Mînakên Astengkirî Import",
"Export Blocks": "Mînakên Astengkirî Export"
}

View File

@ -617,5 +617,7 @@
"Buy link": "koop link",
"Buy links are allowed from the following domains": "Kooplinks zijn toegestaan vanaf de volgende domeinen",
"Media license": "Media licentie",
"Media creator": "Media-maker"
"Media creator": "Media-maker",
"Import Blocks": "Importeer geblokkeerde instanties",
"Export Blocks": "Exporteer geblokkeerde instanties"
}

View File

@ -613,5 +613,7 @@
"Buy link": "Buy link",
"Buy links are allowed from the following domains": "Buy links are allowed from the following domains",
"Media license": "Media license",
"Media creator": "Media creator"
"Media creator": "Media creator",
"Import Blocks": "Import Blocks",
"Export Blocks": "Export Blocks"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Kup Link",
"Buy links are allowed from the following domains": "Kupuj linki są dozwolone z następujących domen",
"Media license": "Licencja medialna",
"Media creator": "Kreator mediów"
"Media creator": "Kreator mediów",
"Import Blocks": "Importuj zablokowane instancje",
"Export Blocks": "Eksportuj zablokowane instancje"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Link de compra",
"Buy links are allowed from the following domains": "Links de compra são permitidos nos seguintes domínios",
"Media license": "Licença de mídia",
"Media creator": "Criador de mídia"
"Media creator": "Criador de mídia",
"Import Blocks": "Importar instâncias bloqueadas",
"Export Blocks": "Exportar instâncias bloqueadas"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Купить ссылку",
"Buy links are allowed from the following domains": "Ссылки на покупку разрешены со следующих доменов",
"Media license": "Медиа лицензия",
"Media creator": "Создатель медиа"
"Media creator": "Создатель медиа",
"Import Blocks": "Импорт заблокированных экземпляров",
"Export Blocks": "Экспорт заблокированных экземпляров"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Nunua kiungo",
"Buy links are allowed from the following domains": "Viungo vya kununua vinaruhusiwa kutoka kwa vikoa vifuatavyo",
"Media license": "Leseni ya media",
"Media creator": "Muundaji wa media"
"Media creator": "Muundaji wa media",
"Import Blocks": "Ingiza Matukio Yaliyozuiwa",
"Export Blocks": "Hamisha Matukio Yaliyozuiwa"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Bağlantı satın al",
"Buy links are allowed from the following domains": "Aşağıdaki alanlardan satın alma bağlantılarına izin verilir",
"Media license": "Medya lisansı",
"Media creator": "Medya yaratıcısı"
"Media creator": "Medya yaratıcısı",
"Import Blocks": "Engellenen Örnekleri İçe Aktar",
"Export Blocks": "Engellenen Örnekleri Dışa Aktar"
}

View File

@ -617,5 +617,7 @@
"Buy link": "Купити посилання",
"Buy links are allowed from the following domains": "Посилання на купівлю дозволено з таких доменів",
"Media license": "Медіа ліцензія",
"Media creator": "Творець медіа"
"Media creator": "Творець медіа",
"Import Blocks": "Імпортувати заблоковані екземпляри",
"Export Blocks": "Експортувати заблоковані екземпляри"
}

View File

@ -617,5 +617,7 @@
"Buy link": "קויפן לינק",
"Buy links are allowed from the following domains": "קויפן פֿאַרבינדונגען זענען ערלויבט פֿון די פאלגענדע דאָומיינז",
"Media license": "מעדיע דערלויבעניש",
"Media creator": "מעדיע באשעפער"
"Media creator": "מעדיע באשעפער",
"Import Blocks": "ימפּאָרט בלאַקט ינסטאַנסיז",
"Export Blocks": "עקספּאָרט בלאַקט ינסטאַנסיז"
}

View File

@ -617,5 +617,7 @@
"Buy link": "购买链接",
"Buy links are allowed from the following domains": "允许来自以下域的购买链接",
"Media license": "媒体许可证",
"Media creator": "媒体创作者"
"Media creator": "媒体创作者",
"Import Blocks": "导入被阻止的实例",
"Export Blocks": "导出被阻止的实例"
}

View File

@ -92,6 +92,7 @@ from roles import is_devops
from session import site_is_verified
THEME_FORMATS = '.zip, .gz'
BLOCKFILE_FORMATS = '.csv'
def _valid_profile_preview_post(post_json_object: {},
@ -2043,6 +2044,20 @@ def _html_edit_profile_filtering(base_dir: str, nickname: str, domain: str,
edit_text_area(translate['Blocked accounts'], None, 'blocked',
blocked_str, 200, '', False)
# import and export blocks
edit_profile_form += \
' <label class="labels">' + \
translate['Import Blocks'] + '</label>\n'
edit_profile_form += ' <input type="file" id="import_blocks" '
edit_profile_form += 'name="submitImportBlocks" '
edit_profile_form += 'accept="' + BLOCKFILE_FORMATS + '">\n'
edit_profile_form += \
' <label class="labels">' + \
translate['Export Blocks'] + '</label><br>\n'
edit_profile_form += \
' <button type="submit" class="button" ' + \
'name="submitExportBlocks">➤</button><br>\n'
idx = 'Direct messages are always allowed from these instances.'
edit_profile_form += \
edit_text_area(translate['Direct Message permitted instances'], None,