startup.el 38.2 KB
Newer Older
David Byers's avatar
David Byers committed
1
;;;;; -*-coding: iso-8859-1;-*-
Linus Tolke's avatar
Linus Tolke committed
2
3
;;;;;
;;;;; $Id$
4
;;;;; Copyright (C) 1991-2002  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
5
;;;;;
6
;;;;; This file is part of the LysKOM Emacs LISP client.
Linus Tolke's avatar
Linus Tolke committed
7
8
9
;;;;; 
;;;;; LysKOM is free software; you can redistribute it and/or modify it
;;;;; under the terms of the GNU General Public License as published by 
10
;;;;; the Free Software Foundation; either version 2, or (at your option) 
Linus Tolke's avatar
Linus Tolke committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
;;;;; 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
26
27
28
29
30
31
32
33
34
35
36
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: startup.el
;;;;
;;;; This file contains functions that are called only when lyskom
;;;; is loaded, started or when a new user is logged in during a 
;;;; session.
;;;;


37
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
;;; ================================================================
;;;                         Start kom.


46
47
48
49
50
51
52
53
(defvar lyskom-www-proxy-connect-phase 1
  "Phase when reading connection response from www-proxy:
1: (initial phase) waiting for the
   string \"HTTP/1.0 2000 Connection established\"
2: we have seen the connection string. After this, it may come lines stating
   proxy-agents and other things. They all seem to end with an empty line,
   so in this phase we wait for an empty line.")

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

(eval-and-compile
  (defun lyskom-compilation-in-progress ()
    "Returns non-nil if the client currently is being compiled, else nil."

    ;; This horrid code is inspired from how cl-compiling-file in the
    ;; cl package works.
    (let ((outbuffersym (lyskom-xemacs-or-gnu 'byte-compile-outbuffer
                                              'outbuffer)))
      (and (boundp outbuffersym)
           (bufferp (symbol-value outbuffersym))
           (equal (buffer-name (symbol-value outbuffersym))
                  " *Compiler Output*")))))


(defconst lyskom-is-running-compiled
  (eval-when-compile (lyskom-compilation-in-progress))
  "Non-nil if the client is running compiled, else nil.")


74
;;;###autoload
75
(defun lyskom (&optional host username password session-priority invisiblep)
Per Cederqvist's avatar
.    
Per Cederqvist committed
76
  "Start a LysKOM session.
77
Optional arguments: HOST, USERNAME, PASSWORD and INVISIBLEP.
Linus Tolke's avatar
Linus Tolke committed
78

79
80
81
A numeric prefix argument sets the session priority. A prefix argument
of `C-u', on the other hand, logs in the session without notifying other
clients of the event. See lyskom-mode for details on lyskom."
David Kågedal's avatar
David Kågedal committed
82
83
84
85
  (interactive (list (lyskom-read-server-name
		      (lyskom-format 'server-q
				     (or (getenv "KOMSERVER")
					 lyskom-default-server)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
86
		     nil
87
88
89
		     nil
		     (if current-prefix-arg
			 (prefix-numeric-value current-prefix-arg)
90
91
		       nil)
		     (and current-prefix-arg (listp current-prefix-arg))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
92

93
  (run-hooks 'lyskom-init-hook)
94
  (setq username
David Kågedal's avatar
David Kågedal committed
95
	(or username (getenv "KOMNAME")))
96
  (setq password
David Kågedal's avatar
David Kågedal committed
97
	(or password (getenv "KOMPASSWORD")))
Per Cederqvist's avatar
.    
Per Cederqvist committed
98
  (if (zerop (length host))
99
      (let* ((env-kom (getenv "KOMSERVER"))
David Byers's avatar
David Byers committed
100
101
	     (canon (or (lyskom-string-rassoc env-kom kom-server-aliases)
                        (lyskom-string-rassoc env-kom kom-builtin-server-aliases))))
102
103
104
	(setq host (or (car canon)
		       env-kom
		       lyskom-default-server))))
105
106
  (let ((port 4894)
	(init-done nil))
Per Cederqvist's avatar
.    
Per Cederqvist committed
107
108
109
110
111
112
113
114
115
    (cond				;Allow "nanny:4892" to use port 4892.
     ((string-match ":" host)
      (setq port (string-to-int (substring host (match-end 0))))
      (cond
       ((zerop (match-beginning 0))
	(setq host lyskom-default-server))
       (t
	(setq host (substring host 0 (match-beginning 0)))))))

David Byers's avatar
David Byers committed
116
117
118
    (let* ((duplicate-buffers
            (delq nil
                  (mapcar (lambda (buffer)
119
                            (and (lyskom-buffer-p buffer t)
David Byers's avatar
David Byers committed
120
121
122
123
124
125
126
127
128
                                 (buffer-name buffer)
                                 (string-match (regexp-quote host)
                                               (buffer-name buffer))
                                 (save-excursion (set-buffer buffer)
                                                 (and 
                                                  (boundp 'lyskom-server-port)
                                                  (eq port lyskom-server-port)))
                                 buffer))
                          (buffer-list))))
David Kågedal's avatar
David Kågedal committed
129
	   (name nil)
David Byers's avatar
David Byers committed
130
	   (proc nil)
David Byers's avatar
David Byers committed
131
           (buffer (car duplicate-buffers))
132
	   ;; (alive (lyskom-buffer-p buffer))
David Byers's avatar
David Byers committed
133
           (reused-buffer nil))
David Byers's avatar
David Byers committed
134
135
      

136
137
138
139
      ;;<<<<<<< startup.el
      ;;      (if (and buffer
      ;;	       alive
      ;;=======
140
      (if (and (lyskom-buffer-p buffer nil)
141
142
	       (not (prog1
			(j-or-n-p (lyskom-get-string
143
				   'start-new-session-same-server))
144
		      (message ""))))
David Byers's avatar
David Byers committed
145
146
147
	  (progn 
            (switch-to-buffer buffer)
            (setq reused-buffer t))
David Kågedal's avatar
David Kågedal committed
148
149
	(unwind-protect
	    (progn
150
	      (cond ((and buffer (not (lyskom-buffer-p buffer)))
David Kågedal's avatar
David Kågedal committed
151
		     (set-buffer buffer)
David Byers's avatar
David Byers committed
152
                     (setq reused-buffer t)
David Kågedal's avatar
David Kågedal committed
153
		     (goto-char (point-max))
154
155
156
157
158
                     (lyskom-insert
                      (format (lyskom-get-string 'new-session-in-buffer)
                              (lyskom-format-time
                               'timeformat-day-yyyy-mm-dd-hh-mm-ss)))
                     (setq name (buffer-name buffer)))
David Kågedal's avatar
David Kågedal committed
159
		    (t
160
		     (setq buffer (lyskom-generate-new-buffer host))
David Kågedal's avatar
David Kågedal committed
161
		     (setq name (buffer-name buffer))))
David Byers's avatar
David Byers committed
162
              (let* ((www-proxy-host-string
David Byers's avatar
David Byers committed
163
164
165
166
167
168
                      (cond ((stringp kom-www-proxy) kom-www-proxy)
                            ((listp kom-www-proxy)
                             (or (cdr (lyskom-string-assoc host kom-www-proxy))
                                 (cdr (assq t kom-www-proxy))
                                 nil))
                            (t nil)))
David Byers's avatar
David Byers committed
169
170
                     (www-proxy-host nil)
                     (www-proxy-port nil)
David Byers's avatar
David Byers committed
171
                     (match (string-match "\\(.*\\):\\([0-9]+\\)"
David Byers's avatar
David Byers committed
172
                                          (or www-proxy-host-string "")))
173
                     (headers nil))
David Byers's avatar
David Byers committed
174
175
176
177
                (setq www-proxy-host (or (and match
                                          (match-string 1 www-proxy-host-string))
                                     www-proxy-host-string)
                      www-proxy-port (or (and match
David Byers's avatar
David Byers committed
178
179
                                          (string-to-int
                                           (match-string 2
David Byers's avatar
David Byers committed
180
                                                         www-proxy-host-string)))
David Byers's avatar
David Byers committed
181
                                     80))
David Byers's avatar
David Byers committed
182
                (cond (www-proxy-host
183
184
185
186
187
188
189
190
                       (setq headers 
                             (cond ((stringp kom-www-proxy-headers)
                                    (list kom-www-proxy-headers))
                                   ((and (listp kom-www-proxy-headers)
                                         (stringp (car kom-www-proxy-headers)))
                                    kom-www-proxy-headers)
                                   ((and (listp kom-www-proxy-headers)
                                         (consp (car kom-www-proxy-headers)))
David Byers's avatar
David Byers committed
191
                                    (cdr (or (lyskom-string-assoc www-proxy-host
192
193
                                                                  kom-www-proxy-headers)
                                             (assq t kom-www-proxy-headers))))))
David Byers's avatar
David Byers committed
194
195
196
                       (setq proc (lyskom-open-network-stream name buffer
                                                              www-proxy-host
                                                              www-proxy-port))
197
                       ;; We do explicit coding
198
                       (set-process-coding-system proc 'no-conversion 'no-conversion)
199
200
201
202
203
204
205
206
207

		       ;; Install our filter.
		       ;; Do this before we send the CONNECT command to
		       ;; the proxy, in case the proxy answers fast.
		       (setq lyskom-www-proxy-connect-phase 1)
		       (set-process-filter proc
					   'lyskom-www-proxy-connect-filter)
		       (process-kill-without-query proc nil)

David Byers's avatar
David Byers committed
208
209
                       (lyskom-process-send-string 
                        proc
210
                        (format "CONNECT %s:%d HTTP/1.0\r\n"
211
212
                                host port))

213
214
215
216
217
218
219
                       (mapcar (lambda (header)
                                 (lyskom-process-send-string proc header)
                                 (lyskom-process-send-string proc "\r\n"))
                               headers)
                       (lyskom-process-send-string proc "\r\n")


220
221
222
223
224
225
226
227
228
229
230
		       ;; Now wait for the answer from the proxy
		       ;;
		       ;; This is because anything we send before the
		       ;; connection ack will be thrown away by the proxy
		       ;; so it is bad to try to start talking with the
		       ;; server before the connection is up.
		       (while (eq 'lyskom-www-proxy-connect-filter
				  (process-filter proc))
			 (accept-process-output proc))
		       ;; Now the proxy has connected to the kom server
		       )
David Byers's avatar
David Byers committed
231
232
                      (t (setq proc (lyskom-open-network-stream name buffer
                                                                host port))
233
                         ;; We do explicit coding
234
                         (set-process-coding-system proc 'no-conversion 'no-conversion))))
David Kågedal's avatar
David Kågedal committed
235
236
237
238
239
240
	      (switch-to-buffer buffer)
	      (lyskom-mode)		;Clearing lyskom-default...
	      (setq lyskom-buffer buffer)
	      (setq lyskom-default-user-name username)
	      (setq lyskom-default-password password)
	      (setq lyskom-server-name host)
241
	      (setq lyskom-server-port port)
David Kågedal's avatar
David Kågedal committed
242
	      (setq lyskom-proc proc)
243
	      (lyskom-setup-faces-for-buffer)
244
              (lyskom-check-configuration)
David Kågedal's avatar
David Kågedal committed
245
246
247
	      (lyskom-insert
	       (lyskom-format 'try-connect lyskom-clientversion host))
	      (set-process-filter proc 'lyskom-connect-filter)
David Byers's avatar
David Byers committed
248
              (process-kill-without-query proc nil)
249
250
251
252
253
	      (lyskom-process-send-string
	       proc
	       (concat "A"
		       (lyskom-format-objects
			(concat (user-login-name)
David Byers's avatar
David Byers committed
254
255
				"%" (system-name)))
                       "\n"))
David Kågedal's avatar
David Kågedal committed
256
257
258
259
260
261
262
263
	      (while (eq 'lyskom-connect-filter (process-filter proc))
		(accept-process-output proc))
	      ;; Now we have got the correct response.
	      (set-process-sentinel proc 'lyskom-sentinel)

	      (save-excursion
		(lyskom-init-parse buffer))

David Byers's avatar
David Byers committed
264
265
266
              ;; Async messages
              (lyskom-accept-async)

267
268
269
	      ;; +++PREFETCH+++
	      (lyskom-setup-prefetch)

David Kågedal's avatar
David Kågedal committed
270
271
272
273
	      ;; Tell the server who we are
	      (initiate-set-client-version 'background nil
					   "lyskom.el" lyskom-clientversion)

274
              (setq lyskom-server-version-info (blocking-do 'get-version-info))
David Byers's avatar
David Byers committed
275
276
277
              (when (or (null lyskom-server-version-info)
                        (<= (version-info->protocol-version 
                             lyskom-server-version-info) 7))
278
                (lyskom-error 'too-old-server))
David Kågedal's avatar
David Kågedal committed
279
	      (lyskom-setup-client-for-server-version)
280
281

	      (setq lyskom-server-info (blocking-do 'get-server-info))
David Kågedal's avatar
David Kågedal committed
282
283
	      (lyskom-format-insert 
	       'connection-done
284
	       (version-info->software-version lyskom-server-version-info))
285

286
              (when (lyskom-have-call 85)
287
                (setq lyskom-collate-table (blocking-do 'get-collate-table))
288
                (setq lyskom-char-classes nil)
289
                (lyskom-update-command-completion))
David Byers's avatar
David Byers committed
290
              (if (not (zerop (server-info->motd-of-lyskom
291
			       lyskom-server-info)))
292
293
294
295
296
297
		  (blocking-do-multiple ((text (get-text 
                                                (server-info->motd-of-lyskom
                                                 lyskom-server-info)))
                                         (text-stat (get-text-stat
                                                     (server-info->motd-of-lyskom
                                                      lyskom-server-info))))
David Byers's avatar
David Byers committed
298
299
300
301
302
303
304
305
306
307
308
309
310
                    (let* ((conf-stat (and text-stat (blocking-do 'get-conf-stat (text-stat->author text-stat))))
                           (str (and text text-stat (text->decoded-text-mass text text-stat))))
                      (if (null text-stat)
                          (lyskom-get-string 'lyskom-motd-was-garbed)
                        (lyskom-insert (lyskom-get-string 'server-has-motd))
                        (when (string-match "\n" str)
                          (setq str (substring str (match-end 0))))
                        (lyskom-format-insert 
                         "%#2$%#1s\n"
                         (if kom-dashed-lines
                             (make-string kom-text-header-dash-length ?-)
                           "")
                         (when kom-highlight-dashed-lines
311
312
                           `(face ,(or kom-dashed-lines-face
                                       lyskom-default-dashed-lines-face))))
David Byers's avatar
David Byers committed
313
314
315
316

                        (lyskom-format-insert "%#2$%#1t\n"
                                              str
                                              (when kom-highlight-text-body
317
318
                                                `(face ,(or kom-text-body-face
                                                            lyskom-default-text-body-face))))
David Byers's avatar
David Byers committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332

                        (lyskom-format-insert
                         "%#2$%#1s\n"
                         (lyskom-format-text-footer 
                          text-stat
                          conf-stat
                          (cond ((eq (text-stat->author text-stat) 0)
                                 (lyskom-get-string 'person-is-anonymous))
                                (conf-stat (conf-stat->name conf-stat))
                                (t (lyskom-format 'person-does-not-exist 
                                                  (text-stat->author text-stat))))
                          kom-text-footer-format
                          lyskom-last-text-format-flags)
                         (when kom-highlight-dashed-lines
333
334
                           `(face ,(or kom-dashed-lines-face
                                       lyskom-default-dashed-lines-face))))
David Byers's avatar
David Byers committed
335
                        ))))
336

David Kågedal's avatar
David Kågedal committed
337
	      ;; Can't use lyskom-end-of-command here.
338
	      (setq lyskom-executing-command nil)
David Kågedal's avatar
David Kågedal committed
339
	      ;; Log in
340
	      (kom-start-anew t session-priority invisiblep)
341
342
343
344
345
346
347
	      (if (memq lyskom-buffer lyskom-buffer-list)
		  (while (not (eq lyskom-buffer (car lyskom-buffer-list)))
		    (setq lyskom-buffer-list
			  (nconc (cdr lyskom-buffer-list)
				 (list (car lyskom-buffer-list)))))
		(setq lyskom-buffer-list
		      (cons lyskom-buffer lyskom-buffer-list)))
348
349
350
              ;; We're done
	      (setq init-done t)
              )
David Kågedal's avatar
David Kågedal committed
351
352
353
354
	  ;; Something went wrong. Lets cleanup everything. :->
	  (if init-done
	      nil
	    (if proc (delete-process proc))
David Byers's avatar
David Byers committed
355
	    (unless reused-buffer (kill-buffer buffer))))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
356
357


David Byers's avatar
David Byers committed
358
(defun lyskom-accept-async ()
359
  (blocking-do 'accept-async '(5 7 8 9 11 12 13 14 15 16 17 18 19))
360
  (let* ((ans (blocking-do 'query-async)))
David Byers's avatar
David Byers committed
361
    (unless (memq 15 (listify-vector ans))
362
      (blocking-do 'accept-async '(0 5 7 8 9 11 12 13 14 16 17 18 19)))))
David Byers's avatar
David Byers committed
363

364
365
366
367
368
369
370

(defun lyskom-www-proxy-connect-filter (proc output)
  "Receive connection acknowledgement from proxy."
  (if lyskom-debug-communications-to-buffer
      (lyskom-debug-insert proc "-----> " output))
  (cond
   ((and (= lyskom-www-proxy-connect-phase 1)
371
	 (string-match "^HTTP/1\\.. 200.*\r\n" output))
372
373
374
375
376
377
378
379
    (setq lyskom-www-proxy-connect-phase 2)
    ;; safety check: see if the empty line is already in this output
    (lyskom-www-proxy-connect-filter proc output))

   ((and (= lyskom-www-proxy-connect-phase 2)
	 (string-match "^\r\n" output))
    (set-process-filter proc 'lyskom-connect-filter))))

David Byers's avatar
David Byers committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
(defun lyskom-open-network-stream (name buffer host service)
  (let ((relay (lyskom-setup-ssh-relay host service buffer)))
    (if relay
        (open-network-stream name
                             buffer
                             "127.0.0.1"
                             (get relay 'relay-port))
    (open-network-stream name
                         buffer
                         host
                         service))))


(defun lyskom-setup-ssh-relay (server port kom-buffer)
  (when kom-ssh-relay-host
    (let* ((procname (format "ssh<%s:%s:%d>" kom-ssh-relay-host server port))
           (bufname (concat " *" procname "*"))
           (proc (get-process procname))
           (procsym (intern procname))
           (relay-port (and proc
                            (eq (process-status proc) 'run)
401
402
                            (get procsym 'relay-port)))
           (msg nil))
David Byers's avatar
David Byers committed
403
404
405
406
      (unwind-protect
          (save-excursion
            (set-buffer (get-buffer-create bufname))
            (if relay-port
407
408
409
410
                (lyskom-message 
                 "%s" 
                 (lyskom-format (setq msg 'using-ssh-connection)
                                kom-ssh-relay-host))
David Byers's avatar
David Byers committed
411
412
413
414
415
416
417
418
              (when proc (delete-process proc))
              (setq relay-port (+ 10000 (random 20000)))
              (put procsym 'relay-host 
                   (if (string-match "@" kom-ssh-relay-host)
                       (substring kom-ssh-relay-host (1+ (match-beginning 0)))
                     kom-ssh-relay-host))
              (put procsym 'relay-port relay-port)
              (put procsym 'num-connected 0)
419
420
421
422
              (lyskom-message
               "%s"
               (lyskom-format (setq msg 'opening-ssh-connection)
                              kom-ssh-relay-host))
David Byers's avatar
David Byers committed
423
424
              (goto-char (point-max))
              (insert "\n--- new connection ---\n")
David Byers's avatar
David Byers committed
425
426
427
428
429
430
431
432
433
434
435
436
              (let ((old-lc-all (getenv "LC_ALL")))
                (unwind-protect
                    (progn
                      (setenv "LC_ALL" "C")
                      (setq proc (start-process
                                  procname
                                  bufname
                                  "ssh" "-n" "-x"
                                  "-L" (format "%d:%s:%d" relay-port server port)
                                  kom-ssh-relay-host
                                  "sh -c \"while :; do echo ok; sleep 600; done\"")))
                  (setenv "LC_ALL" old-lc-all)))
David Byers's avatar
David Byers committed
437
438
439
440
441
              (process-kill-without-query proc))
            (while (progn
                     (goto-char (point-max))
                     (re-search-backward "^--- .* ---$" nil t)
                     (not (re-search-forward "^ok$" nil t)))
442
443
444
445
446
447
448
449
450
451
452
453
              (when (re-search-forward "\\<\\(Enter passphrase.*$\\|^.*password.*$\\)\\|refused\\|disconnect\\|denied\\|error\\|key not found\\>" nil t)
                (cond ((match-string 1)
                       (process-send-string 
                        proc
                        (concat (silent-read (match-string 1)) "\n"))
                       (delete-region (match-beginning 0) 
                                      (match-end 0)))
                      (t (error (lyskom-get-string 'ssh-cant-connect)
                                (buffer-substring-no-properties
                                 (progn (beginning-of-line) (point))
                                 (progn (skip-chars-forward "^\n\r")
                                        (point)))))))
David Byers's avatar
David Byers committed
454
455
              (sleep-for 0.5))
            (setq proc nil)
456
457
458
459
460
            (lyskom-message 
             "%s"
             (lyskom-format (concat 
                             (lyskom-format msg kom-ssh-relay-host)
                             (lyskom-get-string 'done)))))
David Byers's avatar
David Byers committed
461
462
463
464
465
466
467
468
        (if proc (delete-process proc)))

      (save-excursion 
        (set-buffer kom-buffer)
        (put procsym 'num-connected (1+ (or (get procsym 'num-connected) 0)))
        (make-local-variable 'lyskom-ssh-proxy)
        (setq lyskom-ssh-proxy procsym))
      )))
469
470


471
(defun lyskom-setup-client-for-server-version ()
472
  "Setup flags according to protocol versions."
David Byers's avatar
David Byers committed
473
474
475
476
  (lyskom-clear-features)
  (let ((protocol-version 
         (version-info->protocol-version lyskom-server-version-info)))

477
478
479
480
481
482
483
  (when (>= protocol-version 8)
    (lyskom-set-feature long-conf-types t))

  (when (>= protocol-version 9)
    (lyskom-set-feature dynamic-session-info t)
    (lyskom-set-feature idle-time t))

David Byers's avatar
David Byers committed
484
485
486
487
488
489
  (when (>= protocol-version 10)
    (lyskom-set-feature bcc-misc t)
    (lyskom-set-feature aux-items t)
    (lyskom-set-feature highest-call 105)
    (lyskom-set-feature local-to-global t))

490
491
492
493
  (when (>= protocol-version 11)
    (lyskom-set-feature read-ranges t)
    (lyskom-set-feature highest-call 110))
  ))
494
495


Per Cederqvist's avatar
.    
Per Cederqvist committed
496
497
(defun lyskom-connect-filter (proc output)
  "Receive connection acknowledgement from server."
498
  (if lyskom-debug-communications-to-buffer
David Kågedal's avatar
David Kågedal committed
499
      (lyskom-debug-insert proc "-----> " output))
500
501
502
503
504
505
  (cond
   ((string-match "^LysKOM\n" output)
    (set-process-filter proc 'lyskom-filter))))



Per Cederqvist's avatar
.    
Per Cederqvist committed
506
507
508
509
510
511


;;; ================================================================
;;;                        Start anew


512
513
514
(defun kom-start-anew (&optional lyskom-first-time-around session-priority invisiblep)
  "Start/login as a new person. If INVISIBLEP is not nil, the login will not be
shown to other users."
Per Cederqvist's avatar
.    
Per Cederqvist committed
515
516
  (interactive)
  (lyskom-start-of-command 'kom-start-anew)
517
  (lyskom-completing-clear-cache)
David Byers's avatar
David Byers committed
518
  (let ((new-me nil)
David Byers's avatar
David Byers committed
519
520
	(login-successful nil)
        (ignored-user-area-vars nil))
David Kågedal's avatar
David Kågedal committed
521
    (unwind-protect
522
523
524
525
        (progn
          (if lyskom-first-time-around
              nil
            (lyskom-tell-internat 'kom-tell-login))
David Byers's avatar
David Byers committed
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
          ;; We can't allow the prefetch to go on after the new user
          ;; is logged in, but to shut down the prefetch would be too
          ;; brutal, since the new login might be cancelled. To
          ;; prevent the blocking-do calls below from allowing
          ;; prefetch we set lyskom-inhibit-prefetch locally.
          (let ((lyskom-inhibit-prefetch t))
            (while (not new-me)

              (if (and lyskom-first-time-around
                       lyskom-default-user-name)
                  ;; This is nil if we can't find a unique match.
                  (setq new-me
                        (conf-z-info->conf-no
                         (lyskom-lookup-conf-by-name lyskom-default-user-name
                                                     '(pers)))))
              (if new-me
                  nil
David Byers's avatar
David Byers committed
543
544
545
546
547
548
                (let ((name nil))
                  (while (or (null name)
                             (string= name ""))
                    (setq name (lyskom-read-conf-name
                                (lyskom-get-string 'what-is-your-name) 
                                '(pers none) t "" t)))
David Byers's avatar
David Byers committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
                  (setq new-me
                        (or (conf-z-info->conf-no 
                             (lyskom-lookup-conf-by-name name '(pers)))
                            (lyskom-create-new-person name)))))
              ;; Now new-me contains a number of a person.
              ;; Lets log him in.
              (if new-me
                  (let ((conf-stat (blocking-do 'get-conf-stat new-me))
                        (lyskom-inhibit-minibuffer-messages t))

                    ;; Previously this code used lyskom-pers-no
                    ;; directly instead of new-me, but that caused
                    ;; problem with asynchrounous code trying to
                    ;; access it.
                    ;;
                    ;; Setting lyskom-pers-no fscks up other things
                    ;; if we do keyboard-quit in the middle, so don't.
                    ;;
                    ;; (setq lyskom-pers-no new-me)

                    ;; DEBUG
                    (if (null conf-stat)
                        (lyskom-insert "You don't exist. Go away.\n"))

                    (lyskom-insert (concat (conf-stat->name conf-stat) "\n"))
                    (setq lyskom-first-time-around nil)
                    (if (blocking-do 'login new-me
                                     (if lyskom-default-password
                                         (prog1
                                             lyskom-default-password
                                           (setq lyskom-default-password nil)
                                           (set-default 'lyskom-default-password
                                                        nil))
                                       ;; Use password read when creating
                                       ;; the person when loggin in new
                                       ;; users
                                       (or lyskom-is-new-user
                                           (silent-read
                                            (lyskom-get-string 'password))))
588
                                     (if invisiblep 1 0))
David Byers's avatar
David Byers committed
589
590
591
592
593
                        (progn
                          (if lyskom-is-new-user
                              (blocking-do 'add-member
                                           (server-info->conf-pres-conf lyskom-server-info)
                                           new-me
594
595
596
597
                                           100 
                                           1
                                           (lyskom-create-membership-type
                                            nil nil nil nil nil nil nil nil)))
David Byers's avatar
David Byers committed
598
599
                          (setq login-successful t))
                      (lyskom-insert-string 'wrong-password)
600
601
602
603
604
605
606
607
608
609
610
                      (when (lyskom-get-aux-item 
                             (server-info->aux-item-list lyskom-server-info)
                             13)        ; e-mail
                        (lyskom-insert 'wrong-password-help)
                        (mapcar (lambda (el)
                                  (lyskom-format-insert 'wrong-password-email
                                                        (aux-item->data el)))
                                (lyskom-get-aux-item 
                                 (server-info->aux-item-list lyskom-server-info)
                                 13)    ; e-mail
                                ))
David Byers's avatar
David Byers committed
611
612
                      (setq new-me nil))
                    (setq lyskom-is-new-user nil))))
David Byers's avatar
David Byers committed
613

David Byers's avatar
David Byers committed
614
615
616
            ;; Now we are logged in.
            (setq lyskom-pers-no new-me)
            (lyskom-insert-string 'are-logged-in)
David Byers's avatar
David Byers committed
617

618
619
620
            (unless lyskom-is-running-compiled
              (lyskom-insert-string 'warning-about-uncompiled-client))

David Byers's avatar
David Byers committed
621
622
            (unless lyskom-dont-read-user-area
              (setq ignored-user-area-vars (lyskom-read-options)))
623
624
625
            (when (or session-priority kom-default-session-priority)
              (setq lyskom-session-priority
                    (or session-priority kom-default-session-priority)))
David Byers's avatar
David Byers committed
626
627
628
629
630
631
632
633
634
635
636
637
            (lyskom-run-hook-with-args 'lyskom-change-conf-hook
                                       lyskom-current-conf
                                       0)
            (lyskom-run-hook-with-args 'kom-change-conf-hook
                                       lyskom-current-conf
                                       0)
            (setq lyskom-current-conf 0)
            (cache-set-marked-texts (blocking-do 'get-marks))
            ;; What is this variable? It is never used. It is ust to
            ;; fill the cache?
            (let ((lyskom-who-am-i (blocking-do 'who-am-i)))
              (if lyskom-who-am-i (setq lyskom-session-no lyskom-who-am-i))))
David Byers's avatar
David Byers committed
638
639
640

          ;; If login succeeded, clear the caches and set the language

David Byers's avatar
David Byers committed
641
642
          (when login-successful
            (progn (clear-all-caches)
643
644
645
646
647
648
649
650
                   (when (lyskom-set-language kom-default-language 'local)
                     (unless lyskom-have-one-login
                       (lyskom-set-language kom-default-language 'global)
                       (lyskom-maybe-setq-default kom-default-language kom-default-language)
                       (setq-default lyskom-language kom-default-language))
                     (lyskom-format-insert-before-prompt
                      'language-set-to
                      (lyskom-language-name kom-default-language)))
David Byers's avatar
David Byers committed
651
                   (setq lyskom-have-one-login t)))
David Byers's avatar
David Byers committed
652

David Byers's avatar
David Byers committed
653
654
655
656
657
658
659
          (when ignored-user-area-vars
            (lyskom-format-insert-before-prompt
             'ignored-user-area-var
             (mapconcat 'symbol-name 
                        ignored-user-area-vars
                        "\n    ")))

David Byers's avatar
David Byers committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
          ;; Show motd and encourage writing a presentation

          (let ((conf-stat (blocking-do 'get-conf-stat lyskom-pers-no)))
            (if (and conf-stat
                     (/= (conf-stat->msg-of-day conf-stat) 0))
                (progn
                  (lyskom-insert-string 'you-have-motd)
                  (lyskom-view-text (conf-stat->msg-of-day conf-stat))))
            (if (and conf-stat
                     (zerop (conf-stat->presentation conf-stat))
                     (not (zerop (conf-stat->no-of-texts conf-stat))))
                (lyskom-insert-string 'presentation-encouragement)))

          (setq lyskom-is-new-user nil)

          ;; Start the prefetch and update some basic caches
676
          (lyskom-refetch))
David Kågedal's avatar
David Kågedal committed
677
      (lyskom-end-of-command)))
David Byers's avatar
David Byers committed
678

David Kågedal's avatar
David Kågedal committed
679
680
  ;; Run the hook kom-login-hook. We don't want to hang the
  ;; login, just because something crashed here.
David Byers's avatar
David Byers committed
681

David Kågedal's avatar
David Kågedal committed
682
  (condition-case err
683
      (progn
684
685
        (run-hooks 'lyskom-login-hook)
        (run-hooks 'kom-login-hook))
David Kågedal's avatar
David Kågedal committed
686
    (error (lyskom-format-insert-before-prompt
David Byers's avatar
David Byers committed
687
            'error-in-login-hook (format "%s" err)))))
Linus Tolke's avatar
Linus Tolke committed
688
689
690


(defun lyskom-refetch ()
691
692
  "Resets and fetches all reading info.
This is called at login and after prioritize and set-unread."
693

694
  ;; The whole membership!
695

696
697
  (setq lyskom-membership nil)

David Byers's avatar
David Byers committed
698
699
700
701
  (setq lyskom-to-do-list (lyskom-create-read-list))
  (setq lyskom-reading-list (lyskom-create-read-list))


702
  (lyskom-reset-prefetch)
David Byers's avatar
David Byers committed
703
704
705
706
707
  (let ((lyskom-inhibit-prefetch t))
    (lyskom-prefetch-membership lyskom-pers-no)
    (let ((unreads (blocking-do 'get-unread-confs lyskom-pers-no)))
      (lyskom-traverse conf-no (nreverse (conf-no-list->conf-nos unreads))
        (lyskom-prefetch-one-membership conf-no lyskom-pers-no))))
David Byers's avatar
David Byers committed
708
  (lyskom-start-prefetch)
David Byers's avatar
David Byers committed
709
710
711

  (condition-case nil
      (progn (lyskom-update-read-faqs)
712
             (lyskom-clean-read-faqs lyskom-pers-no)
David Byers's avatar
David Byers committed
713
714
715
716
717
             (lyskom-update-rejected-recommendations)
             (lyskom-startup-check-faqs)
             (lyskom-startup-check-recommended-memberships))
    (error nil)
    (quit nil)))
718
719


Linus Tolke's avatar
Linus Tolke committed
720
721
722
(defun lyskom-set-membership (membership)
  "Sets lyskom-membership to a new value.
Args: MEMBERSHIP."
David Byers's avatar
David Byers committed
723
724
  (setq lyskom-membership (listify-vector membership))
  (lyskom-sort-membership)
Linus Tolke's avatar
Linus Tolke committed
725
  (setq lyskom-membership-is-read t))
Per Cederqvist's avatar
.    
Per Cederqvist committed
726
727


David Kågedal's avatar
David Kågedal committed
728
(defun lyskom-create-new-person (name)
Per Cederqvist's avatar
.    
Per Cederqvist committed
729
730
731
732
733
734
735
736
737
738
  "A new user named NAME (or an old that mis-spelled his name)."
  (lyskom-insert
   (lyskom-format 'first-greeting name))
  (lyskom-scroll)
  (cond
   ((ja-or-nej-p (lyskom-format 'is-name-correct name))
    (let ((password (silent-read (lyskom-get-string 'personal-password))))
      (cond
       ((not (equal password 
		    (silent-read (lyskom-get-string 'repeat-password))))
David Kågedal's avatar
David Kågedal committed
739
	;; Failed to enter the same password twice
Per Cederqvist's avatar
.    
Per Cederqvist committed
740
	(lyskom-insert-string 'repeat-failure)
David Kågedal's avatar
David Kågedal committed
741
	nil)
Per Cederqvist's avatar
.    
Per Cederqvist committed
742
       (t
David Kågedal's avatar
David Kågedal committed
743
	;; Entered the same password twice
David Byers's avatar
David Byers committed
744
745
746
747
748
749
750
751
752
753
754
	(let ((new-person (blocking-do 'create-person name
                                       password
                                       (lyskom-create-flags nil
                                                            nil
                                                            nil
                                                            nil
                                                            nil
                                                            nil
                                                            nil
                                                            nil)
                                       nil)))
David Kågedal's avatar
David Kågedal committed
755
756
757
758
759
760
	  (if (null new-person)
	      (lyskom-insert-string 'could-not-create-you)
	    ;; Raise a flag so the user will be added to the
	    ;; presentation conference after login
	    (setq lyskom-is-new-user password))
	  new-person)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
761
   (t
David Kågedal's avatar
David Kågedal committed
762
763
764
765
    ;; Do not create a new person
    nil)))


David Kågedal's avatar
David Kågedal committed
766
767
(defun lyskom-read-server-name (prompt)
  "Read the name of a LysKOM server.
768
769
770
Copmpletion is done on the servers i kom-server-aliases and
kom-builtin-server-aliases. If an alias name is entered, the
corresponding address is returned."
David Kågedal's avatar
David Kågedal committed
771
772
773
774
775
776
  ;; Create a completion table like
  ;; (("kom.lysator.liu.se" . "kom.lysator.liu.se")
  ;;  ("LysKOM" . "kom.lysator.liu.se"))
  (let ((known-servers
	 (append (mapcar (function (lambda (pair)
				     (cons (car pair) (car pair))))
David Byers's avatar
David Byers committed
777
			 (append kom-server-aliases kom-builtin-server-aliases))
David Kågedal's avatar
David Kågedal committed
778
779
		 (mapcar (function (lambda (pair)
				     (cons (cdr pair) (car pair))))
David Byers's avatar
David Byers committed
780
			 (append kom-server-aliases kom-builtin-server-aliases))))
David Kågedal's avatar
David Kågedal committed
781
782
	(completion-ignore-case t)
	server)
David Byers's avatar
David Byers committed
783
784
785
786
    (setq server (lyskom-completing-read prompt
                                         (lyskom-maybe-frob-completion-table
                                          known-servers)
                                         nil nil))
787
    (or (cdr (lyskom-string-assoc server known-servers))
David Kågedal's avatar
David Kågedal committed
788
789
790
	server)))
      

Per Cederqvist's avatar
.    
Per Cederqvist committed
791
792
793
;;; ================================================================
;;;                        The LysKOM mode.

794
795
796
;; The LysKOM mode should not be inherited if we create a new buffer
;; and default-major-mode is nil.
(put 'lyskom-mode 'mode-class 'special)
Per Cederqvist's avatar
.    
Per Cederqvist committed
797
798
799
800

(defun lyskom-mode ()
  "\\<lyskom-mode-map>Mode for LysKOM client.
Commands:
David Kågedal's avatar
David Kågedal committed
801
\\[kom-next-command]	Do the default action. This can be to read the next text,select
802
	next conference with unread texts or whatever the prompt says.
David Kågedal's avatar
David Kågedal committed
803
804
805
806
\\[kom-go-to-conf]	Go to a conference. LysKOM will ask you for a conference
	and make you a member of it if you are not already.
\\[kom-list-conferences]	List conferences matching a given string.
\\[kom-list-persons]	List persons matching a given string.
Per Cederqvist's avatar
.    
Per Cederqvist committed
807

David Kågedal's avatar
David Kågedal committed
808
809
\\[kom-list-news]	List the conferences you have unread texts in.
\\[kom-go-to-next-conf]	Go to the next conference with unread texts.
Per Cederqvist's avatar
.    
Per Cederqvist committed
810

David Kågedal's avatar
David Kågedal committed
811
\\[kom-membership]	Display a buffer with the list of conferences you are member in.
Per Cederqvist's avatar
.    
Per Cederqvist committed
812

David Kågedal's avatar
David Kågedal committed
813
814
\\[kom-quit]	Leave this LysKOM session.
\\[kom-who-is-on]	Show a list of all the users of lyskom right now.
Per Cederqvist's avatar
.    
Per Cederqvist committed
815

David Kågedal's avatar
David Kågedal committed
816
\\[kom-extended-command]	Read a command using the minibuffer and execute it.
817
	This is another way to give commands.
Per Cederqvist's avatar
.    
Per Cederqvist committed
818

David Kågedal's avatar
David Kågedal committed
819
820
821
\\[kom-write-text]	Start writing a new text.
\\[kom-write-comment]	Start writing a comment to the last read article.
\\[kom-private-answer]	Start writing a personal answer to the author of the last
Per Cederqvist's avatar
.    
Per Cederqvist committed
822
	read article.
David Kågedal's avatar
David Kågedal committed
823
\\[kom-send-letter]	Start writing a letter to a person or conference.
Per Cederqvist's avatar
.    
Per Cederqvist committed
824

David Kågedal's avatar
David Kågedal committed
825
826
\\[kom-page-next-command]	Clear the page and do what \\[kom-next-command] does.
\\[kom-line-next-command]	Do what \\[kom-next-command] does, but scroll at most 1 line.
Per Cederqvist's avatar
.    
Per Cederqvist committed
827

David Kågedal's avatar
David Kågedal committed
828
829
0 .. 9	Give a numeric argument to the next command.
\\[describe-mode]	Display this help text.
Per Cederqvist's avatar
.    
Per Cederqvist committed
830

David Kågedal's avatar
David Kågedal committed
831
\\[kom-busy-wait]	Put the lyskom-session in wait mode. The next created text with 
832
	a priority higher than that of the next conference you are going
Per Cederqvist's avatar
.    
Per Cederqvist committed
833
	to will be read directly when it is created.
David Kågedal's avatar
David Kågedal committed
834
835
836
\\[kom-set-unread]	Mark a number of texts as unread.
\\[kom-jump]	Skip (mark as read) all the comments to this article recursively.
\\[kom-display-time]	Show the current date and time.
Per Cederqvist's avatar
.    
Per Cederqvist committed
837

David Kågedal's avatar
David Kågedal committed
838
839
840
841
\\[kom-change-presentation]	Change your presentation.
\\[kom-view]	View the specified text.
\\[kom-view-commented-text]	View the text that the current text comments or is a footnote to.
\\[kom-review-presentation]	Show the presentation for a person or a conferencce.
Per Cederqvist's avatar
.    
Per Cederqvist committed
842

David Kågedal's avatar
David Kågedal committed
843
844
\\[kom-review-comments]	View all comments to the current text.
\\[kom-review-tree]	View all comments to the current text and step through the tree
Per Cederqvist's avatar
.    
Per Cederqvist committed
845
846
	in depth-first order.

David Kågedal's avatar
David Kågedal committed
847
848
849
\\[kom-find-root-review]	View the complete comment tree.
\\[kom-find-root]	Show the root text of this comment tree.
\\[kom-review-by-to]	View the last (first or all) article written by named author
Per Cederqvist's avatar
.    
Per Cederqvist committed
850
851
	in a named conference.

David Kågedal's avatar
David Kågedal committed
852
853
854
855
\\[kom-mark-text]	Create a mark on a text.
\\[kom-unmark-text]	Remove the mark on a text.
\\[kom-review-marked-texts]	View all text marked with a certain mark.
\\[kom-review-all-marked-texts]	View all marked text.
Per Cederqvist's avatar
.    
Per Cederqvist committed
856

David Kågedal's avatar
David Kågedal committed
857
858
859
860
861
862
\\[kom-view-next-new-text]	Read the next text from the list of unread.
\\[kom-review-next]	Continue the viewing.
\\[kom-review-stack]	Show the stack of things we are viewing.
\\[kom-review-clear]	Clear the stack of things we are viewing.
\\[kom-review-backward]	Toggles the read order of reviewed texts. This can only be done
	when viewing texts with \\[kom-review-by-to], \\[kom-review-marked-texts] and \\[kom-review-all-marked-texts].
Per Cederqvist's avatar
.    
Per Cederqvist committed
863

David Kågedal's avatar
David Kågedal committed
864
865
866
\\[kom-status-conf]	Show the status of a conference.
\\[kom-status-person]	Show the status of a person
\\[kom-save-text]	Save the text you are looking at to a file.
Per Cederqvist's avatar
.    
Per Cederqvist committed
867

David Kågedal's avatar
David Kågedal committed
868
869
\\[kom-get-abuse]	Get an insulting text.
\\[kom-get-appreciation]	Get an encouraging text.
Per Cederqvist's avatar
.    
Per Cederqvist committed
870

David Kågedal's avatar
David Kågedal committed
871
872
\\[kom-add-self]	Become a member of a conference.
\\[kom-sub-self]	Removes you as a member of a conference.
Per Cederqvist's avatar
.    
Per Cederqvist committed
873
874
875
876

All bindings (this is here due to the fact that inconsistensies while 
developping this package are frequent):
\\{lyskom-mode-map}
Linus Tolke's avatar
Linus Tolke committed
877
878
879
880
881
882
Entry to this mode runs lyskom-mode-hook.

Functions and variables beginning with kom- are intended for the user to see,
set or call.
Functions and variables beginning with lyskom- are not intended for the user
to see, set of call."
Per Cederqvist's avatar
.    
Per Cederqvist committed
883
884
885
  (interactive)
  (lyskom-clear-vars)
  (setq mode-line-buffer-identification
886
	(list (concat (lyskom-mode-name-from-host) ": ") 'mode-line-conf-name))
Per Cederqvist's avatar
.    
Per Cederqvist committed
887
888
889

  (setq major-mode 'lyskom-mode)
  (setq mode-name "LysKOM")
890
  (setq mode-line-process (lyskom-get-string 'mode-line-working))
Per Cederqvist's avatar
.    
Per Cederqvist committed
891
  (use-local-map lyskom-mode-map)
David Byers's avatar
X    
David Byers committed
892
  (lyskom-set-menus 'lyskom-mode lyskom-mode-map)
893
  (run-hooks 'lyskom-mode-hook)
894

895
  (buffer-disable-undo (current-buffer))
David Kågedal's avatar
David Kågedal committed
896
  (setq buffer-read-only t))
Per Cederqvist's avatar
.    
Per Cederqvist committed
897
898
899

(defun lyskom-clear-vars ()
  "Set up buffer-local vars."
David Byers's avatar
X    
David Byers committed
900
901
902
903
904
905
906
907
908
  (lyskom-save-variables (lyskom-proc
                          lyskom-pers-no
                          lyskom-membership
                          lyskom-membership-is-read
                          lyskom-last-viewed
                          lyskom-unparsed-buffer
                          lyskom-unparsed-marker
                          lyskom-server-info
                          lyskom-server-name)
David Byers's avatar
David Byers committed
909
      (kill-all-local-variables))
David Byers's avatar
X    
David Byers committed
910
    (lyskom-setup-local-variables)
Per Cederqvist's avatar
.    
Per Cederqvist committed
911
    (setq lyskom-do-when-done (cons kom-do-when-done kom-do-when-done))
David Kågedal's avatar
David Kågedal committed
912
    (setq lyskom-output-queues (make-vector 10 nil))
913
    (setq lyskom-collate-table lyskom-default-collate-table)
914
    (setq lyskom-char-classes nil)
915
    (let ((i 0))
David Kågedal's avatar
David Kågedal committed
916
      (while (< i 10)
David Byers's avatar
X    
David Byers committed
917
918
        (aset lyskom-output-queues i (lyskom-queue-create))
        (setq i (1+ i))))
David Kågedal's avatar
David Kågedal committed
919
    (setq lyskom-pending-calls nil)
David Byers's avatar
X    
David Byers committed
920
    (lyskom-set-mode-line
David Byers's avatar
David Byers committed
921
     (lyskom-get-string 'not-present-anywhere)))