* riece-kakasi.el (riece-kakasi-convert-string): Suppress
[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 (eval-and-compile
42   (if (fboundp 'clear-string)
43       (defalias 'riece-twitter-clear-string 'clear-string)
44     (defun riece-twitter-clear-string (string)
45       (fillarray string ?\0))))
46
47 (defun riece-twitter-set-credential (credential)
48   "Set your credential used to login to Twitter."
49   (interactive
50    (let ((username (read-string "Username: "))
51          password)
52      (unwind-protect
53          (list (concat username ":"
54                        (setq password (read-passwd "Password: "))))
55        (if password
56            (riece-twitter-clear-string password))
57        (setq password nil))))
58   (setq riece-twitter-credential credential))
59
60 (defun riece-twitter-update (status)
61   "Update your status."
62   (interactive
63    (progn
64      (unless riece-twitter-credential
65        (error "%s"
66               (substitute-command-keys
67                "\\[riece-twitter-set-credential] to set your credential")))
68      (list (read-string "Status: "))))
69   (message "Sending to Twitter...")
70   (let* ((args
71           (list "-u" riece-twitter-credential
72                 "-d" (concat "status="
73                              (riece-twitter-escape-string
74                               (encode-coding-string status 'utf-8)))
75                 "-s"
76                 "http://twitter.com/statuses/update.json"))
77          (process
78           (apply #'start-process
79                  "curl" nil "curl"
80                  (if (interactive-p)
81                      args
82                    (append args
83                            (list "-H" "X-Twitter-Client: Riece"
84                                  "-H" (concat "X-Twitter-Client-Version: "
85                                               riece-version-number)
86                                  "-H" "X-Twitter-Client-URL: http://riece.nongnu.org/twitter.xml"
87                                  "-d" "source=riece"))))))
88     (set-process-sentinel process #'riece-twitter-sentinel)))
89
90 (defun riece-twitter-sentinel (process status)
91   (if (equal status "finished\n")
92       (message "Sending to Twitter...done")
93     (message "Sending to Twitter...failed: %s"
94              (substring status 0 (1- (length status))))))
95
96 (defun riece-twitter-message-filter (message)
97   (if (and (riece-message-own-p message)
98            (eq 'action (riece-message-type message)))
99       (if riece-twitter-credential
100           (riece-twitter-update (riece-message-text message))
101         (message "%s"
102                  (substitute-command-keys
103                   "\\[riece-twitter-set-credential] to set your credential"))))
104   message)
105
106 (defun riece-twitter-escape-string (string)
107   (let ((index 0))
108     (while (string-match "[^0-9A-Za-z\-\._~:/?@!\$'()*,]" string index)
109       (setq string (replace-match
110                     (format "%%%02X" (aref string (match-beginning 0)))
111                     t t string)
112             index (+ 3 (match-beginning 0))))
113     string))
114
115 (defun riece-twitter-insinuate ()
116   (add-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
117
118 (defun riece-twitter-uninstall ()
119   (remove-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
120
121 (provide 'riece-twitter)
122
123 ;;; riece-twitter.el ends here