1. Support Laconica deployed to a context that is not the root.

Fri, 20 Feb 2009 09:17:20 -0500

author
Christian Cheng <chrycheng at gmail.com>
date
Fri, 20 Feb 2009 09:17:20 -0500
changeset 1
f524670023b3
parent 0
d1ea9fa54856
child 2
f7dfbe1e3a0b

1. Support Laconica deployed to a context that is not the root.
2. Support retrieval of the public timeline.
3. Fixed to handle wierd characters in response body.
4. Enhanced to show no. of characters exceeding message limit.

denting-mode.el file | annotate | diff | comparison | revisions
--- a/denting-mode.el	Fri Feb 20 09:10:17 2009 -0500
+++ b/denting-mode.el	Fri Feb 20 09:17:20 2009 -0500
@@ -55,6 +55,19 @@
   :type 'string
   :group 'denting
   )
+(defcustom laconica-path nil
+  "Path to the Laconica instance you want to use if it is not the
+root context of the server."
+  :type 'string
+  :group 'denting
+  )
+(defun laconica-url ()
+  "Returns the URL to the Laconica instance you want to use.
+Merely `laconica-instance' if `laconica-path' is nil."
+  (if laconica-path
+      (concat laconica-instance "/" laconica-path)
+    laconica-instance))
+
 (defvar denting-mode-map (make-sparse-keymap))
 
 (defvar denting-timer nil "Timer object for timeline refreshing will be stored here. DO NOT SET VALUE MANUALLY."
@@ -116,8 +129,20 @@
 (defun denting-http-buffer ()
   (denting-get-or-generate-buffer denting-http-buffer))
 
-(defvar denting-friends-timeline-data nil)
-(defvar denting-friends-timeline-last-update nil)
+(defvar denting-timeline-data nil)
+(defvar denting-timeline-last-update nil)
+(defcustom denting-timeline-public nil
+  "Controls whether to retrieve the public timeline or just your friends-only timeline."
+  :type 'boolean
+  :group 'denting
+  )
+(defun denting-timeline-method ()
+  "Returns the name of the method to be used to retrieve the
+timeline.  If `denting-timeline-public' is nil, friends_timeline;
+public_timeline otherwise."
+  (if denting-timeline-public
+      "public_timeline"
+    "friends_timeline"))
 
 (defvar denting-username-face 'denting-username-face)
 (defvar denting-uri-face 'denting-uri-face)
@@ -193,7 +218,7 @@
 		(if (not (file-directory-p denting-tmp-dir))
 		    (make-directory denting-tmp-dir))
 		t)))))
-  (denting-render-friends-timeline))
+  (denting-render-timeline))
 
 (defun denting-scroll-mode (&optional arg)
   (interactive)
@@ -250,7 +275,7 @@
 
 (if denting-mode-map
     (let ((km denting-mode-map))
-      (define-key km "\C-c\C-f" 'denting-friends-timeline)
+      (define-key km "\C-c\C-f" 'denting-timeline)
       (define-key km "\C-c\C-s" 'denting-update-status-interactive)
       (define-key km "\C-c\C-e" 'denting-erase-old-statuses)
       (define-key km "\C-m" 'denting-enter)
@@ -375,7 +400,7 @@
 	   (let ((nl "\r\n")
 		 request)
 	     (setq request
-		   (concat "GET http://" laconica-instance "/api/" method-class "/" method
+                   (concat "GET http://" (laconica-url) "/api/" method-class "/" method
 			   ".xml"
 			   (when parameters
 			     (concat "?"
@@ -430,13 +455,13 @@
 	     #'denting-cache-status-datum
 	     (reverse (denting-xmltree-to-status
 		       body)))
-	    (denting-render-friends-timeline)
+            (denting-render-timeline)
 	    (message (if suc-msg suc-msg "Success: Get.")))
 	   (t (message status))))
       (message "Failure: Bad http response.")))
   )
 
-(defun denting-render-friends-timeline ()
+(defun denting-render-timeline ()
   (with-current-buffer (denting-buffer)
     (let ((point (point))
 	  (end (point-max)))
@@ -448,7 +473,7 @@
 	      (fill-region-as-paragraph
 	       (save-excursion (beginning-of-line) (point)) (point))
 	      (insert "\n"))
-	    denting-friends-timeline-data)
+            denting-timeline-data)
       (if denting-image-stack
 	  (clear-image-cache))
       (setq buffer-read-only t)
@@ -613,7 +638,7 @@
        (let ((nl "\r\n")
 	     request)
 	 (setq  request
-		(concat "POST http://" laconica-instance "/api/" method-class "/" method ".xml"
+                (concat "POST http://" (laconica-url) "/api/" method-class "/" method ".xml"
 			(when parameters
 			  (concat "?"
 				  (mapconcat
@@ -680,18 +705,26 @@
   (if (null buffer) (setq buffer (denting-http-buffer)))
   (save-excursion
     (set-buffer buffer)
+    (denting-clean-response-body)
     (let ((content (buffer-string)))
-      (let ((content (buffer-string)))
-	(xml-parse-region (+ (string-match "\r?\n\r?\n" content)
+      (xml-parse-region (+ (string-match "<\\?xml" content)
 			     (length (match-string 0 content)))
-			  (point-max)))
+                        (point-max))
       )))
 
+(defun denting-clean-response-body ()
+  "Removes weird strings (e.g., 1afc, a or 0) from within the
+response body.  Known Laconica issue.  Mostly harmless except if
+in tags."
+  (goto-char (point-min))
+  (while (re-search-forward "\r?\n[0-9a-z]+\r?\n" nil t)
+    (replace-match "")))
+
 (defun denting-cache-status-datum (status-datum &optional data-var)
-  "Cache status datum into data-var(default denting-friends-timeline-data)
+  "Cache status datum into data-var(default denting-timeline-data)
 If STATUS-DATUM is already in DATA-VAR, return nil. If not, return t."
   (if (null data-var)
-      (setf data-var 'denting-friends-timeline-data))
+      (setf data-var 'denting-timeline-data))
   (let ((id (cdr (assq 'id status-datum))))
     (if (or (null (symbol-value data-var))
 	    (not (find-if
@@ -745,7 +778,7 @@
       (add-text-properties
        0 (length user-name)
        `(mouse-face highlight
-		    uri ,(concat "http://" laconica-instance "/" user-screen-name)
+                    uri ,(concat "http://" (laconica-url) "/" user-screen-name)
 		    face denting-username-face)
        user-name)
 
@@ -754,7 +787,7 @@
        0 (length user-screen-name)
        `(mouse-face highlight
 		    face denting-username-face
-		    uri ,(concat "http://" laconica-instance "/" user-screen-name)
+                    uri ,(concat "http://" (laconica-url) "/" user-screen-name)
 		    face denting-username-face)
        user-screen-name)
 
@@ -778,7 +811,7 @@
 		 `(mouse-face
 		   highlight
 		   face denting-uri-face
-		   uri ,(concat "http://" laconica-instance "/" screen-name))
+                   uri ,(concat "http://" (laconica-url) "/" screen-name))
 	       `(mouse-face highlight
 			    face denting-uri-face
 			    uri ,uri))
@@ -801,7 +834,7 @@
 	    ))
 
       ;; save last update time
-      (setq denting-friends-timeline-last-update created-at)
+      (setq denting-timeline-last-update created-at)
 
       (mapcar
        (lambda (sym)
@@ -892,7 +925,12 @@
   (if (null init-str) (setq init-str ""))
   (let ((status init-str) (not-posted-p t))
     (while not-posted-p
-      (setq status (read-from-minibuffer "status: " status nil nil nil nil t))
+      (setq status (read-from-minibuffer "Status (140): " status nil nil nil
+                                         nil t))
+      (while (< 140 (length status))
+        (setq status (read-from-minibuffer (format "Status (%d): "
+                                                   (- 140 (length status)))
+                                           status nil nil nil nil t)))
       (setq not-posted-p
 	    (not (denting-update-status-if-not-blank status))))))
 (defun denting-update-status-from-region (beg end)
@@ -928,7 +966,7 @@
 (defun denting-start (&optional action)
   (interactive)
   (if (null action)
-      (setq action #'denting-friends-timeline))
+      (setq action #'denting-timeline))
   (if denting-timer
       nil
     (setq denting-timer
@@ -941,19 +979,19 @@
   (cancel-timer denting-timer)
   (setq denting-timer nil))
 
-(defun denting-friends-timeline ()
+(defun denting-timeline ()
   (interactive)
   (let ((buf (get-buffer denting-buffer)))
     (if (not buf)
 	(denting-stop)
-      (if (not denting-friends-timeline-last-update)
-          (denting-http-get "statuses" "friends_timeline")
+      (if (not denting-timeline-last-update)
+          (denting-http-get "statuses" (denting-timeline-method))
         (let* ((system-time-locale "C")
                (since
                 (denting-global-strftime
                  "%a, %d %b %Y %H:%M:%S GMT"
-                 denting-friends-timeline-last-update)))
-          (denting-http-get "statuses" "friends_timeline"
+                 denting-timeline-last-update)))
+          (denting-http-get "statuses" (denting-timeline-method)
                             `(("since" . ,since)))))))
 
   (if denting-icon-mode
@@ -982,15 +1020,15 @@
 
 (defun denting-erase-old-statuses ()
   (interactive)
-  (setq denting-friends-timeline-data nil)
-  (if (not denting-friends-timeline-last-update)
-      (denting-http-get "statuses" "friends_timeline")
+  (setq denting-timeline-data nil)
+  (if (not denting-timeline-last-update)
+      (denting-http-get "statuses" (denting-timeline-method))
     (let* ((system-time-locale "C")
 	   (since
             (denting-global-strftime
              "%a, %d %b %Y %H:%M:%S GMT"
-             denting-friends-timeline-last-update)))
-      (denting-http-get "statuses" "friends_timeline"
+             denting-timeline-last-update)))
+      (denting-http-get "statuses" (denting-timeline-method)
                         `(("since" . ,since))))))
 
 (defun denting-click ()
@@ -1109,7 +1147,7 @@
 
 (defun denting-get-status-url (username id)
   "Generate status URL."
-  (format (concat "http://" laconica-instance "/%s/statuses/%d") username id))
+  (format (concat "http://" (laconica-url) "/%s/statuses/%d") username id))
 
 ;;;###autoload
 (defun dent ()

mercurial