Index: lang/elisp/twittering-mode/trunk/twittering-mode.el
===================================================================
--- lang/elisp/twittering-mode/trunk/twittering-mode.el (revision 10)
+++ lang/elisp/twittering-mode/trunk/twittering-mode.el (revision 11)
@@ -2,10 +2,12 @@
 
 ;; Copyright (C) 2007 Yuto Hayamizu.
+;;               2008 Tsuyoshi CHO
 
 ;; Author: Y. Hayamizu <y.hayamizu@gmail.com>
+;;         Tsuyoshi CHO <Tsuyoshi.CHO+develop@Gmail.com>
 ;; Created: Sep 4, 2007
 ;; Version: SVN-HEAD
 ;; Keywords: twitter web
-;; URL: http://hayamin.com/
+;; URL: http://lambdarepos.svnrepository.com/share/trac.cgi/browser/lang/elisp/twittering-mode
 
 ;; This file is free software; you can redistribute it and/or modify
@@ -29,4 +31,13 @@
 ;; You can check friends timeline, and update your status on Emacs.
 
+;;; Feature Request:
+
+;; URL : http://twitter.com/d00dle/statuses/577876082
+;; URL : http://twitter.com/d00dle/statuses/577879732
+;; * Status Input from Popup buffer and C-cC-c to POST.
+;; * Mark fav(star)
+;; URL : http://code.nanigac.com/source/view/419
+;; * update status for region
+
 ;;; Code:
 
@@ -34,4 +45,6 @@
 (require 'xml)
 (require 'parse-time)
+
+(defconst twittering-mode-version "0.3")
 
 (defvar twittering-mode-map (make-sparse-keymap))
@@ -93,7 +106,37 @@
 
 (defun assocref (item alist)
-    (cdr (assoc item alist)))
-
-;;;
+  (cdr (assoc item alist)))
+
+;;; Proxy
+(defvar twittering-proxy-use nil)
+(defvar twittering-proxy-server nil)
+(defvar twittering-proxy-port 8080)
+(defvar twittering-proxy-user nil)
+(defvar twittering-proxy-password nil)
+
+(defun twittering-toggle-proxy () ""
+  (interactive)
+  (setq twittering-proxy-use
+	(not twittering-proxy-use))
+  (message "%s %s"
+	   "Use Proxy:"
+	   (if twittering-proxy-use
+	       "on" "off")))
+
+(defun twittering-user-agent-default-function ()
+  "Twittering mode default User-Agent function."
+  (concat "Emacs/"
+	  (int-to-string emacs-major-version) "." (int-to-string
+						   emacs-minor-version)
+	  " "
+	  "Twittering-mode/"
+	  twittering-mode-version))
+
+(defvar twittering-user-agent-function 'twittering-user-agent-default-function)
+
+(defun twittering-user-agent ()
+  "Return User-Agent header string."
+  (funcall twittering-user-agent-function))
+
 ;;; to show image files
 
@@ -102,5 +145,7 @@
   (twittering-get-or-generate-buffer twittering-wget-buffer))
 
-(defvar twittering-tmp-dir "/tmp/twmode-images")
+(defvar twittering-tmp-dir
+  (expand-file-name (concat "twmode-images-" (user-login-name))
+		    temporary-file-directory))
 
 (defvar twittering-icon-mode nil "You MUST NOT CHANGE this variable directory. You should change through function'twittering-icon-mode'")
@@ -115,5 +160,5 @@
 		    (and arg (> (prefix-numeric-value arg) 0)))
 	    (when (file-writable-p twittering-tmp-dir)
-	      (progn 
+	      (progn
 		(if (not (file-directory-p twittering-tmp-dir))
 		    (make-directory twittering-tmp-dir))
@@ -124,14 +169,14 @@
   (interactive)
   (setq twittering-scroll-mode
-      (if (null arg)
-	  (not twittering-scroll-mode)
-        (> (prefix-numeric-value arg) 0))))
+	(if (null arg)
+	    (not twittering-scroll-mode)
+	  (> (prefix-numeric-value arg) 0))))
 
 (defun twittering-jojo-mode (&optional arg)
   (interactive)
   (setq twittering-jojo-mode
-      (if (null arg)
-	  (not twittering-jojo-mode)
-        (> (prefix-numeric-value arg) 0))))
+	(if (null arg)
+	    (not twittering-jojo-mode)
+	  (> (prefix-numeric-value arg) 0))))
 
 (defvar twittering-image-stack nil)
@@ -160,5 +205,5 @@
 	     (newline)
 	     ,obsym)
-         ,obsym))))
+	 ,obsym))))
 
 (defun twittering-debug-mode ()
@@ -187,4 +232,5 @@
       (define-key km "G" 'end-of-buffer)
       (define-key km "H" 'beginning-of-buffer)
+      (define-key km "\C-c\C-p" 'twittering-toggle-proxy)
       nil))
 
@@ -194,5 +240,5 @@
     ()
   (setq twittering-mode-syntax-table (make-syntax-table))
-;  (modify-syntax-entry ?  "" twittering-mode-syntax-table)
+  ;  (modify-syntax-entry ?  "" twittering-mode-syntax-table)
   (modify-syntax-entry ?\" "w"  twittering-mode-syntax-table)
   )
@@ -233,4 +279,6 @@
     `(decode-char 'ucs ,num)))
 
+(defvar twittering-mode-string "Twittering mode")
+
 (defun twittering-mode ()
   "Major mode for Twitter"
@@ -241,5 +289,5 @@
   (use-local-map twittering-mode-map)
   (setq major-mode 'twittering-mode)
-  (setq mode-name "Twittering mode")
+  (setq mode-name twittering-mode-string)
   (set-syntax-table twittering-mode-syntax-table)
   (run-hooks 'twittering-mode-hook)
@@ -260,29 +308,52 @@
     (erase-buffer))
 
-  (let (proc)
+  (let (proc server port
+	     (proxy-user twittering-proxy-user)
+	     (proxy-password twittering-proxy-password))
     (condition-case nil
 	(progn
+	  (if (and twittering-proxy-use twittering-proxy-server)
+	      (setq server twittering-proxy-server
+		    port (if (integerp twittering-proxy-port)
+			     (int-to-string twittering-proxy-port)
+			   twittering-proxy-port))
+	    (setq server "twitter.com"
+		  port "80"))
 	  (setq proc
 		(open-network-stream
 		 "network-connection-process" (twittering-http-buffer)
-		 "twitter.com" 80))
+		 server (string-to-number port)))
 	  (set-process-sentinel proc sentinel)
 	  (process-send-string
 	   proc
-	   (let ((nl "\r\n"))
-	     (concat "GET /" method-class "/" method ".xml HTTP/1.1" nl
-		     "Host: twitter.com" nl
-		     "Authorization: Basic "
-		     (base64-encode-string
-		      (concat twittering-username ":" twittering-password))
-		     nl
-		     "Accept: text/xml"
-		     ",application/xml"
-		     ",application/xhtml+xml"
-		     ",application/html;q=0.9"
-		     ",text/plain;q=0.8"
-		     ",image/png,*/*;q=0.5" nl
-		     "Accept-Charset: utf-8;q=0.7,*;q=0.7"
-		     nl nl))))
+	   (let ((nl "\r\n")
+		 request)
+	     (setq request
+		   (concat "GET http://twitter.com/" method-class "/" method ".xml HTTP/1.1" nl
+			   "Host: twitter.com" nl
+			   "User-Agent: " (twittering-user-agent) nl
+			   "Authorization: Basic "
+			   (base64-encode-string
+			    (concat twittering-username ":" (twittering-get-password)))
+			   nl
+			   "Accept: text/xml"
+			   ",application/xml"
+			   ",application/xhtml+xml"
+			   ",application/html;q=0.9"
+			   ",text/plain;q=0.8"
+			   ",image/png,*/*;q=0.5" nl
+			   "Accept-Charset: utf-8;q=0.7,*;q=0.7" nl
+			   (when twittering-proxy-use
+			     "Proxy-Connection: Keep-Alive" nl
+			     (when (and proxy-user proxy-password)
+			       (concat
+				"Proxy-Authorization: Basic "
+				(base64-encode-string
+				 (concat proxy-user ":"
+					 proxy-password))
+				nl)))
+			   nl nl))
+	     (debug-print (concat "GET Request\n" request))
+	     request)))
       (error
        (message "Failure: HTTP GET") nil))))
@@ -290,21 +361,21 @@
 (defun twittering-http-get-default-sentinel (proc stat &optional suc-msg)
   (let ((header (twittering-get-response-header))
-	  (body (twittering-get-response-body))
-	  (status nil)
-	  )
-      (if (string-match "HTTP/1\.[01] \\([a-z0-9 ]+\\)\r?\n" header)
-	  (progn
-	    (setq status (match-string-no-properties 1 header))
-	    (case-string
-	     status
-	     (("200 OK")
-	      (mapcar
-	       #'twittering-cache-status-datum
-	       (reverse (twittering-xmltree-to-status
-			 body)))
-	      (twittering-render-friends-timeline)
-	      (message (if suc-msg suc-msg "Success: Get.")))
-	     (t (message status))))
-	(message "Failure: Bad http response.")))
+	(body (twittering-get-response-body))
+	(status nil)
+	)
+    (if (string-match "HTTP/1\.[01] \\([a-z0-9 ]+\\)\r?\n" header)
+	(progn
+	  (setq status (match-string-no-properties 1 header))
+	  (case-string
+	   status
+	   (("200 OK")
+	    (mapcar
+	     #'twittering-cache-status-datum
+	     (reverse (twittering-xmltree-to-status
+		       body)))
+	    (twittering-render-friends-timeline)
+	    (message (if suc-msg suc-msg "Success: Get.")))
+	   (t (message status))))
+      (message "Failure: Bad http response.")))
   )
 
@@ -449,35 +520,58 @@
     (erase-buffer))
 
-  (let (proc)
+  (let (proc server port
+	     (proxy-user twittering-proxy-user)
+	     (proxy-password twittering-proxy-password))
     (progn
+      (if (and twittering-proxy-use twittering-proxy-server)
+	  (setq server twittering-proxy-server
+		port (if (integerp twittering-proxy-port)
+			 (int-to-string twittering-proxy-port)
+		       twittering-proxy-port))
+	(setq server "twitter.com"
+	      port "80"))
       (setq proc
 	    (open-network-stream
 	     "network-connection-process" (twittering-http-buffer)
-	     "twitter.com" 80))
+	     server (string-to-number port)))
       (set-process-sentinel proc sentinel)
       (process-send-string
        proc
-       (let ((nl "\r\n"))
-	 (concat "POST /" method-class "/" method ".xml?"
-		 (if parameters
-		     (mapconcat
-		      (lambda (param-pair)
-			(format "%s=%s"
-				(twittering-percent-encode (car param-pair))
-				(twittering-percent-encode (cdr param-pair))))
-		      parameters
-		      "&"))
-		 " HTTP/1.1" nl
-		 "Host: twitter.com" nl
-		 "Authorization: Basic "
-		 (base64-encode-string
-		  (concat twittering-username ":" twittering-password))
-		 nl
-		 "Content-Type: text/plain" nl
-		 "Content-Length: 0" nl
-		 nl nl))))))
+       (let ((nl "\r\n")
+	     request)
+	 (setq	request
+		(concat "POST http://twitter.com/" method-class "/" method ".xml?"
+			(if parameters
+			    (mapconcat
+			     (lambda (param-pair)
+			       (format "%s=%s"
+				       (twittering-percent-encode (car param-pair))
+				       (twittering-percent-encode (cdr param-pair))))
+			     parameters
+			     "&"))
+			" HTTP/1.1" nl
+			"Host: twitter.com" nl
+			"User-Agent: " (twittering-user-agent) nl
+			"Authorization: Basic "
+			(base64-encode-string
+			 (concat twittering-username ":" (twittering-get-password)))
+			nl
+			"Content-Type: text/plain" nl
+			"Content-Length: 0" nl
+			(when twittering-proxy-use
+			  "Proxy-Connection: Keep-Alive" nl
+			  (when (and proxy-user proxy-password)
+			    (concat
+			     "Proxy-Authorization: Basic "
+			     (base64-encode-string
+			      (concat proxy-user ":"
+				      proxy-password))
+			     nl)))
+			nl nl))
+	 (debug-print (concat "POST Request\n" request))
+	 request)))))
 
 (defun twittering-http-post-default-sentinel (proc stat &optional suc-msg)
-  
+
   (condition-case err-signal
       (let ((header (twittering-get-response-header))
@@ -525,5 +619,5 @@
   (if (null data-var)
       (setf data-var 'twittering-friends-timeline-data))
-  (let ((id (cdr (assq 'id status-datum)))) 
+  (let ((id (cdr (assq 'id status-datum))))
     (if (or (null (symbol-value data-var))
 	    (not (find-if
@@ -553,9 +647,9 @@
 	   user-protected
 	   regex-index)
-    
+
       (setq id (string-to-number (assq-get 'id status-data)))
-      (setq text (twittering-decode-html-entities 
+      (setq text (twittering-decode-html-entities
 		  (assq-get 'text status-data)))
-      (setq source (twittering-decode-html-entities 
+      (setq source (twittering-decode-html-entities
 		    (assq-get 'source status-data)))
       (setq created-at (assq-get 'created_at status-data))
@@ -564,5 +658,5 @@
       (setq user-name (twittering-decode-html-entities
 		       (assq-get 'name user-data)))
-      (setq user-screen-name (twittering-decode-html-entities 
+      (setq user-screen-name (twittering-decode-html-entities
 			      (assq-get 'screen_name user-data)))
       (setq user-location (twittering-decode-html-entities
@@ -605,6 +699,6 @@
 		   uri ,(concat "http://twitter.com/" screen-name))
 	       `(mouse-face highlight
-		 face twittering-uri-face
-		 uri ,uri))
+			    face twittering-uri-face
+			    uri ,uri))
 	     text))
 	  (setq regex-index (match-end 0)) ))
@@ -627,7 +721,7 @@
 	     0 (length source)
 	     `(mouse-face highlight
-	       uri ,uri
-	       face twittering-uri-face
-	       source ,source)
+			  uri ,uri
+			  face twittering-uri-face
+			  source ,source)
 	     source)
 	    ))
@@ -828,4 +922,8 @@
 	(twittering-update-status-from-minibuffer (concat "@" username " ")))))
 
+(defun twittering-get-password ()
+  (or twittering-password
+      (setq twittering-password (read-passwd "twittering-mode: "))))
+
 (provide 'twittering-mode)
 ;;; twittering.el ends here
