<commit_before>const { require } = Components.utils.import('resource://gre/modules/commonjs/toolkit/require.js', {});
const subprocess = require('sdk/system/child_process/subprocess');
const base64 = require('sdk/base64');
Components.utils.import('resource://gre/modules/FileUtils.jsm');

var OS = Components.classes["@mozilla.org/xre/app-info;1"]
         .getService(Components.interfaces.nsIXULRuntime).OS;
var node = (OS == "WINNT") ? "node.exe" : "node";
var nodePath = FileUtils.getFile("ProfD", ["extensions", "<EMAIL>", "components", node]);
var sendMsgPath = FileUtils.getFile("ProfD", ["extensions", "<EMAIL>", "server", "send_message.js"]);

function encodeUTF8 (str)
{
	return unescape(encodeURIComponent(str));
}

function disableWindow ()
{
	document.documentElement.setAttribute("onclose", "event.preventDefault(); event.stopPropagation();");
	document.getElementById("menu_File").disabled = true;
	document.getElementById("menu_Edit").disabled = true;
	document.getElementById("menu_View").disabled = true;
	document.getElementById("insertMenu").disabled = true;
	document.getElementById("formatMenu").disabled = true;
	document.getElementById("optionsMenu").disabled = true;
	document.getElementById("tasksMenu").disabled = true;
	document.getElementById("helpMenu").disabled = true;
	document.getElementById("button-send").disabled = true;
	document.getElementById("spellingButton").disabled = true;
	document.getElementById("button-attach").disabled = true;
	document.getElementById("button-security").disabled = true;
	document.getElementById("button-save").disabled = true;
	window.setCursor("wait");
}

function enableWindow ()
{
	document.documentElement.setAttribute("onclose", windowOCAttr);
	document.getElementById("menu_File").disabled = false;
	document.getElementById("menu_Edit").disabled = false;
	document.getElementById("menu_View").disabled = false;
	document.getElementById("insertMenu").disabled = false;
	document.getElementById("formatMenu").disabled = false;
	document.getElementById("optionsMenu").disabled = false;
	document.getElementById("tasksMenu").disabled = false;
	document.getElementById("helpMenu").disabled = false;
	document.getElementById("button-send").disabled = false;
	document.getElementById("spellingButton").disabled = false;
	document.getElementById("button-attach").disabled = false;
	document.getElementById("button-security").disabled = false;
	document.getElementById("button-save").disabled = false;
	window.setCursor("auto");
}

function composeAndSendMessage (server, deliveryMode)
{
	disableWindow();

	var URL = server.getCharValue("ewsURL");
	var username = server.username;
	var password;
	var authType = server.getCharValue("authType");
	var TLS = server.getCharValue("TLS");

	var passwordManager = Components.classes["@mozilla.org/login-manager;1"]
                          .getService(Components.interfaces.nsILoginManager);

    var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
    	                                         Components.interfaces.nsILoginInfo,
    	                                         "init");

    var logins = passwordManager.findLogins({}, 'chrome://mexint', null, 'User Registration');
      
	for ( var i = 0; i < logins.length; i++ )
	{
		if ( logins[i].username == username )
		{
			password = logins[i].password;
			break;
		}
	}

	gMsgCompose.editor.QueryInterface(Components.interfaces.nsIEditorMailSupport);
	gMsgCompose.SetDocumentCharset("utf-8");

	var msgBody = gMsgCompose.editor.outputToString("text/html", 0);
	var attachEnum = gMsgCompose.compFields.attachments;
	var attachments = [];
	var objects = gMsgCompose.editor.getEmbeddedObjects();

	while ( attachEnum.hasMoreElements() )
	{
		var attach = attachEnum.getNext().QueryInterface(Components.interfaces.nsIMsgAttachment);
		attachments.push(attach)
	}

	var gSendListener = {
		// nsIMsgSendListener
		onStartSending: function (aMsgID, aMsgSize) {},
		onProgress: function (aMsgID, aProgress, aProgressMax) {},
		onStatus: function (aMsgID, aMsg) {},

		onStopSending: function (aMsgID, aStatus, aMsg, aReturnFile) {
			var istream = Components.classes["@mozilla.org/network/file-input-stream;1"]
			              .createInstance(Components.interfaces.nsIFileInputStream);
			istream.init(aReturnFile, -1, -1, false);
			var bstream = Components.classes["@mozilla.org/binaryinputstream;1"]
			              .createInstance(Components.interfaces.nsIBinaryInputStream);
			bstream.setInputStream(istream);
			var bytes = bstream.readBytes(bstream.available());
			var mimeContent_base64 = btoa(bytes);
			istream.close();
			bstream.close();

			try
			{
				aReturnFile.remove(false);
			}
			catch (ex) {}

			// we need to pass bcc recipients separately, because this field doesn't belong to MIME content
			var bccRecipients = gMsgCompose.compFields.bcc.replace(/\s/g, "");

			if ( deliveryMode == nsIMsgCompDeliverMode.Now )
			{
				// do nothing
			}
			else if ( deliveryMode == nsIMsgCompDeliverMode.Later )
			{
				var d = new Date().toString().split(' ');
                var weekday = d[0];
                var month = d[1];
                var day = d[2];
                var year = d[3];
                var time = d[4];
                var date = weekday + " " + month + " " + day + " " + time + " " + year;

				var messageSource = "From - " + date + "\r\n" +
                                    "X-Mozilla-Status: 0000" + "\r\n" +
                                    "X-Mozilla-Status2: 00000000\r\n" +
                                    bytes;

                var outboxFolder = server.rootFolder.getFolderWithFlags(Components.interfaces.nsMsgFolderFlags.Queue);
				outboxFolder.QueryInterface(Components.interfaces.nsIMsgLocalMailFolder);
				var message = outboxFolder.addMessage(messageSource);
				message.setProperty("local", "true");
				message.setProperty("mime_base64", mimeContent_base64);
				message.setProperty("bcc", bccRecipients);

				enableWindow();
				window.close();
				return;
			}
			else if ( deliveryMode == nsIMsgCompDeliverMode.SaveAsDraft || deliveryMode == nsIMsgCompDeliverMode.AutoSaveAsDraft )
			{
				// TODO
				return;
			}
			else
			{
				return;
			}

			var exitCode;
			var stdout;
			var stderr;

			let authData_base64 = base64.encode(URL      + '\n' +
		                                        username + '\n' + 
		                                        password + '\n' +
		                                        authType + '\n' +
		                                        TLS,<commit_msg>bug fix<commit_after>const { require } = Components.utils.import('resource://gre/modules/commonjs/toolkit/require.js', {});
const subprocess = require('sdk/system/child_process/subprocess');
const base64 = require('sdk/base64');
Components.utils.import('resource://gre/modules/FileUtils.jsm');

var OS = Components.classes["@mozilla.org/xre/app-info;1"]
         .getService(Components.interfaces.nsIXULRuntime).OS;
var node = (OS == "WINNT") ? "node.exe" : "node";
var nodePath = FileUtils.getFile("ProfD", ["extensions", "<EMAIL>", "components", node]);
var sendMsgPath = FileUtils.getFile("ProfD", ["extensions", "<EMAIL>", "server", "send_message.js"]);

function encodeUTF8 (str)
{
	return unescape(encodeURIComponent(str));
}

function disableWindow ()
{
	document.documentElement.setAttribute("onclose", "event.preventDefault(); event.stopPropagation();");
	document.getElementById("menu_File").disabled = true;
	document.getElementById("menu_Edit").disabled = true;
	document.getElementById("menu_View").disabled = true;
	document.getElementById("insertMenu").disabled = true;
	document.getElementById("formatMenu").disabled = true;
	document.getElementById("optionsMenu").disabled = true;
	document.getElementById("tasksMenu").disabled = true;
	document.getElementById("helpMenu").disabled = true;
	document.getElementById("button-send").disabled = true;
	document.getElementById("spellingButton").disabled = true;
	document.getElementById("button-attach").disabled = true;
	document.getElementById("button-security").disabled = true;
	document.getElementById("button-save").disabled = true;
	window.setCursor("wait");
}

function enableWindow ()
{
	document.documentElement.setAttribute("onclose", windowOCAttr);
	document.getElementById("menu_File").disabled = false;
	document.getElementById("menu_Edit").disabled = false;
	document.getElementById("menu_View").disabled = false;
	document.getElementById("insertMenu").disabled = false;
	document.getElementById("formatMenu").disabled = false;
	document.getElementById("optionsMenu").disabled = false;
	document.getElementById("tasksMenu").disabled = false;
	document.getElementById("helpMenu").disabled = false;
	document.getElementById("button-send").disabled = false;
	document.getElementById("spellingButton").disabled = false;
	document.getElementById("button-attach").disabled = false;
	document.getElementById("button-security").disabled = false;
	document.getElementById("button-save").disabled = false;
	window.setCursor("auto");
}

function composeAndSendMessage (server, deliveryMode)
{
	disableWindow();

	var URL = server.getCharValue("ewsURL");
	var username = server.username;
	var password;
	var authType = server.getCharValue("authType");
	var TLS = server.getCharValue("TLS");

	var passwordManager = Components.classes["@mozilla.org/login-manager;1"]
                          .getService(Components.interfaces.nsILoginManager);

    var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
    	                                         Components.interfaces.nsILoginInfo,
    	                                         "init");

    var logins = passwordManager.findLogins({}, 'chrome://mexint', null, 'User Registration');
      
	for ( var i = 0; i < logins.length; i++ )
	{
		if ( logins[i].username == username )
		{
			password = logins[i].password;
			break;
		}
	}

	gMsgCompose.editor.QueryInterface(Components.interfaces.nsIEditorMailSupport);
	gMsgCompose.SetDocumentCharset("utf-8");

	var msgBody = gMsgCompose.editor.outputToString("text/html", 0);
	var attachEnum = gMsgCompose.compFields.attachments;
	var attachments = [];
	var objects = gMsgCompose.editor.getEmbeddedObjects();

	while ( attachEnum.hasMoreElements() )
	{
		var attach = attachEnum.getNext().QueryInterface(Components.interfaces.nsIMsgAttachment);
		attachments.push(attach)
	}

	var gSendListener = {
		// nsIMsgSendListener
		onStartSending: function (aMsgID, aMsgSize) {},
		onProgress: function (aMsgID, aProgress, aProgressMax) {},
		onStatus: function (aMsgID, aMsg) {},

		onStopSending: function (aMsgID, aStatus, aMsg, aReturnFile) {
			var istream = Components.classes["@mozilla.org/network/file-input-stream;1"]
			              .createInstance(Components.interfaces.nsIFileInputStream);
			istream.init(aReturnFile, -1, -1, false);
			var bstream = Components.classes["@mozilla.org/binaryinputstream;1"]
			              .createInstance(Components.interfaces.nsIBinaryInputStream);
			bstream.setInputStream(istream);
			var bytes = bstream.readBytes(bstream.available());
			var mimeContent_base64 = btoa(bytes);
			istream.close();
			bstream.close();

			try
			{
				aReturnFile.remove(false);
			}
			catch (ex) {}

			// we need to pass bcc recipients separately, because this field doesn't belong to MIME content
			var bccRecipients = gMsgCompose.compFields.bcc.replace(/\s/g, "");

			if ( deliveryMode == nsIMsgCompDeliverMode.Now )
			{
				// do nothing
			}
			else if ( deliveryMode == nsIMsgCompDeliverMode.Later )
			{
				var d = new Date().toString().split(' ');
                var weekday = d[0];
                var month = d[1];
                var day = d[2];
                var year = d[3];
                var time = d[4];
                var date = weekday + " " + month + " " + day + " " + time + " " + year;

				var messageSource = "From - " + date + "\r\n" +
                                    "X-Mozilla-Status: 0000" + "\r\n" +
                                    "X-Mozilla-Status2: 00000000\r\n" +
                                    bytes;

                var outboxFolder = server.rootFolder.getFolderWithFlags(Components.interfaces.nsMsgFolderFlags.Queue);
				outboxFolder.QueryInterface(Components.interfaces.nsIMsgLocalMailFolder);
				var message = outboxFolder.addMessage(messageSource);
				message.setProperty("local", "true");
				message.setProperty("mime_base64", mimeContent_base64);
				message.setProperty("bcc", bccRecipients);

				enableWindow();
				window.close();
				return;
			}
			else if ( deliveryMode == nsIMsgCompDeliverMode.SaveAsDraft || deliveryMode == nsIMsgCompDeliverMode.AutoSaveAsDraft )
			{
				// TODO
				alert("Not implemented yet");
				enableWindow();
				return;
			}

			var exitCode;
			var stdout;
			var stderr;

			let authData_base64 = base64.encode(URL      + '\n' +
		                                        username + '\n' + 
		                                        password + '\n' +
		                                        authType + '\n' +
		                                        TLS,
{'fraction_non_alphanumeric': 0.13611491108071136, 'fraction_numerical': 0.006535947712418301, 'mean_word_length': 3.3357495881383854, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3870632', 'n_tokens_mistral': 4111, 'n_tokens_neox': 3727, 'n_words': 652}