async.el 18.1 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
2
;;;;;
;;;;; $Id$
3
;;;;; Copyright (C) 1991, 1996  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
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) 
Linus Tolke Y's avatar
Linus Tolke Y committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;;;;; 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. 
;;;;;
Per Cederqvist's avatar
.    
Per Cederqvist committed
25
26
27
28
29
30
31
32
33
34
35
36
37
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: async.el
;;;;
;;;; These functions implement a nice service that give the user
;;;; continuous messages about what other users are doing and what
;;;; is happening inside the lyskom server.
;;;;
;;;; Author: Linus Tolke
;;;; Entry:  Inge Wallin
;;;;

38
39
40
41
(setq lyskom-clientversion-long 
      (concat lyskom-clientversion-long
	      "$Id$\n"))

Per Cederqvist's avatar
.    
Per Cederqvist committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

(defun lyskom-parse-async (tokens buffer)
  "Parse an asynchronous message from the server.
The message consists of TOKENS tokens. Unknown messages are skipped.
Actions are taken to perform the various tasks that is required on reciept of
an asynchronous message.
If variable kom-presence-messages is non-nil or some minibuffer editing is 
going on then nothing is printed on the message area.
This function is called with the lyskom-unparsed-buffer as current-buffer.
All calls using the lyskom-variables have to be made using the buffer BUFFER.

Be careful when editing this. All parsing is done with the buffer this
function is called with as the current-buffer, while all calls from
this function shall be with current-buffer the BUFFER."
  (let ((msg-no (lyskom-parse-num)))
    (cond
     ((eq msg-no 0)			; New text
      (let* ((text-no (lyskom-parse-num))
	     (text-stat (lyskom-parse-text-stat text-no)))
	(lyskom-save-excursion
62
63
	 (set-buffer buffer)
	 (lyskom-async-new-text text-stat)))) ;
Per Cederqvist's avatar
.    
Per Cederqvist committed
64
65

     ((eq msg-no 1)			; Logout (obsolete)
66
      (lyskom-skip-tokens tokens))
Per Cederqvist's avatar
.    
Per Cederqvist committed
67
68
69
70
71
72
73
74
75
76

     ((eq msg-no 2)			; Login, obsolete.
      (lyskom-skip-tokens tokens))

     ((eq msg-no 3)			; Conference deleted
      (lyskom-skip-tokens tokens))

     ((eq msg-no 4)			; Conference created
      (lyskom-skip-tokens tokens))

77
78
     ((eq msg-no 5)			; A person or conference has
					; changed name.
Per Cederqvist's avatar
.    
Per Cederqvist committed
79
80
81
82
      (let ((conf-no (lyskom-parse-num))
	    (old-name (lyskom-parse-string))
	    (new-name (lyskom-parse-string)))
	(lyskom-save-excursion
83
84
85
86
	 (set-buffer buffer)
	 (if (and lyskom-pers-no (= conf-no lyskom-pers-no))
	     (lyskom-format-insert-before-prompt 
	      'you-changed-name-to
87
	      new-name
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	      (lyskom-default-button 'conf conf-no)))
	 ;; (cache-del-conf-stat conf-no) ;+++Borde {ndra i cachen i st{llet.
	 (let ((cached-stat (cache-get-conf-stat conf-no)))
	   (if cached-stat
	       (set-conf-stat->name cached-stat new-name)))
	 (cond
	  ((lyskom-is-in-minibuffer))
	  (kom-presence-messages
	   (lyskom-message "%s" (lyskom-format 'name-has-changed-to-name
					       old-name new-name))))
	 (cond
	  (kom-presence-messages-in-buffer
	   (lyskom-format-insert-before-prompt
	    'name-has-changed-to-name-r 
	    old-name 
	    new-name
	    (lyskom-default-button 'conf conf-no)))))))
     
Per Cederqvist's avatar
.    
Per Cederqvist committed
106
107
108
     ((eq msg-no 6)			;i_am_on - something is moving
      (let ((info (lyskom-parse-who-info)))
	(lyskom-save-excursion
109
110
111
112
113
114
	 (set-buffer buffer)
	 (if (or (not lyskom-pers-no)
		 (zerop lyskom-pers-no))
	     nil
	   (cache-add-who-info info)))))
     
Per Cederqvist's avatar
.    
Per Cederqvist committed
115
116
     ((eq msg-no 7)			; Database is syncing.
      (lyskom-save-excursion
117
118
119
120
121
122
123
124
125
126
127
128
129
       (set-buffer buffer)
       ;; I removed the test for kom-presence-messages /david
       (if (not (lyskom-is-in-minibuffer))
	   (lyskom-message "%s" (lyskom-get-string 'database-sync)))
       (setq mode-line-process (lyskom-get-string 'mode-line-saving))
       (setq lyskom-is-saving t)
       ;; I guess the following two lines could be replaced by
       ;; force-mode-line-update in a modern emacs.
       (set-buffer-modified-p (buffer-modified-p))
       (sit-for 0)
       (if (not lyskom-pending-calls)
	   (initiate-get-time 'async nil))))
     
Per Cederqvist's avatar
.    
Per Cederqvist committed
130
131
     ((eq msg-no 8)			; Forced leave conference
      (lyskom-skip-tokens tokens))
132
     
Per Cederqvist's avatar
.    
Per Cederqvist committed
133
134
135
136
     ((eq msg-no 9)			; A person has logged in
      (let ((pers-no (lyskom-parse-num))
	    (session-no (lyskom-parse-num)))
	(lyskom-save-excursion
137
138
139
140
	 (set-buffer buffer)
	 (if (and lyskom-pers-no
		  (not (zerop lyskom-pers-no))
		  (/= pers-no lyskom-pers-no))
Per Cederqvist's avatar
.    
Per Cederqvist committed
141
					; Don't show myself.
142
143
144
145
146
147
148
149
150
151
152
153
	     (initiate-get-conf-stat 'follow
				     'lyskom-show-logged-in-person
				     pers-no))
	 (if (and lyskom-pers-no
		  (not (zerop lyskom-pers-no))
		  lyskom-who-info-buffer-is-on)
	     (initiate-get-session-info 'who-buffer 'cache-add-session-info
					session-no))
	 )))

     ;; msg-no 10 is the old broadcast message. No longer used.
     
Per Cederqvist's avatar
.    
Per Cederqvist committed
154
155
156
     ((eq msg-no 11)
      (lyskom-save-excursion
       (set-buffer buffer)
157
       (lyskom-insert-before-prompt (lyskom-get-string 'lyskom-is-full))
158
159
160
161
162
163
164
165
166
167
;;;       (if (and (eq major-mode 'lyskom-mode)
;;;		(not (listp lyskom-time-last-command))
;;;		kom-auto-quit-when-idle)
;;;	   (progn
;;;	     (lyskom-insert-before-prompt
;;;	      (lyskom-get-string 'session-auto-ended))
;;;	      (kom-quit 1)
       ))
     
     
Per Cederqvist's avatar
.    
Per Cederqvist committed
168
169
170
171
172
173
     ((eq msg-no 12)			; Message to the user (or everybody)
      (let ((recipient (lyskom-parse-num))
	    (sender (lyskom-parse-num))
	    (message (lyskom-parse-string)))
	(lyskom-save-excursion
	 (set-buffer buffer)
174
175
176
177
178
179
180
181
182
183
184
	 (if (zerop recipient)
	     (initiate-get-conf-stat 'async
				     'lyskom-handle-personal-message
				     sender
				     0
				     message)
	   (lyskom-collect 'async)
	   (initiate-get-conf-stat 'async nil sender)
	   (initiate-get-conf-stat 'async nil recipient)
	   (lyskom-use 'async 'lyskom-handle-personal-message message)))))
     
Per Cederqvist's avatar
.    
Per Cederqvist committed
185
186
187
188
     ((eq msg-no 13)			; New logout
      (let ((pers-no (lyskom-parse-num))
	    (session-no (lyskom-parse-num)))
	(lyskom-save-excursion
189
190
191
192
193
194
195
196
197
198
199
	 (set-buffer buffer)
	 (if (and lyskom-pers-no
		  (not (zerop lyskom-pers-no))
		  (/= lyskom-pers-no pers-no)
		  (or kom-presence-messages
		      kom-presence-messages-in-buffer))
	     (initiate-get-conf-stat 'follow
				     'lyskom-show-logged-out-person
				     pers-no session-no))
	 (if (and lyskom-pers-no (not (zerop lyskom-pers-no)))
	     (lyskom-run 'who-buffer 'cache-del-who-info session-no)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
200
201
202
203
204
205
206
207
208
209

     (t
      (lyskom-skip-tokens tokens)))))


(defun lyskom-show-logged-in-person (conf-stat)
  "Visa p} kommandoraden vem som loggat in."
  (cond
   ((lyskom-is-in-minibuffer))
   (kom-presence-messages
David Kågedal's avatar
David Kågedal committed
210
211
212
213
    (lyskom-message
     "%s"
     (lyskom-format 'has-entered (or conf-stat
				     (lyskom-get-string 'secret-person))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
214
215
  (cond
   (kom-presence-messages-in-buffer
David Kågedal's avatar
David Kågedal committed
216
217
218
219
220
221
222
223
    (if conf-stat
	(lyskom-format-insert-before-prompt 'has-entered-r conf-stat
					    (and kom-text-properties
						 '(face kom-presence-face)))
      (lyskom-format-insert-before-prompt 'has-entered-r
					  (lyskom-get-string 'secret-person)
					  (and kom-text-properties
					       '(face kom-presence-face)))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
224
225
226
227
228
229
230


(defun lyskom-show-logged-out-person (conf-stat session-no)
  "Visa p} kommandoraden vem som loggat ut."
  (cond
   ((lyskom-is-in-minibuffer))
   (kom-presence-messages
David Kågedal's avatar
David Kågedal committed
231
232
233
234
    (lyskom-message
     "%s"
     (lyskom-format 'has-left (or conf-stat
				  (lyskom-get-string 'secret-person))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
235
236
  (cond
   (kom-presence-messages-in-buffer
David Kågedal's avatar
David Kågedal committed
237
238
239
240
241
242
243
244
    (if conf-stat
	(lyskom-format-insert-before-prompt 'has-left-r conf-stat
					    (and kom-text-properties
						 '(face kom-presence-face)))
      (lyskom-format-insert-before-prompt 'has-left-r
					  (lyskom-get-string 'secret-person)
					  (and kom-text-properties
					       '(face kom-presence-face)))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269


(defun lyskom-show-changed-person (personconfstat conf-num doing)
  "Tells the user what another person is doing."
  (if personconfstat			;+++ Annan felhantering
      (progn
	(cond
	 ((lyskom-is-in-minibuffer))
	 ((and kom-presence-messages
	       (or (= 0 conf-num)
		   (eq conf-num lyskom-current-conf))
	       (/= 0 (length doing)))
	  (lyskom-message "%s %s" (conf-stat->name personconfstat) 
		   (let ((string
			  (concat (char-to-string (downcase
						   (string-to-char doing)))
				  (substring doing 1))))
		     (if kom-emacs-knows-iso-8859-1
			 string
		       (iso-8859-1-to-swascii string))))))
	(cond
	 ((and (eq kom-presence-messages-in-buffer t)
	       (or (= 0 conf-num)
		   (eq conf-num lyskom-current-conf))
	       (/= 0 (length doing)))
270
271
	  (lyskom-format-insert-before-prompt
	   "%#1M %#2s\n"
272
	   personconfstat
273
274
	   (concat (char-to-string (downcase (string-to-char doing)))
		   (substring doing 1))))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
275
276
277
278


(defun lyskom-is-in-minibuffer ()
  "Returns non-nil if I am using the minibuffer for some reading."
David Kågedal's avatar
David Kågedal committed
279
280
  (or lyskom-inhibit-minibuffer-messages
      (not (zerop (minibuffer-depth)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
281
282


283
284
(defun lyskom-show-personal-message (sender recipient message 
                                            &optional when nobeep)
Per Cederqvist's avatar
.    
Per Cederqvist committed
285
286
287
  "Insert a personal message into the lyskom buffer.
Args: SENDER: conf-stat for the person issuing the broadcast message or a
	      string that is the sender.
288
289
      RECIPIENT: 0 if this message is for everybody, otherwise the conf-stat
                 of the recipient.
290
291
292
293
294
295
296
297
298
299
300
301
302
      MESSAGE: A string containing the message.
      WHEN: Optional time of arrival. Same format as (current-time-string)
      NOBEEP: True means don't beep. No matter what."
  (lyskom-insert-personal-message sender recipient message when nobeep)
  (setq lyskom-last-personal-message-sender 
        (if (stringp sender) sender (conf-stat->name sender)))
  (setq lyskom-last-group-message-recipient 
        (if (and recipient
                 (not (eq 0 recipient))
                 (not (eq (conf-stat->conf-no recipient)
                          lyskom-pers-no)))
            (conf-stat->name recipient)
          nil))
303
304
  (run-hooks 'lyskom-personal-message-hook))

305

306
307
(defun lyskom-insert-personal-message (sender recipient message
                                              &optional when nobeep)
308
  "Insert a personal message in the current buffer.
309
Arguments: SENDER RECIPIENT MESSAGE.
Linus Tolke Y's avatar
Linus Tolke Y committed
310
SENDER is a conf-stat (possibly nil) or a string.
311
312
RECIPIENT is 0 if the message is public, otherwise the pers-no of the user.
MESSAGE is a string containing the message.
313
314
315
WHEN, if given, is the time when the message arrived. It must be of the same 
format at (current-time-string)
Non-nil NOBEEP means don't beep."
316
  (lyskom-handle-as-personal-message
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
   (lyskom-format-as-personal-message sender recipient message when nobeep)
   (conf-stat->conf-no sender) 
   nil))

(defun lyskom-format-as-personal-message (sender 
                                          recipient
                                          message
                                          &optional when nobeep)
  "Formats a personal message, returning it as a string.
Arguments: SENDER RECIPIENT MESSAGE.
SENDER is a conf-stat (possibly nil) or a string.
RECIPIENT is 0 if the message is public, otherwise the pers-no of the user.
MESSAGE is a string containing the message.
WHEN, if given, is the time when the message arrived. It must be of the same 
format at (current-time-string)
Non-nil NOBEEP means don't beep."
  (progn
    (if (null when)
        (setq when (current-time-string)))
    (if (not (string= (substring when 0 10)
                      (substring (current-time-string) 0 10)))
        (setq when (substring when 4 19))
      (setq when (substring when 11 19)))

David Kågedal's avatar
David Kågedal committed
341
342
343
344
345
    (cond ((or (null recipient)		; Have been seen to be nil when
                                        ; listing recorded
                                        ; messages. Should it be?
                                        ; /davidk
	       (eq recipient 0))	; Public message
346
           (if (not nobeep) (lyskom-beep kom-ding-on-common-messages))
347
348
349
350
351
352
353
354
           (lyskom-format 'message-broadcast
                          (cond
                           ((stringp sender) sender)
                           (sender sender)
                           (t (lyskom-get-string 'unknown)))
                          message
                          when))
          ((= (conf-stat->conf-no recipient) lyskom-pers-no) ; Private
355
           (if (not nobeep) (lyskom-beep kom-ding-on-personal-messages))
356
357
358
359
360
361
362
363
           (lyskom-format 'message-from
                          (cond
                           ((stringp sender) sender)
                           (sender sender)
                           (t (lyskom-get-string 'unknown)))
                          message
                          when))
          (t                            ; Group message
364
           (if (not nobeep) (lyskom-beep kom-ding-on-group-messages))
365
366
367
368
           (lyskom-format 'message-from-to
                          message
                          (cond
                           ((stringp sender) sender)
David Byers's avatar
David Byers committed
369
                           (sender  sender)
370
371
372
                           (t (lyskom-get-string 'unknown)))
                          (cond
                           ((stringp recipient) recipient)
David Byers's avatar
David Byers committed
373
                           (recipient recipient)
374
375
376
                           (t (lyskom-get-string 'unknown)))
                          when)))))

377
378

  
379
380
381
382
(defun lyskom-handle-as-personal-message (string from &optional filter)
  "Insert STRING as a personal message and beep if not from me and
supposed to. The buffer, is chosen according to the
kom-show-personal-messages-in-buffer variable value. The text is
383
converted, before insertion."
384
385
386
387
388
389
390
391
392
393
394
395
396
  (if (and filter
           (or
            (eq 0 (string-match "^Remote-command: [0-9]+ [0-9]+\n" string))
            (eq 0 (string-match "^Auto-reply:\n" string))))
      nil
    (lyskom-save-excursion
     (cond
      ((eq kom-show-personal-messages-in-buffer t)
       (lyskom-insert-before-prompt string))
      ((null kom-show-personal-messages-in-buffer))
      (t
       (set-buffer (get-buffer-create kom-show-personal-messages-in-buffer))
       (goto-char (point-max))
397
398
399
       (lyskom-insert (if kom-emacs-knows-iso-8859-1
                          string
                        (iso-8859-1-to-swascii string)))))
400
401
     (if kom-pop-personal-messages
         (display-buffer (current-buffer))))))
402
  
Per Cederqvist's avatar
.    
Per Cederqvist committed
403
404
405
406
407
408
409

;;; ================================================================
;;;            Functions for dealing with a new text


(defun lyskom-default-new-text-hook (text-stat)
  "Print a message if the user was waiting. Change the prompt. run hooks."
410
411
  (if (and (not lyskom-dont-change-prompt) ;We shall change it
	   (not lyskom-executing-command)) ;We have time to do it.
412
      (lyskom-update-prompt))
413

Per Cederqvist's avatar
.    
Per Cederqvist committed
414
415
416
417
418
419
  (let ((no-message nil))
    (run-hooks 'lyskom-new-text-hook)
  
    (if (and (not no-message)
	     lyskom-is-waiting
	     (not (lyskom-is-in-minibuffer)))
420
421
	(lyskom-message "%s" (lyskom-format 'text-is-created
					    (text-stat->text-no text-stat))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
422
423
424
425
426
427
428

  
(defun lyskom-async-new-text (text-stat)
  "Take care of a message that a new text has been created."
  (cache-del-pers-stat (text-stat->author text-stat)) ;+++Borde {ndra i cachen i st{llet.
  
  (lyskom-traverse
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
      misc-info (text-stat->misc-info-list text-stat)
    (let ((type (misc-info->type misc-info)))
      (cond
       ((or (eq type 'RECPT)
	    (eq type 'CC-RECPT))
	;; add on lyskom-reading-list and lyskom-to-do-list if
	;; this recipient is a recipient that has been checked.
	(initiate-get-conf-stat 'async 'lyskom-add-new-text
				(misc-info->recipient-no misc-info)
				(text-stat->text-no text-stat)
				(misc-info->local-no misc-info)))
       ((eq type 'COMM-TO)
	(cache-del-text-stat (misc-info->comm-to misc-info)))
       ((eq type 'FOOTN-TO)
	(cache-del-text-stat (misc-info->footn-to misc-info)))
       (t
	(signal 'lyskom-internal-error
		(list 'lyskom-async-new-text
		      "Unexpected misc-info in new text "
		      type))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
449

450
  ;; Give a message if the user is waiting. Update the prompt.
451
452
453
  (lyskom-run 'async 'lyskom-default-new-text-hook text-stat)

  (lyskom-run 'async 'lyskom-prefetch-and-print-prompt))
454
455


Per Cederqvist's avatar
.    
Per Cederqvist committed
456
457
458
459
460
461
462
463
464

(defun lyskom-add-new-text (recipient text-no local-no)
  "RECIPIENT is a conf-stat and recipient of TEXT-NO.
Args: RECIPIENT TEXT-NO LOCAL-NO.
LOCAL-NO is the texts local number in RECIPIENT. This info is used
to update the no-of-texts field in the cache.

Also add this info in lyskom-to-do-list if info about RECIPIENT as been
fetched. Does not try to print prompt or do any prefetch. That will be
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
done after all the confs has been handled.

If recipient is nil this means we are crossposting to a protected conference.
In that case, just discard this call."

  (cond
   (recipient				;+++ Annan felhantering.
    ;; Update the cache.

    (set-conf-stat->no-of-texts
     recipient
     (max (conf-stat->no-of-texts recipient)
	  (+ local-no -1
	     (- (conf-stat->first-local-no recipient)))))

    ;; Update the read-lists.

482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
    ;; Prefetch thoughts:
    ;; We need a way to check if a conferences is fetched.
    ;; davidk /960924
    
    (let ((member (lyskom-member-p (conf-stat->conf-no recipient))))
      (if (and member
	       ;; (lyskom-conf-fetched-p (conf-stat->conf-no recipient))
	       (lyskom-visible-membership member)
	       (not (read-list-enter-text text-no recipient
					  lyskom-to-do-list)))
	  ;; If we have already read all texts in the conference or the
	  ;; text has not been prefetched
	  (let ((info (lyskom-create-read-info
		       'CONF
		       recipient
		       (membership->priority
			(lyskom-member-p (conf-stat->conf-no recipient)))
		       (lyskom-create-text-list (list text-no)))))
	    (read-list-enter-read-info info lyskom-to-do-list)
	    (if (= lyskom-current-conf (conf-stat->conf-no recipient))
		(read-list-enter-first info lyskom-reading-list)))))
503
504

    (lyskom-set-mode-line))))
505
506
507
508
509


;;; Local Variables: 
;;; eval: (put 'lyskom-traverse 'lisp-indent-hook 2)
;;; end: