From e344a51b0e7becb3934ab8a3ec4b8b0659c0d712 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 13 Apr 2017 22:04:09 +0300 Subject: Copy remaining stuff from YarnHelper to lib.py --- 900-implements.yarn | 18 ++++++------------ lib.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/900-implements.yarn b/900-implements.yarn index 31cec0c..976e0ce 100644 --- a/900-implements.yarn +++ b/900-implements.yarn @@ -8,10 +8,9 @@ ## HTTP requests IMPLEMENTS WHEN user fetches (\S+) - h = yarnhelper.YarnHelper() address = os.environ['ADDRESS'] url = get_next_match() - status, body = h.http_get(address, url) + status, body = http_get(address, url) vars['http_status'] = status vars['http_body'] = body @@ -87,7 +86,6 @@ ## Send mail over SMTP+TLS IMPLEMENTS WHEN someone sends mail to (\S+) with subject \$(\S+) - import smtplib to_addr = get_next_match() subject_key = get_next_match() subject = vars[subject_key] @@ -105,7 +103,6 @@ ## Check for a mail in a mailbox IMPLEMENTS THEN mailbox of (\S+) has a mail with subject \$(\S+) - h = yarnhelper.YarnHelper() user = get_next_match() subject_key = get_next_match() subject = vars[subject_key] @@ -113,19 +110,18 @@ address = os.environ['ADDRESS'] pass_name = 'pieni.net/{}'.format(user) pass_home = os.environ['PASS_HOME'] - password = h.get_password_with_pass(pass_home, pass_name) + password = get_password_with_pass(pass_home, pass_name) got_it = False def match(imapobj, msgid, msg): global got_it print 'Subject:', msg['Subject'] got_it = got_it or (msg['Subject'] == subject) - h.iterate_mails_in_imap_mailbox(address, user, password, match, False) + iterate_mails_in_imap_mailbox(address, user, password, match, False) assertTrue(got_it) ## Delete mails IMPLEMENTS THEN mails for (\S+) with subject \$(\S+) get deleted - h = yarnhelper.YarnHelper() user = get_next_match() subject_key = get_next_match() subject = vars[subject_key] @@ -133,11 +129,11 @@ address = os.environ['ADDRESS'] pass_name = 'pieni.net/{}'.format(user) pass_home = os.environ['PASS_HOME'] - password = h.get_password_with_pass(pass_home, pass_name) + password = get_password_with_pass(pass_home, pass_name) def delete_matching(imapobj, msgid, msg): if msg['Subject'] == subject: imapobj.store(msgid, '+FLAGS', '(\\Deleted)') - h.iterate_mails_in_imap_mailbox( + iterate_mails_in_imap_mailbox( address, user, password, delete_matching, True) ## Run sequence of SMTP commands, check SMTP status @@ -155,15 +151,13 @@ vars['smtp_commands'] = smtp_commands IMPLEMENTS THEN SMTP status code is (\d+) - import smtplib - h = yarnhelper.YarnHelper() wanted_status = int(get_next_match()) smtp_commands = vars['smtp_commands'] smtp_username = vars['smtp_username'] if smtp_username is not None: pass_home = os.environ['PASS_HOME'] pass_user = 'pieni.net/{}'.format(smtp_username) - smtp_password = h.get_password_with_pass(pass_home, pass_user) + smtp_password = get_password_with_pass(pass_home, pass_user) address = os.environ['ADDRESS'] server = smtplib.SMTP() print 'connecting to', address diff --git a/lib.py b/lib.py index e0bc8cf..c97ff04 100644 --- a/lib.py +++ b/lib.py @@ -1,14 +1,62 @@ +import email +import imaplib import os import random import re +import smtplib +import subprocess import sys +import urlparse import cliapp +import requests +import yaml from yarnutils import * -import yarnhelper srcdir = os.environ['SRCDIR'] datadir = os.environ['DATADIR'] vars = Variables(datadir) + + + +def construct_aliased_http_request(address, method, url): + parts = list(urlparse.urlparse(url)) + headers = { + 'Host': parts[1], + } + parts[1] = address + aliased_url = urlparse.urlunparse(parts) + + r = requests.Request(method, aliased_url, headers=headers) + return r.prepare() + +def http_get(address, url): + r = construct_aliased_http_request(address, 'GET', url) + s = requests.Session() + resp = s.send(r) + return resp.status_code, resp.content + +def get_password_with_pass(pass_home, pass_name): + p = subprocess.Popen( + ['env', 'HOME={}'.format(pass_home), 'pass', 'show', pass_name], + stdout=subprocess.PIPE) + stdout, stderr = p.communicate() + password = stdout.rstrip() + return password + +def iterate_mails_in_imap_mailbox(address, user, password, callback, exp): + m = imaplib.IMAP4_SSL(address) + m.login(user, password) + m.select('INBOX', False) + typ, data = m.search(None, 'ALL') + for num in data[0].split(): + typ, data = m.fetch(num, '(RFC822)') + typ, text = data[0] + msg = email.message_from_string(text) + callback(m, num, msg) + if exp: + m.expunge() + m.close() + m.logout() -- cgit v1.2.1