section.begin
   Name = EventMultiplayerChat
   Code : struct.begin
      [*] = ;const cBackSpaceChar = 8;
      [*] = ;const cSpaceChar  = 32; // space
      [*] = ;const cQuoteChar  = 34; // "
      [*] = ;const cEscChar    = 27; // Escape
      [*] = ;const cReturnChar = 13; // Enter or Return
      [*] = ;const cUpChar     = 38; // key up
      [*] = ;const cDownChar   = 40; // key down
      [*] = ;
      [*] = ;var elmHnd : Integer = GetIntValueByName('ElementHandle');
      [*] = ;var press : String = GetValueByName('Press');
      [*] = ;var tag : Integer = GetIntValueByName('Tag');
      [*] = ;
      [*] = ;var status : String = GetValueByName('Status');
      [*] = ;if (status='shortcut') or (status='key') then // to prevent invalid handle here
      [*] = ;elmHnd := 0;
      [*] = ;
      [*] = ;const currentstatename = 'EventMultiplayerChat';
      [*] = ;var elmname, showstate, eventstate : String;
      [*] = ;_misc_GetGUIShowEventStateNames(currentstatename, elmname, showstate, eventstate);
      [*] = ;
      [*] = ;const cTagCreateRoom = 102;
      [*] = ;const cTagJoinRoom = 103;
      [*] = ;const cTagLeaveRoom = 104;
      [*] = ;const cTagStartRoom = 105;
      [*] = ;const cTagCloseRoom = 106;
      [*] = ;const cTagSwitchToRoom = 107;
      [*] = ;const cTagCleanRecipient = 108;
      [*] = ;const cTagShowFriendsChannel = 109;
      [*] = ;
      [*] = ;procedure ExtractNickname(target : Integer; var s : String);
      [*] = ;begin
      [*] = ;   var recepientOffset, offset : Integer;
      [*] = ;   recepientOffset := StrPos('>', s);
      [*] = ;   if((recepientOffset=0) or (StrPos(':', s)<recepientOffset)) then
      [*] = ;   offset := 16
      [*] = ;   else
      [*] = ;   begin
      [*] = ;      if (target=2) then
      [*] = ;      offset := recepientOffset+16
      [*] = ;      else
      [*] = ;      offset := 16;
      [*] = ;   end;
      [*] = ;   s := SubStr(s, offset, (StrPosEx('%', s, offset))-offset);
      [*] = ;end;
      [*] = ;
      [*] = ;var inputhnd : Integer;
      [*] = ;if (gInternetShell.bshowroom) then
      [*] = ;begin
      [*] = ;   if gInternetShell.bhistoricalbattle then
      [*] = ;   inputhnd := GetGUIElementIndexByNameParent('HistoricalBattle.chatplace.chatback.chatinput.text', _gui_GetTop)
      [*] = ;   else
      [*] = ;   inputhnd := GetGUIElementIndexByNameParent('CustomGame.chatplace.chatback.chatinput.text', _gui_GetTop);
      [*] = ;   if (inputhnd<>0) and (GetGUIFocusedElementHandle<>0) and (GetGUIFocusedElementHandle=inputhnd) and (_gui_GetGUIElementVisibleInherited(inputhnd)) then
      [*] = ;   SetGUIEventStateOnKeyDown(eventstate)
      [*] = ;   else
      [*] = ;   SetGUIEventStateOnKeyDown('');
      [*] = ;end
      [*] = ;else
      [*] = ;begin
      [*] = ;   inputhnd := GetGUIElementIndexByNameParent('InternetShell.chatplace.chatback.chatinput.text', _gui_GetTop);
      [*] = ;   if (inputhnd<>0) and (GetGUIFocusedElementHandle<>0) and (GetGUIFocusedElementHandle=inputhnd) and (_gui_GetGUIElementVisibleInherited(inputhnd)) then
      [*] = ;   SetGUIEventStateOnKeyDown(eventstate)
      [*] = ;   else
      [*] = ;   SetGUIEventStateOnKeyDown('');
      [*] = ;end;
      [*] = ;
      [*] = ;case status of
      [*] = ;   'select' : begin
      [*] = ;      tag := GetGUIListBoxItemTag(elmHnd, GetGUIListBoxItemIndex(elmHnd));
      [*] = ;      if (tag<>0) then
      [*] = ;      begin
      [*] = ;         var elmname : String = GetGUIElementNameByIndex(elmHnd);
      [*] = ;         case elmname of
      [*] = ;            'chat' : begin
      [*] = ;               var nickTo : String;
      [*] = ;               var idTo : Integer;
      [*] = ;               nickTo := GetGUIListBoxItemValue(elmHnd, GetGUIListBoxItemIndex(elmHnd));
      [*] = ;               if (StrPos('%', nickTo)=1) then
      [*] = ;               begin
      [*] = ;                  ExtractNickname(2, nickTo);
      [*] = ;                  if (nickTo=TInternetShellClient(gInternetShell.clients.GetByLanID(LanMyInfoID)).nick) then
      [*] = ;                  begin
      [*] = ;                     nickTo := GetGUIListBoxItemValue(elmHnd, GetGUIListBoxItemIndex(elmHnd));
      [*] = ;                     ExtractNickname(1, nickTo);
      [*] = ;                  end;
      [*] = ;                  gstring_internetshell_msgplnick := nickTo;
      [*] = ;                  var pclient : Pointer = gInternetShell.clients.GetByNick(nickTo);
      [*] = ;                  if (pclient<>nil) then
      [*] = ;                  begin
      [*] = ;                     gint_internetshell_msgplid := TInternetShellClient(gInternetShell.clients.GetByNick(nickTo)).lanid;
      [*] = ;                     if (gint_internetshell_prevplholder<>gint_internetshell_msgplid) then
      [*] = ;                     gint_internetshell_prevplholder := gint_internetshell_msgplid;
      [*] = ;                  end
      [*] = ;                  else
      [*] = ;                  begin
      [*] = ;                     gint_internetshell_msgplid := -1;
      [*] = ;                     gint_internetshell_prevplholder := -1;
      [*] = ;                     SetGUIElementText(inputhnd, nickTo+': ');
      [*] = ;                  end;
      [*] = ;                  SetGUIElementText(inputhnd, nickTo+': ');
      [*] = ;                  if inputhnd<>0 then
      [*] = ;                  begin
      [*] = ;                     SetGUIElementFocusedHandle(inputhnd);
      [*] = ;                     SetGUIEventStateOnKeyDown('EventMultiplayerChat');
      [*] = ;                  end;
      [*] = ;                  SetGUIElementVisible(GetGUIElementIndexByNameParent('InternetShell.chatplace.chatback.bcleanrecipient', _gui_GetTop), True);
      [*] = ;               end;
      [*] = ;            end;
      [*] = ;         end;
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;   'key' : begin
      [*] = ;      if (press='d') then
      [*] = ;      begin
      [*] = ;         case tag of
      [*] = ;            cReturnChar : begin
      [*] = ;               var inputtext : String;
      [*] = ;               if (inputhnd<>0) then
      [*] = ;               inputtext := GetGUIElementText(inputhnd);
      [*] = ;               if (inputtext<>'') then
      [*] = ;               begin
      [*] = ;                  gInternetShell.history[gInternetShell.historyind] := inputtext;
      [*] = ;                  gInternetShell.historyind := gInternetShell.historyind+1;
      [*] = ;                  gInternetShell.historyscrollind := gInternetShell.historyind;
      [*] = ;                  var msg : String = inputtext;
      [*] = ;                  var bsend : Boolean;
      [*] = ;                  var firstword : String = GetDelimiterStringByIndex(inputtext, 32, gc_gui_quotechar, 0);
      [*] = ;                  if (gint_internetshell_msgplid<>0) then
      [*] = ;                  firstword := '/w';
      [*] = ;                  var idTo : Integer;
      [*] = ;                  case firstword of
      [*] = ;                     '/whisper', '/w' : begin
      [*] = ;                        var second : String;
      [*] = ;                        if (gint_internetshell_msgplid<>0) then
      [*] = ;                        begin
      [*] = ;                           second := GetDelimiterStringByIndex(inputtext, 32, gc_gui_quotechar, 0);
      [*] = ;                           second := SubStr(second, 0, StrPos(':', second)-1);
      [*] = ;                        end
      [*] = ;                        else
      [*] = ;                        second := GetDelimiterStringByIndex(inputtext, 32, gc_gui_quotechar, 1);
      [*] = ;                        if (second<>'') then
      [*] = ;                        begin
      [*] = ;                           var offset : Integer;
      [*] = ;                           if (gint_internetshell_msgplid<>0) then
      [*] = ;                           offset := StrLength(second)+1
      [*] = ;                           else
      [*] = ;                           offset := StrLength(firstword)+StrLength(second)+1;
      [*] = ;                           inputtext := SubStr(inputtext, offset+2, StrLength(inputtext)-offset);
      [*] = ;                           if (inputtext<>'') then
      [*] = ;                           begin
      [*] = ;                              var pclient : Pointer;
      [*] = ;                              var id : Integer;
      [*] = ;                              if (StrPos('#', second)=1) then
      [*] = ;                              id := StrToInt(SubStr(second, 2, StrLength(second)-1));//StrToInt(second);
      [*] = ;                              if (id<>0) then
      [*] = ;                              begin
      [*] = ;                                 idTo := id;
      [*] = ;                                 pclient := gInternetShell.clients.GetByLanID(idTo);
      [*] = ;                              end
      [*] = ;                              else
      [*] = ;                              pclient := gInternetShell.clients.GetByNick(second);
      [*] = ;                              if (pclient<>nil) then
      [*] = ;                              begin
      [*] = ;                                 if (idTo=0) then
      [*] = ;                                 idTo := TInternetShellClient(pclient).lanid;
      [*] = ;                                 msg := inputtext;
      [*] = ;                                 bsend := True;
      [*] = ;                              end
      [*] = ;                              else
      [*] = ;                              begin
      [*] = ;                                 if ((idTo=0) or (gint_internetshell_msgplid=-1)) then
      [*] = ;                                 idTo := gInternetShell.lanid;
      [*] = ;                                 msg := GetLocaleTableListItemByID('misc', 'internetshell.chat.nonick');
      [*] = ;                                 msg := StrReplace(msg, '%nick%', gc_font_color_white+second+gc_font_color_default);
      [*] = ;                                 bsend := True;
      [*] = ;                              end;
      [*] = ;                           end;
      [*] = ;                        end
      [*] = ;                        else
      [*] = ;                        bsend := True;
      [*] = ;                     end;
      [*] = ;                     '' : ;
      [*] = ;                     '/help', '/h' : begin
      [*] = ;                        msg := msg+GetLocaleTableListItemByID('misc', 'internetshell.chat.help');
      [*] = ;                        idTo := gInternetShell.lanid;
      [*] = ;                        bsend := True;
      [*] = ;                     end;
      [*] = ;                     '/clear', '/c' : begin
      [*] = ;                        gInternetShell.ClearChat(not gInternetShell.bshowroom, gInternetShell.bshowroom);
      [*] = ;                        gbool_internetshell_firstappearance := False;
      [*] = ;                        _misc_FillChatList(False);
      [*] = ;                        idTo := gInternetShell.lanid;
      [*] = ;                        bsend := True;
      [*] = ;                     end;
      [*] = ;                     else
      [*] = ;                     begin
      [*] = ;                        msg := StrReplace(msg, '|', '');
      [*] = ;                        msg := StrReplace(msg, '"', '');
      [*] = ;                        bsend := True;
      [*] = ;                     end;
      [*] = ;                  end;
      [*] = ;                  if ((bsend)  and ((not gbool_internetshell_friendschannel) or (gint_internetshell_msgplid<>0))) then
      [*] = ;                  begin
      [*] = ;                     msg := StrReplace(msg, #9, ''); // remove tab (can be theoretically added through ctrl+v)
      [*] = ;                     if (gInternetShell.bshowroom) then
      [*] = ;                     LanPublicServerSendSessionMessage(idTo, msg)
      [*] = ;                     else
      [*] = ;                     if (LanMyInfoID=95618) then
      [*] = ;                     Log('banned from chat')
      [*] = ;                     else
      [*] = ;                     begin
      [*] = ;                        var TempText : String = 'User is currently oflline :(';
      [*] = ;                        if (gint_internetshell_msgplid<>0) then
      [*] = ;                        begin
      [*] = ;                           if(gint_internetshell_msgplid<>-1) then
      [*] = ;                           LanPublicServerSendMessage(gint_internetshell_msgplid, msg)
      [*] = ;                           else
      [*] = ;                           LanPublicServerSendMessage(LanMyInfoID, msg);
      [*] = ;                        end
      [*] = ;                        else
      [*] = ;                        LanPublicServerSendMessage(idTo, msg);
      [*] = ;                     end;
      [*] = ;                  end;
      [*] = ;               end;
      [*] = ;               if ((gint_internetshell_msgplid<>0) and (not gInternetShell.bshowroom) and (not gInternetShell.bhistoricalbattle)) then
      [*] = ;               SetGUIElementText(inputhnd, TInternetShellClient(gInternetShell.clients.GetByLanID(gint_internetshell_msgplid)).nick+': ')
      [*] = ;               else
      [*] = ;               SetGUIElementText(inputhnd, '');
      [*] = ;            end;
      [*] = ;            cEscChar : begin
      [*] = ;               _gui_SendTagToStateExt('EventMultiplayerChat', cTagCleanRecipient, 'c', '', 'button', 0);
      [*] = ;               gInternetShell.historyscrollind := gInternetShell.historyind;
      [*] = ;            end;
      [*] = ;            cBackSpaceChar : begin
      [*] = ;               if (gint_internetshell_msgplid<>0) then
      [*] = ;               begin
      [*] = ;                  if ((gint_internetshell_msgplid>0) and (StrLength(TInternetShellClient(gInternetShell.clients.GetByLanID(gint_internetshell_msgplid)).nick+':')=StrLength(GetGUIElementText(inputhnd)))) then
      [*] = ;                  begin
      [*] = ;                     SetGUIElementText(inputhnd, '');
      [*] = ;                     gint_internetshell_msgplid := 0;
      [*] = ;                     gint_internetshell_prevplholder := 0;
      [*] = ;                     SetGUIElementVisible(GetGUIElementIndexByNameParent('InternetShell.chatplace.chatback.bcleanrecipient', _gui_GetTop), False);
      [*] = ;                  end;
      [*] = ;               end;
      [*] = ;            end;
      [*] = ;         end;
      [*] = ;      end
      [*] = ;      else
      [*] = ;      if (press='u') then
      [*] = ;      begin
      [*] = ;         case tag of
      [*] = ;            cUpChar, cDownChar : begin
      [*] = ;               if (inputhnd<>0) and (GetGUIElementFocusedByHandle(inputhnd)) then
      [*] = ;               begin
      [*] = ;                  var msg : String;
      [*] = ;                  if (tag=cUpChar) then
      [*] = ;                  begin
      [*] = ;                     if (gInternetShell.historyscrollind-1>=0) and (gInternetShell.history[gInternetShell.historyscrollind-1]<>'') then
      [*] = ;                     gInternetShell.historyscrollind := gInternetShell.historyscrollind-1;
      [*] = ;                     msg := gInternetShell.history[gInternetShell.historyscrollind];
      [*] = ;                  end
      [*] = ;                  else
      [*] = ;                  begin
      [*] = ;                     if ((gInternetShell.historyscrollind-1)<(gc_gui_maxinternetchatlog-1)) and (gInternetShell.history[gInternetShell.historyscrollind+1]<>'') then
      [*] = ;                     begin
      [*] = ;                        gInternetShell.historyscrollind := gInternetShell.historyscrollind+1;
      [*] = ;                        msg := gInternetShell.history[gInternetShell.historyscrollind];
      [*] = ;                     end
      [*] = ;                     else
      [*] = ;                     begin
      [*] = ;                        if (gInternetShell.history[gInternetShell.historyscrollind]<>'') then
      [*] = ;                        gInternetShell.historyscrollind := gInternetShell.historyscrollind+1;
      [*] = ;                        msg := '';
      [*] = ;                     end;
      [*] = ;                  end;
      [*] = ;                  if IsInRangeInt(gInternetShell.historyscrollind, 0, gc_gui_maxinternetchatlog) then
      [*] = ;                  SetGUIElementText(inputhnd, msg)
      [*] = ;                  else
      [*] = ;                  ErrorLog('EventInternetShell not IsInRangeInt');
      [*] = ;               end;
      [*] = ;               {if textHandle <> 0 then
      [*] = ;               begin
      [*] = ;                  var temp_str : String = gStrRegister_ConsoleLogHistory;
      [*] = ;                  var temp_str2 : String;
      [*] = ;                  var n : Integer = gIntRegister_ConsoleCommandIndex;
      [*] = ;
      [*] = ;                  if StrToInt(sTag) = cUpChar then
      [*] = ;                  n := n - 1
      [*] = ;                  else
      [*] = ;                  n := n + 1;
      [*] = ;
      [*] = ;                  n := Max(Min(n,_sa_GetNum(temp_str)-1),0);
      [*] = ;                  _sa_Get(temp_str,n,temp_str2);
      [*] = ;                  SetGUIElementText(iMsgHnd, temp_str2);
      [*] = ;                  gIntRegister_ConsoleCommandIndex := n;
      [*] = ;               end;}
      [*] = ;            end;
      [*] = ;            //cReturnChar : iTag := GetGUIElementTag(iMsgHnd);
      [*] = ;         end;
      [*] = ;      end;
      [*] = ;      var btnHnd : Integer = GetGUIElementIndexByNameParent('InternetShell.chatplace.chatback.bcleanrecipient', _gui_GetTop);
      [*] = ;      if (btnHnd<>0) then
      [*] = ;      begin
      [*] = ;         if GetGUIElementVisible(btnHnd) and (GetGUIElementText(inputhnd)='') then
      [*] = ;         SetGUIElementVisible(btnHnd, False)
      [*] = ;         else
      [*] = ;         if (not GetGUIElementVisible(btnHnd)) and (GetGUIElementText(inputhnd)<>'') then
      [*] = ;         SetGUIElementVisible(btnHnd, True);
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;end;
      [*] = ;if (status='button') and (press='c') then
      [*] = ;begin
      [*] = ;   case tag of
      [*] = ;      gc_gui_event_tagShow : ExecuteState(showstate);
      [*] = ;      gc_gui_event_tagHide : begin
      [*] = ;         _gui_SetGUIElementVisibleByName(elmname, False);
      [*] = ;      end;
      [*] = ;      cTagCleanRecipient : begin
      [*] = ;         gint_internetshell_msgplid := 0;
      [*] = ;         gint_internetshell_prevplholder := 0;
      [*] = ;         SetGUIElementText(GetGUIElementIndexByNameParent('InternetShell.chatplace.chatback.chatinput.text', _gui_GetTop), '');
      [*] = ;         SetGUIElementVisible(elmHnd, False);
      [*] = ;         ExecuteState(showstate);
      [*] = ;         if inputhnd<>0 then
      [*] = ;         begin
      [*] = ;            SetGUIElementFocusedHandle(inputhnd);
      [*] = ;            SetGUIEventStateOnKeyDown('EventMultiplayerChat');
      [*] = ;         end;
      [*] = ;      end;
      [*] = ;      cTagShowFriendsChannel : begin
      [*] = ;         gbool_internetshell_friendschannel := not gbool_internetshell_friendschannel;
      [*] = ;         SetGUIElementChecked(elmHnd, gbool_internetshell_friendschannel);
      [*] = ;         ExecuteState(showstate);
      [*] = ;      end;
      [*] = ;   end;
      [*] = ;end;
   struct.end
section.end

