* riece-twitter.el (riece-twitter-update): Don't send extra headers.
[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 ((process
71          (start-process
72           "curl" nil "curl"
73 ;;        "-H" "X-Twitter-Client: Riece"
74 ;;        "-H" (concat "X-Twitter-Client-Version: " riece-version-number)
75 ;;        "-H" "X-Twitter-Client-URL: http://riece.nongnu.org/twitter.xml"
76           "-u" riece-twitter-credential
77 ;;        "-d" "source=riece"
78           "-d" (concat "status="
79                        (riece-twitter-escape-string
80                         (encode-coding-string status 'utf-8)))
81           "-s"
82           "http://twitter.com/statuses/update.json")))
83     (set-process-sentinel process #'riece-twitter-sentinel)))
84
85 (defun riece-twitter-sentinel (process status)
86   (if (equal status "finished\n")
87       (message "Sending to Twitter...done")
88     (message "Sending to Twitter...failed: %s"
89              (substring status 0 (1- (length status))))))
90
91 (defun riece-twitter-message-filter (message)
92   (if (and (riece-message-own-p message)
93            (eq 'action (riece-message-type message)))
94       (if riece-twitter-credential
95           (riece-twitter-update (riece-message-text message))
96         (message "%s"
97                  (substitute-command-keys
98                   "\\[riece-twitter-set-credential] to set your credential"))))
99   message)
100
101 (defun riece-twitter-escape-string (string)
102   (let ((index 0))
103     (while (string-match "[^0-9A-Za-z\-\._~:/?@!\$'()*,]" string index)
104       (setq string (replace-match
105                     (format "%%%02X" (aref string (match-beginning 0)))
106                     t t string)
107             index (+ 3 (match-beginning 0))))
108     string))
109
110 (defun riece-twitter-insinuate ()
111   (add-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
112
113 (defun riece-twitter-uninstall ()
114   (remove-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
115
116 (provide 'riece-twitter)
117
118 ;;; riece-twitter.el ends here