Commit | Line | Data |
---|---|---|
07dd1674 FT |
1 | /* |
2 | * Dolda Connect - Modular multiuser Direct Connect-style client | |
3 | * Copyright (C) 2007 Fredrik Tolf <fredrik@dolda2000.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | ||
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <unistd.h> | |
23 | #include <errno.h> | |
24 | #include <time.h> | |
25 | ||
26 | #ifdef HAVE_CONFIG_H | |
27 | #include <config.h> | |
28 | #endif | |
29 | #include "transfer.h" | |
30 | #include "module.h" | |
31 | #include "log.h" | |
32 | ||
33 | struct trdata { | |
34 | size_t startpos; | |
35 | }; | |
36 | ||
37 | static char *fn = NULL; | |
38 | ||
39 | void filelog(char *format, ...) | |
40 | { | |
41 | FILE *out; | |
42 | va_list args; | |
ce93a8cb | 43 | char *b, *t, *p; |
07dd1674 FT |
44 | time_t now; |
45 | ||
46 | if(fn == NULL) | |
47 | return; | |
48 | if((out = fopen(fn, "a")) == NULL) { | |
49 | flog(LOG_WARNING, "could not open reqstat log file `%s': %s", fn, strerror(errno)); | |
50 | return; | |
51 | } | |
52 | now = time(NULL); | |
53 | va_start(args, format); | |
54 | b = vsprintf2(format, args); | |
55 | va_end(args); | |
ce93a8cb FT |
56 | t = ctime(&now); |
57 | if((p = strchr(t, '\n')) != NULL) | |
58 | *p = 0; | |
713581e5 | 59 | fprintf(out, "%s: %s\n", t, b); |
07dd1674 FT |
60 | free(b); |
61 | fclose(out); | |
62 | } | |
63 | ||
64 | void request(struct transfer *transfer, struct trdata *data) | |
65 | { | |
66 | filelog("request %ls", transfer->path); | |
67 | } | |
68 | ||
69 | void start(struct transfer *transfer, struct trdata *data) | |
70 | { | |
ce93a8cb | 71 | filelog("start %ls at %zi", transfer->path, data->startpos); |
07dd1674 FT |
72 | } |
73 | ||
74 | void finish(struct transfer *transfer, struct trdata *data) | |
75 | { | |
ce93a8cb | 76 | filelog("finish %ls at %zi, total %zi", transfer->path, transfer->curpos, transfer->curpos - data->startpos); |
07dd1674 FT |
77 | } |
78 | ||
79 | static int chattr(struct transfer *transfer, wchar_t *attrib, struct trdata *data) | |
80 | { | |
81 | if(!wcscmp(attrib, L"state")) { | |
82 | if(transfer->state == TRNS_MAIN) | |
83 | data->startpos = transfer->curpos; | |
84 | start(transfer, data); | |
85 | } else if(!wcscmp(attrib, L"path")) { | |
86 | if(transfer->path[0] != L'/') { | |
87 | CBUNREG(transfer, trans_ac, data); | |
88 | CBUNREG(transfer, trans_destroy, data); | |
89 | free(data); | |
90 | } | |
91 | request(transfer, data); | |
92 | } | |
93 | return(0); | |
94 | } | |
95 | ||
96 | static int destroy(struct transfer *transfer, struct trdata *data) | |
97 | { | |
ce93a8cb FT |
98 | if(transfer->curpos > data->startpos) |
99 | finish(transfer, data); | |
07dd1674 FT |
100 | free(data); |
101 | return(0); | |
102 | } | |
103 | ||
104 | static int reg(struct transfer *transfer, void *uudata) | |
105 | { | |
106 | struct trdata *data; | |
107 | ||
108 | if(transfer->dir != TRNSD_UP) | |
109 | return(0); | |
110 | data = memset(smalloc(sizeof(*data)), 0, sizeof(*data)); | |
111 | CBREG(transfer, trans_ac, (int (*)(struct transfer *, wchar_t *, void *))chattr, NULL, data); | |
112 | CBREG(transfer, trans_destroy, (int (*)(struct transfer *, void *))destroy, NULL, data); | |
113 | return(0); | |
114 | } | |
115 | ||
116 | static int chfile(struct configvar *var, void *uudata) | |
117 | { | |
118 | if(fn != NULL) | |
119 | free(fn); | |
56c91062 | 120 | if(var->val.str[0] == L'\0') { |
07dd1674 FT |
121 | fn = NULL; |
122 | } else { | |
123 | if((fn = icwcstombs(var->val.str, NULL)) == NULL) | |
124 | flog(LOG_WARNING, "could not convert reqstat filename `%ls' into local charset: %s", var->val.str, strerror(errno)); | |
125 | } | |
126 | return(0); | |
127 | } | |
128 | ||
38b22d1a | 129 | static int init(int hup) |
07dd1674 FT |
130 | { |
131 | if(!hup) { | |
132 | GCBREG(newtransfercb, reg, NULL); | |
ca63c1a6 | 133 | chfile(confgetvar("reqstat", "file"), NULL); |
07dd1674 FT |
134 | CBREG(confgetvar("reqstat", "file"), conf_update, chfile, NULL, NULL); |
135 | } | |
38b22d1a | 136 | return(0); |
07dd1674 FT |
137 | } |
138 | ||
139 | static struct configvar myvars[] = { | |
140 | /** The name of a file to log upload request information to. If | |
141 | * unspecified, upload requests will not be logged. */ | |
142 | {CONF_VAR_STRING, "file", {.str = L""}}, | |
143 | {CONF_VAR_END} | |
144 | }; | |
145 | ||
146 | static struct module me = { | |
147 | .conf = { | |
148 | .vars = myvars | |
149 | }, | |
38b22d1a | 150 | .init = init, |
07dd1674 FT |
151 | .name = "reqstat" |
152 | }; | |
153 | ||
38b22d1a | 154 | MODULE(me) |