1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
10 Cu.import("resource://gre/modules/WspPduHelper.jsm", WSP);
12 Cu.import("resource://gre/modules/WbxmlPduHelper.jsm", WBXML);
14 // set to true to see debug messages
15 let DEBUG = WBXML.DEBUG_ALL | false;
18 * Public identifier for SI
20 * @see http://technical.openmobilealliance.org/tech/omna/omna-wbxml-public-docid.aspx
22 const PUBLIC_IDENTIFIER_SI = "-//WAPFORUM//DTD SI 1.0//EN";
25 * Parse inline date encoded in OPAQUE format.
28 * A wrapped object containing raw PDU data.
30 * Target object for decoding.
32 * @see WAP-167-SERVICEIND-20010731-A, clause 8.2.2
36 decode: function decode_opaque_as_date(data) {
37 let size = WSP.UintVar.decode(data);
38 let dateBuf = [0, 0, 0, 0, 0, 0, 0];
40 // Maximum length for date is 7 bytes.
41 if (size > dateBuf.length)
44 // Read date date, non-specified parts remain 0.
45 for (let i = 0; i < size; i++) {
46 dateBuf[i] = WSP.Octet.decode(data);
49 // Decode and return result in "YYYY-MM-DDThh:mm:ssZ" form
50 let year = ((dateBuf[0] >> 4) & 0x0F) * 1000 + (dateBuf[0] & 0x0F) * 100 +
51 ((dateBuf[1] >> 4) & 0x0F) * 10 + (dateBuf[1] & 0x0F);
52 let month = ((dateBuf[2] >> 4) & 0x0F) * 10 + (dateBuf[2] & 0x0F);
53 let date = ((dateBuf[3] >> 4) & 0x0F) * 10 + (dateBuf[3] & 0x0F);
54 let hour = ((dateBuf[4] >> 4) & 0x0F) * 10 + (dateBuf[4] & 0x0F);
55 let minute = ((dateBuf[5] >> 4) & 0x0F) * 10 + (dateBuf[5] & 0x0F);
56 let second = ((dateBuf[6] >> 4) & 0x0F) * 10 + (dateBuf[6] & 0x0F);
57 let dateValue = new Date(Date.UTC(year, month - 1, date, hour, minute, second));
59 return dateValue.toISOString().replace(".000", "");
67 * A wrapped object containing raw PDU data.
69 * Content type of incoming SI message, should be "text/vnd.wap.si" or
70 * "application/vnd.wap.sic".
72 * @return A message object containing attribute content and contentType.
73 * |content| will contain string of decoded SI message if successfully
74 * decoded, or raw data if failed.
75 * |contentType| will be string representing corresponding type of
78 parse: function parse_si(data, contentType) {
79 // We only need content and contentType
81 contentType: contentType
85 * Message is compressed by WBXML, decode into string.
87 * @see WAP-192-WBXML-20010725-A
89 if (contentType === "application/vnd.wap.sic") {
90 let globalTokenOverride = {};
91 globalTokenOverride[0xC3] = {
97 publicId: PUBLIC_IDENTIFIER_SI,
98 tagTokenList: SI_TAG_FIELDS,
99 attrTokenList: SI_ATTRIBUTE_FIELDS,
100 valueTokenList: SI_VALUE_FIELDS,
101 globalTokenOverride: globalTokenOverride
105 let parseResult = WBXML.PduHelper.parse(data, appToken);
106 msg.content = parseResult.content;
107 msg.contentType = "text/vnd.wap.si";
109 // Provide raw data if we failed to parse.
110 msg.content = data.array;
116 * Message is plain text, transform raw to string.
119 let stringData = WSP.Octet.decodeMultiple(data, data.array.length);
120 msg.content = WSP.PduHelper.decodeStringContent(stringData, "UTF-8");
122 // Provide raw data if we failed to parse.
123 msg.content = data.array;
133 * @see WAP-167-SERVICEIND-20010731-A, clause 8.3.1
135 const SI_TAG_FIELDS = (function () {
137 function add(name, codepage, number) {
142 if (!names[codepage]) {
143 names[codepage] = {};
145 names[codepage][number] = entry;
149 add("indication", 0, 0x06);
150 add("info", 0, 0x07);
151 add("item", 0, 0x08);
159 * @see WAP-167-SERVICEIND-20010731-A, clause 8.3.2
161 const SI_ATTRIBUTE_FIELDS = (function () {
163 function add(name, value, codepage, number) {
169 if (!names[codepage]) {
170 names[codepage] = {};
172 names[codepage][number] = entry;
175 add("action", "signal-none", 0, 0x05);
176 add("action", "signal-low", 0, 0x06);
177 add("action", "signal-medium", 0, 0x07);
178 add("action", "signal-high", 0, 0x08);
179 add("action", "delete", 0, 0x09);
180 add("created", "", 0, 0x0A);
181 add("href", "", 0, 0x0B);
182 add("href", "http://", 0, 0x0C);
183 add("href", "http://www.", 0, 0x0D);
184 add("href", "https://", 0, 0x0E);
185 add("href", "https://www.", 0, 0x0F);
186 add("si-expires", "", 0, 0x10);
187 add("si-id", "", 0, 0x11);
188 add("class", "", 0, 0x12);
193 const SI_VALUE_FIELDS = (function () {
195 function add(value, codepage, number) {
200 if (!names[codepage]) {
201 names[codepage] = {};
203 names[codepage][number] = entry;
206 add(".com/", 0, 0x85);
207 add(".edu/", 0, 0x86);
208 add(".net/", 0, 0x87);
209 add(".org/", 0, 0x88);
214 this.EXPORTED_SYMBOLS = [