2023-06-07 15:23:01 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-06-07 15:23:01 +08:00
|
|
|
#include "detection/users/users.h"
|
|
|
|
|
#include "modules/users/users.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-06-07 15:23:01 +08:00
|
|
|
|
|
|
|
|
#define FF_USERS_NUM_FORMAT_ARGS 1
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintUsers(FFUsersOptions* options)
|
2023-06-07 15:23:01 +08:00
|
|
|
{
|
|
|
|
|
FF_LIST_AUTO_DESTROY users = ffListCreate(sizeof(FFstrbuf));
|
|
|
|
|
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY error = ffStrbufCreate();
|
|
|
|
|
|
|
|
|
|
ffDetectUsers(&users, &error);
|
|
|
|
|
|
|
|
|
|
if(error.length > 0)
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_USERS_MODULE_NAME, 0, &options->moduleArgs, "%*s", error.length, error.chars);
|
2023-06-07 15:23:01 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
|
|
|
|
|
for(uint32_t i = 0; i < users.length; ++i)
|
|
|
|
|
{
|
|
|
|
|
if(i > 0)
|
|
|
|
|
ffStrbufAppendS(&result, ", ");
|
|
|
|
|
FFstrbuf* user = (FFstrbuf*)ffListGet(&users, i);
|
|
|
|
|
ffStrbufAppend(&result, user);
|
|
|
|
|
ffStrbufDestroy(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_USERS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2023-06-07 15:23:01 +08:00
|
|
|
puts(result.chars);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintFormat(FF_USERS_MODULE_NAME, 0, &options->moduleArgs, FF_USERS_NUM_FORMAT_ARGS, (FFformatarg[]){
|
2023-06-07 15:23:01 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &result},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffInitUsersOptions(FFUsersOptions* options)
|
|
|
|
|
{
|
2023-08-17 10:56:54 +08:00
|
|
|
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_USERS_MODULE_NAME, ffParseUsersCommandOptions, ffParseUsersJsonObject, ffPrintUsers);
|
2023-06-07 15:23:01 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseUsersCommandOptions(FFUsersOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_USERS_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyUsersOptions(FFUsersOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:56:54 +08:00
|
|
|
void ffParseUsersJsonObject(FFUsersOptions* options, yyjson_val* module)
|
2023-06-07 15:23:01 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-06-07 15:23:01 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
continue;
|
2023-06-10 15:01:54 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
2023-06-07 15:23:01 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
ffPrintError(FF_USERS_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
|
2023-06-07 15:23:01 +08:00
|
|
|
}
|
|
|
|
|
}
|