Advertisement
Guest User

emacs jabber blacklist by komintern (linux.org.ru)

a guest
Nov 2nd, 2011
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.14 KB | None | 0 0
  1. komintern@komintern:~$ cat ~/elbl.el
  2. (defvar *jabber-blacklist* nil)
  3.  
  4. (defun jabber-add-to-blacklist (&optional jid)
  5.   (interactive)
  6.   (when (not jid)
  7.     (setf jid (read-string "JID: " nil nil jid)))
  8.   (when (not (jabber-check-blacklist jid))
  9.   (setf *jabber-blacklist* (concatenate 'string jid " " *jabber-blacklist*))
  10.   (message "%s added to jabber blacklist" jid)
  11.   (jabber-save-blacklist)))
  12.  
  13. (defun get-string-from-file (filePath)
  14.   (with-temp-buffer
  15.     (insert-file-contents filePath)
  16.     (buffer-string)))
  17.  
  18. (defun write-string-to-file (string file)
  19.   (with-temp-buffer
  20.     (insert string)
  21.     (when (file-writable-p file)
  22.       (write-region (point-min)
  23.             (point-max)
  24.             file))))
  25.  
  26. (defun jabber-load-blacklist ()
  27.   (interactive)
  28.   (setf *jabber-blacklist*
  29.     (get-string-from-file "~/.emacs-jabber-blacklist")))
  30.  
  31. (defun jabber-save-blacklist ()
  32.   (interactive)
  33.   (write-string-to-file *jabber-blacklist* "~/.emacs-jabber-blacklist"))
  34.  
  35. (defun jabber-check-blacklist (&optional from)
  36.   (interactive)
  37.   (when (not from)
  38.     (setf from (read-string "JID: " nil nil from)))
  39.   (when *jabber-blacklist*
  40.   (if (member* from (split-string *jabber-blacklist*) :test 'string=)
  41.     t nil)))
  42.  
  43. (defun jabber-process-chat (jc xml-data)
  44.   "If XML-DATA is a one-to-one chat message, handle it as such."
  45.   (when (not (jabber-muc-message-p xml-data))
  46.     (let ((from (jabber-xml-get-attribute xml-data 'from))
  47.       (error-p (jabber-xml-get-children xml-data 'error))
  48.       (body-text (car (jabber-xml-node-children
  49.                (car (jabber-xml-get-children
  50.                  xml-data 'body))))))
  51.       (if (not (jabber-check-blacklist from))
  52.       (progn
  53.         (when (or error-p
  54.               (run-hook-with-args-until-success 'jabber-chat-printers xml-data :foreign :printp))
  55.           (with-current-buffer (if (jabber-muc-sender-p from)
  56.                        (jabber-muc-private-create-buffer
  57.                     jc
  58.                     (jabber-jid-user from)
  59.                     (jabber-jid-resource from))
  60.                      (jabber-chat-create-buffer jc from))
  61.         (let ((node
  62.                (ewoc-enter-last jabber-chat-ewoc (list (if error-p :error :foreign) xml-data :time (current-time)))))
  63.           (jabber-maybe-print-rare-time node))
  64.        
  65.         (dolist (hook '(jabber-message-hooks jabber-alert-message-hooks))
  66.           (run-hook-with-args hook
  67.                       from (current-buffer) body-text
  68.                       (funcall jabber-alert-message-function
  69.                            from (current-buffer) body-text))))))
  70.     (message "Message from %s blacklisted" from)))))
  71.    
  72. (if (file-exists-p "~/.emacs-jabber-blacklist")
  73.     (jabber-load-blacklist))
  74.  
  75. (defun jabber-add-to-blacklist-jid-at-point ()
  76.   (interactive)
  77.   (jabber-add-to-blacklist (get-text-property (point) 'jabber-jid)))
  78.  
  79. (defun jabber-remove-from-blacklist (&optional jid)
  80.   (interactive)
  81.   (when (not jid)
  82.     (setf jid (read-string "JID: " nil nil jid)))
  83.   (when (jabber-check-blacklist jid)
  84.     (setf *jabber-blacklist* (mapconcat 'identity (delete jid (split-string *jabber-blacklist*)) " "))
  85.     (message "%s removed from jabber blacklist" jid)
  86.     (jabber-save-blacklist)))
  87.  
  88. (defun jabber-remove-from-blacklist-jid-at-point ()
  89.   (interactive)
  90.   (jabber-remove-from-blacklist (get-text-property (point) 'jabber-jid)))
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement