(riece-twitter-credential): Define with defcustom.
[riece] / lisp / riece-twitter.el
1 ;;; riece-twitter.el --- post your status to Twitter
2 ;; Copyright (C) 2007 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: IRC, riece
6
7 ;; This file is 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 ;;; Code:
29
30 (require 'riece-message)
31
32 (defgroup riece-twitter nil
33   "Post your status to Twitter"
34   :group 'riece)
35   
36 (defcustom riece-twitter-credential nil
37   "Your credential used to login to Twitter."
38   :group 'riece-twitter
39   :type 'string)
40
41 (defcustom riece-twitter-cache-credential nil
42   "If non-nil, cache your credential on Twitter."
43   :group 'riece-twitter
44   :type 'boolean)
45
46 (defun riece-twitter-message-filter (message)
47   (let ((credential
48          (or riece-twitter-credential
49              (concat (read-string "Twitter username: ") ":"
50                      (read-passwd "Twitter password: ")))))
51     (if (and riece-twitter-cache-credential
52              (not (eq credential riece-twitter-credential)))
53         (setq riece-twitter-credential credential))
54     (if (and (riece-message-own-p message)
55              (eq 'action (riece-message-type message)))
56         (start-process
57          "curl" nil "curl"
58          "-H" "X-Twitter-Client: Riece"
59          "-H" (concat "X-Twitter-Client-Version: " riece-version-number)
60          "-H" "X-Twitter-Client-URL: http://riece.nongnu.org/twitter.xml"
61          "-u" credential
62          "-d" (concat "status="
63                       (riece-twitter-escape-string
64                        (encode-coding-string (riece-message-text message)
65                                              'utf-8)))
66          "-s"
67          "http://twitter.com/statuses/update.json")))
68   message)
69
70 (defun riece-twitter-escape-string (string)
71   (let ((index 0))
72     (while (string-match "[^0-9A-Za-z\-\._~:/?@!\$'()*,]" string index)
73       (setq string (replace-match
74                     (format "%%%02X" (aref string (match-beginning 0)))
75                     t t string)
76             index (+ 3 (match-beginning 0))))
77     string))
78
79 (defun riece-twitter-insinuate ()
80   (add-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
81
82 (defun riece-twitter-uninstall ()
83   (remove-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
84
85 (provide 'riece-twitter)
86
87 ;;; riece-twitter.el ends here