home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / IRC client Source / ircle sources / IRCaux.p < prev    next >
Encoding:
Text File  |  1993-07-19  |  6.1 KB  |  308 lines  |  [TEXT/PJMM]

  1. {    ircle - Internet Relay Chat client    }
  2. {    File: IRCaux    }
  3. {    Copyright © 1992 Olaf Titz (s_titz@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 IRCaux;
  20. { utilities }
  21.  
  22. interface
  23. uses
  24.     TCPTypes, TCPStuff, TCPConnections, ApplBase, InputLine, MsgWindows, {}
  25.     IRCGlobals, IRCPreferences;
  26.  
  27. var
  28.     Watch: CursHandle;
  29.  
  30. procedure ServerOK (status: connectionEvent);
  31. { Call this with the result of TCP functions }
  32.  
  33. procedure PutLine (var s: string);
  34. { Send a line to the server }
  35.  
  36. function IsChannel (var s: string): boolean;
  37. { is it a valid channel name? }
  38.  
  39. procedure MakeChannel (var s: string);
  40. { insert # to make a channel name }
  41.  
  42. procedure NextArg (var from, arg: string);
  43. { get next arg out of 'from' into 'arg' }
  44.  
  45. procedure OpenConnection;
  46. { open server connection }
  47.  
  48. procedure UpdateStatusLine;
  49. { Draw the IRC status line }
  50.  
  51. implementation
  52.  
  53. type
  54.     str8 = string[8];
  55.  
  56. var
  57.     ip: longint;
  58.  
  59. procedure ServerOK (status: connectionEvent);
  60.     var
  61.         a, n: integer;
  62.         s: Str255;
  63.     begin
  64.         n := 0;
  65.         case status of
  66.             C_SearchFailed: 
  67.                 begin
  68. {a := S_OFFLINE;}
  69.                 n := E_SFAILED;
  70.             end;
  71.             C_NameSearchFailed: 
  72.                 begin
  73. {a := S_OFFLINE;}
  74.                 n := E_NSFAILED;
  75.             end;
  76.             C_FailedToOpen: 
  77.                 begin
  78. {a := S_OFFLINE;}
  79.                 n := E_OFAILED;
  80.             end;
  81.             C_Closing: 
  82.                 begin
  83. {a := S_OFFLINE;}
  84.                 n := E_CLOSING;
  85.             end;
  86.             C_Closed: 
  87.                 begin
  88. {serverStatus := S_OFFLINE;}
  89.                 n := 0; { this alert is redundant }
  90.             end;
  91.             otherwise
  92.         end;
  93. {serverStatus := a;}
  94.         if n <> 0 then begin
  95.             GetIndString(s, 256, n);
  96.             ParamText(s, '', '', '');
  97.             InitCursor;
  98.             n := Alert(A_SSTAT, nil);
  99.         end;
  100.         UpdateStatusLine;
  101.     end;
  102.  
  103. procedure PutLine (var s: string);
  104.     var
  105.         i, n, oe: integer;
  106.         p: TCPConnectionPtr;
  107.     begin
  108.         n := length(s);
  109.         for i := 1 to n do
  110.             s[i] := ISOEncode^^[s[i]];
  111.         s[n + 1] := chr(10);
  112. {oe := TCPSend(p, @s^[1], ord(s[0]) + 2);}
  113.         GetConnectionTCPC(sSocket, p);
  114.         i := TCPSendAsync(p, @s[1], n + 1, false, @oe);
  115.         if i <> 0 then
  116.             serverStatus := i
  117.         else begin
  118.             repeat
  119.                 ApplRun
  120.             until oe <> inProgress;
  121.             serverStatus := oe;
  122.             if oe <> 0 then
  123.                 UpdateStatusLine;
  124.         end;
  125.     end;
  126.  
  127. function IsChannel (var s: string): boolean;
  128.     begin
  129.         IsChannel := (s[1] = '#') or (s[1] = '&'); { RFC 1459 }
  130.     end;
  131.  
  132. procedure MakeChannel (var s: string);
  133.     begin
  134.         if s[1] <> '#' then
  135.             insert('#', s, 1);
  136.     end;
  137.  
  138. procedure NextArg (var from, arg: string);
  139.     var
  140.         i: integer;
  141.     begin
  142.         i := pos(' ', from);
  143.         if i = 0 then begin
  144.             arg := from;
  145.             from := ''
  146.         end
  147.         else begin
  148.             arg := copy(from, 1, i - 1);
  149.             if from[i + 1] = ':' then
  150.                 i := i + 1;
  151.             delete(from, 1, i);
  152.         end
  153.     end;
  154.  
  155.  
  156. function watchFound (var e: EventRecord): boolean;
  157.     var
  158.         c: CEPtr;
  159.     begin
  160.         c := CEPtr(e.message);
  161.         if c^.connection <> sSocket then begin
  162.             watchFound := false;
  163.             exit(watchFound)
  164.         end
  165.         else
  166.             watchFound := true;
  167.         if c^.event = C_Found then
  168.             ip := c^.value
  169.         else begin
  170.             ip := -1;
  171.             ServerOk(c^.event);
  172.         end;
  173.     end;
  174.  
  175. function watchOpen (var e: EventRecord): boolean;
  176.     var
  177.         c: CEPtr;
  178.     begin
  179.         c := CEPtr(e.message);
  180.         if c^.connection <> sSocket then begin
  181.             watchOpen := false;
  182.             exit(watchOpen)
  183.         end
  184.         else
  185.             watchOpen := true;
  186.         if c^.event = C_Established then
  187.             ip := 1
  188.         else begin
  189.             ip := -1;
  190.             ServerOk(c^.event);
  191.         end;
  192.     end;
  193.  
  194. procedure WaitEvent (p: ProcPtr);
  195.     var
  196.         i: integer;
  197.     begin
  198.         i := ApplTask(p, TCPMsg);
  199.         ip := 0;
  200.         repeat
  201.             ApplRun;
  202.             if flushing then begin
  203.                 flushing := false;
  204.                 ip := -1
  205.             end
  206.         until ip <> 0;
  207.         ApplUNtask(i);
  208.     end;
  209.  
  210. procedure OpenConnection;
  211.     var
  212.         e: integer;
  213.     begin
  214.         if not validPrefs then
  215.             validPrefs := GetPrefs(true);
  216.         if validPrefs and (serverStatus <> 0) then begin
  217.             CurrentNick := default^^.Nick;
  218.             serverStatus := S_LOOKUP;
  219.             flushing := false;
  220.             UpdateStatusLine;
  221.             SetCursor(Watch^^);
  222.             e := FindAddress(sSocket, serverConn, nil);
  223.             if e = 0 then begin
  224.                 ip := 0;
  225.                 WaitEvent(@watchFound);
  226.                 if ip <> -1 then begin
  227.                     serverStatus := S_OPENING;
  228.                     UpdateStatusLine;
  229.                     e := NewActiveConnection(sSocket, 8192, ip, default^^.port, nil);
  230.                     if e = 0 then begin
  231.                         WaitEvent(@watchOpen);
  232.                         if ip <> -1 then begin
  233.                             serverStatus := S_CONN;
  234.                         end
  235.                         else
  236.                             serverOk(C_FailedToOpen)
  237.                     end
  238.                     else
  239.                         serverOk(C_FailedToOpen)
  240.                 end
  241.             end
  242.             else
  243.                 serverOk(C_SearchFailed)
  244.         end;
  245.         InitCursor;
  246.     end;
  247.  
  248.  
  249. function two (n: integer): str8;
  250.     var
  251.         s: str8;
  252.     begin
  253.         s := stringof(n + 100 : 3);
  254.         two := copy(s, 2, 2)
  255.     end;
  256.  
  257. procedure UpdateStatusLine;
  258.     var
  259.         s: string[80];
  260.         s0: string[40];
  261.         sa, s1, s2, s3: string[10];
  262.         d: DateTimeRec;
  263.     begin
  264.         case serverStatus of
  265.             S_OFFLINE: 
  266.                 s := '(Offline)';
  267.             S_LOOKUP: 
  268.                 s := '(Address lookup)';
  269.             S_OPENING: 
  270.                 s := '(opening)';
  271.             S_CONN: 
  272.                 s := CurrentServer;
  273.             connectionClosing: 
  274.                 s := '(closing)';
  275.             otherwise
  276.                 s := stringof('Err(', serverStatus : 1, ')');
  277.         end;
  278.         if (serverStatus = S_CONN) or (serverStatus = S_OPENING) or (serverStatus = S_LOOKUP) then
  279.             DisableItem(GetMHandle(fileMenu), M_F_OPEN)
  280.         else
  281.             EnableItem(GetMHandle(fileMenu), M_F_OPEN);
  282.         if IsAway then
  283.             sa := ' (away)   '
  284.         else
  285.             sa := 'talking to';
  286.         s0 := CurrentTarget;
  287.         if s0 = '' then
  288.             s0 := '(nobody)';
  289.         s0 := concat(s0, '               ');
  290.         if logging then
  291.             s1 := 'Log'
  292.         else
  293.             s1 := '';
  294.         if flushing then
  295.             s2 := 'Flsh'
  296.         else
  297.             s2 := '';
  298.         if NFT > 0 then
  299.             s3 := stringof('FT(', NFT : 1, ')')
  300.         else
  301.             s3 := '';
  302.         GetTime(d);
  303.         s := stringof(CurrentNick : 10, ' talking to', copy(s0, 1, 12) : 13, s : 18, s1 : 4, s2 : 5, s3 : 8, '  ', two(d.hour), ':', two(d.minute), ':', two(d.second));
  304.         StatusLine(s);
  305.     end;
  306.  
  307.  
  308. end.