Re: dealing with html email programatically (was: getting body from message-entity programatically (was: get subject and from on wl-biff-notify-hook function))

andrés ramírez <[email protected]> Wed, 21 Aug 2019 14:58:30 +0000
Newsgroups gmane.mail.wanderlust.general.japanese
Organization bien.comun.org
Message-ID <[email protected]>
Hi Kazuhiro San.

Kazuhiro> Wanderlust uses SEMI and FLIM for parsing and displaying
Kazuhiro> messages.  Please read mime-display-message and caller
Kazuhiro> functions for details.  HTML is rendered by shr.el (included
Kazuhiro> by Emacs) or emacs-w3m package.  mime-shr.el (in SEMI-EPG)
Kazuhiro> and mime-w3m.el (in emacs-w3m) provide interface function.

The functions as they are today:
--8<---------------cut here---------------start------------->8---
(defun my/wl-biff-notify()
  "Improving default wl mail notifications for Maildirs
files in \"new\" directory are moved
into \"cur\" directory (need to deal with it later)"
  (let ((user-home-dir (getenv "HOME"))
        message-entity-item
        my-to my-from my-subject my-first-body-line my-notification-body my-notification-title my-message-raw-string my-message-body-string
        folder-item 
        elmo-folder 
        unread msgdb 
        my-blank-line-index (my-count 0))
    (if (fboundp 'notifications-notify)
        (when (> (length wl-biff-check-folder-list) 0)
          (dolist (folder-item wl-biff-check-folder-list)
            (setq elmo-folder (wl-folder-get-elmo-folder folder-item))
            (elmo-folder-open elmo-folder)
            (setq unread (cadr (elmo-list-diff
                                (elmo-folder-list-messages elmo-folder t t)
                                (elmo-folder-list-messages elmo-folder t nil)))
                  msgdb (elmo-folder-msgdb-create elmo-folder unread nil))
            (dolist (key-item (elmo-msgdb-list-messages msgdb))
              (setq my-count (+ my-count 1))
              (setq
               message-entity-item (elmo-msgdb-message-entity msgdb key-item) 
               my-to (elmo-msgdb-overview-entity-get-to message-entity-item)
               my-from (elmo-msgdb-overview-entity-get-from message-entity-item)
               my-notification-title my-to
               my-subject (elmo-msgdb-overview-entity-get-subject message-entity-item)
               my-message-raw-string (elmo-message-fetch-string
                                      elmo-folder key-item (elmo-find-fetch-strategy elmo-folder key-item) t) ; number = key-item {chk html mail}
               my-blank-line-index (string-match "^$" my-message-raw-string)
               my-message-body-string (if my-blank-line-index (substring my-message-raw-string (1+ my-blank-line-index)))
               my-first-body-line (my/get-body-according-to-content-type (elmo-message-entity-field message-entity-item 'content-type) my-message-body-string)
               my-first-body-line (if (< (length my-first-body-line) 120)
                                      my-first-body-line
                                    (concat (substring my-first-body-line -120) " ..."))
               my-notification-body (concat my-from "\t" my-subject "\n" my-first-body-line))
              (notifications-notify :title my-notification-title
                                    :bus (if (boundp 'my-dbus-address) my-dbus-address)
                                    :body my-notification-body ;from\tsubject\nfirst line of body
                                    :sound-file (concat user-home-dir "/downloads/KDE-Im-New-Mail.ogg")
                                    :app-icon (concat user-home-dir "/downloads/mail-unread.png")
                                    :timeout 1)
              )
            ))
      (if (file-exists-p (concat user-home-dir "/downloads/KDE-Im-New-Mail.au"))
          (play-sound-file (concat user-home-dir "/downloads/KDE-Im-New-Mail.au"))
        (progn (ding)(message (concat "file not present " (concat user-home-dir "/downloads/KDE-Im-New-Mail.au")))))
      )
    my-count))
 --8<---------------cut here---------------end--------------->8---

second function:
--8<---------------cut here---------------start------------->8---
(defun my/get-body-according-to-content-type (content-type-text-param my-message-body-string-param)
"dealing with html email
my/chomp does trim (remove blank space)"
(let ((my-content-type-list (split-string  content-type-text-param ";"))
      my-content-type-value my-content-type-second-arg)
  (setq my-content-type-value (my/chomp (car my-content-type-list)))
  (cond ((string-equal "text/plain" my-content-type-value)
         my-message-body-string-param
         )
        ((string-equal "text/html" my-content-type-value)
         (with-temp-buffer
           (insert my-message-body-string-param)
           (w3m-buffer)
           (buffer-substring-no-properties (point-min) (point-max))
           )
         )
        ((string-equal "multipart/alternative" my-content-type-value)
         (setq 
          my-content-type-second-arg (my/chomp (cadr my-content-type-list))
          my-content-type-second-arg (cadr (split-string  my-content-type-second-arg "\""))
          )
         (with-temp-buffer
           (insert my-message-body-string-param)
           (goto-char (point-min))
           (search-forward my-content-type-second-arg nil nil 2)
           (forward-char 1)
           (let ((beg (point))) (search-forward my-content-type-second-arg)(beginning-of-line) (forward-char -1)(copy-region-as-kill beg (point)))
           )
         (with-temp-buffer
           (insert (current-kill 0))
           (w3m-buffer)
           (buffer-substring-no-properties (point-min) (point-max))
           )         
         )
        ((string-equal "multipart/mixed" my-content-type-value)
         ; dealing with this later
         my-content-type-value
         )
        ))
  )
 --8<---------------cut here---------------end--------------->8---

BR