1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <dirent.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include "log.h"
7 #include "config.h"
8 
9 int doPInbound();
10 int doXArc(char fname[12]);
11 int browseArgs(int argc, char *argv[]);
12 void printUsage();
13 
14 int cmdX = 0;
15 int cmdA = 0;
16 int cmdAA = 0;
17 
18 int isError = 0;
19 
20 
main(int argc,char * argv[])21 int main(int argc, char *argv[])
22 {
23     char *logFileName;
24 
25     if (openConfig()) {
26        printf("Error while opening the config file. Bailing out!\n");
27        isError =1;
28     }
29 
30     logFileName = (char *) malloc(strlen(getLogFileDir())+8+1);
31     strcpy(logFileName, getLogFileDir());
32     strcat(logFileName, "carc.log");
33 
34 #ifdef DEBUGLEVEL
35     if (openLog(logFileName, DEBUG)) {
36         printf("Error opening log file. Bailing out.\n");
37     }
38 #else
39     if (openLog(logFileName, NORMAL)) {
40         printf("Error opening log file. Bailing out.\n");
41     }
42 #endif
43 
44     free(logFileName);
45 
46     if (browseArgs(argc, argv)) {
47         addLog(NORMAL, "Error while parsing the Commandline");
48         isError = 1;
49     } else {
50         addLog(DEBUG, "command line done");
51     }
52 
53     if (cmdX) {
54         if (doPInbound() == -1) {
55             addLog(NORMAL, "Error while processing the PInbound. Bailing Out");
56             isError = 1;
57         } else {
58             addLog(DEBUG, "eXpanded without errors.");
59         }
60     }
61 
62     closeConfig();
63     closeLog();
64 
65     if (isError) {
66         printf("There were errors during execution.\n");
67         printf("Check the logfile for detailed information.\n");
68         return 1;
69     } else {
70         return 0;
71     }
72 }
73 
74 
doPInbound()75 int doPInbound()
76 {
77     DIR *pinbound;
78     struct dirent *fname;
79 
80     char *curdir;
81     char *logentry;
82 
83     int errXArc;
84     int xarced = -1; /* -1 indicates error */
85 
86     logentry = malloc(255);
87     curdir = malloc(255);
88     getcwd(curdir, 255);
89 
90     addLog(NORMAL, "processing pinbound");
91     xarced = -1; /* -1 indicates error */
92 
93     if ((pinbound = opendir(getProtInbound()))) {
94         chdir(getProtInbound());
95         xarced++; /* -1 + 1 = 0: no error, but no files extracted yet */
96 
97         while ((fname=readdir(pinbound))) {
98             /* ugly, how is ist done better? */
99             if ((strstr(fname->d_name,".mo") ||
100                  strstr(fname->d_name,".tu") ||
101 		 strstr(fname->d_name,".we") ||
102 		 strstr(fname->d_name,".th") ||
103 		 strstr(fname->d_name,".fr") ||
104 		 strstr(fname->d_name,".sa") ||
105                  strstr(fname->d_name,".su") ||
106 
107                  strstr(fname->d_name,".MO") ||
108        	         strstr(fname->d_name,".TU") ||
109 		 strstr(fname->d_name,".WE") ||
110 		 strstr(fname->d_name,".TH") ||
111 		 strstr(fname->d_name,".FR") ||
112 		 strstr(fname->d_name,".SA") ||
113                  strstr(fname->d_name,".SU"))) {
114 
115                 errXArc = doXArc(fname->d_name);
116 
117                 if (errXArc) {
118                     sprintf(logentry, "processing %s - error %d returned", fname->d_name, errXArc);
119                 } else {
120                     sprintf(logentry, "processing %s - done", fname->d_name);
121                     xarced++;
122                 }
123                 addLog(DEBUG, logentry);
124             }
125         }
126         closedir(pinbound);
127         chdir(curdir);
128     }
129     return xarced;
130 }
131 
132 
doXArc(char fname[12])133 int doXArc(char fname[12])
134 {
135     char arc[256];
136     char cmd[256];
137 
138     int cmdexit;
139 
140     FILE *fhandle;
141     char header[5];
142 
143     strcpy(arc, fname);
144 
145     if ((fhandle = fopen(arc, "r"))) {
146         fgets(header, 5, fhandle);
147 
148         /* what kinda arc */
149         if (header[0] == 0x60) {
150             sprintf(cmd, "unarj e %s > /dev/null", fname);
151         } else if (strstr(header, " ")) {
152             printf("is ARC\n");
153         } else if (strstr(header, "PK")) {
154             sprintf(cmd, "unzip -j -Loqq %s%s -d%s", getProtInbound(),
155                     fname, getProtInbound());
156         } else if (strstr(header, "lh")) {
157             printf("is LHA\n");
158         } else if (strstr(header, "ZOO")) {
159             printf("is ZOO\n");
160         } else {
161             addLog(NORMAL, "unable to determine arc type\n");
162             return 1;
163         }
164 
165         cmdexit = system(cmd);
166 
167         if (cmdexit) {
168             return cmdexit;
169             /* keep file */
170         } else {
171             sprintf(cmd, "rm %s", arc);
172             system(cmd);
173         }
174     } else {
175         printf("open failed: %s", arc);
176         return 1;
177     }
178     return 0;
179 }
180 
181 
browseArgs(int argc,char * argv[])182 int browseArgs(int argc, char* argv[])
183 {
184     if (argc < 2) {
185         printUsage();
186         return 1;
187     } else {
188         if (strcmp(argv[1], "x") == 0) {
189             addLog(DEBUG, "eXpand requested");
190             if (argc ==2) {
191                 cmdX = 1;
192             } else {
193                 addLog(NORMAL, "to many parameters for command x. Bailing out");
194                 return 1;
195             }
196         } else if (strcmp(argv[1], "a") == 0) {
197             printf("feature currently not supported\n");
198         } else if (strcmp(argv[1], "a") == 0) {
199             printf("feature currently not supported\n");
200         }
201         return 0;
202     }
203 }
204 
205 
printUsage()206 void printUsage()
207 {
208     printf("carc usage:\n");
209     printf("carc { x | a | aa}\n\n");
210     printf("Commands:\n");
211     printf("x  - eXpand files in protected inbound\n");
212     printf("a  - attach file to node\n");
213     printf("     a <file> <aka> <flavour>\n");
214     printf("aa - arc attach a file to node\n");
215     printf("     aa <file> <aka> <flavour> <packer>\n");
216 }
217 
218