139 lines
3.8 KiB
Python
Executable file
139 lines
3.8 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
###
|
|
### ReqAccept v0.2
|
|
### by Zoey Mae (@izaliamae@barkshark.tk)
|
|
###
|
|
|
|
import os, sys, getpass, json
|
|
from mastodon import Mastodon
|
|
import configparser as config
|
|
|
|
app_name = 'Req Accept'
|
|
ini = 'config'
|
|
|
|
confDir = os.environ['HOME'] + '/.config/' + app_name.replace(" ", "")
|
|
confFile = confDir + '/' + ini + '.ini'
|
|
|
|
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:
|
|
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: ')
|
|
|
|
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()
|
|
|
|
cfgInit['login']['domain'] = domain
|
|
|
|
try:
|
|
cfgInit['login']['token'] = mastodon.log_in(username, password, scopes = scopes, to_file=None)
|
|
except:
|
|
print('Wrong username or password')
|
|
sys.exit()
|
|
|
|
if not os.path.exists(confDir):
|
|
os.makedirs(confDir)
|
|
print('creating config dir')
|
|
|
|
with open(confFile, 'w') as newConf:
|
|
cfgInit.write(newConf)
|
|
print('Logged in and config written. Exiting to allow whitelist configing')
|
|
sys.exit()
|
|
|
|
cfg = config.ConfigParser()
|
|
cfg.read(confFile)
|
|
|
|
mastodon = Mastodon(access_token = cfg['login']['token'], api_base_url = cfg['login']['domain'])
|
|
|
|
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:
|
|
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"')
|