Instant Message Audio for GNU Emacs and ERC

I use GNU Emacs and ERC as my IRC client. I also use ERC with various instant messaging services via BitlBee.

Because I use several virtual desktops on my workstation and am not always looking at the screen with Emacs on it, I need a way to be notified when someone sends me an instant message. However, I don't want to hear any audio in normal IRC channels. I came up with the following Emacs Lisp code to accomplish this task.

;; Define a custom function
(defun erc-custom-sounds (str)
  "Play sound files when str matches certain conditions"
  ;; Play a logout sound when someone leaves the &bitlbee channel
  (when (string-match " has quit: Leaving..." str)
    ;; Play logout.wav
    (start-process "aucat-process" "aucat" "aucat" "/home/ejm/share/sounds/logout.wav"))
  ;; Play a login sound when someone enters the &bitlbee channel
  (when (string-match " has joined channel &bitlbee" str)
    ;; Play login.wav file
    (start-process "aucat-process" "aucat" "aucat" "/home/ejm/share/sounds/login.wav"))
  ;; Don't play any audio when the "bitlbee" user sends a message
  ;; Also don't play any audio when in a normal IRC channel
  (unless (or (string-match "" str) (string-match "#" (buffer-name)))
    ;; Play an audio sound when someone types a message
    (when (string-match "<[a-zA-Z0-9]+>" str)
      ;; Play receive.wav file
      (start-process "aucat-process" "aucat" "aucat" "/home/ejm/share/sounds/receive.wav"))))
;; Insert the custom function into ERC
(add-hook 'erc-insert-pre-hook 'erc-custom-sounds)
(add-hook 'erc-send-pre-hook 'erc-custom-sounds)

Note: I use OpenBSD as my primary workstation operating system. aucat is an OpenBSD utility that writes audio files to the default audio device. Any similar utility from other operating systems can be substituted.

Last modified: Sunday, 04-May-2008 17:47:46 MDT :: Erik Mugele :: Main Page :: Contact Page :: Powered by Teuton