added many models and continued drafting

This commit is contained in:
Dennis Eichhorn 2022-09-20 14:26:55 +02:00
parent 80db114771
commit a6193462d4
14 changed files with 408 additions and 46 deletions

View File

View File

@ -0,0 +1,59 @@
/**
* Karaka
*
* @package Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
#ifndef MODELS_ACCOUNT_H
#define MODELS_ACCOUNT_H
#include <stdio.h>
#include <stdlib.h>
#include "AccountStatus.h"
#include "Organization.h"
namespace Models {
typedef struct {
int id = 0;
AccountStatus status = AccountStatus::ACTIVE;
char *email = NULL;
char *lang = NULL;
Organization *org = NULL;
time_t created_at = 0;
} Account;
inline
void freeAccount(Account *obj)
{
if (obj->email != NULL) {
free(obj->email);
obj->email = NULL;
}
if (obj->lang != NULL) {
free(obj->lang);
obj->lang = NULL;
}
if (obj->org != NULL) {
freeOrganization(obj->org);
obj->org = NULL;
}
free(obj);
}
}
#endif

View File

@ -0,0 +1,20 @@
/**
* Karaka
*
* @package Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
#ifndef MODELS_ACCOUNT_STATUS_H
#define MODELS_ACCOUNT_STATUS_H
namespace Models {
typedef enum {
ACTIVE = 1,
INACTIVE = 2
} AccountStatus;
}
#endif

View File

@ -10,17 +10,21 @@
#ifndef MODELS_DB_H
#define MODELS_DB_H
namespace Models {
class Db {
private:
#include <stdio.h>
public:
static inline
int setup_connection (char *cfg)
{
return 0;
}
};
namespace Models {
namespace Db {
inline
int setup_connection (char *cfg)
{
return 0;
}
Resource *get_unchecked_resources(time_t olderThan)
{
}
}
}
#endif

View File

@ -0,0 +1,20 @@
/**
* Karaka
*
* @package Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
#ifndef MODELS_INSTALL_TYPE_H
#define MODELS_INSTALL_TYPE_H
namespace Models {
typedef enum {
WEB = 0,
LOCAL = 1
} InstallType;
}
#endif

View File

@ -0,0 +1,30 @@
/**
* Karaka
*
* @package Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
#ifndef MODELS_ORGANIZATION_H
#define MODELS_ORGANIZATION_H
#include <stdio.h>
#include <stdlib.h>
#include "ReosurceStatus.h"
namespace Models {
typedef struct {
int id = 0;
} Organization;
inline
void freeOrganization(Organization *obj)
{
free(obj);
}
}
#endif

View File

@ -0,0 +1,20 @@
/**
* Karaka
*
* @package Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
#ifndef MODELS_RESOURCE_STATUS_H
#define MODELS_RESOURCE_STATUS_H
namespace Models {
typedef enum {
ACTIVE = 1,
INACTIVE = 2
} ResourceStatus;
}
#endif

View File

@ -0,0 +1,88 @@
/**
* Karaka
*
* @package Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
#ifndef MODELS_RESOURCE_H
#define MODELS_RESOURCE_H
#include <stdio.h>
#include <stdlib.h>
#include "ReosurceStatus.h"
#include "Organization.h"
#include "ResourceInfo.h"
namespace Models {
typedef struct {
int id = 0;
ResourceStatus status = ResourceStatus::INACTIVE;
char *uri = NULL;
char *xpath = NULL;
char *hash = NULL;
char *last_version_path = NULL;
time_t last_version_date = 0;
time_t checked_at = 0;
Organization *org = NULL;
time_t created_at = 0;
ResourceInfo *info = NULL;
} Resource;
inline
void freeResource(Resource *obj)
{
if (obj->uri != NULL) {
free(obj->uri);
obj->uri = NULL;
}
if (obj->xpath != NULL) {
free(obj->xpath);
obj->xpath = NULL;
}
if (obj->hash != NULL) {
free(obj->hash);
obj->hash = NULL;
}
if (obj->last_version_path != NULL) {
free(obj->last_version_path);
obj->last_version_path = NULL;
}
if (obj->info != NULL) {
free(obj->info);
obj->info = NULL;
}
if (obj->org != NULL) {
freeOrganization(obj->org);
obj->org = NULL;
}
free(obj);
}
}
#endif

View File

@ -0,0 +1,47 @@
/**
* Karaka
*
* @package Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
#ifndef MODELS_RESOURCE_INFO_H
#define MODELS_RESOURCE_INFO_H
#include <stdio.h>
#include <stdlib.h>
#include "Account.h"
namespace Models {
typedef struct {
int id = 0;
char *mail = NULL;
Account *account = NULL;
int resource = 0;
} ResourceInfo;
inline
void freeResourceInfo(ResourceInfo *obj)
{
if (obj->mail != NULL) {
free(obj->mail);
obj->mail = NULL;
}
if (obj->account != NULL) {
freeAccount(obj->account);
obj->account = NULL;
}
free(obj);
}
}
#endif

View File

@ -1,5 +1,6 @@
#!/bin/bash
sudo apt-get install libcurl4-openssl-dev
sudo apt-get install sqlite3 libsqlite3-dev
sudo apt install default-libmysqlclient-dev
sudo apt-get install libxml2-dev

Binary file not shown.

View File

@ -10,22 +10,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
#include <curl/curl.h>
#include <regex.h>
#include "cOMS/Utils/ArrayUtils.h"
#include "cOMS/Utils/FileUtils.h"
#include "cOMS/Utils/WebUtils.h"
#include "cOMS/Hash/MeowHash.h"
#include "cOMS/Utils/Parser/Json.h"
#include "Stdlib/HashTable.h"
#include "Models/Db.h"
#include "Models/InstallType.h"
#ifndef OMS_DEMO
#define OMS_DEMO false
#endif
void printHelp()
void (*f_ptr)(int, char **);
void printHelp(int argc, char **argv)
{
printf(" The Online Resource Watcher app developed by jingga checks online or local resources\n");
printf(" for changes and and informs the user about them.\n\n");
@ -42,6 +45,19 @@ void printVersion()
printf("Version: 1.0.0\n");
}
void install(Models::InstallType type = Models::InstallType::LOCAL)
{
if (type == Models::InstallType::LOCAL) {
// create sqlite database
} else {
}
// create config file
nlohmann::json config;
}
void parseConfigFile()
{
FILE *fp = fopen("config.json");
@ -49,39 +65,73 @@ void parseConfigFile()
nlohmann::json config = nlohmann::json::parse(fp);
}
bool isResourceModified(char *filename, time_t last_change)
inline
bool isResourceDateModified(char *filename, time_t lastChange)
{
return oms_abs(Utils::FileUtils::last_modification(filename) - last_change) > 1;
return oms_abs(Utils::FileUtils::last_modification(filename) - lastChange) > 1;
}
bool hasResourceContentChanged(char *filename1, char *filename2)
inline
bool hasResourceContentChanged(Utils::FileUtils::file_body f1, Utils::FileUtils::file_body f2)
{
Utils::FileUtils::file_body f1 = Utils::FileUtils::read_file(filename1);
Utils::FileUtils::file_body f2 = Utils::FileUtils::read_file(filename2);
Hash::Meow::meow_u128 h1 = Hash::Meow::MeowHash(Hash::Meow::MeowDefaultSeed, f1.size, f1.content);
Hash::Meow::meow_u128 h2 = Hash::Meow::MeowHash(Hash::Meow::MeowDefaultSeed, f2.size, f2.content);
bool areHashesEqual = Hash::Meow::MeowHashesAreEqual(h1, h2);
return Hash::Meow::MeowHashesAreEqual(h1, h2);
}
Utils::FileUtils::file_body hasChanged(char *oldResource, char *newResource, time_t lastChange)
{
char *t;
int length = 0;
for (t = newResource; *t != '\0' && length < 7; ++t) {
++length;
}
Utils::FileUtils::file_body f1;
Utils::FileUtils::file_body f2;
bool isFileModified = false;
if (length > 5
&& (strncmp(newResource, "https:", 6) || strncmp(newResource, "www.", 4))
) {
// web resource
f1 = Utils::FileUtils::read_file(oldResource);
f2 = Utils::WebUtils::download(newResource);
} else {
// local resource
isFileModified = isResourceDateModified(oldResource, lastChange);
if (isFileModified) {
f1 = Utils::FileUtils::read_file(oldResource);
f2 = Utils::FileUtils::read_file(newResource);
}
}
bool hasChanged = isFileModified || hasResourceContentChanged(f1, f2);
free(f1.content);
free(f2.content);
return areHashesEqual;
if (hasChanged) {
free(f2.content);
f2.size = -1;
}
return f2;
}
void saveResourceChange()
void saveResourceChange(char *url, char *oldResource)
{
Utils::FileUtils::file_body dowloadData = Utils::WebUtils::download(url);
Utils::FileUtils::file_body fileData = Utils::FileUtils::read_file(oldResource);
}
MYSQL *con = null;
int main(int argc, char **argv)
{
bool hasHelpCmd = Utils::ArrayUtils::has_arg("-h", argv, argc);
if (hasHelpCmd) {
printHelp();
printHelp(argc, argv);
return 0;
}
@ -93,6 +143,27 @@ int main(int argc, char **argv)
return 0;
}
f_ptr = &printHelp;
Stdlib::HashTable::ht *table = Stdlib::HashTable::create_table();
if (table == NULL) {
return -1;
}
Stdlib::HashTable::set_entry(table, "-h", &printHelp);
regex_t regex;
regcomp(&regex, "\-h", 0);
regexec(&regex, argv[0], 0, NULL, 0) == 0;
// @todo handle install
// create config
// check install type
// web = copy config from web
// local
// create sqlite db
// create config from template
if (!Utils::FileUtils::file_exists("config.json")) {
printf("No config file available.");
@ -101,15 +172,17 @@ int main(int argc, char **argv)
unsigned long resourceId = (unsigned long) Utils::ArrayUtils::get_arg("-r", argv, argc);
// @todo handle resources
// load config
// get resources
// active
// last check older than 23 h
// check if resource changed
// save new version
// find differences
// inform users
// read config file
// create database connection (either mariadb or sqlite)
// @todo create wrapper for sqlite, mysql and postgresql
Resource res[10];
con = mysql_init(NULL);
if (mysql_real_connect(con, "localhost", "root", "root_passwd", NULL, 0, NULL, 0) == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
Stdlib::HashTable::free_table(table);
}

View File

@ -159,25 +159,25 @@
}
}
},
"org_addressrel": {
"name": "org_addressrel",
"org_address_rel": {
"name": "org_address_rel",
"fields": {
"org_addressrel_id": {
"name": "org_addressrel_id",
"org_address_rel_id": {
"name": "org_address_rel_id",
"type": "INT",
"null": false,
"primary": true,
"autoincrement": true
},
"org_addressrel_org": {
"name": "org_addressrel_org",
"org_address_rel_org": {
"name": "org_address_rel_org",
"type": "INT",
"null": false,
"foreignTable": "org",
"foreignKey": "org_id"
},
"org_addressrel_address": {
"name": "org_addressrel_address",
"org_address_rel_address": {
"name": "org_address_rel_address",
"type": "INT",
"null": false,
"foreignTable": "address",
@ -413,8 +413,8 @@
"foreignTable": "resource",
"foreignKey": "resource_id"
},
"resource_check_created_at": {
"name": "resource_check_created_at",
"resource_info_created_at": {
"name": "resource_info_created_at",
"type": "DATETIME",
"null": false
}