Skip to content
This repository has been archived by the owner on Sep 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #48 from dmitrydnl/v0.3
Browse files Browse the repository at this point in the history
V0.3
  • Loading branch information
dmitrydnl authored Nov 4, 2019
2 parents 1008612 + dcb5e4a commit 992d7aa
Show file tree
Hide file tree
Showing 47 changed files with 875 additions and 301 deletions.
2 changes: 1 addition & 1 deletion src/StashBot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ Global
$4.scope = text/x-json
$0.StandardHeader = $5
$0.VersionControlPolicy = $6
version = 0.2
version = 0.3
EndGlobalSection
EndGlobal
17 changes: 17 additions & 0 deletions src/StashBot/BotResponses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"welcomeMessage": "Hi, good to see you!",
"mainCommands": "Registration: /reg\nAuthorization: /auth\nInformation about me: /info\nClose chat in any time: /e or /exit",
"information": "This is open source bot for stashing in Telegram Messenger.\nThe code you can find here: https://github.com/dmitrydnl/StashBot",
"registrationWarning": "If you have already registered you will lose all your old data!\nAre you sure? /yes or /no",
"registrationReady": "Input your password or /cancel",
"successRegistration": "Success!\nNow you can auth with password",
"passwordEmpty": "Input password",
"passwordMinLength": "Password min length 12!",
"passwordMaxLength": "Password max length 25!",
"passwordCharacters": "Password can contain only letters, numbers and special characters!",
"authorisationReady": "Input your password or /back",
"successAuthorisation": "Success!",
"failAuthorisation": "WRONG",
"login": "Input message to save it in stash.\nGet messages in stash: /stash\nLogout: /logout",
"logout": "You're logged out"
}
4 changes: 4 additions & 0 deletions src/StashBot/BotSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"chatSessionsClearInterval": 10,
"chatSessionLiveTime": 60
}
16 changes: 15 additions & 1 deletion src/StashBot/StashBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<ReleaseVersion>0.2</ReleaseVersion>
<ReleaseVersion>0.3</ReleaseVersion>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
</PropertyGroup>

Expand All @@ -30,5 +30,19 @@
<Folder Include="app\Module\Database\Stash\" />
<Folder Include="app\Module\Message\Handler\ChatStateHandler\" />
<Folder Include="app\Module\Secure\AesHmacCrypto\" />
<Folder Include="app\BotSettings\" />
<Folder Include="app\BotResponses\" />
</ItemGroup>
<ItemGroup>
<None Remove="BotSettings.json" />
<None Remove="BotResponses.json" />
</ItemGroup>
<ItemGroup>
<Content Include="BotSettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="BotResponses.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
11 changes: 6 additions & 5 deletions src/StashBot/app/AppSettings/BotToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@

namespace StashBot.AppSetting
{
internal class BotToken
internal static class BotToken
{
private const string BOT_TOKEN_FILE_NAME = "AppSettings.json";
private const string APP_SETTINGS_FILE_NAME = "AppSettings.json";

private static string botToken;

internal string Get()
internal static string Get()
{
if (string.IsNullOrEmpty(botToken))
{
string text = File.ReadAllText(BOT_TOKEN_FILE_NAME);
string text = File.ReadAllText(APP_SETTINGS_FILE_NAME);
dynamic jsonObject = JsonConvert.DeserializeObject<dynamic>(text);
botToken = (string)jsonObject.botToken;
}

return botToken;
}

internal void Set(string newBotToken)
internal static void Set(string newBotToken)
{
if (string.IsNullOrEmpty(botToken) && !string.IsNullOrEmpty(newBotToken))
{
Expand Down
21 changes: 21 additions & 0 deletions src/StashBot/app/BotResponses/ResponseType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace StashBot.BotResponses
{
internal enum ResponseType
{
WelcomeMessage,
MainCommands,
Information,
RegistrationWarning,
RegistrationReady,
SuccessRegistration,
PasswordEmpty,
PasswordMinLength,
PasswordMaxLength,
PasswordCharacters,
AuthorisationReady,
SuccessAuthorisation,
FailAuthorisation,
Login,
Logout
}
}
46 changes: 46 additions & 0 deletions src/StashBot/app/BotResponses/TextResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace StashBot.BotResponses
{
internal static class TextResponse
{
private const string BOT_RESPONSES_FILE_NAME = "BotResponses.json";

private static Dictionary<ResponseType, string> responses;

internal static string Get(ResponseType responseType)
{
return responses[responseType];
}

internal static void SetUpResponses()
{
if (responses != null)
{
return;
}

responses = new Dictionary<ResponseType, string>();
string text = File.ReadAllText(BOT_RESPONSES_FILE_NAME);
dynamic jsonObject = JsonConvert.DeserializeObject<dynamic>(text);

responses.Add(ResponseType.WelcomeMessage, (string)jsonObject.welcomeMessage);
responses.Add(ResponseType.MainCommands, (string)jsonObject.mainCommands);
responses.Add(ResponseType.Information, (string)jsonObject.information);
responses.Add(ResponseType.RegistrationWarning, (string)jsonObject.registrationWarning);
responses.Add(ResponseType.RegistrationReady, (string)jsonObject.registrationReady);
responses.Add(ResponseType.SuccessRegistration, (string)jsonObject.successRegistration);
responses.Add(ResponseType.PasswordEmpty, (string)jsonObject.passwordEmpty);
responses.Add(ResponseType.PasswordMinLength, (string)jsonObject.passwordMinLength);
responses.Add(ResponseType.PasswordMaxLength, (string)jsonObject.passwordMaxLength);
responses.Add(ResponseType.PasswordCharacters, (string)jsonObject.passwordCharacters);
responses.Add(ResponseType.AuthorisationReady, (string)jsonObject.authorisationReady);
responses.Add(ResponseType.SuccessAuthorisation, (string)jsonObject.successAuthorisation);
responses.Add(ResponseType.FailAuthorisation, (string)jsonObject.failAuthorisation);
responses.Add(ResponseType.Login, (string)jsonObject.login);
responses.Add(ResponseType.Logout, (string)jsonObject.logout);
}
}
}
54 changes: 54 additions & 0 deletions src/StashBot/app/BotSettings/ChatSessionSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.IO;
using Newtonsoft.Json;

namespace StashBot.BotSettings
{
internal static class ChatSessionSettings
{
private const string BOT_SETTINGS_FILE_NAME = "BotSettings.json";

private static bool isSetUp;
private static int chatSessionsClearInterval;
private static int chatSessionLiveTime;

internal static int ChatSessionsClearInterval
{
get
{
SetUpSettings();
return chatSessionsClearInterval;
}
private set
{
chatSessionsClearInterval = value;
}
}

internal static int ChatSessionLiveTime
{
get
{
SetUpSettings();
return chatSessionLiveTime;
}
private set
{
chatSessionLiveTime = value;
}
}

private static void SetUpSettings()
{
if (isSetUp)
{
return;
}

string text = File.ReadAllText(BOT_SETTINGS_FILE_NAME);
dynamic jsonObject = JsonConvert.DeserializeObject<dynamic>(text);
ChatSessionsClearInterval = (int)jsonObject.chatSessionsClearInterval;
ChatSessionLiveTime = (int)jsonObject.chatSessionLiveTime;
isSetUp = true;
}
}
}
11 changes: 9 additions & 2 deletions src/StashBot/app/ITelegramUserMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ long ChatId
get;
}

int MessageId
DateTime DateSent
{
get;
}

DateTime DateSent
int MessageId
{
get;
}
Expand All @@ -23,5 +23,12 @@ string Message
{
get;
}

string PhotoId
{
get;
}

bool IsEmpty();
}
}
9 changes: 9 additions & 0 deletions src/StashBot/app/ITelegramUserMessageFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Telegram.Bot.Types;

namespace StashBot
{
internal interface ITelegramUserMessageFactory
{
ITelegramUserMessage Create(Message telegramMessage);
}
}
19 changes: 11 additions & 8 deletions src/StashBot/app/Module/Database/Account/DatabaseAccountLocal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ internal DatabaseAccountLocal()

public void CreateNewUser(long chatId, string password)
{
IDatabaseManager databaseManager =
ModulesManager.GetModulesManager().GetDatabaseManager();
IDatabaseManager databaseManager = ModulesManager.GetModulesManager().GetDatabaseManager();

if (databaseManager.IsStashExist(chatId))
{
databaseManager.ClearStash(chatId);
}

User newUser = new User(chatId, password);
if (IsUserExist(chatId))
{
databaseManager.ClearStash(chatId);
usersDatabase[chatId] = newUser;
}
else
Expand All @@ -28,11 +31,6 @@ public void CreateNewUser(long chatId, string password)
}
}

public bool IsUserExist(long chatId)
{
return usersDatabase.ContainsKey(chatId);
}

public IUser GetUser(long chatId)
{
if (IsUserExist(chatId))
Expand All @@ -42,5 +40,10 @@ public IUser GetUser(long chatId)

return null;
}

public bool IsUserExist(long chatId)
{
return usersDatabase.ContainsKey(chatId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
internal interface IDatabaseAccount
{
void CreateNewUser(long chatId, string password);
bool IsUserExist(long chatId);
IUser GetUser(long chatId);
bool IsUserExist(long chatId);
}
}
24 changes: 24 additions & 0 deletions src/StashBot/app/Module/Database/Account/IUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace StashBot.Module.Database
{
internal interface IUser
{
long ChatId
{
get;
}

bool IsAuthorized
{
get;
}

string EncryptedPassword
{
get;
}

void Login(string password);
void Logout();
bool ValidatePassword(string password);
}
}
69 changes: 69 additions & 0 deletions src/StashBot/app/Module/Database/Account/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using StashBot.Module.Secure;

namespace StashBot.Module.Database
{
internal class User : IUser
{
public long ChatId
{
get;
private set;
}

public bool IsAuthorized
{
get;
private set;
}

public string EncryptedPassword
{
get;
private set;
}

private readonly string hashPassword;

internal User(long chatId, string password)
{
if (string.IsNullOrEmpty(password))
{
throw new ArgumentException("Password cannot be null");
}

ISecureManager secureManager = ModulesManager.GetModulesManager().GetSecureManager();

ChatId = chatId;
IsAuthorized = false;
hashPassword = secureManager.CalculateHash(password);
EncryptedPassword = null;
}

public void Login(string password)
{
if (string.IsNullOrEmpty(password))
{
throw new ArgumentException("Password cannot be null");
}

ISecureManager secureManager = ModulesManager.GetModulesManager().GetSecureManager();

EncryptedPassword = secureManager.EncryptWithAes(password);
IsAuthorized = true;
}

public void Logout()
{
EncryptedPassword = null;
IsAuthorized = false;
}

public bool ValidatePassword(string password)
{
ISecureManager secureManager = ModulesManager.GetModulesManager().GetSecureManager();

return secureManager.CompareWithHash(password, hashPassword);
}
}
}
Loading

0 comments on commit 992d7aa

Please sign in to comment.