Removed HAVE_CONFIG_H
[amule.git] / src / utils / cas / cas.c
blob68d7ceebadbed7445969e30a5f77ed24fd73287c
1 /*
2 * Name: Main cas file
4 * Purpose: aMule Statistics
6 * Author: Pedro de Oliveira <falso@rdk.homeip.net>
8 * Copyright (c) 2004-2011 Pedro de Oliveira ( falso@rdk.homeip-net )
10 * This file is part of aMule.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the
24 * Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <time.h>
34 #include <getopt.h>
36 #include "version.h"
37 #include "configfile.h"
38 #include "functions.h"
39 #include "graphics.h"
40 #include "html.h"
41 #include "lines.h"
42 #include "amule-config.h" // For HAVE_GETOPT_LONG
44 #ifndef HAVE_GETOPT_LONG
45 /* Code from getopt_long.h - getopt_long() for systems that lack it
46 Copyright (c) 2001-2011 Arthur de Jong, GPL 2 and later */
47 # define no_argument 0
48 # define required_argument 1
49 # define optional_argument 2
51 struct option {
52 const char *name;
53 int has_arg;
54 int *flag;
55 int val;
58 int getopt_long(int argc,
59 char * const argv[],
60 const char *optstring,
61 const struct option *longopts,
62 int *longindex);
63 #endif
66 * History:
68 * ????.??.?? - falso: creation of cas.
69 * ????.??.?? - Jacobo221: Detect connecting state
70 * ????.??.?? - falso: HTML page generation
71 * 2004.08.27 - GonoszTopi: New line handling routines, to cope with lines
72 * longer than 80 characters. Fixes buffer overflow.
73 * 2005.12.10 - fulgas: added kad info support
74 * 2005,12,16 - stefanero: fixed Kad related stuff and some other things
77 static struct option long_options[] = {
78 { "help", no_argument, NULL, 'h' },
79 { "html", optional_argument, NULL, 'p' },
80 { "picture", optional_argument, NULL, 'o' },
81 { "config-dir", required_argument, NULL, 'c' },
82 { NULL, 0, NULL, 0 }
85 void usage(char *myname)
87 printf (" ___ _ _ ___ c aMule statistics\n"
88 " /'___) /'_` )/',__) by Pedro de Oliveira\n"
89 "( (___ ( (_| |\\__, \\ <falso@rdk.homeip.net>\n"
90 "`\\____)`\\__,_)(____/ Version %s\n\n"
92 "Usage: %s [OPTION]\n"
93 "If run without any option prints stats to stdout\n\n"
94 "OPTIONS:\n"
95 #ifdef __GD__
96 "-o, --picture, -P\tWrites the online signature picture\n"
97 #endif
98 "-p, --html, -H\t\tHTML Page with stats and picture\n"
99 "-c, --config-dir\tSpecifies a config-dir different from home\n"
100 "-h, --help\t\tThis help you're reading\n", CAS_VERSION, myname);
103 #ifndef HAVE_GETOPT_LONG
105 /* Code from getopt_long.c - getopt_long() for systems that lack it
106 Copyright (c) 2001-2011 Arthur de Jong, GPL 2 and later
107 Slightly edited for the sake of clarity by Gaznevada */
109 int getopt_long(int argc,
110 char * const argv[],
111 const char *optstring,
112 const struct option *longopts,
113 int *longindex)
115 if ( (optind > 0) && (optind < argc) &&
116 (strncmp(argv[optind],"--",2) == 0) &&
117 (argv[optind][2] != '\0') ) {
118 int i;
119 for (i = 0; longopts[i].name != NULL; i++) {
120 int length = strlen(longopts[i].name);
121 if (strncmp(argv[optind]+2,longopts[i].name,length) == 0) {
122 if ( (longopts[i].has_arg == no_argument) && (argv[optind][2+length] == '\0') ) {
123 optind++;
124 return longopts[i].val;
126 else if ( (longopts[i].has_arg == required_argument) && (argv[optind][2+length] == '=') ) {
127 optarg=argv[optind]+3+length;
128 optind++;
129 return longopts[i].val;
131 else if ( (longopts[i].has_arg == required_argument) && (argv[optind][2+length] == '\0') ) {
132 optarg=argv[optind+1];
133 optind+=2;
134 return longopts[i].val;
136 else if ( (longopts[i].has_arg == optional_argument) && (argv[optind][2+length] == '=') ) {
137 optarg=argv[optind]+3+length;
138 optind++;
139 return longopts[i].val;
141 else if ( (longopts[i].has_arg==optional_argument) && (argv[optind][2+length] == '\0') ) {
142 optind++;
143 return longopts[i].val;
148 return getopt(argc,argv,optstring);
151 #endif // HAVE_GETOPT_LONG
153 int main(int argc, char *argv[])
155 /* Declaration of variables */
156 FILE *amulesig;
157 int use_page = 0;
158 char *config_path=NULL;
159 char *path;
160 char *stats[20];
161 char *lines[IMG_TEXTLINES];
162 long lSize;
163 char * buffer;
164 int i;
165 int c;
166 int errflag = 0;
167 #ifdef __GD__
168 int use_out_pic = 0;
169 char *path_for_picture=NULL;
170 #endif /* __GD__ */
171 char *path_for_html=NULL;
172 CONF config;
173 time_t lt;
174 struct tm *ltp;
175 char arr[20];
177 while ((c = getopt_long (argc, argv, "c:P:H:hpo", long_options, NULL)) != -1)
179 switch (c)
181 case 'c':
182 config_path=optarg;
183 break;
184 case 'h':
185 usage(argv[0]);
186 exit(0);
187 case 'H':
188 case 'p':
189 use_page=1;
190 if (optarg != NULL) {
191 path_for_html = optarg;
193 break;
194 #ifdef __GD__
195 case 'P':
196 case 'o':
197 use_out_pic=1;
198 if (optarg != NULL) {
199 path_for_picture = optarg;
201 break;
202 #endif /* __GD__ */
203 case '?':
204 errflag++;
206 if (errflag) {
207 usage(argv[0]);
208 exit (2);
211 /* get amulesig path */
213 path = get_amule_path("amulesig.dat", 1, config_path);
215 if (path == NULL) {
216 perror("Unable to get aMule settings path\n");
217 exit(1);
220 /* open the file and if not exists exit with an error */
221 if ((amulesig = fopen(path, "r")) == NULL) {
222 fprintf(stderr, "Unable to open file %s\nCheck if you have amule online signature enabled.\n", path);
223 exit(2);
225 /* i believe this shouldnt be here.
226 The freq of update could be higher than 60 seconds.
227 And it doesn't mean that the amule is not running.
230 else {
231 struct stat s_file;
232 if ( stat(path, &s_file) == 0 ) {
233 time_t t_now = time(0);
234 if ( (t_now - s_file.st_mtime) > 60 ) {
235 perror("aMule online signature last updated more then 60 sec ago\n");
236 perror("Check that your aMule is running\n");
240 free(path);
242 /* initialize all the strings with nothing */
243 for (i = 0; i <= 19; i++)
244 stats[i] = 0;
246 /* start reading the stuff from amulesign to the stats array */
247 // obtain file size.
248 fseek (amulesig , 0 , SEEK_END);
249 lSize = ftell (amulesig);
250 if (0 == lSize) {
251 perror("aMule signature file is 0 Byte, exiting.\n");
252 exit(2);
254 rewind (amulesig);
255 buffer = (char*) malloc (lSize);
256 if (buffer == NULL) {
257 perror("Could not create buffer\n");
258 exit (2);
260 if (fread(buffer,1,lSize,amulesig)){} // // prevent GCC warning
261 fclose(amulesig);
263 stats[0] = strtok (buffer,"\n");
264 for (i=1;i<17;i++) {
265 stats[i] = strtok (NULL,"\n");
266 if (NULL == stats[i]) {
267 perror("Too few fields in aMule signature file, exiting.\n");
268 exit(2);
272 // local time stored as stats[17]
273 lt = time(NULL);
274 ltp = localtime(&lt);
275 strftime(arr, 20, "%b %d %Y, %H:%M", ltp);
277 // if amule isn't running say that and exit else print out the stuff
279 // if amule uptime is 0, then its not running...
280 if (strncmp(stats[16],"0",1) == 0 ) {
281 perror("aMule is not running\n");
282 exit(3);
286 if (strncmp(stats[0],"2",1) == 0)
287 CreateLine(lines, 0 ,"aMule %s is connecting\n", stats[13]);
288 else
289 CreateLine(lines, 0, "aMule %s has been running for %s\n",
290 stats[13], timeconv(stats[16]));
294 if (strncmp(stats[0],"0",1) == 0 && strncmp(stats[5],"0",1) == 0)
295 CreateLine(lines, 1, "%s is not connected ", stats[10]);
296 else if (strncmp(stats[0],"0",1) == 0 && strncmp(stats[5],"0",1) != 0)
297 CreateLine(lines, 1, "%s is connected to ", stats[10]);
298 else
299 CreateLine(lines, 1, "%s is connected to %s [%s:%s] with ", stats[10],
300 stats[1], stats[2], stats[3]);
303 if (strncmp(stats[5],"2",1) == 0) {
304 if (strncmp(stats[4],"H",1) == 0)
305 AppendToLine(lines, 1, "HighID | Kad: ok \n");
306 else if (strncmp(stats[4],"L",1) == 0)
307 AppendToLine(lines, 1, "LowID | Kad: ok \n");
308 else
309 AppendToLine(lines, 1, "Kad: ok \n");
310 } else if (strncmp(stats[5],"1",1) == 0) {
311 if (strncmp(stats[4],"H",1) == 0)
312 AppendToLine(lines, 1, "HighID | Kad: firewalled \n");
313 else if (strncmp(stats[4],"L",1) == 0)
314 AppendToLine(lines, 1, "LowID | Kad: firewalled \n");
315 else
316 AppendToLine(lines, 1, "Kad: firewalled \n");
317 } else {
318 if (strncmp(stats[4],"H",1) == 0)
319 AppendToLine(lines, 1, "HighID | Kad: off \n");
320 else if (strncmp(stats[4],"L",1) == 0)
321 AppendToLine(lines, 1, "LowID | Kad: off \n");
322 else
323 AppendToLine(lines, 1, "but running\n");
326 stats[11] = strdup(convbytes(stats[11]));
327 stats[12] = strdup(convbytes(stats[12]));
329 CreateLine(lines, 2, "Total Download: %s, Upload: %s\n",stats[11] , stats[12]);
331 stats[15] = strdup(convbytes(stats[15]));
332 stats[14] = strdup(convbytes(stats[14]));
334 CreateLine(lines, 3, "Session Download: %s, Upload: %s\n",stats[14], stats[15]);
336 CreateLine(lines, 4, "Download: %s kB/s, Upload: %s kB/s\n", stats[6], stats[7]);
338 CreateLine(lines, 5, "Sharing: %s file(s), Clients on queue: %s\n", stats[9] , stats[8]);
340 CreateLine(lines, 6, "Time: %s\n", arr);
342 #ifdef __GD__
343 if (use_out_pic == 1) {
344 if (!readconfig(&config)) {
345 perror("Could not read config file\n");
346 exit(4);
349 if (!createimage(&config, lines, path_for_picture)) {
350 perror("Could not create image!\n");
351 exit(5);
353 exit(0);
355 #endif
357 if (use_page == 1) {
359 if (!readconfig(&config)) {
360 perror("Could not read config file\n");
361 exit(4);
364 if (!create_html(stats,lines,config.template, path_for_html)) {
365 perror("Could not create the HTML Page.\n");
368 #ifdef __GD__
369 if (!createimage(&config, lines, path_for_picture)) {
370 perror("Could not create image!\n");
371 exit(5);
373 #endif
376 exit(0);
378 for (i = 0; i <= 6; i++) {
379 printf("%s", lines[i]);
380 free(lines[i]);
382 free(buffer);
383 exit(0);