diff options
author | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2016-07-07 17:55:27 +0100 |
---|---|---|
committer | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2016-07-07 17:55:27 +0100 |
commit | 47a6c1c711552040704a2d88276d8c515e0aceb5 (patch) | |
tree | 7f50da85f0c88e8a012c035465f68b5b1e2a7ea9 | |
parent | fcc287c61a28a16a7a17a36105c0097646c02f02 (diff) | |
download | sievely-47a6c1c711552040704a2d88276d8c515e0aceb5.tar.bz2 |
Bunch of stuff
-rw-r--r-- | index.html | 16 | ||||
l--------- | lib/sievelib | 1 | ||||
-rw-r--r-- | sievely.py | 109 | ||||
-rw-r--r-- | sievely/blocks.js | 27 | ||||
-rw-r--r-- | sievely/funcs.js | 31 | ||||
-rw-r--r-- | test.html | 13 |
6 files changed, 188 insertions, 9 deletions
diff --git a/index.html b/index.html new file mode 100644 index 0000000..1fec411 --- /dev/null +++ b/index.html @@ -0,0 +1,16 @@ +<html> + <head> + <title>Log into Sievely</title> + </head> + <body> + <h1>Log into sievely:</h1> + <form action="/login" method="POST"> + <table> + <tr><th align="right">Username:</th><td><input name="username"></td></tr> + <tr><th align="right">Password:</th><td><input name="password" type="password"></td></tr> + <tr><th align="right">Server:</th><td><input name="server" value="mail.codethink.co.uk"></td></tr> + <tr><td></td><td><input type="submit"></td></tr> + </table> + </form> + </body> +</html> diff --git a/lib/sievelib b/lib/sievelib new file mode 120000 index 0000000..778239f --- /dev/null +++ b/lib/sievelib @@ -0,0 +1 @@ +sievelib-submodule/sievelib
\ No newline at end of file diff --git a/sievely.py b/sievely.py new file mode 100644 index 0000000..1b927b3 --- /dev/null +++ b/sievely.py @@ -0,0 +1,109 @@ +#!/usr/bin/python + +from bottle import route, run, static_file, request, redirect, post, response + +import sys +import imaplib +import re + +sys.path.append("./lib") + +from sievelib.managesieve import Client + +ListResponse = re.compile(r'\((?P<flags>[^\)]*)\) "(?P<sep>.)" (?P<box>.*)') +ignore_flags = set(["\\Drafts", "\\Sent", "\\Trash", "\\Noselect"]) + + +@route("/1.0/folders") +def handle_folders(): + cookie = request.get_cookie("sievely_data", default=None, secret="sievely") + if cookie is None: + return {"error": "No cookie"} + mail = imaplib.IMAP4_SSL(cookie['server']) + ret = mail.login(cookie['username'], cookie['password']) + print(repr(ret)) + if ret[0] != 'OK': + mail.logout() + return {"error": "Unable to login"} + print "Ok, logged into IMAP" + (ok, res) = mail.list() + mail.logout() + if ok != "OK": + return {"result": "error", "error": "Unable to list"} + mailboxes = [] + for entry in res: + mo = ListResponse.match(entry) + if not mo: + return {"result": "error", "error": "Unable to parse list response"} + else: + flagset = set(mo.group('flags').split()) + sep = mo.group('sep') + box = mo.group('box') + if box[0] == '"': + # Unquote mailbox name. Is this enough? + box = box[1:-1] + if len(flagset.intersection(ignore_flags)) > 0: + continue + mailboxes.append(box) + def cmpbox(a,b): + if a == "INBOX": + return -1; + if b == "INBOX": + return 1; + return cmp(a, b) + + mailboxes.sort(cmp=cmpbox) + + return {"result": "ok", "folders": mailboxes } + +@route("/google/<path:path>") +def handle_google(path): + return static_file(path, "./google") + +@route("/test.html") +def handle_test(): + cookie = request.get_cookie("sievely_data", default=None, secret="sievely") + if cookie is None: + redirect("/") + else: + return static_file("test.html", ".") + +@route("/") +@route("/index.html") +def handle_test(): + return static_file("index.html", ".") + +@route("/sievely/<path:path>") +def handle_sievely(path): + return static_file(path, "./sievely") + +@post() +@route("/login") +def login(): + username = request.params['username'] + password = request.params['password'] + server = request.params['server'] + # Have a go at verifying the provided credentials + print "Testing Sieve credentials" + c = Client(server) + if not c.connect(username, password, starttls=True): + redirect("/") + return + print "Ok, logged in to sieve" + c.logout() + mail = imaplib.IMAP4_SSL(server) + ret = mail.login(username, password) + print(repr(ret)) + if ret[0] != 'OK': + redirect("/") + return + print "Ok, logged into IMAP" + mail.logout() + + cookie_content = {"username":username,"password":password,"server":server} + response.set_cookie("sievely_data", cookie_content, secret="sievely") + redirect("/test.html") + +# And run the app + +run(host='localhost', port=8080, debug=True, reloader=True) diff --git a/sievely/blocks.js b/sievely/blocks.js index 5f30037..90fc9a8 100644 --- a/sievely/blocks.js +++ b/sievely/blocks.js @@ -115,7 +115,7 @@ Blockly.Blocks['sievely_header'] = { Blockly.Blocks['sievely_fileinto'] = { init: function() { this.appendValueInput("MAILBOX") - .setCheck("String") + .setCheck(["String","Folder"]) .appendField("file into"); this.setPreviousStatement(true, null); this.setNextStatement(true, null); @@ -128,7 +128,7 @@ Blockly.Blocks['sievely_fileinto'] = { Blockly.Blocks['sievely_filecopyinto'] = { init: function() { this.appendValueInput("MAILBOX") - .setCheck("String") + .setCheck(["String","Folder"]) .appendField("file a copy into"); this.setPreviousStatement(true, null); this.setNextStatement(true, null); @@ -137,6 +137,7 @@ Blockly.Blocks['sievely_filecopyinto'] = { this.setHelpUrl(Sievely.Blocks.HELP_URL); } }; + Blockly.Blocks['sievely_reject'] = { init: function() { this.appendValueInput("REASON") @@ -319,3 +320,25 @@ Blockly.Blocks['sievely_group_extra'] = { this.setHelpUrl(Sievely.Blocks.HELP_URL); } }; + +/* Live folder list */ + +function livesievelyfolderlist() { + var ret = []; + var folders = Sievely.allSievelyFolders; + for (var i = 0; i < folders.length; i++) { + ret[i] = [folders[i], folders[i]]; + } + return ret; +} + +Blockly.Blocks['sievely_folder'] = { + init: function() { + this.appendDummyInput() + .appendField(new Blockly.FieldDropdown(livesievelyfolderlist), "FOLDER"); + this.setOutput(true, "Folder"); + this.setColour(Blockly.Blocks.texts.HUE); + this.setTooltip(''); + this.setHelpUrl(Sievely.Blocks.HELP_URL); + } +}; diff --git a/sievely/funcs.js b/sievely/funcs.js new file mode 100644 index 0000000..e6f86f8 --- /dev/null +++ b/sievely/funcs.js @@ -0,0 +1,31 @@ +/* + * Sievely functions + * Copyright 2016 Daniel Silverstone <daniel.silverstone@codethink.co.uk> + * + * See COPYING + */ + +'use strict'; + +goog.provide("Sievely") + +goog.require("Blockly.Blocks") + +Sievely.allSievelyFolders = ["INBOX"] // default to "INBOX" just in case + +function loadedfolders(ftext) { + var result = JSON.parse(ftext) + if (result["result"] == "error") { return; } + Sievely.allSievelyFolders = result["folders"] +} + +function reloadfolders() { + var x = new XMLHttpRequest(); + x.onreadystatechange = function() { + if (x.readyState == 4 && x.status == 200) { + loadedfolders(x.responseText) + } + } + x.open("GET", "/1.0/folders", true); + x.send(null); +} @@ -5,6 +5,7 @@ <script src="google/blockly/blocks_compressed.js"></script> <script src="google/blockly/msg/js/en.js"></script> <script src="sievely/blocks.js"></script> + <script src="sievely/funcs.js"></script> </head> <body> <div id="blocklyDiv" style="height: 600px; width: 800px;"></div> @@ -70,6 +71,7 @@ <field name="HEADER">Cc</field> </block> <block type="text"></block> + <block type="sievely_folder"></block> </category> <category name="Actions"> <category name="Message flags"> @@ -86,9 +88,7 @@ <sep></sep> <block type="sievely_fileinto"> <value name="MAILBOX"> - <block type="text"> - <field name="TEXT">FolderName</field> - </block> + <block type="sievely_folder"></block> </value> <next> <block type="sievely_stop"></block> @@ -96,9 +96,7 @@ </block> <block type="sievely_filecopyinto"> <value name="MAILBOX"> - <block type="text"> - <field name="TEXT">AnotherFolderName</field> - </block> + <block type="sievely_folder"></block> </value> <next> <block type="sievely_stop"></block> @@ -119,7 +117,7 @@ </xml> <xml id="startblocks" style="display: none"> - <block type="sievely_start" deletable="false" x="20" y="20"> + <block type="sievely_start" deletable="false" x="0" y="0"> </block> </xml> <script> @@ -128,6 +126,7 @@ Blockly.Xml.domToWorkspace(document.getElementById('startblocks'), workspace) workspace.addChangeListener(Blockly.Events.disableOrphans); + reloadfolders(); </script> </body> </html> |