Archive for April, 2008

Sending mail through gmail Using Emacs

Very brief summary for the impatient
Ensure that the gnutls-bin program is installed (

apt-get install gnutls-bin

on ubuntu)

Paste the following into your init file changing username and password as appropriate.

(setq send-mail-function 'smtpmail-send-it)
(setq smtpmail-smtp-server "")
(setq smtpmail-smtp-service 25)
(setq smtpmail-auth-credentials '(("" 25 "USERNAME" "PASSWORD")))
(setq smtpmail-starttls-credentials '(("" 25 nil nil)))

Evaluate this with “M-x eval-buffer”


Longer explanation for the troubled / interested

Emacs can, if set up correctly, send mail through gmails smtp server.

First tell emacs to use smtp to send mail by evaluating:
(setq send-mail-function ‘smtpmail-send-it)

(If you don’t do this email will attempt to use sendmail to send mail – on my machine this resulted in mails not being sent despite emacs reporting that they were).

You must then set smtpmail’s authentification details. First run

(setq smtpmail-debug-info t)
(setq smtpmail-debug-verb t)

so as to get as much output as possible.

Then run

(setq smtpmail-smtp-server "")
(setq smtpmail-smtp-service 25)

to tell emacs to use gmail for outgoing mail.

There are (at least) two mechanisms that smtp servers can use to authenticate users.

One method has the user provide a user name and password in plain(ish) text in the smtp connection. This connection can also be encrypted. This encryption mechanism is known as STARTTLS. Gmail uses a plain text authentication method sent over an encrypted connection in this manner.

With the other method, the user passes a certificate to the server which asserts that the user is who they say they are. This method of authentication is, confusingly, also know as STARTTLS.

To set-up authentication within SMTP evaluate

(setq smtpmail-auth-credentials '(("" 25 "USERNAME" "PASSWORD")))

To set up STARTTLS encryption of the connection call

(setq smtpmail-starttls-credentials '(("" 25 nil nil)))

(This variable is also used to set up authentication using STARTTLS).

After you have done this you should be able to send mail from within emacs. To test this run hit
C-x C-m. Write a test message, and then hit C-c C-s, before toggling to a buffer with a name beginning with “*trace of SMTP.” If anything goes wrong this should tell you the approximate reason.

More details

There doesn’t seem to be a way to make emacs prompt for a password when sending mail (though one could probably be hacked-together with half an hours work – after the other 10 hours needed to learn emacs lisp).

You can avoid keeping a password in your initialization file by using a .netrc file.

smtpmail’s documentation can be found here .

If things start not working you might like to use the source – but be careful, some older versions of smtpmail don’t support starttls.


April 27, 2008 at 7:51 pm 4 comments

Higher-order functions are quite like objects

I occasionally read things that cite higher-order functions as one of the main advantages of dynamic or functional programming languages… this is good since I quite like higher-order functions and dynamic programming languages, the only issue is that creating higher order functions is almost exactly the same as object instantiation.

To make this slightly clearer, consider this example written in python:

def makeAdder(x):
    def f(y):
         return x + y
    return f
g = makeAdder(5)

and compare this to

class Adder:
    def __init__(self, x):
        self.x = x
    def execute(self, y):
        return x + y

    def execute_something_else(self, y):

g = Adder(5)

This code is more or less identical, both examples are just storing some state and then executing a function at a later time based on this state, the only difference is that the higher-order function uses a more specific mechanism – there is only one function that we can call on the state we set – whereas with a class we can call as many functions as we want.

Does this mean that higher-order functions are meaningless and we should just go back to doing object oriented programming, which is more general? Not exactly.

Higher order functions still have some advantages over class instantiation, it’s probably just that they aren’t the ones that people normally list.

Some advantages include:

  1. Function calls have a fixed interface. By limiting yourself to using this fixed interface all code that uses this interface can be composed easily. People might be more likely to limit themselves to using language features than they are to using some arbitrarily decided upon interface.
  2. The syntax for defining and calling functions has slightly less noise than that for defining and instantiating classes in most languages – (thought not a hideous amount less). Removing noise has a habit of making things easier to read (assuming this noise isn’t providing people with context)
  3. Using higher-order functions tend to produce code that is slightly more declarative in nature (i.e things ‘are something’ rather than ‘doing something’), this can, depending on mood, make code easier to read as one doesn’t need to follow the flow of the program so much. (To make this declarative versus imperative argument clearer consider the task of parsing an SQL statement compared to a function that does the corresponding task).
  4. You don’t need to make a conscious decision about what you are doing when you use higher-order functions, whereas if you use the corresponding “class and function” code, you will have probably have had to decide upon this idiom

So using higher-order function can help you when programming by being less powerful, less general and more restrictive… or something like that.

April 26, 2008 at 11:10 pm Leave a comment

Backing up one’s home directory with tar

This is something that should be fairly easy. However in practice there are some problems:

  1. Tar has a proliferation of options and can be an effort to use – at least if you are using it infrequently
  2. One’s home directory often contains large files (like perfectly legally downloaded music and videos), which you don’t really want to back up repeatedly.
  3. Some applications put rather large things in hidden directories under your home directory

There are various solutions:

April 20, 2008 at 2:01 pm 3 comments

How configuration should work.

If you use emacs you might know about the “customize-apropos” command. This command will take a regular expression and search for all settings that match it. Scripts written in emacs lisp can register settings so that this command can find them. For any of the settings found it is possible to jump to a group of similar settings. This is a very nice way of doing configuration.

In fact some applications do do such things: in windows vista is is now possible to search the control panel for controls (though the granularity is quite a lot higher and one can’t use regular expressions).

I also quite like the idea of ‘faceted selection of options’, for want of a less pretentious name. Those interested in the ‘semantic web’ seem to quite like this concept. The idea here would be that each system setting would have a number of facets associated with it. For example a setting might have a ‘string facet’ indicating it is a string type, a ‘networking’ and ‘security’ facet, and perhaps an advanced facet. When searching for settings one could could specify a number facets to restrict results together with search strings. This might well make it a great deal easier to find settings, and would remove the ‘guess the taxonomy’ game that one normally needs to play when looking at configuration options. It also means you don’t

April 19, 2008 at 1:03 am Leave a comment

How to backup a directory using tar in one line.

This is all terribly simple… but I’m rather inclined to forget it. The reason I’m putting this here is that I’ve written something like this at least twice.

file=$(basename `pwd`);
backupName=$file-$(date +%Y-%m-%d.tgz);
tar -C .. -czf $backupName $file --exclude=$file/$backupName

This creates a gunzipped tar file of the current directory, in the current directory and includes the current date in the name. This is quite useful for your home directory.

April 5, 2008 at 2:09 pm 1 comment

A local firewall for ubuntu

A very simple local firewall for Ubuntu machines using iptables.

I use the following set up:

In /etc/network/if-pre-up.d, I have an executable shell script called firewall which uses iptables-restore to load firewall settings stored in /etc/network/firewall. This script is as follows:

#/etc/network/if-pre-up.d/firewall - A script to restore a saved firewall on startup.
cat /etc/network/firewall | iptables-restore

The firewall settings file /etc/network/firewall was created using the command:

iptables-save > /etc/network/firewall

after I set up a working firewall using iptables. (See man iptables). This file currently looks like this.

:INPUT ACCEPT [1745:1484069]
:OUTPUT ACCEPT [1763:303728]
:DROPCHAIN - [0:0]
-A INPUT -s -p tcp -j ACCEPT
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j LOGCONNECT

This stops any forwarding and any attempts to form incoming tcp connections. Any attempts to open a connection are logged via kernel logging.

I couldn’t find an easy way to filter just these messages into a file (short off piping all appropriate messages to a process in /etc/syslogd.conf that only keeps iptables messages… though I’m sure there is a clever way of doing this).

Also, I probably should be paying attention to rpc udp packets… or something like that.

Shout at me if there is some better way of doing this.

April 1, 2008 at 11:22 pm Leave a comment

April 2008
« Mar   May »