dot/ercrc.el: This is the modern way to configure autojoin.
[profile] / dot / ercrc.el
1 ;;; -*-emacs-lisp-*-
2 ;;;
3 ;;; ERC configuration
4
5 (setq erc-nick "mdw"
6 erc-user-full-name "Mark Wooding")
7
8 (if (not (memq 'truncate erc-modules))
9 (setq erc-modules (cons 'truncate erc-modules)))
10
11 (setq erc-fill-column 76
12 erc-timestamp-right-column 68
13 erc-fill-prefix " "
14 erc-max-buffer-size (* 60 3000))
15
16 (load "~/.erc-local.el")
17
18 (setq erc-track-exclude-types '("NICK" "JOIN" "PART"))
19
20 (setq erc-auto-query 'buffer)
21
22 (defun mdw-erc-turn-off-truncate-lines ()
23 (setq truncate-lines nil
24 truncate-partial-with-windows nil
25 word-wrap t
26 wrap-prefix (concat (propertize " " 'face 'erc-prompt-face)
27 " ")))
28 (add-hook 'erc-mode-hook 'mdw-erc-turn-off-truncate-lines)
29
30 (setq erc-autojoin t
31 erc-autojoin-domain-only nil
32 erc-autojoin-channels-alist
33 '(("chiark.greenend.org.uk" "#chiark")
34 ("irc.distorted.org.uk" "#distorted" "#jukebox")
35 ("irc.hstg.corp.good.com" "#hstg")))
36 (erc-autojoin-mode 1)
37
38 (defvar mdw-erc-auto-greet-bots-alist nil
39 "*Alist of (SERVER-REGEXP BOT-NICK MESSAGE-FORM).
40 Evaluate MESSAGE-FORM and sent to BOT-NICK when connected to a server which
41 matches SERVER-REGEXP.")
42
43 (defvar mdw-erc-ircop-alist nil
44 "*Alist of (SERVER-REGEXP ACCT PASSWD).
45 Login details for claiming server admin rights.")
46
47 (defun mdw-remprop-nondestructive (indic plist)
48 "Return a plist like PLIST, only without the first entry for INDIC.
49 The PLIST is not itself modified."
50 (if (getf plist indic)
51 (let* ((head (cons nil nil))
52 (tail head))
53 (while (and plist (not (eq (car plist) indic)))
54 (let* ((i (pop plist)) (v (pop plist))
55 (vv (cons v nil)) (ii (cons i vv)))
56 (rplacd tail ii)
57 (setq tail vv)))
58 (rplacd tail (cddr plist))
59 (cdr head))
60 plist))
61
62 (defun* mdw-cons-replace
63 (item list &rest keys &key (key '#'identity) &allow-other-keys)
64 "Return LIST, with ITEM at the start, replacing any existing matching item.
65 Specifically, any item in the list satisfying the test are removed
66 \(nondestructively), and then the new ITEM is added to the front."
67 (cons item (apply #'remove* (funcall key item) list :key key
68 (mdw-remprop-nondestructive :key keys))))
69
70 (defmacro* mdw-pushnew-replace (item place &rest keys)
71 "Add ITEM to the list PLACE, replacing any existing matching item.
72 Specifically, any item in the list satisfying the test are removed
73 \(nondestructively), and then the new ITEM is added to the front.
74
75 Evaluation order for the keywords is a bit screwy: don't rely on it."
76 (cond ((fboundp 'cl-callf2)
77 `(cl-callf2 mdw-cons-replace ,item ,place ,@keys))
78 ((fboundp 'cl-setf-do-modify)
79 ;; `cl-setf-do-modify' returns a list (LETS STORE FETCH).
80 (let ((setf-things (cl-setf-do-modify place (cons 'list keys))))
81 `(let (,@(car setf-things))
82 ,(cl-setf-do-store (cadr setf-things)
83 `(mdw-cons-replace ,item ,place
84 ,@keys)))))
85 (t (error "Don't know how to hack places on this Emacs."))))
86
87 (defun mdw-define-bot-greeting (server bot greeting)
88 "Define a new bot greeting."
89 (mdw-pushnew-replace (list server bot greeting)
90 mdw-erc-auto-greet-bots-alist
91 :test #'string= :key #'car))
92 (defun mdw-add-ircop-credentials (server acct passwd)
93 "Define a new set of `ircop' credentials."
94 (mdw-pushnew-replace (list server acct passwd)
95 mdw-erc-ircop-alist
96 :test #'string= :key #'car))
97 (load "~/.erc-auth.el")
98
99 (defun mdw-assoc-regexp (regexp alist)
100 "Return the association in ALIST whose car matches REGEXP."
101 (let ((answer nil))
102 (dolist (l alist)
103 (when (string-match (car l) regexp)
104 (setq answer l)))
105 answer))
106
107 (defun mdw-erc-auto-greet-bots (server nick)
108 "Send greeting message to bots."
109 (let ((a (mdw-assoc-regexp server mdw-erc-auto-greet-bots-alist)))
110 (when a
111 (let ((bot (cadr a))
112 (message (caddr a)))
113 (erc-server-send (concat "PRIVMSG " bot " :" message))))))
114 (add-hook 'erc-after-connect 'mdw-erc-auto-greet-bots)
115
116 (defun erc-cmd-GREET ()
117 "Send greeting messages, according to `mdw-erc-auto-greet-bots-alist'."
118 (mdw-erc-auto-greet-bots erc-session-server (erc-current-nick)))
119
120 (defun erc-cmd-IRCOP ()
121 "Claim `ircop' privileges."
122 (let ((a (mdw-assoc-regexp erc-session-server mdw-erc-ircop-alist)))
123 (when a
124 (let ((acct (cadr a))
125 (passwd (caddr a)))
126 (erc-server-send (concat "OPER " acct " " passwd))))))