home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Applications / ircle 1.5.1 / source / ircle sources / IRCInput.p < prev    next >
Encoding:
Text File  |  1993-11-15  |  8.4 KB  |  381 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, MiscGlue, MsgWindows,{}
  27.     InputLine, IRCGlobals, IRCaux, IRCPreferences, IRCChannels, IRCCommands, {}
  28.     IRCNotify, IRCIgnore, DCC, IRCSComm;
  29.  
  30. procedure InitIRCInput;
  31. { Startup }
  32.  
  33. implementation
  34.  
  35. var
  36.     prevcr: boolean;
  37.  
  38. procedure LoadCommandFile;
  39.     var
  40.         s: string;
  41.         f: text;
  42.     begin
  43.         s := OldFileName('Load commands from:');
  44.         if s <> '' then begin
  45.             reset(f, s);
  46.             while not eof(f) do begin
  47.                 readln(f, s);
  48.                 HandleCommand(s)
  49.             end;
  50.             close(f);
  51.         end
  52.     end;
  53.  
  54. procedure SaveSet;
  55.     var
  56.         i, e: integer;
  57.         s: str255;
  58.         f: text;
  59.         w: MWHndl;
  60.     procedure writewindows (wl: string; com: str20);
  61.         begin
  62.             while wl <> '' do begin
  63.                 NextArg(wl, s);
  64.                 w := ChannelWindow(s);
  65.                 if w <> nil then
  66.                     with w^^.w^.portRect, w^^.w^.portBits do
  67.                         writeln(f, 'WINDOW', -bounds.left, -bounds.top, right - bounds.left, bottom - bounds.top);
  68.                 writeln(f, com, s);
  69.             end;
  70.         end;
  71.     begin
  72.         s := NewFileName('Save Set as:', currentNick);
  73.         if s = '' then
  74.             exit(SaveSet);
  75.         SetCursor(Watch^^);
  76.         e := Create(s, 0, CREATOR_IRCLE, TYPE_PREFS);
  77.         if e = dupFnErr then begin
  78.             e := FSDelete(s, 0);
  79.             if e = 0 then
  80.                 e := Create(s, 0, CREATOR_IRCLE, TYPE_PREFS);
  81.         end;
  82.         if e = 0 then begin
  83.             open(f, s);
  84.             writeln(f, 'NICK ', currentNick);
  85.             writeln(f, 'USERNAME ', default^^.userName);
  86.             writeln(f, 'USERINFO ', default^^.userLoginName);
  87.             writeln(f, 'FONT', MWDefaultFont, MWDefaultSize);
  88.             s := '0000';
  89.             for i := 1 to 4 do
  90.                 if default^^.notify[i] then
  91.                     s[i] := '1';
  92.             writeln(f, 'USERNOTIFY ', s);
  93.             for i := 1 to 10 do
  94.                 if shortcuts^^[i] <> '' then begin
  95.                     if i = 10 then
  96.                         e := 0
  97.                     else
  98.                         e := i;
  99.                     writeln(f, 'SHORTCUT', e, ' ', shortcuts^^[i]);
  100.                 end;
  101.             writeln(f, 'SERVER ', default^^.Server, default^^.Port);
  102.             GetAllWindows(true, false, false, ' ', s);
  103.             writewindows(s, 'JOIN ');
  104.             GetAllWindows(false, true, false, ' ', s);
  105.             writewindows(s, 'QUERY ');
  106.             GetNotifyList(s);
  107.             if s <> '' then
  108.                 writeln(f, 'NOTIFY ', s);
  109.             GetIgnoreList(s);
  110.             if s <> '' then
  111.                 writeln(f, 'IGNORE ', s);
  112.             close(f);
  113.             InitCursor;
  114.             dirtyPrefs := false
  115.         end
  116.         else begin
  117.             NumToString(e, s);
  118.             InitCursor;
  119.             ParamText(s, '', '', '');
  120.             e := Alert(A_FERR, nil)
  121.         end;
  122.     end;
  123.  
  124.  
  125. procedure PasteCommand (s: str20; s2: string);  { Set the input line to a command }
  126.     begin
  127.         s := concat(cmdchar, s);
  128.         SetInputLine(s);
  129.         if s2 <> '' then
  130.             InsertInputLine(s2, true);
  131.     end;
  132.  
  133. function MenuFILE (var e: EventRecord): boolean;
  134.     var
  135.         i: integer;
  136.         s: string;
  137.     begin
  138.         MenuFILE := true;
  139.         case loword(e.message) of
  140.             M_F_OPEN: 
  141.                 if ValidPrefs then begin
  142.                     OpenConnection;
  143.                     if serverStatus = S_CONN then
  144.                         RegUser;
  145.                 end
  146.                 else
  147.                     StatusMsg(E_NOPREFS);
  148.             M_F_CLOSE: 
  149.                 if GetWRefCon(FrontWindow) <> 0 then
  150.                     partWindow(FrontWindow);
  151.             M_F_LOAD: 
  152.                 begin
  153.                 LoadCommandFile
  154.             end;
  155.             M_F_SAVE: 
  156.                 begin
  157.                 SaveSet
  158.             end;
  159.             M_F_PREFS: 
  160.                 begin
  161.                 GetPrefs
  162.             end;
  163.             M_F_LOG: 
  164.                 begin
  165.                 if logging then begin
  166.                     close(logfile);
  167.                     logging := false
  168.                 end
  169.                 else begin
  170.                     s := NewFileName('Save log to file:');
  171.                     if s <> '' then begin
  172.                         rewrite(logfile, s);
  173.                         logging := true
  174.                     end
  175.                 end;
  176.             end;
  177.             M_F_FLUSH: 
  178.                 begin
  179.                 flushing := true;
  180.                 UpdateStatusLine
  181.             end;
  182.             M_F_QUIT: 
  183.                 begin
  184.                 if serverStatus = S_CONN then begin
  185.                     if Alert(A_QUIT, nil) <> 1 then
  186.                         exit(menuFILE);
  187.                     s := 'QUIT';
  188.                     HandleCommand(s);    { try a regular exit }
  189.                 end;
  190.                 ApplExit; { Emergency exit - will give 'bad link' as reason }
  191.             end;
  192.         end;
  193.     end;
  194.  
  195. function MenuCOMMANDS (var e: EventRecord): boolean;
  196.     begin
  197.         case loword(e.message) of
  198.             M_CO_JOIN: 
  199.                 PasteCommand('join ', lastInvite);
  200.             M_CO_PART: 
  201.                 begin
  202.                 if IsChannel(CurrentTarget) then
  203.                     PasteCommand('part ', currentTarget)
  204.                 else
  205.                     PasteCommand('part ', '');
  206.             end;
  207.             M_CO_LIST: 
  208.                 PasteCommand('list ', '');
  209.             M_CO_WHO: 
  210.                 PasteCommand('who ', '');
  211.             M_CO_QUERY: 
  212.                 PasteCommand('query ', lastMsg);
  213.             M_CO_WHOIS: 
  214.                 PasteCommand('whois ', lastMsg);
  215.             M_CO_INVITE: 
  216.                 PasteCommand('invite ', '');
  217.             M_CO_KICK: 
  218.                 PasteCommand('kick ', '');
  219.             M_CO_AWAY: 
  220.                 PasteCommand('away ', '');
  221.             M_CO_MSG: 
  222.                 PasteCommand('msg ', '');
  223.         end;
  224.         MenuCOMMANDS := true
  225.     end;
  226.  
  227.  
  228. function MenuSHCUTS (var e: EventRecord): boolean;
  229.     var
  230.         s: string;
  231.         i: integer;
  232.     begin
  233.         if e.message = M_SH_DEFINE then
  234.             GetShortcuts
  235.         else begin
  236.             s := Shortcuts^^[loword(e.message) - M_SH_FIRST];
  237.             if s <> '' then begin
  238.                 repeat
  239.                     i := pos('\n', s);
  240.                     if i = 0 then
  241.                         leave;
  242.                     delete(s, i, 1);
  243.                     s[i] := chr(13);
  244.                 until false;
  245.                 InsertInputLine(s, false);
  246.             end;
  247.         end;
  248.         MenuSHCUTS := true
  249.     end;
  250.  
  251. function MenuFONTS (var e: EventRecord): boolean;
  252.     var
  253.         s: Str255;
  254.         p0: GrafPtr;
  255.         m: MenuHandle;
  256.         i: integer;
  257.     begin
  258.         m := GetMHandle(M_FONT);
  259.         case loword(e.message) of
  260.             M_FO_9: 
  261.                 MWDefaultSize := 9;
  262.             M_FO_10: 
  263.                 MWDefaultSize := 10;
  264.             M_FO_12: 
  265.                 MWDefaultSize := 12;
  266.             M_FO_14: 
  267.                 MWDefaultSize := 14;
  268.             otherwise
  269.                 begin
  270.                 GetItem(m, LoWord(e.message), s);
  271.                 GetFNum(s, MWDefaultFont);
  272.             end
  273.         end;
  274.         AdjustFontMenu;
  275.         if MWActive <> nil then begin
  276.             GetPort(p0);
  277.             SetPort(MWActive^^.w);
  278.             SetFontSize(MWActive, MWDefaultFont, MWDefaultSize);
  279.             SetPort(p0)
  280.         end;
  281.         MenuFONTS := true;
  282.     end;
  283.  
  284. { Process a typed line as message. }
  285. { This means: convert it to a PRIVMSG command to the current target, }
  286. { i.e. the channel or query of the active window. }
  287. procedure HandleMessage (var s: string);
  288.     var
  289.         c: string;
  290.     begin
  291.         if currentTarget = '' then
  292.             StatusMsg(E_NOTARGET)
  293.         else if CurrentTarget[1] = '(' then
  294.             StatusMsg(E_NOTARGET)
  295.         else if CurrentTarget[1] = DCC_CHAT_PREFIX then
  296.             DCCChatSend(s)
  297.         else begin
  298.             c := concat('> ', s);
  299.             Message(c);
  300.             c := concat('PRIVMSG ', CurrentTarget, ' :', s);
  301.             HandleCommand(c);
  302.             s := '';
  303.         end;
  304.     end;
  305.  
  306. { 'srvHandler' handles lines received from server }
  307. procedure srvHandler (var s: string);
  308.     begin
  309.         if s <> '' then
  310.             if prevcr then
  311.                 ServerCommands(s)
  312.             else
  313.                 MWMessage(lastwindow, s);
  314.     end;
  315.  
  316. { 'InputHandler' process handles input from the user }
  317. procedure InputHandler (var s: string);
  318.     begin
  319.         GetDateTime(idleTime);
  320.         if s <> '' then
  321.             if s[1] = CmdChar then
  322.                 HandleCommand(s)
  323.             else
  324.                 HandleMessage(s);
  325.     end;
  326.  
  327. function watchLine (var e: EventRecord): boolean;
  328.     var
  329.         c: CEPtr;
  330.         s: string;
  331.         nn: longint;
  332.         i, j: integer;
  333.         cr: boolean;
  334.     begin
  335.         c := CEPtr(e.message);
  336.         if c^.connection = sSocket then begin
  337.             watchLine := true;
  338.             if c^.event = C_CharsAvailable then begin
  339.                 nn := 1;
  340.                 i := TCPReceiveUpTo(c^.tcpc, 10, readTimeout, @s[0], 250, nn, cr);
  341.                 j := nn - 1;
  342.                 while (j > 0) and ((s[j] = chr(10)) or (s[j] = chr(13))) do
  343.                     j := pred(j);
  344.                 if j > 0 then begin
  345.                     s[0] := chr(j);
  346.                     for i := 1 to j do
  347.                         s[i] := ISODecode^^[s[i]];
  348.                     srvHandler(s);
  349.                 end;
  350.                 prevcr := cr;
  351.             end
  352.             else
  353.                 serverOk(c^.event)
  354.         end
  355.         else
  356.             watchLine := false;
  357.     end;
  358.  
  359. procedure DoSave;
  360.     begin
  361.         while dirtyPrefs do
  362.             if Alert(A_SAVE, nil) = ok then
  363.                 SaveSet { may loop if canceled here! }
  364.             else
  365.                 dirtyPrefs := false;
  366.     end;
  367.  
  368. procedure InitIRCInput;
  369.     var
  370.         i: integer;
  371.     begin
  372.         OpenInputLine(@InputHandler);
  373.         i := ApplTask(@MenuFILE, menuMsg + fileMenu);
  374.         i := ApplTask(@MenuCOMMANDS, menuMsg + M_COMMANDS);
  375.         i := ApplTask(@MenuSHCUTS, menuMsg + M_SHCUTS);
  376.         i := ApplTask(@MenuFONTS, menuMsg + M_FONT);
  377.         i := ApplTask(@watchLine, TCPMsg);
  378.         ApplExitproc(@DoSave);
  379.     end;
  380.  
  381. end.