This is my first post in 2013! Every begining of a new year, people tend to make a list of good resolution. I also did and one of them is to switch from Perl to Python to develop! Being a Perl addict for years, I don’t expect to completely abandon my beloved language but… I must be able to write better Python scripts! It’s a fact: Python is more and more chosen as the preferred language in many open source projects. I just started to read the book “Violent Python: A Cookbook for Hackers, Forensic Analysts, Penetration Testers and Security Engineers“. Based on my experience with Python, It looks very good and I hope to write a quick review soon. So, here is my first Python script!
I’m using several “iDevices” and all of them are connected to iCloud. Let’s put aside the pros & cons of cloud storage (of course, I don’t store sensitive information in my iCloud account). It’s so convenient to take some notes during a conference on your iPad and have them already synchronized on your Macbook when you’re back at home! But I’m also using several UNIX systems and sometimes, sharing notes from them could also have multiple benefits. You can of course access your iCloud data via a regular browser but I was looking for something more “command line” based. I did not find a solution so I wrote mine!
If you have a iCloud email address (@me.com), you can already access your mail via the IMAP protocol. The good news? Your notes are stored in a specific IMAP folder called “Notes“. Using your preferred MUA, connect to your IMAP account viaÂ imap.mail.me.com and list the messages in the Notes folder. Their format looks like:
Date: Fri, 11 Jan 2013 13:08:33 +0100 From: email@example.com X-Uniform-Type-Identifier: com.apple.mail-note Content-Type: text/html; Subject: Test <html> <head></head> <body> Test </body></html>
To make a message seen as a “Note” by the Apple applications, you just add the following header:
That’s easy! To manage your iCloud notes, you just need an … IMAP client! Python has a wonderful set of libraries to extend its capabilities. Of course, one of them supports… IMAP! It is called imaplib. This library implements all the required classes to perform all the operation that I wanted on an IMAP folder (read: my Notes):
- List the notes
- Search for a keyword in notes
- Create a new note
$ ./inotes.py -h Usage: inotes.py [options] Options: --version show program's version number and exit -h, --help show this help message and exit -c CONFIGFILE, --config=CONFIGFILE specify the configuration file -C, --count count the number of notes -d, --debug display this message -H, --html save the new note in HTML format -l, --list list saved notes -q QUERY, --query=QUERY search fo keyword in saved notes -s SUBJECT, --subject=SUBJECT create a new note with subject -S, --striphtml remove HTML tags from displayed notes
The script (default location: $HOME/inotes.cfg) requires a small configuration file which contains your credentials:
[server] hostname: imap.mail.me.com username: mylogin password: MySup3rS3cr3tP4$$W0rD
To create a note note, just type it:
$ ./inotes.py -c inotes.cfg -s "Test Note" This is a UNIQUEKEYWORD! ^D
Or use the output of another command (it reads from STDIN):
$ cat foo.conf |Â ./inotes.py -c inotes.cfg -s "foo configuration"
Again, do NOT store sensitive information in iCloud notes! To search for a specific keyboard in all notes:
$ ./inotes.py -c inotes.cfg -q UNIQUEKEYWORD Subject: Test Note --- This is a UNIQUEKEYWORD!
By default, the Apple tools store notes in HTML format. To handle this, I added two switches:
- ‘-S’ to strip the HTML tags while searching for notes
- ‘-H’ to save a new note in HTML
As you can see, nothing fancy but it does what I need! The code is maybe hugly, buggy but available here. Feel free to use (and improve!) it. Note that you could use this script with any IMAP server, no need to have an iCloud access.