imapsend.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 002.005.004 |
  3. |==============================================================================|
  4. | Content: IMAP4rev1 client |
  5. |==============================================================================|
  6. | Copyright (c)1999-2015, Lukas Gebauer |
  7. | All rights reserved. |
  8. | |
  9. | Redistribution and use in source and binary forms, with or without |
  10. | modification, are permitted provided that the following conditions are met: |
  11. | |
  12. | Redistributions of source code must retain the above copyright notice, this |
  13. | list of conditions and the following disclaimer. |
  14. | |
  15. | Redistributions in binary form must reproduce the above copyright notice, |
  16. | this list of conditions and the following disclaimer in the documentation |
  17. | and/or other materials provided with the distribution. |
  18. | |
  19. | Neither the name of Lukas Gebauer nor the names of its contributors may |
  20. | be used to endorse or promote products derived from this software without |
  21. | specific prior written permission. |
  22. | |
  23. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
  24. | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
  25. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
  26. | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
  27. | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
  28. | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
  29. | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  30. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  31. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
  32. | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
  33. | DAMAGE. |
  34. |==============================================================================|
  35. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  36. | Portions created by Lukas Gebauer are Copyright (c)2001-2015. |
  37. | All Rights Reserved. |
  38. |==============================================================================|
  39. | Contributor(s): |
  40. |==============================================================================|
  41. | History: see HISTORY.HTM from distribution package |
  42. | (Found at URL: http://www.ararat.cz/synapse/) |
  43. |==============================================================================}
  44. {:@abstract(IMAP4 rev1 protocol client)
  45. Used RFC: RFC-2060, RFC-2595
  46. }
  47. {$IFDEF FPC}
  48. {$MODE DELPHI}
  49. {$ENDIF}
  50. {$H+}
  51. {$IFDEF UNICODE}
  52. {$WARN IMPLICIT_STRING_CAST OFF}
  53. {$WARN IMPLICIT_STRING_CAST_LOSS OFF}
  54. {$ENDIF}
  55. unit imapsend;
  56. interface
  57. uses
  58. SysUtils, Classes,
  59. blcksock, synautil;
  60. const
  61. cIMAPProtocol = '143';
  62. type
  63. {:@abstract(Implementation of IMAP4 protocol.)
  64. Note: Are you missing properties for setting Username and Password? Look to
  65. parent @link(TSynaClient) object!
  66. Are you missing properties for specify server address and port? Look to
  67. parent @link(TSynaClient) too!}
  68. TIMAPSend = class(TSynaClient)
  69. protected
  70. FSock: TTCPBlockSocket;
  71. FTagCommand: integer;
  72. FResultString: string;
  73. FFullResult: TStringList;
  74. FIMAPcap: TStringList;
  75. FAuthDone: Boolean;
  76. FSelectedFolder: string;
  77. FSelectedCount: integer;
  78. FSelectedRecent: integer;
  79. FSelectedUIDvalidity: integer;
  80. FUID: Boolean;
  81. FAutoTLS: Boolean;
  82. FFullSSL: Boolean;
  83. function ReadResult: string;
  84. function AuthLogin: Boolean;
  85. function Connect: Boolean;
  86. procedure ParseMess(Value:TStrings);
  87. procedure ParseFolderList(Value:TStrings);
  88. procedure ParseSelect;
  89. procedure ParseSearch(Value:TStrings);
  90. procedure ProcessLiterals;
  91. public
  92. constructor Create;
  93. destructor Destroy; override;
  94. {:By this function you can call any IMAP command. Result of this command is
  95. in adequate properties.}
  96. function IMAPcommand(Value: string): string;
  97. {:By this function you can call any IMAP command what need upload any data.
  98. Result of this command is in adequate properties.}
  99. function IMAPuploadCommand(Value: string; const Data:TStrings): string;
  100. {:Call CAPABILITY command and fill IMAPcap property by new values.}
  101. function Capability: Boolean;
  102. {:Connect to IMAP server and do login to this server. This command begin
  103. session.}
  104. function Login: Boolean;
  105. {:Disconnect from IMAP server and terminate session session. If exists some
  106. deleted and non-purged messages, these messages are not deleted!}
  107. function Logout: Boolean;
  108. {:Do NOOP. It is for prevent disconnect by timeout.}
  109. function NoOp: Boolean;
  110. {:Lists folder names. You may specify level of listing. If you specify
  111. FromFolder as empty string, return is all folders in system.}
  112. function List(FromFolder: string; const FolderList: TStrings): Boolean;
  113. {:Lists folder names what match search criteria. You may specify level of
  114. listing. If you specify FromFolder as empty string, return is all folders
  115. in system.}
  116. function ListSearch(FromFolder, Search: string; const FolderList: TStrings): Boolean;
  117. {:Lists subscribed folder names. You may specify level of listing. If you
  118. specify FromFolder as empty string, return is all subscribed folders in
  119. system.}
  120. function ListSubscribed(FromFolder: string; const FolderList: TStrings): Boolean;
  121. {:Lists subscribed folder names what matching search criteria. You may
  122. specify level of listing. If you specify FromFolder as empty string, return
  123. is all subscribed folders in system.}
  124. function ListSearchSubscribed(FromFolder, Search: string; const FolderList: TStrings): Boolean;
  125. {:Create a new folder.}
  126. function CreateFolder(FolderName: string): Boolean;
  127. {:Delete a folder.}
  128. function DeleteFolder(FolderName: string): Boolean;
  129. {:Rename folder names.}
  130. function RenameFolder(FolderName, NewFolderName: string): Boolean;
  131. {:Subscribe folder.}
  132. function SubscribeFolder(FolderName: string): Boolean;
  133. {:Unsubscribe folder.}
  134. function UnsubscribeFolder(FolderName: string): Boolean;
  135. {:Select folder.}
  136. function SelectFolder(FolderName: string): Boolean;
  137. {:Select folder, but only for reading. Any changes are not allowed!}
  138. function SelectROFolder(FolderName: string): Boolean;
  139. {:Close a folder. (end of Selected state)}
  140. function CloseFolder: Boolean;
  141. {:Ask for given status of folder. I.e. if you specify as value 'UNSEEN',
  142. result is number of unseen messages in folder. For another status
  143. indentificator check IMAP documentation and documentation of your IMAP
  144. server (each IMAP server can have their own statuses.)}
  145. function StatusFolder(FolderName, Value: string): integer;
  146. {:Hardly delete all messages marked as 'deleted' in current selected folder.}
  147. function ExpungeFolder: Boolean;
  148. {:Touch to folder. (use as update status of folder, etc.)}
  149. function CheckFolder: Boolean;
  150. {:Append given message to specified folder.}
  151. function AppendMess(ToFolder: string; const Mess: TStrings): Boolean;
  152. {:'Delete' message from current selected folder. It mark message as Deleted.
  153. Real deleting will be done after sucessfull @link(CloseFolder) or
  154. @link(ExpungeFolder)}
  155. function DeleteMess(MessID: integer): boolean;
  156. {:Get full message from specified message in selected folder.}
  157. function FetchMess(MessID: integer; const Mess: TStrings): Boolean;
  158. {:Get message headers only from specified message in selected folder.}
  159. function FetchHeader(MessID: integer; const Headers: TStrings): Boolean;
  160. {:Return message size of specified message from current selected folder.}
  161. function MessageSize(MessID: integer): integer;
  162. {:Copy message from current selected folder to another folder.}
  163. function CopyMess(MessID: integer; ToFolder: string): Boolean;
  164. {:Return message numbers from currently selected folder as result
  165. of searching. Search criteria is very complex language (see to IMAP
  166. specification) similar to SQL (but not same syntax!).}
  167. function SearchMess(Criteria: string; const FoundMess: TStrings): Boolean;
  168. {:Sets flags of message from current selected folder.}
  169. function SetFlagsMess(MessID: integer; Flags: string): Boolean;
  170. {:Gets flags of message from current selected folder.}
  171. function GetFlagsMess(MessID: integer; var Flags: string): Boolean;
  172. {:Add flags to message's flags.}
  173. function AddFlagsMess(MessID: integer; Flags: string): Boolean;
  174. {:Remove flags from message's flags.}
  175. function DelFlagsMess(MessID: integer; Flags: string): Boolean;
  176. {:Call STARTTLS command for upgrade connection to SSL/TLS mode.}
  177. function StartTLS: Boolean;
  178. {:return UID of requested message ID.}
  179. function GetUID(MessID: integer; var UID : Integer): Boolean;
  180. {:Try to find given capabily in capabilty string returned from IMAP server.}
  181. function FindCap(const Value: string): string;
  182. published
  183. {:Status line with result of last operation.}
  184. property ResultString: string read FResultString;
  185. {:Full result of last IMAP operation.}
  186. property FullResult: TStringList read FFullResult;
  187. {:List of server capabilites.}
  188. property IMAPcap: TStringList read FIMAPcap;
  189. {:Authorization is successful done.}
  190. property AuthDone: Boolean read FAuthDone;
  191. {:Turn on or off usage of UID (unicate identificator) of messages instead
  192. only sequence numbers.}
  193. property UID: Boolean read FUID Write FUID;
  194. {:Name of currently selected folder.}
  195. property SelectedFolder: string read FSelectedFolder;
  196. {:Count of messages in currently selected folder.}
  197. property SelectedCount: integer read FSelectedCount;
  198. {:Count of not-visited messages in currently selected folder.}
  199. property SelectedRecent: integer read FSelectedRecent;
  200. {:This number with name of folder is unique indentificator of folder.
  201. (If someone delete folder and next create new folder with exactly same name
  202. of folder, this number is must be different!)}
  203. property SelectedUIDvalidity: integer read FSelectedUIDvalidity;
  204. {:If is set to true, then upgrade to SSL/TLS mode if remote server support it.}
  205. property AutoTLS: Boolean read FAutoTLS Write FAutoTLS;
  206. {:SSL/TLS mode is used from first contact to server. Servers with full
  207. SSL/TLS mode usualy using non-standard TCP port!}
  208. property FullSSL: Boolean read FFullSSL Write FFullSSL;
  209. {:Socket object used for TCP/IP operation. Good for seting OnStatus hook, etc.}
  210. property Sock: TTCPBlockSocket read FSock;
  211. end;
  212. implementation
  213. constructor TIMAPSend.Create;
  214. begin
  215. inherited Create;
  216. FFullResult := TStringList.Create;
  217. FIMAPcap := TStringList.Create;
  218. FSock := TTCPBlockSocket.Create;
  219. FSock.Owner := self;
  220. FSock.ConvertLineEnd := True;
  221. FSock.SizeRecvBuffer := 32768;
  222. FSock.SizeSendBuffer := 32768;
  223. FTimeout := 60000;
  224. FTargetPort := cIMAPProtocol;
  225. FTagCommand := 0;
  226. FSelectedFolder := '';
  227. FSelectedCount := 0;
  228. FSelectedRecent := 0;
  229. FSelectedUIDvalidity := 0;
  230. FUID := False;
  231. FAutoTLS := False;
  232. FFullSSL := False;
  233. end;
  234. destructor TIMAPSend.Destroy;
  235. begin
  236. FSock.Free;
  237. FIMAPcap.Free;
  238. FFullResult.Free;
  239. inherited Destroy;
  240. end;
  241. function TIMAPSend.ReadResult: string;
  242. var
  243. s: string;
  244. x, l: integer;
  245. begin
  246. Result := '';
  247. FFullResult.Clear;
  248. FResultString := '';
  249. repeat
  250. s := FSock.RecvString(FTimeout);
  251. if Pos('S' + IntToStr(FTagCommand) + ' ', s) = 1 then
  252. begin
  253. FResultString := s;
  254. break;
  255. end
  256. else
  257. FFullResult.Add(s);
  258. if (s <> '') and (s[Length(s)]='}') then
  259. begin
  260. s := Copy(s, 1, Length(s) - 1);
  261. x := RPos('{', s);
  262. s := Copy(s, x + 1, Length(s) - x);
  263. l := StrToIntDef(s, -1);
  264. if l <> -1 then
  265. begin
  266. s := FSock.RecvBufferStr(l, FTimeout);
  267. FFullResult.Add(s);
  268. end;
  269. end;
  270. until FSock.LastError <> 0;
  271. s := Trim(separateright(FResultString, ' '));
  272. Result:=uppercase(Trim(separateleft(s, ' ')));
  273. end;
  274. procedure TIMAPSend.ProcessLiterals;
  275. var
  276. l: TStringList;
  277. n, x: integer;
  278. b: integer;
  279. s: string;
  280. begin
  281. l := TStringList.Create;
  282. try
  283. l.Assign(FFullResult);
  284. FFullResult.Clear;
  285. b := 0;
  286. for n := 0 to l.Count - 1 do
  287. begin
  288. s := l[n];
  289. if b > 0 then
  290. begin
  291. FFullResult[FFullresult.Count - 1] :=
  292. FFullResult[FFullresult.Count - 1] + s;
  293. inc(b);
  294. if b > 2 then
  295. b := 0;
  296. end
  297. else
  298. begin
  299. if (s <> '') and (s[Length(s)]='}') then
  300. begin
  301. x := RPos('{', s);
  302. Delete(s, x, Length(s) - x + 1);
  303. b := 1;
  304. end
  305. else
  306. b := 0;
  307. FFullResult.Add(s);
  308. end;
  309. end;
  310. finally
  311. l.Free;
  312. end;
  313. end;
  314. function TIMAPSend.IMAPcommand(Value: string): string;
  315. begin
  316. Inc(FTagCommand);
  317. FSock.SendString('S' + IntToStr(FTagCommand) + ' ' + Value + CRLF);
  318. Result := ReadResult;
  319. end;
  320. function TIMAPSend.IMAPuploadCommand(Value: string; const Data:TStrings): string;
  321. var
  322. l: integer;
  323. begin
  324. Inc(FTagCommand);
  325. l := Length(Data.Text);
  326. FSock.SendString('S' + IntToStr(FTagCommand) + ' ' + Value + ' {'+ IntToStr(l) + '}' + CRLF);
  327. FSock.RecvString(FTimeout);
  328. FSock.SendString(Data.Text + CRLF);
  329. Result := ReadResult;
  330. end;
  331. procedure TIMAPSend.ParseMess(Value:TStrings);
  332. var
  333. n: integer;
  334. begin
  335. Value.Clear;
  336. for n := 0 to FFullResult.Count - 2 do
  337. if (length(FFullResult[n]) > 0) and (FFullResult[n][Length(FFullResult[n])] = '}') then
  338. begin
  339. Value.Text := FFullResult[n + 1];
  340. Break;
  341. end;
  342. end;
  343. procedure TIMAPSend.ParseFolderList(Value:TStrings);
  344. var
  345. n, x: integer;
  346. s: string;
  347. begin
  348. ProcessLiterals;
  349. Value.Clear;
  350. for n := 0 to FFullResult.Count - 1 do
  351. begin
  352. s := FFullResult[n];
  353. if (s <> '') and (Pos('\NOSELECT', UpperCase(s)) = 0) then
  354. begin
  355. if s[Length(s)] = '"' then
  356. begin
  357. Delete(s, Length(s), 1);
  358. x := RPos('"', s);
  359. end
  360. else
  361. x := RPos(' ', s);
  362. if (x > 0) then
  363. Value.Add(Copy(s, x + 1, Length(s) - x));
  364. end;
  365. end;
  366. end;
  367. procedure TIMAPSend.ParseSelect;
  368. var
  369. n: integer;
  370. s, t: string;
  371. begin
  372. ProcessLiterals;
  373. FSelectedCount := 0;
  374. FSelectedRecent := 0;
  375. FSelectedUIDvalidity := 0;
  376. for n := 0 to FFullResult.Count - 1 do
  377. begin
  378. s := uppercase(FFullResult[n]);
  379. if Pos(' EXISTS', s) > 0 then
  380. begin
  381. t := Trim(separateleft(s, ' EXISTS'));
  382. t := Trim(separateright(t, '* '));
  383. FSelectedCount := StrToIntDef(t, 0);
  384. end;
  385. if Pos(' RECENT', s) > 0 then
  386. begin
  387. t := Trim(separateleft(s, ' RECENT'));
  388. t := Trim(separateright(t, '* '));
  389. FSelectedRecent := StrToIntDef(t, 0);
  390. end;
  391. if Pos('UIDVALIDITY', s) > 0 then
  392. begin
  393. t := Trim(separateright(s, 'UIDVALIDITY '));
  394. t := Trim(separateleft(t, ']'));
  395. FSelectedUIDvalidity := StrToIntDef(t, 0);
  396. end;
  397. end;
  398. end;
  399. procedure TIMAPSend.ParseSearch(Value:TStrings);
  400. var
  401. n: integer;
  402. s: string;
  403. begin
  404. ProcessLiterals;
  405. Value.Clear;
  406. for n := 0 to FFullResult.Count - 1 do
  407. begin
  408. s := uppercase(FFullResult[n]);
  409. if Pos('* SEARCH', s) = 1 then
  410. begin
  411. s := Trim(SeparateRight(s, '* SEARCH'));
  412. while s <> '' do
  413. Value.Add(Fetch(s, ' '));
  414. end;
  415. end;
  416. end;
  417. function TIMAPSend.FindCap(const Value: string): string;
  418. var
  419. n: Integer;
  420. s: string;
  421. begin
  422. s := UpperCase(Value);
  423. Result := '';
  424. for n := 0 to FIMAPcap.Count - 1 do
  425. if Pos(s, UpperCase(FIMAPcap[n])) = 1 then
  426. begin
  427. Result := FIMAPcap[n];
  428. Break;
  429. end;
  430. end;
  431. function TIMAPSend.AuthLogin: Boolean;
  432. begin
  433. Result := IMAPcommand('LOGIN "' + FUsername + '" "' + FPassword + '"') = 'OK';
  434. if Result then
  435. FAuthDone := True;
  436. end;
  437. function TIMAPSend.Connect: Boolean;
  438. begin
  439. FSock.CloseSocket;
  440. FSock.Bind(FIPInterface, cAnyPort);
  441. if FSock.LastError = 0 then
  442. FSock.Connect(FTargetHost, FTargetPort);
  443. if FSock.LastError = 0 then
  444. if FFullSSL then
  445. FSock.SSLDoConnect;
  446. Result := FSock.LastError = 0;
  447. end;
  448. function TIMAPSend.Capability: Boolean;
  449. var
  450. n: Integer;
  451. s, t: string;
  452. begin
  453. Result := False;
  454. FIMAPcap.Clear;
  455. s := IMAPcommand('CAPABILITY');
  456. if s = 'OK' then
  457. begin
  458. ProcessLiterals;
  459. for n := 0 to FFullResult.Count - 1 do
  460. if Pos('* CAPABILITY ', FFullResult[n]) = 1 then
  461. begin
  462. s := Trim(SeparateRight(FFullResult[n], '* CAPABILITY '));
  463. while not (s = '') do
  464. begin
  465. t := Trim(separateleft(s, ' '));
  466. s := Trim(separateright(s, ' '));
  467. if s = t then
  468. s := '';
  469. FIMAPcap.Add(t);
  470. end;
  471. end;
  472. Result := True;
  473. end;
  474. end;
  475. function TIMAPSend.Login: Boolean;
  476. var
  477. s: string;
  478. begin
  479. FSelectedFolder := '';
  480. FSelectedCount := 0;
  481. FSelectedRecent := 0;
  482. FSelectedUIDvalidity := 0;
  483. Result := False;
  484. FAuthDone := False;
  485. if not Connect then
  486. Exit;
  487. s := FSock.RecvString(FTimeout);
  488. if Pos('* PREAUTH', s) = 1 then
  489. FAuthDone := True
  490. else
  491. if Pos('* OK', s) = 1 then
  492. FAuthDone := False
  493. else
  494. Exit;
  495. if Capability then
  496. begin
  497. if Findcap('IMAP4rev1') = '' then
  498. Exit;
  499. if FAutoTLS and (Findcap('STARTTLS') <> '') then
  500. if StartTLS then
  501. Capability;
  502. end;
  503. Result := AuthLogin;
  504. end;
  505. function TIMAPSend.Logout: Boolean;
  506. begin
  507. Result := IMAPcommand('LOGOUT') = 'OK';
  508. FSelectedFolder := '';
  509. FSock.CloseSocket;
  510. end;
  511. function TIMAPSend.NoOp: Boolean;
  512. begin
  513. Result := IMAPcommand('NOOP') = 'OK';
  514. end;
  515. function TIMAPSend.List(FromFolder: string; const FolderList: TStrings): Boolean;
  516. begin
  517. Result := IMAPcommand('LIST "' + FromFolder + '" *') = 'OK';
  518. ParseFolderList(FolderList);
  519. end;
  520. function TIMAPSend.ListSearch(FromFolder, Search: string; const FolderList: TStrings): Boolean;
  521. begin
  522. Result := IMAPcommand('LIST "' + FromFolder + '" "' + Search +'"') = 'OK';
  523. ParseFolderList(FolderList);
  524. end;
  525. function TIMAPSend.ListSubscribed(FromFolder: string; const FolderList: TStrings): Boolean;
  526. begin
  527. Result := IMAPcommand('LSUB "' + FromFolder + '" *') = 'OK';
  528. ParseFolderList(FolderList);
  529. end;
  530. function TIMAPSend.ListSearchSubscribed(FromFolder, Search: string; const FolderList: TStrings): Boolean;
  531. begin
  532. Result := IMAPcommand('LSUB "' + FromFolder + '" "' + Search +'"') = 'OK';
  533. ParseFolderList(FolderList);
  534. end;
  535. function TIMAPSend.CreateFolder(FolderName: string): Boolean;
  536. begin
  537. Result := IMAPcommand('CREATE "' + FolderName + '"') = 'OK';
  538. end;
  539. function TIMAPSend.DeleteFolder(FolderName: string): Boolean;
  540. begin
  541. Result := IMAPcommand('DELETE "' + FolderName + '"') = 'OK';
  542. end;
  543. function TIMAPSend.RenameFolder(FolderName, NewFolderName: string): Boolean;
  544. begin
  545. Result := IMAPcommand('RENAME "' + FolderName + '" "' + NewFolderName + '"') = 'OK';
  546. end;
  547. function TIMAPSend.SubscribeFolder(FolderName: string): Boolean;
  548. begin
  549. Result := IMAPcommand('SUBSCRIBE "' + FolderName + '"') = 'OK';
  550. end;
  551. function TIMAPSend.UnsubscribeFolder(FolderName: string): Boolean;
  552. begin
  553. Result := IMAPcommand('UNSUBSCRIBE "' + FolderName + '"') = 'OK';
  554. end;
  555. function TIMAPSend.SelectFolder(FolderName: string): Boolean;
  556. begin
  557. Result := IMAPcommand('SELECT "' + FolderName + '"') = 'OK';
  558. FSelectedFolder := FolderName;
  559. ParseSelect;
  560. end;
  561. function TIMAPSend.SelectROFolder(FolderName: string): Boolean;
  562. begin
  563. Result := IMAPcommand('EXAMINE "' + FolderName + '"') = 'OK';
  564. FSelectedFolder := FolderName;
  565. ParseSelect;
  566. end;
  567. function TIMAPSend.CloseFolder: Boolean;
  568. begin
  569. Result := IMAPcommand('CLOSE') = 'OK';
  570. FSelectedFolder := '';
  571. end;
  572. function TIMAPSend.StatusFolder(FolderName, Value: string): integer;
  573. var
  574. n: integer;
  575. s, t: string;
  576. begin
  577. Result := -1;
  578. Value := Uppercase(Value);
  579. if IMAPcommand('STATUS "' + FolderName + '" (' + Value + ')' ) = 'OK' then
  580. begin
  581. ProcessLiterals;
  582. for n := 0 to FFullResult.Count - 1 do
  583. begin
  584. s := FFullResult[n];
  585. // s := UpperCase(FFullResult[n]);
  586. if (Pos('* ', s) = 1) and (Pos(FolderName, s) >= 1) and (Pos(Value, s) > 0 ) then
  587. begin
  588. t := SeparateRight(s, Value);
  589. t := SeparateLeft(t, ')');
  590. t := trim(t);
  591. Result := StrToIntDef(t, -1);
  592. Break;
  593. end;
  594. end;
  595. end;
  596. end;
  597. function TIMAPSend.ExpungeFolder: Boolean;
  598. begin
  599. Result := IMAPcommand('EXPUNGE') = 'OK';
  600. end;
  601. function TIMAPSend.CheckFolder: Boolean;
  602. begin
  603. Result := IMAPcommand('CHECK') = 'OK';
  604. end;
  605. function TIMAPSend.AppendMess(ToFolder: string; const Mess: TStrings): Boolean;
  606. begin
  607. Result := IMAPuploadCommand('APPEND "' + ToFolder + '"', Mess) = 'OK';
  608. end;
  609. function TIMAPSend.DeleteMess(MessID: integer): boolean;
  610. var
  611. s: string;
  612. begin
  613. s := 'STORE ' + IntToStr(MessID) + ' +FLAGS.SILENT (\Deleted)';
  614. if FUID then
  615. s := 'UID ' + s;
  616. Result := IMAPcommand(s) = 'OK';
  617. end;
  618. function TIMAPSend.FetchMess(MessID: integer; const Mess: TStrings): Boolean;
  619. var
  620. s: string;
  621. begin
  622. s := 'FETCH ' + IntToStr(MessID) + ' (RFC822)';
  623. if FUID then
  624. s := 'UID ' + s;
  625. Result := IMAPcommand(s) = 'OK';
  626. ParseMess(Mess);
  627. end;
  628. function TIMAPSend.FetchHeader(MessID: integer; const Headers: TStrings): Boolean;
  629. var
  630. s: string;
  631. begin
  632. s := 'FETCH ' + IntToStr(MessID) + ' (RFC822.HEADER)';
  633. if FUID then
  634. s := 'UID ' + s;
  635. Result := IMAPcommand(s) = 'OK';
  636. ParseMess(Headers);
  637. end;
  638. function TIMAPSend.MessageSize(MessID: integer): integer;
  639. var
  640. n: integer;
  641. s, t: string;
  642. begin
  643. Result := -1;
  644. s := 'FETCH ' + IntToStr(MessID) + ' (RFC822.SIZE)';
  645. if FUID then
  646. s := 'UID ' + s;
  647. if IMAPcommand(s) = 'OK' then
  648. begin
  649. ProcessLiterals;
  650. for n := 0 to FFullResult.Count - 1 do
  651. begin
  652. s := UpperCase(FFullResult[n]);
  653. if (Pos('* ', s) = 1) and (Pos('RFC822.SIZE', s) > 0 ) then
  654. begin
  655. t := SeparateRight(s, 'RFC822.SIZE ');
  656. t := Trim(SeparateLeft(t, ')'));
  657. t := Trim(SeparateLeft(t, ' '));
  658. Result := StrToIntDef(t, -1);
  659. Break;
  660. end;
  661. end;
  662. end;
  663. end;
  664. function TIMAPSend.CopyMess(MessID: integer; ToFolder: string): Boolean;
  665. var
  666. s: string;
  667. begin
  668. s := 'COPY ' + IntToStr(MessID) + ' "' + ToFolder + '"';
  669. if FUID then
  670. s := 'UID ' + s;
  671. Result := IMAPcommand(s) = 'OK';
  672. end;
  673. function TIMAPSend.SearchMess(Criteria: string; const FoundMess: TStrings): Boolean;
  674. var
  675. s: string;
  676. begin
  677. s := 'SEARCH ' + Criteria;
  678. if FUID then
  679. s := 'UID ' + s;
  680. Result := IMAPcommand(s) = 'OK';
  681. ParseSearch(FoundMess);
  682. end;
  683. function TIMAPSend.SetFlagsMess(MessID: integer; Flags: string): Boolean;
  684. var
  685. s: string;
  686. begin
  687. s := 'STORE ' + IntToStr(MessID) + ' FLAGS.SILENT (' + Flags + ')';
  688. if FUID then
  689. s := 'UID ' + s;
  690. Result := IMAPcommand(s) = 'OK';
  691. end;
  692. function TIMAPSend.AddFlagsMess(MessID: integer; Flags: string): Boolean;
  693. var
  694. s: string;
  695. begin
  696. s := 'STORE ' + IntToStr(MessID) + ' +FLAGS.SILENT (' + Flags + ')';
  697. if FUID then
  698. s := 'UID ' + s;
  699. Result := IMAPcommand(s) = 'OK';
  700. end;
  701. function TIMAPSend.DelFlagsMess(MessID: integer; Flags: string): Boolean;
  702. var
  703. s: string;
  704. begin
  705. s := 'STORE ' + IntToStr(MessID) + ' -FLAGS.SILENT (' + Flags + ')';
  706. if FUID then
  707. s := 'UID ' + s;
  708. Result := IMAPcommand(s) = 'OK';
  709. end;
  710. function TIMAPSend.GetFlagsMess(MessID: integer; var Flags: string): Boolean;
  711. var
  712. s: string;
  713. n: integer;
  714. begin
  715. Flags := '';
  716. s := 'FETCH ' + IntToStr(MessID) + ' (FLAGS)';
  717. if FUID then
  718. s := 'UID ' + s;
  719. Result := IMAPcommand(s) = 'OK';
  720. ProcessLiterals;
  721. for n := 0 to FFullResult.Count - 1 do
  722. begin
  723. s := uppercase(FFullResult[n]);
  724. if (Pos('* ', s) = 1) and (Pos('FLAGS', s) > 0 ) then
  725. begin
  726. s := SeparateRight(s, 'FLAGS');
  727. s := Separateright(s, '(');
  728. Flags := Trim(SeparateLeft(s, ')'));
  729. end;
  730. end;
  731. end;
  732. function TIMAPSend.StartTLS: Boolean;
  733. begin
  734. Result := False;
  735. if FindCap('STARTTLS') <> '' then
  736. begin
  737. if IMAPcommand('STARTTLS') = 'OK' then
  738. begin
  739. Fsock.SSLDoConnect;
  740. Result := FSock.LastError = 0;
  741. end;
  742. end;
  743. end;
  744. //Paul Buskermolen <[email protected]>
  745. function TIMAPSend.GetUID(MessID: integer; var UID : Integer): boolean;
  746. var
  747. s, sUid: string;
  748. n: integer;
  749. begin
  750. sUID := '';
  751. s := 'FETCH ' + IntToStr(MessID) + ' UID';
  752. Result := IMAPcommand(s) = 'OK';
  753. ProcessLiterals;
  754. for n := 0 to FFullResult.Count - 1 do
  755. begin
  756. s := uppercase(FFullResult[n]);
  757. if Pos('FETCH (UID', s) >= 1 then
  758. begin
  759. s := Separateright(s, '(UID ');
  760. sUID := Trim(SeparateLeft(s, ')'));
  761. end;
  762. end;
  763. UID := StrToIntDef(sUID, 0);
  764. end;
  765. {==============================================================================}
  766. end.