Upstream tarball 20080304
[amule.git] / src / utils / cas / cas.c
blob50ab07acf8b77c6f04e809222352ab99579efb6b
1 /*
2 * Name: Main cas file
4 * Purpose: aMule Statistics
6 * Author: Pedro de Oliveira <falso@rdk.homeip.net>
8 * Copyright (C) 2004 by Pedro de Oliveira
9 *
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"
44 * History:
46 * ????.??.?? - falso: creation of cas.
47 * ????.??.?? - Jacobo221: Detect connecting state
48 * ????.??.?? - falso: HTML page generation
49 * 2004.08.27 - GonoszTopi: New line handling routines, to cope with lines
50 * longer than 80 characters. Fixes buffer overflow.
51 * 2005.12.10 - fulgas: added kad info support
52 * 2005,12,16 - stefanero: fixed Kad related stuff and some other things
55 static struct option long_options[] = {
56 { "help", no_argument, NULL, 'h' },
57 { "html", optional_argument, NULL, 'p' },
58 { "picture", optional_argument, NULL, 'o' },
59 { "config-dir", required_argument, NULL, 'c' },
60 { NULL, 0, NULL, 0 }
63 void usage(char *myname)
65 printf (" ___ _ _ ___ c aMule statistics\n"
66 " /'___) /'_` )/',__) by Pedro de Oliveira\n"
67 "( (___ ( (_| |\\__, \\ <falso@rdk.homeip.net>\n"
68 "`\\____)`\\__,_)(____/ Version %s\n\n"
70 "Usage: %s [OPTION]\n"
71 "If run without any option prints stats to stdout\n\n"
72 "OPTIONS:\n"
73 #ifdef __GD__
74 "-o, --picture, -P\tWrites the online signature picture\n"
75 #endif
76 "-p, --html, -H\t\tHTML Page with stats and picture\n"
77 "-c, --config-dir\tSpecifies a config-dir different from home\n"
78 "-h, --help\t\tThis help you're reading\n", CAS_VERSION, myname);
81 int main(int argc, char *argv[])
83 /* Declaration of variables */
84 FILE *amulesig;
85 int use_out_pic = 0;
86 int use_page = 0;
87 char *config_path=NULL;
88 char *path;
89 char *stats[20];
90 char *lines[7];
91 long lSize;
92 char * buffer;
93 int i;
94 int c;
95 char *path_for_picture=NULL;
96 char *path_for_html=NULL;
97 CONF config;
98 time_t lt;
99 struct tm *ltp;
100 char arr[20];
102 while ((c = getopt_long (argc, argv, "c:P:H:hpo", long_options, NULL)) != -1)
104 switch (c)
106 case 'c':
107 config_path=optarg;
108 break;
109 case 'h':
110 usage(argv[0]);
111 exit(0);
112 case 'H':
113 case 'p':
114 use_page=1;
115 if (optarg != NULL) {
116 path_for_html = optarg;
118 break;
119 case 'P':
120 case 'o':
121 use_out_pic=1;
122 if (optarg != NULL) {
123 path_for_picture = optarg;
125 break;
128 /* get amulesig path */
130 path = get_amule_path("amulesig.dat", 1, config_path);
132 if (path == NULL) {
133 perror("Unable to get aMule settings path\n");
134 exit(1);
137 /* open the file and if not exists exit with an error */
138 if ((amulesig = fopen(path, "r")) == NULL) {
139 fprintf(stderr, "Unable to open file %s\nCheck if you have amule online signature enabled.\n", path);
140 exit(2);
142 /* i believe this shouldnt be here.
143 The freq of update could be higher than 60 seconds.
144 And it doesn't mean that the amule is not running.
147 else {
148 struct stat s_file;
149 if ( stat(path, &s_file) == 0 ) {
150 time_t t_now = time(0);
151 if ( (t_now - s_file.st_mtime) > 60 ) {
152 perror("aMule online signature last updated more then 60 sec ago\n");
153 perror("Check that your aMule is running\n");
157 free(path);
159 /* initialize all the strings with nothing */
160 for (i = 0; i <= 19; i++)
161 stats[i] = 0;
163 /* start reading the stuff from amulesign to the stats array */
164 // obtain file size.
165 fseek (amulesig , 0 , SEEK_END);
166 lSize = ftell (amulesig);
167 rewind (amulesig);
168 buffer = (char*) malloc (lSize);
169 if (buffer == NULL) {
170 perror("Could not create buffer\n");
171 exit (2);
173 fread (buffer,1,lSize,amulesig);
174 fclose(amulesig);
176 stats[0] = strtok (buffer,"\n"); /* ed2k status*/
177 stats[1] = strtok (NULL,"\n"); /* server name */
178 stats[2] = strtok (NULL,"\n"); /* server ip */
179 stats[3] = strtok (NULL,"\n"); /* server port */
180 stats[4] = strtok (NULL,"\n"); /* high or low id */
181 stats[5] = strtok (NULL,"\n"); /* kad status */
182 stats[6] = strtok (NULL,"\n"); /* dl */
183 stats[7] = strtok (NULL,"\n"); /* ul*/
184 stats[8] = strtok (NULL,"\n"); /* queue*/
185 stats[9] = strtok (NULL,"\n"); /* clients*/
186 stats[10] = strtok (NULL,"\n"); /* nick*/
187 stats[11] = strtok (NULL,"\n"); /* total download */
188 stats[12] = strtok (NULL,"\n"); /* total upload */
189 stats[13] = strtok (NULL,"\n"); /* version */
190 stats[14] = strtok (NULL,"\n"); /*Session download*/
191 stats[15] = strtok (NULL,"\n"); /* Session upload */
192 stats[16] = strtok (NULL,"\n"); /* aMule running Time */
194 // local time stored as stats[17]
195 lt = time(NULL);
196 ltp = localtime(&lt);
197 strftime(arr, 20, "%b %d %Y, %H:%M", ltp);
199 // if amule isn't running say that and exit else print out the stuff
201 // if amule uptime is 0, then its not running...
202 if (strncmp(stats[16],"0",1) == 0 ) {
203 perror("aMule is not running\n");
204 exit(3);
208 if (strncmp(stats[0],"2",1) == 0)
209 CreateLine(lines, 0 ,"aMule %s is connecting\n", stats[13]);
210 else
211 CreateLine(lines, 0, "aMule %s has been running for %s\n",
212 stats[13], timeconv(stats[16]));
216 if (strncmp(stats[0],"0",1) == 0 && strncmp(stats[5],"0",1) == 0)
217 CreateLine(lines, 1, "%s is not connected ", stats[10]);
218 else if (strncmp(stats[0],"0",1) == 0 && strncmp(stats[5],"0",1) != 0)
219 CreateLine(lines, 1, "%s is connected to ", stats[10]);
220 else
221 CreateLine(lines, 1, "%s is connected to %s [%s:%s] with ", stats[10],
222 stats[1], stats[2], stats[3]);
225 if (strncmp(stats[5],"2",1) == 0) {
226 if (strncmp(stats[4],"H",1) == 0)
227 AppendToLine(lines, 1, "HighID | Kad: ok \n");
228 else if (strncmp(stats[4],"L",1) == 0)
229 AppendToLine(lines, 1, "LowID | Kad: ok \n");
230 else
231 AppendToLine(lines, 1, "Kad: ok \n");
232 } else if (strncmp(stats[5],"1",1) == 0) {
233 if (strncmp(stats[4],"H",1) == 0)
234 AppendToLine(lines, 1, "HighID | Kad: firewalled \n");
235 else if (strncmp(stats[4],"L",1) == 0)
236 AppendToLine(lines, 1, "LowID | Kad: firewalled \n");
237 else
238 AppendToLine(lines, 1, "Kad: firewalled \n");
239 } else {
240 if (strncmp(stats[4],"H",1) == 0)
241 AppendToLine(lines, 1, "HighID | Kad: off \n");
242 else if (strncmp(stats[4],"L",1) == 0)
243 AppendToLine(lines, 1, "LowID | Kad: off \n");
244 else
245 AppendToLine(lines, 1, "but running\n");
248 stats[11] = strdup(convbytes(stats[11]));
249 stats[12] = strdup(convbytes(stats[12]));
251 CreateLine(lines, 2, "Total Download: %s, Upload: %s\n",stats[11] , stats[12]);
253 stats[15] = strdup(convbytes(stats[15]));
254 stats[14] = strdup(convbytes(stats[14]));
256 CreateLine(lines, 3, "Session Download: %s, Upload: %s\n",stats[14], stats[15]);
258 CreateLine(lines, 4, "Download: %s kB/s, Upload: %s kB/s\n", stats[6], stats[7]);
260 CreateLine(lines, 5, "Sharing: %s file(s), Clients on queue: %s\n", stats[9] , stats[8]);
262 CreateLine(lines, 6, "Time: %s\n", arr);
264 #ifdef __GD__
265 if (use_out_pic == 1) {
266 if (!readconfig(&config)) {
267 perror("Could not read config file\n");
268 exit(4);
271 if (!createimage(&config, lines, path_for_picture)) {
272 perror("Could not create image!\n");
273 exit(5);
275 exit(0);
277 #endif
279 if (use_page == 1) {
281 if (!readconfig(&config)) {
282 perror("Could not read config file\n");
283 exit(4);
286 if (!create_html(stats,lines,config.template, path_for_html)) {
287 perror("Could not create the HTML Page.\n");
290 #ifdef __GD__
291 if (!createimage(&config, lines, path_for_picture)) {
292 perror("Could not create image!\n");
293 exit(5);
295 #endif
298 exit(0);
300 for (i = 0; i <= 6; i++) {
301 printf("%s", lines[i]);
302 free(lines[i]);
304 free(buffer);
305 exit(0);