00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 static int tcl_getdesc STDVAR
00027 {
00028 char *s = NULL;
00029
00030 BADARGS(3, 3, " dir file");
00031
00032 filedb_getdesc(argv[1], argv[2], &s);
00033 if (s) {
00034 Tcl_AppendResult(irp, s, NULL);
00035 my_free(s);
00036 return TCL_OK;
00037 } else {
00038 Tcl_AppendResult(irp, "filedb access failed", NULL);
00039 return TCL_ERROR;
00040 }
00041 }
00042
00043 static int tcl_setdesc STDVAR
00044 {
00045 BADARGS(4, 4, " dir file desc");
00046
00047 filedb_setdesc(argv[1], argv[2], argv[3]);
00048 return TCL_OK;
00049 }
00050
00051 static int tcl_getowner STDVAR
00052 {
00053 char *s = NULL;
00054
00055 BADARGS(3, 3, " dir file");
00056
00057 filedb_getowner(argv[1], argv[2], &s);
00058 if (s) {
00059 Tcl_AppendResult(irp, s, NULL);
00060 my_free(s);
00061 return TCL_OK;
00062 } else {
00063 Tcl_AppendResult(irp, "filedb access failed", NULL);
00064 return TCL_ERROR;
00065 }
00066 }
00067
00068 static int tcl_setowner STDVAR
00069 {
00070 BADARGS(4, 4, " dir file owner");
00071
00072 filedb_setowner(argv[1], argv[2], argv[3]);
00073 return TCL_OK;
00074 }
00075
00076 static int tcl_getgots STDVAR
00077 {
00078 int i;
00079 char s[10];
00080
00081 BADARGS(3, 3, " dir file");
00082
00083 i = filedb_getgots(argv[1], argv[2]);
00084 sprintf(s, "%d", i);
00085 Tcl_AppendResult(irp, s, NULL);
00086 return TCL_OK;
00087 }
00088
00089 static int tcl_setlink STDVAR
00090 {
00091 BADARGS(4, 4, " dir file link");
00092
00093 filedb_setlink(argv[1], argv[2], argv[3]);
00094 return TCL_OK;
00095 }
00096
00097 static int tcl_getlink STDVAR
00098 {
00099 char *s = NULL;
00100
00101 BADARGS(3, 3, " dir file");
00102
00103 filedb_getlink(argv[1], argv[2], &s);
00104 if (s) {
00105 Tcl_AppendResult(irp, s, NULL);
00106 return TCL_OK;
00107 } else {
00108 Tcl_AppendResult(irp, "filedb access failed", NULL);
00109 return TCL_ERROR;
00110 }
00111 }
00112
00113 static int tcl_setpwd STDVAR
00114 {
00115 int i, idx;
00116
00117 BADARGS(3, 3, " idx dir");
00118
00119 i = atoi(argv[1]);
00120 idx = findanyidx(i);
00121 if ((idx < 0) || (dcc[idx].type != &DCC_FILES)) {
00122 Tcl_AppendResult(irp, "invalid idx", NULL);
00123 return TCL_ERROR;
00124 }
00125 files_setpwd(idx, argv[2]);
00126
00127 return TCL_OK;
00128 }
00129
00130 static int tcl_getpwd STDVAR
00131 {
00132 int i, idx;
00133
00134 BADARGS(2, 2, " idx");
00135
00136 i = atoi(argv[1]);
00137 idx = findanyidx(i);
00138 if ((idx < 0) || (dcc[idx].type != &DCC_FILES)) {
00139 Tcl_AppendResult(irp, "invalid idx", NULL);
00140 return TCL_ERROR;
00141 }
00142 Tcl_AppendResult(irp, dcc[idx].u.file->dir, NULL);
00143
00144 return TCL_OK;
00145 }
00146
00147 static int tcl_getfiles STDVAR
00148 {
00149 BADARGS(2, 2, " dir");
00150
00151 filedb_getfiles(irp, argv[1]);
00152 return TCL_OK;
00153 }
00154
00155 static int tcl_getdirs STDVAR
00156 {
00157 BADARGS(2, 2, " dir");
00158
00159 filedb_getdirs(irp, argv[1]);
00160 return TCL_OK;
00161 }
00162
00163 static int tcl_hide STDVAR
00164 {
00165 BADARGS(3, 3, " dir file");
00166
00167 filedb_change(argv[1], argv[2], FILEDB_HIDE);
00168 return TCL_OK;
00169 }
00170
00171 static int tcl_unhide STDVAR
00172 {
00173 BADARGS(3, 3, " dir file");
00174
00175 filedb_change(argv[1], argv[2], FILEDB_UNHIDE);
00176 return TCL_OK;
00177 }
00178
00179 static int tcl_share STDVAR
00180 {
00181 BADARGS(3, 3, " dir file");
00182
00183 filedb_change(argv[1], argv[2], FILEDB_SHARE);
00184 return TCL_OK;
00185 }
00186
00187 static int tcl_unshare STDVAR
00188 {
00189 BADARGS(3, 3, " dir file");
00190
00191 filedb_change(argv[1], argv[2], FILEDB_UNSHARE);
00192 return TCL_OK;
00193 }
00194
00195 static int tcl_setflags STDVAR
00196 {
00197 FILE *fdb;
00198 filedb_entry *fdbe;
00199 char *s = NULL, *p, *d;
00200
00201 BADARGS(3, 4, " dir ?flags ?channel??");
00202
00203 malloc_strcpy(s, argv[1]);
00204 if (s[strlen(s) - 1] == '/')
00205 s[strlen(s) - 1] = 0;
00206 p = strrchr(s, '/');
00207 if (p == NULL) {
00208 p = s;
00209 d = "";
00210 } else {
00211 *p = 0;
00212 p++;
00213 d = s;
00214 }
00215
00216 fdb = filedb_open(d, 0);
00217 if (!fdb) {
00218 Tcl_AppendResult(irp, "-3", NULL);
00219 my_free(s);
00220 return TCL_OK;
00221 }
00222 filedb_readtop(fdb, NULL);
00223 fdbe = filedb_matchfile(fdb, ftell(fdb), p);
00224 my_free(s);
00225
00226 if (!fdbe) {
00227 Tcl_AppendResult(irp, "-1", NULL);
00228 return TCL_OK;
00229 }
00230 if (!(fdbe->stat & FILE_DIR)) {
00231 Tcl_AppendResult(irp, "-2", NULL);
00232 return TCL_OK;
00233 }
00234 if (argc >= 3) {
00235 struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0 };
00236 char f[100];
00237
00238 break_down_flags(argv[2], &fr, NULL);
00239 build_flags(f, &fr, NULL);
00240 malloc_strcpy_nocheck(fdbe->flags_req, f);
00241 } else
00242 my_free(fdbe->flags_req);
00243 if (argc == 4)
00244 malloc_strcpy(fdbe->chan, argv[3]);
00245
00246 filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_ALL);
00247 free_fdbe(&fdbe);
00248 filedb_close(fdb);
00249 Tcl_AppendResult(irp, "0", NULL);
00250 return TCL_OK;
00251 }
00252
00253 static int tcl_getflags STDVAR
00254 {
00255 filedb_entry *fdbe;
00256 char *s = NULL, *p, *d;
00257
00258 BADARGS(2, 2, " dir");
00259
00260 malloc_strcpy(s, argv[1]);
00261 if (s[strlen(s) - 1] == '/')
00262 s[strlen(s) - 1] = 0;
00263 p = strrchr(s, '/');
00264 if (p == NULL) {
00265 p = s;
00266 d = "";
00267 } else {
00268 *p = 0;
00269 p++;
00270 d = s;
00271 }
00272
00273 fdbe = filedb_getentry(d, p);
00274
00275 if (!fdbe || !(fdbe->stat & FILE_DIR)) {
00276 Tcl_AppendResult(irp, "", NULL);
00277 my_free(s);
00278 free_fdbe(&fdbe);
00279 return TCL_OK;
00280 }
00281 if (fdbe->flags_req) {
00282 malloc_strcpy(s, fdbe->flags_req);
00283 if (s[0] == '-')
00284 s[0] = 0;
00285 } else
00286 s[0] = 0;
00287 Tcl_AppendElement(irp, s);
00288 Tcl_AppendElement(irp, fdbe->chan);
00289 my_free(s);
00290 free_fdbe(&fdbe);
00291 return TCL_OK;
00292 }
00293
00294 static int tcl_mkdir STDVAR
00295 {
00296 FILE *fdb;
00297 filedb_entry *fdbe;
00298 char *s = NULL, *t, *d, *p;
00299 struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0 };
00300
00301 BADARGS(2, 4, " dir ?required-flags ?channel??");
00302
00303 malloc_strcpy(s, argv[1]);
00304
00305 if (s[strlen(s) - 1] == '/')
00306 s[strlen(s) - 1] = 0;
00307
00308 p = strrchr(s, '/');
00309 if (p == NULL) {
00310 p = s;
00311 d = "";
00312 } else {
00313 *p = 0;
00314 p++;
00315 d = s;
00316 }
00317
00318 fdb = filedb_open(d, 0);
00319 if (!fdb) {
00320 Tcl_AppendResult(irp, "-3", NULL);
00321 my_free(s);
00322 return TCL_OK;
00323 }
00324 filedb_readtop(fdb, NULL);
00325 fdbe = filedb_matchfile(fdb, ftell(fdb), p);
00326
00327 if (!fdbe) {
00328 t = nmalloc(strlen(dccdir) + strlen(d) + strlen(p) + 2);
00329 sprintf(t, "%s%s/%s", dccdir, d, p);
00330 if (mkdir(t, 0755) != 0) {
00331 Tcl_AppendResult(irp, "1", NULL);
00332 my_free(t);
00333 my_free(s);
00334 filedb_close(fdb);
00335 return TCL_OK;
00336 }
00337 fdbe = malloc_fdbe();
00338 fdbe->stat = FILE_DIR;
00339 malloc_strcpy(fdbe->filename, argv[1]);
00340 fdbe->uploaded = now;
00341 } else if (!(fdbe->stat & FILE_DIR)) {
00342 Tcl_AppendResult(irp, "2", NULL);
00343 free_fdbe(&fdbe);
00344 my_free(s);
00345 filedb_close(fdb);
00346 return TCL_OK;
00347 }
00348 if (argc >= 3) {
00349 char f[100];
00350
00351 break_down_flags(argv[2], &fr, NULL);
00352 build_flags(f, &fr, NULL);
00353 malloc_strcpy_nocheck(fdbe->flags_req, f);
00354 } else if (fdbe->flags_req) {
00355 my_free(fdbe->flags_req);
00356 }
00357 if (argc == 4) {
00358 malloc_strcpy(fdbe->chan, argv[3]);
00359 } else if (fdbe->chan)
00360 my_free(fdbe->chan);
00361
00362 if (!fdbe->pos)
00363 fdbe->pos = POS_NEW;
00364 filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_ALL);
00365 filedb_close(fdb);
00366 free_fdbe(&fdbe);
00367
00368 Tcl_AppendResult(irp, "0", NULL);
00369 return TCL_OK;
00370 }
00371
00372 static int tcl_rmdir STDVAR
00373 {
00374 FILE *fdb;
00375 filedb_entry *fdbe;
00376 char *s = NULL, *t, *d, *p;
00377
00378 BADARGS(2, 2, " dir");
00379
00380 malloc_strcpy(s, argv[1]);
00381 if (s[strlen(s) - 1] == '/')
00382 s[strlen(s) - 1] = 0;
00383 p = strrchr(s, '/');
00384 if (p == NULL) {
00385 p = s;
00386 d = "";
00387 } else {
00388 *p = 0;
00389 p++;
00390 d = s;
00391 }
00392
00393 fdb = filedb_open(d, 0);
00394 if (!fdb) {
00395 Tcl_AppendResult(irp, "1", NULL);
00396 my_free(s);
00397 return TCL_OK;
00398 }
00399 filedb_readtop(fdb, NULL);
00400 fdbe = filedb_matchfile(fdb, ftell(fdb), p);
00401
00402 if (!fdbe) {
00403 Tcl_AppendResult(irp, "1", NULL);
00404 filedb_close(fdb);
00405 my_free(s);
00406 return TCL_OK;
00407 }
00408 if (!(fdbe->stat & FILE_DIR)) {
00409 Tcl_AppendResult(irp, "1", NULL);
00410 filedb_close(fdb);
00411 free_fdbe(&fdbe);
00412 my_free(s);
00413 return TCL_OK;
00414 }
00415
00416 t = nmalloc(strlen(dccdir) + strlen(d) + strlen(p) + 11);
00417 sprintf(t, "%s%s/%s/.filedb", dccdir, d, p);
00418 unlink(t);
00419 sprintf(t, "%s%s/%s/.files", dccdir, d, p);
00420 unlink(t);
00421 sprintf(t, "%s%s/%s", dccdir, d, p);
00422 my_free(s);
00423 if (rmdir(t) == 0) {
00424 filedb_delfile(fdb, fdbe->pos);
00425 filedb_close(fdb);
00426 free_fdbe(&fdbe);
00427 my_free(t);
00428 Tcl_AppendResult(irp, "0", NULL);
00429 return TCL_OK;
00430 }
00431 my_free(t);
00432 free_fdbe(&fdbe);
00433 filedb_close(fdb);
00434 Tcl_AppendResult(irp, "1", NULL);
00435 return TCL_OK;
00436 }
00437
00438 static int tcl_mv_cp(Tcl_Interp *irp, int argc, char **argv, int copy)
00439 {
00440 char *p, *fn = NULL, *oldpath = NULL, *s = NULL, *s1 = NULL;
00441 char *newfn = NULL, *newpath = NULL;
00442 int ok = 0, only_first, skip_this;
00443 FILE *fdb_old, *fdb_new;
00444 filedb_entry *fdbe_old, *fdbe_new;
00445 long where;
00446
00447 BADARGS(3, 3, " oldfilepath newfilepath");
00448
00449 malloc_strcpy(fn, argv[1]);
00450 p = strrchr(fn, '/');
00451 if (p != NULL) {
00452 *p = 0;
00453 malloc_strcpy(s, fn);
00454 strcpy(fn, p + 1);
00455 if (!resolve_dir("/", s, &oldpath, -1)) {
00456
00457 Tcl_AppendResult(irp, "-1", NULL);
00458 my_free(fn);
00459 my_free(oldpath);
00460 return TCL_OK;
00461 }
00462 my_free(s);
00463 } else
00464 malloc_strcpy(oldpath, "/");
00465 malloc_strcpy(s, argv[2]);
00466 if (!resolve_dir("/", s, &newpath, -1)) {
00467
00468 p = strrchr(s, '/');
00469 if (!p) {
00470 malloc_strcpy(newfn, s);
00471 s[0] = 0;
00472 } else {
00473 *p = 0;
00474 malloc_strcpy(newfn, p + 1);
00475 }
00476 my_free(newpath);
00477 if (!resolve_dir("/", s, &newpath, -1)) {
00478 Tcl_AppendResult(irp, "-2", NULL);
00479 my_free(newpath);
00480 my_free(s);
00481 my_free(newfn);
00482 return TCL_OK;
00483 }
00484 } else
00485 malloc_strcpy(newfn, "");
00486 my_free(s);
00487
00488
00489 if ((!strcmp(oldpath, newpath)) && (!newfn[0] || !strcmp(newfn, fn))) {
00490 my_free(newfn);
00491 my_free(fn);
00492 my_free(oldpath);
00493 my_free(newpath);
00494 Tcl_AppendResult(irp, "-3", NULL);
00495 return TCL_OK;
00496 }
00497
00498 if ((strchr(fn, '?') || strchr(fn, '*')) && newfn[0])
00499 only_first = 1;
00500 else
00501 only_first = 0;
00502
00503 fdb_old = filedb_open(oldpath, 0);
00504 if (!strcmp(oldpath, newpath))
00505 fdb_new = fdb_old;
00506 else
00507 fdb_new = filedb_open(newpath, 0);
00508 if (!fdb_old || !fdb_new) {
00509 my_free(newfn);
00510 my_free(fn);
00511 my_free(oldpath);
00512 my_free(newpath);
00513 if (fdb_old)
00514 filedb_close(fdb_old);
00515 else if (fdb_new)
00516 filedb_close(fdb_new);
00517 Tcl_AppendResult(irp, "-5", NULL);
00518 return -1;
00519 }
00520
00521 filedb_readtop(fdb_old, NULL);
00522 fdbe_old = filedb_matchfile(fdb_old, ftell(fdb_old), fn);
00523 if (!fdbe_old) {
00524 my_free(newfn);
00525 my_free(fn);
00526 my_free(oldpath);
00527 my_free(newpath);
00528 if (fdb_new != fdb_old)
00529 filedb_close(fdb_new);
00530 filedb_close(fdb_old);
00531 Tcl_AppendResult(irp, "-4", NULL);
00532 return -2;
00533 }
00534 while (fdbe_old) {
00535 where = ftell(fdb_old);
00536 skip_this = 0;
00537 if (!(fdbe_old->stat & (FILE_HIDDEN | FILE_DIR))) {
00538 s = nmalloc(strlen(dccdir) + strlen(oldpath)
00539 + strlen(fdbe_old->filename) + 2);
00540 s1 = nmalloc(strlen(dccdir) + strlen(newpath)
00541 + strlen(newfn[0] ? newfn : fdbe_old->filename) + 2);
00542 sprintf(s, "%s%s%s%s", dccdir, oldpath,
00543 oldpath[0] ? "/" : "", fdbe_old->filename);
00544 sprintf(s1, "%s%s%s%s", dccdir, newpath,
00545 newpath[0] ? "/" : "", newfn[0] ? newfn : fdbe_old->filename);
00546 if (!strcmp(s, s1)) {
00547 Tcl_AppendResult(irp, "-3", NULL);
00548 skip_this = 1;
00549 }
00550
00551 filedb_readtop(fdb_new, NULL);
00552 fdbe_new = filedb_matchfile(fdb_new, ftell(fdb_new),
00553 newfn[0] ? newfn : fdbe_old->filename);
00554 if (fdbe_new) {
00555
00556
00557
00558
00559 if (fdbe_new->stat & FILE_DIR)
00560 skip_this = 1;
00561 else
00562 filedb_delfile(fdb_new, fdbe_new->pos);
00563 free_fdbe(&fdbe_new);
00564 }
00565 if (!skip_this) {
00566 if ((fdbe_old->sharelink) ||
00567 ((copy ? copyfile(s, s1) : movefile(s, s1)) == 0)) {
00568
00569 ok++;
00570 fdbe_new = malloc_fdbe();
00571 fdbe_new->stat = fdbe_old->stat;
00572
00573
00574
00575 malloc_strcpy(fdbe_new->flags_req, fdbe_old->flags_req);
00576 malloc_strcpy(fdbe_new->chan, fdbe_old->chan);
00577 malloc_strcpy(fdbe_new->filename, fdbe_old->filename);
00578 malloc_strcpy(fdbe_new->desc, fdbe_old->desc);
00579 if (newfn[0])
00580 malloc_strcpy(fdbe_new->filename, newfn);
00581 malloc_strcpy(fdbe_new->uploader, fdbe_old->uploader);
00582 fdbe_new->uploaded = fdbe_old->uploaded;
00583 fdbe_new->size = fdbe_old->size;
00584 fdbe_new->gots = fdbe_old->gots;
00585 malloc_strcpy(fdbe_new->sharelink, fdbe_old->sharelink);
00586 filedb_addfile(fdb_new, fdbe_new);
00587 if (!copy)
00588 filedb_delfile(fdb_old, fdbe_old->pos);
00589 free_fdbe(&fdbe_new);
00590 }
00591 }
00592 my_free(s);
00593 my_free(s1);
00594 }
00595 free_fdbe(&fdbe_old);
00596 fdbe_old = filedb_matchfile(fdb_old, where, fn);
00597 if (ok && only_first) {
00598 free_fdbe(&fdbe_old);
00599 }
00600 }
00601 if (fdb_old != fdb_new)
00602 filedb_close(fdb_new);
00603 filedb_close(fdb_old);
00604 if (!ok)
00605 Tcl_AppendResult(irp, "-4", NULL);
00606 else {
00607 char x[30];
00608
00609 sprintf(x, "%d", ok);
00610 Tcl_AppendResult(irp, x, NULL);
00611 }
00612 my_free(newfn);
00613 my_free(fn);
00614 my_free(oldpath);
00615 my_free(newpath);
00616 return TCL_OK;
00617 }
00618
00619 static int tcl_mv STDVAR
00620 {
00621 return tcl_mv_cp(irp, argc, argv, 0);
00622 }
00623
00624 static int tcl_cp STDVAR
00625 {
00626 return tcl_mv_cp(irp, argc, argv, 1);
00627 }
00628
00629 static int tcl_fileresend_send(ClientData cd, Tcl_Interp *irp, int argc,
00630 char *argv[], int resend)
00631 {
00632 int i, idx;
00633 char s[21];
00634
00635 BADARGS(3, 4, " idx filename ?nick?");
00636
00637 i = atoi(argv[1]);
00638 idx = findanyidx(i);
00639 if (idx < 0 || dcc[idx].type != &DCC_FILES) {
00640 Tcl_AppendResult(irp, "invalid idx", NULL);
00641 return TCL_ERROR;
00642 }
00643 if (argc == 4)
00644 i = files_reget(idx, argv[2], argv[3], resend);
00645 else
00646 i = files_reget(idx, argv[2], "", resend);
00647 sprintf(s, "%d", i);
00648 Tcl_AppendResult(irp, s, NULL);
00649 return TCL_OK;
00650 }
00651
00652 static int tcl_fileresend STDVAR
00653 {
00654 return tcl_fileresend_send(cd, irp, argc, argv, 1);
00655 }
00656
00657 static int tcl_filesend STDVAR
00658 {
00659 return tcl_fileresend_send(cd, irp, argc, argv, 0);
00660 }
00661
00662 static tcl_cmds mytcls[] = {
00663 {"getdesc", tcl_getdesc},
00664 {"getowner", tcl_getowner},
00665 {"setdesc", tcl_setdesc},
00666 {"setowner", tcl_setowner},
00667 {"getgots", tcl_getgots},
00668 {"getpwd", tcl_getpwd},
00669 {"setpwd", tcl_setpwd},
00670 {"getlink", tcl_getlink},
00671 {"setlink", tcl_setlink},
00672 {"getfiles", tcl_getfiles},
00673 {"getdirs", tcl_getdirs},
00674 {"hide", tcl_hide},
00675 {"unhide", tcl_unhide},
00676 {"share", tcl_share},
00677 {"unshare", tcl_unshare},
00678 {"filesend", tcl_filesend},
00679 {"fileresend", tcl_fileresend},
00680 {"mkdir", tcl_mkdir},
00681 {"rmdir", tcl_rmdir},
00682 {"cp", tcl_cp},
00683 {"mv", tcl_mv},
00684 {"getflags", tcl_getflags},
00685 {"setflags", tcl_setflags},
00686 {NULL, NULL}
00687 };