MozillaZine

Saving email to disk on TB 60.8

Talk about add-ons and extension development.
ManOfOrange
 
Posts: 5
Joined: January 23rd, 2019, 12:31 am

Post Posted August 12th, 2019, 7:50 am

Hello all!

I have a ThunderBird extention that saving email to disk. It using function SaveMessageToDisk and before all was OK.
As i remember on version TB 60.4 it worked fine, but on TB 60.8 all incoming letters automatically marked as read by this function.

I try to write saving to disk by myself. I try example from here: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIMsgMessageService#streamMessage().
Code: Select all
var content = "";
  var MessageURI = GetFirstSelectedMessage();
  var MsgService = messenger.messageServiceFromURI(MessageURI);
  var MsgStream =  Components.classes["@mozilla.org/network/sync-stream-listener;1"].createInstance();
  var consumer = MsgStream.QueryInterface(Components.interfaces.nsIInputStream);
  var ScriptInput = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance();
  var ScriptInputStream = ScriptInput.QueryInterface(Components.interfaces.nsIScriptableInputStream);
  ScriptInputStream.init(consumer);
  try {
    MsgService.streamMessage(MessageURI, MsgStream, msgWindow, null, false, null);
  } catch (ex) {
    alert("error: "+ex)
  }
  ScriptInputStream.available();
  while (ScriptInputStream.available()) {
    content = content + ScriptInputStream.read(512);
  }


But on line «ScriptInputStream.available()» it is stopped and no execute after tis line. Sometimes after about ten minutes TB crushed.
Just for test i tryed use only «ScriptInputStream.read(512)» — same result.

Then i found topic: http://forums.mozillazine.org/viewtopic.php?f=19&p=14756968 and use example with function getMsgTextFromStream:
Code: Select all
var msgHdr = gFolderDisplay.selectedMessage;
var msgUri = msgHdr.folder.getUriForMsg(msgHdr);
var messenger = Components.classes["@mozilla.org/messenger;1"].
  createInstance(Components.interfaces.nsIMessenger);
var msgService = messenger.messageServiceFromURI(msgUri);
var syncStreamListener = Components.classes["@mozilla.org/network/sync-stream-listener;1"].
  createInstance(Components.interfaces.nsISyncStreamListener);
msgService.streamMessage(msgUri, syncStreamListener, null, null, false, "", true);
var contentType = new Object();
var data = msgHdr.folder.getMsgTextFromStream(
  syncStreamListener.inputStream, msgHdr.Charset, 65536, 32768, false, true, contentType);

And again — on line with getMsgTextFromStream function it stopped and sometimes crashed after few minutes.

So, i have two questions:
1) Is there any way to use the function SaveMessageToDisk so that it doesn't mark emails as read?
2) If not — how to make the way with the functions streamMessage or getMsgTextFromStream work?

tanstaafl
Moderator

User avatar
 
Posts: 45869
Joined: July 30th, 2003, 5:06 pm

Post Posted August 26th, 2019, 5:23 pm

Thunderbird 68.0 will be out soon. Right now its undergoing smoke testing. They will try to retain support for legacy add-ons (though you will need to make some changes) but its very webextension-centric. I suggest you look at the source code for "ImportExportTools NG", the webextension based replacement for the ImportExportTools add-on. That add-on imports/exports mail to mbox files.

https://github.com/thundernest/import-export-tools-ng

I suggest you read https://developer.thunderbird.net/add-ons/updates and then https://developer.thunderbird.net/ .

ManOfOrange
 
Posts: 5
Joined: January 23rd, 2019, 12:31 am

Post Posted September 24th, 2019, 4:28 am

tanstaafl wrote:Thunderbird 68.0 will be out soon. Right now its undergoing smoke testing. They will try to retain support for legacy add-ons (though you will need to make some changes) but its very webextension-centric.


Thank you for answer!

I waited for the version 68.1 to come out and check it.
Well, function SaveMessageToDisk still mark mails as read.

I tried again MsgService.streamMessage (like in code example above) and now it is not crashed. It is not answering for 20-30 minutes and after that — it give me the data i need!
Of course, that's not an option.

tanstaafl wrote:I suggest you look at the source code for "ImportExportTools NG", the webextension based replacement for the ImportExportTools add-on. That add-on imports/exports mail to mbox files.

https://github.com/thundernest/import-export-tools-ng

I suggest you read https://developer.thunderbird.net/add-ons/updates and then https://developer.thunderbird.net/ .


Thank you again! I will look at the sources and links which you suggest and try it. I will write the results later.

ManOfOrange
 
Posts: 5
Joined: January 23rd, 2019, 12:31 am

Post Posted September 30th, 2019, 5:51 am

tanstaafl wrote:I suggest you look at the source code for "ImportExportTools NG", the webextension based replacement for the ImportExportTools add-on. That add-on imports/exports mail to mbox files.

https://github.com/thundernest/import-export-tools-ng



I found the solution in those extension.

If someone interested there is the code, that working for my purpose:
Code: Select all
var myEMLlistner = {
   scriptStream: null,
   emailtext: "",

   QueryInterface: function (iid) {
      if (iid.equals(Ci.nsIStreamListener) ||
         iid.equals(Ci.nsISupports))
         return this;

      throw Cr.NS_NOINTERFACE;
   },

   onStartRequest: function (aRequest) { },

   onStopRequest: function (aRequest, aStatusCode) {
      var data;

      this.scriptStream = null;
      
      if (this.emailtext !== "")
         data = this.emailtext + "\n";

      var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
                  createInstance(Components.interfaces.nsIFileOutputStream);
      foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
      if (data)
         foStream.write(data, data.length);
      foStream.close();
   },

   onDataAvailable: function (aRequest, aInputStream, aOffset, aCount) {
      var scriptStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
      scriptStream.init(aInputStream);
      this.emailtext += scriptStream.read(scriptStream.available());
   },
};

var mms = messenger.messageServiceFromURI(uri)
   .QueryInterface(Ci.nsIMsgMessageService);
var hdr = mms.messageURIToMsgHdr(uri);
mms.streamMessage(uri, myEMLlistner, msgWindow, null, false, null);


tanstaafl, thank you again!

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 2 guests