summaryrefslogtreecommitdiff
path: root/load_webhooks.h
diff options
context:
space:
mode:
Diffstat (limited to 'load_webhooks.h')
-rw-r--r--load_webhooks.h56
1 files changed, 56 insertions, 0 deletions
diff --git a/load_webhooks.h b/load_webhooks.h
new file mode 100644
index 0000000..5c3a60b
--- /dev/null
+++ b/load_webhooks.h
@@ -0,0 +1,56 @@
+#define WH_LOAD_SUCCESS 0
+#define WH_LOAD_FAILED -1
+
+#define WH_PARSE_SUCCESS 0
+#define WH_PARSE_FAILED -1
+
+int parse_webhooks( webhooks_data_t *whdt, size_t wh_file_len ){
+ if ( whdt->webhooks_config == NULL || (uint64_t)strlen(whdt->webhooks_config) <= 0 )
+ return WH_PARSE_FAILED;
+ if ( wh_file_len <= 0 )
+ return WH_PARSE_FAILED;
+ whdt->webhooks_parsed = cJSON_ParseWithLength( whdt->webhooks_config, wh_file_len );
+ if ( whdt->webhooks_parsed == NULL )
+ return WH_PARSE_FAILED;
+ return WH_PARSE_SUCCESS;
+}
+
+int load_webhooks( char *wh_file_name, webhooks_data_t *wdt ){
+ if ( wh_file_name == NULL || (uint64_t)strlen(wh_file_name) <= 0 )
+ return WH_LOAD_FAILED;
+ FILE *whconf = fopen( wh_file_name, "r" );
+ if ( whconf == NULL )
+ return WH_LOAD_FAILED;
+ if ( fseek(whconf, 0, SEEK_END) == -1 ){
+ fclose( whconf );
+ return WH_LOAD_FAILED;
+ }
+ long long int wh_file_size = (long long int)ftell( whconf );
+ if ( wh_file_size <= 0 ){
+ wh_file_size = 0;
+ fclose( whconf );
+ return WH_LOAD_FAILED;
+ }
+ rewind( whconf );
+ wdt->webhooks_config = (char*)calloc( (size_t)wh_file_size, sizeof(char) );
+ if ( wdt->webhooks_config == NULL ){
+ wh_file_size = 0;
+ fclose( whconf );
+ return WH_LOAD_FAILED;
+ }
+ if ( (long long int)fread(wdt->webhooks_config, sizeof(char), (size_t)(wh_file_size * sizeof(char)), whconf) == (long long int)-1 ){
+ wh_file_size = 0;
+ free( wdt->webhooks_config );
+ fclose( whconf );
+ return WH_LOAD_FAILED;
+ }
+ fclose( whconf );
+ if ( parse_webhooks(wdt, wh_file_size) != WH_PARSE_SUCCESS ){
+ free( wdt->webhooks_config );
+ wh_file_size = 0;
+ return WH_LOAD_FAILED;
+ }
+ wh_file_size = 0;
+ free( wdt->webhooks_config );
+ return WH_LOAD_SUCCESS;
+}