New module.
[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 (put 'unpack 'lisp-indent-function 1)
28 (defmacro unpack (string &rest body)
29   `(let* ((*unpack*string* (string-as-unibyte ,string))
30           (*unpack*index* 0))
31      ,@body))
32
33 (defun unpack-skip (len)
34   (setq *unpack*index* (+ len *unpack*index*)))
35
36 (defun unpack-fixed (len)
37   (prog1
38       (substring *unpack*string* *unpack*index* (+ *unpack*index* len))
39     (unpack-skip len)))
40
41 (defun unpack-byte ()
42   (char-int (aref (unpack-fixed 1) 0)))
43
44 (defun unpack-short ()
45   (let* ((b0 (unpack-byte))
46          (b1 (unpack-byte)))
47     (+ (* 256 b0) b1)))
48
49 (defun unpack-long ()
50   (let* ((s0 (unpack-short))
51          (s1 (unpack-short)))
52     (+ (* 65536 s0) s1)))
53
54 (defun unpack-string ()
55   (let ((len (unpack-byte)))
56     (unpack-fixed len)))
57
58 (defun unpack-string-sjis ()
59   (decode-mime-charset-string (unpack-string) 'shift_jis))
60
61 (defun postpet-decode (string)
62   (condition-case nil
63       (unpack string
64         (let (res)
65           (unpack-skip 4)
66           (set-alist 'res 'carryingcount (unpack-long))
67           (unpack-skip 8)
68           (set-alist 'res 'sentyear (unpack-short))
69           (set-alist 'res 'sentmonth (unpack-short))
70           (set-alist 'res 'sentday (unpack-short))
71           (unpack-skip 8)
72           (set-alist 'res 'petname (unpack-string-sjis))
73           (set-alist 'res 'owner (unpack-string-sjis))
74           (set-alist 'res 'pettype (unpack-fixed 4))
75           (set-alist 'res 'health (unpack-short))
76           (unpack-skip 2)
77           (set-alist 'res 'sex (unpack-long))
78           (unpack-skip 1)
79           (set-alist 'res 'brain (unpack-byte))
80           (unpack-skip 39)
81           (set-alist 'res 'happiness (unpack-byte))
82           (unpack-skip 14)
83           (set-alist 'res 'petbirthyear (unpack-short))
84           (set-alist 'res 'petbirthmonth (unpack-short))
85           (set-alist 'res 'petbirthday (unpack-short))
86           (unpack-skip 8)
87           (set-alist 'res 'from (unpack-string))
88           (unpack-skip 5)
89           (unpack-skip 160)
90           (unpack-skip 4)
91           (unpack-skip 8)
92           (unpack-skip 8)
93           (unpack-skip 26)
94           (set-alist 'res 'treasure (unpack-short))
95           (set-alist 'res 'money (unpack-long))
96           res))
97     (error nil)))
98
99 (defun mime-display-application/x-postpet (entity situation)
100   (save-restriction
101     (narrow-to-region (point-max)(point-max))
102     (let ((pet (postpet-decode (mime-entity-content entity))))
103       (if pet
104           (insert "Petname: " (cdr (assq 'petname pet)) "\n"
105                   "Owner: " (cdr (assq 'owner pet)) "\n"
106                   "Pettype: " (cdr (assq 'pettype pet)) "\n"
107                   "From: " (cdr (assq 'from pet)) "\n"
108                   "CarryingCount: " (int-to-string (cdr (assq 'carryingcount pet))) "\n"
109                   "SentYear: " (int-to-string (cdr (assq 'sentyear pet))) "\n"
110                   "SentMonth: " (int-to-string (cdr (assq 'sentmonth pet))) "\n"
111                   "SentDay: " (int-to-string (cdr (assq 'sentday pet))) "\n"
112                   "PetbirthYear: " (int-to-string (cdr (assq 'petbirthyear pet))) "\n"
113                   "PetbirthMonth: " (int-to-string (cdr (assq 'petbirthmonth pet))) "\n"
114                   "PetbirthDay: " (int-to-string (cdr (assq 'petbirthday pet))) "\n"
115                   "Health: " (int-to-string (cdr (assq 'health pet))) "\n"
116                   "Sex: " (int-to-string (cdr (assq 'sex pet))) "\n"
117                   "Brain: " (int-to-string (cdr (assq 'brain pet))) "\n"
118                   "Happiness: " (int-to-string (cdr (assq 'happiness pet))) "\n"
119                   "Treasure: " (int-to-string (cdr (assq 'treasure pet))) "\n"
120                   "Money: " (int-to-string (cdr (assq 'money pet))) "\n"
121                   )
122         (insert "Invalid format\n"))
123       (run-hooks 'mime-display-application/x-postpet-hook))))
124
125
126 ;;; @ end
127 ;;;
128
129 (provide 'postpet)
130
131 ;;; postpet.el ends here