f16a62e09605fda19f770fd889b09a40b8b401ab
[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 (require 'riece-message)
37
38 (defgroup riece-mixi nil
39   "Riece integration for mixi."
40   :prefix "riece-"
41   :group 'riece)
42
43 (defcustom riece-mixi-regexp "\\(https?://\\([^.]+.\\)?mixi.jp[^ ]+\\)"
44   "*Pattern of string to retrieving to mixi."
45   :type 'string
46   :group 'riece-mixi)
47
48 (defcustom riece-mixi-reply-to-only-me nil
49   "*If non-nil, reply to only my messages."
50   :type 'boolean
51   :group 'riece-mixi)
52
53 (defcustom riece-mixi-check-alist nil
54   "*An alist for checking to detect new articles.
55 Each element looks like (CHANNEL . URL) or (CHANNEL . FUNCTION).
56 CHANNEL is a channel name.
57 URL is the URL for mixi access point of the channel.  If URL is friend's, get
58 his/her diaries as article.  If community's, get its BBSes.  If diary's or
59 BBS's, get its comments.
60 FUNCTION is the function which has one `range' argument and returns the list
61 of mixi object."
62   :type '(repeat (cons :format "%v"
63                        (string :tag "Channel")
64                        (radio (string :tag "URL")
65                               (function :tag "Other function"))))
66   :group 'riece-mixi)
67
68 (defcustom riece-mixi-check-range 1
69   "*The number of ranges that should be checked to detect new articles."
70   :type 'integer
71   :group 'riece-mixi)
72
73 (defcustom riece-mixi-timer-step 3600
74   "*Seconds for checking to detect new articles."
75   :type 'integer
76   :group 'riece-mixi)
77
78 (defvar riece-mixi-timer nil)
79 (defvar riece-mixi-last-check nil)
80
81 (defconst riece-mixi-description
82   "Riece integration for mixi.")
83
84 (defun riece-mixi-send-notice (target string)
85   (riece-send-string
86    (format "NOTICE %s :%s\r\n" (riece-identity-prefix target) string))
87   (riece-display-message
88    (riece-make-message (riece-current-nickname) target string 'notice)))
89
90 (defun riece-mixi-send-object (target object)
91   (condition-case nil
92       (let ((string (concat (mixi-make-title object t) " [AR]")))
93         (riece-mixi-send-notice target string))
94     (error nil)))
95
96 (defun riece-mixi-display-message-function (message)
97   (when (and (get 'riece-mixi 'riece-addon-enabled)
98              (or (riece-message-own-p message)
99                  (not riece-mixi-reply-to-only-me))
100              (string-match riece-mixi-regexp (riece-message-text message)))
101     (let* ((url (match-string 1 (riece-message-text message)))
102            (object (mixi-make-object-from-url url)))
103       (when (mixi-object-p object)
104         (let ((target (riece-message-target message)))
105           (riece-mixi-send-object target object))))))
106
107 (defun riece-mixi-send-object-with-url (target object)
108   (condition-case nil
109       (let ((string (mixi-make-title object t))
110             (url (mixi-make-url object)))
111         (riece-mixi-send-notice target string)
112         (riece-mixi-send-notice target url))
113     (error nil)))
114
115 (defun riece-mixi-check ()
116   "Check to detect new articles.
117 If they exist, send them as notice to the corresponding channel."
118   (when (get 'riece-mixi 'riece-addon-enabled)
119     (mapc (lambda (list)
120             (let ((target (riece-parse-identity (car list)))
121                   (url-or-function (cdr list)))
122               (when (member target riece-current-channels)
123                 (let ((objects (mixi-make-objects url-or-function
124                                                   riece-mixi-check-range)))
125                   (while objects
126                     (let ((object (car objects)))
127                       (when (mixi-parent-p object)
128                         (let ((comments (mixi-get-comments
129                                          object riece-mixi-check-range)))
130                           (while comments
131                             (let ((time (mixi-object-time (car comments))))
132                               (when (mixi-time-less-p riece-mixi-last-check
133                                                       time)
134                                 (riece-mixi-send-object-with-url
135                                  target (car comments))))
136                             (setq comments (cdr comments)))))
137                       (let ((time (mixi-object-time object)))
138                         (when (mixi-time-less-p riece-mixi-last-check time)
139                           (riece-mixi-send-object-with-url target object))))
140                     (setq objects (cdr objects)))))))
141           riece-mixi-check-alist)
142     (setq riece-mixi-last-check (current-time))))
143
144 (defun riece-mixi-insinuate ()
145   (add-hook 'riece-after-display-message-functions
146             'riece-mixi-display-message-function))
147
148 (defun riece-mixi-enable ()
149   (when riece-mixi-check-alist
150     (setq riece-mixi-timer
151           (run-at-time riece-mixi-timer-step riece-mixi-timer-step
152                        'riece-mixi-check))
153     (setq riece-mixi-last-check (current-time))))
154
155 (defun riece-mixi-disable ()
156   (when (timerp riece-mixi-timer)
157     (cancel-timer riece-mixi-timer)
158     (setq riece-mixi-timer nil)))
159
160 (provide 'riece-mixi)
161
162 ;;; riece-mixi.el ends here