Browse code

Cleanup: change tabs to spaces.

Dario Rodriguez authored on 03/11/2022 18:21:15
Showing 1 changed files
... ...
@@ -29,9 +29,9 @@ typedef enum esection {
29 29
 newslconfig *
30 30
 newslconfig_init(char *configfile, char *deflogfile, char *defrootdir)
31 31
 {
32
-	newslconfig *config;
33
-	FILE *f;
34
-	char line[MAXLINESIZE];
32
+        newslconfig *config;
33
+        FILE *f;
34
+        char line[MAXLINESIZE];
35 35
         int len;
36 36
         char *ptr,*sep,*value;
37 37
         esection section;
... ...
@@ -54,8 +54,8 @@ newslconfig_init(char *configfile, char *deflogfile, char *defrootdir)
54 54
                         ;
55 55
                 len=strlen(line);
56 56
                 while(len>0 && strchr("\n\t ",line[len-1])!=NULL)
57
-	                line[len-1]='\0',len--;
58
-		for(ptr=line;*ptr!='\0' && strchr("\t ",*ptr)!=NULL;ptr++)
57
+                        line[len-1]='\0',len--;
58
+                for(ptr=line;*ptr!='\0' && strchr("\t ",*ptr)!=NULL;ptr++)
59 59
                         ;
60 60
                 len=strlen(ptr);
61 61
                 /* ignore empty lines and comments */
... ...
@@ -82,12 +82,12 @@ newslconfig_init(char *configfile, char *deflogfile, char *defrootdir)
82 82
                         /* trim value */
83 83
                         while(*value!='\0' && strchr(" \t",*value)!=NULL)
84 84
                                 value++;
85
-			/* sanity check */
85
+                        /* sanity check */
86 86
                         if(*value=='\0') {
87
-				log_write("CONF","%s:%i: ignoring key without value; key:\"%s\"\n",configfile,lineno,ptr);
87
+                                log_write("CONF","%s:%i: ignoring key without value; key:\"%s\"\n",configfile,lineno,ptr);
88 88
                                 continue;
89 89
                         }
90
-			 /* assign value */
90
+                         /* assign value */
91 91
                         if(section==section_none) {
92 92
                                 log_write("CONF","%s:%i: ignoring key-value pair in unknown section; key:\"%s\"\n",configfile,lineno,ptr);
93 93
                                 continue;
... ...
@@ -98,13 +98,13 @@ newslconfig_init(char *configfile, char *deflogfile, char *defrootdir)
98 98
                                         configvalue=&(config->logfile);
99 99
                                 } else if(strcmp(ptr,"rootdir")==0) {
100 100
                                         configvalue=&(config->rootdir);
101
-				} else {
101
+                                } else {
102 102
                                         log_write("CONF","%s:%i: unknown key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
103 103
                                         continue;
104 104
                                 }
105
-			} else
105
+                        } else
106 106
                                 continue;
107
-			/* store value */
107
+                        /* store value */
108 108
                         if(*configvalue!=NULL) {
109 109
                                 log_write("CONF","%s:%i: duplicate key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
110 110
                                 continue;
... ...
@@ -121,11 +121,11 @@ newslconfig_init(char *configfile, char *deflogfile, char *defrootdir)
121 121
                 }
122 122
         }
123 123
         fclose(f),f=NULL;
124
-	if((config->logfile==NULL && (config->logfile=strdup(deflogfile))==NULL) ||
125
-	   (config->rootdir==NULL && (config->rootdir=strdup(defrootdir))==NULL)) {
126
-		newslconfig_free(config),config=NULL;
127
-		return(NULL);
128
-	}
124
+        if((config->logfile==NULL && (config->logfile=strdup(deflogfile))==NULL) ||
125
+           (config->rootdir==NULL && (config->rootdir=strdup(defrootdir))==NULL)) {
126
+                newslconfig_free(config),config=NULL;
127
+                return(NULL);
128
+        }
129 129
         return(config);
130 130
 }
131 131
 
Browse code

Add simple form to ask for the e-mail and get the reply via POST request

Dario Rodriguez authored on 05/05/2015 19:14:56
Showing 1 changed files
... ...
@@ -27,7 +27,7 @@ typedef enum esection {
27 27
 
28 28
 
29 29
 newslconfig *
30
-newslconfig_init(char *configfile)
30
+newslconfig_init(char *configfile, char *deflogfile, char *defrootdir)
31 31
 {
32 32
 	newslconfig *config;
33 33
 	FILE *f;
... ...
@@ -96,6 +96,8 @@ newslconfig_init(char *configfile)
96 96
                         if(section==section_general) {
97 97
                                 if(strcmp(ptr,"logfile")==0) {
98 98
                                         configvalue=&(config->logfile);
99
+                                } else if(strcmp(ptr,"rootdir")==0) {
100
+                                        configvalue=&(config->rootdir);
99 101
 				} else {
100 102
                                         log_write("CONF","%s:%i: unknown key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
101 103
                                         continue;
... ...
@@ -119,6 +121,11 @@ newslconfig_init(char *configfile)
119 121
                 }
120 122
         }
121 123
         fclose(f),f=NULL;
124
+	if((config->logfile==NULL && (config->logfile=strdup(deflogfile))==NULL) ||
125
+	   (config->rootdir==NULL && (config->rootdir=strdup(defrootdir))==NULL)) {
126
+		newslconfig_free(config),config=NULL;
127
+		return(NULL);
128
+	}
122 129
         return(config);
123 130
 }
124 131
 
... ...
@@ -129,6 +136,8 @@ newslconfig_free(newslconfig *config)
129 136
                 return;
130 137
         if(config->logfile!=NULL)
131 138
                 free(config->logfile),config->logfile=NULL;
139
+        if(config->rootdir!=NULL)
140
+                free(config->rootdir),config->rootdir=NULL;
132 141
         free(config),config=NULL;
133 142
         return;
134 143
 }
... ...
@@ -143,7 +152,7 @@ newslconfig_exists(char *configfile)
143 152
 }
144 153
 
145 154
 int
146
-newslconfig_write(char *configfile,char *logfile)
155
+newslconfig_write(char *configfile,char *logfile,char *rootdir)
147 156
 {
148 157
         FILE *f;
149 158
         if((f=fopen(configfile,"w"))==NULL)
... ...
@@ -151,6 +160,7 @@ newslconfig_write(char *configfile,char *logfile)
151 160
         fprintf(f,"; newslettersan config file\n");
152 161
         fprintf(f,"[general]\n");
153 162
         fprintf(f,"logfile=%s\n",(logfile!=NULL)?logfile:"newslettersan.log");
163
+        fprintf(f,"rootdir=%s\n",(rootdir!=NULL)?rootdir:"subscribe");
154 164
         return(0);
155 165
 }
156 166
 
Browse code

Basic webserver part (blatantly ripped from kakumei)

Dario Rodriguez authored on 23/04/2015 20:11:31
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,158 @@
1
+/*
2
+ * newsl_config.c
3
+ *
4
+ * Configuration load/save for newslettersan
5
+ *
6
+ * Author: Dario Rodriguez dario@softhome.net
7
+ * This program is licensed under the terms of the Affero GPL v1+
8
+ */
9
+
10
+#include <stdio.h>
11
+#include <stdlib.h>
12
+#include <unistd.h>
13
+#include <string.h>
14
+#include <sys/types.h>
15
+#include <sys/stat.h>
16
+
17
+#include "loglib.h"
18
+#include "newsl_config.h"
19
+
20
+#define MAXLINESIZE 2048
21
+#define LINESBLOCK 32
22
+
23
+typedef enum esection {
24
+        section_none=0,
25
+        section_general
26
+} esection;
27
+
28
+
29
+newslconfig *
30
+newslconfig_init(char *configfile)
31
+{
32
+	newslconfig *config;
33
+	FILE *f;
34
+	char line[MAXLINESIZE];
35
+        int len;
36
+        char *ptr,*sep,*value;
37
+        esection section;
38
+        int lineno;
39
+        char **configvalue;
40
+        if((f=fopen(configfile,"r"))==NULL)
41
+                return(NULL);
42
+        if((config=malloc(sizeof(newslconfig)))==NULL) {
43
+                fclose(f),f=NULL;
44
+                return(NULL);
45
+        }
46
+        memset(config,0,sizeof(newslconfig));
47
+        section=section_none;
48
+        lineno=0;
49
+        while(fgets(line,sizeof(line),f)!=NULL) {
50
+                lineno++;
51
+                line[sizeof(line)-1]='\0';
52
+                /* trim line */
53
+                for(ptr=line;*ptr!='\0' && strchr("\t ",*ptr)!=NULL;ptr++)
54
+                        ;
55
+                len=strlen(line);
56
+                while(len>0 && strchr("\n\t ",line[len-1])!=NULL)
57
+	                line[len-1]='\0',len--;
58
+		for(ptr=line;*ptr!='\0' && strchr("\t ",*ptr)!=NULL;ptr++)
59
+                        ;
60
+                len=strlen(ptr);
61
+                /* ignore empty lines and comments */
62
+                if(*ptr=='\0' || strchr("#;",*ptr)!=NULL)
63
+                        continue;
64
+                /* parse line */
65
+                if(*ptr=='[' && ptr[len-1]==']') {
66
+                        /* section */
67
+                        if(strcmp(ptr,"[general]")==0) {
68
+                                section=section_general;
69
+                        } else {
70
+                                log_write("CONF","%s:%i: unknown section name \"%s\"\n",configfile,lineno,ptr);
71
+                                section=section_none;
72
+                        }
73
+                        continue;
74
+                } else if((sep=strchr(ptr,'='))!=NULL) {
75
+                        *sep='\0';
76
+                        value=sep+1;
77
+                        /* trim key */
78
+                        while(sep>ptr && strchr(" \t",sep[-1])!=NULL) {
79
+                                sep--;
80
+                                *sep='\0';
81
+                        }
82
+                        /* trim value */
83
+                        while(*value!='\0' && strchr(" \t",*value)!=NULL)
84
+                                value++;
85
+			/* sanity check */
86
+                        if(*value=='\0') {
87
+				log_write("CONF","%s:%i: ignoring key without value; key:\"%s\"\n",configfile,lineno,ptr);
88
+                                continue;
89
+                        }
90
+			 /* assign value */
91
+                        if(section==section_none) {
92
+                                log_write("CONF","%s:%i: ignoring key-value pair in unknown section; key:\"%s\"\n",configfile,lineno,ptr);
93
+                                continue;
94
+                        }
95
+                        /* identify key-value pair */
96
+                        if(section==section_general) {
97
+                                if(strcmp(ptr,"logfile")==0) {
98
+                                        configvalue=&(config->logfile);
99
+				} else {
100
+                                        log_write("CONF","%s:%i: unknown key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
101
+                                        continue;
102
+                                }
103
+			} else
104
+                                continue;
105
+			/* store value */
106
+                        if(*configvalue!=NULL) {
107
+                                log_write("CONF","%s:%i: duplicate key, ignoring key-value pair; key:\"%s\"\n",configfile,lineno,ptr);
108
+                                continue;
109
+                        }
110
+                        if((*configvalue=strdup(value))==NULL) {
111
+                                fclose(f),f=NULL;
112
+                                newslconfig_free(config),config=NULL;
113
+                                return(NULL); /* insufficient memory */
114
+                        }
115
+                        continue;
116
+                } else {
117
+                        log_write("CONF","%s:%i: malformed line, ignoring; contents:\"%s\"\n",configfile,lineno,ptr);
118
+                        continue;
119
+                }
120
+        }
121
+        fclose(f),f=NULL;
122
+        return(config);
123
+}
124
+
125
+void
126
+newslconfig_free(newslconfig *config)
127
+{
128
+        if(config==NULL)
129
+                return;
130
+        if(config->logfile!=NULL)
131
+                free(config->logfile),config->logfile=NULL;
132
+        free(config),config=NULL;
133
+        return;
134
+}
135
+
136
+int
137
+newslconfig_exists(char *configfile)
138
+{
139
+        struct stat st;
140
+        if(stat(configfile,&st)!=0)
141
+                return(-1);
142
+        return(0);
143
+}
144
+
145
+int
146
+newslconfig_write(char *configfile,char *logfile)
147
+{
148
+        FILE *f;
149
+        if((f=fopen(configfile,"w"))==NULL)
150
+                return(-1);
151
+        fprintf(f,"; newslettersan config file\n");
152
+        fprintf(f,"[general]\n");
153
+        fprintf(f,"logfile=%s\n",(logfile!=NULL)?logfile:"newslettersan.log");
154
+        return(0);
155
+}
156
+
157
+
158
+