Synch with the semi-1_14 branch.
[elisp/semi.git] / postpet.el
1 ;;; postpet.el --- Postpet support for GNU Emacs
2
3 ;; Copyright (C) 1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: Tanaka Akira  <akr@jaist.ac.jp>
6 ;; Keywords: Postpet, MIME, multimedia, mail, news
7
8 ;; This file is part of SEMI (Sample of Elastic MIME Interfaces).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'alist)
28
29 (put 'unpack 'lisp-indent-function 1)
30 (defmacro unpack (string &rest body)
31   `(let* ((*unpack*string* (string-as-unibyte ,string))
32           (*unpack*index* 0))
33      ,@body))
34
35 (defun unpack-skip (len)
36   (setq *unpack*index* (+ len *unpack*index*)))
37
38 (defun unpack-fixed (len)
39   (prog1
40       (substring *unpack*string* *unpack*index* (+ *unpack*index* len))
41     (unpack-skip len)))
42
43 (defun unpack-byte ()
44   (char-int (aref (unpack-fixed 1) 0)))
45
46 (defun unpack-short ()
47   (let* ((b0 (unpack-byte))
48          (b1 (unpack-byte)))
49     (+ (* 256 b0) b1)))
50
51 (defun unpack-long ()
52   (let* ((s0 (unpack-short))
53          (s1 (unpack-short)))
54     (+ (* 65536 s0) s1)))
55
56 (defun unpack-string ()
57   (let ((len (unpack-byte)))
58     (unpack-fixed len)))
59
60 (defun unpack-string-sjis ()
61   (decode-mime-charset-string (unpack-string) 'shift_jis))
62
63 ;;;###autoload
64 (defun postpet-decode (string)
65   (condition-case nil
66       (unpack string
67         (let (res)
68           (unpack-skip 4)
69           (set-alist 'res 'carryingcount (unpack-long))
70           (unpack-skip 8)
71           (set-alist 'res 'sentyear (unpack-short))
72           (set-alist 'res 'sentmonth (unpack-short))
73           (set-alist 'res 'sentday (unpack-short))
74           (unpack-skip 8)
75           (set-alist 'res 'petname (unpack-string-sjis))
76           (set-alist 'res 'owner (unpack-string-sjis))
77           (set-alist 'res 'pettype (unpack-fixed 4))
78           (set-alist 'res 'health (unpack-short))
79           (unpack-skip 2)
80           (set-alist 'res 'sex (unpack-long))
81           (unpack-skip 1)
82           (set-alist 'res 'brain (unpack-byte))
83           (unpack-skip 39)
84           (set-alist 'res 'happiness (unpack-byte))
85           (unpack-skip 14)
86           (set-alist 'res 'petbirthyear (unpack-short))
87           (set-alist 'res 'petbirthmonth (unpack-short))
88           (set-alist 'res 'petbirthday (unpack-short))
89           (unpack-skip 8)
90           (set-alist 'res 'from (unpack-string))
91           (unpack-skip 5)
92           (unpack-skip 160)
93           (unpack-skip 4)
94           (unpack-skip 8)
95           (unpack-skip 8)
96           (unpack-skip 26)
97           (set-alist 'res 'treasure (unpack-short))
98           (set-alist 'res 'money (unpack-long))
99           res))
100     (error nil)))
101
102 ;;;###autoload
103 (defun mime-display-application/x-postpet (entity situation)
104   (save-restriction
105     (narrow-to-region (point-max)(point-max))
106     (let ((pet (postpet-decode (mime-entity-content entity))))
107       (if pet
108           (insert
109            "Petname: " (cdr (assq 'petname pet))
110            "\n"
111            "Owner: " (cdr (assq 'owner pet))
112            "\n"
113            "Pettype: " (cdr (assq 'pettype pet))
114            "\n"
115            "From: " (cdr (assq 'from pet))
116            "\n"
117            "CarryingCount: " (int-to-string (cdr (assq 'carryingcount pet)))
118            "\n"
119            "SentYear: " (int-to-string (cdr (assq 'sentyear pet)))
120            "\n"
121            "SentMonth: " (int-to-string (cdr (assq 'sentmonth pet)))
122            "\n"
123            "SentDay: " (int-to-string (cdr (assq 'sentday pet)))
124            "\n"
125            "PetbirthYear: " (int-to-string (cdr (assq 'petbirthyear pet)))
126            "\n"
127            "PetbirthMonth: " (int-to-string (cdr (assq 'petbirthmonth pet)))
128            "\n"
129            "PetbirthDay: " (int-to-string (cdr (assq 'petbirthday pet)))
130            "\n"
131            "Health: " (int-to-string (cdr (assq 'health pet)))
132            "\n"
133            "Sex: " (int-to-string (cdr (assq 'sex pet)))
134            "\n"
135            "Brain: " (int-to-string (cdr (assq 'brain pet)))
136            "\n"
137            "Happiness: " (int-to-string (cdr (assq 'happiness pet)))
138            "\n"
139            "Treasure: " (int-to-string (cdr (assq 'treasure pet)))
140            "\n"
141            "Money: " (int-to-string (cdr (assq 'money pet)))
142            "\n")
143         (insert "Invalid format\n"))
144       (run-hooks 'mime-display-application/x-postpet-hook))))
145
146
147 ;;; @ end
148 ;;;
149
150 (provide 'postpet)
151
152 ;;; postpet.el ends here