Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*** ***
- * RChat - Raf's External Chat System - Server *
- *** ***
- * @Author Rafael 'R@f' Keramidas <rafael@keramid.as> *
- * @Date 9th May 2013 *
- * @Version 1.0.0 *
- * @Licence MIT License *
- * @Comment Android client available (SA-MP Chat) *
- * *
- * Copyright (C) 2013 Rafael Keramidas *
- * *
- * Permission is hereby granted, free of charge, to any *
- * person obtaining a copy of this software and *
- * associated documentation files (the "Software"), to *
- * deal in the Software without restriction, including *
- * without limitation the rights to use, copy, modify, *
- * merge, publish, distribute, sublicense, and/or sell *
- * copies of the Software, and to permit persons to whom *
- * the Software is furnished to do so, subject to the *
- * following conditions: *
- * *
- * The above copyright notice and this permission notice *
- * shall be included in all copies or substantial *
- * portions of the Software. *
- * *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF *
- * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT *
- * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS *
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO *
- * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE *
- * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN *
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
- * USE OR OTHER DEALINGS IN THE SOFTWARE. *
- *** ***/
- #include <a_samp>
- #include <socket>
- /* Config */
- #define LISTEN_PORT 9014 /* TCP port to listen to (default 9014) */
- #define SERVER_IP "192.168.1.47" /* Server IP (public IP) */
- #define MAX_CONN 20 /* Maximum TCP connections allowed (amount of clients X 2) */
- #define PASSWORD "test" /* Password to connect to the chat (leave blank for none) */
- #define CLIENT_COLOR "00FF00" /* Color for remote clients in the chat */
- #define INFOMSG_COLOR 0xAAAAAAAA /* Color for connect, disconnect messages and other infos */
- #define ENABLE_AUTOMSG true /* Automatic messages informing players that they can remotely connect to the chat */
- /* DO NOT EDIT BELOW (if you don't know what you're doing) */
- /* Script Info */
- #define MAJOR_VERSION 1
- #define MINOR_VERSION 0
- #define BUGFIX 0
- #define LAST_UPDATE "09.05.2013"
- /* Versions (DO NOT CHANGE !!!) */
- #define MIN_VERSION 1 /* Minimum client version accepted (integer) */
- #define CURR_VERSION 1 /* Current server version (integer) */
- /* COLORS */
- #define COLOR_LIGHTBLUE 0x33DAFFAA
- #define COLOR_RED 0xFF0000FF
- #define COLOR_WHITE 0xFFFFFFAA
- #define COLOR_YELLOW 0xFFD700AA
- /* Variables */
- new
- Socket:sSocket,
- bool:bClientAllowed[MAX_CONN],
- bool:bConnected[MAX_CONN],
- sClientName[MAX_CONN][24],
- sClientDevice[MAX_CONN][30],
- sClientIP[MAX_CONN][20],
- sClientUniqueID[MAX_CONN][17];
- /*############################################################################*/
- public OnFilterScriptInit() {
- printf("++++++++++++++++++++++++++++++++++++++++");
- printf("++ RChat - Raf's External Chat System ++");
- printf("++ V%d.%d.%d - Last update: %s ++", MAJOR_VERSION, MINOR_VERSION, BUGFIX, LAST_UPDATE);
- printf("++ Script by Rafael 'R@f' Keramidas ++");
- printf("++++++++++++++++++++++++++++++++++++++++\n");
- for(new i = 0; i < MAX_CONN; i++)
- bConnected[i] = false;
- sSocket = socket_create(TCP);
- if(is_socket_valid(sSocket)) {
- socket_set_max_connections(sSocket, MAX_CONN);
- socket_bind(sSocket, SERVER_IP);
- socket_listen(sSocket, LISTEN_PORT);
- }
- #if ENABLE_AUTOMSG == true
- /* Message every 10 minutes */
- SetTimer("autoMessages", 10*60000, true);
- #endif
- return true;
- }
- /*############################################################################*/
- public OnFilterScriptExit() {
- if(is_socket_valid(sSocket))
- socket_destroy(sSocket);
- return true;
- }
- /*############################################################################*/
- public OnPlayerConnect(playerid) {
- new
- sPlayerName[MAX_PLAYER_NAME],
- sRemoteMsg[128];
- GetPlayerName(playerid, sPlayerName, sizeof(sPlayerName));
- format(sRemoteMsg, sizeof(sRemoteMsg), "%s(%d) has joined the server\n", sPlayerName, playerid);
- sendMessageToAllRemoteClients(sRemoteMsg);
- return true;
- }
- /*############################################################################*/
- public OnPlayerDisconnect(playerid, reason) {
- new
- sPlayerName[MAX_PLAYER_NAME],
- sRemoteMsg[128];
- GetPlayerName(playerid, sPlayerName, sizeof(sPlayerName));
- switch(reason) {
- case 0:
- format(sRemoteMsg, sizeof(sRemoteMsg), "%s(%d) has left the server (timeout) \n", sPlayerName, playerid);
- case 1:
- format(sRemoteMsg, sizeof(sRemoteMsg), "%s(%d) has left the server (leaving) \n", sPlayerName, playerid);
- case 2:
- format(sRemoteMsg, sizeof(sRemoteMsg), "%s(%d) has left the server (kicked/banned) \n", sPlayerName, playerid);
- }
- sendMessageToAllRemoteClients(sRemoteMsg);
- return true;
- }
- /*############################################################################*/
- public OnPlayerDeath(playerid, killerid, reason) {
- /* Adding deaths in a later version */
- return true;
- }
- /*############################################################################*/
- public OnPlayerText(playerid, text[]) {
- new
- sPlayerName[MAX_PLAYER_NAME],
- sRemoteMsg[128];
- GetPlayerName(playerid, sPlayerName, sizeof(sPlayerName));
- format(sRemoteMsg, sizeof(sRemoteMsg), "%s(%d): %s\n", sPlayerName, playerid, text);
- sendMessageToAllRemoteClients(sRemoteMsg);
- return true;
- }
- /*############################################################################*/
- public OnPlayerCommandText(playerid, cmdtext[]) {
- new
- sCmd[128],
- sTmp[128],
- sIngameMsg[128],
- sRemoteMsg[128],
- iRid,
- iIndex;
- sCmd = strtok(cmdtext, iIndex);
- if (strcmp(sCmd, "/showremote", true) == 0) {
- SendClientMessage(playerid, COLOR_LIGHTBLUE, "Currently remote clients connected: ");
- if(remoteClientCount() > 0) {
- for(new i = 0; i < MAX_CONN; i++) {
- if(isRemoteClientConnected(i) && isRemoteClientAllowed(i)) {
- format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) - Client: %s", sClientName[i], i, sClientDevice[i]);
- SendClientMessage(playerid, COLOR_WHITE, sIngameMsg);
- }
- }
- }
- else {
- SendClientMessage(playerid, COLOR_WHITE, "None");
- }
- return true;
- }
- if (strcmp(sCmd, "/kickremote", true) == 0) {
- sTmp = strtok(cmdtext, iIndex);
- if(!IsPlayerAdmin(playerid)) {
- SendClientMessage(playerid, COLOR_RED, "RECS: You have to be admin to use this command!");
- return true;
- }
- if(!strlen(sTmp)) {
- SendClientMessage(playerid, COLOR_YELLOW, "USAGE: /kickremote [clientid]");
- return true;
- }
- iRid = strval(sTmp);
- if (!isRemoteClientConnected(iRid) && !isRemoteClientAllowed(iRid)) {
- SendClientMessage(playerid, COLOR_RED, "RECS: Remote client is not connected!");
- return true;
- }
- format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) has been kicked from the server", sClientName[iRid], iRid);
- SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
- format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) has been kicked from the server\n", sClientName[iRid], iRid);
- sendMessageToAllRemoteClients(sRemoteMsg);
- bClientAllowed[iRid] = false;
- socket_sendto_remote_client(sSocket, iRid, "ERR You have been kicked from the server!\n");
- closeRemoteConnection(iRid);
- return true;
- }
- if (strcmp(sCmd, "/banremote", true) == 0) {
- sTmp = strtok(cmdtext, iIndex);
- if(!IsPlayerAdmin(playerid)) {
- SendClientMessage(playerid, COLOR_RED, "RECS: You have to be admin to use this command!");
- return true;
- }
- if(!strlen(sTmp)) {
- SendClientMessage(playerid, COLOR_YELLOW, "USAGE: /banremote [clientid]");
- return true;
- }
- iRid = strval(sTmp);
- if (!isRemoteClientConnected(iRid) && !isRemoteClientAllowed(iRid)) {
- SendClientMessage(playerid, COLOR_RED, "RECS: Remote client is not connected!");
- return true;
- }
- format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) has been banned from the server", sClientName[iRid], iRid);
- SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
- format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) has been banned from the server\n", sClientName[iRid], iRid);
- sendMessageToAllRemoteClients(sRemoteMsg);
- bClientAllowed[iRid] = false;
- socket_sendto_remote_client(sSocket, iRid, "ERR You have been banned from the server!\n");
- banRemoteClient(iRid);
- return true;
- }
- return false;
- }
- /*############################################################################*/
- public onSocketRemoteConnect(Socket:id, remote_client[], remote_clientid) {
- printf("[RECS] Incoming connection from [%d:%s]", remote_clientid, remote_client);
- format(sClientIP[remote_clientid], 20, "%s", remote_client);
- if(isBanned(remote_clientid)) {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR You are banned from this server !\n");
- closeRemoteConnection(remote_clientid);
- }
- bClientAllowed[remote_clientid] = false;
- bConnected[remote_clientid] = true;
- sClientName[remote_clientid] = "";
- sClientDevice[remote_clientid] = "";
- sClientIP[remote_clientid] = "";
- return true;
- }
- /*############################################################################*/
- public onSocketRemoteDisconnect(Socket:id, remote_clientid) {
- new
- sRemoteMsg[128],
- sIngameMsg[128];
- if(bClientAllowed[remote_clientid] == true) {
- format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) disconnected from the chat", sClientName[remote_clientid], remote_clientid);
- SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
- format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) disconnected from the chat\n", sClientName[remote_clientid], remote_clientid);
- sendMessageToAllRemoteClients(sRemoteMsg);
- }
- printf("[RECS] Remote client [%d] has disconnected.", remote_clientid);
- bClientAllowed[remote_clientid] = false;
- bConnected[remote_clientid] = false;
- sClientName[remote_clientid] = "";
- sClientDevice[remote_clientid] = "";
- sClientIP[remote_clientid] = "";
- return true;
- }
- /*############################################################################*/
- public onSocketReceiveData(Socket:id, remote_clientid, data[], data_len)
- {
- new
- sType[20],
- sRemoteMsg[128],
- sIngameMsg[128],
- iIndex;
- sType = strtok(data, iIndex);
- /* Empty string */
- if(strlen(data) == 2) {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR Empty message !\n");
- closeRemoteConnection(remote_clientid);
- }
- /* Name type */
- else if(!strcmp(sType, "CON", false, 3)) {
- new
- sName[24],
- sClient[30],
- sVersion[20],
- sPassword[30],
- sRandomString[17];
- sName = strtok(data, iIndex);
- sClient = strtok(data, iIndex);
- sVersion = strtok(data, iIndex);
- sPassword = strtok(data, iIndex);
- if(strlen(sName) < 3 || strlen(sName) > 24) {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR Invalid name !\n");
- closeRemoteConnection(remote_clientid);
- }
- else {
- if(in_array(sName, sClientName)) {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR This name is already used !\n");
- closeRemoteConnection(remote_clientid);
- }
- else {
- if(strlen(sClient) < 3 || strlen(sClient) > 30) {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR Invalid device name !\n");
- closeRemoteConnection(remote_clientid);
- }
- else {
- if(strval(sVersion) < MIN_VERSION) {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR Your client version is not compatible with the server !\n");
- closeRemoteConnection(remote_clientid);
- }
- else {
- format(sRandomString, sizeof(sRandomString), randomString());
- if(strlen(PASSWORD) != 0) {
- if(strlen(sPassword) == 0) {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR Password is empty !\n");
- closeRemoteConnection(remote_clientid);
- }
- else {
- if(strcmp(sPassword, PASSWORD)) {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR Incorrect password !\n");
- closeRemoteConnection(remote_clientid);
- }
- else {
- format(sClientName[remote_clientid], 24, "%s", sName);
- format(sClientDevice[remote_clientid], 30, "%s", sClient);
- format(sRemoteMsg, sizeof(sRemoteMsg), "CID %s\n", sRandomString);
- socket_sendto_remote_client(sSocket, remote_clientid, sRemoteMsg);
- format(sClientUniqueID[remote_clientid], 17, sRandomString);
- bClientAllowed[remote_clientid] = true;
- format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) connected to the chat (Client: %s)", sClientName[remote_clientid], remote_clientid, sClientDevice[remote_clientid]);
- SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
- format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) connected to the chat (Client: %s) \n", sClientName[remote_clientid], remote_clientid, sClientDevice[remote_clientid]);
- sendMessageToAllRemoteClients(sRemoteMsg);
- }
- }
- }
- else {
- format(sClientName[remote_clientid], 24, "%s", sName);
- format(sClientDevice[remote_clientid], 30, "%s", sClient);
- format(sRemoteMsg, sizeof(sRemoteMsg), "CID %s\n", sRandomString);
- socket_sendto_remote_client(sSocket, remote_clientid, sRemoteMsg);
- format(sClientUniqueID[remote_clientid], 17, sRandomString);
- bClientAllowed[remote_clientid] = true;
- format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) connected to the chat (Client: %s)", sClientName[remote_clientid], remote_clientid, sClientDevice[remote_clientid]);
- SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
- format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) connected to the chat (Client: %s) \n", sClientName[remote_clientid], remote_clientid, sClientDevice[remote_clientid]);
- sendMessageToAllRemoteClients(sRemoteMsg);
- }
- }
- }
- }
- }
- }
- /* Version type */
- else if(!strcmp(sType, "VER", false, 3)) {
- new
- sMsg[128];
- format(sMsg, sizeof(sMsg), "VER %d\n", CURR_VERSION);
- socket_sendto_remote_client(sSocket, remote_clientid, sMsg);
- }
- /* Message type */
- else if(!strcmp(sType, "MSG", false, 3)) {
- new
- sCode[20],
- iClientID;
- sCode = strtok(data, iIndex);
- iClientID = array_search(sCode, sClientUniqueID);
- if(iClientID == -1) {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR You are not allowed to send messages !\n");
- closeRemoteConnection(remote_clientid);
- }
- else {
- if(strlen(data[4]) == 0) {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR You can't send empty messages !\n");
- closeRemoteConnection(remote_clientid);
- }
- else {
- socket_sendto_remote_client(sSocket, remote_clientid, "MOK\n");
- closeRemoteConnection(remote_clientid);
- format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d): %s", sClientName[iClientID], iClientID, data[21]);
- sendMessageToAllRemoteClients(sRemoteMsg);
- format(sIngameMsg, sizeof(sIngameMsg), "{"CLIENT_COLOR"}[REMOTE]%s(%d):{FFFFFF} %s", sClientName[iClientID], iClientID, data[21]);
- SendClientMessageToAll(-1, sIngameMsg);
- }
- }
- }
- /* Message type */
- else if(!strcmp(sType, "CLS", false, 3)) {
- new
- sCode[20],
- iClientID;
- sCode = strtok(data, iIndex);
- iClientID = array_search(sCode, sClientUniqueID);
- if(iClientID == -1) {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR Client not found !\n");
- closeRemoteConnection(remote_clientid);
- }
- else {
- closeRemoteConnection(iClientID);
- closeRemoteConnection(remote_clientid);
- }
- }
- /* Unknown type */
- else {
- socket_sendto_remote_client(sSocket, remote_clientid, "ERR Unknown type !\n");
- closeRemoteConnection(remote_clientid);
- }
- return true;
- }
- /*############################################################################*/
- strtok (const string[], &index) {
- new length = strlen(string);
- while ((index < length) && (string[index] <= ' ')) {
- index++;
- }
- new offset = index;
- new result[20];
- while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1))) {
- result[index - offset] = string[index];
- index++;
- }
- result[index - offset] = EOS;
- return result;
- }
- /*############################################################################*/
- /* By Ssk */
- RemoveGarbage(str[]) {
- new
- i = 0;
- while(str[i] != 0) {
- if(str[i] == '\n' || str[i] == '\r') {
- str[i] = 0;
- }
- else {
- i++;
- }
- }
- }
- /*############################################################################*/
- /* By Yiin */
- stock randomString() {
- new
- _text[17];
- for(new i = 0; i < 17; i++) {
- switch(random(2)) {
- case 0: _text[i] = random(25) + 66;
- case 1: _text[i] = random(25) + 98;
- }
- }
- return _text;
- }
- /*############################################################################*/
- stock array_search(const sStringToFind[], const aTargetArray[][], const iArraySize = sizeof aTargetArray) {
- new
- iIndex = 0;
- if(strlen(sStringToFind) == 16) {
- while(iIndex < iArraySize) {
- if(strlen(aTargetArray[iIndex]) == 16)
- if(!strcmp(sStringToFind, aTargetArray[iIndex], true, 16))
- return iIndex;
- iIndex++;
- }
- }
- return -1;
- }
- /*############################################################################*/
- stock in_array(const sStringToFind[], const aTargetArray[][], const iArraySize = sizeof aTargetArray) {
- new
- iIndex = 0;
- if(strlen(sStringToFind) != 0) {
- while(iIndex < iArraySize) {
- if(strlen(aTargetArray[iIndex]) != 0)
- if(!strcmp(sStringToFind, aTargetArray[iIndex]))
- return true;
- iIndex++;
- }
- }
- return false;
- }
- /*############################################################################*/
- forward closeRemoteConnection(iClientid);
- public closeRemoteConnection(iClientid) {
- new
- sRemoteMsg[128],
- sIngameMsg[128];
- if(bClientAllowed[iClientid] == true) {
- format(sIngameMsg, sizeof(sIngameMsg), "[REMOTE]%s(%d) disconnected from the chat", sClientName[iClientid], iClientid);
- SendClientMessageToAll(INFOMSG_COLOR, sIngameMsg);
- format(sRemoteMsg, sizeof(sRemoteMsg), "[REMOTE]%s(%d) disconnected from the chat\n", sClientName[iClientid], iClientid);
- sendMessageToAllRemoteClients(sRemoteMsg);
- }
- printf("[RECS] Remote client [%d] has disconnected.", iClientid);
- bClientAllowed[iClientid] = false;
- bConnected[iClientid] = false;
- sClientName[iClientid] = "";
- sClientDevice[iClientid] = "";
- sClientIP[iClientid] = "";
- socket_close_remote_client(sSocket, iClientid);
- }
- /*############################################################################*/
- forward isRemoteClientConnected(iClientid);
- public isRemoteClientConnected(iClientid) {
- return bConnected[iClientid];
- }
- /*############################################################################*/
- forward isRemoteClientAllowed(iClientid);
- public isRemoteClientAllowed(iClientid) {
- return bClientAllowed[iClientid];
- }
- /*############################################################################*/
- forward remoteClientCount();
- public remoteClientCount() {
- new
- iCount = 0;
- for(new i = 0; i < MAX_CONN; i++)
- if(isRemoteClientConnected(i) && isRemoteClientAllowed(i))
- iCount++;
- return iCount;
- }
- /*############################################################################*/
- forward sendMessageToAllRemoteClients(text[]);
- public sendMessageToAllRemoteClients(text[]) {
- for(new i = 0; i < MAX_CONN; i++) {
- if(isRemoteClientConnected(i) && isRemoteClientAllowed(i)) {
- new
- sMsg[128];
- format(sMsg, sizeof(sMsg), "MSG %s", text);
- socket_sendto_remote_client(sSocket, i, sMsg);
- }
- }
- }
- /*############################################################################*/
- forward isBanned(iClientid);
- public isBanned(iClientid) {
- new
- bool:bBanned = false,
- sBanIP[22],
- File:fBanList = fopen("recs-banlist.txt", io_readwrite);
- while(fread(fBanList, sBanIP)) {
- RemoveGarbage(sBanIP);
- if(!strcmp(sBanIP, sClientIP[iClientid])) {
- bBanned = true;
- }
- }
- fclose(fBanList);
- return bBanned;
- }
- /*############################################################################*/
- forward banRemoteClient(iClientid);
- public banRemoteClient(iClientid) {
- new
- sBanIP[22],
- File:fBanList = fopen("recs-banlist.txt", io_append);
- if(fBanList) {
- format(sBanIP, sizeof(sBanIP), "%s\r\n", sClientIP[iClientid]);
- fwrite(fBanList, sBanIP);
- fclose(fBanList);
- }
- closeRemoteConnection(iClientid);
- }
- /*############################################################################*/
- forward autoMessages();
- public autoMessages() {
- new
- sIngameMsg[128];
- 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);
- SendClientMessageToAll(COLOR_LIGHTBLUE, sIngameMsg);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement