ansaphone.el 7.93 KB
Newer Older
1
2
;;;;;
;;;;; $Id$
3
;;;;; Copyright (C) 1991, 1996  Lysator Academic Computer Association.
4
5
6
7
8
;;;;;
;;;;; This file is part of the LysKOM server.
;;;;; 
;;;;; LysKOM is free software; you can redistribute it and/or modify it
;;;;; under the terms of the GNU General Public License as published by 
9
;;;;; the Free Software Foundation; either version 2, or (at your option) 
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
;;;;; any later version.
;;;;; 
;;;;; LysKOM 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 LysKOM; see the file COPYING.  If not, write to
;;;;; Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
;;;;; or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
;;;;; MA 02139, USA.
;;;;;
;;;;; Please mail bug reports to bug-lyskom@lysator.liu.se. 
;;;;;
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: ansaphone.el
;;;; Author: David Byers
;;;;
;;;; This file implements the auto-reply facility.
;;;; It must be loaded after messages.el
;;;;

David Kågedal's avatar
David Kågedal committed
35
36
37
38
39
40
41
42
43
(eval-when-compile
  (require 'lyskom-vars "vars")
  (require 'lyskom-macros "macros")
  (require 'lyskom-command "command")
  (require 'lyskom-language "language")
  (require 'lyskom-messages "messages")
  (require 'lyskom-services "services"))


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(setq lyskom-clientversion-long 
      (concat lyskom-clientversion-long
	      "$Id$\n"))

(defconst lyskom-ansaphone-tag "Auto-reply:\n")


;;;============================================================
;;;
;;; User functions
;;;

(def-kom-command kom-change-auto-reply (&optional message)
  "Change the default automatic reply message."
  (interactive)
  (let ((message (or message
                    (read-from-minibuffer 
David Byers's avatar
David Byers committed
61
62
                     (lyskom-get-string 'ansaphone-new-message))))
        (lyskom-last-text-format-flags nil))
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
    (setq kom-ansaphone-default-reply message)
    (lyskom-format-insert (lyskom-get-string 'ansaphone-message)
                          kom-ansaphone-default-reply)))


(def-kom-command kom-toggle-auto-reply ()
  "Toggle automatic replies to personal messages."
  (interactive)
  (setq kom-ansaphone-on (not kom-ansaphone-on))
  (lyskom-format-insert (lyskom-get-string 'ansaphone-state-r)
                        (lyskom-get-string (if kom-ansaphone-on
                                               'state-on
                                             'state-off)))
  (if kom-ansaphone-on
      (progn
        (setq lyskom-ansaphone-when-set (current-time-string))
        (lyskom-format-insert (lyskom-get-string 'ansaphone-message)
                              kom-ansaphone-default-reply))))


(def-kom-command kom-list-messages ()
  "List collected messages"
  (interactive)
  (if (null lyskom-ansaphone-messages)
      (lyskom-format-insert (lyskom-get-string 'ansaphone-no-messages))
    (progn
      (lyskom-format-insert (lyskom-get-string 'ansaphone-message-list-start))
      (mapcar (function
               (lambda (msg)
                 (lyskom-show-personal-message 
                  (blocking-do 'get-conf-stat (elt msg 0))
                  (blocking-do 'get-conf-stat (elt msg 1))
                  (elt msg 2)
                  (elt msg 3)
                  'nobeep)))
David Kågedal's avatar
David Kågedal committed
98
              (reverse lyskom-ansaphone-messages))
99
100
101
102
103
104
      (lyskom-format-insert (lyskom-get-string 'ansaphone-message-list-end)))))


(def-kom-command kom-erase-messages ()
  "Erase collected messages"
  (interactive)
105
  (lyskom-message "%s" (lyskom-get-string 'ansaphone-messages-gone))
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
  (setq lyskom-ansaphone-messages nil))



(defun lyskom-ansaphone-send-message (recipient message)
  (initiate-send-message 'async 
                         nil
                         (if (numberp recipient)
                             recipient
                           (conf-stat->conf-no recipient))
                         (concat lyskom-ansaphone-tag
                                 message)))



(defun lyskom-ansaphone-message-handler (message-type sender recipient text)
  "Personal message handler.

Automatically reply to certain personal messages and strip auto-reply
identification from messages.

See kom-ansaphone-on"

  (let ((is-automatic (eq 0 (string-match lyskom-ansaphone-tag text))))
    (if is-automatic
        (progn
          (string-match (concat "^"
                                lyskom-ansaphone-tag 
                                "\\(\\(.\\|\n\\)*\\)") text)
          (lyskom-set-current-message-text (substring text
                                                      (match-beginning 1)
                                                      (match-end 1)))))
138
139
140
141
142

    ;;
    ;; See if we want to reply to this message
    ;;

143
144
    (if (and kom-ansaphone-on
             sender
David Byers's avatar
David Byers committed
145
	     recipient
146
147
148
             (not is-automatic))
        (let ((reply (lyskom-ansaphone-find-reply 
                      message-type
David Byers's avatar
David Byers committed
149
150
		      (conf-stat->conf-no sender)
                      (cond ((numberp recipient) recipient)
David Kågedal's avatar
David Kågedal committed
151
			    (t (conf-stat->conf-no recipient)))
152
153
154
155
156
157
158
159
160
161
                      text)))
          (if (and reply (elt reply 4))
              (progn
                (setq reply
                      (concat 
                       (lyskom-format
                        (lyskom-get-string 'ansaphone-message-header)
                        lyskom-ansaphone-when-set)
                       (elt reply 4)))
                (lyskom-ansaphone-send-message sender reply)))))
162
163
164
165
166
167
168
169

    ;;
    ;; See if we want to record this message
    ;;

    (if (and kom-ansaphone-on 
             kom-ansaphone-record-messages
             sender)
170
171
172
        (lyskom-ansaphone-record-message sender
                                         recipient
                                         lyskom-message-current-text)))
173
174
175
176
177
178
179
180

  ;;
  ;; Perhaps we want to show the message, perhaps not
  ;;
  
  (if kom-ansaphone-on
      (not kom-ansaphone-show-messages)
    nil))
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



(defun lyskom-ansaphone-find-reply (message-type sender recipient text)
  "Find an automatic reply suitable for messages of type MESSAGE-TYPE from
SENDER to RECIPIENT consisting of TEXT. See the documentation for
kom-ansaphone-default-reply and kom-ansaphone-replies."
  (let ((exprs kom-ansaphone-replies)
        (result nil))
    (while exprs
      (if (and (or (null (elt (car exprs) 0))
                   (eq (elt (car exprs) 0) message-type))
               (or (null (elt (car exprs) 1))
                   (eq (elt (car exprs) 1) sender)
                   (and (listp (elt (car exprs) 1))
                        (memq sender (elt (car exprs) 1))))
               (or (null (elt (car exprs) 2))
                   (eq (elt (car exprs) 2) recipient)
                   (and (listp (elt (car exprs) 2))
                        (memq recipient (elt (car exprs) 2))))
               (or (null (elt (car exprs) 3))
                   (string-match (elt (car exprs) 3) text)))
          (progn
            (setq result (car exprs))
            (setq exprs nil)))
      (setq exprs (cdr-safe exprs)))
    (or result (and (eq message-type 'personal)
                    (list nil nil nil nil kom-ansaphone-default-reply)))))
        

(defun lyskom-ansaphone-record-message (sender recipient text)
  (if (not (numberp sender))
      (setq sender (conf-stat->conf-no sender)))
  (if (not (numberp recipient))
      (setq recipient (conf-stat->conf-no recipient)))
  (setq lyskom-ansaphone-messages (cons (list sender recipient text
                                              (current-time-string))
                                        lyskom-ansaphone-messages)))



(lyskom-add-personal-message-handler 'lyskom-ansaphone-message-handler
                                     'before
                                     nil
                                     t)


228
(eval-and-compile (provide 'lyskom-ansaphone))
David Kågedal's avatar
David Kågedal committed
229
230

;;; ansaphone.el ends here