1 ;;; riece-twitter.el --- post your status to Twitter
2 ;; Copyright (C) 2007 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: IRC, riece
7 ;; This file is part of Riece.
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)
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.
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.
26 ;; NOTE: This is an add-on module for Riece.
30 (require 'riece-message)
32 (defgroup riece-twitter nil
33 "Post your status to Twitter"
36 (defcustom riece-twitter-credential nil
37 "Your credential used to login to Twitter."
42 (if (fboundp 'clear-string)
43 (defalias 'riece-twitter-clear-string 'clear-string)
44 (defun riece-twitter-clear-string (string)
45 (fillarray string ?\0))))
47 (defun riece-twitter-set-credential (credential)
48 "Set your credential used to login to Twitter."
50 (let ((username (read-string "Username: "))
53 (setq password (read-passwd "Password: "))
55 (riece-twitter-clear-string password))
57 (list (concat username ":" password))))
58 (setq riece-twitter-credential credential))
60 (defun riece-twitter-update (status)
64 (unless riece-twitter-credential
66 (substitute-command-keys
67 "\\[riece-twitter-set-credential] to set your credential")))
68 (list (read-string "Status: "))))
69 (message "Sending to Twitter...")
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
78 "-d" (concat "status="
79 (riece-twitter-escape-string
80 (encode-coding-string status 'utf-8)))
82 "http://twitter.com/statuses/update.json")))
83 (set-process-sentinel process #'riece-twitter-sentinel)))
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))))))
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))
97 (substitute-command-keys
98 "\\[riece-twitter-set-credential] to set your credential"))))
101 (defun riece-twitter-escape-string (string)
103 (while (string-match "[^0-9A-Za-z\-\._~:/?@!\$'()*,]" string index)
104 (setq string (replace-match
105 (format "%%%02X" (aref string (match-beginning 0)))
107 index (+ 3 (match-beginning 0))))
110 (defun riece-twitter-insinuate ()
111 (add-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
113 (defun riece-twitter-uninstall ()
114 (remove-hook 'riece-message-filter-functions 'riece-twitter-message-filter))
116 (provide 'riece-twitter)
118 ;;; riece-twitter.el ends here