]> git.ipfire.org Git - thirdparty/sarg.git/blame - email.c
All the sprintf/fputs have been replaced by fprintf to avoid an intermediary buffer...
[thirdparty/sarg.git] / email.c
CommitLineData
25697a35 1/*
c37945ed
FM
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
94ff9470 4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
25697a35
GS
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26#include "include/conf.h"
27
32e71fa4 28int geramail(const char *dirname, int debug, const char *outdir, int userip, const char *email, const char *TempDir)
25697a35
GS
29{
30
31 FILE *fp_in, *fp_top1, *fp_top2, *fp_top3;
32 long long int ttnbytes=0, ttnacc=0, tnacc=0;
33 long long int tnbytes=0, ttnelap=0, tnelap=0;
34 float perc=0.00;
35 float perc2=0.00;
36 int posicao=0;
d6e703cc 37 char olduser[MAXLEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN];
354c1a68 38 char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAXLEN], nacc[20], nbytes[20], url[1024], tusr[MAXLEN];
25697a35
GS
39 char ip[MAXLEN], hora[9], data[11], elap[15], user2[MAXLEN], wperc[8], wperc2[8];
40 char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
41 char incac[16], oucac[16];
42 int totuser=0;
43 time_t t;
44 struct tm *local;
456d78a5 45 int cstatus;
25697a35
GS
46
47 strcpy(wger,dirname);
48 strcpy(top1,dirname);
49 strcpy(top2,dirname);
50 strcpy(top3,dirname);
51 strcpy(tusr,dirname);
d6e703cc 52 strcat(wger,"/sarg-general");
25697a35
GS
53 strcat(top1,"/top");
54 strcat(top2,"/top.tmp");
55 strcat(top3,"/report");
d6e703cc 56 strcat(tusr,"/sarg-users");
25697a35
GS
57
58 if((fp_in=fopen(wger,"r"))==NULL) {
59 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],wger);
60 exit(1);
61 }
62
63 if((fp_top1=fopen(top1,"w"))==NULL) {
64 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
65 exit(1);
66 }
67
68 if((fp_top2=fopen(top2,"w"))==NULL) {
69 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top2);
70 exit(1);
71 }
72
73 fscanf(fp_in,"%s",user);
74 fscanf(fp_in,"%s",nacc);
75 fscanf(fp_in,"%s",nbytes);
76 fscanf(fp_in,"%s",url);
77 fscanf(fp_in,"%s",ip);
78 fscanf(fp_in,"%s",hora);
79 fscanf(fp_in,"%s",data);
80 fscanf(fp_in,"%s",elap);
81 fscanf(fp_in,"%s",incac);
82 fscanf(fp_in,"%s",oucac);
83
84 strcpy(olduser,user);
85 totuser=1;
86
87 while(!feof(fp_in))
88 {
89
90 if(strcmp(olduser,user) != 0)
91 {
92 if(strcmp(user,"TOTAL") != 0)
93 totuser++;
94
95#if defined(__FreeBSD__)
354c1a68 96 fprintf(fp_top2,"%s %15qu %15qu %15qu\n",olduser,tnbytes,tnacc,tnelap);
25697a35 97#elif defined(__alpha) || __ALPHA
354c1a68 98 fprintf(fp_top2,"%s %15ld %15ld %15ld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 99#else
354c1a68 100 fprintf(fp_top2,"%s %15lld %15lld %15lld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 101#endif
354c1a68 102 strcpy(olduser,user);
25697a35 103 ttnbytes=ttnbytes+tnbytes;
354c1a68
FM
104 ttnacc=ttnacc+tnacc;
105 ttnelap=ttnelap+tnelap;
25697a35
GS
106 tnbytes=0;
107 tnacc=0;
108 tnelap=0;
109 }
110
111 tnbytes=tnbytes+atol(nbytes);
112 tnacc=tnacc+atol(nacc);
113 tnelap=tnelap+atol(elap);
114
115 fscanf(fp_in,"%s",user);
116 fscanf(fp_in,"%s",nacc);
117 fscanf(fp_in,"%s",nbytes);
118 fscanf(fp_in,"%s",url);
119 fscanf(fp_in,"%s",ip);
120 fscanf(fp_in,"%s",hora);
121 fscanf(fp_in,"%s",data);
122 fscanf(fp_in,"%s",elap);
123 fscanf(fp_in,"%s",incac);
124 fscanf(fp_in,"%s",oucac);
125
126 if(strcmp(user,"TOTAL") == 0)
127 continue;
128 }
129#if defined(__FreeBSD__)
354c1a68 130 fprintf(fp_top2,"%s %15qu %15qu %15qu\n",olduser,tnbytes,tnacc,tnelap);
25697a35 131#elif defined(__alpha) || __ALPHA
354c1a68 132 fprintf(fp_top2,"%s %15ld %15ld %15ld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 133#else
354c1a68 134 fprintf(fp_top2,"%s %15lld %15lld %15lld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 135#endif
25697a35
GS
136 ttnbytes=ttnbytes+tnbytes;
137 ttnacc=ttnacc+tnacc;
138 ttnelap=ttnelap+tnelap;
139
25697a35
GS
140 fclose(fp_in);
141 fclose(fp_top2);
142
94ff9470 143 sprintf(csort,"sort -n -T %s -r -k 2,2 -o '%s' '%s'", TempDir, top1, top2);
456d78a5
FM
144 cstatus=system(csort);
145 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
146 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
147 fprintf(stderr, "SARG: sort command: %s\n",csort);
148 exit(1);
149 }
25697a35
GS
150
151 unlink(top2);
152
153 if((fp_top1=fopen(top1,"a"))==NULL) {
154 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
155 exit(1);
156 }
354c1a68
FM
157#if defined(__FreeBSD__)
158 fprintf(fp_top1,"TOTAL %15qu %15qu %15qu\n",ttnbytes,ttnacc,ttnelap);
159#elif defined(__alpha) || __ALPHA
160 fprintf(fp_top1,"TOTAL %15ld %15ld %15ld\n",ttnbytes,ttnacc,ttnelap);
161#else
162 fprintf(fp_top1,"TOTAL %15lld %15lld %15lld\n",ttnbytes,ttnacc,ttnelap);
163#endif
25697a35
GS
164 fclose(fp_top1);
165
166 if((fp_top1=fopen(top1,"r"))==NULL) {
167 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
168 exit(1);
169 }
170
171 if((fp_top3=fopen(top3,"w"))==NULL) {
172 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
173 exit(1);
174 }
175
176 /*
d6e703cc 177 * Obtem o period
25697a35
GS
178 */
179
180 strcpy(arqper,dirname);
d6e703cc 181 strcat(arqper,"/sarg-period");
25697a35
GS
182
183 if ((fp_in = fopen(arqper, "r")) == 0){
184 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],arqper);
185 exit(1);
186 }
187
d6e703cc 188 fgets(period,sizeof(period),fp_in);
25697a35
GS
189 fclose(fp_in);
190
191 sprintf(strip1,"%s",text[88]);
192 strip_latin(strip1);
354c1a68 193 fprintf(fp_top3,"%s\n",strip1);
25697a35
GS
194
195 sprintf(strip1,"%s",text[97]);
196 strip_latin(strip1);
354c1a68 197 fprintf(fp_top3,"%s\n",strip1);
25697a35
GS
198
199 sprintf(strip1,"%s",text[89]);
200 strip_latin(strip1);
354c1a68 201 fprintf(fp_top3,"%s %s\n\n",strip1,period);
25697a35
GS
202
203 sprintf(strip1,"%s",text[100]);
204 strip_latin(strip1);
205 sprintf(strip2,"%s",text[98]);
206 strip_latin(strip2);
207 sprintf(strip3,"%s",text[92]);
208 strip_latin(strip3);
209 sprintf(strip4,"%s",text[93]);
210 strip_latin(strip4);
211 sprintf(strip5,"%s",text[94]);
212 strip_latin(strip5);
213 sprintf(strip6,"%s",text[95]);
214 strip_latin(strip6);
215 sprintf(strip7,"%s",text[99]);
216 strip_latin(strip7);
217
354c1a68 218 fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
25697a35
GS
219
220 fscanf(fp_top1,"%s",user);
221 fscanf(fp_top1,"%s",nbytes);
222 fscanf(fp_top1,"%s",nacc);
223 fscanf(fp_top1,"%s",elap);
224
225 while(!feof(fp_top1))
226 {
227
228 if(strstr(user,"_") != 0)
229 fixip(user);
230
231 strcpy(user2,user);
232
233 tnbytes=atol(nbytes);
234
235 if(tnbytes) {
236 perc=ttnbytes / 100;
237 perc=tnbytes / perc;
238 } else perc = 0;
239
240 if(atol(elap)) {
241 perc2=ttnelap / 100;
242 perc2=atol(elap) / perc2;
243 } else perc2 = 0;
244
245 posicao++;
246 tnelap=atol(elap);
247
248 sprintf(wperc,"%3.2f%%",perc);
249 sprintf(wperc2,"%3.2f%%",perc2);
250
354c1a68
FM
251 if(strstr(user,"TOTAL") != 0){
252 fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3);
25697a35 253#if defined(__FreeBSD__)
354c1a68 254 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 255#elif defined(__alpha) || __ALPHA
354c1a68 256 fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 257#else
354c1a68 258 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 259#endif
354c1a68 260 } else {
25697a35 261#if defined(__FreeBSD__)
354c1a68 262 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10qu %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
25697a35 263#elif defined(__alpha) || __ALPHA
354c1a68 264 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10ld %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
25697a35 265#else
354c1a68 266 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10lld %7s\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
25697a35
GS
267#endif
268 }
269
25697a35
GS
270 fscanf(fp_top1,"%s",user);
271 fscanf(fp_top1,"%s",nbytes);
272 fscanf(fp_top1,"%s",nacc);
273 fscanf(fp_top1,"%s",elap);
274 }
275
276 if(ttnbytes) {
277 tnbytes=ttnbytes / totuser;
278 } else tnbytes=0;
279
280 sprintf(strip1,"%s",text[96]);
281 strip_latin(strip1);
282#if defined(__FreeBSD__)
354c1a68 283 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 284#elif defined(__alpha) || __ALPHA
354c1a68 285 fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 286#else
354c1a68 287 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 288#endif
25697a35
GS
289
290 fclose(fp_top1);
291 unlink(top1);
292
293 t = time(NULL);
294 local = localtime(&t);
354c1a68 295 fprintf(fp_top3, "\n%s\n", asctime(local));
25697a35
GS
296
297 fclose(fp_top3);
298
299 if(strcmp(email,"stdout") == 0) {
300 if((fp_top3=fopen(top3,"r"))==NULL) {
301 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
302 exit(1);
303 }
304
305 while(fgets(buf,sizeof(buf),fp_top3)!=NULL)
306 printf("%s",buf);
307 } else {
308 sprintf(buf,"%s -s 'SARG %s, %s' %s <%s",MailUtility,text[55],asctime(local),email,top3);
456d78a5
FM
309 cstatus=system(buf);
310 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
311 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
312 fprintf(stderr, "SARG: command: %s\n",buf);
313 exit(1);
314 }
315 }
25697a35 316
d6e703cc 317 sprintf(csort,"rm -rf %s/sarg",TempDir);
25697a35
GS
318 system(csort);
319
320 return (0);
321}