Skip to content

Commit

Permalink
Implemented renaming guilds and other stuff probably idk
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePinkUnicorn6 committed Jan 13, 2024
1 parent 38cf882 commit f608fd4
Showing 1 changed file with 77 additions and 8 deletions.
85 changes: 77 additions & 8 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Dynamic;
using System.Net.Mime;
using System.Windows.Markup;
using System.CodeDom;

class MessageServer
{
Expand All @@ -32,11 +33,20 @@ static void Main(string[] args)
log("DEBUG", "Using SQLite version: " + con.ServerVersion);
}
createDB(connectionString);
const string url = "http://localhost:8080/"; // Sets up http server
const string url = "http://+:8080/"; // Sets up http server
// TODO: log and give error if port is already in use.
HttpListener listener = new HttpListener();
listener.Prefixes.Add(url);
listener.Start();
try
{
listener.Start();
}
catch (Exception ex)
{
Console.WriteLine("Failed to start server, the port is likeley already in use");
log("ERROR", "Server failed to start. Error: ", ex);
Environment.Exit(-1);
}
Console.WriteLine("Listening for requests on " + url);
log("INFO", "Started server at " + url);
while (true)
Expand All @@ -53,7 +63,8 @@ static void Main(string[] args)
case "/api/content/sendMessage": apiSendMessage(context); break; //Post
case "/api/user/getInfo": apiGetUserInfo(context); break; //Get
case "/api/directMessage/create": apiCreateChannel(context, true); break; //Post
case "/api/guild/createChannel": apiCreateChannel(context, false); break; //Post
case "/api/guild/channel/create": apiCreateChannel(context, false); break; //Post
case "/api/guild/channel/rename": apiRenameChannel(context); break; //Post
case "/api/guild/create": apiCreateGuild(context); break; //Post
case "/api/guild/listGuilds": apiListGuilds(context); break; //Get
case "/api/guild/fetchDetails": apiFetchGuildDetails(context); break; //Get
Expand Down Expand Up @@ -789,6 +800,7 @@ static void apiCreateChannel(HttpListenerContext context, bool isDM)
string? guildID;
string? userID2;
string? responseMessage;
int code;
int? channelType;
dynamic jsonBodyObject = parseJsonPost(context);
if (jsonBodyObject == null)
Expand All @@ -807,14 +819,12 @@ static void apiCreateChannel(HttpListenerContext context, bool isDM)
}
if (!isDM)
{
channelType = int.Parse(jsonBodyObject.channelType);
channelType = 1;
}
else
{
channelType = null;
}
int code;

if (string.IsNullOrEmpty(channelName) | string.IsNullOrEmpty(token) | (isDM & string.IsNullOrEmpty(userID2)) | (!isDM & string.IsNullOrEmpty(guildID))) {
returnMissingParameterError(out responseMessage, out code);
}
Expand All @@ -839,8 +849,7 @@ static void apiCreateChannel(HttpListenerContext context, bool isDM)
}
else
{

int userPermission = checkUserGuildPerms(userID1, guildID);
int userPermission = checkUserGuildPerms(guildID, userID1);
if (userPermission <= guildNotExist)
{
var responseJson = new { error = "Invalid GuildID", errcode = "INVALID_GUILDID" };
Expand All @@ -863,6 +872,66 @@ static void apiCreateChannel(HttpListenerContext context, bool isDM)
}
sendResponse(context, typeJson, code, responseMessage);
}
static void apiRenameChannel(HttpListenerContext context)
{
string? channelName;
string? channelID;
string? token;
string? responseMessage;
int code;
dynamic jsonBodyObject = parseJsonPost(context);
if (jsonBodyObject == null)
{
var responseJson = new { error = "Incorrectly formatted request", errcode = "FORMATTING_ERROR"};
responseMessage = JsonConvert.SerializeObject(responseJson);
sendResponse(context, typeJson, 400, responseMessage);
return;
}
else
{
token = jsonBodyObject.token;
channelName = jsonBodyObject.channelName;
channelID = jsonBodyObject.channelID;
}
if (string.IsNullOrEmpty(channelName) | string.IsNullOrEmpty(token)) {
returnMissingParameterError(out responseMessage, out code);
}
else if (!tokenValid(token)) returnInvalidTokenError(out responseMessage, out code);
else
{
string userID1 = getUserIDFromToken(token);
int userPermission = checkUserChannelPerms(channelID, userID1);
if (userPermission <= guildNotExist)
{
var responseJson = new { error = "Invalid GuildID", errcode = "INVALID_GUILDID" };
responseMessage = JsonConvert.SerializeObject(responseJson);
code = 400;
}
else if (userPermission < admin) // Reject the request if the user has lower permissions than admin
{
var responseJson = new { error = "You do not have permissions to carry out this action", errcode = "FORBIDDEN" };
responseMessage = JsonConvert.SerializeObject(responseJson);
code = 403;
}
else
{
using (var con = new SQLiteConnection(connectionString))
using (var cmd = new SQLiteCommand(con))
{
con.Open();
cmd.CommandText = @"UPDATE tblChannels
SET ChannelName = @ChannelName
WHERE ChannelID = @ChannelID";
cmd.Parameters.AddWithValue("ChannelID", channelID);
cmd.Parameters.AddWithValue("ChannelName", channelName);
cmd.ExecuteNonQuery();
}
responseMessage = null;
code = 200;
}
sendResponse(context, typeJson, code, responseMessage);
}
}
static void createDM(string userID1, string userID2)
{
string channelID = Guid.NewGuid().ToString();
Expand Down

0 comments on commit f608fd4

Please sign in to comment.