2014-08-31

How to view clojure docs in Emacs Cider

Developing clojure with Emacs Cider is great. To be able to view summaries for clojure functions and special forms, type this in you Cider REPL:

(use 'clojure.repl)

Afterwards, you can do stuff like this:
my.repl> (doc conj)
-------------------------
clojure.core/conj
([coll x] [coll x & xs])
  conj[oin]. Returns a new collection with the xs
    'added'. (conj nil item) returns (item).  The 'addition' may
    happen at different 'places' depending on the concrete type.
nil

2014-08-20

How to automatically refresh Cider when saving a clojure file in Emacs

Emacs has a great mode for using Clojure called Cider. Cider comes with an interactive REPL. The REPL allows you to test your code, start web apps, if you are using Luminus, and all in all accelerates  development. One annoying thing though is that you have to refresh Cider every time you saved your sources. The good thing is that you can do this automatically. Just add the following to your init.el. I took the function from a blog post by Kris Jenkins:

  (add-hook 'cider-mode-hook 
     '(lambda () (add-hook 'after-save-hook 
      '(lambda () 
         (if (and (boundp 'cider-mode) cider-mode)
      (cider-namespace-refresh)
           )))))

  (defun cider-namespace-refresh ()
    (interactive)
    (cider-interactive-eval
     "(require 'clojure.tools.namespace.repl)
    (clojure.tools.namespace.repl/refresh)"))

  (define-key clojure-mode-map (kbd "C-c C-r") 'cider-namespace-refresh)

Additionally, you can now run the refresh command manually, by hitting C-c C-r.

Update: I changed the after-save-hook to only trigger if we are in a buffer that has cider-mode enabled. Otherwise every save command in Emacs would have triggered the refresh!

2014-08-17

Forwarding emails using fetchmail and msmtp

My goal here was to forward emails from my GMX freemail account to my iCloud account. Up until now, I used GMX's own forwarding capability, which is a bit hidden in the filters settings. However, iCloud cranked up its spam filtering, and is now using spamhaus blacklists, which very often label the GMX forwarding servers as bad.

Hence I would only get bounce mails instead of the actual mails. Since this is no good, I set up fetchmail and msmtp on my root server to do the forwarding for me. First, fetchmail will get all mail on GMX via POP3 and pass it on to msmtp, which will in turn pass it on to the iCloud mx server.

At first I tried to deliver it via authenticated SMTP, but iCloud refuses mails sent this way, if the header from field does not contain any of your own iCloud aliases. This will most of the time be a problem, since we are trying to forward emails that were sent to you, not sent from you.

So first let's see the ~/.fetchmailrc (make sure to chmod 0600 it):

poll pop.gmx.net
with proto POP3
user "user@gmx.net"
there with password "secretpassword"
mda "/usr/bin/msmtp -- someuser@icloud.com"
options
no keep
ssl
sslcertck
sslcertpath /etc/ssl/certs
set daemon 300

This will poll GMX every 300 seconds and pass the received mails to msmtp for delivery to someuser@icloud.com.

The corresponding ~/.msmtprc looks like this:

account default
host mx6.mail.icloud.com
port 25
auto_from off
from "user@localdomain"
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
domain mx.of.localdomain

You can find out the valid mx entries for iCloud by running nslookup -type=mx icloud.com.

The settings above are assuming Debian stable. Other distributions or operating systems may have the SSL certs at different places in the file system.

2014-07-30

hal+json is the way to go for representing REST resources

If you are implementing REST APIs, and are thinking about using application/json as your content type, please consider application/hal+json. It allows you to represent link relations and embedded resources in a standardized manner.

The content type offers three things:

  1. Semantic link relations using the _link key. The self link is a good example.
  2. Embedded resources using the _embedded key, which are a subset of the link relations
  3. Properties with arbitrary keys

As an example, you might have a programmable power strip with three sockets. The strip itself can be modeled as a resource. It will have three optionally embedded resources with the link relation sockets. These resources themselves will have a link relation called toggle. Doing e.g. a POST on this resource will allow you to toggle it. The lovely thing here is that the API will give you the correct URL to turn on or off the socket. An example request and response:

GET http://foo.com/powerstrip/1
Accept: application/hal+json

{
    "_links" : {
 "self" : { "href" : "http://somedomain/powerstrip/1" },
 "sockets" : [
     { "href" : "http://somedomain/powerstrip/1/sockets/1", "name" : "Socket 1" },
     { "href" : "http://somedomain/powerstrip/1/sockets/1", "name" : "Socket 2" },
     { "href" : "http://somedomain/powerstrip/1/sockets/1", "name" : "Socket 3" }
 ]
    },
    "_embedded" : {
 "sockets" : [
     { 
  "_links" : {
      "self" : { "href" : "http://somedomain/powerstrip/1/sockets/1" },
      "toggle" : { "href" : "http://somedomain/powerstrip/1/sockets/1/off" }
  }
  "state" : "on"
     },
     { 
  "_links" : {
      "self" : { "href" : "http://somedomain/powerstrip/1/sockets/2" },
      "toggle" : { "href" : "http://somedomain/powerstrip/1/sockets/2/on" }
  }
  "state" : "off"
     },
     { 
  "_links" : {
      "self" : { "href" : "http://somedomain/powerstrip/1/sockets/3" },
      "toggle" : { "href" : "http://somedomain/powerstrip/1/sockets/3/on" }
  }
  "state" : "off"
     }
 ]
    },
    "numberOfSockets" : 3,
    "voltage" : 230
}

The content type is described in an RFC draft, and is well on its way to become a standard. Make sure to also read the associated web linking RFC.

Amazon is already using this content type in its AppStream API.

The strength of using link relations and a content type such as HAL is that you can actually document your link relations, which are a fundamental part of your API. You should actively design the link relations and make them meaningful.

For resources, I recommend that you document the following aspects:

  1. Expected link relations and their embeddedness
  2. Attributes of the resource
  3. Example method calls for all allowed methods (GET, POST, ...) with example responses 

For link relations you should document these aspects:

  1. Synopsis what the link relation means or represents, and which resource is to be expected
  2. Allowed methods with optional templated arguments
I place the example method calls with the resource documentation, since they might be redundant if specified with the link relations. But you should link to the resource documentation from the associated link relation documentation.

2014-07-15

Extracting Names from Email Addresses

Given a CSV file with the following format:
;;firstname.lastname@somehost.com
The task is to extract the names from the email addresses. We assume that the names are seperated by periods (.) and that all the names are supposed to be capitalized and printed with strings:
#!/usr/bin/env python

import sys

if len( sys.argv ) < 2:
    print "Usage: %s filename" % sys.argv[ 0 ]
    sys.exit( 1 )

textFileName = sys.argv[ 1 ]
textFile = open( textFileName, "r" )

for line in textFile:
    fields = line.strip().split( ';' )
    email = fields[ 2 ].split( "@" )
    emailName = email[ 0 ].split( '.' )
    capitalizedName = [ x[:1].upper() + x[1:].lower() for x in emailName ]
    print '%s;%s;%s' % ( capitalizedName[ 0 ], ' '.join( capitalizedName[ 1: ] ), fields[ 2 ] )

2014-03-22

How to backup your Raspberry Pi SD-Card

In the following I will explain on how you can backup your Pi's SD-card, so that whenever it breaks down, you can simply restore the backup image to a fresh SD-Card. This may also help with tinkering, when you totally screwed up your Pi's Linux-installation. I will explain the first step specifically for Mac OS X, but you can do this similarly on other UNIXes and Linux, by using the mount and umount commands.

So, shut down your Pi using the command sudo shutdown -h now and remove the SD-card after the Pi has done so. Take the card and insert it into your Mac's SD-card slot.

First of all, we need to find out which disk device has been assigned to the card. We can do this with diskutil list:

user@mymacintosh:~ $ diskutil list                                                                
/dev/disk0                                                                                            
   #:                       TYPE NAME                    SIZE       IDENTIFIER                        
   0:      GUID_partition_scheme                        *512.1 GB   disk0                             
   1:                        EFI EFI                     209.7 MB   disk0s1                           
   2:                  Apple_HFS M4                      511.1 GB   disk0s2                           
   3:                 Apple_Boot Recovery HD             784.2 MB   disk0s3                           
/dev/disk1                                                                                            
   #:                       TYPE NAME                    SIZE       IDENTIFIER                        
   0:     FDisk_partition_scheme                        *15.9 GB    disk1                             
   1:             Windows_FAT_32                         58.7 MB    disk1s1                         
   2:                      Linux                         15.9 GB    disk1s2                           

Then unmount the card using diskutil unmountDisk:

user@mymacintosh:~ $ diskutil umountDisk /dev/disk1                                              
Unmount of all volumes on disk1 was successful                                                     

Next we will use dd to produce an image, which we can compress using bzip2, or pbzip2 for more performance. You can use MacPorts to install pbzip2.

user@mymacintosh:~ $ sudo dd if=/dev/disk1 | pbzip2 > raspberry_pi_$(date "+%Y-%m-%d").img.bz2 
31116288+0 records in                                                                                                           
31116288+0 records out                                                                                                        
15931539456 bytes transferred in 2347.692010 secs (6786043 bytes/sec)                                                           

Restoring the image is also easy:

user@mymacintosh:~ $ bunzip2 raspberry_pi_2014-03-22.img.bz2 | sudo dd of=/dev/disk1      

2014-03-16

Getting Emacs for Mac OS X to honour the shell environment

If your are using the excellent Emacs for Mac OS X distribution, you may have noticed that it does not use the environment you may have defined in your ~/.bashrc. This can be a problem if you are using MacPorts, for example to install clojure and Leiningen. You will get an error message like this, when trying to run inferior-lisp in clojure-mode:

apply: Searching for program: No such file or directory, lein

This can be avoided by using the excellent exec-path-from-shell package for Emacs. It is available via (M)ELPA, just install it using list-packages, or download it from github. You can enable it, especially for OS X, by putting this in your init.el:

(when (memq window-system '(mac ns))
    (exec-path-from-shell-initialize))