Skip to content

Instantly share code, notes, and snippets.

@SebDeclercq
Last active December 16, 2015 12:19
Show Gist options
  • Save SebDeclercq/5434002 to your computer and use it in GitHub Desktop.
Save SebDeclercq/5434002 to your computer and use it in GitHub Desktop.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Check new incomes in Gmail INBOX (using Elisp only) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun gmail-check ()
(interactive)
;; Download Gmail INBOX feed (requires identification in minibuffer)
(url-copy-file "https://mail.google.com/mail/feed/atom" "/tmp/gmail.atom")
;; Still can't use with-temp-buffer properly, so open file in new buffer
(find-file "/tmp/gmail.atom")
(goto-char 1)
;; Skipping Feed title
(search-forward "</title>")
;; Count how many new mails are available
(setq nbmails (how-many "<title>"))
;; If no new mail : kill-buffer/remove file and output
(if (= nbmails 0)
(message "%s" #("Your mailbox is empty..." 0 24 (face bold-italic)))
;; Define empty lists + nbread for iteration
(progn
(setq titles '()
names '()
emails '()
nbread 0))
;; Until all new mails are read
(while (> nbmails nbread)
(setq start (search-forward "<title>") ;; Search next title, get position of begin and end of title
end (- (search-forward "</") 2))
(if (= start end)
(push "::: No Title :::" titles) ;; If no subject, set title to ":::No Title:::"
(push (buffer-substring start end) titles)) ;; Push <title>.*</title> to list
(setq start (search-forward "<name>") ;; Same as above, for name
end (- (search-forward "</") 2))
(push (buffer-substring start end) names)
(setq start (search-forward "<email>") ;; Same as above, for email
end (- (search-forward "</") 2))
(push (buffer-substring start end) emails)
(setq nbread (1+ nbread))) ;; Iteration
(if (> nbmails 1)
;; If more than one mail
(progn
;; Create empty string for output message
(setq output "")
(setq head (format "%s %d %s\n"
#("You've got" 0 10 (face bold-italic))
nbmails
#("new mails !" 0 11 (face bold-italic))))
;; Add a “header” to output (You've XXX new mails)
(setq output (concat output head))
;; For iteration -- could have been titles or emails as well
(while names
(setq row (format "%s %s %s %s %s\t%s\n"
#("From" 0 4 (face bold))
(car names)
#("(<" 0 2 (face bold))
(car emails)
#(">) :" 0 5 (face bold))
(car titles)))
;; Add a new “row” to output (From XXX (<XXX>) : XXX)
(setq output (concat output row))
;; Remove car (first element) from list ; For iteration
(setq names (cdr names)
emails (cdr emails)
titles (cdr titles)))
;; Message output to minibuffer
(message "%s" output)) ;; End of (if (> nbmails 1)
;; If script gets here : there only one new mail, so message output to minibuffer
(message "%s\n%s %s %s %s %s\t%s"
#("You've got 1 new mail !" 0 23 (face bold-italic))
#("From" 0 4 (face bold))
(pop names)
#("(<" 0 2 (face bold))
(pop emails)
#(">) :" 0 5 (face bold))
(pop titles))))
;; Kill buffer/remove file
(kill-buffer "gmail.atom")
(delete-file "/tmp/gmail.atom"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment