TB: translating international characters - Mozilla

This is a discussion on TB: translating international characters - Mozilla ; Hello, I'm writing an extension in which I want to add the e-mails to another program. I already get the whole content of the e-mail and I already extracted the body etc. Problem is that the e-mails contain code like ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: TB: translating international characters

  1. TB: translating international characters

    Hello,
    I'm writing an extension in which I want to add the e-mails to another
    program.

    I already get the whole content of the e-mail and I already extracted
    the body etc. Problem is that the e-mails contain code like this:
    "Viele Gr=FC=DFe"

    I could write a lot of string.replace methods, but it seems over-the-
    top. So I have started to look for methods that automatically
    translate them into something readable (or at least several methods
    that translate UTF-8, ISO-8859-1 into one thing). TB must have
    something like this since Gr=FC=DFe is correctly displayed.
    DisplayMessage() seems to be something, but I couldn't find any
    example so far. And using mozilla.org I don't understand it.

    However so far I have not been very lucky. So reason might be my not-
    so-efficient searchwords ( UTF-8, thunderbird).

    Any better searchwords or hints or links would be helpful.

    Greetings,
    Serratia

  2. Re: TB: translating international characters

    On 13 Jan., 14:26, gNeandr wrote:

    > Isn't it the nsIScriptableUnicodeConverter you have to use here??http://www.xulplanet.com/references/...ScriptableUnic...


    After a long break where I worked on something else, I returned back
    to the problem and tried to find out more about
    nsIScriptableUnicodeConverter. Unfortunately XULPlanet doesn't seem to
    exist anymore, so I used https://developer.mozilla.org/en/Reading_textual_data
    from MDC. I tried the examples under "Converting Read Data" and both
    "Reading Strings" Versions. Neither of them brought the expected
    result.

    Here is an snippet from my Code, using the newer "Reading Strings"-
    Version:
    var MY_ID = "something@example.net";
    var em = Components.classes["@mozilla.org/extensions/manager;
    1"].getService(Components.interfaces.nsIExtensionMana ger);
    var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    var foStream = Components.classes["@mozilla.org/network/file-output-
    stream;1"].createInstance(Components.interfaces.nsIFileOutpu tStream);
    foStream.init(file, 0x02 | 0x08 | 0x10, -1, 0);
    foStream.write(encoded_string, encoded_string.length);
    foStream.close();

    var istream = Components.classes["@mozilla.org/network/file-input-
    stream;1"].createInstance(Components.interfaces.nsIFileInput Stream);
    var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    istream.init(file, -1, -1, false);

    var replacementChar =
    Components.interfaces.nsIConverterInputStream.DEFA ULT_REPLACEMENT_CHARACTER;
    var is = Components.classes["@mozilla.org/intl/converter-input-stream;
    1"].createInstance(Components.interfaces.nsIConverter InputStream);
    is.init(istream, charset, 1024, replacementChar);

    var str = {};
    while (is.readString(4096, str) != 0) {
    decoded_string += str.value;
    }
    is.close();
    file.remove(true);


    The other code snippet (here with nsIScripableUnicodeConverter) looks
    like this:
    var MY_ID = "something@example.net";
    var em = Components.classes["@mozilla.org/extensions/manager;
    1"].getService(Components.interfaces.nsIExtensionMana ger);
    var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    var foStream = Components.classes["@mozilla.org/network/file-output-
    stream;1"].createInstance(Components.interfaces.nsIFileOutpu tStream);
    foStream.init(file, 0x02 | 0x08 | 0x10, -1, 0);
    foStream.write(encoded_string, encoded_string.length);
    foStream.close();

    var istream = Components.classes["@mozilla.org/network/file-input-
    stream;1"].createInstance(Components.interfaces.nsIFileInput Stream);
    var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    istream.init(file, -1, -1, false);

    var converter = Components.classes["@mozilla.org/intl/
    scriptableunicodeconverter"].createInstance
    (Components.interfaces.nsIScriptableUnicodeConvert er);
    converter.charset = charset;

    var scriptableStream = Components.classes["@mozilla.org/
    scriptableinputstream;1"].createInstance
    (Components.interfaces.nsIScriptableInputStream);
    scriptableStream.init(istream);
    var body;
    while (scriptableStream.available()) {
    body = body + scriptableStream.read(512);
    }

    decoded_string = converter.ConvertToUnicode(body);
    file.remove(true);

    encoded_string was in the first example Gr=FC=DFe (charset =
    iso-8859-1) (=Gre) and in the second =C3=A4 (charset = utf-8)(= ).
    The decoded string at the end though was always the same as the input.

    Hints, on what I am doing wrong, would be greatly appreciated.

    Greetings,
    Serratia

  3. Re: TB: translating international characters

    On 11 Mrz., 15:27, Serratia wrote:
    > On 13 Jan., 14:26, gNeandr wrote:
    >
    > > Isn't it the nsIScriptableUnicodeConverter you have to use here??http://www.xulplanet.com/references/...ScriptableUnic...

    >
    > After a long break where I worked on something else, I returned back
    > to the problem and tried to find out more about
    > nsIScriptableUnicodeConverter. Unfortunately XULPlanet doesn't seem to
    > exist anymore, so I usedhttps://developer.mozilla.org/en/Reading_textual_data
    > from MDC. I tried the examples under "Converting Read Data" and both
    > "Reading Strings" Versions. Neither of them brought the expected
    > result.
    >
    > Here is an snippet from my Code, using the newer "Reading Strings"-
    > Version:
    > * * * * var MY_ID = "someth...@example.net";
    > * * * * var em = Components.classes["@mozilla.org/extensions/manager;
    > 1"].getService(Components.interfaces.nsIExtensionMana ger);
    > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > * * * * var foStream = Components.classes["@mozilla.org/network/file-output-
    > stream;1"].createInstance(Components.interfaces.nsIFileOutpu tStream);
    > * * * * foStream.init(file, 0x02 | 0x08 | 0x10, -1, 0);
    > * * * * foStream.write(encoded_string, encoded_string.length);
    > * * * * foStream.close();
    >
    > * * * * var istream = Components.classes["@mozilla.org/network/file-input-
    > stream;1"].createInstance(Components.interfaces.nsIFileInput Stream);
    > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > * * * * istream.init(file, -1, -1, false);
    >
    > * * * * var replacementChar =
    > Components.interfaces.nsIConverterInputStream.DEFA ULT_REPLACEMENT_CHARACTER;
    > * * * * var is = Components.classes["@mozilla.org/intl/converter-input-stream;
    > 1"].createInstance(Components.interfaces.nsIConverter InputStream);
    > * * * * is.init(istream, charset, 1024, replacementChar);
    >
    > * * * * var str = {};
    > * * * * while (is.readString(4096, str) != 0) {
    > * * * * * * * * decoded_string += str.value;
    > * * * * }
    > * * * * is.close();
    > * * * * file.remove(true);
    >
    > The other code snippet (here with nsIScripableUnicodeConverter) looks
    > like this:
    > * * * * var MY_ID = "someth...@example.net";
    > * * * * var em = Components.classes["@mozilla.org/extensions/manager;
    > 1"].getService(Components.interfaces.nsIExtensionMana ger);
    > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > * * * * var foStream = Components.classes["@mozilla.org/network/file-output-
    > stream;1"].createInstance(Components.interfaces.nsIFileOutpu tStream);
    > * * * * foStream.init(file, 0x02 | 0x08 | 0x10, -1, 0);
    > * * * * foStream.write(encoded_string, encoded_string.length);
    > * * * * foStream.close();
    >
    > * * * * var istream = Components.classes["@mozilla.org/network/file-input-
    > stream;1"].createInstance(Components.interfaces.nsIFileInput Stream);
    > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > * * * * istream.init(file, -1, -1, false);
    >
    > * * * * var converter = Components.classes["@mozilla.org/intl/
    > scriptableunicodeconverter"].createInstance
    > (Components.interfaces.nsIScriptableUnicodeConvert er);
    > * * * * converter.charset = charset;
    >
    > * * * * var scriptableStream = Components.classes["@mozilla.org/
    > scriptableinputstream;1"].createInstance
    > (Components.interfaces.nsIScriptableInputStream);
    > * * * * scriptableStream.init(istream);
    > * * * * var body;
    > * * * * while (scriptableStream.available()) {
    > * * * * * * * * body = body + scriptableStream.read(512);
    > * * * * }
    >
    > * * * * decoded_string = converter.ConvertToUnicode(body);
    > * * * * file.remove(true);
    >
    > encoded_string was in the first example Gr=FC=DFe (charset =
    > iso-8859-1) (=Gre) and in the second =C3=A4 (charset = utf-8) (= ).
    > The decoded string at the end though was always the same as the input.
    >
    > Hints, on what I am doing wrong, would be greatly appreciated.
    >
    > Greetings,
    > Serratia


    Somehow I have come to think that nsIScriptableUnicodeConverter is not
    the correct way. After all: I have a string looking like this =E4 (or
    =C3=A4). The bytes behind this are specified by the Xuls charset, are
    they not? Anyway the above methods don't recognize that =E4 is some
    kind of special character. The =E4 is something MIME specific so I
    guess I have to search for a mimeconverter or something similar.
    I have been looking at MXR to find out how mime2decodedSubject is
    created from subject, but so far I have not found the correct package/
    js-file/method.

    Hints, tipps, other plugins in this direction would be greatly
    appreciated.

    Greetings,
    Serratia

  4. Re: TB: translating international characters

    On 12 Mrz., 16:01, Serratia wrote:
    > On 11 Mrz., 15:27, Serratia wrote:
    >
    >
    >
    > > On 13 Jan., 14:26, gNeandr wrote:

    >
    > > > Isn't it the nsIScriptableUnicodeConverter you have to use here??http://www.xulplanet.com/references/...ScriptableUnic...

    >
    > > After a long break where I worked on something else, I returned back
    > > to the problem and tried to find out more about
    > > nsIScriptableUnicodeConverter. Unfortunately XULPlanet doesn't seem to
    > > exist anymore, so I usedhttps://developer.mozilla.org/en/Reading_textual_data
    > > from MDC. I tried the examples under "Converting Read Data" and both
    > > "Reading Strings" Versions. Neither of them brought the expected
    > > result.

    >
    > > Here is an snippet from my Code, using the newer "Reading Strings"-
    > > Version:
    > > * * * * var MY_ID = "someth...@example.net";
    > > * * * * var em = Components.classes["@mozilla.org/extensions/manager;
    > > 1"].getService(Components.interfaces.nsIExtensionMana ger);
    > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > * * * * var foStream = Components.classes["@mozilla.org/network/file-output-
    > > stream;1"].createInstance(Components.interfaces.nsIFileOutpu tStream);
    > > * * * * foStream.init(file, 0x02 | 0x08 | 0x10, -1, 0);
    > > * * * * foStream.write(encoded_string, encoded_string.length);
    > > * * * * foStream.close();

    >
    > > * * * * var istream = Components.classes["@mozilla.org/network/file-input-
    > > stream;1"].createInstance(Components.interfaces.nsIFileInput Stream);
    > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > * * * * istream.init(file, -1, -1, false);

    >
    > > * * * * var replacementChar =
    > > Components.interfaces.nsIConverterInputStream.DEFA ULT_REPLACEMENT_CHARACTER;
    > > * * * * var is = Components.classes["@mozilla.org/intl/converter-input-stream;
    > > 1"].createInstance(Components.interfaces.nsIConverter InputStream);
    > > * * * * is.init(istream, charset, 1024, replacementChar);

    >
    > > * * * * var str = {};
    > > * * * * while (is.readString(4096, str) != 0) {
    > > * * * * * * * * decoded_string += str.value;
    > > * * * * }
    > > * * * * is.close();
    > > * * * * file.remove(true);

    >
    > > The other code snippet (here with nsIScripableUnicodeConverter) looks
    > > like this:
    > > * * * * var MY_ID = "someth...@example.net";
    > > * * * * var em = Components.classes["@mozilla.org/extensions/manager;
    > > 1"].getService(Components.interfaces.nsIExtensionMana ger);
    > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > * * * * var foStream = Components.classes["@mozilla.org/network/file-output-
    > > stream;1"].createInstance(Components.interfaces.nsIFileOutpu tStream);
    > > * * * * foStream.init(file, 0x02 | 0x08 | 0x10, -1, 0);
    > > * * * * foStream.write(encoded_string, encoded_string.length);
    > > * * * * foStream.close();

    >
    > > * * * * var istream = Components.classes["@mozilla.org/network/file-input-
    > > stream;1"].createInstance(Components.interfaces.nsIFileInput Stream);
    > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > * * * * istream.init(file, -1, -1, false);

    >
    > > * * * * var converter = Components.classes["@mozilla.org/intl/
    > > scriptableunicodeconverter"].createInstance
    > > (Components.interfaces.nsIScriptableUnicodeConvert er);
    > > * * * * converter.charset = charset;

    >
    > > * * * * var scriptableStream = Components.classes["@mozilla.org/
    > > scriptableinputstream;1"].createInstance
    > > (Components.interfaces.nsIScriptableInputStream);
    > > * * * * scriptableStream.init(istream);
    > > * * * * var body;
    > > * * * * while (scriptableStream.available()) {
    > > * * * * * * * * body = body + scriptableStream.read(512);
    > > * * * * }

    >
    > > * * * * decoded_string = converter.ConvertToUnicode(body);
    > > * * * * file.remove(true);

    >
    > > encoded_string was in the first example Gr=FC=DFe (charset =
    > > iso-8859-1) (=Gre) and in the second =C3=A4 (charset = utf-8) (= ).
    > > The decoded string at the end though was always the same as the input.

    >
    > > Hints, on what I am doing wrong, would be greatly appreciated.

    >
    > > Greetings,
    > > Serratia

    >
    > Somehow I have come to think that nsIScriptableUnicodeConverter is not
    > the correct way. After all: I have a string looking like this =E4 (or
    > =C3=A4). The bytes behind this are specified by the Xuls charset, are
    > they not? Anyway the above methods don't recognize that =E4 is some
    > kind of special character. The =E4 is something MIME specific so I
    > guess I have to search for a mimeconverter or something similar.
    > I have been looking at MXR to find out how mime2decodedSubject is
    > created from subject, but so far I have not found the correct package/
    > js-file/method.
    >
    > Hints, tipps, other plugins in this direction would be greatly
    > appreciated.
    >
    > Greetings,
    > Serratia


    With some help I found a solution which worked for my problem:
    [...]
    encoded_string = cb_email_converter.unescape_chars(encoded_string);

    var converter = Components.classes["@mozilla.org/intl/
    scriptableunicodeconverter"].createInstance
    (Components.interfaces.nsIScriptableUnicodeConvert er);
    converter.charset = charset;
    decoded_string = converter.ConvertToUnicode(encoded_string);
    return decoded_string;
    }

    /**
    * unescapes the characters ergo =E4 --> %E4 ( which is needed by
    unescape) --> (its byte order according to its charset)
    */
    unescape_chars = function(text){
    text = text.replace(/=[0-9A-F]{2}/g,decode_HEX_into_DEC);
    text = text.replace(/=\s\n/gi,"");
    return text;
    }

    decode_HEX_into_DEC = function(encodedString) {
    decCode = "%" + encodedString.substr(1, 2);
    return unescape(decCode);
    }

  5. Re: TB: translating international characters

    On Mar 12, 11:01*am, Serratia wrote:
    > On 11 Mrz., 15:27, Serratia wrote:
    >
    >
    >
    > > On 13 Jan., 14:26, gNeandr wrote:

    >
    > > > Isn't it the nsIScriptableUnicodeConverter you have to use here??http://www.xulplanet.com/references/...ScriptableUnic...

    >
    > > After a long break where I worked on something else, I returned back
    > > to the problem and tried to find out more about
    > > nsIScriptableUnicodeConverter. Unfortunately XULPlanet doesn't seem to
    > > exist anymore, so I usedhttps://developer.mozilla.org/en/Reading_textual_data
    > > from MDC. I tried the examples under "Converting Read Data" and both
    > > "Reading Strings" Versions. Neither of them brought the expected
    > > result.

    >
    > > Here is an snippet from my Code, using the newer "Reading Strings"-
    > > Version:
    > > * * * * var MY_ID = "someth...@example.net";
    > > * * * * var em = Components.classes["@mozilla.org/extensions/manager;
    > > 1"].getService(Components.interfaces.nsIExtensionMana ger);
    > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > * * * * var foStream = Components.classes["@mozilla.org/network/file-output-
    > > stream;1"].createInstance(Components.interfaces.nsIFileOutpu tStream);
    > > * * * * foStream.init(file, 0x02 | 0x08 | 0x10, -1, 0);
    > > * * * * foStream.write(encoded_string, encoded_string.length);
    > > * * * * foStream.close();

    >
    > > * * * * var istream = Components.classes["@mozilla.org/network/file-input-
    > > stream;1"].createInstance(Components.interfaces.nsIFileInput Stream);
    > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > * * * * istream.init(file, -1, -1, false);

    >
    > > * * * * var replacementChar =
    > > Components.interfaces.nsIConverterInputStream.DEFA ULT_REPLACEMENT_CHARACTER;
    > > * * * * var is = Components.classes["@mozilla.org/intl/converter-input-stream;
    > > 1"].createInstance(Components.interfaces.nsIConverter InputStream);
    > > * * * * is.init(istream, charset, 1024, replacementChar);

    >
    > > * * * * var str = {};
    > > * * * * while (is.readString(4096, str) != 0) {
    > > * * * * * * * * decoded_string += str.value;
    > > * * * * }
    > > * * * * is.close();
    > > * * * * file.remove(true);

    >
    > > The other code snippet (here with nsIScripableUnicodeConverter) looks
    > > like this:
    > > * * * * var MY_ID = "someth...@example.net";
    > > * * * * var em = Components.classes["@mozilla.org/extensions/manager;
    > > 1"].getService(Components.interfaces.nsIExtensionMana ger);
    > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > * * * * var foStream = Components.classes["@mozilla.org/network/file-output-
    > > stream;1"].createInstance(Components.interfaces.nsIFileOutpu tStream);
    > > * * * * foStream.init(file, 0x02 | 0x08 | 0x10, -1, 0);
    > > * * * * foStream.write(encoded_string, encoded_string.length);
    > > * * * * foStream.close();

    >
    > > * * * * var istream = Components.classes["@mozilla.org/network/file-input-
    > > stream;1"].createInstance(Components.interfaces.nsIFileInput Stream);
    > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > * * * * istream.init(file, -1, -1, false);

    >
    > > * * * * var converter = Components.classes["@mozilla.org/intl/
    > > scriptableunicodeconverter"].createInstance
    > > (Components.interfaces.nsIScriptableUnicodeConvert er);
    > > * * * * converter.charset = charset;

    >
    > > * * * * var scriptableStream = Components.classes["@mozilla.org/
    > > scriptableinputstream;1"].createInstance
    > > (Components.interfaces.nsIScriptableInputStream);
    > > * * * * scriptableStream.init(istream);
    > > * * * * var body;
    > > * * * * while (scriptableStream.available()) {
    > > * * * * * * * * body = body + scriptableStream.read(512);
    > > * * * * }

    >
    > > * * * * decoded_string = converter.ConvertToUnicode(body);
    > > * * * * file.remove(true);

    >
    > > encoded_string was in the first example Gr=FC=DFe (charset =
    > > iso-8859-1) (=Gre) and in the second =C3=A4 (charset = utf-8) (= ).
    > > The decoded string at the end though was always the same as the input.

    >
    > > Hints, on what I am doing wrong, would be greatly appreciated.

    >
    > > Greetings,
    > > Serratia

    >
    > Somehow I have come to think that nsIScriptableUnicodeConverter is not
    > the correct way. After all: I have a string looking like this =E4 (or
    > =C3=A4). The bytes behind this are specified by the Xuls charset, are
    > they not? Anyway the above methods don't recognize that =E4 is some
    > kind of special character. The =E4 is something MIME specific so I
    > guess I have to search for a mimeconverter or something similar.
    > I have been looking at MXR to find out how mime2decodedSubject is
    > created from subject, but so far I have not found the correct package/
    > js-file/method.


    That is called quoted-printable format. MIME but different from
    base64.

    Aron

  6. Re: TB: translating international characters

    On Mar 13, 8:48*am, Serratia wrote:
    > On 12 Mrz., 16:01, Serratia wrote:
    >
    >
    >
    > > On 11 Mrz., 15:27, Serratia wrote:

    >
    > > > On 13 Jan., 14:26, gNeandr wrote:

    >
    > > > > Isn't it the nsIScriptableUnicodeConverter you have to use here??http://www.xulplanet.com/references/...ScriptableUnic...

    >
    > > > After a long break where I worked on something else, I returned back
    > > > to the problem and tried to find out more about
    > > > nsIScriptableUnicodeConverter. Unfortunately XULPlanet doesn't seem to
    > > > exist anymore, so I usedhttps://developer.mozilla.org/en/Reading_textual_data
    > > > from MDC. I tried the examples under "Converting Read Data" and both
    > > > "Reading Strings" Versions. Neither of them brought the expected
    > > > result.

    >
    > > > Here is an snippet from my Code, using the newer "Reading Strings"-
    > > > Version:
    > > > * * * * var MY_ID = "someth...@example.net";
    > > > * * * * var em = Components.classes["@mozilla.org/extensions/manager;
    > > > 1"].getService(Components.interfaces.nsIExtensionMana ger);
    > > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > > * * * * var foStream = Components.classes["@mozilla.org/network/file-output-
    > > > stream;1"].createInstance(Components.interfaces.nsIFileOutpu tStream);
    > > > * * * * foStream.init(file, 0x02 | 0x08 | 0x10, -1, 0);
    > > > * * * * foStream.write(encoded_string, encoded_string.length);
    > > > * * * * foStream.close();

    >
    > > > * * * * var istream = Components.classes["@mozilla.org/network/file-input-
    > > > stream;1"].createInstance(Components.interfaces.nsIFileInput Stream);
    > > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > > * * * * istream.init(file, -1, -1, false);

    >
    > > > * * * * var replacementChar =
    > > > Components.interfaces.nsIConverterInputStream.DEFA ULT_REPLACEMENT_CHARACTER;
    > > > * * * * var is = Components.classes["@mozilla.org/intl/converter-input-stream;
    > > > 1"].createInstance(Components.interfaces.nsIConverter InputStream);
    > > > * * * * is.init(istream, charset, 1024, replacementChar);

    >
    > > > * * * * var str = {};
    > > > * * * * while (is.readString(4096, str) != 0) {
    > > > * * * * * * * * decoded_string += str.value;
    > > > * * * * }
    > > > * * * * is.close();
    > > > * * * * file.remove(true);

    >
    > > > The other code snippet (here with nsIScripableUnicodeConverter) looks
    > > > like this:
    > > > * * * * var MY_ID = "someth...@example.net";
    > > > * * * * var em = Components.classes["@mozilla.org/extensions/manager;
    > > > 1"].getService(Components.interfaces.nsIExtensionMana ger);
    > > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > > * * * * var foStream = Components.classes["@mozilla.org/network/file-output-
    > > > stream;1"].createInstance(Components.interfaces.nsIFileOutpu tStream);
    > > > * * * * foStream.init(file, 0x02 | 0x08 | 0x10, -1, 0);
    > > > * * * * foStream.write(encoded_string, encoded_string.length);
    > > > * * * * foStream.close();

    >
    > > > * * * * var istream = Components.classes["@mozilla.org/network/file-input-
    > > > stream;1"].createInstance(Components.interfaces.nsIFileInput Stream);
    > > > * * * * var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID,"tmp.txt");
    > > > * * * * istream.init(file, -1, -1, false);

    >
    > > > * * * * var converter = Components.classes["@mozilla.org/intl/
    > > > scriptableunicodeconverter"].createInstance
    > > > (Components.interfaces.nsIScriptableUnicodeConvert er);
    > > > * * * * converter.charset = charset;

    >
    > > > * * * * var scriptableStream = Components.classes["@mozilla..org/
    > > > scriptableinputstream;1"].createInstance
    > > > (Components.interfaces.nsIScriptableInputStream);
    > > > * * * * scriptableStream.init(istream);
    > > > * * * * var body;
    > > > * * * * while (scriptableStream.available()) {
    > > > * * * * * * * * body = body + scriptableStream.read(512);
    > > > * * * * }

    >
    > > > * * * * decoded_string = converter.ConvertToUnicode(body);
    > > > * * * * file.remove(true);

    >
    > > > encoded_string was in the first example Gr=FC=DFe (charset =
    > > > iso-8859-1) (=Gre) and in the second =C3=A4 (charset = utf-8) (= ).
    > > > The decoded string at the end though was always the same as the input..

    >
    > > > Hints, on what I am doing wrong, would be greatly appreciated.

    >
    > > > Greetings,
    > > > Serratia

    >
    > > Somehow I have come to think that nsIScriptableUnicodeConverter is not
    > > the correct way. After all: I have a string looking like this =E4 (or
    > > =C3=A4). The bytes behind this are specified by the Xuls charset, are
    > > they not? Anyway the above methods don't recognize that =E4 is some
    > > kind of special character. The =E4 is something MIME specific so I
    > > guess I have to search for a mimeconverter or something similar.
    > > I have been looking at MXR to find out how mime2decodedSubject is
    > > created from subject, but so far I have not found the correct package/
    > > js-file/method.

    >
    > > Hints, tipps, other plugins in this direction would be greatly
    > > appreciated.

    >
    > > Greetings,
    > > Serratia

    >
    > With some help I found a solution which worked for my problem:
    > [...]
    > * *encoded_string = cb_email_converter.unescape_chars(encoded_string);
    >
    > * * var converter = Components.classes["@mozilla.org/intl/
    > scriptableunicodeconverter"].createInstance
    > (Components.interfaces.nsIScriptableUnicodeConvert er);
    > * * converter.charset = charset;
    > * * decoded_string = converter.ConvertToUnicode(encoded_string);
    > * * return decoded_string;
    >
    > }
    >
    > /**
    > * * unescapes the characters ergo =E4 --> %E4 ( which is needed by
    > unescape) --> (its byte order according to its charset)
    > * */
    > unescape_chars = function(text){
    > * * * * text = text.replace(/=[0-9A-F]{2}/g,decode_HEX_into_DEC);
    > * * * * text = text.replace(/=\s\n/gi,"");
    > * * * * return text;
    >
    > }
    >
    > decode_HEX_into_DEC = function(encodedString) {
    > * * * *decCode = "%" + encodedString.substr(1, 2);
    > * * * *return unescape(decCode);
    >
    > }


    I like this condensed version from http://code.google.com/p/obremsdk a
    little better:
    function QPDec(s)
    {
    return s.replace(/=[\r\n]+/g, "").replace(/=[0-9A-F]{2}/gi,
    function(v){ return String.fromCharCode(parseInt
    (v.substr(1),16)); });
    }

    Also be careful about using the scriptable unicode converter outside a
    try catch block. I learned that one the hard way.

    Aron

  7. Re: TB: translating international characters

    On 20 Mrz., 12:49, Arivald wrote:
    > Red_Serra...@hotmail.de pisze:
    >
    >
    >
    > > Hello,
    > > I'm writing an extension in which I want to add the e-mails to another
    > > program.

    >
    > > I already get the whole content of the e-mail and I already extracted
    > > the body etc. Problem is that the e-mails contain code like this:
    > > "Viele Gr=FC=DFe"

    >
    > > I could write a lot of string.replace methods, but it seems over-the-
    > > top. So I have started to look for methods that automatically
    > > translate them into something readable (or at least several methods
    > > that translate UTF-8, ISO-8859-1 into one thing). TB must have
    > > something like this since *Gr=FC=DFe is correctly displayed.
    > > DisplayMessage() seems to be something, but I couldn't find any
    > > example so far. And using mozilla.org I don't understand it.

    >
    > > However so far I have not been very lucky. So reason might be my not-
    > > so-efficient searchwords ( UTF-8, thunderbird).

    >
    > > Any better searchwords or hints or links would be helpful.

    >
    > > Greetings,
    > > Serratia

    >
    > You doing something wrong.
    >
    > I suspect You get source of message, and strip headers to get Your body
    > text. But You forgot to decode it!
    > "=FC" and "=DF" are traces of quoted-printable encoding. before
    > stripping headers, You should find headers related to encoding, and
    > decode message.
    >
    > As example, try with one of base64 encoded mails... If You don't decode
    > it, You will get something like
    > "SGFsbG8gTHVrYXN6LA0KDQpoaWVyIGZlaGx0IGRpZSBNw7ZnbG" instead of text...
    >
    > TB have built in methods used to do decoding, but i not use it, so I
    > can't tell You how to.
    >
    > Bellow is some example from my library, but i personally does not use it
    > yet:
    >
    >
    >
    > > PM wrote:
    > >> Hi,

    >
    > >> I have a mail message in rfc format stored in file or I have a nsInputStream.
    > >> I need to fetch some part of message. Part is specified in Tunderbird format (i.e. part=1.2.1.7, or part1.2.1.7&type=image/jpeg&filename=1.jpg).

    >
    > >> As a result of fetch I need to get nsInputStream of spcified part.
    > >> How to achieve this by mime interfaces.

    >
    > > nsIStreamConverter:

    >
    > > var converter = Cc["@mozilla.org/streamconv;1?from=message/rfc822&to=*/*"];
    > > converter = converter.createInstance(Ci.nsIMimeStreamConverter );
    > > converter.SetMimeOutputType(5);
    > > converter.QueryInterface(Ci.nsIStreamConverter);

    >
    > > And then you use |converter| as an asynchronous stream converter, except that the source URI gets a &part=1.2.1.7 appended to it (or ?part=1.2..1.7, if necessary).

    >
    > --
    > Arivald


    I did take care of the content-transfer-encoding before I used my
    solution above. Since that one worked fine, I didn't include it in the
    above example. Basically I extract the information I need from the
    header (content-type, charset, content-transfer-encoding, boundary,
    and so on) and then work on the body according to the different
    headers. I simply needed a way to decode the charset and so far the
    emails I used my solution on worked correctly. Therefore I will keep
    it that way for now. However thank you for your answers anyway. I will
    keep these other possible solutions in mind in case mine doesn't work
    so well after all.

    @Aron Rubin: I shall heed your advice and use the try/catch-blocks.
    Thank you.

    Greetings,
    Serratia

+ Reply to Thread