File: rail-user-agent.el

package info (click to toggle)
rail 1.2.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 328 kB
  • sloc: lisp: 854; perl: 116; makefile: 60; sh: 23
file content (183 lines) | stat: -rw-r--r-- 7,354 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
;;; rail-user-agent.el --- Replace Agent-string Internal Library

;; Copyright (C) 1999 by Free Software Foundation, Inc.

;; Author: SHIMADA Mitsunobu <simm-emacs@fan.gr.jp>
;;         Youhei SASAKI <uwabami@gfd-dennou.org>
;; Keywords: User-Agent, MIME-Version, FLIM, SEMI, Rail

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Current version supports only MULE, FLIM, and SEMI.

;;; Code:

(require 'rail-common)
(eval-when-compile
  (require 'mime-edit))

;; search header
(defun rail-user-agent-search-header (head)
  "Search header string and return region."
  (let ((case-fold-search t))
    (save-excursion
      (goto-char (point-min))
      (and (re-search-forward (format "^%s:\\s +" head) nil t)
           (let ((pt (point)))
             (if (catch 'header-end
                   (while (not (eobp))
                     (forward-line 1)
                     (or (looking-at "\\s ")
                         (throw 'header-end t))))
                 (progn
                   (forward-char -1)
                   (cons pt (point)))))))))

;; replace MIME-Version: header dynamically
(defun rail-user-agent-replace-mime-version-region (region)
  "Replace codename in MIME-Version: region."
  (and region
       (car region)
       (cdr region)
       (save-restriction
         (narrow-to-region (car region) (cdr region))
         (goto-char (point-min))
         (and (looking-at rail-mime-version-header-format)
              (rail-replace-codename-primitive
               rail-mime-version-header-format
               rail-additional-semi-codename-alist rail-semi-codename-alist))
         (buffer-substring (point-min) (point-max)))))

;; replace User-Agent: header dynamically
(defun rail-user-agent-replace-user-agent-region (region)
  "Replace codename in User-Agent: region."
  (and region
       (car region)
       (cdr region)
       (save-restriction
         (narrow-to-region (car region) (cdr region))
         (goto-char (point-min))
         (while (not (eobp))
           (skip-chars-forward " \t\r\n")
           (if (not (looking-at "\\([^ \t\r\n/]+\\)/"))
               (skip-chars-forward "^ \t\r\n")
             (let ((kind (rail-assoc
                          (upcase (buffer-substring (match-beginning 1) (match-end 1)))
                          rail-product-name-alist t)))
               (if (not kind)
                   (skip-chars-forward "^ \t\r\n")
                 (goto-char (match-end 0))
                 (or
                  (let* ((skind (prin1-to-string kind))
                         (alist-a
                          (intern (format "rail-additional-%s-codename-alist" skind)))
                         (alist-b
                          (intern (format "rail-%s-codename-alist" skind))))
                    (and (looking-at rail-user-agent-header-format)
                         (boundp alist-a)
                         (boundp alist-b)
                         (rail-replace-codename-primitive
                          rail-user-agent-header-format
                          (symbol-value alist-a) (symbol-value alist-b)))))
                 ))))
         (buffer-substring (point-min) (point-max)))))

;; replace header with rail-user-agent-replace-{mime-version|user-agent}-region
(defun rail-user-agent-translate-body (&optional buf)
  "Translate message body. It executes after mime-edit-translate-body."
  (and rail-user-agent-convert-dynamically
       (save-excursion
         (and buf (set-buffer buf))
         (mapc
          (lambda (item)
            (rail-user-agent-replace-mime-version-region
             (rail-user-agent-search-header item)))
          rail-mime-version-header-item-list)
         (mapc
          (lambda (item)
            (rail-user-agent-replace-user-agent-region
             (rail-user-agent-search-header item)))
          rail-user-agent-header-item-list))))

;; replace statically
(defun rail-user-agent-replace-string ()
  "Replace mime-edit-user-agent-value and mime-edit-mime-version-value."
  (and rail-user-agent-convert-statically
       (let (buf codename)
         (save-excursion
           (setq buf (get-buffer-create rail-temporary-buffer-name))
           (if (set-buffer buf)
               (progn
                 ;; mime-edit-mime-version-value
                 (erase-buffer)
                 (insert mime-edit-mime-version-value)
                 (setq mime-edit-mime-version-value
                       (rail-user-agent-replace-mime-version-region
                        (cons (point-min) (point-max))))
                 ;; mime-edit-user-agent-value
                 (erase-buffer)
                 (insert mime-edit-user-agent-value)
                 (setq mime-edit-user-agent-value
                       (rail-user-agent-replace-user-agent-region
                        (cons (point-min) (point-max))))
                 ;; end
                 (kill-buffer buf))))))
  ;; mime-library-product
  (and rail-user-agent-convert-statically
       rail-user-agent-replace-mime-library-product
       (boundp 'mime-library-product)
       (let ((codename (mime-product-code-name mime-library-product)))
         (aset mime-library-product 2
               (or (rail-assoc
                    codename
                    (append rail-additional-flim-codename-alist
                            rail-flim-codename-alist)
                    rail-convert-direction)
                   codename))))
  ;; mime-user-interface-product
  (and rail-user-agent-convert-statically
       rail-user-agent-replace-mime-user-interface-product
       (boundp 'mime-user-interface-product)
       (let ((codename (mime-product-code-name mime-user-interface-product)))
         (aset mime-user-interface-product 2
               (or (rail-assoc
                    codename
                    (append rail-additional-semi-codename-alist
                            rail-semi-codename-alist)
                    rail-convert-direction)
                   codename)))))

;; add mime-edit-translate-buffer-hook
(defun rail-user-agent-add-hook ()
  "Add hook for rail-user-agent"
  (let ((list (memq 'mime-edit-translate-body mime-edit-translate-buffer-hook)))
    (or (memq 'rail-user-agent-translate-body mime-edit-translate-buffer-hook)
        (setcdr list (cons 'rail-user-agent-translate-body (cdr list))))))

;; add mime-edit-load-hook
(if (not (featurep 'mime-edit))
    (add-hook 'mime-edit-load-hook
              (lambda ()
                (rail-user-agent-add-hook)
                (rail-user-agent-replace-string)))
  (rail-user-agent-add-hook)
  (rail-user-agent-replace-string))

(provide 'rail-user-agent)

;;; rail-user-agent.el ends here