Idle Your Script Users @ IRC
I wrote/edited this quickly. Do not comment on my standards, etc, please.
SCAR Code:
program new;
const
IRCPlz = True; // Have SCAR connect and idle at SRL's irc room?
SRLChatPlz = False; // Join #SRL ?
IRCNickname = ''; // The nickname you want to connect to IRC on. Leave it at default if you do not know how to IRC. No spaces can be in it, and this nickname will be public on the IRC, irc.freenode.net #SRL
Botchans = '#SRL,#fakechannel'; // chans to join. seperate by a comma, no space.
var Sock_fd:integer;
procedure SendData(buf: string);
begin
AddToReport('=> ' + buf);
SendConnectionData(Sock_fd, buf + #10 + #13);
end;
procedure scriptterminate;
begin
SendData('QUIT :Leaving');
end;
// IRC Stuff. Thanks to Yakman and LeeLokHin.
var
line: string;
nicke, host, command, channel, args: string;
type
BufferedReader = record
buffer: string;
pos, len: Integer;
usesocket: boolean;
end;
var reader, lineparse: BufferedReader;
procedure FillReaderBuffer(var reader: BufferedReader);
var
buf: string;
begin
try ReadConnectionData(Sock_fd, buf); except end;
try reader.pos:= 1; except end;
try reader.len:= Length(buf); except end;
try reader.buffer:= buf; except end;
end;
function Connect:Integer;
var botnick,botnic:string;
begin
case random(10) of
0: botnick := 'm'+Trim(TrimNumbers(TrimOthers(ircnickname)))+inttostr(random(999)); // So if they are a nub at IRC, it will not allow their username to begin with a number.
1: botnick := 's'+Trim(TrimNumbers(TrimOthers(ircnickname)))+inttostr(random(999));
2: botnick := 'c'+Trim(TrimNumbers(TrimOthers(ircnickname)))+inttostr(random(999));
3: botnick := 'f'+Trim(TrimNumbers(TrimOthers(ircnickname)))+inttostr(random(999));
4: botnick := 'w'+Trim(TrimNumbers(TrimOthers(ircnickname)))+inttostr(random(999));
5: botnick := 't'+Trim(TrimNumbers(TrimOthers(ircnickname)))+inttostr(random(999));
6: botnick := 'g'+Trim(TrimNumbers(TrimOthers(ircnickname)))+inttostr(random(999));
7: botnick := 'x'+Trim(TrimNumbers(TrimOthers(ircnickname)))+inttostr(random(999));
8: botnick := 'z'+Trim(TrimNumbers(TrimOthers(ircnickname)))+inttostr(random(999));
9: botnick := 'i'+Trim(TrimNumbers(TrimOthers(ircnickname)))+inttostr(random(999));
end;
Result := OpenConnection('irc.freenode.net', 8001, 15000);
if(Result < 0)then
begin
Writeln('Error connecting');
Exit;
end;
ClearReport;
SendData('USER ' + BotNick + ' 8 * :' + BotNick);
SendData('NICK '+botnick);
SendData('MODE '+botnick+' +i');
SendData('MODE '+botnick+' +E');
SendData('MODE '+botnick+' +C');
SendData('MODE '+botnick+' +I');
SendData('MODE '+botnick+' +Q');
if SRLChatPlz then
SendData('JOIN '+botchans+',#SRL')
else
SendData('JOIN '+botchans);
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; //skip the char your just read
Exit;
end;
//Didnt find the char, fill the buffer from socket
if(reader.usesocket)then
FillReaderBuffer(reader)
else
Break;
until(False);
//if we get to here, it means we arnt using the socket, and the buffer is empty
Result:= #0; //nul means error
end;
function ReadNextLine(var reader: BufferedReader): string;
var
line: string;
c: char;
begin
try line:= ReadNextSegment(reader, #10, #13); except end;
try
c:= reader.buffer[reader.pos];
except
exit;
end;
//sometimes line breaks come in twos
if(c = #13)or(c = #10)then
reader.pos:= reader.pos + 1;
Result:= line;
end;
function ReadRest(var reader: BufferedReader):string;
begin
Result:= Copy(reader.buffer, reader.pos, reader.len - reader.pos + 1);
reader.pos:= reader.len;
end;
procedure ircwake; //keep alive the connection :D
var watwat: Integer;
begin
SetTimeout(2500, 'ircwake');
//marktime(watwat);
// while timefrommark(watwat) < 200 do
begin
line:= ReadNextLine(reader);
addtoreport('<= ' + line);
lineparse.buffer:= line;
lineparse.pos:= 1;
lineparse.len:= Length(line);
lineparse.usesocket:= false;
if(line[1] = ':')then //we've got a nick and host
begin
lineparse.pos:= 2;
nicke:= ReadNextSegment(lineparse, '!', #0);
host:= ReadNextSegment(lineparse, ' ', #0);
end;
command:= ReadNextSegment(lineparse, ' ', #0);
channel:= ReadNextSegment(lineparse, ' ', #0);
args:= ReadRest(lineparse);
//if(args <> '')then
//if(args[1] = ':')then
//delete(args, 1, 1);
if(command = 'PING')then
begin
line[2] := 'O';
SendData(line);
end else
if(command = 'KICK')then
begin
SendData('JOIN '+botchans);
end else exit;
end;
end;
procedure Main;
begin
Sock_fd := Connect;
if(Sock_fd < 0)then
begin
Writeln('Failed to connect');
Exit;
end;
reader.usesocket:= true;
FillReaderBuffer(reader);
// marktime(watwat);
ircwake;
//FreeConnection(sock_fd);
end;
begin main; while true do wait(10) end.
Put this into the script somewhere. Now, just call Main; before OR after SetupSRL;
Now, your script users are idle in IRC, and it can give you a live-time status on how popular your script is, without SRL-Stats :)
I doubt this will be added into SRL, but it would be cool. Would be nice for RemoteControl, etc.
Enjoy, and comment. :)
~ Harry