X-Git-Url: https://cgit.sxemacs.org/?p=riece;a=blobdiff_plain;f=lisp%2Friece-log.el;h=8c3767368d71487a3c6f4c990e0597801ad5ca97;hp=42ac8b3fccd114fc22ac22a65f9a709eed523b17;hb=054dd7af144ca7d663ec5551c0bb609067f4e87a;hpb=ce06cb691e8251eccad1d2450b0360cc25758077 diff --git a/lisp/riece-log.el b/lisp/riece-log.el index 42ac8b3..8c37673 100644 --- a/lisp/riece-log.el +++ b/lisp/riece-log.el @@ -1,7 +1,9 @@ ;;; riece-log.el --- saving irc logs add-on ;; Copyright (C) 2003 OHASHI Akira +;; Copyright (C) 2004 Daiki Ueno ;; Author: OHASHI Akira +;; Daiki Ueno ;; Keywords: IRC, riece ;; This file is part of Riece. @@ -26,11 +28,12 @@ ;; This add-on saves irc logs for every channel. ;; To use, add the following line to your ~/.riece/init.el: -;; (add-to-list 'riece-addons 'riece-log t) +;; (add-to-list 'riece-addons 'riece-log) ;;; Code: -(eval-when-compile (require 'riece-message)) +(require 'riece-message) +(require 'riece-button) (defgroup riece-log nil "Save irc log" @@ -52,72 +55,192 @@ "*If non-nil, irc messages flash back from log files. If integer, flash back only this line numbers. t means all lines." :type '(choice (integer :tag "line numbers") - (boolean :tag "flash back or not")) + (const t :tag "of the day") + (const nil :tag "no flashback")) :group 'riece-log) +(defcustom riece-log-coding-system nil + "*Coding system used for log files." + :type 'symbol + :group 'riece-log) + +(defcustom riece-log-file-name-coding-system + (if (boundp 'file-name-coding-system) + file-name-coding-system) + "*Coding system used for filenames of log files." + :type 'symbol + :group 'riece-log) + +(defcustom riece-log-open-directory-function 'find-file + "*Function for opening a directory." + :type 'function + :group 'riece-log) + +(defface riece-log-date-face + '((((class color) + (background dark)) + (:foreground "Gray70")) + (((class color) + (background light)) + (:foreground "DimGray")) + (t + (:bold t))) + "Face used for displaying \"(YYYY/MM/dd)\" extent." + :group 'riece-highlight-faces) +(defvar riece-log-date-face 'riece-log-date-face) + +(defvar riece-log-enabled nil) + +(defconst riece-log-description + "Saving IRC logs") + (defun riece-log-display-message-function (message) - (let ((open-bracket - (funcall riece-message-make-open-bracket-function message)) - (close-bracket - (funcall riece-message-make-close-bracket-function message)) - (name - (funcall riece-message-make-name-function message)) - (file (riece-log-get-file (riece-message-target message)))) - (unless (file-directory-p (file-name-directory file)) - (make-directory (file-name-directory file) t)) - (with-temp-buffer - (insert (concat (format-time-string "%H:%M") " " - open-bracket name close-bracket - " " (riece-message-text message) "\n")) - (write-region (point-min) (point-max) file t 0)))) + (if riece-log-enabled + (let ((file (riece-log-get-file (riece-message-target message))) + (coding-system-for-write riece-log-coding-system) + file-name-coding-system + default-file-name-coding-system) + (unless (file-directory-p (file-name-directory file)) + (make-directory (file-name-directory file) t)) + (write-region (concat (format-time-string "%H:%M") " " + (riece-format-message message)) + nil file t 0)))) (defun riece-log-get-file (identity) (expand-file-name - (concat (format-time-string "%Y%m%d") ".log") + (concat (format-time-string "%Y%m%d") ".txt") (riece-log-get-directory identity))) +(defun riece-log-get-files (identity) + (let ((directory (riece-log-get-directory identity))) + (if (file-directory-p directory) + (nreverse (sort (directory-files directory t + (concat "^" + (riece-make-interval-regexp "[0-9]" 8) + "\\.txt$") + t) + #'string-lessp))))) + (defun riece-log-get-directory (identity) - (let ((channel (riece-identity-prefix identity)) - (server (riece-identity-server identity))) - (let ((map (assoc channel riece-log-directory-map))) - (if map - (expand-file-name (cdr map) riece-log-directory) - (if (string-match (concat riece-channel-regexp - "\\([^:]+\\)\\(:\\*\\.\\(.*\\)\\)?") channel) - (let ((name (match-string 1 channel)) - (suffix (match-string 3 channel))) - (let ((name (if suffix (concat name "-" suffix) name))) - (if server - (expand-file-name - name - (expand-file-name server riece-log-directory)) - (expand-file-name name riece-log-directory)))) - riece-log-directory))))) + (let ((prefix (riece-identity-canonicalize-prefix + (riece-identity-prefix identity))) + (server (riece-identity-server identity)) + (map (assoc (riece-format-identity identity) riece-log-directory-map))) + (if map + (expand-file-name (cdr map) riece-log-directory) + (expand-file-name (riece-log-encode-file-name prefix) + (expand-file-name + (concat "." (riece-log-encode-file-name server)) + riece-log-directory))))) + +(defun riece-log-encode-file-name (file-name) + (if riece-log-file-name-coding-system + (setq file-name + (encode-coding-string file-name + riece-log-file-name-coding-system))) + (let ((index 0) + c) + (while (string-match "[^-0-9A-Za-z_\x80-\xFF]" file-name index) + (setq c (aref file-name (match-beginning 0))) + (if (eq c ?=) + (setq file-name (replace-match "==" nil t file-name) + index (1+ (match-end 0))) + (setq file-name (replace-match (format "=%02X" c) nil t file-name) + index (+ 2 (match-end 0))))) + file-name)) + +(defun riece-log-decode-file-name (file-name) + (let ((index 0)) + (while (string-match "==\\|=\\([0-7][0-9A-F]\\)" file-name index) + (setq file-name (replace-match + (if (eq (aref file-name (1- (match-end 0))) ?=) + "=" + (char-to-string + (car (read-from-string + (concat "?\\x" (match-string 1 file-name)))))) + nil t file-name) + index (1+ (match-beginning 0)))) + file-name) + (if riece-log-file-name-coding-system + (setq file-name + (decode-coding-string file-name + riece-log-file-name-coding-system))) + file-name) + +(defun riece-log-insert (identity lines) + "Insert logs for IDENTITY at most LINES. +If LINES is t, insert today's logs entirely." + (if (eq lines t) + (let ((file (riece-log-get-file identity))) + (if (file-exists-p file) + (insert-file-contents file))) + (let ((files (riece-log-get-files identity)) + (lines (- lines)) + name date point) + (while (and (< lines 0) files) + (if (and (file-exists-p (car files)) + (string-match (concat (riece-make-interval-regexp "[0-9]" 8) + "\\.txt$") + (setq name (file-name-nondirectory + (car files))))) + (save-restriction + (narrow-to-region (point) (point)) + (insert-file-contents (car files)) + (goto-char (point-max)) + (setq lines (forward-line lines)) + (delete-region (point-min) (point)) + (unless (equal name (format-time-string "%Y%m%d.txt")) + (setq date (concat " (" (substring name 0 4) "/" + (substring name 4 6) "/" + (substring name 6 8) ")")) + (while (not (eobp)) + (end-of-line) + (setq point (point)) + (insert date) + (put-text-property point (point) + 'riece-overlay-face 'riece-log-date-face) + (forward-line)) + (goto-char (point-min))))) + (setq files (cdr files)))))) (defun riece-log-flashback (identity) (when riece-log-flashback - (let ((file (riece-log-get-file identity))) - (when (file-exists-p file) - (let (string) - (with-temp-buffer - (insert-file-contents file) - (if (not (integerp riece-log-flashback)) - (goto-char (point-min)) - (goto-char (point-max)) - (forward-line (- riece-log-flashback))) - (setq string (buffer-substring (point) (point-max)))) - (let (buffer-read-only) - (goto-char (point-max)) - (insert string) - (goto-char (point-max)) - (set-window-point (get-buffer-window (current-buffer)) - (point)))))))) + (riece-insert-info (current-buffer) + (if (eq riece-log-flashback t) + "Recent messages of the day:\n" + (format "Recent messages up to %d lines:\n" + riece-log-flashback))) + (let (buffer-read-only + (point (goto-char (point-max)))) + (insert (with-temp-buffer + (riece-log-insert identity riece-log-flashback) + (buffer-string))) + (goto-char point) + (while (re-search-forward + (concat "^" riece-time-prefix-regexp + "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)") + nil t) + (put-text-property (1+ (match-beginning 1)) (1- (match-end 1)) + 'riece-identity + (riece-make-identity + (riece-match-string-no-properties 1) + (riece-identity-server identity)))) + (run-hook-with-args 'riece-after-insert-functions + point (goto-char (point-max))) + (set-window-point (get-buffer-window (current-buffer)) + (point))))) (defun riece-log-open-directory (&optional channel) (interactive) - (if channel - (find-file (riece-log-get-directory channel)) - (find-file riece-log-directory))) + (let ((directory (riece-log-get-directory + (or channel riece-current-channel)))) + (if (file-directory-p directory) + (funcall riece-log-open-directory-function directory) + (error "No log directory")))) + +(defun riece-log-requires () + (if (memq 'riece-button riece-addons) + '(riece-button))) (defun riece-log-insinuate () ;; FIXME: Use `riece-after-insert-functions' for trapping change, @@ -127,6 +250,15 @@ If integer, flash back only this line numbers. t means all lines." (add-hook 'riece-channel-buffer-create-functions 'riece-log-flashback)) +(defvar riece-command-mode-map) +(defun riece-log-enable () + (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory) + (setq riece-log-enabled t)) + +(defun riece-log-disable () + (define-key riece-command-mode-map "\C-cd" nil) + (setq riece-log-enabled nil)) + (provide 'riece-log) ;;; riece-log.el ends here