* riece-twitter.el (riece-twitter-message-filter): Send a "source"
[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 t
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   (if (and (riece-message-own-p message)
48            (eq 'action (riece-message-type message)))
49       (let ((credential
50              (or riece-twitter-credential
51                  (concat (read-string "Twitter username: ") ":"
52                          (read-passwd "Twitter password: ")))))
53         (if (and riece-twitter-cache-credential
54                  (not (eq credential riece-twitter-credential)))
55             (setq riece-twitter-credential credential))
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" "source=riece"
63          "-d" (concat "status="
64                       (riece-twitter-escape-string
65                        (encode-coding-string (riece-message-text message)
66                                              'utf-8)))
67          "-s"
68          "http://twitter.com/statuses/update.json")))
69   message)
70
71 (defun riece-twitter-escape-string (string)
72   (let ((index 0))
73     (while (string-match "[^0-9A-Za-z\-\._~:/?@!\$'()*,]" string index)
74       (setq string (replace-match
75                     (format "%%%02X" (aref string (match-beginning 0)))
76                     t t string)
77             index (+ 3 (match-beginning 0))))
78     string))
79
80 (defun riece-twitter-insinuate ()
81   (add-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
82
83 (defun riece-twitter-uninstall ()
84   (remove-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
85
86 (provide 'riece-twitter)
87
88 ;;; riece-twitter.el ends here