If incoming msg isn't an email address, send help msg
This commit is contained in:
parent
a8c1d1776f
commit
337597e4aa
|
|
@ -1,4 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import re
|
||||||
|
|
||||||
from deltachat_rpc_client import events, run_bot_cli, EventType, Message
|
from deltachat_rpc_client import events, run_bot_cli, EventType, Message
|
||||||
|
|
||||||
from keyserver_bot.wkd import request_from_wkd
|
from keyserver_bot.wkd import request_from_wkd
|
||||||
|
|
@ -7,20 +9,22 @@ from keyserver_bot.vcard import construct_vcard, save_vcard
|
||||||
|
|
||||||
hooks = events.HookCollection()
|
hooks = events.HookCollection()
|
||||||
|
|
||||||
|
HELP_MSG = "If you send me an email address, "\
|
||||||
@hooks.on(events.RawEvent)
|
"I will fetch it's public PGP key "\
|
||||||
def log_event(event):
|
"(from WKD and keys.openpgp.org) "\
|
||||||
print(event)
|
"and send you a contact you can encrypt to."
|
||||||
if event.kind == EventType.MSG_DELIVERED or event.kind == EventType.MSG_FAILED:
|
|
||||||
msg = Message(event.account, event.msg_id).get_snapshot()
|
|
||||||
delete_data(msg)
|
|
||||||
|
|
||||||
|
|
||||||
@hooks.on(events.NewMessage)
|
@hooks.on(events.NewMessage)
|
||||||
def echo(event):
|
def command(event):
|
||||||
snapshot = event.message_snapshot
|
snapshot = event.message_snapshot
|
||||||
|
|
||||||
email = snapshot.text
|
email = snapshot.text
|
||||||
# TODO: validate email RFC
|
regex = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
|
||||||
|
if not re.match(regex, email):
|
||||||
|
snapshot.chat.send_text(HELP_MSG)
|
||||||
|
return
|
||||||
|
|
||||||
public_key = request_key_by_email(email)
|
public_key = request_key_by_email(email)
|
||||||
if not public_key:
|
if not public_key:
|
||||||
snapshot.chat.send_text("Sorry, I could not find a key for this user.")
|
snapshot.chat.send_text("Sorry, I could not find a key for this user.")
|
||||||
|
|
@ -32,13 +36,6 @@ def echo(event):
|
||||||
snapshot.chat.send_file(vcard_path)
|
snapshot.chat.send_file(vcard_path)
|
||||||
|
|
||||||
|
|
||||||
def delete_data(msg):
|
|
||||||
contacts = msg.chat.get_contacts()
|
|
||||||
msg.chat.delete()
|
|
||||||
for member in contacts:
|
|
||||||
member.delete()
|
|
||||||
|
|
||||||
|
|
||||||
def request_key_by_email(email) -> str:
|
def request_key_by_email(email) -> str:
|
||||||
domain = email.split("@")[1]
|
domain = email.split("@")[1]
|
||||||
public_key = request_from_wkd(email, domain)
|
public_key = request_from_wkd(email, domain)
|
||||||
|
|
@ -49,6 +46,21 @@ def request_key_by_email(email) -> str:
|
||||||
return public_key
|
return public_key
|
||||||
|
|
||||||
|
|
||||||
|
@hooks.on(events.RawEvent)
|
||||||
|
def cleanup(event):
|
||||||
|
print(event)
|
||||||
|
if event.kind == EventType.MSG_DELIVERED or event.kind == EventType.MSG_FAILED:
|
||||||
|
msg = Message(event.account, event.msg_id).get_snapshot()
|
||||||
|
delete_data(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def delete_data(msg):
|
||||||
|
contacts = msg.chat.get_contacts()
|
||||||
|
msg.chat.delete()
|
||||||
|
for member in contacts:
|
||||||
|
member.delete()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# TODO: print invite link
|
# TODO: print invite link
|
||||||
run_bot_cli(hooks)
|
run_bot_cli(hooks)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue