home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / rclesrc.10 / ircle sources / IRCCommands.p < prev    next >
Encoding:
Text File  |  1992-09-05  |  6.3 KB  |  274 lines

  1. {    ircle - Internet Relay Chat client    }
  2. {    File: IRCCommands    }
  3. {    Copyright ⌐ 1992 Olaf Titz (s_titz@iravcl.ira.uka.de)    }
  4.  
  5. {    This program is free software; you can redistribute it and/or modify    }
  6. {    it under the terms of the GNU General Public License as published by    }
  7. {    the Free Software Foundation; either version 2 of the License, or    }
  8. {    (at your option) any later version.    }
  9.  
  10. {    This program is distributed in the hope that it will be useful,    }
  11. {    but WITHOUT ANY WARRANTY; without even the implied warranty of    }
  12. {    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    }
  13. {    GNU General Public License for more details.    }
  14.  
  15. {    You should have received a copy of the GNU General Public License    }
  16. {    along with this program; if not, write to the Free Software    }
  17. {    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.    }
  18.  
  19. unit IRCCommands;
  20. { Handles commands typed in by the user }
  21.  
  22. interface
  23. uses
  24.     TCPTypes, TCPStuff, TCPConnections, ApplBase, MsgWindows, {}
  25.     IRCGlobals, IRCaux, IRCPreferences, IRCChannels, IRCHelp, IRCIgnore, DCC;
  26.  
  27. procedure HandleCommand (var s: string);
  28. { Process s as command line }
  29.  
  30. procedure sendCTCP (var t, s: string);
  31. { send CTCP message }
  32.  
  33. procedure RegUser;
  34. { Send the server the first commands to register the user }
  35.  
  36. implementation
  37.  
  38. { This procedure is to be run in the background, to type }
  39. { a file to the current channel. }
  40. procedure TypeCmd;
  41.     var
  42.         s, t: Str255;
  43.         f: text;
  44.     begin
  45.         t := CurrentTarget;
  46.         if t <> '' then begin
  47.             s := OldFileName(concat('Type to ', t, ':'));
  48.             if s <> '' then begin
  49.                 reset(f, s);
  50.                 while not eof(f) do begin
  51.                     readln(f, s);
  52.                     if s <> '' then begin
  53.                         s := concat('MSG ', t, ' ', s);
  54.                         HandleCommand(s);
  55.                     end
  56.                 end;
  57.                 close(f);
  58.             end;
  59.         end
  60.     end;
  61.  
  62. procedure ParseComLine (var l: string; var com: str255; var rest: string);
  63.     var
  64.         i: integer;
  65.         c: char;
  66.     begin
  67.         if l[1] = cmdChar then
  68.             delete(l, 1, 1);
  69.         i := pos(' ', l);
  70.         if i = 0 then begin
  71.             com := copy(l, 1, 255);
  72.             rest := ''
  73.         end
  74.         else begin
  75.             com := copy(l, 1, i - 1);
  76.             while (i <= length(l)) and (l[i] = ' ') do
  77.                 i := succ(i);
  78.             rest := copy(l, i, 255)
  79.         end;
  80.         UprString(com, false);
  81.     end;
  82.  
  83. procedure TranslateCommand (var s: string);
  84. { Translates aliases & processes internal commands }
  85. { Will return an empty string if command already processed }
  86. { Note: valid commands not mentioned here get sent to the server unprocessed anyway. }
  87. { That means that an error message for wrong commands comes always from the server. }
  88.     var
  89.         com: str255;
  90.         rest, s1: string;
  91.         i: integer;
  92.         dd: MWHndl;
  93.     procedure join;
  94.         begin
  95.             if rest = '' then
  96.                 rest := lastInvite;
  97.             s := concat('JOIN ', rest);
  98.         end;
  99.     procedure signoff;
  100.         begin
  101.             if rest = '' then
  102.                 rest := 'Leaving';
  103.             s := concat('QUIT :', rest);
  104.             QuitRequest := true
  105.         end;
  106.     begin
  107.         ParseComLine(s, com, rest);
  108.         if com = 'BYE' then
  109.             signoff
  110.         else if com = 'CHANNEL' then
  111.             join
  112.         else if com = 'CTCP' then begin
  113.             i := pos(' ', rest);
  114.             if i = 0 then begin
  115.                 com := rest;
  116.                 rest := ''
  117.             end
  118.             else begin
  119.                 com := copy(rest, 1, i - 1);
  120.                 delete(rest, 1, i)
  121.             end;
  122.             sendCTCP(com, rest);
  123.             s := ''
  124.         end
  125.         else if com = 'DATE' then
  126.             s := concat('TIME ', rest)
  127.         else if com = 'DCC' then begin
  128.             DCCcommand(rest);
  129.             s := ''
  130.         end
  131.         else if com = 'EXIT' then
  132.             signoff
  133.         else if com = 'HELP' then begin
  134.             ShowHelp;
  135.             ApplRun;
  136.             ApplRun;
  137.             ApplRun;
  138.             s1 := '*** This is the server''s HELP information. For Client Help refer to the Help window';
  139.             LineMsg(s1);
  140.             ApplRun;
  141.             ApplRun
  142.         end
  143.         else if com = 'IGNORE' then begin
  144.             DoIgnore(rest);
  145.             s := ''
  146.         end
  147.         else if com = 'JOIN' then
  148.             join
  149.         else if com = 'LEAVE' then
  150.             s := concat('PART ', rest)
  151.         else if com = 'ME' then begin
  152.             s := concat(CurrentNick, ' ', rest);
  153.             Message(s);
  154.             s := concat('ACTION ', rest);
  155.             sendCTCP(currentTarget, s);
  156.             s := ''
  157.         end
  158.         else if com = 'MSG' then begin
  159.             i := pos(' ', rest);
  160.             s1 := copy(rest, 1, i - 1);
  161.             if IsChannel(s1) then
  162.                 s := concat('> ', s1, copy(rest, i, 255))
  163.             else
  164.                 s := concat('> *', s1, '*', copy(rest, i, 255));
  165.             ChannelMsg(s1, s);
  166.             s := concat('PRIVMSG ', rest);
  167.         end
  168.         else if com = 'NOTICE' then begin
  169.             i := pos(' ', rest);
  170.             s1 := copy(rest, 1, i - 1);
  171.             s := concat('> -', s1, '-', copy(rest, i, 255));
  172.             ChannelMsg(s1, s);
  173.             s := concat('NOTICE ', rest)
  174.         end
  175.         else if com = 'QUERY' then begin
  176.             if rest = '' then begin
  177.                 if lastMSG <> '' then
  178.                     dd := DoJoin(lastMSG)
  179.             end
  180.             else
  181.                 dd := DoJoin(rest);
  182.             s := ''
  183.         end
  184.         else if com = 'QUIT' then
  185.             signoff
  186.         else if com = 'QUOTE' then
  187.             s := rest
  188.         else if com = 'SIGNOFF' then
  189.             signoff
  190.         else if com = 'TYPE' then begin
  191.             i := ApplCoroutine(@TypeCmd, COSPACE);
  192.             s := ''
  193.         end
  194.         else if com = 'WHO' then begin
  195.             if rest = '' then
  196.                 s := concat(com, ' ', CurrentTarget)
  197.             else if rest[1] = '*' then
  198.                 s := concat(com, ' ', CurrentTarget);
  199.         end
  200.         else if com = 'WHOIS' then begin
  201.             if rest = '' then
  202.                 s := concat(com, ' ', lastMSG)
  203.             else
  204.                 s := concat(com, ' ', rest);
  205.         end;
  206.     end;
  207.  
  208.  
  209. procedure sendCTCP (var t, s: string);
  210.     var
  211.         i: integer;
  212.         com: str255;
  213.     begin
  214.         if serverStatus = 0 then begin
  215.             i := pos(' ', s);
  216.             if i = 0 then begin
  217.                 com := s;
  218.                 s := ''
  219.             end
  220.             else begin
  221.                 com := copy(s, 1, i - 1);
  222.                 delete(s, 1, i);
  223.             end;
  224.             UprString(com, false);
  225.             s := concat('PRIVMSG ', t, ' ', chr(1), com, ' ', s, chr(1));
  226.             PutLine(s);
  227.         end
  228.         else
  229.             StatusMsg(E_NOSERVER);
  230.     end;
  231.  
  232. procedure HandleCommand (var s: string);
  233.     begin
  234.         if serverStatus = 0 then begin
  235.             flushing := false;
  236.             UpdateStatusLine;
  237.             TranslateCommand(s);
  238.             if s <> '' then begin
  239.                 PutLine(s);
  240.                 s := ''
  241.             end
  242.         end
  243.         else
  244.             StatusMsg(E_NOSERVER);
  245.     end;
  246.  
  247. procedure RegUser;
  248.     var
  249.         s0, s: string;
  250.         i: integer;
  251.     begin
  252.         CurrentServer := ''; { server will respond with NOTICE }
  253. { This is used to determine server name }
  254.         s := concat('NICK ', currentNick);
  255.         HandleCommand(s);
  256.         s0 := default^^.userLoginName;
  257.         i := pos('@', s0);
  258.         if i > 0 then
  259.             s := concat('USER ', copy(s0, 1, i - 1), ' ', copy(s0, i + 1, 255), ' . :', default^^.username)
  260.         else
  261.             s := concat('USER ', s0, ' . . :', default^^.username);
  262.         HandleCommand(s);
  263.         s0 := default^^.autoExec;
  264.         while s0 <> '' do begin
  265.             i := pos(';', s0);
  266.             if i = 0 then
  267.                 i := 255;
  268.             s := copy(s0, 1, i - 1);
  269.             HandleCommand(s);
  270.             delete(s0, 1, i)
  271.         end;
  272.     end;
  273.  
  274. end.