IdSNPP.pas 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.10 3/6/2004 3:05:08 PM JPMugaas
  18. Added quit code 211 as per bug #83
  19. Rev 1.9 2004.03.06 1:31:54 PM czhower
  20. To match Disconnect changes to core.
  21. Rev 1.8 2004.02.07 1:42:00 PM czhower
  22. Fixed visibility
  23. Rev 1.7 2004.02.03 5:44:22 PM czhower
  24. Name changes
  25. Rev 1.6 1/21/2004 4:03:40 PM JPMugaas
  26. InitComponent
  27. Rev 1.5 10/17/2003 1:08:06 AM DSiders
  28. Added localization comments.
  29. Rev 1.4 6/5/2003 04:54:12 AM JPMugaas
  30. Reworkings and minor changes for new Reply exception framework.
  31. Rev 1.3 1/27/2003 11:36:54 AM DSiders
  32. Modified Connect to raise an exception when any response other than 220 is
  33. received. Exception code and text comes from the server response.
  34. Added TODOs needed to complete SNPP Level 1 support.
  35. Rev 1.2 12/8/2002 07:25:48 PM JPMugaas
  36. Added published host and port properties.
  37. Rev 1.1 12/6/2002 05:30:34 PM JPMugaas
  38. Now decend from TIdTCPClientCustom instead of TIdTCPClient.
  39. Rev 1.0 11/13/2002 08:01:08 AM JPMugaas
  40. }
  41. unit IdSNPP;
  42. interface
  43. {$i IdCompilerDefines.inc}
  44. uses
  45. IdComponent, IdGlobal, IdException, IdGlobalProtocols,
  46. IdReplyRFC,
  47. IdTCPConnection,
  48. IdTCPClient;
  49. {
  50. Simple Network Paging Protocol based on RFC 1861
  51. Original Author: Mark Holmes
  52. }
  53. { Note that this only supports Level One SNPP }
  54. type
  55. { TODO : Unused... remove? }
  56. TConnectionResult = (crCanPost, crNoPost, crAuthRequired, crTempUnavailable);
  57. { TODO : Unused... remove? }
  58. TCheckResp = Record
  59. Code : Int16;
  60. Resp : String;
  61. end;
  62. { TODO : Add optional HELP command }
  63. { TODO : Add QUIT procedure }
  64. { TODO : Add overridden GetResponse to handle multiline 214 response codes
  65. that omit the continuation mark. For example:
  66. 214 First line...
  67. 214 Second line...
  68. 214 Final line
  69. 250 OK
  70. }
  71. { TODO : Raise an exception when the fatal error response code 421
  72. is received in SendMessage, SNPPMsg, Pager
  73. }
  74. TIdSNPP = class(TIdTCPClientCustom)
  75. protected
  76. function Pager(APagerId: String): Boolean;
  77. function SNPPMsg(AMsg: String): Boolean;
  78. procedure InitComponent; override;
  79. public
  80. procedure Connect; override;
  81. procedure DisconnectNotifyPeer; override;
  82. procedure Reset;
  83. procedure SendMessage(APagerId, AMsg: String);
  84. published
  85. property Port default 7777;
  86. property Host;
  87. end;
  88. EIdSNPPException = class(EIdException);
  89. EIdSNPPConnectionRefused = class (EIdReplyRFCError);
  90. EIdSNPPProtocolError = class (EIdReplyRFCError);
  91. EIdSNPPNoMultiLineMessages = class(EIdSNPPException);
  92. implementation
  93. uses
  94. IdResourceStringsProtocols;
  95. { TIdSNPP }
  96. procedure TIdSNPP.InitComponent;
  97. begin
  98. inherited InitComponent;
  99. Port := 7777;
  100. end;
  101. procedure TIdSNPP.Connect;
  102. begin
  103. inherited Connect;
  104. try
  105. if GetResponse <> 220 then
  106. begin
  107. raise EIdSNPPConnectionRefused.CreateError(LastCmdResult.NumericCode,
  108. LastCmdResult.Text.Text);
  109. end;
  110. except
  111. Disconnect;
  112. Raise;
  113. end;
  114. end;
  115. procedure TIdSNPP.DisconnectNotifyPeer;
  116. begin
  117. inherited DisconnectNotifyPeer;
  118. SendCmd('QUIT', 211); {do not localize}
  119. end;
  120. function TIdSNPP.Pager(APagerId: String): Boolean;
  121. begin
  122. Result := False;
  123. if SendCmd('PAGER ' + APagerID) = 250 then begin {do not localize}
  124. Result := True;
  125. end else begin
  126. {$IFDEF OVERLOADED_OPENARRAY_BUG}DoStatusArr{$ELSE}DoStatus{$ENDIF}(hsStatusText, [LastCmdResult.Text[0]]);
  127. end;
  128. end;
  129. procedure TIdSNPP.Reset;
  130. begin
  131. Writeln('RESET'); {do not localize}
  132. end;
  133. procedure TIdSNPP.SendMessage(APagerId, AMsg : String);
  134. begin
  135. if (Pos(CR,AMsg)>0) or (Pos(LF,AMsg)>0) then
  136. begin
  137. raise EIdSNPPNoMultiLineMessages.Create(RSSNPPNoMultiLine);
  138. end;
  139. if (Length(APagerId) > 0) and (Length(AMsg) > 0) then begin
  140. if Pager(APagerID) then begin
  141. if SNPPMsg(AMsg) then begin
  142. WriteLn('SEND'); {do not localize}
  143. end;
  144. GetResponse(250);
  145. end;
  146. end;
  147. end;
  148. function TIdSNPP.SNPPMsg(AMsg: String): Boolean;
  149. begin
  150. Result := False;
  151. if SendCmd('MESS ' + AMsg) = 250 then begin {do not localize}
  152. Result := True;
  153. end else begin
  154. {$IFDEF OVERLOADED_OPENARRAY_BUG}DoStatusArr{$ELSE}DoStatus{$ENDIF}(hsStatusText, [LastCmdResult.Text.Text]);
  155. end;
  156. end;
  157. end.