1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4
5 #include "flag.h"
6 #include "core.h"
7 #include "variables.h"
8 #include "fileapi.h"
9 #include "submodes.h"
10 #include "list.h"
11 #include "mystring.h"
12
13 struct submode *submodes;
14
new_submodes(void)15 void new_submodes(void)
16 {
17 submodes = NULL;
18 }
19
nuke_submodes(void)20 void nuke_submodes(void)
21 {
22 struct submode *temp, *temp2;
23 temp = submodes;
24 while(temp) {
25 temp2 = temp->next;
26 if(temp->modename) free (temp->modename);
27 if(temp->flaglist) free (temp->flaglist);
28 if(temp->description) free (temp->description);
29 temp = temp2;
30 }
31 submodes = NULL;
32 }
33
get_submodes(void)34 struct submode *get_submodes(void)
35 {
36 return submodes;
37 }
38
read_submodes(const char * list)39 void read_submodes(const char *list)
40 {
41 char buf[BIG_BUF];
42 char *modename, *modeflags, *desc, *flag, *flagend;
43 struct listserver_flag *fdata;
44 struct submode *newmode;
45 int valid = 1;
46 FILE *fp;
47 char *listdir;
48
49 listdir = list_directory(get_string("list"));
50
51 buffer_printf(buf, sizeof(buf) - 1, "%s/%s", listdir,
52 get_string("submodes-file"));
53
54 free(listdir);
55
56 fp = open_file(buf, "r");
57 if(!fp) return;
58
59 while(read_file(buf, sizeof(buf), fp)) {
60 if(buf[0] == '#') continue; /* skip comments */
61 if(buf[strlen(buf)-1] == '\n')
62 buf[strlen(buf)-1] = '\0'; /* smash newline */
63 modename = &buf[0];
64 modeflags = strstr(buf, " : ");
65 if(!modeflags) {
66 log_printf(2, "Invalid subscription mode '%s'\n", modename);
67 continue;
68 }
69 *modeflags = '\0';
70 modeflags += 3;
71 desc = strstr(modeflags, " : ");
72 if(desc) {
73 *desc = '\0';
74 desc += 3;
75 }
76 /* okay, we have everything, let's validate the flags */
77 if((*modeflags != '|') || (modeflags[strlen(modeflags)-1] != '|')) {
78 log_printf(2, "Invalid subscription mode '%s'\n", modename);
79 continue;
80 }
81 flag = modeflags+1;
82 flagend = strchr(flag, '|');
83 valid = 1;
84 while(flagend) {
85 *flagend = '\0';
86 fdata = get_flag(flag);
87 if(!fdata) {
88 valid = 0;
89 log_printf(2, "Flag '%s' in subscription mode '%s' is not valid.\n", flag, modename);
90 }
91 *flagend = '|';
92 flag = flagend+1;
93 flagend = strchr(flag, '|');
94 }
95 if(!valid) continue;
96 newmode = (struct submode *)malloc(sizeof(struct submode));
97 newmode->modename = strdup(modename);
98 newmode->flaglist = strdup(modeflags);
99 newmode->description = desc ? strdup(desc) : NULL;
100 newmode->next = submodes;
101 submodes = newmode;
102 }
103 }
104
get_submode(const char * mode)105 struct submode *get_submode(const char *mode)
106 {
107 struct submode *temp = submodes;
108 while(temp) {
109 if(strcasecmp(temp->modename, mode) == 0) {
110 return temp;
111 }
112 temp = temp->next;
113 }
114 return NULL;
115 }
116
get_submode_flags(const char * mode)117 const char *get_submode_flags(const char *mode)
118 {
119 struct submode *temp = submodes;
120 while(temp) {
121 if(strcasecmp(temp->modename, mode) == 0) {
122 return temp->flaglist;
123 }
124 temp = temp->next;
125 }
126 return NULL;
127 }
128