2004. június 17., csütörtök

Simple useful Irc routines for server protocol


Problem/Question/Abstract:

How to implement some elements of the IRC protocol

Answer:

{-----------------------------------------------------------------------------
Unit Name: IrcStructures

Documentation Date: 19/02/02 23:56:45
Release Date: 8/21/2002
Version: 1.0

Compiler directives:

Purpose:

  Quick framework for keeping track of IRC protocol.

  Was written as a base framework for a simple IRC server

History:

          Copyright 2001 by Stewart Moss
          All rights reserved.
-----------------------------------------------------------------------------}

unit IrcStructures;

interface

type
  recNick = record
    Nick, Email: string;
  end;

type
  RecChannel = record
    ChannelName,
      ChannelTopic: string;
    NumberOfPeople: Integer;
    NickCount: Integer;
    Nicks: array[0..99] of recNick;
  end;

var
  ChanCount: Integer;
  Channels: array[0..99] of RecChannel;

function InitializeChannels: Boolean;

function ChanExist(Channel, Nick: string): Boolean;
function AddChannel(Channel, Topic, Nick: string): Boolean;
function FindChannel(Channel, Nick: string): Integer;
function JoinChannel(Channel, Nick: string): string;

function addNick(ChannelNo: Integer; Nick: string): Boolean;
function NickExists(ChannelNo: Integer; Nick: string): Boolean;

implementation

function InitializeChannels: Boolean;
var
  loop2, loop: integer;
begin
  for loop := 0 to 99 do
  begin
    for loop2 := 0 to 99 do
      Channels[Loop].Nicks[Loop2].Nick := '';
  end;
end;

function ChanExist(Channel, Nick: string): Boolean;
var
  loop: integer;
begin
  Result := False;
  for loop := 0 to ChanCount do
  begin
    if Channel = Channels[Loop].ChannelName then
      Result := true;
  end;
end;

function AddChannel(Channel, Topic, Nick: string): Boolean;
begin
  result := false;
  if ChanExist(Channel, Nick) then
    exit;
  inc(ChanCount);
  Channels[ChanCount].ChannelName := Channel;
  Channels[ChanCount].ChannelTopic := Topic;
  Channels[ChanCount].NumberOfPeople := 0;
  Channels[ChanCount].NickCount := 0;
  result := true;
end;

function FindChannel(Channel, Nick: string): Integer;
var
  loop: integer;
begin
  Result := -1;
  for loop := 0 to ChanCount do
  begin
    if Channel = Channels[Loop].ChannelName then
      Result := Loop;
  end;
end;

function NickExists(ChannelNo: Integer; Nick: string): Boolean;
var
  loop: integer;
begin
  Result := false;
  with Channels[ChannelNo] do
  begin
    for loop := 1 to NickCount do
    begin
      if nicks[Loop - 1].Nick = Nick then
        result := true;
    end;
  end;
end;

function addNick(ChannelNo: Integer; Nick: string): Boolean;
begin
  Result := false;
  if NickExists(ChannelNo, Nick) then
    exit;
  with Channels[ChannelNo] do
  begin
    inc(NickCount);
    nicks[NickCount].Nick := Nick;
  end;
  Result := True;
end;

function JoinChannel(Channel, Nick: string): string;
var
  ChanOfs: Integer;
begin
  Result := 'Could not join ' + Channel;
  ChanOfs := FindChannel(Channel, Nick);
  if ChanOfs = -1 then
    exit;
  with Channels[ChanOfs] do
  begin
    if addNick(ChanOfs, Nick) then
      Result := 'Joined ' + Channel;
  end;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése