Merge pull request #36 from git-sid/multi-deployment

merge small stylechanges
This commit is contained in:
b3yond 2018-09-15 18:58:08 +02:00 committed by GitHub
commit 11f3c5713b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 59 additions and 51 deletions

View file

@ -50,16 +50,16 @@ to check if something was retweeted in the last hour or something.
To this date, we have never heard of this happening though. To this date, we have never heard of this happening though.
### blacklisting ### blockisting
You also need to edit the goodlist and the blacklist. You can do this on the You also need to edit the goodlist and the blocklist. You can do this on the
website, in the settings of your bot. website, in the settings of your bot.
Just add the words to the goodlist, which you want to require. A report is only Just add the words to the goodlist, which you want to require. A report is only
spread if it contains at least one of them. If you want to RT everything, just spread if it contains at least one of them. If you want to RT everything, just
add a ```*```. add a ```*```.
There is also a blacklist, which you can use to automatically sort out There is also a blocklist, which you can use to automatically sort out
malicious messages. Be careful though, our filter can't read the intention with malicious messages. Be careful though, our filter can't read the intention with
which a word was used. Maybe you wanted it there. which a word was used. Maybe you wanted it there.

View file

@ -12,4 +12,3 @@ for loader, name, is_pkg in pkgutil.walk_packages(__path__):
globals()[name] = value globals()[name] = value
__all__.append(name) __all__.append(name)

View file

@ -18,7 +18,8 @@ class Mailbot(Bot):
# returns a list of Report objects # returns a list of Report objects
def crawl(self, user): def crawl(self, user):
reports = [] reports = []
mails = mailbox.mbox('/var/mail/test') # todo: adjust to actual mailbox # todo: adjust to actual mailbox
mails = mailbox.mbox('/var/mail/test')
for msg in mails: for msg in mails:
if get_date_from_header(msg['Date']) > user.get_seen_mail(): if get_date_from_header(msg['Date']) > user.get_seen_mail():
reports.append(make_report(msg, user)) reports.append(make_report(msg, user))

View file

@ -21,7 +21,7 @@ class MastodonBot(Bot):
try: try:
m = Mastodon(*user.get_masto_credentials()) m = Mastodon(*user.get_masto_credentials())
except TypeError: except TypeError:
#logger.error("No Mastodon Credentials in database.", exc_info=True) # logger.error("No Mastodon Credentials in database.", exc_info=True)
return mentions return mentions
try: try:
notifications = m.notifications() notifications = m.notifications()
@ -29,7 +29,7 @@ class MastodonBot(Bot):
logger.error("Unknown Mastodon API Error.", exc_info=True) logger.error("Unknown Mastodon API Error.", exc_info=True)
return mentions return mentions
for status in notifications: for status in notifications:
if user.get_seen_toot() == None: if user.get_seen_toot() is None:
user.init_seen_toot(m.instance()['uri']) user.init_seen_toot(m.instance()['uri'])
if (status['type'] == 'mention' and if (status['type'] == 'mention' and
status['status']['id'] > user.get_seen_toot()): status['status']['id'] > user.get_seen_toot()):
@ -38,8 +38,8 @@ class MastodonBot(Bot):
# add mention to mentions # add mention to mentions
text = re.sub(r'<[^>]*>', '', status['status']['content']) text = re.sub(r'<[^>]*>', '', status['status']['content'])
text = re.sub( text = re.sub(
"(?<=^|(?<=[^a-zA-Z0-9-_.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "(?<=^|(?<=[^a-zA-Z0-9-_.]))@([A-Za-z]+[A-Za-z0-9-_]+)",
"", text) "", text)
if status['status']['visibility'] == 'public': if status['status']['visibility'] == 'public':
mentions.append(Report(status['account']['acct'], mentions.append(Report(status['account']['acct'],
self, self,
@ -71,4 +71,5 @@ class MastodonBot(Bot):
try: try:
m.toot(text) m.toot(text)
except Exception: except Exception:
logger.error('Error tooting: ' + user.get_city() + ': ' + report.id, exc_info=True) logger.error('Error tooting: ' + user.get_city() + ': ' +
report.id, exc_info=True)

View file

@ -25,18 +25,25 @@ class TelegramBot(Bot):
user.save_seen_tg(update.update_id) user.save_seen_tg(update.update_id)
if update.message.text.lower() == "/start": if update.message.text.lower() == "/start":
user.add_telegram_subscribers(update.message.sender.id) user.add_telegram_subscribers(update.message.sender.id)
tb.send_message(update.message.sender.id, "You are now subscribed to report notifications.") tb.send_message(
update.message.sender.id,
"You are now subscribed to report notifications.")
# TODO: /start message should be set in frontend # TODO: /start message should be set in frontend
elif update.message.text.lower() == "/stop": elif update.message.text.lower() == "/stop":
user.remove_telegram_subscribers(update.message.sender.id) user.remove_telegram_subscribers(update.message.sender.id)
tb.send_message(update.message.sender.id, "You are now unsubscribed from report notifications.") tb.send_message(
update.message.sender.id,
"You are now unsubscribed from report notifications.")
# TODO: /stop message should be set in frontend # TODO: /stop message should be set in frontend
elif update.message.text.lower() == "/help": elif update.message.text.lower() == "/help":
tb.send_message(update.message.sender.id, "Send reports here to share them with other users. Use /start and /stop to get reports or not.") tb.send_message(
update.message.sender.id,
"Send reports here to share them with other users. Use /start and /stop to get reports or not.")
# TODO: /help message should be set in frontend # TODO: /help message should be set in frontend
else: else:
reports.append(Report(update.message.sender.username, self, reports.append(Report(update.message.sender.username, self,
update.message.text, None, update.message.date)) update.message.text, None,
update.message.date))
return reports return reports
def post(self, user, report): def post(self, user, report):

View file

@ -33,14 +33,14 @@ class TwitterBot(Bot):
return reports # no twitter account for this user. return reports # no twitter account for this user.
last_dm = user.get_seen_dm() last_dm = user.get_seen_dm()
try: try:
if last_dm == None: if last_dm is None:
mentions = api.direct_messages() mentions = api.direct_messages()
else: else:
mentions = api.mentions_timeline(since_id=last_dm[0]) mentions = api.mentions_timeline(since_id=last_dm[0])
for status in mentions: for status in mentions:
text = re.sub( text = re.sub(
"(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)",
"", status.text) "", status.text)
reports.append(report.Report(status.author.screen_name, reports.append(report.Report(status.author.screen_name,
"twitterDM", "twitterDM",
text, text,

View file

@ -48,8 +48,8 @@ class TwitterBot(Bot):
user.set_last_twitter_request(time()) user.set_last_twitter_request(time())
for status in mentions: for status in mentions:
text = re.sub( text = re.sub(
"(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)", "(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-_]+)",
"", status.text) "", status.text)
reports.append(report.Report(status.author.screen_name, reports.append(report.Report(status.author.screen_name,
self, self,
text, text,

23
db.py
View file

@ -162,7 +162,7 @@ class DB(object):
masto_link TEXT, masto_link TEXT,
twit_link TEXT, twit_link TEXT,
FOREIGN KEY(user_id) REFERENCES user(id), FOREIGN KEY(user_id) REFERENCES user(id),
UNIQUE(user_id, city) ON CONFLICT IGNORE UNIQUE(user_id, city) ON CONFLICT IGNORE
); );
CREATE TABLE IF NOT EXISTS secret ( CREATE TABLE IF NOT EXISTS secret (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
@ -198,11 +198,11 @@ class DB(object):
:return: :return:
""" """
return jwt.encode({ return jwt.encode({
'email': email, 'email': email,
'passhash': scrypt_mcf( 'passhash': scrypt_mcf(
password.encode('utf-8') password.encode('utf-8')
).decode('ascii') ).decode('ascii')
}, self.secret).decode('ascii') }, self.secret).decode('ascii')
def mail_subscription_token(self, email, city): def mail_subscription_token(self, email, city):
""" """
@ -223,7 +223,6 @@ class DB(object):
json = jwt.decode(token, self.secret) json = jwt.decode(token, self.secret)
return json['email'], json['city'] return json['email'], json['city']
def confirm(self, token, city): def confirm(self, token, city):
from user import User from user import User
try: try:
@ -262,10 +261,10 @@ u\d\d?"""
(uid, json['email'])) (uid, json['email']))
self.execute("""INSERT INTO telegram_accounts (user_id, apikey, self.execute("""INSERT INTO telegram_accounts (user_id, apikey,
active) VALUES(?, ?, ?);""", (uid, "", 1)) active) VALUES(?, ?, ?);""", (uid, "", 1))
self.execute("INSERT INTO seen_telegrams (user_id, tg_id) VALUES (?,?);", self.execute(
(uid, 0)) "INSERT INTO seen_telegrams (user_id, tg_id) VALUES (?,?);", (uid, 0))
self.execute("INSERT INTO seen_mail (user_id, mail_date) VALUES (?,?);", self.execute(
(uid, 0)) "INSERT INTO seen_mail (user_id, mail_date) VALUES (?,?);", (uid, 0))
self.commit() self.commit()
user = User(uid) user = User(uid)
user.set_city(city) user.set_city(city)
@ -290,7 +289,7 @@ u\d\d?"""
return User(uid) return User(uid)
def user_facing_properties(self, city): def user_facing_properties(self, city):
self.execute("""SELECT city, markdown, mail_md, masto_link, twit_link self.execute("""SELECT city, markdown, mail_md, masto_link, twit_link
FROM cities FROM cities
WHERE city=?;""", (city, )) WHERE city=?;""", (city, ))
try: try:

View file

@ -123,7 +123,7 @@ def unsubscribe(token):
user = db.by_city(city) user = db.by_city(city)
user.remove_subscriber(email) user.remove_subscriber(email)
return city_page(city, info="You successfully unsubscribed " + email + return city_page(city, info="You successfully unsubscribed " + email +
" from the mail notifications.") " from the mail notifications.")
@get('/settings') @get('/settings')
@ -145,6 +145,7 @@ def update_mail_md(user):
user.set_mail_md(request.forms['mail_md']) user.set_mail_md(request.forms['mail_md'])
return user.state() return user.state()
@post('/settings/goodlist') @post('/settings/goodlist')
@view('template/settings.tpl') @view('template/settings.tpl')
def update_trigger_patterns(user): def update_trigger_patterns(user):
@ -152,10 +153,10 @@ def update_trigger_patterns(user):
return user.state() return user.state()
@post('/settings/blacklist') @post('/settings/blocklist')
@view('template/settings.tpl') @view('template/settings.tpl')
def update_badwords(user): def update_badwords(user):
user.set_badwords(request.forms['blacklist']) user.set_badwords(request.forms['blocklist'])
return user.state() return user.state()

View file

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
class Report(object): class Report(object):
""" """
A ticketfrei report object. A ticketfrei report object.

View file

@ -34,7 +34,7 @@ class Mailer(object):
try: try:
context = ssl.create_default_context() context = ssl.create_default_context()
self.s.starttls(context=context) self.s.starttls(context=context)
except: except BaseException: # TODO: Amend specific exception
logger.error('StartTLS failed.', exc_info=True) logger.error('StartTLS failed.', exc_info=True)
self.s.login(config["mail"]["user"], config["mail"]["passphrase"]) self.s.login(config["mail"]["user"], config["mail"]["passphrase"])

View file

@ -83,16 +83,16 @@
</form> </form>
</div> </div>
<!-- blacklist entry field --> <!-- blocklist entry field -->
<div style="float:right; padding: 1.5em;"> <div style="float:right; padding: 1.5em;">
<p> <p>
These words are not allowed in reports. These words are not allowed in reports.
If you encounter spam, you can add more here - the bot will ignore reports which use such words. If you encounter spam, you can add more here - the bot will ignore reports which use such words.
<!-- There are words which you can't exclude from the blacklist, e.g. certain racist, sexist, or antisemitic slurs. (to be implemented) --> <!-- There are words which you can't exclude from the blocklist, e.g. certain racist, sexist, or antisemitic slurs. (to be implemented) -->
</p> </p>
<form action="/settings/blacklist" method="post"> <form action="/settings/blocklist" method="post">
<!-- find a way to display current blacklist. js which reads from a cookie? template? --> <!-- find a way to display current blocklist. js which reads from a cookie? template? -->
<textarea id="blacklist" rows="8" cols="70" name="blacklist" wrap="physical"></textarea> <textarea id="blocklist" rows="8" cols="70" name="blocklist" wrap="physical"></textarea>
<input name='confirm' value='Submit' type='submit'/> <input name='confirm' value='Submit' type='submit'/>
</form> </form>
</div> </div>

View file

@ -36,4 +36,4 @@ document.getElementById("enablebutton").innerHTML = enableButton();
document.getElementById("goodlist").innerHTML = listformat(getCookie("goodlist")); document.getElementById("goodlist").innerHTML = listformat(getCookie("goodlist"));
document.getElementById("blacklist").innerHTML = listformat(getCookie("blacklist")); document.getElementById("blocklist").innerHTML = listformat(getCookie("blocklist"));

View file

@ -141,15 +141,15 @@
</div> </div>
<div> <div>
<h2>Edit the blacklist</h2> <h2>Edit the blocklist</h2>
<p> <p>
These words are not allowed in reports. If you encounter spam, you can These words are not allowed in reports. If you encounter spam, you can
add more here - the bot will ignore reports which use such words. add more here - the bot will ignore reports which use such words.
<!-- There are words which you can't exclude from the blacklist, e.g. certain racist, sexist, or antisemitic slurs. <!-- There are words which you can't exclude from the blocklist, e.g. certain racist, sexist, or antisemitic slurs.
</p> </p>
<form action="/settings/blacklist" method="post"> <form action="/settings/blocklist" method="post">
<!-- find a way to display current blacklist. js which reads from a cookie? template? --> <!-- find a way to display current blocklist. js which reads from a cookie? template? -->
<textarea id="blacklist" rows="8" cols="70" name="blacklist" wrap="physical">{{badwords}}</textarea> <textarea id="blocklist" rows="8" cols="70" name="blocklist" wrap="physical">{{badwords}}</textarea>
<input name='confirm' value='Submit' type='submit'/> <input name='confirm' value='Submit' type='submit'/>
</form> </form>
</div> </div>

View file

@ -80,7 +80,7 @@ fag
faggot faggot
nigger nigger
neger neger
schlitz schlitz
""" """
db.execute("SELECT words FROM badwords WHERE user_id=?;", db.execute("SELECT words FROM badwords WHERE user_id=?;",
(self.uid, )) (self.uid, ))
@ -253,7 +253,7 @@ schlitz
# - markdown # - markdown
# - mail_md # - mail_md
# - goodlist # - goodlist
# - blacklist # - blocklist
# - logged in with twitter? # - logged in with twitter?
# - logged in with mastodon? # - logged in with mastodon?
# - enabled? # - enabled?
@ -345,8 +345,8 @@ schlitz
return db.cur.fetchone()[0] return db.cur.fetchone()[0]
def set_city(self, city): def set_city(self, city):
masto_link = "https://example.mastodon.social/@" + city # get masto_link masto_link = "https://example.mastodon.social/@" + city # get masto_link
twit_link = "https://example.twitter.com/" + city # get twit_link twit_link = "https://example.twitter.com/" + city # get twit_link
mailinglist = city + "@" + config['web']['host'] mailinglist = city + "@" + config['web']['host']
markdown = """# Wie funktioniert Ticketfrei? markdown = """# Wie funktioniert Ticketfrei?
@ -464,4 +464,3 @@ unsubscribe-link mitgeschickt.
masto_link, twit_link) VALUES(?,?,?,?,?,?)""", masto_link, twit_link) VALUES(?,?,?,?,?,?)""",
(self.uid, city, markdown, mail_md, masto_link, twit_link)) (self.uid, city, markdown, mail_md, masto_link, twit_link))
db.commit() db.commit()