2 Copyright (C) 2010 MegaBrutal
4 This unit is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This unit is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 uses SysUtils
, Classes
, SocketUtils
;
28 PRFCReply
= ^TRFCReply
;
32 FReplyText
: TStringList
;
35 destructor Destroy
; override;
36 function Count
: integer;
37 function GetLine(n
: integer): string;
38 function GetNumericCode
: word;
39 procedure Add(Line
: string);
41 procedure SetNumericCode(Code
: word);
42 procedure SetReply(NumericCode
: word; Text: shortstring
);
43 property NumericCode
: word read GetNumericCode
;
44 property ReplyText
: TStringList read FReplyText
;
47 function ReadCommand(Sock
: socket
; var Command
: shortstring
; var Prms
: string): boolean;
48 function ReadResponse(Sock
: socket
; Response
: TRFCReply
): boolean;
49 function SendCommand(Sock
: socket
; Command
: shortstring
): boolean; overload
;
50 function SendCommand(Sock
: socket
; Command
: shortstring
; Prms
: string): boolean; overload
;
51 function SendResponse(Sock
: socket
; Response
: TRFCReply
): boolean;
56 constructor TRFCReply
.Create
;
60 FReplyText
:= TStringList
.Create
;
64 destructor TRFCReply
.Destroy
;
70 function TRFCReply
.Count
: integer;
72 Count
:= FReplyText
.Count
;
75 function TRFCReply
.GetLine(n
: integer): string;
77 GetLine
:= FReplyText
.Strings
[n
];
80 function TRFCReply
.GetNumericCode
: word;
82 GetNumericCode
:= FNumericCode
;
85 procedure TRFCReply
.Add(Line
: string);
90 procedure TRFCReply
.Clear
;
96 procedure TRFCReply
.SetNumericCode(Code
: word);
101 procedure TRFCReply
.SetReply(NumericCode
: word; Text: shortstring
);
103 SetNumericCode(NumericCode
);
105 FReplyText
.Add(Text);
109 function ReadCommand(Sock
: socket
; var Command
: shortstring
; var Prms
: string): boolean;
110 var Line
: string; i
: integer;
114 if SockReadLn(Sock
, Line
) then begin
117 Command
:= Copy(Line
, 1, i
- 1);
118 Prms
:= Copy(Line
, i
+ 1, Length(Line
) - i
);
125 else ReadCommand
:= false;
131 function ReadResponse(Sock
: socket
; Response
: TRFCReply
): boolean;
132 var Line
: string; ReadOK
: boolean;
138 ReadOK
:= SockReadLn(Sock
, Line
);
139 if ReadOK
then Response
.Add(Copy(Line
, 5, Length(Line
) - 4));
140 until (Line
[4] = #32) or (not ReadOK
);
141 if ReadOK
then Response
.SetNumericCode(StrToInt(Copy(Line
, 1, 3)));
142 ReadResponse
:= ReadOK
;
144 ReadResponse
:= false;
148 function SendCommand(Sock
: socket
; Command
: shortstring
): boolean;
151 SendCommand
:= SockWriteLn(Sock
, Command
);
157 function SendCommand(Sock
: socket
; Command
: shortstring
; Prms
: string): boolean;
160 SendCommand
:= SockWriteLn(Sock
, Command
+ #32 + Prms
);
166 function SendResponse(Sock
: socket
; Response
: TRFCReply
): boolean;
172 for i
:= 0 to c
- 2 do SockWriteLn(Sock
, IntToStr(Response
.GetNumericCode
) + '-' + Response
.GetLine(i
));
173 SendResponse
:= SockWriteLn(Sock
, IntToStr(Response
.GetNumericCode
) + #32 + Response
.GetLine(c
- 1));
176 SendResponse
:= false;