Fix docs.
[elisp/mixi.git] / riece-mixi.el
1 ;;; riece-mixi.el --- Riece integration for mixi
2 ;; Copyright (C) 2007 OHASHI Akira
3
4 ;; Author: OHASHI Akira <bg66@koka-in.org>
5 ;; Keywords: IRC, riece
6
7 ;; This file is *NOT* part of Riece.
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;; NOTE: This is an add-on module for Riece.
27
28 ;; If you have bug reports and/or suggestions for improvement, please
29 ;; send them via <URL:http://mixi.jp/view_community.pl?id=1596390>.
30
31 ;;; Code:
32
33 (require 'mixi)
34 (require 'mixi-utils)
35 (require 'timer)
36
37 ;; Functions and variables which should be defined in the other module
38 ;; at run-time.
39 (eval-when-compile
40   (defvar riece-current-channels)
41   (autoload 'riece-send-string "riece-server")
42   (autoload 'riece-identity-prefix "riece-identity")
43   (autoload 'riece-display-message "riece-message")
44   (autoload 'riece-make-message "riece-message")
45   (autoload 'riece-current-nickname "riece-misc")
46   (autoload 'riece-message-own-p "riece-message")
47   (autoload 'riece-message-text "riece-message")
48   (autoload 'riece-message-target "riece-message")
49   (autoload 'riece-parse-identity "riece-identity"))
50
51 (defgroup riece-mixi nil
52   "Riece integration for mixi."
53   :prefix "riece-"
54   :group 'riece)
55
56 (defcustom riece-mixi-regexp "\\(https?://\\([^.]+.\\)?mixi.jp[^ ]+\\)"
57   "*Pattern of string to retrieving to mixi."
58   :type 'string
59   :group 'riece-mixi)
60
61 (defcustom riece-mixi-reply-to-only-me nil
62   "*If non-nil, reply to only my messages."
63   :type 'boolean
64   :group 'riece-mixi)
65
66 (defcustom riece-mixi-check-alist nil
67   "*An alist for checking to detect new articles.
68 Each element looks like (CHANNEL . URL) or (CHANNEL . FUNCTION).
69 CHANNEL is a channel name.
70 URL is the URL for mixi access point of the channel.  If URL is friend's, get
71 his/her diaries as article.  If community's, get its BBSes.  If diary's or
72 BBS's, get its comments.
73 FUNCTION is the function which has one `range' argument and returns the list
74 of mixi object."
75   :type '(repeat (cons :format "%v"
76                        (string :tag "Channel")
77                        (radio (string :tag "URL")
78                               (function :tag "Other function"))))
79   :group 'riece-mixi)
80
81 (defcustom riece-mixi-check-range 1
82   "*The number of ranges that should be checked to detect new articles."
83   :type 'integer
84   :group 'riece-mixi)
85
86 (defcustom riece-mixi-timer-step 3600
87   "*Seconds for checking to detect new articles."
88   :type 'integer
89   :group 'riece-mixi)
90
91 (defvar riece-mixi-timer nil)
92 (defvar riece-mixi-last-check nil)
93
94 (defconst riece-mixi-description
95   "Riece integration for mixi.")
96
97 (defun riece-mixi-send-notice (target string)
98   (riece-send-string
99    (format "NOTICE %s :%s\r\n" (riece-identity-prefix target) string))
100   (riece-display-message
101    (riece-make-message (riece-current-nickname) target string 'notice)))
102
103 (defun riece-mixi-send-object (target object)
104   (condition-case nil
105       (let ((string (concat (mixi-make-title object t) " [AR]")))
106         (riece-mixi-send-notice target string))
107     (error nil)))
108
109 (defun riece-mixi-display-message-function (message)
110   (when (and (get 'riece-mixi 'riece-addon-enabled)
111              (or (riece-message-own-p message)
112                  (not riece-mixi-reply-to-only-me))
113              (string-match riece-mixi-regexp (riece-message-text message)))
114     (let* ((url (match-string 1 (riece-message-text message)))
115            (object (mixi-make-object-from-url url)))
116       (when (mixi-object-p object)
117         (let ((target (riece-message-target message)))
118           (riece-mixi-send-object target object))))))
119
120 (defun riece-mixi-send-object-with-url (target object)
121   (condition-case nil
122       (let ((string (mixi-make-title object t))
123             (url (mixi-make-url object)))
124         (riece-mixi-send-notice target string)
125         (riece-mixi-send-notice target url))
126     (error nil)))
127
128 (defun riece-mixi-check ()
129   "Check to detect new articles.
130 If they exist, send them as notice to the corresponding channel."
131   (when (get 'riece-mixi 'riece-addon-enabled)
132     (mapc (lambda (list)
133             (let ((target (riece-parse-identity (car list)))
134                   (url-or-function (cdr list)))
135               (when (member target riece-current-channels)
136                 (let ((objects (mixi-make-objects url-or-function
137                                                   riece-mixi-check-range)))
138                   (while objects
139                     (let ((object (car objects)))
140                       (when (mixi-parent-p object)
141                         (let ((comments (mixi-get-comments
142                                          object riece-mixi-check-range)))
143                           (while comments
144                             (let ((time (mixi-object-time (car comments))))
145                               (when (mixi-time-less-p riece-mixi-last-check
146                                                       time)
147                                 (riece-mixi-send-object-with-url
148                                  target (car comments))))
149                             (setq comments (cdr comments)))))
150                       (let ((time (mixi-object-time object)))
151                         (when (mixi-time-less-p riece-mixi-last-check time)
152                           (riece-mixi-send-object-with-url target object))))
153                     (setq objects (cdr objects)))))))
154           riece-mixi-check-alist)
155     (setq riece-mixi-last-check (current-time))))
156
157 (defun riece-mixi-insinuate ()
158   (add-hook 'riece-after-display-message-functions
159             'riece-mixi-display-message-function))
160
161 (defun riece-mixi-enable ()
162   (when riece-mixi-check-alist
163     (setq riece-mixi-timer
164           (run-at-time riece-mixi-timer-step riece-mixi-timer-step
165                        'riece-mixi-check))
166     (setq riece-mixi-last-check (current-time))))
167
168 (defun riece-mixi-disable ()
169   (when (timerp riece-mixi-timer)
170     (cancel-timer riece-mixi-timer)
171     (setq riece-mixi-timer nil)))
172
173 (provide 'riece-mixi)
174
175 ;;; riece-mixi.el ends here