おもしろwebサービス開発日記

Ruby や Rails を中心に、web技術について書いています

hiki-mode.elをbasic認証に対応させた

下記の西山さんのファイルを参考にしました。
Hiki-dev:01036 hiki-mode.el: Basic認証対応



パッチ作ったので置いときます。自由に使ってください。

--- hiki-mode.el	2008-05-23 00:21:24.000000000 +0900
+++ hiki-mode.basic.el	2008-05-23 00:29:34.000000000 +0900
@@ -255,13 +255,15 @@
 	     (setq prompt (or retry-prompt prompt)))))
     c))
 
-(defun hiki-http-request (mode cmd pagename site-url &optional post-data)
+(defun hiki-http-request (mode cmd pagename site-url &optional site-info post-data)
   (let* ((url (if (eq mode 'get)
                   (concat (format "%s?c=%s" site-url cmd)
                           (if pagename 
                               (format ";p=%s" (hiki-http-url-hexify-string pagename hiki-coding-system))))
                 site-url))
-         (buf (hiki-http-fetch url mode nil nil 
+         (buf (hiki-http-fetch url mode
+			       (hiki-site-auth-basic-user site-info)
+			       (hiki-site-auth-basic-pass site-info)
                                (hiki-http-url-hexify-alist post-data hiki-coding-system))))
     (if (bufferp buf)
         (save-excursion
@@ -626,7 +628,7 @@
     (add-to-list 'post-data (cons "c" "login"))
     (add-to-list 'post-data (cons "name" username))
     (add-to-list 'post-data (cons "password" password))
-    (setq buf (hiki-http-request 'post nil nil (hiki-site-url) post-data))
+    (setq buf (hiki-http-request 'post nil nil (hiki-site-url) hiki-site-info post-data))
     (set-buffer buf)
     (goto-char (point-min))
     (if (re-search-forward "HTTP/1.[01] \\([0-9][0-9][0-9]\\) \\(.*\\)" nil t)
@@ -641,7 +643,7 @@
   (interactive)
   (let (post-data buf)
     (add-to-list 'post-data (cons "c" "logout"))
-    (setq buf (hiki-http-request 'post nil nil (hiki-site-url) post-data))
+    (setq buf (hiki-http-request 'post nil nil (hiki-site-url) hiki-site-info post-data))
     (message "Logged out.")))
 
 ;;; func
@@ -716,7 +718,7 @@
 (defun hiki-fetch-index (site-info)
   "?ڡ???????????????롣"
   (let (indexes history (i 1)
-                (buf (hiki-http-request 'get "index" nil (hiki-site-url site-info))))
+                (buf (hiki-http-request 'get "index" nil (hiki-site-url site-info) site-info )))
     (when (bufferp buf)
       (save-excursion
         (set-buffer buf)
@@ -751,7 +753,7 @@
   (update-timestamp . t/nil)
   (password . t/nil)) ???֤???"
   (let (buf start end pt result)
-    (setq buf (hiki-http-request 'get "edit" pagename site-url))
+    (setq buf (hiki-http-request 'get "edit" pagename site-url site-info))
     (when (bufferp buf)
       (save-excursion
 	(set-buffer buf)
@@ -820,7 +822,7 @@
     (add-to-list 'post-data (cons "save" "save"))
     (setq buf 
 	  (hiki-http-request 'post nil hiki-pagename
-			     (hiki-site-url) post-data))
+			     (hiki-site-url) hiki-site-info post-data))
     (when (bufferp buf)
       (save-excursion
 	(set-buffer buf)
@@ -898,6 +900,19 @@
   (or (nth 2 (or site-info hiki-site-info))
       'default))
 
+(defun hiki-site-auth (&optional site-info)
+  (nth 3 (or site-info hiki-site-info)))
+
+(defun hiki-site-auth-basic-user (&optional site-info)
+  (let ((auth (hiki-site-auth site-info)))
+    (and auth (eq (car auth) 'basic)
+	 (nth 1 auth))))
+
+(defun hiki-site-auth-basic-pass (&optional site-info)
+  (let ((auth (hiki-site-auth site-info)))
+    (and auth (eq (car auth) 'basic)
+	 (nth 2 auth))))
+
 (defun hiki-page-buffer-name (pagename site-info)
   (let ((buf (cdr (assoc (list (hiki-site-name site-info) pagename) 
 			 hiki-page-buffer-alist))))

パッチ当てたら.emacshiki-site-listにこんな感じで書けばOK。

(setq hiki-site-list
      '(("example" "http://www.example/hiki.cgi"
	 nil (basic "username" "password"))
        ))