* riece-twitter.el (riece-twitter-sentinel): New function.
[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          (setq password (read-passwd "Password: "))
54        (if password
55            (riece-twitter-clear-string password))
56        (setq password nil))
57      (list (concat username ":" password))))
58   (setq riece-twitter-credential credential))
59
60 (defun riece-twitter-update (status)
61   "Update your status."
62   (interactive "sStatus: ")
63   (message "Sending to Twitter...")
64   (let ((process
65          (start-process
66           "curl" nil "curl"
67           "-H" "X-Twitter-Client: Riece"
68           "-H" (concat "X-Twitter-Client-Version: " riece-version-number)
69           "-H" "X-Twitter-Client-URL: http://riece.nongnu.org/twitter.xml"
70           "-u" credential
71           "-d" "source=riece"
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     (set-process-sentinel process #'riece-twitter-sentinel)))
78
79 (defun riece-twitter-sentinel (process status)
80   (if (equal status "finished\n")
81       (message "Sending to Twitter...done")
82     (message "Sending to Twitter...failed: %s"
83              (substring status 0 (1- (length status))))))
84
85 (defun riece-twitter-message-filter (message)
86   (if (and (riece-message-own-p message)
87            (eq 'action (riece-message-type message)))
88       (if riece-twitter-credential
89           (riece-twitter-update (riece-message-text message))
90         (message "%s"
91                  (substitute-command-keys
92                   "\\[riece-twitter-set-credential] to set your credential"))))
93   message)
94
95 (defun riece-twitter-escape-string (string)
96   (let ((index 0))
97     (while (string-match "[^0-9A-Za-z\-\._~:/?@!\$'()*,]" string index)
98       (setq string (replace-match
99                     (format "%%%02X" (aref string (match-beginning 0)))
100                     t t string)
101             index (+ 3 (match-beginning 0))))
102     string))
103
104 (defun riece-twitter-insinuate ()
105   (add-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
106
107 (defun riece-twitter-uninstall ()
108   (remove-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
109
110 (provide 'riece-twitter)
111
112 ;;; riece-twitter.el ends here