48b061125d16a7245dcc528c893f97b9f8e03687
[lurk.git] / lurk.el
1 ;;; lurk.el --- Little Uni-buffer iRc Klient -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 2021 Tim Vaughan
4
5 ;; Author: Tim Vaughan <timv@ughan.xyz>
6 ;; Created: 14 June 2021
7 ;; Version: 1.0
8 ;; Keywords: network
9 ;; Homepage: http://thelambdalab.xyz/lurk
10 ;; Package-Requires: ((emacs "26"))
11
12 ;; This file is not part of GNU Emacs.
13
14 ;; This program is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this file.  If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (provide 'lurk)
32
33
34 ;;; Customizations
35 ;;
36
37 (defgroup lurk nil
38   "Little Unified iRc Klient."
39   :group 'network)
40
41 (defcustom lurk-nick "plugd"
42   "Default nick.")
43
44 (defcustom lurk-default-quit-msg "Bye"
45   "Default quit message when none supplied.")
46
47 (defcustom lurk-networks
48   '(("libera" "irc.libera.chat" 6697)
49     ("freenode" "chat.freenode.net" 6697)
50     ("tilde" "tilde.chat" 6697)
51     ("mbr" "mbrserver.com" 6667 :notls)
52     ("local" "localhost" 6697))
53   "IRC networks.")
54
55 (defcustom lurk-allow-ipv6 nil
56   "Set to non-nil to allow use of IPv6.")
57
58 (defcustom lurk-show-joins nil
59   "Set to non-nil to be notified of joins, parts and quits.")
60
61 ;;; Faces
62 ;;
63
64 (defface lurk-text
65   '((t :inherit default))
66   "Face used for Lurk text.")
67
68 (defface lurk-prompt
69   '((t :inherit org-priority))
70   "Face used for the prompt.")
71
72 (defface lurk-context
73   '((t :inherit org-tag))
74   "Face used for the context name in the prompt.")
75
76 (defface lurk-faded
77   '((t :inherit org-agenda-dimmed-todo-face))
78   "Face used for faded Lurk text.")
79
80 (defface lurk-error
81   '((t :inherit font-lock-regexp-grouping-construct))
82   "Face used for Lurk error text.")
83
84 ;;; Global variables
85 ;;
86
87 (defvar lurk-version "Lurk v0.1")
88
89 (defvar lurk-notice-prefix "-!-")
90
91 (defvar lurk-error-prefix "!!!")
92
93 (defvar lurk-prompt-string
94   (propertize "> " 'face 'lurk-prompt))
95
96
97 ;;; Network process
98 ;;
99
100 (defvar lurk-response "")
101
102 (defun lurk-filter (proc string)
103   (dolist (line (split-string (concat lurk-response string) "\n"))
104     (if (string-suffix-p "\r" line)
105         (lurk-eval-msg-string (string-trim line))
106       (setq lurk-response line))))
107
108 (defun lurk-sentinel (proc string)
109   (unless (equal "open" (string-trim string))
110     (lurk-display-error "Disconnected from server.")
111     (clrhash lurk-contexts)
112     (lurk-set-current-context nil)
113     (lurk-render-prompt)
114     (cancel-timer lurk-ping-timer)))
115
116 (defun lurk-start-process (network)
117   (let* ((row (assoc network lurk-networks))
118          (host (elt row 1))
119          (port (elt row 2))
120          (flags (seq-drop row 3)))
121     (make-network-process :name "lurk"
122                           :host host
123                           :service port
124                           :family (if lurk-allow-ipv6 nil 'ipv4)
125                           :filter #'lurk-filter
126                           :sentinel #'lurk-sentinel
127                           :nowait nil
128                           :tls-parameters (if (memq :notls flags)
129                                               nil
130                                             (cons 'gnutls-x509pki
131                                                   (gnutls-boot-parameters
132                                                    :type 'gnutls-x509pki
133                                                    :hostname host)))
134                           :buffer "*lurk*")))
135
136 (defvar lurk-ping-timer nil)
137 (defvar lurk-ping-period 60)
138
139 (defun lurk-ping-function ()
140   (lurk-send-msg (lurk-msg nil nil "PING" (car (process-contact (get-process "lurk")))))
141   (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))
142
143 (defun lurk-connect (network)
144   (if (get-process "lurk")
145       (lurk-display-error "Already connected.  Disconnect first.")
146     (if (not (assoc network lurk-networks))
147         (lurk-display-error "Network '" network "' is unknown.")
148       (clrhash lurk-contexts)
149       (lurk-set-current-context nil)
150       (lurk-start-process network)
151       (lurk-send-msg (lurk-msg nil nil "USER" lurk-nick 0 "*" lurk-nick))
152       (lurk-send-msg (lurk-msg nil nil "NICK" lurk-nick))
153       (setq lurk-ping-timer (run-with-timer lurk-ping-period nil #'lurk-ping-function)))))
154
155 (defun lurk-connected-p ()
156   (let ((proc (get-process "lurk")))
157     (and proc (eq (process-status proc) 'open))))
158
159 (defun lurk-send-msg (msg)
160   (let ((proc (get-process "lurk")))
161     (if (and proc (eq (process-status proc) 'open))
162         (process-send-string proc (concat (lurk-msg->string msg) "\r\n"))
163       (lurk-display-error "No server connection established.")
164       (error "No server connection established"))))
165
166
167 ;;; Server messages
168 ;;
169
170 (defun lurk--as-string (obj)
171   (if obj
172       (with-output-to-string (princ obj))
173     nil))
174
175 (defun lurk-msg (tags src cmd &rest params)
176   (list (lurk--as-string tags)
177         (lurk--as-string src)
178         (upcase (lurk--as-string cmd))
179         (mapcar #'lurk--as-string
180                 (if (and params (listp (elt params 0)))
181                     (elt params 0)
182                   params))))
183
184 (defun lurk-msg-tags (msg) (elt msg 0))
185 (defun lurk-msg-src (msg) (elt msg 1))
186 (defun lurk-msg-cmd (msg) (elt msg 2))
187 (defun lurk-msg-params (msg) (elt msg 3))
188 (defun lurk-msg-trail (msg)
189   (let ((params (lurk-msg-params msg)))
190     (if params
191         (elt params (- (length params) 1)))))
192
193 (defvar lurk-msg-regex
194   (rx
195    (opt (: "@" (group (* (not (or "\n" "\r" ";" " ")))))
196         (* whitespace))
197    (opt (: ":" (: (group (* (not (any space "!" "@"))))
198                   (* (not (any space)))))
199         (* whitespace))
200    (group (: (* (not whitespace))))
201    (* whitespace)
202    (opt (group (+ not-newline))))
203   "Regex used to parse IRC messages.
204 Note that this regex is incomplete.  Noteably, we discard the non-nick
205 portion of the source component of the message, as LURK doesn't use this.")
206
207 (defun lurk-string->msg (string)
208   (if (string-match lurk-msg-regex string)
209       (let* ((tags (match-string 1 string))
210              (src (match-string 2 string))
211              (cmd (upcase (match-string 3 string)))
212              (params-str (match-string 4 string))
213              (params
214               (if params-str
215                   (let* ((idx (cl-search ":" params-str))
216                          (l (split-string (string-trim (substring params-str 0 idx))))
217                          (r (if idx (list (substring params-str (+ 1 idx))) nil)))
218                     (append l r))
219                 nil)))
220         (apply #'lurk-msg (append (list tags src cmd) params)))
221     (error "Failed to parse string " string)))
222
223 (defun lurk--filtered-join (&rest args)
224   (string-join (seq-filter (lambda (el) el) args) " "))
225
226 (defun lurk-msg->string (msg)
227   (let ((tags (lurk-msg-tags msg))
228         (src (lurk-msg-src msg))
229         (cmd (lurk-msg-cmd msg))
230         (params (lurk-msg-params msg)))
231     (lurk--filtered-join
232      (if tags (concat "@" tags) nil)
233      (if src (concat ":" src) nil)
234      cmd
235      (if (> (length params) 1)
236          (string-join (seq-take params (- (length params) 1)) " ")
237        nil)
238      (if (> (length params) 0)
239          (concat ":" (elt params (- (length params) 1)))
240        nil))))
241
242
243 ;;; Contexts
244 ;;
245
246 (defvar lurk-current-context nil)
247 (defvar lurk-contexts (make-hash-table :test #'equal))
248
249 (defun lurk-add-context (name)
250   (puthash name nil lurk-contexts))
251
252 (defun lurk-del-context (name)
253   (remhash name lurk-contexts))
254
255 (defun lurk-get-context-users (name)
256   (gethash name lurk-contexts))
257
258 (defun lurk-add-context-users (context users)
259   (puthash context
260            (append users
261                    (gethash context lurk-contexts))
262            lurk-contexts))
263
264 (defun lurk-del-context-user (context user)
265   (puthash context
266            (remove user (gethash context lurk-contexts))
267            lurk-contexts))
268
269 (defun lurk-del-user (user)
270   (dolist (context (lurk-get-context-list))
271     (lurk-del-context-user context user)))
272
273 (defun lurk-rename-user (old-nick new-nick)
274   (dolist (context (lurk-get-context-list))
275     (lurk-del-context-user context old-nick)
276     (lurk-add-context-users context (list new-nick))))
277
278 (defun lurk-get-context-type (name)
279   (cond
280    ((string-prefix-p "#" name) 'channel)
281    ((string-match-p (rx (or "." "localhost")) name) 'host)
282    (t 'nick)))
283
284 (defun lurk-get-context-list ()
285   (let ((res nil))
286     (maphash (lambda (key val)
287                (cl-pushnew key res))
288              lurk-contexts)
289     res))
290
291 (defun lurk-get-next-context (&optional prev)
292   (if lurk-current-context
293       (let* ((context-list (if prev
294                                (reverse (lurk-get-context-list))
295                              (lurk-get-context-list)))
296              (context-list* (member lurk-current-context context-list)))
297         (if (> (length context-list*) 1)
298             (cadr context-list*)
299           (car context-list)))
300     nil))
301
302 (defun lurk-set-current-context (context)
303   (setq lurk-current-context context)
304   (lurk-highlight-context context))
305
306 (defun lurk-cycle-contexts (&optional rev)
307   (if lurk-current-context
308       (progn
309         (lurk-set-current-context (lurk-get-next-context rev))
310         (lurk-render-prompt))
311     (lurk-display-error "No channels joined.")))
312
313
314 ;;; Buffer
315 ;;
316
317 (defun lurk-render-prompt ()
318   (with-current-buffer "*lurk*"
319     (let ((update-point (= lurk-input-marker (point)))
320           (update-window-points (mapcar (lambda (w)
321                                           (list (= (window-point w) lurk-input-marker)
322                                                 w))
323                                         (get-buffer-window-list nil nil t))))
324       (save-excursion
325         (set-marker-insertion-type lurk-prompt-marker nil)
326         (set-marker-insertion-type lurk-input-marker t)
327         (let ((inhibit-read-only t))
328           (delete-region lurk-prompt-marker lurk-input-marker)
329           (goto-char lurk-prompt-marker)
330           (insert
331            (propertize (if lurk-current-context
332                            lurk-current-context
333                          "")
334                        'face 'lurk-context
335                        'read-only t)
336            (propertize lurk-prompt-string
337                        'face 'lurk-prompt
338                        'read-only t
339                        'rear-nonsticky t)))
340         (set-marker-insertion-type lurk-input-marker nil))
341       (if update-point
342           (goto-char lurk-input-marker))
343       (dolist (v update-window-points)
344         (if (car v)
345             (set-window-point (cadr v) lurk-input-marker))))))
346   
347 (defvar lurk-prompt-marker nil
348   "Marker for prompt position in LURK buffer.")
349
350 (defvar lurk-input-marker nil
351   "Marker for prompt position in LURK buffer.")
352
353 (defun lurk-setup-buffer ()
354   (with-current-buffer (get-buffer-create "*lurk*")
355     (setq-local scroll-conservatively 1)
356     (if (markerp lurk-prompt-marker)
357         (set-marker lurk-prompt-marker (point-max))
358       (setq lurk-prompt-marker (point-max-marker)))
359     (if (markerp lurk-input-marker)
360         (set-marker lurk-input-marker (point-max))
361       (setq lurk-input-marker (point-max-marker)))
362     (goto-char (point-max))
363     (lurk-render-prompt)))
364
365
366 ;;; Output formatting and highlighting
367 ;;
368
369 ;; Partially-implemented idea: the face text property can be
370 ;; a list of faces, applied in order.  By assigning each context
371 ;; a unique list and keeping track of these in a hash table, we can
372 ;; easily switch the face corresponding to a particular context
373 ;; by modifying the elements of this list.
374 ;;
375 ;; More subtly, we make only the cdrs of this list shared among
376 ;; all text of a given context, allowing the cars to be different
377 ;; and for different elements of the context-specific text to have
378 ;; different styling.
379
380 ;; Additionally, we can allow selective hiding of contexts via
381 ;; the buffer-invisibility-spec.
382
383 (defvar lurk-context-facelists (make-hash-table :test 'equal)
384   "List of seen contexts and associated face lists.")
385
386 (defun lurk-get-context-facelist (context)
387   (let ((facelist (gethash context lurk-context-facelists)))
388     (unless facelist
389       (setq facelist (list 'lurk-text))
390       (puthash context facelist lurk-context-facelists))
391     facelist))
392
393 (defun lurk-display-string (context &rest strings)
394   (with-current-buffer (get-buffer-create "*lurk*")
395     (save-excursion
396       (goto-char lurk-prompt-marker)
397       (let ((inhibit-read-only t)
398             (old-pos (marker-position lurk-prompt-marker))
399             (adaptive-fill-regexp (rx (= 6 anychar)))
400             (fill-column 80)
401             (context-atom (if context (intern context) nil)))
402         (insert-before-markers
403          (propertize (concat (format-time-string "%H:%M") " ")
404                      'face (lurk-get-context-facelist context)
405                      'read-only t
406                      'context context
407                      'invisible context-atom
408                      'help-echo (concat "Context: " (or context "none")))
409          (propertize (concat (apply #'concat strings) "\n")
410                      'face (lurk-get-context-facelist context)
411                      'read-only t
412                      'context context
413                      'invisible context-atom
414                      'help-echo (concat "Context: " (or context "none"))))
415         (fill-region old-pos lurk-prompt-marker nil t)))))
416
417 (defun lurk-display-message (from to text)
418   (let ((context (if (eq 'channel (lurk-get-context-type to))
419                      to
420                    (if (equal to lurk-nick) from to))))
421     (lurk-display-string
422      context
423      (pcase (lurk-get-context-type to)
424        ('channel (concat to " <" from "> "))
425        ('nick (concat "[" from " -> " to "] "))
426        (_
427         (error "Unsupported context type")))
428      text)))
429
430 (defun lurk-display-action (from to action-text)
431   (let ((context (if (eq 'channel (lurk-get-context-type to))
432                      to
433                    (if (equal to lurk-nick) from to))))
434     (lurk-display-string
435      context
436      "* " from " " action-text)))
437      
438
439 (defun lurk-display-notice (context &rest notices)
440   (lurk-display-string
441    context
442    lurk-notice-prefix " "
443    (apply #'concat notices)))
444
445 (defun lurk-display-error (&rest messages)
446   (lurk-display-string
447    nil
448    lurk-error-prefix " "
449    (apply #'concat messages)))
450
451 (defun lurk-highlight-context (context)
452   (maphash
453    (lambda (this-context facelist)
454      (if (equal this-context context)
455          (setcar facelist 'lurk-text)
456        (setcar facelist 'lurk-faded)))
457    lurk-context-facelists)
458   (force-window-update "*lurk*"))
459
460 (defun lurk-zoom-in (context)
461   (with-current-buffer "*lurk*"
462     (maphash
463      (lambda (this-context _)
464        (when this-context
465          (let ((this-context-atom (intern this-context)))
466            (if (equal this-context context)
467                (remove-from-invisibility-spec this-context-atom)
468              (add-to-invisibility-spec this-context-atom)))))
469      lurk-context-facelists)
470     (force-window-update "*lurk*")))
471
472 (defun lurk-zoom-out ()
473   (with-current-buffer "*lurk*"
474     (maphash
475      (lambda (this-context _)
476        (let ((this-context-atom (if this-context (intern this-context) nil)))
477          (remove-from-invisibility-spec this-context-atom)))
478      lurk-context-facelists)
479     (force-window-update "*lurk*")))
480
481 ;;; Message evaluation
482 ;;
483
484 (defun lurk-eval-msg-string (string)
485   ;; (lurk-display-string nil string)
486   (let* ((msg (lurk-string->msg string)))
487     (pcase (lurk-msg-cmd msg)
488       ("PING"
489        (lurk-send-msg
490         (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
491        ;; (lurk-display-notice nil "ping-pong (server initiated)"))
492
493       ("PONG")
494        ;; (lurk-display-notice nil "ping-pong (client initiated)"))
495
496       ("001"
497        (let* ((params (lurk-msg-params msg))
498               (nick (elt params 0))
499               (text (string-join (seq-drop params 1) " ")))
500          (setq lurk-nick nick)
501          (lurk-display-notice nil text)))
502
503       ("353" ; NAMEREPLY
504        (let* ((params (lurk-msg-params msg))
505               (channel (elt params 2))
506               (names (split-string (elt params 3))))
507          (lurk-add-context-users channel names)))
508
509       ("366" ; ENDOFNAMES
510        (let* ((params (lurk-msg-params msg))
511               (channel (elt params 1)))
512          (lurk-display-notice
513           channel
514           (lurk--as-string (length (lurk-get-context-users channel)))
515           " users in " channel)))
516
517       ("331"
518        (let* ((params (lurk-msg-params msg))
519               (channel (elt params 1)))
520          (lurk-display-notice
521           channel
522           "No topic set.")))
523
524       ("332"
525        (let* ((params (lurk-msg-params msg))
526               (channel (elt params 1))
527               (topic (elt params 2)))
528          (lurk-display-notice channel "Topic: " topic)))
529
530       ((rx (= 3 (any digit)))
531        (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
532
533       ((and "JOIN"
534             (guard (equal lurk-nick (lurk-msg-src msg))))
535        (let ((channel (car (lurk-msg-params msg))))
536          (lurk-add-context channel)
537          (lurk-set-current-context channel)
538          (lurk-display-notice channel "Joining channel " channel)
539          (lurk-render-prompt)))
540
541       ("JOIN"
542        (let ((channel (car (lurk-msg-params msg)))
543              (nick (lurk-msg-src msg)))
544          (lurk-add-context-users channel (list nick))
545          (if lurk-show-joins
546              (lurk-display-notice channel nick " joined channel " channel))))
547
548       ((and "PART"
549             (guard (equal lurk-nick (lurk-msg-src msg))))
550        (let ((channel (car (lurk-msg-params msg))))
551          (lurk-display-notice channel "Left channel " channel)
552          (lurk-del-context channel)
553          (if (equal channel lurk-current-context)
554              (lurk-set-current-context (lurk-get-next-context)))
555          (lurk-render-prompt)))
556
557       ("PART"
558        (let ((channel (car (lurk-msg-params msg)))
559              (nick (lurk-msg-src msg)))
560          (lurk-del-context-user channel nick)
561          (if lurk-show-joins
562              (lurk-display-notice channel nick " left channel " channel))))
563
564       ("QUIT"
565        (let ((nick (lurk-msg-src msg))
566              (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
567          (lurk-del-user nick)
568          (if lurk-show-joins
569              (lurk-display-notice nil nick " quit: " reason))))
570
571       ((and "NICK"
572             (guard (equal lurk-nick (lurk-msg-src msg))))
573        (setq lurk-nick (car (lurk-msg-params msg)))
574        (lurk-display-notice nil "Set nick to " lurk-nick))
575
576       ("NICK"
577        (let ((old-nick (lurk-msg-src msg))
578              (new-nick (car (lurk-msg-params msg))))
579          (lurk-display-notice nil old-nick " is now known as " new-nick)
580          (lurk-rename-user old-nick new-nick)))
581
582       ("NOTICE"
583        (let ((nick (lurk-msg-src msg))
584              (channel (car (lurk-msg-params msg)))
585              (text (cadr (lurk-msg-params msg))))
586          (pcase text
587            ((rx (: "\01VERSION "
588                    (let version (* (not "\01")))
589                    "\01"))
590             (lurk-display-notice nil "CTCP version reply from " nick ": " version))
591            (_
592             (lurk-display-notice nil text)))))
593
594       ("PRIVMSG"
595        (let* ((from (lurk-msg-src msg))
596               (params (lurk-msg-params msg))
597               (to (car params))
598               (text (cadr params)))
599          (pcase text
600            ("\01VERSION\01"
601             (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
602               (lurk-send-msg (lurk-msg nil nil "NOTICE"
603                                        (list from (concat "\01VERSION "
604                                                           version-string
605                                                           "\01")))))
606             (lurk-display-notice nil "CTCP version request received from " from))
607
608            ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
609             (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
610             (lurk-display-notice from "CTCP ping received from " from))
611
612            ("\01USERINFO\01"
613             (lurk-display-notice from "CTCP userinfo request from " from " (no response sent)"))
614
615            ((rx (: "\01ACTION " (let action-text (* (not "\01"))) "\01"))
616             (lurk-display-action from to action-text))
617
618            (_
619             (if (and (equal from "BitBot")
620                      (equal to "##moshpit")
621                      (cl-search "\\_o< QUACK!" text))
622               (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to ",bef")))
623             (lurk-display-message from to text)))))
624       (_
625        (lurk-display-notice nil (lurk-msg->string msg))))))
626
627
628 ;;; Command entering
629 ;;
630
631 (defun lurk-enter-string (string)
632   (if (string-prefix-p "/" string)
633       (pcase (substring string 1)
634         ((rx (: "CONNECT " (let network (* not-newline))))
635          (lurk-display-notice nil "Attempting to connect to " network "...")
636          (lurk-connect network))
637
638         ((rx (: "TOPIC " (let new-topic (* not-newline))))
639          (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
640
641         ((rx (: "ME " (let action (* not-newline))))
642          (let ((ctcp-text (concat "\01ACTION " action "\01")))
643            (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
644                                     (list lurk-current-context ctcp-text)))
645            (lurk-display-action lurk-nick lurk-current-context action)))
646
647         ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
648          (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
649                                   (list nick "\01VERSION\01")))
650          (lurk-display-notice nil "CTCP version request sent to " nick))
651
652         ((rx "PART" (opt (: " " (let channel (* not-newline)))))
653          (if (or lurk-current-context channel)
654              (lurk-send-msg (lurk-msg nil nil "PART" (if channel
655                                                          channel
656                                                        lurk-current-context)))
657            (lurk-display-error "No current channel to leave.")))
658
659         ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
660          (lurk-send-msg (lurk-msg nil nil "QUIT"
661                                   (or quit-msg lurk-default-quit-msg))))
662
663         ((rx (: "NICK" (* whitespace) string-end))
664          (lurk-display-notice nil "Current nick: " lurk-nick))
665
666         ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
667          (if (lurk-connected-p)
668              (lurk-send-msg (lurk-msg nil nil "NICK" nick))
669            (setq lurk-nick nick)
670            (lurk-display-notice nil "Set default nick to '" nick "'")))
671
672         ((rx "MSG "
673              (let to (* (not whitespace)))
674              " "
675              (let text (* not-newline)))
676          (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
677          (lurk-display-message lurk-nick to text))
678
679         ((rx (: (let cmd-str (+ (not whitespace)))
680                 (opt (: " " (let params-str (* not-newline))))))
681          (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
682                                   (if params-str
683                                       (split-string params-str)
684                                     nil)))))
685
686     (unless (string-empty-p string)
687       (if lurk-current-context
688           (progn
689             (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
690                                      lurk-current-context
691                                      string))
692             (lurk-display-message lurk-nick lurk-current-context string))
693         (lurk-display-error "No current context.")))))
694
695 (defun lurk-enter ()
696   "Enter current contents of line after prompt."
697   (interactive)
698   (with-current-buffer "*lurk*"
699     (let ((line (buffer-substring lurk-input-marker (point-max))))
700       (let ((inhibit-read-only t))
701         (delete-region lurk-input-marker (point-max)))
702       (lurk-enter-string line))))
703
704
705 ;;; Command completion
706 ;;
707
708 ;;; Interactive functions
709 ;;
710
711 (defun lurk-cycle-contexts-forward ()
712   (interactive)
713   (lurk-cycle-contexts))
714
715 (defun lurk-cycle-contexts-reverse ()
716   (interactive)
717   (lurk-cycle-contexts t))
718
719 (defvar lurk-zoomed nil
720   "Keeps track of zoom status.")
721
722 (defun lurk-toggle-zoom ()
723   (interactive)
724   (if lurk-zoomed
725       (lurk-zoom-out)
726     (lurk-zoom-in lurk-current-context))
727   (setq lurk-zoomed (not lurk-zoomed)))
728
729 ;;; Mode
730 ;;
731
732 (defvar lurk-mode-map
733   (let ((map (make-sparse-keymap)))
734     (define-key map (kbd "RET") 'lurk-enter)
735     (define-key map (kbd "<tab>") 'lurk-complete)
736     (define-key map (kbd "C-c C-z") 'lurk-toggle-zoom)
737     (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
738     (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
739     map))
740
741 (define-derived-mode lurk-mode text-mode "lurk"
742   "Major mode for LURK.")
743
744 (when (fboundp 'evil-set-initial-state)
745   (evil-set-initial-state 'lurk-mode 'insert))
746
747 ;;; Main start procedure
748 ;;
749
750 (defun lurk ()
751   "Switch to *lurk* buffer."
752   (interactive)
753   (if (get-buffer "*lurk*")
754       (switch-to-buffer "*lurk*")
755     (switch-to-buffer "*lurk*"))
756   (lurk-mode)
757   (lurk-setup-buffer)
758   "Started LURK.")
759
760
761 ;;; lurk.el ends here