Monday, September 29, 2008

Emacs Hidden Save

One thing about emacs that annoys me is its default behavior for backing up files. I don't have anything against generally backing up files, but I am annoyed that any directory I work heavily in doubles in size because of the backup files.

I've seen a number of different solutions for this little annoyance, ranging from eliminating backups entirely to creating an entire shadow directory structure for backups. The closest thing I've seen to what I like, though, is passing an option to ls that excludes backup files.

Although excluding files ending in ~ works fairly well, if someone else is browsing through my filesystem that doesn't know to exclude these files will still be annoyed by them. A more general solution would be to prepend . to all of the file names so that they'd be hidden for everybody. Now, this isn't a perfect solution as backups of hidden files are still visible, but I feel this is a more acceptable degree of loss than visitors having problems. (Note: this program is covered under the GPL)

;;; Copyright (c) 2008, Colin Williams
;;; All rights reserved

;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.

;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.

;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see .

(setq make-backup-file-name-function (lambda (file)
           (concat (file-name-directory file)
            "."
            (file-name-nondirectory file)
            "~")))

(defun backup-file-name-p (file)
  "Return non-nil if FILE is a backup file name (numeric or not).
This is a separate function so you can redefine it for customization.
You may need to redefine `file-name-sans-versions' as well."
  (string-match "^\\..*~$" (file-name-nondirectory file)))

;; Much of this function was based off of file-name-sans-version
;; in files.el, the source and credit for this package is located
;; at 
(defun file-name-sans-versions (name &optional keep-backup-version)
  "Return file NAME sans backup versions or strings.
This is a separate procedure so your site-init or startup file can
redefine it.
If the optional argument KEEP-BACKUP-VERSION is non-nil,
we do not remove backup version numbers, only true file version numbers."
  (let ((handler (find-file-name-handler name 'file-name-sans-versions)))
    (if handler
 (funcall handler 'file-name-sans-versions name keep-backup-version)
      (if (eq system-type 'vax-vms)
   ;; VMS version number is (a) semicolon, optional
   ;; sign, zero or more digits or (b) period, option
   ;; sign, zero or more digits, provided this is the
   ;; second period encountered outside of the
   ;; device/directory part of the file name.
   (substring name 0
       (or (string-match ";[-+]?[0-9]*\\'" name)
    (if (string-match "\\.[^]>:]*\\(\\.[-+]?[0-9]*\\)\\'"
        name)
        (match-beginning 1))
    (length name)))
 (if keep-backup-version
     name
   (let ((file-name (file-name-nondirectory name))
  (directory-name (file-name-directory name)))
     (concat directory-name (let* ((file-name-end
        (or (string-match "\\.~[0-9.]+~\\'"
            file-name)
            (string-match "~\\'"
            file-name)
            (length file-name)))
       (file-name-start
        (if (= file-name-end
        (length file-name))
            0
          (string-match "^\\." file-name))))

         (substring file-name
      file-name-start
      (if (= file-name-start 0)
          (length file-name)
        file-name-end))))))))))
      

Ironically, the code for actually creating the filename was the easy part. The tricky bits were the support functions backup-file-name-p and file-name-sans-versions. I've been using this scheme on my desktop for a while without any problems, if anybody has suggestions for improving this scheme please let me know.

Wednesday, January 16, 2008

Six Degrees of Wikipedia Automation

Well, it's been a while since my last post, been waiting till I got the six degrees of wikipedia stuff put together. There are basically 5 things that I had to do to get the automation in working order.

  • Create a blog that could be updated via email
  • Create an email address to administer the game through
  • Setup mutt
  • Write a new revision of the generation script
  • Add a line to my crontab

The first step was one of the easier ones. Blogger fits the bill pretty well, letting me create an email address that anything mailed to it will be posted.

The second step was even easier, gmail fit the bill perfectly. The only real parameters I had were that it supported pop3 or imap and smtp.

Mutt is a text-based mail client which can send emails automatically when text is received from STDIN. Although simple in concept, I had a bitch of a time getting it set up correctly. First I found a simple configuration file and made a minor tweak.

set imap_user = "sixdegreesofwikipedia@gmail.com"
set imap_pass = "*******"

set smtp_url = "smtp://sixdegreesofwikipedia@smtp.gmail.com:587/"
set smtp_pass = "*******"
set from = "sixdegreesofwikipedia@gmail.com"
set realname = "Six Degrees of Wikipedia"
set content_type = "text/html"

set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set record="+[Gmail]/Sent Mail"
set postponed="+[Gmail]/Drafts"

set header_cache="~/.mutt/cache/headers"
set message_cachedir="~/.mutt/cache/bodies"
set certificate_file=~/.mutt/certificates

set move = no

set sort = 'threads'
set sort_aux = 'last-date-received'

ignore "Authentication-Results:"
ignore "DomainKey-Signature:"
ignore "DKIM-Signature:"
hdr_order Date From To Cc Content-Type

You'll note that the only real tweak I had to make was set content_type = "text/html". This was necessary because if the content_type of the email were "text/plain" then blogger would replace all of my angle brackets with < and > and change the urls into hyperlinks. Figuring out how to set the content-type was very poorly documented and I only really found out because I went onto the irc channel and asked the developers directly. It is a little annoying and not at all obvious that the way to set 'Content-Type' is to set the variable 'content_type'.

Once I got the content-type set correctly, I found out that revision 1.5.17-1 had an incredibly annoying bug where it segfaulted whenever you try to send an email using stdin. I was able to sync to revision 1.5.17-2, but until that's put into the debian testing branch I have to keep an eye on that file in my apt repository.

There were two problems with the wikipedia script:

  • wget creates a file whenever it's run, which was polluting my file system
  • the name in the anchor tag isn't a true title

The first problem was easy, a simple rm took care of it fairly directly. Before I removed the file, though, I was able to extract the title tag from the file that was created.

#!/bin/bash

echo "This round's challenge:<br>"
for ((i=0;i<2;++i))
do
    wgetout=($(wget -nv http://en.wikipedia.org/wiki/Special:Random 2>&1 \
      | sed 's/^.* URL:\([^ ]*\) .* "\([^\"]*\)".*$/\1 \2/'<br>))
    echo -n "<a href='${wgetout[0]}'>"
    sed -n "/<title>.*<\/title>/s/.*<title>\(.*\) - Wikipedia, the free encyclopedia<\/title>.*/\1/p" ${wgetout[1]}
    echo "</a><br>"
    rm ${wgetout[1]}
done

One final piece that I needed was to keep track of the round number. To this end, the file ~/round holds the number of the next round, and is incremented in the crontab line.

0 0 * * 1,3,5 round=$(cat ~/round);/home/sixdegreesofwikipedia/bin/generategame | mutt *******@blogger.com -s "Round $round";echo $(($round + 1)) > ~/round

Although this represents the work required to get a new game generated and posted every couple days, posting the victor from the previous day isn't complete. My friend Mike has written a script to verify that a list of links is correct, and I need to write a script that collects all of the mail for a single game together and passes that information into his script. In the meantime, you can play the generated games at http://sixdegreesofwikipedia.blogspot.com/