Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
5.3 KiB

  1. /////////////////////////////////////////////////////////////////
  2. // (t-lleav) Windows Script Host - Checkin E-Mail Script.
  3. //
  4. // This script can be set as the editor for SourceDepot.
  5. // After editing the checkin, it will automagically send an
  6. // e-mail to the address in RECIPIENT.
  7. //
  8. // It sets the body of the e-mail as the contents of the file
  9. // that follow the DESCRIPTION tag.
  10. //
  11. // It sets the subject as the first line of the DESCRIPTION.
  12. //
  13. //
  14. // To use the script, use the following:
  15. // SET SDEDITOR=cscript edit.js (or)
  16. // SD SET -S SDEDITOR=cscript edit.js
  17. //
  18. // Constants. (change these if you want different behavior ).
  19. var SEPARATOR = "\r\n";
  20. var DESCRIPTION = "Description:";
  21. var MAILLIST = "#To:";
  22. var RECIPIENT = "sdnci";
  23. var MINLENGTH = 5;
  24. var FIRSTLINE = "# A Source Depot Change Specification."
  25. var EDITOR = "notepad";
  26. /////////////////////////////////////////////////////////////////
  27. //
  28. // Main
  29. //
  30. /////////////////////////////////////////////////////////////////
  31. var vbOKCancel = 1;
  32. var vbInformation = 64;
  33. var vbCancel = 2;
  34. var L_MsgBox_Message_Text1 = "File "
  35. var L_MsgBox_Message_Text2 = " edited. Send Checkin E-Mail?";
  36. var L_MsgBox_Title_Text = "E-mail Verification.";
  37. /////////////////////////////////////////////////////////////////
  38. //
  39. //
  40. //
  41. /////////////////////////////////////////////////////////////////
  42. var colArgs = WScript.Arguments
  43. var shell = WScript.CreateObject("WScript.Shell");
  44. if( colArgs.length < 1 )
  45. {
  46. WScript.Quit();
  47. }
  48. Edit( colArgs(0) );
  49. if( Query( colArgs(0) ) == 1)
  50. {
  51. Send( colArgs(0) );
  52. }
  53. WScript.Quit();
  54. //////////////////////////////////////////////////////////////////////////////////
  55. //
  56. // Query
  57. //
  58. /////////////////////////////////////////////////////////////////
  59. function Query( file )
  60. {
  61. var intDoIt;
  62. var retval = 1;
  63. // Open the text file.
  64. var fso = new ActiveXObject("Scripting.FileSystemObject");
  65. var a = fso.OpenTextFile( file, 1 );
  66. // read the text file.
  67. var s = a.ReadAll();
  68. // Make sure that the first line compares to the correct firstline.
  69. if( s.indexOf( FIRSTLINE ) != 0 )
  70. {
  71. retval = 0;
  72. }
  73. else
  74. {
  75. intDoIt = shell.Popup(L_MsgBox_Message_Text1 + file + L_MsgBox_Message_Text2,
  76. 0,
  77. L_MsgBox_Title_Text,
  78. vbOKCancel + vbInformation );
  79. if (intDoIt == vbCancel)
  80. {
  81. retval = 0;
  82. }
  83. }
  84. return retval;
  85. }
  86. //////////////////////////////////////////////////////////////////////////////////
  87. //
  88. // Edit
  89. //
  90. /////////////////////////////////////////////////////////////////
  91. function Edit( file )
  92. {
  93. return shell.Run( EDITOR+" "+ file , 4, true);
  94. }
  95. //////////////////////////////////////////////////////////////////////////////////
  96. //
  97. // Get the subject.
  98. //
  99. /////////////////////////////////////////////////////////////////
  100. function getSubject( txt )
  101. {
  102. var i;
  103. var j;
  104. var spl;
  105. var sbj;
  106. var s;
  107. var des = DESCRIPTION;
  108. // Find the first line > MINLENGTH for subject.
  109. // The first line begins 'DESCRIPTION'
  110. // Split the text at line breaks.
  111. spl = txt.split( SEPARATOR );
  112. // find the first line that is long enough.
  113. for( i = 0; i < spl.length; ++i )
  114. {
  115. var j = 0;
  116. if( i == 0 ) s = spl[i].substr( DESCRIPTION.length + 1 );
  117. else s = spl[i];
  118. // remove whitespace.
  119. while( s.charAt( j ) <= ' ' && j < s.length )
  120. ++j;
  121. // check length
  122. if( s.length - j > MINLENGTH )
  123. {
  124. sbj = s.substr( j );
  125. break;
  126. }
  127. }
  128. return (sbj);
  129. }
  130. //////////////////////////////////////////////////////////////////////////////////
  131. //
  132. // Get the additional mailing list.
  133. //
  134. /////////////////////////////////////////////////////////////////
  135. function getMailList( txt, mail)
  136. {
  137. var i;
  138. var j;
  139. var ml;
  140. var spl;
  141. // The first line begins 'MAILLIST'
  142. txt = txt.substr(MAILLIST.length);
  143. //Split the text at line breaks. Only ml[0] is valid
  144. ml = txt.split(SEPARATOR);
  145. //Emails can be seperated by ",", ";" or " "
  146. var re = new RegExp("[,; ]");
  147. spl = ml[0].split( re );
  148. // find the first line that is long enough.
  149. for( i = 0; i < spl.length; ++i )
  150. {
  151. if (spl[i].length)
  152. {
  153. mail.Recipients.Add(spl[i]);
  154. }
  155. }
  156. return;
  157. }
  158. //////////////////////////////////////////////////////////////////////////////////
  159. //
  160. // Send
  161. //
  162. /////////////////////////////////////////////////////////////////
  163. function Send( file )
  164. {
  165. // Open Outlook.
  166. var obj = WScript.CreateObject("Outlook.Application");
  167. var mail = obj.CreateItem( 0 );
  168. // Open the text file.
  169. var fso = new ActiveXObject("Scripting.FileSystemObject");
  170. var a = fso.OpenTextFile( file, 1 );
  171. // read the text file.
  172. var s = a.ReadAll();
  173. var l = s;
  174. var index = l.indexOf(MAILLIST);
  175. if (index != -1)
  176. {
  177. //We find the keyword MAILLIST, go ahead and add the emails to the recipient collection.
  178. l = l.substr(index);
  179. getMailList(l, mail);
  180. }
  181. // Find the second DESCRIPTION string.
  182. s = s.substr( s.indexOf( DESCRIPTION ) + DESCRIPTION.length );
  183. var txt = s.substr( s.indexOf( DESCRIPTION ) );
  184. sbj = getSubject( txt );
  185. // Send the e-mail.
  186. mail.Recipients.Add( RECIPIENT );
  187. mail.Body = txt;
  188. mail.Subject = sbj;
  189. mail.send();
  190. }