IdIdent.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10201: IdIdent.pas
  11. {
  12. { Rev 1.0 2002.11.12 10:41:44 PM czhower
  13. }
  14. unit IdIdent;
  15. interface
  16. uses Classes, IdAssignedNumbers, IdException, IdTCPClient;
  17. { 2001 - Feb 12 - J. Peter Mugaas
  18. started this client
  19. This is the Ident client which is based on RFC 1413.
  20. }
  21. const
  22. IdIdentQryTimeout = 60000;
  23. type
  24. EIdIdentException = class(EIdException);
  25. EIdIdentReply = class(EIdIdentException);
  26. EIdIdentInvalidPort = class(EIdIdentReply);
  27. EIdIdentNoUser = class(EIdIdentReply);
  28. EIdIdentHiddenUser = class(EIdIdentReply);
  29. EIdIdentUnknownError = class(EIdIdentReply);
  30. EIdIdentQueryTimeOut = class(EIdIdentReply);
  31. TIdIdent = class(TIdTCPClient)
  32. protected
  33. FQueryTimeOut : Integer;
  34. FReplyString : String;
  35. function GetReplyCharset: String;
  36. function GetReplyOS: String;
  37. function GetReplyOther: String;
  38. function GetReplyUserName: String;
  39. function FetchUserReply : String;
  40. function FetchOS : String;
  41. Procedure ParseError;
  42. public
  43. Constructor Create(AOwner : TComponent); override;
  44. Procedure Query(APortOnServer, APortOnClient : Word);
  45. Property Reply : String read FReplyString;
  46. Property ReplyCharset : String read GetReplyCharset;
  47. Property ReplyOS : String read GetReplyOS;
  48. Property ReplyOther : String read GetReplyOther;
  49. Property ReplyUserName : String read GetReplyUserName;
  50. published
  51. property QueryTimeOut : Integer read FQueryTimeOut write FQueryTimeOut default IdIdentQryTimeout;
  52. Property Port default IdPORT_AUTH;
  53. end;
  54. implementation
  55. uses IdGlobal, IdResourceStrings, SysUtils;
  56. const IdentErrorText : Array[0..3] of string =
  57. ('INVALID-PORT', 'NO-USER', 'HIDDEN-USER', 'UNKNOWN-ERROR'); {Do not Localize}
  58. { TIdIdent }
  59. constructor TIdIdent.Create(AOwner: TComponent);
  60. begin
  61. inherited;
  62. FQueryTimeOut := IdIdentQryTimeout;
  63. Port := IdPORT_AUTH;
  64. end;
  65. function TIdIdent.FetchOS: String;
  66. var Buf : String;
  67. begin
  68. Buf := FetchUserReply;
  69. Result := Trim(Fetch(Buf,':')); {Do not Localize}
  70. end;
  71. function TIdIdent.FetchUserReply: String;
  72. var Buf : String;
  73. begin
  74. Result := ''; {Do not Localize}
  75. Buf := FReplyString;
  76. Fetch(Buf,':'); {Do not Localize}
  77. if UpperCase(Trim(Fetch(Buf,':'))) = 'USERID' then {Do not Localize}
  78. Result := TrimLeft(Buf);
  79. end;
  80. function TIdIdent.GetReplyCharset: String;
  81. var Buf : String;
  82. begin
  83. Buf := FetchOS;
  84. if (Length(Buf) > 0) and (Pos(',',Buf)>0) then {Do not Localize}
  85. begin
  86. Result := Trim(Fetch(Buf,',')); {Do not Localize}
  87. end
  88. else
  89. Result := 'US-ASCII'; {Do not Localize}
  90. end;
  91. function TIdIdent.GetReplyOS: String;
  92. var Buf : String;
  93. begin
  94. Buf := FetchOS;
  95. if Length(Buf) > 0 then
  96. begin
  97. Result := Trim(Fetch(Buf,',')); {Do not Localize}
  98. end
  99. else
  100. Result := ''; {Do not Localize}
  101. end;
  102. function TIdIdent.GetReplyOther: String;
  103. var Buf : String;
  104. begin
  105. if FetchOS = 'OTHER' then {Do not Localize}
  106. begin
  107. Buf := FetchUserReply;
  108. Fetch(Buf,':'); {Do not Localize}
  109. Result := TrimLeft(Buf);
  110. end;
  111. end;
  112. function TIdIdent.GetReplyUserName: String;
  113. var Buf : String;
  114. begin
  115. if FetchOS <> 'OTHER' then {Do not Localize}
  116. begin
  117. Buf := FetchUserReply;
  118. {OS ID}
  119. Fetch(Buf,':'); {Do not Localize}
  120. Result := TrimLeft(Buf);
  121. end;
  122. end;
  123. procedure TIdIdent.ParseError;
  124. var Buf : String;
  125. begin
  126. Buf := FReplyString;
  127. Fetch(Buf,':'); {Do not Localize}
  128. if Trim(Fetch(Buf,':')) = 'ERROR' then {Do not Localize}
  129. begin
  130. case PosInStrArray(UpperCase(Trim(Buf)),IdentErrorText) of
  131. {Invalid Port}
  132. 0 : Raise EIdIdentInvalidPort.Create(RSIdentInvalidPort);
  133. {No user}
  134. 1 : Raise EIdIdentNoUser.Create(RSIdentNoUser);
  135. {Hidden User}
  136. 2 : Raise EIdIdentHiddenUser.Create(RSIdentHiddenUser)
  137. else {Unknwon or other error}
  138. Raise EIdIdentUnknownError.Create(RSIdentUnknownError);
  139. end;
  140. end;
  141. end;
  142. procedure TIdIdent.Query(APortOnServer, APortOnClient: Word);
  143. var RTO : Boolean;
  144. begin
  145. FReplyString := ''; {Do not Localize}
  146. Connect;
  147. try
  148. WriteLn(IntToStr(APortOnServer)+', '+IntToStr(APortOnClient)); {Do not Localize}
  149. FReplyString := ReadLn('',FQueryTimeOut); {Do not Localize}
  150. {We check here and not return an exception at the moment so we can close our
  151. connection before raising our exception if the read timed out}
  152. RTO := ReadLnTimedOut;
  153. finally
  154. Disconnect;
  155. end;
  156. if RTO then
  157. Raise EIdIdentQueryTimeOut.Create(RSIdentReplyTimeout)
  158. else
  159. ParseError;
  160. end;
  161. end.