lmimewrapper.pp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. { lNet MIME Wrapper
  2. CopyRight (C) 2007-2008 by Ales Katona
  3. This library is Free software; you can rediStribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at your
  6. option) any later version.
  7. This program is diStributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  10. for more details.
  11. You should have received a Copy of the GNU Library General Public License
  12. along with This library; if not, Write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. This license has been modified. See File LICENSE.ADDON for more inFormation.
  15. Should you find these sources without a LICENSE File, please contact
  16. me at [email protected]
  17. }
  18. unit lMimeWrapper;
  19. {$mode objfpc}{$H+}
  20. interface
  21. uses
  22. SysUtils, Classes, Contnrs, lMimeStreams;
  23. const
  24. MIME_VERSION = 'MIME-version: 1.0' + CRLF;
  25. type
  26. TMimeEncoding = (me8bit, meBase64);
  27. TMimeDisposition = (mdInline, mdAttachment);
  28. { TMimeSection }
  29. TMimeSection = class
  30. protected
  31. FContentType: string;
  32. FEncoding: TMimeEncoding;
  33. FActivated: Boolean;
  34. FDescription: string;
  35. FDisposition: TMimeDisposition;
  36. FBuffer: string;
  37. FEncodingStream: TStream;
  38. FOutputStream: TStream;
  39. FLocalStream: TBogusStream;
  40. function RecalculateSize(const OriginalSize: Integer): Integer;
  41. function GetSize: Integer; virtual; abstract;
  42. procedure SetDescription(const AValue: string);
  43. procedure SetDisposition(const AValue: TMimeDisposition);
  44. procedure SetEncoding(const AValue: TMimeEncoding);
  45. procedure CreateEncodingStream; virtual;
  46. function GetHeader: string; virtual;
  47. function ReadBuffer(const aSize: Integer): string;
  48. procedure FillBuffer(const aSize: Integer); virtual; abstract;
  49. public
  50. constructor Create(aOutputStream: TStream);
  51. destructor Destroy; override;
  52. function Read(const aSize: Integer): Integer;
  53. procedure Reset; virtual;
  54. public
  55. property ContentType: string read FContentType write FContentType;
  56. property Encoding: TMimeEncoding read FEncoding write SetEncoding;
  57. property Disposition: TMimeDisposition read FDisposition write SetDisposition;
  58. property Description: string read FDescription write SetDescription;
  59. property Header: string read GetHeader;
  60. property Size: Integer read GetSize;
  61. end;
  62. { TMimeTextSection }
  63. TMimeTextSection = class(TMimeSection)
  64. protected
  65. FOriginalData: string;
  66. FData: string;
  67. function GetSize: Integer; override;
  68. procedure SetData(const AValue: string);
  69. function GetCharset: string;
  70. procedure SetCharset(const AValue: string);
  71. procedure FillBuffer(const aSize: Integer); override;
  72. public
  73. constructor Create(aOutputStream: TStream; const aText: string);
  74. procedure Reset; override;
  75. public
  76. property Charset: string read GetCharset write SetCharset;
  77. property Text: string read FData write SetData;
  78. end;
  79. { TMimeStreamSection }
  80. TMimeStreamSection = class(TMimeSection)
  81. protected
  82. FStream: TStream;
  83. FOwnsStreams: Boolean;
  84. FOriginalPosition: Int64;
  85. function GetSize: Integer; override;
  86. procedure SetStream(aValue: TStream);
  87. procedure FillBuffer(const aSize: Integer); override;
  88. public
  89. constructor Create(aOutputStream: TStream; aStream: TStream);
  90. destructor Destroy; override;
  91. procedure Reset; override;
  92. public
  93. property Stream: TStream read FStream write SetStream;
  94. property OwnsStreams: Boolean read FOwnsStreams write FOwnsStreams;
  95. end;
  96. { TMimeFileSection }
  97. TMimeFileSection = class(TMimeStreamSection)
  98. protected
  99. FFileName: string;
  100. procedure SetFileName(const AValue: string);
  101. procedure SetContentType(const aFileName: string);
  102. function GetHeader: string; override;
  103. public
  104. constructor Create(aOutputStream: TStream; const aFileName: string);
  105. property FileName: string read FFileName write SetFileName;
  106. end;
  107. { TMimeStream }
  108. TMimeStream = class(TStream)
  109. protected
  110. FSections: TFPObjectList;
  111. FOutputStream: TMimeOutputStream;
  112. FBoundary: string;
  113. FActiveSection: Integer;
  114. FCalledRead: Boolean;
  115. FCalledWrite: Boolean;
  116. function GetBoundarySize: Integer;
  117. function GetSize: Int64; override;
  118. function GetCount: Integer;
  119. function GetBoundary: string;
  120. function GetSection(i: Integer): TMimeSection;
  121. function GetMimeHeader: string;
  122. procedure SetSection(i: Integer; const AValue: TMimeSection);
  123. procedure ActivateFirstSection;
  124. procedure ActivateNextSection;
  125. procedure DoRead(const aSize: Integer);
  126. public
  127. constructor Create;
  128. destructor Destroy; override;
  129. function Seek(Offset: Longint; Origin: Word): Longint; overload; override;
  130. function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload; override;
  131. function Read(var Buffer; Count: Longint): Longint; override;
  132. function Write(const Buffer; Count: Longint): Longint; override;
  133. procedure AddTextSection(const aText: string; const aCharSet: string = 'UTF-8');
  134. procedure AddFileSection(const aFileName: string);
  135. procedure AddStreamSection(aStream: TStream; const FreeStream: Boolean = False);
  136. procedure Delete(const i: Integer);
  137. procedure Remove(aSection: TMimeSection);
  138. procedure Reset;
  139. public
  140. property Sections[i: Integer]: TMimeSection read GetSection write SetSection; default;
  141. property Count: Integer read GetCount;
  142. property Boundary: string read FBoundary;
  143. end;
  144. { EAlreadyActivatedException }
  145. EAlreadyActivatedException = class(Exception)
  146. public
  147. constructor Create;
  148. end;
  149. { EAlreadyCalledReadException }
  150. EAlreadyCalledReadException = class(Exception)
  151. public
  152. constructor Create;
  153. end;
  154. { EAlreadyCalledWriteException }
  155. EAlreadyCalledWriteException = class(Exception)
  156. public
  157. constructor Create;
  158. end;
  159. implementation
  160. uses
  161. Math, Base64;
  162. function EncodingToStr(const Encoding: TMimeEncoding): string;
  163. begin
  164. Result := '';
  165. case Encoding of
  166. me8bit : Result := '8bit';
  167. meBase64 : Result := 'base64';
  168. end;
  169. end;
  170. function DispositionToStr(const Disposition: TMimeDisposition): string;
  171. begin
  172. Result := '';
  173. case Disposition of
  174. mdInline : Result := 'inline';
  175. mdAttachment : Result := 'attachment';
  176. end;
  177. end;
  178. { TMimeSection }
  179. function TMimeSection.RecalculateSize(const OriginalSize: Integer): Integer;
  180. begin
  181. Result := 0;
  182. if OriginalSize = 0 then
  183. Exit;
  184. case FEncoding of
  185. me8bit : Result := OriginalSize;
  186. meBase64 : if OriginalSize mod 3 = 0 then
  187. Result := (OriginalSize div 3) * 4 // this is simple, 4 bytes per 3 bytes
  188. else
  189. Result := ((OriginalSize + 3) div 3) * 4; // add "padding" trupplet
  190. end;
  191. end;
  192. procedure TMimeSection.SetDescription(const AValue: string);
  193. begin
  194. if not FActivated then
  195. FDescription := AValue;
  196. end;
  197. procedure TMimeSection.SetDisposition(const AValue: TMimeDisposition);
  198. begin
  199. if not FActivated then
  200. FDisposition := AValue;
  201. end;
  202. procedure TMimeSection.SetEncoding(const AValue: TMimeEncoding);
  203. begin
  204. if not FActivated then begin
  205. FEncoding := aValue;
  206. if Assigned(FEncodingStream) then
  207. FEncodingStream.Free;
  208. CreateEncodingStream;
  209. end;
  210. end;
  211. procedure TMimeSection.CreateEncodingStream;
  212. begin
  213. case FEncoding of
  214. me8bit : FEncodingStream := nil;
  215. meBase64 : FEncodingStream := TBase64EncodingStream.Create(FLocalStream);
  216. end;
  217. end;
  218. function TMimeSection.GetHeader: string;
  219. begin
  220. Result := 'Content-Type: ' + FContentType + CRLF;
  221. Result := Result + 'Content-Transfer-Encoding: ' + EncodingToStr(FEncoding) + CRLF;
  222. Result := Result + 'Content-Disposition: ' + DispositionToStr(FDisposition) + CRLF;
  223. if Length(FDescription) > 0 then
  224. Result := Result + 'Content-Description: ' + FDescription + CRLF;
  225. Result := Result + CRLF;
  226. end;
  227. function TMimeSection.ReadBuffer(const aSize: Integer): string;
  228. begin
  229. Result := '';
  230. if aSize >= Length(FBuffer) then
  231. FillBuffer(aSize);
  232. Result := Copy(FBuffer, 1, aSize);
  233. end;
  234. constructor TMimeSection.Create(aOutputStream: TStream);
  235. begin
  236. FOutputStream := aOutputStream;
  237. FEncodingStream := nil;
  238. FLocalStream := TBogusStream.Create;
  239. end;
  240. destructor TMimeSection.Destroy;
  241. begin
  242. if Assigned(FEncodingStream) then
  243. FEncodingStream.Free;
  244. FLocalStream.Free;
  245. inherited Destroy;
  246. end;
  247. function TMimeSection.Read(const aSize: Integer): Integer;
  248. var
  249. s: string;
  250. begin
  251. Result := 0;
  252. if aSize <= 0 then
  253. Exit;
  254. if not FActivated then begin
  255. FActivated := True;
  256. FBuffer := GetHeader;
  257. end;
  258. if Length(FBuffer) < aSize then
  259. FillBuffer(aSize);
  260. s := ReadBuffer(aSize);
  261. if Length(s) >= aSize then begin
  262. Result := FOutputStream.Write(s[1], aSize);
  263. Delete(FBuffer, 1, Result);
  264. end else if Length(s) > 0 then begin
  265. Result := FOutputStream.Write(s[1], Length(s));
  266. Delete(FBuffer, 1, Result);
  267. end;
  268. end;
  269. procedure TMimeSection.Reset;
  270. begin
  271. FActivated := False;
  272. FBuffer := '';
  273. FLocalStream.Reset;
  274. SetEncoding(FEncoding);
  275. end;
  276. { TMimeTextSection }
  277. procedure TMimeTextSection.SetCharset(const aValue: string);
  278. begin
  279. if not FActivated then begin
  280. if Length(aValue) > 0 then
  281. FContentType := 'text/plain; charset="' + aValue + '"'
  282. else
  283. FContentType := 'text/plain';
  284. end;
  285. end;
  286. procedure TMimeTextSection.FillBuffer(const aSize: Integer);
  287. var
  288. s: string;
  289. n: Integer;
  290. begin
  291. s := Copy(FData, 1, aSize);
  292. if Length(s) = 0 then
  293. Exit;
  294. n := aSize;
  295. if Assigned(FEncodingStream) then begin
  296. n := FEncodingStream.Write(s[1], Length(s));
  297. Delete(FData, 1, n);
  298. if Length(FData) = 0 then begin
  299. FEncodingStream.Free; // to fill in the last bit
  300. CreateEncodingStream;
  301. FLocalStream.Write(CRLF[1], Length(CRLF));
  302. end;
  303. SetLength(s, FLocalStream.Size);
  304. SetLength(s, FLocalStream.Read(s[1], Length(s)));
  305. end else begin
  306. Delete(FData, 1, n);
  307. if Length(FData) = 0 then
  308. s := s + CRLF;
  309. end;
  310. FBuffer := FBuffer + s;
  311. end;
  312. function TMimeTextSection.GetSize: Integer;
  313. begin
  314. if FActivated then
  315. Result := Length(FBuffer) + RecalculateSize(Length(FData))
  316. else
  317. Result := Length(FBuffer) + Length(GetHeader) + RecalculateSize(Length(FData));
  318. if not FActivated
  319. or (Length(FBuffer) > 0)
  320. or (Length(FData) > 0) then
  321. if Length(FOriginalData) > 0 then
  322. Result := Result + Length(CRLF); // CRLF after each msg body
  323. end;
  324. procedure TMimeTextSection.SetData(const AValue: string);
  325. begin
  326. if not FActivated then begin
  327. FOriginalData := aValue;
  328. FData := aValue;
  329. end;
  330. end;
  331. function TMimeTextSection.GetCharset: string;
  332. var
  333. n: Integer;
  334. begin
  335. Result := '';
  336. n := Pos('=', FContentType);
  337. if n > 0 then
  338. Result := StringReplace(Copy(FContentType, n + 1, Length(FContentType)),
  339. '"', '', [rfReplaceAll]);
  340. end;
  341. constructor TMimeTextSection.Create(aOutputStream: TStream; const aText: string);
  342. begin
  343. inherited Create(aOutputStream);
  344. FContentType := 'text/plain; charset="UTF-8"';
  345. FOriginalData := aText;
  346. FData := FOriginalData;
  347. end;
  348. procedure TMimeTextSection.Reset;
  349. begin
  350. inherited Reset;
  351. FData := FOriginalData;
  352. end;
  353. { TMimeStreamSection }
  354. function TMimeStreamSection.GetSize: Integer;
  355. begin
  356. if FActivated then
  357. Result := Length(FBuffer) + RecalculateSize(FStream.Size - FStream.Position)
  358. else
  359. Result := Length(FBuffer) + Length(GetHeader) + RecalculateSize(FStream.Size - FStream.Position);
  360. if not FActivated
  361. or (Length(FBuffer) > 0)
  362. or (FStream.Size - FStream.Position > 0) then
  363. if FStream.Size - FOriginalPosition > 0 then
  364. Result := Result + Length(CRLF); // CRLF after each msg body
  365. end;
  366. procedure TMimeStreamSection.SetStream(aValue: TStream);
  367. begin
  368. if Assigned(FStream)
  369. and FOwnsStreams then begin
  370. FStream.Free;
  371. FStream := nil;
  372. end;
  373. FStream := aValue;
  374. FOriginalPosition := FStream.Position;
  375. end;
  376. procedure TMimeStreamSection.FillBuffer(const aSize: Integer);
  377. var
  378. s: string;
  379. n: Integer;
  380. begin
  381. SetLength(s, aSize);
  382. SetLength(s, FStream.Read(s[1], aSize));
  383. if Length(s) <= 0 then
  384. Exit;
  385. if Assigned(FEncodingStream) then begin
  386. n := FEncodingStream.Write(s[1], Length(s));
  387. if n < Length(s) then
  388. FStream.Position := FStream.Position - (n - Length(s));
  389. if FStream.Size - FStream.Position = 0 then begin
  390. FEncodingStream.Free; // to fill in the last bit
  391. CreateEncodingStream;
  392. FLocalStream.Write(CRLF[1], Length(CRLF));
  393. end;
  394. SetLength(s, FLocalStream.Size);
  395. SetLength(s, FLocalStream.Read(s[1], FLocalStream.Size));
  396. end else if FStream.Size - FStream.Position = 0 then
  397. s := s + CRLF;
  398. FBuffer := FBuffer + s;
  399. end;
  400. constructor TMimeStreamSection.Create(aOutputStream: TStream; aStream: TStream);
  401. begin
  402. inherited Create(aOutputStream);
  403. FDisposition := mdAttachment;
  404. FStream := aStream;
  405. FOriginalPosition := FStream.Position;
  406. FContentType := 'application/octet-stream';
  407. end;
  408. destructor TMimeStreamSection.Destroy;
  409. begin
  410. if FOwnsStreams then
  411. FStream.Free;
  412. inherited Destroy;
  413. end;
  414. procedure TMimeStreamSection.Reset;
  415. begin
  416. inherited Reset;
  417. FStream.Position := FOriginalPosition;
  418. end;
  419. { TMimeStream }
  420. function TMimeStream.GetBoundarySize: Integer;
  421. var
  422. n: Integer;
  423. begin
  424. Result := 0;
  425. if FSections.Count > 1 then begin
  426. n := Max(FActiveSection, 0);
  427. Result := (Length(FBoundary) + 4) * (FSections.Count - n) + 2;
  428. // # sections * (boundarylength + --CRLF +) ending --
  429. end;
  430. end;
  431. function TMimeStream.GetSize: Int64;
  432. var
  433. i: Integer;
  434. begin
  435. Result := 0;
  436. if FActiveSection > -2 then
  437. for i := 0 to Count - 1 do
  438. Result := Result + TMimeSection(FSections[i]).Size;
  439. if FActiveSection = -1 then // not yet active, must add header info
  440. Result := Result + Length(GetMimeHeader) + GetBoundarySize;
  441. Result := Result + FOutputStream.Size;
  442. end;
  443. function TMimeStream.GetCount: Integer;
  444. begin
  445. Result := FSections.Count;
  446. end;
  447. function TMimeStream.GetBoundary: string;
  448. var
  449. i: Integer;
  450. begin
  451. Result := '';
  452. for i := 1 to 25 + Random(15) do
  453. Result := Result + Char(Random(Ord('9') - Ord('0') + 1) + Ord('0'));
  454. end;
  455. function TMimeStream.GetSection(i: Integer): TMimeSection;
  456. begin
  457. Result := nil;
  458. if (i >= 0)
  459. and (i < FSections.Count) then
  460. Result := TMimeSection(FSections[i]);
  461. end;
  462. function TMimeStream.GetMimeHeader: string;
  463. const
  464. MIME_HEADER = 'Content-type: multipart/mixed; boundary="';
  465. begin
  466. Result := MIME_VERSION;
  467. if FSections.Count > 1 then
  468. Result := Result + MIME_HEADER + FBoundary + '"' + CRLF + CRLF +
  469. 'This is a multi-part message in MIME format.' + CRLF +
  470. '--' + FBoundary + CRLF;
  471. end;
  472. procedure TMimeStream.SetSection(i: Integer; const AValue: TMimeSection);
  473. begin
  474. if (i >= 0)
  475. and (i < FSections.Count) then
  476. FSections[i] := aValue;
  477. end;
  478. procedure TMimeStream.ActivateFirstSection;
  479. var
  480. s: string;
  481. begin
  482. if FActiveSection = -1 then
  483. if FSections.Count > 0 then begin
  484. FActiveSection := 0;
  485. s := GetMimeHeader;
  486. FOutputStream.Write(s[1], Length(s));
  487. end;
  488. end;
  489. procedure TMimeStream.ActivateNextSection;
  490. var
  491. s: string;
  492. begin
  493. Inc(FActiveSection);
  494. if FSections.Count > 1 then begin
  495. if FActiveSection >= FSections.Count then
  496. s := '--' + FBoundary + '--' + CRLF
  497. else
  498. s := '--' + FBoundary + CRLF;
  499. FOutputStream.Write(s[1], Length(s));
  500. end;
  501. if FActiveSection >= FSections.Count then
  502. FActiveSection := -2;
  503. end;
  504. procedure TMimeStream.DoRead(const aSize: Integer);
  505. begin
  506. ActivateFirstSection;
  507. if FActiveSection < 0 then
  508. Exit;
  509. TMimeSection(FSections[FActiveSection]).Read(aSize);
  510. if TMimeSection(FSections[FActiveSection]).Size = 0 then
  511. ActivateNextSection;
  512. end;
  513. constructor TMimeStream.Create;
  514. begin
  515. Randomize;
  516. FActiveSection := -1;
  517. FBoundary := GetBoundary;
  518. FSections := TFPObjectList.Create(True);
  519. FOutputStream := TMimeOutputStream.Create(@DoRead);
  520. end;
  521. destructor TMimeStream.Destroy;
  522. begin
  523. FSections.Free;
  524. FOutputStream.Free;
  525. inherited Destroy;
  526. end;
  527. function TMimeStream.Seek(Offset: Longint; Origin: Word): Longint;
  528. begin
  529. Result := Offset;
  530. end;
  531. function TMimeStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
  532. begin
  533. Result := Offset;
  534. end;
  535. function TMimeStream.Read(var Buffer; Count: Longint): Longint;
  536. begin
  537. if Count <= 0 then
  538. Exit(0);
  539. if FCalledWrite then
  540. raise EAlreadyCalledWriteException.Create;
  541. FCalledRead := True;
  542. Result := FOutputStream.Read(Buffer, Count);
  543. end;
  544. function TMimeStream.Write(const Buffer; Count: Longint): Longint;
  545. begin
  546. if Count <= 0 then
  547. Exit(0);
  548. if FCalledRead then
  549. raise EAlreadyCalledReadException.Create;
  550. Result := 0;
  551. FCalledWrite := True;
  552. raise Exception.Create('Not yet implemented');
  553. end;
  554. procedure TMimeStream.AddTextSection(const aText: string; const aCharSet: string = 'UTF-8');
  555. var
  556. s: TMimeTextSection;
  557. begin
  558. if FActiveSection >= 0 then
  559. raise EAlreadyActivatedException.Create;
  560. s := TMimeTextSection.Create(FOutputStream, aText);
  561. s.Charset := aCharSet;
  562. FSections.Add(s);
  563. end;
  564. procedure TMimeStream.AddFileSection(const aFileName: string);
  565. begin
  566. if FActiveSection >= 0 then
  567. raise EAlreadyActivatedException.Create;
  568. FSections.Add(TMimeFileSection.Create(FOutputStream, aFileName));
  569. end;
  570. procedure TMimeStream.AddStreamSection(aStream: TStream; const FreeStream: Boolean
  571. );
  572. var
  573. s: TMimeStreamSection;
  574. begin
  575. if FActiveSection >= 0 then
  576. raise EAlreadyActivatedException.Create;
  577. s := TMimeStreamSection.Create(FOutputStream, aStream);
  578. if FreeStream then
  579. s.OwnsStreams := True;
  580. FSections.Add(s);
  581. end;
  582. procedure TMimeStream.Delete(const i: Integer);
  583. begin
  584. if (i >= 0) and (i < Count) then
  585. FSections.Delete(i);
  586. end;
  587. procedure TMimeStream.Remove(aSection: TMimeSection);
  588. begin
  589. FSections.Remove(aSection);
  590. end;
  591. procedure TMimeStream.Reset;
  592. var
  593. i: Integer;
  594. begin
  595. FCalledRead := False;
  596. FCalledWrite := False;
  597. for i := 0 to FSections.Count - 1 do
  598. TMimeSection(FSections[i]).Reset;
  599. FOutputStream.Reset;
  600. FActiveSection := -1;
  601. end;
  602. { TMimeFileSection }
  603. procedure TMimeFileSection.SetFileName(const AValue: string);
  604. begin
  605. if not FActivated then begin
  606. FFileName := aValue;
  607. Stream := TFileStream.Create(aValue, fmOpenRead);
  608. SetContentType(aValue);
  609. end;
  610. end;
  611. procedure TMimeFileSection.SetContentType(const aFileName: string);
  612. var
  613. s: string;
  614. begin
  615. s := StringReplace(ExtractFileExt(aFileName), '.', '', [rfReplaceAll]);
  616. if (s = 'txt')
  617. or (s = 'pas')
  618. or (s = 'pp')
  619. or (s = 'pl')
  620. or (s = 'cpp')
  621. or (s = 'cc')
  622. or (s = 'h')
  623. or (s = 'hh')
  624. or (s = 'rb')
  625. or (s = 'pod')
  626. or (s = 'php')
  627. or (s = 'php3')
  628. or (s = 'php4')
  629. or (s = 'php5')
  630. or (s = 'c++') then FContentType := 'text/plain';
  631. if (s = 'html')
  632. or (s = 'shtml') then FContentType := 'text/html';
  633. if s = 'css' then FContentType := 'text/css';
  634. if s = 'png' then FContentType := 'image/x-png';
  635. if s = 'xpm' then FContentType := 'image/x-pixmap';
  636. if s = 'xbm' then FContentType := 'image/x-bitmap';
  637. if (s = 'tif')
  638. or (s = 'tiff') then FContentType := 'image/tiff';
  639. if s = 'mng' then FContentType := 'image/x-mng';
  640. if s = 'gif' then FContentType := 'image/gif';
  641. if s = 'rgb' then FContentType := 'image/rgb';
  642. if (s = 'jpg')
  643. or (s = 'jpeg') then FContentType := 'image/jpeg';
  644. if s = 'bmp' then FContentType := 'image/x-ms-bmp';
  645. if s = 'wav' then FContentType := 'audio/x-wav';
  646. if s = 'mp3' then FContentType := 'audio/x-mp3';
  647. if s = 'ogg' then FContentType := 'audio/x-ogg';
  648. if s = 'avi' then FContentType := 'video/x-msvideo';
  649. if (s = 'qt')
  650. or (s = 'mov') then FContentType := 'video/quicktime';
  651. if (s = 'mpg')
  652. or (s = 'mpeg') then FContentType := 'video/mpeg';
  653. if s = 'pdf' then FContentType := 'application/pdf';
  654. if s = 'rtf' then FContentType := 'application/rtf';
  655. if s = 'tex' then FContentType := 'application/x-tex';
  656. if s = 'latex' then FContentType := 'application/x-latex';
  657. if s = 'doc' then FContentType := 'application/msword';
  658. if s = 'gz' then FContentType := 'application/x-gzip';
  659. if s = 'zip' then FContentType := 'application/zip';
  660. if s = '7z' then FContentType := 'application/x-7zip';
  661. if s = 'rar' then FContentType := 'application/rar';
  662. if s = 'tar' then FContentType := 'application/x-tar';
  663. if s = 'arj' then FContentType := 'application/arj';
  664. end;
  665. function TMimeFileSection.GetHeader: string;
  666. begin
  667. Result := 'Content-Type: ' + FContentType + CRLF;
  668. Result := Result + 'Content-Transfer-Encoding: ' + EncodingToStr(FEncoding) + CRLF;
  669. Result := Result + 'Content-Disposition: ' + DispositionToStr(FDisposition) +
  670. '; filename="' + FFileName + '"' + CRLF;
  671. if Length(FDescription) > 0 then
  672. Result := Result + 'Content-Description: ' + FDescription + CRLF;
  673. Result := Result + CRLF;
  674. end;
  675. constructor TMimeFileSection.Create(aOutputStream: TStream; const aFileName: string);
  676. begin
  677. inherited Create(aOutputStream, TFileStream.Create(aFileName, fmOpenRead));
  678. SetContentType(aFileName);
  679. FDescription := ExtractFileName(aFileName);
  680. Encoding := meBase64;
  681. FFileName := ExtractFileName(aFileName);
  682. FOwnsStreams := True;
  683. end;
  684. { EAlreadyActivatedException }
  685. constructor EAlreadyActivatedException.Create;
  686. begin
  687. inherited Create('The stream or section has already been activated (by Read() or Write())');
  688. end;
  689. { EAlreadyCalledReadException }
  690. constructor EAlreadyCalledReadException.Create;
  691. begin
  692. inherited Create('The stream has already been used for reading');
  693. end;
  694. { EAlreadyCalledWriteException }
  695. constructor EAlreadyCalledWriteException.Create;
  696. begin
  697. inherited Create('The stream has already been used for writing');
  698. end;
  699. end.