Comment detail

日本語メールのエンコード (Nested Flatten)
  • 差し込みデータはiniファイル形式を使用しました。

  • JScriptが文字をUTF-16で扱うため、Subjectのエンコーディングが変態的です。 UTF-16 -> バイト列 -> iso-2022-jp -> Base64 と自前でやるのはしんどかったので…。(--;)

 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
function ADOReadOrSaveFile(path, encoding, str){
  with(new ActiveXObject("ADODB.Stream")) try {
    open(); charset = encoding; type = 2; // 1:TypeBinary, 2:TypeText
    if(str){
      writeText(str);
      saveToFile(path, 2); // 1:SaveCreateNotExist, 2:SaveCreateOverWrite
    } else {
      loadFromFile(path);
      return readText();
    }
  } catch(e){ throw e } finally { close() }
}
function parseIni(ini){
  var r = { Default: {} }, sect = "Default", lines = ini.split(/[\r\n]+/);
  with(RegExp) for(var i = 0, l = lines.length; i < l; i++)
    if(/^\[(.*)\]$|^([^;=]+)=([^;]*)$/.test(lines[i]))
      $1 ? r[sect = $1] = {} : r[sect][$2] = $3;
  return r;
}
function jsBase64Enc(s){
  var $64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
  var r = "", bytes = [], b0, b1, b2, i, l, lrm;
  for(i = 0, l = s.length; i < l; i++){
    if((c = s.charCodeAt(i)) <= 0xff) bytes[bytes.length] = c; // single
    else if(c <= 0xffff) bytes.push(c >> 8, c & 0xff); // double
    else bytes.push(c >> 16, c >> 8 & 0xff, c & 0xff); // triple
  }
  for(i = 0, lrm = bytes.length % 3, l = bytes.length - lrm; i < l; i += 3){
    b0 = bytes[i], b1 = bytes[i + 1], b2 = bytes[i + 2];
    r += $64[b0 >> 2] + $64[(b0 & 3) << 4 | b1 >> 4]
      +  $64[(b1 & 0xf) << 2 | b2 >> 6] + $64[b2 & 0x3f];
  }
  if(lrm){
    b0 = bytes[l], b1 = bytes[l + 1];
    r += $64[b0 >> 2] + (lrm == 1 ? $64[(b0 & 3) << 4] +"=="
                          : $64[(b0 & 3) << 4 | b1 >> 4] + $64[(b1 & 0xf) << 2] +"=");
  }
  return r;
}
function doukaku24(tmpl, insr, path){
  insr = parseIni(ADOReadOrSaveFile(insr, 'utf-8')).Insertions;
  ADOReadOrSaveFile
    (path, "iso-2022-jp", ADOReadOrSaveFile
     (tmpl, 'utf-8').replace(/\r\n?/g, '\n').replace(/\[\[(.*?)\]\]/g, function(_, $){
       return insr[$] || "";
     }).replace(/(\nSubject: )([^\n]+)/, function(_, $1, $2){
       return $1 + '=?UTF-16?B?'+ jsBase64Enc($2) +'?=';
     }).replace(/\n\n/, function($){ with(new Date)
       return [,'Date: '+ toUTCString()
               ,'Message-ID: <'+ getTime() * Math.random() * 24 +'@doukaku.org>'
               ,'MIME-Version: 1.0'
               ,'Content-Type: text/plain; charset="ISO-2022-JP"'
               ,'Content-Transfer-Encoding: 7bit',,].join('\n') }));
}
doukaku24("template.txt", "insertions.ini", "doukaku24.mail");

Index

Feed

Other

Link

Pathtraq

loading...