PageRenderTime 30ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/units/synapse/pop3send.pas

http://github.com/rofl0r/KOL
Pascal | 265 lines | 210 code | 29 blank | 26 comment | 13 complexity | 0afa76b61dcc4d498a13f337ca7e376b MD5 | raw file
  1. {==============================================================================|
  2. | Project : Delphree - Synapse | 001.001.002 |
  3. |==============================================================================|
  4. | Content: POP3 client |
  5. |==============================================================================|
  6. | The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
  7. | (the "License"); you may not use this file except in compliance with the |
  8. | License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
  9. | |
  10. | Software distributed under the License is distributed on an "AS IS" basis, |
  11. | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
  12. | the specific language governing rights and limitations under the License. |
  13. |==============================================================================|
  14. | The Original Code is Synapse Delphi Library. |
  15. |==============================================================================|
  16. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  17. | Portions created by Lukas Gebauer are Copyright (c)2001. |
  18. | All Rights Reserved. |
  19. |==============================================================================|
  20. | Contributor(s): |
  21. |==============================================================================|
  22. | History: see HISTORY.HTM from distribution package |
  23. | (Found at URL: http://www.ararat.cz/synapse/) |
  24. |==============================================================================}
  25. {$WEAKPACKAGEUNIT ON}
  26. unit POP3send;
  27. interface
  28. uses
  29. KOL,
  30. blcksock, SynaUtil, SynaCode;
  31. const
  32. cPop3Protocol = 'pop3';
  33. type
  34. TPOP3AuthType = (POP3AuthAll, POP3AuthLogin, POP3AuthAPOP);
  35. PPOP3Send = ^TPOP3Send;
  36. TPOP3Send = object(TObj)
  37. private
  38. FSock: PTCPBlockSocket;
  39. FTimeout: Integer;
  40. FPOP3Host: string;
  41. FPOP3Port: string;
  42. FResultCode: Integer;
  43. FResultString: string;
  44. FFullResult: PStrList;
  45. FUsername: string;
  46. FPassword: string;
  47. FStatCount: Integer;
  48. FStatSize: Integer;
  49. FTimeStamp: string;
  50. FAuthType: TPOP3AuthType;
  51. function ReadResult(Full: Boolean): Integer;
  52. function Connect: Boolean;
  53. function AuthLogin: Boolean;
  54. function AuthApop: Boolean;
  55. public
  56. destructor Destroy; virtual;
  57. function Login: Boolean;
  58. procedure Logout;
  59. function Reset: Boolean;
  60. function NoOp: Boolean;
  61. function Stat: Boolean;
  62. function List(Value: Integer): Boolean;
  63. function Retr(Value: Integer): Boolean;
  64. function Dele(Value: Integer): Boolean;
  65. function Top(Value, Maxlines: Integer): Boolean;
  66. function Uidl(Value: Integer): Boolean;
  67. property Timeout: Integer read FTimeout Write FTimeout;
  68. property POP3Host: string read FPOP3Host Write FPOP3Host;
  69. property POP3Port: string read FPOP3Port Write FPOP3Port;
  70. property ResultCode: Integer read FResultCode;
  71. property ResultString: string read FResultString;
  72. property FullResult: PStrList read FFullResult;
  73. property Username: string read FUsername Write FUsername;
  74. property Password: string read FPassword Write FPassword;
  75. property StatCount: Integer read FStatCount;
  76. property StatSize: Integer read FStatSize;
  77. property TimeStamp: string read FTimeStamp;
  78. property AuthType: TPOP3AuthType read FAuthType Write FAuthType;
  79. property Sock: PTCPBlockSocket read FSock;
  80. end;
  81. function NewPOP3Send : PPOP3Send;
  82. implementation
  83. const
  84. CRLF = #13#10;
  85. function NewPOP3Send : PPOP3Send;
  86. begin
  87. New(Result,Create);
  88. Result.FFullResult := NewStrList;
  89. Result.FSock := NewTCPBlockSocket;
  90. Result.FSock.CreateSocket;
  91. Result.FTimeout := 300000;
  92. Result.FPOP3host := cLocalhost;
  93. Result.FPOP3Port := cPop3Protocol;
  94. Result.FUsername := '';
  95. Result.FPassword := '';
  96. Result.FStatCount := 0;
  97. Result.FStatSize := 0;
  98. Result.FAuthType := POP3AuthAll;
  99. end;
  100. destructor TPOP3Send.Destroy;
  101. begin
  102. FSock.Free;
  103. FullResult.Free;
  104. inherited Destroy;
  105. end;
  106. function TPOP3Send.ReadResult(Full: Boolean): Integer;
  107. var
  108. s: string;
  109. begin
  110. Result := 0;
  111. FFullResult.Clear;
  112. s := FSock.RecvString(FTimeout);
  113. if Pos('+OK', s) = 1 then
  114. Result := 1;
  115. FResultString := s;
  116. if Full and (Result = 1) then
  117. repeat
  118. s := FSock.RecvString(FTimeout);
  119. if s = '.' then
  120. Break;
  121. FFullResult.Add(s);
  122. until FSock.LastError <> 0;
  123. FResultCode := Result;
  124. end;
  125. function TPOP3Send.AuthLogin: Boolean;
  126. begin
  127. Result := False;
  128. FSock.SendString('USER ' + FUserName + CRLF);
  129. if ReadResult(False) <> 1 then
  130. Exit;
  131. FSock.SendString('PASS ' + FPassword + CRLF);
  132. Result := ReadResult(False) = 1;
  133. end;
  134. function TPOP3Send.AuthAPOP: Boolean;
  135. var
  136. s: string;
  137. begin
  138. s := StrToHex(MD5(FTimeStamp + FPassWord));
  139. FSock.SendString('APOP ' + FUserName + ' ' + s + CRLF);
  140. Result := ReadResult(False) = 1;
  141. end;
  142. function TPOP3Send.Connect: Boolean;
  143. begin
  144. // Do not call this function! It is calling by LOGIN method!
  145. FStatCount := 0;
  146. FStatSize := 0;
  147. FSock.CloseSocket;
  148. FSock.LineBuffer := '';
  149. FSock.CreateSocket;
  150. FSock.Connect(POP3Host, POP3Port);
  151. Result := FSock.LastError = 0;
  152. end;
  153. function TPOP3Send.Login: Boolean;
  154. var
  155. s, s1: string;
  156. begin
  157. Result := False;
  158. FTimeStamp := '';
  159. if not Connect then
  160. Exit;
  161. if ReadResult(False) <> 1 then
  162. Exit;
  163. s := SeparateRight(FResultString, '<');
  164. if s <> FResultString then
  165. begin
  166. s1 := SeparateLeft(s, '>');
  167. if s1 <> s then
  168. FTimeStamp := '<' + s1 + '>';
  169. end;
  170. Result := False;
  171. if (FTimeStamp <> '') and not (FAuthType = POP3AuthLogin) then
  172. Result := AuthApop;
  173. if not Result and not (FAuthType = POP3AuthAPOP) then
  174. Result := AuthLogin;
  175. end;
  176. procedure TPOP3Send.Logout;
  177. begin
  178. FSock.SendString('QUIT' + CRLF);
  179. ReadResult(False);
  180. FSock.CloseSocket;
  181. end;
  182. function TPOP3Send.Reset: Boolean;
  183. begin
  184. FSock.SendString('RSET' + CRLF);
  185. Result := ReadResult(False) = 1;
  186. end;
  187. function TPOP3Send.NoOp: Boolean;
  188. begin
  189. FSock.SendString('NOOP' + CRLF);
  190. Result := ReadResult(False) = 1;
  191. end;
  192. function TPOP3Send.Stat: Boolean;
  193. var
  194. s: string;
  195. begin
  196. Result := False;
  197. FSock.SendString('STAT' + CRLF);
  198. if ReadResult(False) <> 1 then
  199. Exit;
  200. s := SeparateRight(ResultString, '+OK ');
  201. FStatCount := StrToIntDef(SeparateLeft(s, ' '), 0);
  202. FStatSize := StrToIntDef(SeparateRight(s, ' '), 0);
  203. Result := True;
  204. end;
  205. function TPOP3Send.List(Value: Integer): Boolean;
  206. begin
  207. if Value = 0 then
  208. FSock.SendString('LIST' + CRLF)
  209. else
  210. FSock.SendString('LIST ' + Int2Str(Value) + CRLF);
  211. Result := ReadResult(Value = 0) = 1;
  212. end;
  213. function TPOP3Send.Retr(Value: Integer): Boolean;
  214. begin
  215. FSock.SendString('RETR ' + Int2Str(Value) + CRLF);
  216. Result := ReadResult(True) = 1;
  217. end;
  218. function TPOP3Send.Dele(Value: Integer): Boolean;
  219. begin
  220. FSock.SendString('DELE ' + Int2Str(Value) + CRLF);
  221. Result := ReadResult(False) = 1;
  222. end;
  223. function TPOP3Send.Top(Value, Maxlines: Integer): Boolean;
  224. begin
  225. FSock.SendString('TOP ' + Int2Str(Value) + ' ' + Int2Str(Maxlines) + CRLF);
  226. Result := ReadResult(True) = 1;
  227. end;
  228. function TPOP3Send.Uidl(Value: Integer): Boolean;
  229. begin
  230. if Value = 0 then
  231. FSock.SendString('UIDL' + CRLF)
  232. else
  233. FSock.SendString('UIDL ' + Int2Str(Value) + CRLF);
  234. Result := ReadResult(Value = 0) = 1;
  235. end;
  236. end.