Proper handling of topic messages.
[lurk.git] / lurk.el
1 ;;; lurk.el --- Little Unified 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 (defun lurk-cycle-contexts-forward ()
314   (interactive)
315   (lurk-cycle-contexts))
316
317 (defun lurk-cycle-contexts-reverse ()
318   (interactive)
319   (lurk-cycle-contexts t))
320
321
322 ;;; Buffer
323 ;;
324 (defun lurk-render-prompt ()
325   (with-current-buffer "*lurk*"
326     (let ((update-point (= lurk-input-marker (point)))
327           (update-window-points (mapcar (lambda (w)
328                                           (list (= (window-point w) lurk-input-marker)
329                                                 w))
330                                         (get-buffer-window-list nil nil t))))
331       (save-excursion
332         (set-marker-insertion-type lurk-prompt-marker nil)
333         (set-marker-insertion-type lurk-input-marker t)
334         (let ((inhibit-read-only t))
335           (delete-region lurk-prompt-marker lurk-input-marker)
336           (goto-char lurk-prompt-marker)
337           (insert
338            (propertize (if lurk-current-context
339                            lurk-current-context
340                          "")
341                        'face 'lurk-context
342                        'read-only t)
343            (propertize lurk-prompt-string
344                        'face 'lurk-prompt
345                        'read-only t
346                        'rear-nonsticky t)))
347         (set-marker-insertion-type lurk-input-marker nil))
348       (if update-point
349           (goto-char lurk-input-marker))
350       (dolist (v update-window-points)
351         (if (car v)
352             (set-window-point (cadr v) lurk-input-marker))))))
353   
354 (defvar lurk-prompt-marker nil
355   "Marker for prompt position in LURK buffer.")
356
357 (defvar lurk-input-marker nil
358   "Marker for prompt position in LURK buffer.")
359
360 (defun lurk-setup-buffer ()
361   (with-current-buffer (get-buffer-create "*lurk*")
362     (setq-local scroll-conservatively 1)
363     (if (markerp lurk-prompt-marker)
364         (set-marker lurk-prompt-marker (point-max))
365       (setq lurk-prompt-marker (point-max-marker)))
366     (if (markerp lurk-input-marker)
367         (set-marker lurk-input-marker (point-max))
368       (setq lurk-input-marker (point-max-marker)))
369     (goto-char (point-max))
370     (lurk-render-prompt)))
371
372
373 ;;; Output formatting and highlighting
374 ;;
375
376 ;; Partially-implemented idea: the face text property can be
377 ;; a list of faces, applied in order.  By assigning each context
378 ;; a unique list and keeping track of these in a hash table, we can
379 ;; easily switch the face corresponding to a particular context
380 ;; by modifying the elements of this list.
381 ;;
382 ;; More subtly, we make only the cdrs of this list shared among
383 ;; all text of a given context, allowing the cars to be different
384 ;; and for different elements of the context-specific text to have
385 ;; different styling.
386
387 ;; Additionally, we can allow selective hiding of contexts via
388 ;; the buffer-invisibility-spec.
389
390 (defvar lurk-context-facelists (make-hash-table :test 'equal)
391   "List of seen contexts and associated face lists.")
392
393 (defun lurk-get-context-facelist (context)
394   (let ((facelist (gethash context lurk-context-facelists)))
395     (unless facelist
396       (setq facelist (list 'lurk-text))
397       (puthash context facelist lurk-context-facelists))
398     facelist))
399
400 (defun lurk-display-string (context &rest strings)
401   (with-current-buffer (get-buffer-create "*lurk*")
402     (save-excursion
403       (goto-char lurk-prompt-marker)
404       (let ((inhibit-read-only t)
405             (old-pos (marker-position lurk-prompt-marker))
406             (adaptive-fill-regexp (rx (= 6 anychar)))
407             (fill-column 80)
408             (context-atom (if context (intern context) nil)))
409         (insert-before-markers
410          (propertize (concat (format-time-string "%H:%M") " ")
411                      'face (lurk-get-context-facelist context)
412                      'read-only t
413                      'context context
414                      'invisible context-atom
415                      'help-echo (concat "Context: " (or context "none")))
416          (propertize (concat (apply #'concat strings) "\n")
417                      'face (lurk-get-context-facelist context)
418                      'read-only t
419                      'context context
420                      'invisible context-atom
421                      'help-echo (concat "Context: " (or context "none"))))
422         (fill-region old-pos lurk-prompt-marker nil t)))))
423
424 (defun lurk-display-message (from to text)
425   (let ((context (if (eq 'channel (lurk-get-context-type to))
426                      to
427                    (if (equal to lurk-nick) from to))))
428     (lurk-display-string
429      context
430      (pcase (lurk-get-context-type to)
431        ('channel (concat to " <" from "> " text))
432        ('nick (concat "[" from " -> " to "] " text))
433        (_
434         (error "Unsupported context type"))))))
435
436 (defun lurk-display-notice (context &rest notices)
437   (lurk-display-string
438    context
439    lurk-notice-prefix " "
440    (apply #'concat notices)))
441
442 (defun lurk-display-error (&rest messages)
443   (lurk-display-string
444    nil
445    lurk-error-prefix " "
446    (apply #'concat messages)))
447
448 (defun lurk-highlight-context (context)
449   (maphash
450    (lambda (this-context facelist)
451      (if (equal this-context context)
452          (setcar facelist 'lurk-text)
453        (setcar facelist 'lurk-faded)))
454    lurk-context-facelists)
455   (force-window-update "*lurk*"))
456
457 (defun lurk-zoom-in (context)
458   (with-current-buffer "*lurk*"
459     (maphash
460      (lambda (this-context _)
461        (let ((this-context-atom (if this-context (intern this-context) nil)))
462          (if (equal this-context context)
463              (remove-from-invisibility-spec this-context-atom)
464            (add-to-invisibility-spec this-context-atom))))
465      lurk-context-facelists)
466     (force-window-update "*lurk*")))
467
468 (defun lurk-zoom-out ()
469   (with-current-buffer "*lurk*"
470     (maphash
471      (lambda (this-context _)
472        (let ((this-context-atom (if this-context (intern this-context) nil)))
473          (remove-from-invisibility-spec this-context-atom)))
474      lurk-context-facelists)
475     (force-window-update "*lurk*")))
476
477 ;;; Message evaluation
478 ;;
479
480 (defun lurk-eval-msg-string (string)
481   ;; (lurk-display-string nil string)
482   (let* ((msg (lurk-string->msg string)))
483     (pcase (lurk-msg-cmd msg)
484       ("PING"
485        (lurk-send-msg
486         (lurk-msg nil nil "PONG" (lurk-msg-params msg))))
487        ;; (lurk-display-notice nil "ping-pong (server initiated)"))
488
489       ("PONG")
490        ;; (lurk-display-notice nil "ping-pong (client initiated)"))
491
492       ("001"
493        (let* ((params (lurk-msg-params msg))
494               (nick (elt params 0))
495               (text (string-join (seq-drop params 1) " ")))
496          (setq lurk-nick nick)
497          (lurk-display-notice nil text)))
498
499       ("353" ; NAMEREPLY
500        (let* ((params (lurk-msg-params msg))
501               (channel (elt params 2))
502               (names (split-string (elt params 3))))
503          (lurk-add-context-users channel names)))
504
505       ("366" ; ENDOFNAMES
506        (let* ((params (lurk-msg-params msg))
507               (channel (elt params 1)))
508          (lurk-display-notice
509           channel
510           (lurk--as-string (length (lurk-get-context-users channel)))
511           " users in " channel)))
512
513       ("331"
514        (let* ((params (lurk-msg-params msg))
515               (channel (elt params 1)))
516          (lurk-display-notice
517           channel
518           "No topic set.")))
519
520       ("332"
521        (lurk-display-notice nil "Detected 332: "string)
522        (let* ((params (lurk-msg-params msg))
523               (channel (elt params 1))
524               (topic (elt params 2)))
525          (lurk-display-notice channel "Topic: " topic)))
526
527       ((rx (= 3 (any digit)))
528        (lurk-display-notice nil (mapconcat 'identity (cdr (lurk-msg-params msg)) " ")))
529
530       ((and "JOIN"
531             (guard (equal lurk-nick (lurk-msg-src msg))))
532        (let ((channel (car (lurk-msg-params msg))))
533          (lurk-add-context channel)
534          (lurk-set-current-context channel)
535          (lurk-display-notice channel "Joining channel " channel)
536          (lurk-render-prompt)))
537
538       ("JOIN"
539        (let ((channel (car (lurk-msg-params msg)))
540              (nick (lurk-msg-src msg)))
541          (lurk-add-context-users channel (list nick))
542          (if lurk-show-joins
543              (lurk-display-notice channel nick " joined channel " channel))))
544
545       ((and "PART"
546             (guard (equal lurk-nick (lurk-msg-src msg))))
547        (let ((channel (car (lurk-msg-params msg))))
548          (lurk-display-notice channel "Left channel " channel)
549          (lurk-del-context channel)
550          (if (equal channel lurk-current-context)
551              (lurk-set-current-context (lurk-get-next-context)))
552          (lurk-render-prompt)))
553
554       ("PART"
555        (let ((channel (car (lurk-msg-params msg)))
556              (nick (lurk-msg-src msg)))
557          (lurk-del-context-user channel nick)
558          (if lurk-show-joins
559              (lurk-display-notice channel nick " left channel " channel))))
560
561       ("QUIT"
562        (let ((nick (lurk-msg-src msg))
563              (reason (mapconcat 'identity (lurk-msg-params msg) " ")))
564          (lurk-del-user nick)
565          (if lurk-show-joins
566              (lurk-display-notice nil nick " quit: " reason))))
567
568       ((and "NICK"
569             (guard (equal lurk-nick (lurk-msg-src msg))))
570        (setq lurk-nick (car (lurk-msg-params msg)))
571        (lurk-display-notice nil "Set nick to " lurk-nick))
572
573       ("NICK"
574        (let ((old-nick (lurk-msg-src msg))
575              (new-nick (car (lurk-msg-params msg))))
576          (lurk-display-notice nil old-nick " is now known as " new-nick)
577          (lurk-rename-user old-nick new-nick)))
578
579       ("NOTICE"
580        (let ((nick (lurk-msg-src msg))
581              (channel (car (lurk-msg-params msg)))
582              (text (cadr (lurk-msg-params msg))))
583          (pcase text
584            ((rx (: "\01VERSION "
585                    (let version (* (not "\01")))
586                    "\01"))
587             (lurk-display-notice nil "CTCP version reply from " nick ": " version))
588            (_
589             (lurk-display-notice nil text)))))
590
591       ("PRIVMSG"
592        (let* ((from (lurk-msg-src msg))
593               (params (lurk-msg-params msg))
594               (to (car params))
595               (text (cadr params)))
596          (pcase text
597            ("\01VERSION\01"
598             (let ((version-string (concat lurk-version " - running on GNU Emacs " emacs-version)))
599               (lurk-send-msg (lurk-msg nil nil "NOTICE"
600                                        (list from (concat "\01VERSION "
601                                                           version-string
602                                                           "\01")))))
603             (lurk-display-notice nil "CTCP version request received from " from))
604
605            ((rx (let ping (: "\01PING " (* (not "\01")) "\01")))
606             (lurk-send-msg (lurk-msg nil nil "NOTICE" (list from ping)))
607             (lurk-display-notice "CTCP ping received from " from))
608
609            ("\01USERINFO\01"
610             (lurk-display-notice "CTCP userinfo request from " from " (no response sent)"))
611
612            (_
613             (if (and (equal from "BitBot")
614                      (equal to "##moshpit")
615                      (cl-search "\\_o< QUACK!" text))
616               (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to ",bef")))
617             (lurk-display-message from to text)))))
618       (_
619        (lurk-display-notice nil (lurk-msg->string msg))))))
620
621
622 ;;; Command entering
623 ;;
624
625 (defun lurk-enter-string (string)
626   (if (string-prefix-p "/" string)
627       (pcase (substring string 1)
628         ((rx (: "CONNECT " (let network (* not-newline))))
629          (lurk-display-notice nil "Attempting to connect to " network "...")
630          (lurk-connect network))
631
632         ((rx (: "TOPIC " (let new-topic (* not-newline))))
633          (lurk-send-msg (lurk-msg nil nil "TOPIC" lurk-current-context new-topic)))
634
635         ((rx (: "ME " (let action (* not-newline))))
636          (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
637                                   (list lurk-current-context
638                                         (concat "\01ACTION " action "\01"))))
639          (lurk-display-action lurk-nick action))
640
641         ((rx (: "VERSION" " " (let nick (+ (not whitespace)))))
642          (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
643                                   (list nick "\01VERSION\01")))
644          (lurk-display-notice nil "CTCP version request sent to " nick))
645
646         ((rx "PART" (opt (: " " (let channel (* not-newline)))))
647          (if (or lurk-current-context channel)
648              (lurk-send-msg (lurk-msg nil nil "PART" (if channel
649                                                          channel
650                                                        lurk-current-context)))
651            (lurk-display-error "No current channel to leave.")))
652
653         ((rx "QUIT" (opt (: " " (let quit-msg (* not-newline)))))
654          (lurk-send-msg (lurk-msg nil nil "QUIT"
655                                   (or quit-msg lurk-default-quit-msg))))
656
657         ((rx (: "NICK" (* whitespace) string-end))
658          (lurk-display-notice nil "Current nick: " lurk-nick))
659
660         ((rx (: "NICK" (+ whitespace) (let nick (+ (not whitespace)))))
661          (if (lurk-connected-p)
662              (lurk-send-msg (lurk-msg nil nil "NICK" nick))
663            (setq lurk-nick nick)
664            (lurk-display-notice nil "Set default nick to '" nick "'")))
665
666         ((rx "MSG "
667              (let to (* (not whitespace)))
668              " "
669              (let text (* not-newline)))
670          (lurk-send-msg (lurk-msg nil nil "PRIVMSG" to text))
671          (lurk-display-message lurk-nick to text))
672
673         ((rx (: (let cmd-str (+ (not whitespace)))
674                 (opt (: " " (let params-str (* not-newline))))))
675          (lurk-send-msg (lurk-msg nil nil (upcase cmd-str)
676                                   (if params-str
677                                       (split-string params-str)
678                                     nil)))))
679
680     (unless (string-empty-p string)
681       (if lurk-current-context
682           (progn
683             (lurk-send-msg (lurk-msg nil nil "PRIVMSG"
684                                      lurk-current-context
685                                      string))
686             (lurk-display-message lurk-nick lurk-current-context string))
687         (lurk-display-error "No current context.")))))
688
689 (defun lurk-enter ()
690   "Enter current contents of line after prompt."
691   (interactive)
692   (with-current-buffer "*lurk*"
693     (let ((line (buffer-substring lurk-input-marker (point-max))))
694       (let ((inhibit-read-only t))
695         (delete-region lurk-input-marker (point-max)))
696       (lurk-enter-string line))))
697
698
699 ;;; Mode
700 ;;
701
702 (defvar lurk-mode-map
703   (let ((map (make-sparse-keymap)))
704     (define-key map (kbd "RET") 'lurk-enter)
705     (define-key map (kbd "<C-tab>") 'lurk-cycle-contexts-forward)
706     (define-key map (kbd "<C-S-tab>") 'lurk-cycle-contexts-reverse)
707     map))
708
709 (define-derived-mode lurk-mode text-mode "lurk"
710   "Major mode for LURK.")
711
712 (when (fboundp 'evil-set-initial-state)
713   (evil-set-initial-state 'lurk-mode 'insert))
714
715 ;;; Main start procedure
716 ;;
717
718 (defun lurk ()
719   "Switch to *lurk* buffer."
720   (interactive)
721   (if (get-buffer "*lurk*")
722       (switch-to-buffer "*lurk*")
723     (switch-to-buffer "*lurk*"))
724   (lurk-mode)
725   (lurk-setup-buffer)
726   "Started LURK.")
727
728
729 ;;; lurk.el ends here