Advertisement
Guest User

RECS 1.0.0

a guest
May 9th, 2013
4,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 22.30 KB | None | 0 0
  1. /***                                                    ***
  2.  * RChat - Raf's External Chat System - Server            *
  3.  ***                                                    ***
  4.  * @Author  Rafael 'R@f' Keramidas <rafael@keramid.as>    *
  5.  * @Date    9th May 2013                                  *
  6.  * @Version 1.0.0                                         *
  7.  * @Licence MIT License                                   *
  8.  * @Comment Android client available (SA-MP Chat)         *
  9.  *                                                        *
  10.  * Copyright (C) 2013 Rafael Keramidas                    *
  11.  *                                                        *
  12.  * Permission is hereby granted, free of charge, to any   *
  13.  * person obtaining a copy of this software and           *
  14.  * associated documentation files (the "Software"), to    *
  15.  * deal in the Software without restriction, including    *
  16.  * without limitation the rights to use, copy, modify,    *
  17.  * merge, publish, distribute, sublicense, and/or sell    *
  18.  * copies of the Software, and to permit persons to whom  *
  19.  * the Software is furnished to do so, subject to the     *
  20.  * following conditions:                                  *
  21.  *                                                        *
  22.  * The above copyright notice and this permission notice  *
  23.  * shall be included in all copies or substantial         *
  24.  * portions of the Software.                              *
  25.  *                                                        *
  26.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF  *
  27.  * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT        *
  28.  * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS  *
  29.  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO    *
  30.  * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE *
  31.  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN  *
  32.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      *
  33.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
  34.  * USE OR OTHER DEALINGS IN THE SOFTWARE.                 *
  35.  ***                                                    ***/
  36.  
  37. #include <a_samp>
  38. #include <socket>
  39.  
  40. /* Config */
  41. #define LISTEN_PORT     9014                /* TCP port to listen to (default 9014) */
  42. #define SERVER_IP       "192.168.1.47"      /* Server IP (public IP) */
  43. #define MAX_CONN        20                  /* Maximum TCP connections allowed (amount of clients X 2) */
  44. #define PASSWORD        "test"              /* Password to connect to the chat (leave blank for none) */
  45. #define CLIENT_COLOR    "00FF00"            /* Color for remote clients in the chat */
  46. #define INFOMSG_COLOR   0xAAAAAAAA          /* Color for connect, disconnect messages and other infos */
  47. #define ENABLE_AUTOMSG  true                /* Automatic messages informing players that they can remotely connect to the chat */
  48.  
  49. /* DO NOT EDIT BELOW (if you don't know what you're doing) */
  50.  
  51. /* Script Info */
  52. #define MAJOR_VERSION   1
  53. #define MINOR_VERSION   0
  54. #define BUGFIX          0
  55. #define LAST_UPDATE     "09.05.2013"
  56.  
  57. /* Versions (DO NOT CHANGE !!!) */
  58. #define MIN_VERSION     1       /* Minimum client version accepted (integer) */
  59. #define CURR_VERSION    1       /* Current server version (integer) */
  60.  
  61. /* COLORS */
  62. #define COLOR_LIGHTBLUE 0x33DAFFAA
  63. #define COLOR_RED       0xFF0000FF
  64. #define COLOR_WHITE     0xFFFFFFAA
  65. #define COLOR_YELLOW    0xFFD700AA
  66.  
  67. /* Variables */
  68. new
  69.     Socket:sSocket,
  70.     bool:bClientAllowed[MAX_CONN],
  71.     bool:bConnected[MAX_CONN],
  72.     sClientName[MAX_CONN][24],
  73.     sClientDevice[MAX_CONN][30],
  74.     sClientIP[MAX_CONN][20],
  75.     sClientUniqueID[MAX_CONN][17];
  76.  
  77.  
  78. /*############################################################################*/
  79.  
  80. public OnFilterScriptInit() {
  81.     printf("++++++++++++++++++++++++++++++++++++++++");
  82.     printf("++ RChat - Raf's External Chat System ++");
  83.     printf("++ V%d.%d.%d - Last update: %s   ++", MAJOR_VERSION, MINOR_VERSION, BUGFIX, LAST_UPDATE);
  84.     printf("++ Script by Rafael 'R@f' Keramidas   ++");
  85.     printf("++++++++++++++++++++++++++++++++++++++++\n");
  86.    
  87.     for(new i = 0; i < MAX_CONN; i++)
  88.         bConnected[i] = false;
  89.    
  90.     sSocket = socket_create(TCP);
  91.     if(is_socket_valid(sSocket)) {
  92.         socket_set_max_connections(sSocket, MAX_CONN);
  93.         socket_bind(sSocket, SERVER_IP);
  94.         socket_listen(sSocket, LISTEN_PORT);
  95.     }
  96.    
  97.     #if ENABLE_AUTOMSG == true
  98.     /* Message every 10 minutes */
  99.     SetTimer("autoMessages", 10*60000, true);
  100.     #endif
  101.  
  102.     return true;
  103. }
  104.  
  105. /*############################################################################*/
  106.  
  107. public OnFilterScriptExit() {
  108.     if(is_socket_valid(sSocket))
  109.         socket_destroy(sSocket);
  110.        
  111.     return true;
  112. }
  113.  
  114. /*############################################################################*/
  115.  
  116. public OnPlayerConnect(playerid) {
  117.     new
  118.         sPlayerName[MAX_PLAYER_NAME],
  119.         sRemoteMsg[128];
  120.  
  121.     GetPlayerName(playerid, sPlayerName, sizeof(sPlayerName));
  122.  
  123.     format(sRemoteMsg, sizeof(sRemoteMsg), "%s(%d) has joined the server\n", sPlayerName, playerid);
  124.     sendMessageToAllRemoteClients(sRemoteMsg);
  125.     return true;
  126. }
  127.  
  128. /*############################################################################*/
  129.  
  130. public OnPlayerDisconnect(playerid, reason) {
  131.     new
  132.         sPlayerName[MAX_PLAYER_NAME],
  133.         sRemoteMsg[128];
  134.  
  135.     GetPlayerName(playerid, sPlayerName, sizeof(sPlayerName));
  136.    
  137.     switch(reason) {
  138.         case 0:
  139.             format(sRemoteMsg, sizeof(sRemoteMsg), "%s(%d) has left the server (timeout) \n", sPlayerName, playerid);
  140.         case 1:
  141.             format(sRemoteMsg, sizeof(sRemoteMsg), "%s(%d) has left the server (leaving) \n", sPlayerName, playerid);
  142.         case 2:
  143.             format(sRemoteMsg, sizeof(sRemoteMsg), "%s(%d) has left the server (kicked/banned) \n", sPlayerName, playerid);
  144.     }
  145.    
  146.     sendMessageToAllRemoteClients(sRemoteMsg);
  147.     return true;
  148. }
  149.  
  150. /*############################################################################*/
  151.  
  152. public OnPlayerDeath(playerid, killerid, reason) {
  153.     /* Adding deaths in a later version */
  154.     return true;
  155. }
  156.  
  157. /*############################################################################*/
  158.  
  159. public OnPlayerText(playerid, text[]) {
  160.     new
  161.         sPlayerName[MAX_PLAYER_NAME],
  162.         sRemoteMsg[128];
  163.        
  164.     GetPlayerName(playerid, sPlayerName, sizeof(sPlayerName));
  165.    
  166.     format(sRemoteMsg, sizeof(sRemoteMsg), "%s(%d): %s\n", sPlayerName, playerid, text);
  167.     sendMessageToAllRemoteClients(sRemoteMsg);
  168.     return true;
  169. }
  170.  
  171. /*############################################################################*/
  172.  
  173. public OnPlayerCommandText(playerid, cmdtext[]) {
  174.     new
  175.         sCmd[128],
  176.         sTmp[128],
  177.         sIngameMsg[128],
  178.         sRemoteMsg[128],
  179.         iRid,
  180.         iIndex;
  181.    
  182.     sCmd = strtok(cmdtext, iIndex);
  183.    
  184.     if (strcmp(sCmd, "/showremote", true) == 0) {
  185.         SendClientMessage(playerid, COLOR_LIGHTBLUE, "Currently remote clients connected: ");
  186.         if(remoteClientCount() > 0) {
  187.             for(new i = 0; i < MAX_CONN; i++) {
  188.                 if(isRemoteClientConnected(i) && isRemoteClientAllowed(i)) {
  189.                     format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) - Client: %s", sClientName[i], i, sClientDevice[i]);
  190.                     SendClientMessage(playerid, COLOR_WHITE, sIngameMsg);
  191.                 }
  192.             }
  193.         }
  194.         else {
  195.             SendClientMessage(playerid, COLOR_WHITE, "None");
  196.         }
  197.         return true;
  198.     }
  199.  
  200.     if (strcmp(sCmd, "/kickremote", true) == 0) {
  201.         sTmp = strtok(cmdtext, iIndex);
  202.         if(!IsPlayerAdmin(playerid)) {
  203.             SendClientMessage(playerid, COLOR_RED, "RECS: You have to be admin to use this command!");
  204.             return true;
  205.         }
  206.        
  207.         if(!strlen(sTmp)) {
  208.             SendClientMessage(playerid, COLOR_YELLOW, "USAGE: /kickremote [clientid]");
  209.             return true;
  210.         }
  211.  
  212.         iRid = strval(sTmp);
  213.         if (!isRemoteClientConnected(iRid) && !isRemoteClientAllowed(iRid)) {
  214.             SendClientMessage(playerid, COLOR_RED, "RECS: Remote client is not connected!");
  215.             return true;
  216.         }
  217.  
  218.         format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) has been kicked from the server", sClientName[iRid], iRid);
  219.         SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
  220.  
  221.         format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) has been kicked from the server\n", sClientName[iRid], iRid);
  222.         sendMessageToAllRemoteClients(sRemoteMsg);
  223.  
  224.         bClientAllowed[iRid] = false;
  225.         socket_sendto_remote_client(sSocket, iRid, "ERR You have been kicked from the server!\n");
  226.         closeRemoteConnection(iRid);
  227.         return true;
  228.     }
  229.    
  230.     if (strcmp(sCmd, "/banremote", true) == 0) {
  231.         sTmp = strtok(cmdtext, iIndex);
  232.         if(!IsPlayerAdmin(playerid)) {
  233.             SendClientMessage(playerid, COLOR_RED, "RECS: You have to be admin to use this command!");
  234.             return true;
  235.         }
  236.  
  237.         if(!strlen(sTmp)) {
  238.             SendClientMessage(playerid, COLOR_YELLOW, "USAGE: /banremote [clientid]");
  239.             return true;
  240.         }
  241.  
  242.         iRid = strval(sTmp);
  243.         if (!isRemoteClientConnected(iRid) && !isRemoteClientAllowed(iRid)) {
  244.             SendClientMessage(playerid, COLOR_RED, "RECS: Remote client is not connected!");
  245.             return true;
  246.         }
  247.  
  248.         format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) has been banned from the server", sClientName[iRid], iRid);
  249.         SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
  250.  
  251.         format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) has been banned from the server\n", sClientName[iRid], iRid);
  252.         sendMessageToAllRemoteClients(sRemoteMsg);
  253.  
  254.         bClientAllowed[iRid] = false;
  255.         socket_sendto_remote_client(sSocket, iRid, "ERR You have been banned from the server!\n");
  256.         banRemoteClient(iRid);
  257.         return true;
  258.     }
  259.  
  260.     return false;
  261. }
  262.  
  263. /*############################################################################*/
  264.  
  265. public onSocketRemoteConnect(Socket:id, remote_client[], remote_clientid) {
  266.     printf("[RECS] Incoming connection from [%d:%s]", remote_clientid, remote_client);
  267.    
  268.     format(sClientIP[remote_clientid], 20, "%s", remote_client);
  269.  
  270.     if(isBanned(remote_clientid)) {
  271.         socket_sendto_remote_client(sSocket, remote_clientid, "ERR You are banned from this server !\n");
  272.         closeRemoteConnection(remote_clientid);
  273.     }
  274.  
  275.     bClientAllowed[remote_clientid] = false;
  276.     bConnected[remote_clientid] = true;
  277.     sClientName[remote_clientid] = "";
  278.     sClientDevice[remote_clientid] = "";
  279.     sClientIP[remote_clientid] = "";
  280.     return true;
  281. }
  282.  
  283. /*############################################################################*/
  284.  
  285. public onSocketRemoteDisconnect(Socket:id, remote_clientid) {
  286.     new
  287.         sRemoteMsg[128],
  288.         sIngameMsg[128];
  289.        
  290.     if(bClientAllowed[remote_clientid] == true) {
  291.         format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) disconnected from the chat", sClientName[remote_clientid], remote_clientid);
  292.         SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
  293.        
  294.         format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) disconnected from the chat\n", sClientName[remote_clientid], remote_clientid);
  295.         sendMessageToAllRemoteClients(sRemoteMsg);
  296.     }
  297.    
  298.     printf("[RECS] Remote client [%d] has disconnected.", remote_clientid);
  299.     bClientAllowed[remote_clientid] = false;
  300.     bConnected[remote_clientid] = false;
  301.     sClientName[remote_clientid] = "";
  302.     sClientDevice[remote_clientid] = "";
  303.     sClientIP[remote_clientid] = "";
  304.     return true;
  305. }
  306.  
  307. /*############################################################################*/
  308.  
  309. public onSocketReceiveData(Socket:id, remote_clientid, data[], data_len)
  310. {
  311.     new
  312.         sType[20],
  313.         sRemoteMsg[128],
  314.         sIngameMsg[128],
  315.         iIndex;
  316.        
  317.     sType = strtok(data, iIndex);
  318.    
  319.     /* Empty string */
  320.     if(strlen(data) == 2) {
  321.         socket_sendto_remote_client(sSocket, remote_clientid, "ERR Empty message !\n");
  322.         closeRemoteConnection(remote_clientid);
  323.     }
  324.     /* Name type */
  325.     else if(!strcmp(sType, "CON", false, 3)) {
  326.         new
  327.             sName[24],
  328.             sClient[30],
  329.             sVersion[20],
  330.             sPassword[30],
  331.             sRandomString[17];
  332.            
  333.         sName = strtok(data, iIndex);
  334.         sClient = strtok(data, iIndex);
  335.         sVersion = strtok(data, iIndex);
  336.         sPassword = strtok(data, iIndex);
  337.        
  338.         if(strlen(sName) < 3 || strlen(sName) > 24) {
  339.             socket_sendto_remote_client(sSocket, remote_clientid, "ERR Invalid name !\n");
  340.             closeRemoteConnection(remote_clientid);
  341.         }
  342.         else {
  343.             if(in_array(sName, sClientName)) {
  344.                 socket_sendto_remote_client(sSocket, remote_clientid, "ERR This name is already used !\n");
  345.                 closeRemoteConnection(remote_clientid);
  346.             }
  347.             else {
  348.                 if(strlen(sClient) < 3 || strlen(sClient) > 30) {
  349.                     socket_sendto_remote_client(sSocket, remote_clientid, "ERR Invalid device name !\n");
  350.                     closeRemoteConnection(remote_clientid);
  351.                 }
  352.                 else {
  353.                     if(strval(sVersion) < MIN_VERSION) {
  354.                         socket_sendto_remote_client(sSocket, remote_clientid, "ERR Your client version is not compatible with the server !\n");
  355.                         closeRemoteConnection(remote_clientid);
  356.                     }
  357.                     else {
  358.                         format(sRandomString, sizeof(sRandomString), randomString());
  359.  
  360.                         if(strlen(PASSWORD) != 0) {
  361.                             if(strlen(sPassword) == 0) {
  362.                                 socket_sendto_remote_client(sSocket, remote_clientid, "ERR Password is empty !\n");
  363.                                 closeRemoteConnection(remote_clientid);
  364.                             }
  365.                             else {
  366.                                 if(strcmp(sPassword, PASSWORD)) {
  367.                                     socket_sendto_remote_client(sSocket, remote_clientid, "ERR Incorrect password !\n");
  368.                                     closeRemoteConnection(remote_clientid);
  369.                                 }
  370.                                 else {
  371.                                     format(sClientName[remote_clientid], 24, "%s", sName);
  372.                                     format(sClientDevice[remote_clientid], 30, "%s", sClient);
  373.  
  374.                                     format(sRemoteMsg, sizeof(sRemoteMsg), "CID %s\n", sRandomString);
  375.                                     socket_sendto_remote_client(sSocket, remote_clientid, sRemoteMsg);
  376.  
  377.                                     format(sClientUniqueID[remote_clientid], 17, sRandomString);
  378.                                     bClientAllowed[remote_clientid] = true;
  379.  
  380.                                     format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) connected to the chat (Client: %s)", sClientName[remote_clientid], remote_clientid, sClientDevice[remote_clientid]);
  381.                                     SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
  382.  
  383.                                     format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) connected to the chat (Client: %s) \n", sClientName[remote_clientid], remote_clientid, sClientDevice[remote_clientid]);
  384.                                     sendMessageToAllRemoteClients(sRemoteMsg);
  385.                                 }
  386.                             }
  387.                         }
  388.                         else {
  389.                             format(sClientName[remote_clientid], 24, "%s", sName);
  390.                             format(sClientDevice[remote_clientid], 30, "%s", sClient);
  391.  
  392.                             format(sRemoteMsg, sizeof(sRemoteMsg), "CID %s\n", sRandomString);
  393.                             socket_sendto_remote_client(sSocket, remote_clientid, sRemoteMsg);
  394.  
  395.                             format(sClientUniqueID[remote_clientid], 17, sRandomString);
  396.                             bClientAllowed[remote_clientid] = true;
  397.  
  398.                             format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) connected to the chat (Client: %s)", sClientName[remote_clientid], remote_clientid, sClientDevice[remote_clientid]);
  399.                             SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
  400.  
  401.                             format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) connected to the chat (Client: %s) \n", sClientName[remote_clientid], remote_clientid, sClientDevice[remote_clientid]);
  402.                             sendMessageToAllRemoteClients(sRemoteMsg);
  403.                         }
  404.                     }
  405.                 }
  406.             }
  407.         }
  408.     }
  409.     /* Version type */
  410.     else if(!strcmp(sType, "VER", false, 3)) {
  411.         new
  412.             sMsg[128];
  413.  
  414.         format(sMsg, sizeof(sMsg), "VER %d\n", CURR_VERSION);
  415.         socket_sendto_remote_client(sSocket, remote_clientid, sMsg);
  416.     }
  417.     /* Message type */
  418.     else if(!strcmp(sType, "MSG", false, 3)) {
  419.         new
  420.             sCode[20],
  421.             iClientID;
  422.            
  423.         sCode = strtok(data, iIndex);
  424.         iClientID = array_search(sCode, sClientUniqueID);
  425.        
  426.         if(iClientID == -1) {
  427.             socket_sendto_remote_client(sSocket, remote_clientid, "ERR You are not allowed to send messages !\n");
  428.             closeRemoteConnection(remote_clientid);
  429.         }
  430.         else {
  431.             if(strlen(data[4]) == 0) {
  432.                 socket_sendto_remote_client(sSocket, remote_clientid, "ERR You can't send empty messages !\n");
  433.                 closeRemoteConnection(remote_clientid);
  434.             }
  435.             else {
  436.                 socket_sendto_remote_client(sSocket, remote_clientid, "MOK\n");
  437.                 closeRemoteConnection(remote_clientid);
  438.                
  439.                 format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d): %s", sClientName[iClientID], iClientID, data[21]);
  440.                 sendMessageToAllRemoteClients(sRemoteMsg);
  441.  
  442.                 format(sIngameMsg, sizeof(sIngameMsg), "{"CLIENT_COLOR"}[REMOTE]%s(%d):{FFFFFF} %s", sClientName[iClientID], iClientID, data[21]);
  443.                 SendClientMessageToAll(-1, sIngameMsg);
  444.             }
  445.         }
  446.     }
  447.     /* Message type */
  448.     else if(!strcmp(sType, "CLS", false, 3)) {
  449.         new
  450.             sCode[20],
  451.             iClientID;
  452.  
  453.         sCode = strtok(data, iIndex);
  454.         iClientID = array_search(sCode, sClientUniqueID);
  455.  
  456.         if(iClientID == -1) {
  457.             socket_sendto_remote_client(sSocket, remote_clientid, "ERR Client not found !\n");
  458.             closeRemoteConnection(remote_clientid);
  459.         }
  460.         else {
  461.             closeRemoteConnection(iClientID);
  462.             closeRemoteConnection(remote_clientid);
  463.         }
  464.     }
  465.     /* Unknown type */
  466.     else {
  467.         socket_sendto_remote_client(sSocket, remote_clientid, "ERR Unknown type !\n");
  468.         closeRemoteConnection(remote_clientid);
  469.     }
  470.    
  471.     return true;
  472. }
  473.  
  474. /*############################################################################*/
  475.  
  476. strtok (const string[], &index) {
  477.     new length = strlen(string);
  478.     while ((index < length) && (string[index] <= ' ')) {
  479.         index++;
  480.     }
  481.  
  482.     new offset = index;
  483.     new result[20];
  484.     while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1))) {
  485.         result[index - offset] = string[index];
  486.         index++;
  487.     }
  488.     result[index - offset] = EOS;
  489.     return result;
  490. }
  491.  
  492. /*############################################################################*/
  493. /* By Ssk */
  494.  
  495. RemoveGarbage(str[]) {
  496.     new
  497.         i = 0;
  498.  
  499.     while(str[i] != 0) {
  500.         if(str[i] == '\n' || str[i] == '\r') {
  501.             str[i] = 0;
  502.         }
  503.         else {
  504.             i++;
  505.         }
  506.     }
  507. }
  508.  
  509. /*############################################################################*/
  510. /* By Yiin */
  511.  
  512. stock randomString() {
  513.     new
  514.         _text[17];
  515.        
  516.     for(new i = 0; i < 17; i++) {
  517.         switch(random(2)) {
  518.             case 0: _text[i] = random(25) + 66;
  519.             case 1: _text[i] = random(25) + 98;
  520.         }
  521.     }
  522.    
  523.     return _text;
  524. }
  525.  
  526. /*############################################################################*/
  527.  
  528. stock array_search(const sStringToFind[], const aTargetArray[][], const iArraySize = sizeof aTargetArray) {
  529.     new
  530.         iIndex = 0;
  531.        
  532.     if(strlen(sStringToFind) == 16) {
  533.         while(iIndex < iArraySize) {
  534.             if(strlen(aTargetArray[iIndex]) == 16)
  535.                 if(!strcmp(sStringToFind, aTargetArray[iIndex], true, 16))
  536.                     return iIndex;
  537.  
  538.             iIndex++;
  539.         }
  540.     }
  541.     return -1;
  542. }
  543.  
  544. /*############################################################################*/
  545.  
  546. stock in_array(const sStringToFind[], const aTargetArray[][], const iArraySize = sizeof aTargetArray) {
  547.     new
  548.         iIndex = 0;
  549.  
  550.     if(strlen(sStringToFind) != 0) {
  551.         while(iIndex < iArraySize) {
  552.             if(strlen(aTargetArray[iIndex]) != 0)
  553.                 if(!strcmp(sStringToFind, aTargetArray[iIndex]))
  554.                     return true;
  555.  
  556.             iIndex++;
  557.         }
  558.     }
  559.  
  560.     return false;
  561. }
  562.  
  563. /*############################################################################*/
  564.  
  565. forward closeRemoteConnection(iClientid);
  566. public closeRemoteConnection(iClientid) {
  567.     new
  568.         sRemoteMsg[128],
  569.         sIngameMsg[128];
  570.  
  571.     if(bClientAllowed[iClientid] == true) {
  572.         format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) disconnected from the chat", sClientName[iClientid], iClientid);
  573.         SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
  574.  
  575.         format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) disconnected from the chat\n", sClientName[iClientid], iClientid);
  576.         sendMessageToAllRemoteClients(sRemoteMsg);
  577.     }
  578.  
  579.     printf("[RECS] Remote client [%d] has disconnected.", iClientid);
  580.     bClientAllowed[iClientid] = false;
  581.     bConnected[iClientid] = false;
  582.     sClientName[iClientid] = "";
  583.     sClientDevice[iClientid] = "";
  584.     sClientIP[iClientid] = "";
  585.     socket_close_remote_client(sSocket, iClientid);
  586. }
  587.  
  588.  
  589. /*############################################################################*/
  590.  
  591. forward isRemoteClientConnected(iClientid);
  592. public isRemoteClientConnected(iClientid) {
  593.     return bConnected[iClientid];
  594. }
  595.  
  596. /*############################################################################*/
  597.  
  598. forward isRemoteClientAllowed(iClientid);
  599. public isRemoteClientAllowed(iClientid) {
  600.     return bClientAllowed[iClientid];
  601. }
  602.  
  603. /*############################################################################*/
  604.  
  605. forward remoteClientCount();
  606. public remoteClientCount() {
  607.     new
  608.         iCount = 0;
  609.        
  610.     for(new i = 0; i < MAX_CONN; i++)
  611.         if(isRemoteClientConnected(i) && isRemoteClientAllowed(i))
  612.             iCount++;
  613.            
  614.     return iCount;
  615. }
  616.  
  617. /*############################################################################*/
  618.  
  619. forward sendMessageToAllRemoteClients(text[]);
  620. public sendMessageToAllRemoteClients(text[]) {
  621.     for(new i = 0; i < MAX_CONN; i++) {
  622.         if(isRemoteClientConnected(i) && isRemoteClientAllowed(i)) {
  623.             new
  624.                 sMsg[128];
  625.                
  626.             format(sMsg, sizeof(sMsg), "MSG %s", text);
  627.             socket_sendto_remote_client(sSocket, i, sMsg);
  628.         }
  629.     }
  630. }
  631.  
  632. /*############################################################################*/
  633.  
  634. forward isBanned(iClientid);
  635. public isBanned(iClientid) {
  636.     new
  637.         bool:bBanned = false,
  638.         sBanIP[22],
  639.         File:fBanList = fopen("recs-banlist.txt", io_readwrite);
  640.  
  641.     while(fread(fBanList, sBanIP)) {
  642.         RemoveGarbage(sBanIP);
  643.        
  644.         if(!strcmp(sBanIP, sClientIP[iClientid])) {
  645.             bBanned = true;
  646.         }
  647.     }
  648.  
  649.     fclose(fBanList);
  650.     return bBanned;
  651. }
  652.  
  653. /*############################################################################*/
  654.  
  655. forward banRemoteClient(iClientid);
  656. public banRemoteClient(iClientid) {
  657.     new
  658.         sBanIP[22],
  659.         File:fBanList = fopen("recs-banlist.txt", io_append);
  660.    
  661.     if(fBanList) {
  662.         format(sBanIP, sizeof(sBanIP), "%s\r\n", sClientIP[iClientid]);
  663.         fwrite(fBanList, sBanIP);
  664.         fclose(fBanList);
  665.     }
  666.    
  667.     closeRemoteConnection(iClientid);
  668. }
  669.  
  670. /*############################################################################*/
  671.  
  672. forward autoMessages();
  673. public autoMessages() {
  674.     new
  675.         sIngameMsg[128];
  676.        
  677.     format(sIngameMsg, sizeof(sIngameMsg), "Connect to the chat using your Android device! Get 'SA-MP Chat' on the Play Store and connect to %s:%d", SERVER_IP, LISTEN_PORT);
  678.     SendClientMessageToAll(COLOR_LIGHTBLUE, sIngameMsg);
  679. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement