6212ff0c6dc8118c62d0f5be6280778d4bfe520f
[elisp/riece.git] / 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 (if (fboundp 'clear-string)
42     (defalias 'riece-twitter-clear-string 'clear-string)
43   (defun riece-twitter-clear-string (string)
44     (fillarray string ?\x0)))
45
46 (defun riece-twitter-set-credential (credential)
47   "Set your credential used to login to Twitter."
48   (interactive
49    (let ((username (read-string "Username: "))
50          password)
51      (unwind-protect
52          (setq password (read-passwd "Password: "))
53        (if password
54            (riece-twitter-clear-string password))
55        (setq password nil))
56      (list (concat username ":" password))))
57   (setq riece-twitter-credential credential))
58
59 (defun riece-twitter-update (status)
60   "Update your status."
61   (interactive "sStatus: ")
62   (start-process
63          "curl" nil "curl"
64          "-H" "X-Twitter-Client: Riece"
65          "-H" (concat "X-Twitter-Client-Version: " riece-version-number)
66          "-H" "X-Twitter-Client-URL: http://riece.nongnu.org/twitter.xml"
67          "-u" credential
68          "-d" "source=riece"
69          "-d" (concat "status="
70                       (riece-twitter-escape-string
71                        (encode-coding-string status 'utf-8)))
72          "-s"
73          "http://twitter.com/statuses/update.json"))
74
75 (defun riece-twitter-message-filter (message)
76   (if (and (riece-message-own-p message)
77            (eq 'action (riece-message-type message)))
78       (if riece-twitter-credential
79           (riece-twitter-update (riece-message-text message))
80         (message "%s"
81                  (substitute-command-keys
82                   "\\[riece-twitter-set-credential] to set your credential"))))
83   message)
84
85 (defun riece-twitter-escape-string (string)
86   (let ((index 0))
87     (while (string-match "[^0-9A-Za-z\-\._~:/?@!\$'()*,]" string index)
88       (setq string (replace-match
89                     (format "%%%02X" (aref string (match-beginning 0)))
90                     t t string)
91             index (+ 3 (match-beginning 0))))
92     string))
93
94 (defun riece-twitter-insinuate ()
95   (add-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
96
97 (defun riece-twitter-uninstall ()
98   (remove-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
99
100 (provide 'riece-twitter)
101
102 ;;; riece-twitter.el ends here