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

  1. {    ircle - Internet Relay Chat client    }
  2. {    File: IRCInput    }
  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 IRCInput;
  20. { Handles input from the user and sends messages to the server. }
  21. { Handles lines sent from the server. }
  22. { And handles menu commands. }
  23.  
  24. interface
  25. uses
  26.     TCPTypes, TCPStuff, TCPConnections, Coroutines, ApplBase, MsgWindows, InputLine, {}
  27.     IRCGlobals, IRCaux, IRCPreferences, IRCChannels, IRCCommands, DCC, IRCSComm;
  28.  
  29. procedure InitIRCInput;
  30. { Startup }
  31.  
  32. implementation
  33.  
  34. var
  35.     prevcr: boolean;
  36.  
  37.  
  38. procedure PasteCommand (s: str20);  { Set the input line to a command }
  39.     begin
  40.         s := concat(cmdchar, s);
  41.         SetInputLine(s);
  42.     end;
  43.  
  44. function MenuFILE (var e: EventRecord): boolean;
  45.     var
  46.         i: integer;
  47.         s: string;
  48.     begin
  49.         MenuFILE := true;
  50.         case loword(e.message) of
  51.             M_F_OPEN: 
  52.                 OpenConnection;
  53.             M_F_CLOSE: 
  54.                 if GetWRefCon(FrontWindow) <> 0 then
  55.                     partWindow(FrontWindow);
  56.             M_F_LOG: 
  57.                 begin
  58.                 if logging then begin
  59.                     close(logfile);
  60.                     logging := false
  61.                 end
  62.                 else begin
  63.                     s := NewFileName('Save log to file:');
  64.                     if s <> '' then begin
  65.                         rewrite(logfile, s);
  66.                         logging := true
  67.                     end
  68.                 end;
  69.             end;
  70.             M_F_FLUSH: 
  71.                 begin
  72.                 flushing := true;
  73.                 UpdateStatusLine
  74.             end;
  75.             M_F_PREFS: 
  76.                 begin
  77.                 ValidPrefs := GetPrefs(true);
  78.                 if ValidPrefs then
  79.                     EnableItem(GetMHandle(M_SHCUTS), 0);
  80.             end;
  81.             M_F_QUIT: 
  82.                 begin
  83.                 if serverStatus = 0 then begin
  84.                     if Alert(A_QUIT, nil) <> 1 then
  85.                         exit(menuFILE);
  86.                     s := 'QUIT';
  87.                     HandleCommand(s);    { try a regular exit }
  88.                 end;
  89.                 ApplExit; { Emergency exit - will give 'bad link' as reason }
  90.             end;
  91.         end;
  92.     end;
  93.  
  94. function MenuCOMMANDS (var e: EventRecord): boolean;
  95.     begin
  96.         case loword(e.message) of
  97.             M_CO_JOIN: 
  98.                 PasteCommand('join ');
  99.             M_CO_PART: 
  100.                 PasteCommand('part ');
  101.             M_CO_LIST: 
  102.                 PasteCommand('list ');
  103.             M_CO_WHO: 
  104.                 PasteCommand('who ');
  105.             M_CO_QUERY: 
  106.                 PasteCommand('query ');
  107.             M_CO_WHOIS: 
  108.                 PasteCommand('whois ');
  109.             M_CO_INVITE: 
  110.                 PasteCommand('invite ');
  111.             M_CO_KICK: 
  112.                 PasteCommand('kick ');
  113.             M_CO_AWAY: 
  114.                 PasteCommand('away ');
  115.             M_CO_MSG: 
  116.                 PasteCommand('msg ');
  117.         end;
  118.         MenuCOMMANDS := true
  119.     end;
  120.  
  121.  
  122. function MenuSHCUTS (var e: EventRecord): boolean;
  123.     var
  124.         s: string;
  125.     begin
  126.         if e.message = M_SH_DEFINE then
  127.             GetShortcuts
  128.         else begin
  129.             s := Shortcuts^^[loword(e.message) - M_SH_FIRST];
  130.             if s <> '' then
  131.                 InsertInputLine(s);
  132.         end;
  133.         MenuSHCUTS := true
  134.     end;
  135.  
  136. function MenuFONTS (var e: EventRecord): boolean;
  137.     var
  138.         s: Str255;
  139.         p0: GrafPtr;
  140.         m: MenuHandle;
  141.         i: integer;
  142.     begin
  143.         m := GetMHandle(262);
  144.         case loword(e.message) of
  145.             M_FO_9: 
  146.                 MWDefaultSize := 9;
  147.             M_FO_10: 
  148.                 MWDefaultSize := 10;
  149.             M_FO_12: 
  150.                 MWDefaultSize := 12;
  151.             M_FO_14: 
  152.                 MWDefaultSize := 14;
  153.             otherwise
  154.                 begin
  155.                 GetItem(m, LoWord(e.message), s);
  156.                 GetFNum(s, MWDefaultFont);
  157.             end
  158.         end;
  159.         if e.message < 5 then
  160.             for i := 1 to 4 do
  161.                 CheckItem(m, i, (i = Loword(e.message)))
  162.         else
  163.             for i := 6 to CountMItems(m) do
  164.                 CheckItem(m, i, (i = Loword(e.message)));
  165.         if MWActive <> nil then begin
  166.             GetPort(p0);
  167.             SetPort(MWActive^^.w);
  168.             SetFontSize(MWActive, MWDefaultFont, MWDefaultSize);
  169.             SetPort(p0)
  170.         end;
  171.         MenuFONTS := true;
  172.     end;
  173.  
  174. { Process a typed line as message. }
  175. { This means: convert it to a PRIVMSG command to the current target, }
  176. { i.e. the channel or query of the active window. }
  177. procedure HandleMessage (var s: string);
  178.     var
  179.         c: string;
  180.     begin
  181.         if currentTarget = '' then
  182.             StatusMsg(E_NOTARGET)
  183.         else if CurrentTarget[1] = '(' then
  184.             StatusMsg(E_NOTARGET)
  185.         else if CurrentTarget[1] = DCC_CHAT_PREFIX then
  186.             DCCChatSend(s)
  187.         else begin
  188.             c := concat('> ', s);
  189.             Message(c);
  190.             c := concat('PRIVMSG ', CurrentTarget, ' :', s);
  191.             HandleCommand(c);
  192.             s := '';
  193.         end;
  194.     end;
  195.  
  196. { 'srvHandler' handles lines received from server }
  197. procedure srvHandler (var s: string);
  198.     begin
  199.         if s[0] <> chr(0) then
  200.             if prevcr then
  201.                 ServerCommands(s)
  202.             else
  203.                 MWMessage(lastwindow, s);
  204.     end;
  205.  
  206. { 'InputHandler' process handles input from the user }
  207. procedure InputHandler (var s: string);
  208.     begin
  209.         GetDateTime(idleTime);
  210.         if s[0] <> chr(0) then
  211.             if s[1] = CmdChar then
  212.                 HandleCommand(s)
  213.             else
  214.                 HandleMessage(s);
  215.     end;
  216.  
  217. function watchLine (var e: EventRecord): boolean;
  218.     var
  219.         c: CEPtr;
  220.         s: string;
  221.         nn: longint;
  222.         i, j: integer;
  223.         cr: boolean;
  224.     begin
  225.         c := CEPtr(e.message);
  226.         if c^.connection = sSocket then begin
  227.             watchLine := true;
  228.             if c^.event = C_CharsAvailable then begin
  229.                 nn := 1;
  230.                 i := TCPReceiveUpTo(c^.tcpc, 10, readTimeout, @s[0], 250, nn, cr);
  231.                 j := nn - 1;
  232.                 while (j > 0) and ((s[j] = chr(10)) or (s[j] = chr(13))) do
  233.                     j := pred(j);
  234.                 if j > 0 then begin
  235.                     s[0] := chr(j);
  236.                     for i := 1 to j do
  237.                         s[i] := ISODecode^^[s[i]];
  238.                     srvHandler(s);
  239.                 end;
  240.                 prevcr := cr;
  241.             end
  242.             else
  243.                 serverOk(c^.event)
  244.         end
  245.         else
  246.             watchLine := false;
  247.     end;
  248.  
  249.  
  250. procedure InitIRCInput;
  251.     var
  252.         i: integer;
  253.     begin
  254.         OpenInputLine(@InputHandler);
  255.         i := ApplTask(@MenuFILE, menuMsg + fileMenu);
  256.         i := ApplTask(@MenuCOMMANDS, menuMsg + M_COMMANDS);
  257.         i := ApplTask(@MenuSHCUTS, menuMsg + M_SHCUTS);
  258.         i := ApplTask(@MenuFONTS, menuMsg + M_FONT);
  259.         i := ApplTask(@watchLine, TCPMsg);
  260.     end;
  261.  
  262. end.