added sub-commands for whitelist management

This commit is contained in:
Izalia Mae 2019-04-10 18:05:40 -04:00
parent 813ff3f883
commit 02dfd6f2d7
2 changed files with 123 additions and 50 deletions

170
app.py
View file

@ -1,66 +1,138 @@
#!/usr/bin/env python3
import os, sys, getpass
###
### ReqAccept v0.2
### by Zoey Mae (@izaliamae@barkshark.tk)
###
import os, sys, getpass, json
from mastodon import Mastodon
import configparser as config
def login(app_name, app_site, scopes, ini, gui = False, domain = None, username = None, password = None):
confDir = os.environ['HOME'] + '/.config/' + app_name.replace(" ", "")
confFile = confDir + '/' + ini + '.ini'
app_name = 'Req Accept'
ini = 'config'
if not os.path.exists(confFile):
cfgInit = config.ConfigParser()
cfgInit['login'] = {'domain': '', 'token': ''}
whitelist = ['chomp.life', 'barkshark.tk', 'yiff.life', 'snouts.online']
cfgInit['settings'] = {'whitelist': whitelist}
confDir = os.environ['HOME'] + '/.config/' + app_name.replace(" ", "")
confFile = confDir + '/' + ini + '.ini'
try:
domain = os.environ['domain']
username = os.environ['username']
password = os.environ['password']
except KeyError:
if gui == False:
domain = input('Domain: ')
username = input('E-Mail: ')
password = getpass.getpass('Pass: ')
if not os.path.exists(confFile):
cfgInit = config.ConfigParser()
cfgInit['login'] = {'domain': '', 'token': ''}
whitelistInit = ["chomp.life", "barkshark.tk", "yiff.life", "snouts.online"]
cfgInit['settings'] = {'whitelist': whitelistInit}
try:
id = Mastodon.create_app(app_name, website = app_site, scopes = scopes, api_base_url = domain)
mastodon = Mastodon(client_id = id[0], client_secret = id[1], api_base_url = domain)
except:
print('Failed to get token. Did you spell the domain right?')
sys.exit()
try:
domain = os.environ['domain']
username = os.environ['username']
password = os.environ['password']
except KeyError:
if gui == False:
domain = input('Domain: ')
username = input('E-Mail: ')
password = getpass.getpass('Pass: ')
cfgInit['login']['domain'] = domain
try:
id = Mastodon.create_app(app_name, website = 'https://git.barkshark.tk/izaliamae/reqaccept', scopes = ['read:follows', 'write:follows'], api_base_url = domain)
mastodon = Mastodon(client_id = id[0], client_secret = id[1], api_base_url = domain)
except:
print('Failed to get token. Did you spell the domain right?')
sys.exit()
try:
cfgInit['login']['token'] = mastodon.log_in(username, password, scopes = scopes, to_file=None)
except:
print('Wrong username or password')
sys.exit()
cfgInit['login']['domain'] = domain
if not os.path.exists(confDir):
os.makedirs(confDir)
print('creating config dir')
try:
cfgInit['login']['token'] = mastodon.log_in(username, password, scopes = scopes, to_file=None)
except:
print('Wrong username or password')
sys.exit()
with open(confFile, 'w') as newConf:
cfgInit.write(newConf)
print('Logged in and config written. Exiting to allow whitelist configing')
sys.exit()
if not os.path.exists(confDir):
os.makedirs(confDir)
print('creating config dir')
parsedCfg = config.ConfigParser()
parsedCfg.read(confFile)
return parsedCfg
with open(confFile, 'w') as newConf:
cfgInit.write(newConf)
print('Logged in and config written. Exiting to allow whitelist configing')
sys.exit()
cfg = login('ReqAccept', 'https://git.barkshark.tk/izaliamae/reqaccept', ['read:follows', 'write:follows'], 'config')
cfg = config.ConfigParser()
cfg.read(confFile)
mastodon = Mastodon(access_token = cfg['login']['token'], api_base_url = cfg['login']['domain'])
for req in mastodon.follow_requests():
account = req['acct'].split("@")
user = account[0]
inst = account[1]
if inst in cfg['settings']['whitelist']:
print(req['acct'] + ' is in the whitelist. Accepting request')
mastodon.follow_request_authorize(req['id'])
def runAccept():
for req in mastodon.follow_requests():
account = req['acct'].split("@")
user = account[0]
inst = account[1]
if inst in json.loads(cfg['settings']['whitelist']):
print(req['acct'] + ' is in the whitelist. Accepting request')
mastodon.follow_request_authorize(req['id'])
else:
print(inst + ' not in whitelist. Ignoring ' + req['acct'])
try:
action = sys.argv[1]
except IndexError:
action = 'accept'
if action == 'accept':
runAccept()
elif action == 'add':
if not sys.argv[2:]:
print('You forgot to specify at least one domain')
sys.exit()
else:
print(inst + ' not in whitelist. Ignoring ' + req['acct'])
inst = sys.argv[2:]
whitelist = json.loads(cfg['settings']['whitelist'])
for domain in inst:
if domain in whitelist:
print(domain + ' is already in the whitelist')
else:
whitelist.append(domain)
print('added ' + domain + ' to the whitelist')
cfg['settings']['whitelist'] = json.dumps(sorted(whitelist))
with open(confFile, 'w') as newConf:
cfg.write(newConf)
sys.exit()
elif action == 'rm':
if not sys.argv[2:]:
print('You forgot to specify at least one domain')
sys.exit()
else:
inst = sys.argv[2:]
whitelist = json.loads(cfg['settings']['whitelist'])
for domain in inst:
if domain in whitelist:
whitelist.remove(domain)
print('removed ' + domain + ' from the whitelist')
else:
print(domain + ' isn\'t in the whitelist')
cfg['settings']['whitelist'] = json.dumps(sorted(whitelist))
with open(confFile, 'w') as newConf:
cfg.write(newConf)
sys.exit()
elif action == 'list':
print('Instances currently on the whitelist:')
print('')
for domain in json.loads(cfg['settings']['whitelist']):
print('' + domain)
else:
print('Req Accept v0.2')
print('')
print('Available sub-commands:')
print(' add: Add instances to the whitelist. ')
print(' rm: Remove instances from the whitelist. ')
print(' list: Show all instances on the whitelist.')
print('')
print('Multiple domains separated by a space can be used with "add" and "rm"')

View file

@ -59,4 +59,5 @@ screech.social
therian.club
masto.werefoxsoftware.com
social.kithop.ca
asonix.dog
asonix.dog
thicc.horse