6cb8748a9ea481ed5f898654f2494978bf39c290
[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 (not riece-mixi-reply-to-only-me)
112                  (and riece-mixi-reply-to-only-me
113                       (riece-message-own-p message)))
114              (string-match riece-mixi-regexp (riece-message-text message)))
115     (let* ((url (match-string 1 (riece-message-text message)))
116            (object (mixi-make-object-from-url url)))
117       (when (mixi-object-p object)
118         (let ((target (riece-message-target message)))
119           (riece-mixi-send-object target object))))))
120
121 (defun riece-mixi-send-object-with-url (target object)
122   (condition-case nil
123       (let ((string (mixi-make-title object t))
124             (url (mixi-make-url object)))
125         (riece-mixi-send-notice target string)
126         (riece-mixi-send-notice target url))
127     (error nil)))
128
129 (defun riece-mixi-check ()
130   "Check to detect new articles.
131 If they exist, send them as notice to the corresponding channel."
132   (when (get 'riece-mixi 'riece-addon-enabled)
133     (mapc (lambda (list)
134             (let ((target (riece-parse-identity (car list)))
135                   (url-or-function (cdr list)))
136               (when (member target riece-current-channels)
137                 (let ((objects (mixi-make-objects url-or-function
138                                                   riece-mixi-check-range)))
139                   (mapc (lambda (object)
140                           (when (mixi-parent-p object)
141                             (let ((comments (mixi-get-comments
142                                              object riece-mixi-check-range)))
143                               (mapc (lambda (comment)
144                                       (let ((time (mixi-object-time comment)))
145                                         (when (mixi-time-less-p
146                                                riece-mixi-last-check time)
147                                           (riece-mixi-send-object-with-url
148                                            target comment))))
149                                     comments)))
150                           (let ((time (mixi-object-time object)))
151                             (when (mixi-time-less-p riece-mixi-last-check
152                                                     time)
153                               (riece-mixi-send-object-with-url target
154                                                                object))))
155                         objects)))))
156           riece-mixi-check-alist)
157     (setq riece-mixi-last-check (current-time))))
158
159 (defun riece-mixi-insinuate ()
160   (add-hook 'riece-after-display-message-functions
161             'riece-mixi-display-message-function))
162
163 (defun riece-mixi-enable ()
164   (when riece-mixi-check-alist
165     (setq riece-mixi-timer
166           (run-at-time riece-mixi-timer-step riece-mixi-timer-step
167                        'riece-mixi-check))
168     (setq riece-mixi-last-check (current-time))))
169
170 (defun riece-mixi-disable ()
171   (when (timerp riece-mixi-timer)
172     (cancel-timer riece-mixi-timer)
173     (setq riece-mixi-timer nil)))
174
175 (provide 'riece-mixi)
176
177 ;;; riece-mixi.el ends here