All pastes #612418 Raw Edit

SCAR irc

public text v1 · immutable
·published 2007-07-10 12:03 UTC
rendered paste body
////////////////////////////////////////////////////////////////////////////////
//                            HexKey, Secks now plx                           //
////////////////////////////////////////////////////////////////////////////////
{.include StringLib.scar}
  const
    TYPE_MODE = 1; // MODE event
    TYPE_NICK = 2; // NICK event
    TYPE_JOIN = 3; // JOIN event
    TYPE_PART = 4; // PART event
    TYPE_QUIT = 5; // QUIT event
    TYPE_TOPIC = 6; // TOPIC EVENT
    TYPE_PRIVMSG = 7; // PRIVMSG EVENT
    TYPE_NOTICE = 8; // NOTICE EVENT
    TYPE_CTCP = 9; // Is CTCP just PRIVMSG? I cbf'd to open my java lib.
    TYPE_DISCONNECT = 10; // We've been disconnected.
    TYPE_ERROR = 11; // There was some internal error.

  type IRCEvent = record
      id: Integer;
      serverMessageId: Integer;
      nick, host, name, content, recipient, target: String;
  end;

  type IRCConnection = record
    host, nick, name, login, fullname, linebuffer: String;
    sock, port: Integer;
    events: array of IRCEvent;
  end;

  procedure fireEvent(var con: IRCConnection; e: IRCEvent);
  var
    size: Integer;
  begin;
    size:= GetArrayLength(con.events);
    SetArrayLength(con.events, size + 1);
    con.events[size]:= e;
  end;

  procedure writeLine(var con: IRCConnection; line: String);
  var
    error: IRCEvent;
  begin;
    if(con.sock = -1) then begin
      error.id:= TYPE_ERROR;
      error.content:= 'Could not send line: Not connected to server';
      fireEvent(con, error);
    end else
      SendConnectionData(con.sock, line + Chr(13) + Chr(10));
  end;

  function connect(host: String; port: Integer; nick, name, login, fullname: String): IRCConnection;
  var
    error: IRCEvent;
  begin;
    Result.host:= host;
    Result.port:= port;
    Result.sock:= OpenConnection(host,port, 1000);
    if not (Result.sock = -1) then begin
      Result.nick:= nick;
      Result.name:= name;
      Result.login:= login;
      Result.fullname:= fullname;
      writeLine(Result, 'USER ' + name + ' ' + login + ' * :' + fullname);
      writeLine(Result, 'NICK ' + nick);
    end else begin
      error.id:= TYPE_ERROR;
      error.content:= 'Could not connect to ' + host + ':' + IntToStr(port);
      fireEvent(Result, error);
    end;
  end;

  procedure handleCrap(con: IRCConnection);
  var
    s: String;
    event: IRCEvent;
    idx, x : Integer;
    lines: Array of String;
  begin
    if ReadConnectionData(con.sock, s) then begin
      con.linebuffer:= con.linebuffer + s;
      idx:= lastIndexOf(Chr(13) + Chr(10), con.linebuffer);
      if idx > -1 then begin
        s:= copy(con.linebuffer, 0, idx);
        con.linebuffer:= copy(con.linebuffer, idx, 2);
        lines:= split(Chr(13) + Chr(10), s);
        for x:= 0 to GetArrayLength(lines) - 1 do
          writeln('Line:' + lines[x]);
      end;
    end else begin
      con.sock:= -1;
      event.id:= TYPE_ERROR;
      event.content:= 'Could not read from ' + con.host + ':' + IntToStr(con.port);
      fireEvent(con, event);
    end;
  end;

  // Test library
  var
    con: IRCConnection;
  begin
    con:= connect('irc.freenode.net', 6667, 'SCARIRC', 'Imkul', 'lolr', 'Made with love <3');
    if(GetArrayLength(con.events) > 0) then begin
      writeln(con.events[0].content);
      terminateScript;
    end;
    writeLine(con, 'JOIN #lungexpress');
    while con.sock > -1 do
      handleCrap(con);
  end.