Make erc use auth-source to look up channel keys
[bpt/emacs.git] / lisp / url / url-domsuf.el
CommitLineData
9ea49b28
LMI
1;;; url-domsuf.el --- Say what domain names can have cookies set.
2
3;; Copyright (C) 2011 Free Software Foundation, Inc.
4
5;; Keywords: comm, data, processes, hypermedia
6
7;; This file is part of GNU Emacs.
8;;
9;; GNU Emacs is free software: you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22;;; Commentary:
23
24;; The rules for what domains can have cookies set is defined here:
25;; http://publicsuffix.org/list/
26
27;;; Code:
28
29(defvar url-domsuf-domains nil)
30
31(defun url-domsuf-parse-file ()
32 (with-temp-buffer
33 (insert-file-contents
34 (expand-file-name "publicsuffix.txt" data-directory))
35 (let ((domains nil)
36 domain exception)
37 (while (not (eobp))
38 (when (not (looking-at "[/\n\t ]"))
39 ;; !pref.aichi.jp means that it's allowed.
40 (if (not (eq (following-char) ?!))
41 (setq exception nil)
42 (setq exception t)
43 (forward-char 1))
44 (setq domain (buffer-substring (point) (line-end-position)))
45 (cond
46 ((string-match "\\`\\*\\." domain)
47 (setq domain (substring domain 2))
48 (push (cons domain (1+ (length (split-string domain "[.]"))))
49 domains))
50 (exception
51 (push (cons domain t) domains))
52 (t
53 (push (cons domain nil) domains))))
54 (forward-line 1))
55 (setq url-domsuf-domains (nreverse domains)))))
56
57(defun url-domsuf-cookie-allowed-p (domain)
58 (unless url-domsuf-domains
59 (url-domsuf-parse-file))
60 (let* ((allowedp t)
61 (domain-bits (split-string domain "[.]"))
62 (length (length domain-bits))
63 (upper-domain (mapconcat 'identity (cdr domain-bits) "."))
64 entry modifier)
65 (dolist (elem url-domsuf-domains)
66 (setq entry (car elem)
67 modifier (cdr elem))
68 (cond
69 ;; "com"
70 ((and (null modifier)
71 (string= domain entry))
72 (setq allowedp nil))
73 ;; "!pref.hokkaido.jp"
74 ((and (eq modifier t)
75 (string= domain entry))
76 (setq allowedp t))
77 ;; "*.ar"
78 ((and (numberp modifier)
79 (= length modifier)
80 (string= entry upper-domain))
81 (setq allowedp nil))))
82 allowedp))
83
84;; Tests:
85
86;; (url-domsuf-cookie-allowed-p "com") => nil
87;; (url-domsuf-cookie-allowed-p "foo.bar.ar") => t
88;; (url-domsuf-cookie-allowed-p "bar.ar") => nil
89;; (url-domsuf-cookie-allowed-p "co.uk") => nil
90;; (url-domsuf-cookie-allowed-p "foo.bar.hokkaido.jo") => t
91;; (url-domsuf-cookie-allowed-p "bar.hokkaido.jp") => nil
92;; (url-domsuf-cookie-allowed-p "pref.hokkaido.jp") => t
93
94(provide 'url-domsuf)
95
96;;; url-domsuf.el ends here