* mixi.el (mixi-entity-alist): New constant.
[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 ((url (mixi-make-url object)))
110         (riece-mixi-send-notice target url))
111     (error nil)))
112
113 (defun riece-mixi-check ()
114   "Check to detect new articles.
115 If they exist, send them as notice to the corresponding channel."
116   (when (get 'riece-mixi 'riece-addon-enabled)
117     (mapc (lambda (list)
118             (let ((target (riece-parse-identity (car list)))
119                   (url-or-function (cdr list)))
120               (when (member target riece-current-channels)
121                 (let ((objects (mixi-make-objects url-or-function
122                                                   riece-mixi-check-range)))
123                   (while objects
124                     (let ((object (car objects)))
125                       (when (mixi-parent-p object)
126                         (let ((comments (mixi-get-comments
127                                          object riece-mixi-check-range)))
128                           (while comments
129                             (let ((time (mixi-object-time (car comments))))
130                               (when (mixi-time-less-p riece-mixi-last-check
131                                                       time)
132                                 (riece-mixi-send-object-with-url
133                                  target (car comments))))
134                             (setq comments (cdr comments)))))
135                       (let ((time (mixi-object-time object)))
136                         (when (mixi-time-less-p riece-mixi-last-check time)
137                           (riece-mixi-send-object-with-url target object))))
138                     (setq objects (cdr objects)))))))
139           riece-mixi-check-alist)
140     (setq riece-mixi-last-check (current-time))))
141
142 (defun riece-mixi-insinuate ()
143   (add-hook 'riece-after-display-message-functions
144             'riece-mixi-display-message-function))
145
146 (defun riece-mixi-enable ()
147   (when riece-mixi-check-alist
148     (setq riece-mixi-timer
149           (run-at-time riece-mixi-timer-step riece-mixi-timer-step
150                        'riece-mixi-check))
151     (setq riece-mixi-last-check (current-time))))
152
153 (defun riece-mixi-disable ()
154   (when (timerp riece-mixi-timer)
155     (cancel-timer riece-mixi-timer)
156     (setq riece-mixi-timer nil)))
157
158 (provide 'riece-mixi)
159
160 ;;; riece-mixi.el ends here