00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 static int fstat_unpack(struct userrec *u, struct user_entry *e)
00024 {
00025 char *par, *arg;
00026 struct filesys_stats *fs;
00027
00028 fs = user_malloc(sizeof(struct filesys_stats));
00029 egg_bzero(fs, sizeof(struct filesys_stats));
00030 par = e->u.list->extra;
00031
00032 arg = newsplit(&par);
00033 if (arg[0])
00034 fs->uploads = atoi(arg);
00035
00036 arg = newsplit(&par);
00037 if (arg[0])
00038 fs->upload_ks = atoi(arg);
00039
00040 arg = newsplit(&par);
00041 if (arg[0])
00042 fs->dnloads = atoi(arg);
00043
00044 arg = newsplit(&par);
00045 if (arg[0])
00046 fs->dnload_ks = atoi(arg);
00047
00048 list_type_kill(e->u.list);
00049 e->u.extra = fs;
00050
00051 return 1;
00052 }
00053
00054 static int fstat_pack(struct userrec *u, struct user_entry *e)
00055 {
00056 register struct filesys_stats *fs;
00057 struct list_type *l = user_malloc(sizeof(struct list_type));
00058
00059 fs = e->u.extra;
00060 l->extra = user_malloc(41);
00061 egg_snprintf(l->extra, 41, "%09u %09u %09u %09u", fs->uploads, fs->upload_ks,
00062 fs->dnloads, fs->dnload_ks);
00063 l->next = NULL;
00064 e->u.list = l;
00065 nfree(fs);
00066
00067 return 1;
00068 }
00069
00070 static int fstat_write_userfile(FILE *f, struct userrec *u,
00071 struct user_entry *e)
00072 {
00073 register struct filesys_stats *fs;
00074
00075 fs = e->u.extra;
00076 if (fprintf(f, "--FSTAT %09u %09u %09u %09u\n", fs->uploads, fs->upload_ks,
00077 fs->dnloads, fs->dnload_ks) == EOF)
00078 return 0;
00079
00080 return 1;
00081 }
00082
00083 static int fstat_set(struct userrec *u, struct user_entry *e, void *buf)
00084 {
00085 register struct filesys_stats *fs = buf;
00086
00087 if (e->u.extra != fs) {
00088 if (e->u.extra)
00089 nfree(e->u.extra);
00090 e->u.extra = fs;
00091 } else if (!fs)
00092 return 1;
00093
00094 if (!noshare && !(u->flags & (USER_BOT | USER_UNSHARED))) {
00095 if (fs)
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 shareout(NULL, "ch fstat %09u %09u %09u %09u\n", fs->uploads,
00110 fs->upload_ks, fs->dnloads, fs->dnload_ks);
00111 else
00112 shareout(NULL, "ch fstat r\n");
00113 }
00114
00115 return 1;
00116 }
00117
00118 static int fstat_tcl_get(Tcl_Interp *irp, struct userrec *u,
00119 struct user_entry *e, int argc, char **argv)
00120 {
00121 register struct filesys_stats *fs;
00122 char d[50];
00123
00124 BADARGS(3, 4, " handle FSTAT ?u/d?");
00125
00126 fs = e->u.extra;
00127 if (argc == 3)
00128 egg_snprintf(d, sizeof d, "%u %u %u %u", fs->uploads, fs->upload_ks,
00129 fs->dnloads, fs->dnload_ks);
00130 else
00131 switch (argv[3][0]) {
00132 case 'u':
00133 egg_snprintf(d, sizeof d, "%u %u", fs->uploads, fs->upload_ks);
00134 break;
00135 case 'd':
00136 egg_snprintf(d, sizeof d, "%u %u", fs->dnloads, fs->dnload_ks);
00137 break;
00138 }
00139
00140 Tcl_AppendResult(irp, d, NULL);
00141 return TCL_OK;
00142 }
00143
00144 static int fstat_kill(struct user_entry *e)
00145 {
00146 if (e->u.extra)
00147 nfree(e->u.extra);
00148 nfree(e);
00149
00150 return 1;
00151 }
00152
00153 static int fstat_expmem(struct user_entry *e)
00154 {
00155 return sizeof(struct filesys_stats);
00156 }
00157
00158 static void fstat_display(int idx, struct user_entry *e)
00159 {
00160 struct filesys_stats *fs;
00161
00162 fs = e->u.extra;
00163 dprintf(idx, " FILES: %u download%s (%luk), %u upload%s (%luk)\n",
00164 fs->dnloads, (fs->dnloads == 1) ? "" : "s", fs->dnload_ks,
00165 fs->uploads, (fs->uploads == 1) ? "" : "s", fs->upload_ks);
00166 }
00167
00168 static struct user_entry_type USERENTRY_FSTAT = {
00169 NULL,
00170 fstat_gotshare,
00171 fstat_dupuser,
00172 fstat_unpack,
00173 fstat_pack,
00174 fstat_write_userfile,
00175 fstat_kill,
00176 NULL,
00177 fstat_set,
00178 fstat_tcl_get,
00179 fstat_tcl_set,
00180 fstat_expmem,
00181 fstat_display,
00182 "FSTAT"
00183 };
00184
00185 static int fstat_gotshare(struct userrec *u, struct user_entry *e,
00186 char *par, int idx)
00187 {
00188 char *p;
00189 struct filesys_stats *fs;
00190
00191 noshare = 1;
00192 switch (par[0]) {
00193 case 'u':
00194 case 'd':
00195 break;
00196 case 'r':
00197 set_user(&USERENTRY_FSTAT, u, NULL);
00198 break;
00199 default:
00200 if (!(fs = e->u.extra)) {
00201 fs = user_malloc(sizeof(struct filesys_stats));
00202 egg_bzero(fs, sizeof(struct filesys_stats));
00203 }
00204
00205 p = newsplit(&par);
00206 if (p[0])
00207 fs->uploads = atoi(p);
00208
00209 p = newsplit(&par);
00210 if (p[0])
00211 fs->upload_ks = atoi(p);
00212
00213 p = newsplit(&par);
00214 if (p[0])
00215 fs->dnloads = atoi(p);
00216
00217 p = newsplit(&par);
00218 if (p[0])
00219 fs->dnload_ks = atoi(p);
00220
00221 set_user(&USERENTRY_FSTAT, u, fs);
00222 break;
00223 }
00224 noshare = 0;
00225
00226 return 1;
00227 }
00228
00229 static int fstat_dupuser(struct userrec *u, struct userrec *o,
00230 struct user_entry *e)
00231 {
00232 struct filesys_stats *fs;
00233
00234 if (e->u.extra) {
00235 fs = user_malloc(sizeof(struct filesys_stats));
00236 my_memcpy(fs, e->u.extra, sizeof(struct filesys_stats));
00237 return set_user(&USERENTRY_FSTAT, u, fs);
00238 }
00239
00240 return 0;
00241 }
00242
00243 static void stats_add_dnload(struct userrec *u, unsigned long bytes)
00244 {
00245 struct user_entry *ue;
00246 register struct filesys_stats *fs;
00247
00248 if (u) {
00249 if (!(ue = find_user_entry(&USERENTRY_FSTAT, u)) || !(fs = ue->u.extra)) {
00250 fs = user_malloc(sizeof(struct filesys_stats));
00251 egg_bzero(fs, sizeof(struct filesys_stats));
00252 }
00253 fs->dnloads++;
00254 fs->dnload_ks += ((bytes + 512) / 1024);
00255 set_user(&USERENTRY_FSTAT, u, fs);
00256
00257 }
00258 }
00259
00260 static void stats_add_upload(struct userrec *u, unsigned long bytes)
00261 {
00262 struct user_entry *ue;
00263 register struct filesys_stats *fs;
00264
00265 if (u) {
00266 if (!(ue = find_user_entry(&USERENTRY_FSTAT, u)) || !(fs = ue->u.extra)) {
00267 fs = user_malloc(sizeof(struct filesys_stats));
00268 egg_bzero(fs, sizeof(struct filesys_stats));
00269 }
00270 fs->uploads++;
00271 fs->upload_ks += ((bytes + 512) / 1024);
00272 set_user(&USERENTRY_FSTAT, u, fs);
00273
00274 }
00275 }
00276
00277 static int fstat_tcl_set(Tcl_Interp *irp, struct userrec *u,
00278 struct user_entry *e, int argc, char **argv)
00279 {
00280 register struct filesys_stats *fs;
00281 int f = 0, k = 0;
00282
00283 BADARGS(4, 6, " handle FSTAT u/d ?files ?ks??");
00284
00285 if (argc > 4)
00286 f = atoi(argv[4]);
00287 if (argc > 5)
00288 k = atoi(argv[5]);
00289
00290 switch (argv[3][0]) {
00291 case 'u':
00292 case 'd':
00293 if (!(fs = e->u.extra)) {
00294 fs = user_malloc(sizeof(struct filesys_stats));
00295 egg_bzero(fs, sizeof(struct filesys_stats));
00296 }
00297 switch (argv[3][0]) {
00298 case 'u':
00299 fs->uploads = f;
00300 fs->upload_ks = k;
00301 break;
00302 case 'd':
00303 fs->dnloads = f;
00304 fs->dnload_ks = k;
00305 break;
00306 }
00307 set_user(&USERENTRY_FSTAT, u, fs);
00308 break;
00309 case 'r':
00310 set_user(&USERENTRY_FSTAT, u, NULL);
00311 break;
00312 }
00313
00314 return TCL_OK;
00315 }