forked from kmroz/rmoo
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rmoo-rmail.el
268 lines (232 loc) · 7.53 KB
/
rmoo-rmail.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
;;;;
;;;; Majors modes for dealing with MOO Mail
;;;;
;;;; Original Author: Ron Tapia <[email protected]>
;;;; Revised by: mattcamp
;;
;; This file gives MOO worlds a new property.
;;
;; rmoo-rmail-lists-buffer
;;
;;
;; Hook variables
;;
(defvar rmoo-rmail-list-mode-hooks nil "Hooks to run when MOO RMail List mode starts.")
(defvar rmoo-rmail-summary-mode-hooks nil "Hooks to run when MOO RMail Summary mode starts.")
(defvar rmoo-rmail-message-mode-hooks nil "Hooks to run when MOO RMail Message mode starts.")
(defvar rmoo-rmail-window-list nil)
(defvar rmoo-rmail-message-regexp "^\\([^ :]+\\): .*$")
;;
;; Keymaps
;;
(defvar rmoo-rmail-summary-mode-map
(let ((map (make-sparse-keymap)))
(define-key map " " 'rmoo-rmail-get-message)
(define-key map "l" 'rmoo-rmail-switch-to-lists-buffer)
(define-key map "s" 'scroll-other-window)
map)
"Map for use with MOO RMail Summary Mode.")
(defvar rmoo-rmail-list-mode-map
(let ((map (make-sparse-keymap)))
(define-key map " " 'rmoo-rmail-list-get-mail)
(define-key map "\^m" 'rmoo-rmail-list-get-mail-in-sequence)
map)
"Map for use with MOO RMail List Mode.")
(defvar rmoo-rmail-message-mode-map
(let ((map (make-sparse-keymap)))
map)
"Map for use with MOO RMail Message Mode.")
;;
;; Major modes
;;
(defun rmoo-rmail-list-mode ()
"Major mode for looking at MOO Recipients.
Commands:
\\{rmoo-rmail-list-mode-map}"
(interactive)
(kill-all-local-variables)
(setq mode-name "RMOO-RMail-Lists")
(setq major-mode 'rmoo-rmail-list-mode)
(use-local-map (copy-keymap rmoo-rmail-list-mode-map))
(run-hooks rmoo-rmail-list-mode-hooks))
(defun rmoo-rmail-summary-mode ()
(interactive)
(kill-all-local-variables)
(setq mode-name "RMOO-RMail-Summary")
(setq major-mode 'rmoo-rmail-summary-mode)
(use-local-map (copy-keymap rmoo-rmail-summary-mode-map))
(run-hooks rmoo-rmail-summary-mode-hooks))
(defun rmoo-rmail-message-mode ()
(interactive)
(kill-all-local-variables)
(setq mode-name "RMOO-RMail-Message")
(setq major-mode 'rmoo-rmail-message-mode)
(use-local-map (copy-keymap rmoo-rmail-message-mode-map))
(run-hooks rmoo-rmail-message-mode-hooks))
;;
;; Functions that request that the MOO send us mail thingys.
;;
(defun rmoo-rmail ()
(interactive)
(rmoo-send-here rmoo-rmail-get-lists-command))
(defvar rmoo-rmail-get-lists-command "@mcp-subscribed" "Command to download a list of MOO mail recipients.")
(defvar rmoo-rmail-list-get-mail-command "@mcp-mail")
(defvar rmoo-rmail-get-message-command "@mcp-get-message")
(defun rmoo-rmail-get-message ()
(interactive)
(let ((message (rmoo-rmail-find-message)))
(rmoo-send-here (concat rmoo-rmail-get-message-command
" "
message
" on "
rmoo-recipient))))
(defun rmoo-rmail-list-get-mail ()
(interactive)
(let ((rmoo-list (rmoo-rmail-list-find-list)))
(rmoo-send-here (concat rmoo-rmail-list-get-mail-command
" new on "
rmoo-list))))
(defun rmoo-rmail-list-get-mail-in-sequence (seq)
(interactive (list (rmoo-rmail-request-sequence)))
(let ((rmoo-list (rmoo-rmail-list-find-list)))
(rmoo-send-here (concat rmoo-rmail-list-get-mail-command
" "
seq
" on "
rmoo-list))))
;;;;
;;;; Interface with RMOO-MCP
;;;;
;;
;; Register the request types
;;
(rmoo-mcp-register "display-mail-lists"
'()
'rmoo-rmail-display-lists
"1.0"
"1.0"
nil)
(rmoo-mcp-register "display-mail-summary"
'(("list" . 'required))
'rmoo-rmail-display-summary
"1.0"
"1.0"
nil)
(rmoo-mcp-register "display-mail-message"
'(("list" . 'required)
("message-number" . 'required))
'rmoo-rmail-display-message
"1.0"
"1.0"
nil)
;;
;; Functions called when the appropriate request comes through.
;;
(defvar rmoo-recipient "")
(make-variable-buffer-local 'rmoo-recipient)
(defun rmoo-rmail-display-summary (list-name)
(let ((buf (current-buffer))
(world rmoo-world-here))
(set-buffer (get-buffer-create (concat "Summary For "
list-name)))
(if buffer-read-only
(toggle-read-only))
(setq rmoo-world-here world)
(rmoo-take-control-of-output 'rmoo-mcp-output-function)
(put world 'output-buffer (current-buffer))
(put world 'last-output-buffer buf)
(erase-buffer)
(put world 'goto-buffer (current-buffer))
(setq rmoo-recipient list-name)
(setq rmoo-mcp-cleanup-function 'rmoo-rmail-display-summary-cleanup)
(set-buffer buf)))
(defun rmoo-rmail-display-message (list-name message-number)
(let ((buf (current-buffer))
(world rmoo-world-here))
(set-buffer (get-buffer-create (concat "Msg: "
message-number
" on "
list-name)))
(if buffer-read-only
(toggle-read-only))
(setq rmoo-world-here world)
(rmoo-take-control-of-output 'rmoo-mcp-output-function)
(put world 'output-buffer (current-buffer))
(put world 'last-output-buffer buf)
(erase-buffer)
(setq rmoo-recipient list-name)
(setq rmoo-mcp-cleanup-function 'rmoo-rmail-display-message-cleanup)
(set-buffer buf)))
(defun rmoo-rmail-display-lists ()
(let ((buf (current-buffer))
(world rmoo-world-here))
(set-buffer (get-buffer-create (concat "Lists on "
(symbol-name world))))
(put world 'rmoo-rmail-lists-buffer (current-buffer))
(if buffer-read-only
(toggle-read-only))
(setq rmoo-world-here world)
(rmoo-take-control-of-output 'rmoo-mcp-output-function)
(put world 'output-buffer (current-buffer))
(put world 'last-output-buffer buf)
(erase-buffer)
(setq rmoo-mcp-cleanup-function 'rmoo-rmail-display-lists-cleanup)
(set-buffer buf)))
;;
;; Cleanup functions
;;
(defun rmoo-rmail-display-lists-cleanup ()
(let ((world rmoo-world-here))
(rmoo-rmail-list-mode)
(setq rmoo-world-here world)
(setq buffer-read-only t)
(put world 'goto-buffer (current-buffer))
(if (string= window-system "x")
(put world 'goto-function 'switch-to-buffer-other-frame)
(put world 'goto-function 'switch-to-buffer-other-window))
(setq rmoo-rmail-window-list (cons (list rmoo-world-here
(get-buffer-window
(current-buffer)))
rmoo-rmail-window-list))))
(defun rmoo-rmail-display-summary-cleanup ()
(let ((world rmoo-world-here)
(recip rmoo-recipient))
(rmoo-rmail-summary-mode)
(setq rmoo-world-here world)
(setq rmoo-recipient recip)
(setq buffer-read-only t)
(put rmoo-world-here 'goto-function 'rmoo-rmail-summary-goto-function)
(put world 'goto-buffer (current-buffer))))
(defun rmoo-rmail-summary-goto-function (buf)
(switch-to-buffer buf)
(set-buffer buf)
(message (concat "Current buffer: " (prin1-to-string (current-buffer)))))
(defun rmoo-rmail-display-message-cleanup ()
(let ((world rmoo-world-here)
(recip rmoo-recipient))
(rmoo-rmail-message-mode)
(setq rmoo-world-here world)
(setq rmoo-recipient recip)
(setq buffer-read-only t)
(display-buffer (current-buffer) t)))
;;
;; Some utility functions
;;
(defun rmoo-rmail-request-sequence ()
(read-string "Message sequence: "))
(defvar rmoo-rmail-list-regexp "^ *\\([^ ]+\\).*$")
(defun rmoo-rmail-list-find-list ()
(save-excursion
(beginning-of-line)
(if (looking-at rmoo-rmail-list-regexp)
(buffer-substring (match-beginning 1) (match-end 1))
"me")))
(defun rmoo-rmail-find-message ()
(save-excursion
(beginning-of-line)
(if (looking-at rmoo-rmail-message-regexp)
(buffer-substring (match-beginning 1) (match-end 1))
"$")))
(defun rmoo-rmail-switch-to-lists-buffer ()
(interactive)
(switch-to-buffer (get rmoo-world-here 'rmoo-rmail-lists-buffer)))