Ok, I have the include:
Type
BufferedReader = Record
Buffer : String;
Pos, Len: Integer;
UseSocket: Boolean;
End;
Var
Line : String;
Sock_Fd : Integer;
Reader : BufferedReader;
Questions, Answers : TStringArray;
Lines : TStringArray;
Const
BotChan = '#answerme';
Function GenerateName: String;
Var
I : Integer;
Begin
For I := 0 To 4 Do
Result := Result + Chr(RandomRange(97, 122));
Result := Result + IntToStr(Random(1000) + 1);
End;
Procedure DBug(S : String; T : Integer);
Begin
Case T Of
1: Writeln('- ' + S);
2: Writeln('=> ' + S);
3: Writeln('<= ' + S);
End;
End;
Procedure SendData(Buf : String);
Begin
DBug(Buf, 2);
SendConnectionData(Sock_Fd, Buf + #10 + #13);
end;
Procedure FillReaderBuffer(Var Reader : BufferedReader);
Var
Buf : String;
Begin
Try
ReadConnectionData(Sock_Fd, Buf);
Reader.Pos := 1;
Reader.Len:= Length(Buf);
Reader.Buffer:= Buf;
Except End;
End;
function Connect : Integer;
Var
Name : String;
begin
Result := OpenConnection('irc.rizon.net', 6669, 15000);
if(Result < 0)then
begin
DBug('Error connecting', 1);
Exit;
end;
ClearReport;
Name := GenerateName;
Dbug('Chosen name: ' + Name, 1);
SendData('USER ' + Name + ' 8 * :' + Name);
SendData('NICK ' + Name);
SendData('MODE ' + Name + ' +i');
SendData('MODE ' + Name + ' +E');
SendData('MODE ' + Name + ' +C');
SendData('MODE ' + Name + ' +I');
SendData('MODE ' + Name + ' +Q');
SendData('JOIN ' + BotChan);
end;
function ReadNextSegment(var reader: BufferedReader; c1, c2: char): string;
var
f: Integer;
begin
repeat
for f := reader.pos to reader.len do
if (reader.buffer[f] = c1) or (reader.buffer[f] = c2) then
begin
Result:= Copy(reader.buffer, reader.pos, f - reader.pos);
reader.pos:= f + 1;
Exit;
end;
if (reader.usesocket) then
FillReaderBuffer(reader)
else
Break;
until(False);
Result:= #0;
end;
function ReadNextLine(var reader: BufferedReader): string;
var
line: string;
c: char;
begin
try
line:= ReadNextSegment(reader, #10, #13);
c := reader.buffer[reader.pos-1];
except
Writeln('Error number ' + IntToStr(ExceptionPos) + ', procedure number ' + IntToStr(ExceptionProc) + ', Reason: ' + ExceptionToString(ExceptionType, ExceptionParam));
exit;
end;
if (c = #13) or (c = #10) then
reader.pos := reader.pos + 1;
Result := line;
end;
Function GetMsg(L : String): String;
Begin
Result := Right(L, (Length(L) - Pos('PRIVMSG ' + BotChan + ' :', L)) - Length('RIVMSG ' + BotChan + ' :'))
End;
Function GetCommand(L : String): String;
Var
S : String;
Begin
S := GetMsg(L);
If Pos('.', S) = 0 Then
Exit;
Result := Between('.', ' ', S);
If Result = '' Then
Result := Right(S, Length(S) - Pos('.', S));
End;
Function GetArgument(L : String): String;
Var
S, SS : String;
Begin
S := GetCommand(L);
SS := GetMsg(L);
If Pos(S, SS) + Length(S) = Length(SS) Then
Exit;
Result := Right(SS, (Length(SS) - Pos(S, SS)) - (Length(S) - 1));
Result := Right(Result, Length(Result) - 1);
End;
Procedure IRCWake;
Var
Msg, S : String;
Begin
Line := ReadNextLine(Reader);
DBug(Line, 3);
If Pos('PING', Line) > 0 Then
Begin
SendData('PONG :' + Right(Line, Length(Line) - Pos(':', Line)));
Exit;
End;
Msg := GetMsg(Line);
If Msg = '' Then
Exit;
If Msg[1] = '.' Then
If Pos(':', Line) > 0 Then
If Pos('PRIVMSG ' + BotChan, Line) > 0 Then
Begin
S := GetCommand(Line);
Case StrToIntDef(S, -2) Of
Low(Questions)..High(Questions): Answers[StrToInt(S)] := GetArgument(Line);
{More commands here}
End;
End;
End;
Function AutoRespond: Boolean;
Var
Temp : TStringArray;
Diffs : TIntegerArray;
H, I : Integer;
Begin
If Length(Answers) > 0 Then
Begin
Result := True;
For H := 0 To High(Answers) Do
Begin
If Answers[H] = '' Then
Continue;
TypeSend(Answers[H]);
End;
SetLength(Answers, 0);
SetLength(Questions, 0);
Exit;
End;
Temp := ChatBoxTextArray(ClBlue);
For I := 0 To 7 Do
If Lines[I] <> Temp[I] Then
Begin
SetLength(Diffs, Length(Diffs) + 1);
Diffs[High(Diffs)] := I;
End;
If Length(Diffs) = 0 Then
Exit;
SetLength(Questions, Length(Diffs));
SetLength(Answers, Length(Diffs));
H := High(Questions);
For I := 0 To H Do
SendData('PRIVMSG ' + BotChan + ' :' + IntToStr(H) + '.' + Questions[Diffs[H]]);
End;
Procedure SetupAutoResponder;
Begin
Sock_fd := Connect;
if(Sock_fd < 0)then
begin
DBug('Failed to connect', 1);
Exit;
end;
Reader.UseSocket := True;
FillReaderBuffer(reader);
Wait(2500);
SetTimeOut(30, 'IRCWake');
End;
But it has problems:
- Gives runtime error because of ChatBoxTextArray(don't know why).
- It doesn't have any question detector. It'll send to channel every new line it gets.
Also, Rizon KLined me after I tried a few times because they say I was 'hammering' their connections.