[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

Re: automatically filling out WordPress comment fields



Katsumi Yamaoka <yamaoka@xxxxxxx> writes:

>>>>>> In [emacs-w3m : No.10902] 青田さん wrote:
> [...]
>> ちょっと時間がとれたので書いてみました。まだ、コードがちょっと怪しいので
>> すが…とりあえずバグ出しと査読をできるようにパッチを出してみます。
>
> どうもありがとうございます。ちょっと試してみて最初に気が付いたの
> ですが、
>
> [...]
>
>> 収集結果は、たとえば http://example.com/foo/bar/baz というページでhoge と
>> いうフォームに fuga を入力した後にはこのようなっています。
>
>> (("example.com"
>>   (("foo"
>>     (("bar"
>>       (("baz" nil
>> 	(("hoge" . "fuga"))))
>>       nil))
>>     nil))
>>   nil))
>
> `w3m-form-auto-fill-data' の値
>
> (("host1" tree...) ("host2" tree...) ...))
>
> の tree の部分が、ホストに関わらず、すべて最後にアクセスしたホス
> トの値になってしまっているように見えます。

なるほど…たしかにそうなっていました。 どうも w3m-form-input の部分がおか
しいようです。ぼくのところでもリストがループしたりしていました。

`() を (list ...)  のように書き直してみたところ、今のところうまく動いてい
るようです。

;; でも、実質変わりないはず…ですよねぇ?

--
青田
Index: w3m-form.el
===================================================================
RCS file: /storage/cvsroot/emacs-w3m/w3m-form.el,v
retrieving revision 1.178
diff -u -r1.178 w3m-form.el
--- w3m-form.el	20 May 2009 23:13:29 -0000	1.178
+++ w3m-form.el	5 Jun 2009 04:27:20 -0000
@@ -173,6 +173,15 @@
   "Specify non-nil value to download contents after sending form.
 It is useful to bind this variable with `let', but do not set it globally.")
 
+(defvar w3m-form-values '(w3m-form-auto-fill)
+  "")
+(defvar w3m-form-auto-fill-data nil)
+(defcustom w3m-form-auto-fill-file
+  (expand-file-name ".form-data" w3m-profile-directory)
+  "*File name to keep form data."
+  :group 'w3m
+  :type '(file :size 0))
+
 ;;; w3m-form structure:
 
 (defun w3m-form-normalize-action (action url)
@@ -702,8 +711,35 @@
 			       no_effect       ; map
 			       name value)
 	  (incf id)
-	  (when value
-	    (setq value (w3m-decode-entities-string value)))
+	  (setq value 
+		(catch 'loop
+		  (dolist (x w3m-form-values)
+		    (if (listp x)
+			(cond
+			 ((and (stringp (car x))
+			       (string= (car x) name))
+			  (throw 'loop (cdr x)))
+			 ((listp (car x))
+			  (let* ((plist (car x))
+				 (name-reg  (plist-get plist :name))
+				 (url-reg   (plist-get plist :url))
+				 (value-reg (plist-get plist :value)))
+			    (when (and (or (null name-reg)
+					   (string-match name-reg name))
+				       (or (null url-reg)
+					   (string-match url-reg w3m-current-url))
+				       (or (null value-reg)
+					   (string-match value-reg value)))
+			      (throw 'loop (cdr x)))))
+			 ((functionp (car x))
+			  (when (funcall (car x) w3m-current-url name value)
+			    (throw 'loop (cdr x)))))
+		     (when (functionp x)
+		       (let ((item (funcall x w3m-current-url name value)))
+			 (when item
+			   (throw 'loop item))))))
+		  (when value
+		    (throw 'loop (w3m-decode-entities-string value)))))
 	  (save-excursion
 	    (search-forward "</input_alt>")
 	    (setq end (match-beginning 0)))
@@ -972,8 +1008,31 @@
 		  (insert input)
 		  (w3m-form-coding-system-accept-region-p nil nil coding))
 	    (w3m-form-put form id name input)
-	    (w3m-form-replace input)))))))
-
+	    (w3m-form-replace input))
+	  (w3m-string-match-url-components w3m-current-url)
+	  (let* ((host (match-string 4 w3m-current-url))
+		 (path (match-string 5 w3m-current-url))
+		 (tree (assoc host w3m-form-auto-fill-data)))
+	    (unless tree
+	      (setq tree (list host nil nil)) 
+	      (setq w3m-form-auto-fill-data
+		    (cons tree
+			  w3m-form-auto-fill-data)))
+	    (while (string-match "^/\\([^/]+\\)" path)
+	      (let* ((dir (match-string 1 path))
+		     (next-tree (assoc dir (cadr tree))))
+		(unless next-tree
+		  (setq next-tree (list dir () ()))
+		  (setf (cadr tree) (cons next-tree (cadr tree))))
+		(setq path (substring path (match-end 0))
+		      tree next-tree)))
+	    (let ((elem (assoc name (caddr tree))))
+	      (if elem
+		  (setcdr elem input)
+		(setf (caddr tree)
+		      (cons (cons name input)
+			    (caddr tree)))))))))))
+	  
 (defun w3m-form-input-password (form id name)
   (if (get-text-property (point) 'w3m-form-readonly)
       (message "This input box is read-only.")
@@ -1892,6 +1951,56 @@
 	(goto-char (or (w3m-form-real-reset form (w3m-action pos))
 		       (next-single-property-change pos 'w3m-action)))))))
 
+(defun w3m-form-search-for-auto-fill-value (tree name)
+  (let (result elem)
+    (dolist (x tree)
+      (dolist (item (let ((child (w3m-form-search-for-auto-fill-value (cadr x) name))
+			  (myval (cdr (assoc name (caddr x)))))
+		      (if (setq elem (assoc myval child))
+			  (progn (setcdr elem (1+ (cdr elem)))
+				 child)
+			(if myval
+			    (cons (cons myval 1)
+				  child)
+			  child))))
+	(if (setq elem (assoc (car item) result))
+	    (setcdr elem (1+ (cdr elem)))
+	  (setq result (cons item result)))))
+    result))
+
+(defun w3m-form-auto-fill (url name value)
+  (unless w3m-form-auto-fill-data
+    (setq w3m-form-auto-fill-data
+	  (or (w3m-load-list w3m-form-auto-fill-file)
+	      '(("" () ())))))
+  (w3m-string-match-url-components url)
+  (let* ((host (match-string 4 url))
+	 (path (match-string 5 url))
+	 (tree (assoc host w3m-form-auto-fill-data))
+	 (traveled (list tree))
+	 (parsed nil))
+    (when tree
+      (setq tree
+	    (catch 'loop
+	      (while (string-match "^/\\([^/]*\\)" path)
+		(let* ((dir (match-string 1 path))
+		       (next-tree (assoc dir (cadr tree))))
+		  (unless next-tree (throw 'loop tree))
+		  (setq tree next-tree
+			traveled (cons next-tree traveled)
+			path (substring path (match-end 0)))))
+	      (setq parsed t)
+	      (throw 'loop tree)))
+      (or (and parsed (cdr (assoc name (caddr tree))))
+	  (catch 'loop
+	    (dolist (tree traveled)
+	      (let ((val-list (w3m-form-search-for-auto-fill-value (cadr tree) name))
+		    (max 0) val)
+		(dolist (l val-list)
+		  (when (< max (cdr l))
+		    (setq max (cdr l)
+			  val (car l))))
+		(when val (throw 'loop val)))))))))
 
 (provide 'w3m-form)