PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/content/Dialogs/Assistant/4-gmail.js

https://github.com/SvenDowideit/fireGPG
JavaScript | 203 lines | 87 code | 46 blank | 70 comment | 11 complexity | 41afd7df07b1fbc3a5273bdf820476e0 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*
  2. ***** BEGIN LICENSE BLOCK *****
  3. Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. The contents of this source code are subject to the Mozilla Public License
  5. Version 1.1 (the "License"); you may not use this source code except in
  6. compliance with the License. You may obtain a copy of the License at
  7. http://www.mozilla.org/MPL/
  8. Software distributed under the License is distributed on an "AS IS" basis,
  9. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10. for the specific language governing rights and limitations under the
  11. License.
  12. The Original Code is the FireGPG extension.
  13. The Initial Developer of the Original Code is Maximilien Cuony.
  14. Portions created by the Initial Developer are Copyright (C) 2007
  15. the Initial Developer. All Rights Reserved.
  16. Contributor(s):
  17. Alternatively, the contents of this source code may be used under the terms of
  18. either the GNU General Public License Version 2 or later (the "GPL"), or
  19. the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  20. in which case the provisions of the GPL or the LGPL are applicable instead
  21. of those above. If you wish to allow use of your version of this source code
  22. only under the terms of either the GPL or the LGPL, and not to allow others to
  23. use your version of this source code under the terms of the MPL, indicate your
  24. decision by deleting the provisions above and replace them with the notice
  25. and other provisions required by the GPL or the LGPL. If you do not delete
  26. the provisions above, a recipient may use your version of this source code
  27. under the terms of any one of the MPL, the GPL or the LGPL.
  28. ***** END LICENSE BLOCK *****
  29. */
  30. /*
  31. Variable: gmailOk
  32. True if gmail is working
  33. */
  34. var gmailOk = false;
  35. /*
  36. Function: onLoad
  37. This function is called when the form is show.
  38. Parameters:
  39. win - The form herself.
  40. */
  41. function onLoad(win) {
  42. testGmailOn();
  43. }
  44. /*
  45. Function: next
  46. Process to the next step of the assistant
  47. */
  48. function next() {
  49. this.close();
  50. if (!gmailOk)
  51. var assis = window.openDialog('chrome://firegpg/content/Dialogs/Assistant/4-gmail.xul','', 'chrome, dialog, resizable=false');
  52. else
  53. var assis = window.openDialog('chrome://firegpg/content/Dialogs/Assistant/5-options.xul','', 'chrome, dialog, resizable=false');
  54. assis.focus();
  55. }
  56. /*
  57. Function: switchedEnabled
  58. Switch on or off gmail
  59. */
  60. function switchedEnabled() {
  61. var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  62. getService(Components.interfaces.nsIPrefService);
  63. prefs = prefs.getBranch("extensions.firegpg.");
  64. prefs.setBoolPref("gmail_enabled",document.getElementById('gmail-enabled').checked);
  65. testGmailOn();
  66. }
  67. /*
  68. Function: testGmailOn
  69. Test if gmail is activated and working, and change the interface
  70. */
  71. function testGmailOn() {
  72. document.getElementById('gmail-turned-on').style.display = 'none';
  73. document.getElementById('smtp-working').style.display = 'none';
  74. document.getElementById('smtp-not-working').style.display = 'none';
  75. var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  76. getService(Components.interfaces.nsIPrefService);
  77. prefs = prefs.getBranch("extensions.firegpg.");
  78. if (prefs.getBoolPref("gmail_enabled",true)) {
  79. document.getElementById('gmail-enabled').checked = true;
  80. document.getElementById('gmail-turned-on').style.display = '';
  81. testSmtp();
  82. } else {
  83. gmailOk = true;
  84. document.getElementById('gmail-enabled').checked = false;
  85. }
  86. }
  87. /*
  88. Function: testSmtp
  89. Test is the current smtp server set in options is working. Change the interface
  90. */
  91. function testSmtp() {
  92. var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  93. getService(Components.interfaces.nsIPrefService);
  94. prefs = prefs.getBranch("extensions.firegpg.");
  95. if (prefs.getBoolPref("gmail_use_ssl",true))
  96. smtpSocketTypes = new Array("ssl")
  97. else
  98. smtpSocketTypes = null;
  99. transportService = Components
  100. .classes["@mozilla.org/network/socket-transport-service;1"]
  101. .getService(Components.interfaces.nsISocketTransportService);
  102. transport = transportService
  103. .createTransport(smtpSocketTypes, smtpSocketTypes ? smtpSocketTypes.length : 0, prefs.getCharPref("gmail_host"), parseInt( prefs.getCharPref("gmail_port")), null);
  104. outstream = transport.openOutputStream(transport.OPEN_BLOCKING,0,0);
  105. instream = transport.openInputStream(0,0,0);
  106. sriptInstream = Components
  107. .classes["@mozilla.org/scriptableinputstream;1"]
  108. .createInstance(Components.interfaces.nsIScriptableInputStream);
  109. sriptInstream.init(instream);
  110. dataListener = {
  111. onStartRequest: function(request, context) {
  112. if (outstream)
  113. outstream.write("HELO 127.0.0.1","HELO 127.0.0.1".length);
  114. },
  115. onStopRequest: function(request, context, status) {
  116. if (outstream)
  117. outstream.close();
  118. if (sriptInstream)
  119. sriptInstream.close();
  120. },
  121. onDataAvailable: function(request, context, inputStream, offset, count) {
  122. retour = sriptInstream.read(count);
  123. retour = retour.split(/ /gi);
  124. if (retour[0] == "220") {
  125. gmailOk = true;
  126. document.getElementById('smtp-working').style.display = '';
  127. document.getElementById('smtp-not-working').style.display = 'none';
  128. }
  129. if (outstream)
  130. outstream.close()
  131. if (sriptInstream)
  132. sriptInstream.close()
  133. }
  134. };
  135. pump = Components
  136. .classes["@mozilla.org/network/input-stream-pump;1"]
  137. .createInstance(Components.interfaces.nsIInputStreamPump);
  138. pump.init(instream, -1, -1, 0, 0, false);
  139. pump.asyncRead(dataListener, null);
  140. gmailOk = false;
  141. document.getElementById('smtp-not-working').style.display = '';
  142. }