chat/copypaste: some adjustments and sanity checks
authorsheepluva
Sun, 08 Mar 2015 01:51:27 +0100
changeset 10851 f25dad9c3136
parent 10850 c76ea22ea249
child 10852 9e5763cb805e
chat/copypaste: some adjustments and sanity checks
QTfrontend/game.cpp
hedgewars/uChat.pas
hedgewars/uIO.pas
--- a/QTfrontend/game.cpp	Sun Mar 08 01:04:41 2015 +0100
+++ b/QTfrontend/game.cpp	Sun Mar 08 01:51:27 2015 +0100
@@ -213,18 +213,6 @@
             SendIPC("!");
             break;
         }
-        case 'Y':
-        {
-            // copy string to clipboard
-            QApplication::clipboard()->setText(QString::fromUtf8(msg.mid(2)));
-            break;
-        }
-        case 'P':
-        {
-            // paste clipboard to game
-            SendIPC(QString("P").toAscii() + QApplication::clipboard()->text().toUtf8().left(254).replace('\n', ' '));
-            break;
-        }
         case 'C':
         {
             switch (gameType)
@@ -273,6 +261,18 @@
                 .arg(QString::fromUtf8(msg.mid(2).left(size - 4))));
             return;
         }
+        case 'y':
+        {
+            // copy string to clipboard
+            QApplication::clipboard()->setText(QString::fromUtf8(msg.mid(2)));
+            break;
+        }
+        case 'Y':
+        {
+            // paste clipboard to game
+            SendIPC(QString("Y").toAscii() + QApplication::clipboard()->text().toUtf8().left(250).replace('\n', ' '));
+            break;
+        }
         case 'i':
         {
             emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3)));
--- a/hedgewars/uChat.pas	Sun Mar 08 01:04:41 2015 +0100
+++ b/hedgewars/uChat.pas	Sun Mar 08 01:51:27 2015 +0100
@@ -36,6 +36,7 @@
 uses SDLh, uInputHandler, uTypes, uVariables, uCommands, uUtils, uTextures, uRender, uIO, uScript, uRenderUtils;
 
 const MaxStrIndex = 27;
+      MaxInputStrLen = 240;
 
 type TChatLine = record
     Tex: PTexture;
@@ -198,6 +199,7 @@
     begin
     cl.s:= str;
     color:= colors[#6];
+    // TODO FIX render InputLinePrefix seperately so that it doesn't mess up max len
     str:= InputLinePrefix + str + ' ';
     end
 else
@@ -668,7 +670,7 @@
 
 procedure CopyToClipboard(var newContent: shortstring);
 begin
-    SendIPC(_S'Y' + copy(newContent, 1, 253) + #0);
+    SendIPC(_S'y' + copy(newContent, 1, 253) + #0);
 end;
 
 procedure CopySelectionToClipboard();
@@ -683,9 +685,10 @@
 
 // TODO: honor utf8, don't break utf8 chars when shifting chars beyond limit
 procedure InsertIntoInputStr(var s: shortstring);
-var i, l, lastc: integer;
+var i, l, il, lastc: integer;
 begin
-    l:= Length(s);
+    // safe length for string
+    l:= min(MaxInputStrLen-cursorPos, Length(s));
 
     // if we insert rather than append, shift info in InputStrL accordingly
     if cursorPos < Length(InputStr.s) then
@@ -694,25 +697,32 @@
             begin
             if InputStrL[i] <> InputStrLNoPred then
                 begin
-                InputStrL[i+l]:= InputStrL[i] + l;
+                il:= i + l;
+                // only shift if not overflowing
+                if il <= MaxInputStrLen then
+                    InputStrL[il]:= InputStrL[i] + l;
                 InputStrL[i]:= InputStrLNoPred;
                 end;
             end;
         end;
 
     InputStrL[cursorPos + l]:= cursorPos;
-    Insert(s, InputStr.s, cursorPos + 1);
+    // insert string truncated to safe length
+    Insert(copy(s,1,l), InputStr.s, cursorPos + 1);
+    if Length(InputStr.s) > MaxInputStrLen then
+        InputStr.s[0]:= char(MaxInputStrLen);
+
     SetLine(InputStr, InputStr.s, true);
 
     // move cursor to end of inserted string
-    lastc:= 255;
+    lastc:= MaxInputStrLen;
     cursorPos:= min(lastc, cursorPos + l);
     UpdateCursorCoords();
 end;
 
 procedure PasteFromClipboard();
 begin
-    SendIPC(_S'P');
+    SendIPC(_S'Y');
 end;
 
 procedure CheckPasteBuffer();
@@ -983,7 +993,7 @@
 
         utf8:= char(Key or firstByteMark[Pred(btw)]) + utf8;
 
-        if Length(InputStr.s) + btw > 240 then
+        if Length(InputStr.s) + btw > MaxInputStrLen then
             exit;
 
         InsertIntoInputStr(utf8);
@@ -1071,7 +1081,7 @@
     ChatHidden:= false;
     firstDraw:= true;
 
-    InputLinePrefix:= UserNick + '> ';
+    InputLinePrefix:= '';
     inputStr.s:= '';
     inputStr.Tex := nil;
     for i:= 0 to MaxStrIndex do
--- a/hedgewars/uIO.pas	Sun Mar 08 01:04:41 2015 +0100
+++ b/hedgewars/uIO.pas	Sun Mar 08 01:51:27 2015 +0100
@@ -138,7 +138,6 @@
 case s[1] of
      '!': begin AddFileLog('Ping? Pong!'); isPonged:= true; end;
      '?': SendIPC(_S'!');
-     'P': ChatPasteBuffer:= copy(s, 2, Length(s) - 1);
      'e': ParseCommand(copy(s, 2, Length(s) - 1), true);
      'E': OutError(copy(s, 2, Length(s) - 1), true);
      'W': OutError(copy(s, 2, Length(s) - 1), false);
@@ -164,6 +163,7 @@
              ParseChatCommand('chatmsg ' + #4, s, 2)
           else
              isProcessed:= false;
+     'Y': ChatPasteBuffer:= copy(s, 2, Length(s) - 1);
      else
         isProcessed:= false;
      end;