1 /* $Id$ */
2 /*
3 * hptUtil - purge, pack, link and sort utility for HPT
4 * by Fedor Lizunkov 2:5020/960@Fidonet and val khokhlov 2:550/180@fidonet
5 *
6 * This file is part of HPT.
7 *
8 * HPT is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * HPT is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HPT; see the file COPYING. If not, write to the free
20 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 *****************************************************************************
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28
29 #include <smapi/msgapi.h>
30 #include <fidoconf/fidoconf.h>
31 #include <fidoconf/common.h>
32 #include <huskylib/xstr.h>
33
34 #include <hptutil.h>
35 #include <linkarea.h>
36 #include <sortarea.h>
37 #include <purgearea.h>
38 #include <packarea.h>
39 #include <fixarea.h>
40 #include <undelete.h>
41
42 #include "version.h"
43
44 char *versionStr;
45
46 FILE *filesout;
47 FILE *fileserr;
48 #define LogFileName "hptutil.log"
49 FILE *hptutil_log = NULL;
50
51 char quiet = 0;
52 char jam_by_crc = 0;
53 char keepImportLog = 0;
54 char typebase;
55 char *basefilename;
56 char *altImportLog = NULL;
57 unsigned int debugLevel = 0;
58
OutScreen(char * str,...)59 void OutScreen(char *str, ...)
60 {
61 char buf[256], dt[20];
62 char *pos;
63 int cnt = 0, cnt2;
64 static int cont = 0;
65 va_list par;
66
67 va_start (par, str);
68 if (hptutil_log != NULL) {
69 time_t t = time(NULL);
70 struct tm *tm = localtime(&t);
71 cnt = vsprintf(buf, str, par);
72 if(cnt >= sizeof(buf))
73 exit(1);
74 cnt2 = sprintf(dt, "%2d.%02d.%02d %02d:%02d:%02d ",
75 tm->tm_mday, tm->tm_mon+1, tm->tm_year%100,
76 tm->tm_hour, tm->tm_min, tm->tm_sec);
77 if(cnt2 >= sizeof(dt))
78 exit(1);
79 buf[cnt] = dt[cnt2] = 0;
80 }
81 if (quiet == 0) vfprintf(filesout, str, par);
82 va_end (par);
83
84 if (hptutil_log != NULL) {
85 char *cur = buf;
86 do {
87 if ((pos = strchr(cur, '\n')) != NULL) *pos = 0;
88 if (!cont && *cur) fprintf(hptutil_log, "%s", dt);
89 if (pos == NULL) fprintf(hptutil_log, "%s", cur);
90 else fprintf(hptutil_log, "%s\n", cur);
91 cont = 0;
92 cur = (pos != NULL) ? pos + 1 : buf + cnt;
93 } while (*cur);
94 cont = (pos == NULL);
95 fflush(hptutil_log);
96 }
97 }
98 /*
99 void OutScreen(char *str, ...)
100 {
101 va_list par;
102
103 va_start (par, str);
104 if (quiet == 0) vfprintf(filesout, str, par);
105 va_end (par);
106 }
107 */
wait_d()108 void wait_d()
109 {
110 char form[]="-\\|/";
111 static int i=0;
112
113 if (i == 4) i = 0;
114
115 OutScreen("\b%c", form[i++]);
116 }
117
printFixHelp()118 void printFixHelp()
119 {
120 fprintf(filesout,
121 "Usage: hptutil fix [options]\n"
122 "Options: -s <%cpath%cfilename>\t- fix squish area file\n"
123 "\t -j <%cpath%cfilename>\t- fix jam area file\n\n",
124 PATH_DELIM, PATH_DELIM, PATH_DELIM, PATH_DELIM);
125 exit(0);
126 }
127
FixBase(char c)128 char FixBase(char c)
129 {
130 switch (c) {
131 case 's':
132 return (char)1;
133 break;
134 case 'S':
135 return (char)1;
136 case 'j':
137 return (char)2;
138 case 'J':
139 return (char)2;
140 default:
141 fprintf(fileserr, "Don't known type base. See ...\n");
142 printFixHelp();
143 break;
144 }
145 return 0;
146 }
147
parseFixLine(int argc,char * argv[],int * i,int * what)148 void parseFixLine(int argc, char *argv[], int *i, int *what)
149 {
150 if (*i < argc-1) {
151 (*i)++;
152 if (stricmp(argv[*i], "-?") == 0) {
153 printFixHelp();
154 }
155 if (argv[*i][0] != '-') {
156 fprintf(fileserr, "Error options. See ...\n");
157 printFixHelp();
158 } else if (stricmp(argv[*i], "-s") == 0 || stricmp(argv[*i], "-j") == 0) {
159 // typebase
160 // 0 - error
161 // 1 - Squish
162 // 2 - Jam
163 typebase = FixBase(argv[*i][1]);
164 if (*i < argc-1) {
165 (*i)++;
166 xstrcat(&basefilename, argv[*i]);
167 *what |= 0x10;
168 return;
169 } else {
170 fprintf(fileserr, "Error options. See ...\n");
171 printFixHelp();
172 }
173 } else {
174 typebase = FixBase(argv[*i][1]);
175 xstrcat(&basefilename, argv[*i]+2);
176 }
177 } else {
178 fprintf(fileserr, "Error options. See ...\n");
179 printFixHelp();
180 }
181 }
182
printMyTitle()183 static void printMyTitle()
184 {
185 static int already = 0;
186
187 if (!already) {
188 if (filesout) fprintf(filesout, "%s\n\n", versionStr);
189 already = 1;
190 }
191 }
192
processCommandLine(int argc,char * argv[],int * what)193 void processCommandLine(int argc, char *argv[], int *what)
194 {
195 int i = 0;
196 char *tmp = NULL;
197
198 if (argc == 1) {
199 printMyTitle();
200 printf("Usage: hptutil [options]\n"
201 "Options: sort\t\t- sort unread messages by time and date\n"
202 "\t link\t\t- reply-link messages\n"
203 "\t purge\t\t- purge areas\n"
204 "\t pack\t\t- pack areas\n"
205 "\t fix\t\t- fix base (hptutil fix -? for more help)\n"
206 "\t -j\t\t- link Jam areas by CRC (great speed-up)\n"
207 "\t -k\t\t- keep import.log file\n"
208 "\t -q\t\t- quiet mode (no screen output)\n"
209 "\t -i <filename>\t- alternative import.log\n\n");
210 exit(1);
211 } /* endif */
212
213 while (i < argc-1) {
214 i++;
215 if (stricmp(argv[i], "purge") == 0) *what |= 0x01;
216 else if (stricmp(argv[i], "pack") == 0) *what |= 0x02;
217 else if (stricmp(argv[i], "sort") == 0) *what |= 0x04;
218 else if (stricmp(argv[i], "link") == 0) *what |= 0x08;
219 else if (stricmp(argv[i], "fix") == 0) parseFixLine(argc, argv, &i, what);
220 else if (stricmp(argv[i], "undel") == 0) *what |= 0x20;
221 else if (stricmp(argv[i], "-k") == 0) keepImportLog = 1;
222 else if (stricmp(argv[i], "-q") == 0) quiet = 1;
223 else if (stricmp(argv[i], "-j") == 0) jam_by_crc = 1;
224 else if (stricmp(argv[i], "-i") == 0) {
225 if (i < argc-1) {
226 i++;
227 xstrcat(&altImportLog, argv[i]);
228 } else {
229 printMyTitle();
230 fprintf(fileserr, "Parameter is required for '-i'\n\n");
231 exit (5);
232 }
233 }
234 else if (argv[i][0] == '-' && (argv[i][1] == 'i' || argv[i][1] == 'I')) {
235 xstrcat(&altImportLog, argv[i]+2);
236 }
237 else if (stricmp(argv[i], "-d") == 0) {
238 if (i < argc-1) {
239 i++;
240 xstrcat(&tmp, argv[i]);
241 debugLevel = (unsigned int)(atoi(tmp));
242 nfree(tmp);
243 } else {
244 printMyTitle();
245 fprintf(fileserr, "Parameter is required for '-d'\n\n");
246 exit (5);
247 }
248 }
249 else if (argv[i][0] == '-' && (argv[i][1] == 'd' || argv[i][1] == 'D')) {
250 xstrcat(&tmp, argv[i]+2);
251 debugLevel = (unsigned int)(atoi(tmp));
252 nfree(tmp);
253 } else {
254 printMyTitle();
255 fprintf(fileserr, "Unknown option '%s'\n\n", argv[i]);
256 }
257 } /* endwhile */
258 }
259
main(int argc,char * argv[])260 int main(int argc, char *argv[])
261 {
262 s_fidoconfig *config;
263 char *keepOrigImportLog = NULL;
264 char *buff = NULL;
265 int what = 0;
266 int ret = 0;
267
268 filesout=stdout;
269 fileserr=stderr;
270
271 setbuf(filesout, NULL);
272 setbuf(fileserr, NULL);
273
274 versionStr = GenVersionStr( "hptutil", VER_MAJOR, VER_MINOR, VER_PATCH,
275 VER_BRANCH, cvs_date);
276
277 processCommandLine(argc, argv, &what);
278 if (quiet) filesout=NULL;
279 printMyTitle();
280
281 if (what) {
282 setvar("module", "hptutil");
283 config = readConfig(NULL);
284 if (config) {
285 /* init log */
286 if (config->logFileDir) {
287 xstrscat(&buff, config->logFileDir, LogFileName, NULL);
288 hptutil_log = fopen(buff, "a");
289 nfree(buff);
290 }
291
292 if (altImportLog) {
293 keepOrigImportLog = config->importlog;
294 config->importlog = altImportLog;
295 }
296 if (what & 0x10) ret = fixArea(config);
297 else if (what & 0x20) ret = undeleteMsgs(config, altImportLog);
298 else {
299 if (what & 0x04) sortAreas(config);
300 if (what & 0x08) linkAreas(config);
301 if (what & 0x01) purgeAreas(config);
302 if (what & 0x02) packAreas(config);
303 }
304
305 if (altImportLog) {
306 config->importlog = keepOrigImportLog;
307 nfree(altImportLog);
308 }
309
310 disposeConfig(config);
311 } else {
312 fprintf(fileserr, "Could not read fido config\n");
313 ret = 1;
314 } /* endif */
315 } else {
316 if (argc > 1) OutScreen("Nothing to do ...\n\n");
317 } /* endif */
318
319 if (hptutil_log != NULL) fclose(hptutil_log);
320 return ret;
321 }
322
323