Gnus is a mail user agent (MUA) and newsreader written in Emacs Lisp. It is famous for being very configurable, and for being one of the few MUAs that come with a decent editor to compose your messages (by virtue of actually running inside Emacs). I have switched between Gnus and Thunderbird as my primary MUA several times over the years and recently switched to Gnus again. This time, however, I made an additional resolution: should I encounter any problems or rough edges I would attempt to debug and fix them (instead of grudgingly living with them and eventually switching MUAs again).

I already had some superficial experience with Emacs Lisp, and this seemed like a great opportunity to do something more complicated with it. Not surprisingly, I quickly discovered things I disliked. To my delight, I also managed to fix several issues and got the changes incorporated upstream.

However, the predominant feeling that I had when hacking Gnus was one of disappointment. I always wondered how you could actually write something as complicated as a MUA in a language designed to facilitate text editing. After looking at the source, my answer to that question is "badly". There are no clever techniques or powerful language features I wasn't aware of, instead everything looks more hacky and fragile than I ever thought possible for a project with the size, history and reputation of Gnus.

Turning an editor into a MUA I think I was always assuming that Gnus was essentially implemented like any other MUA - only using Emacs buffers to render the user interface. This turns out not to be the case. Instead, Gnus attempts to cast most of the tasks that a MUA has to perform into something that looks like text editing, and then uses Emacs text editing functions to accomplish the task. This may not seem like such a terrible idea at first, but here are some examples of what this means in practice: There are no complex data structures to pass information from one function to another. Instead, information is passed in a dedicated Emacs buffer. Sometimes the buffer is allocated dynamically, but sometimes even with a fixed name (giving the term "global variable" a whole new meaning). I would expect that e.g. a function that fetches headers from an IMAP server would return some sort of array of structs but instead it actually writes them into buffer in RFC822 format. The caller then reads this buffer and parses the context.

Basically any sort of parsing is done in an ad-hoc fashion using lots of regular expressions (potentially inside a while loop). This includes both the parsing of the buffers that are used for communication between Gnus functions, and the parsing of standardized protocols that are used with remote servers. Take, for example, the parsing of the IMAP FETCH response. The format is specified in RFC 3501, section 7.4.2. Instead of writing a proper parser that would e.g. ensure that parentheses are properly nested and would return some structured object, the way Gnus determines the properties of a message is to first move to the beginning of the line, do a regular expression search for the name of the property (e.g. RFC822.SIZE followed by some numbers), and then take the first match. After this, the cursor (remember, everything happens in a buffer) is moved to the beginning of the line and the procedure starts again for the next property. There is absolutely zero awareness of nesting levels or a distinction between keys and values. This sort of parsing appears extremely fragile. I'm pretty sure that something like an email with content type text/RFC822.SIZE 47 would confuse the hell out of Gnus. It doesn't even take a malicious sender: one of the bugs that I fixed was the implicit assumption that in the FETCH response of an IMAP server the UID always comes before the size - because the "parser" forgot to go back to the beginning of the line after it was looking for the UID.

There are no good means to signal errors. Basically, if there is any sort of problem (e.g. the SMTP server refuses to accept a message) there are only two choices: write a message into the minibuffer (that the user may or may not see, and that's overwritten as soon as any other Emacs components wants to say something), or signal a lisp error. Unfortunately Lisp errors are (or are used this way in Gnus) completely unstructured, so they are often caught and suppressed indistinctly by higher level code. There are various examples for this: did you specify an invalid expiry target? Messages will just silently not expire. Did you specify the wrong credentials for your IMAP server? You will only get a brief message in the minibuffer. Is the server refusing to delete or store an IMAP message? You may or may get a brief message in the minibuffer, but Gnus will otherwise appear to have successfully executed the command.

There is very little error detection, and lots of chances for errors to occur and propagate. Since data is passed in form of buffers, functions can receive and return completely unstructured, arbitrary values. In some cases, the expected format of the buffer is documented in the comments. In other cases, it is completely undocumented and has to be deduced by looking at all the participating functions. But only very rarely is the structure of the data in actually reflected in the datatype of a function's arguments.

Even worse, most functions also do not make any attempt to validate the buffers they work with. If they happen to contain garbage, the function will happily work with it. And since parsing of the buffer is done with some ad-hoc regular expression, chances are that the function will even be able "process" the buffer and obtain a result that it passes on (but which is just garbage).

The Gnus code is full of arcane workarounds. For example, "select methods" (which is Gnus name for backends that implement different protocols) can share common code. Normally, this would be implemented by making backends into classes that can inherit from each other. Gnus, however, was written before Emacs Lisp had support for object-oriented programming and thus comes with its own runtime emulation of classes. In other words, there is code that dynamically renames your variables and functions to make it appear they're part of multiple backends (look at nnoo.el for some code that will blow your head). UPDATE: there has just been a decision to move Gnus development into the GNU Emacs Git repository and scrap support for anything but the most-recent GNU Emacs version. Nice!.