Fixed a couple of presentation bugs in the request log.
[doldaconnect.git] / daemon / reqstat.c
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;
43     char *b, *t, *p;
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);
56     t = ctime(&now);
57     if((p = strchr(t, '\n')) != NULL)
58         *p = 0;
59     fprintf(out, "%s: %s\n", p, b);
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 {
71     filelog("start %ls at %zi", transfer->path, data->startpos);
72 }
73
74 void finish(struct transfer *transfer, struct trdata *data)
75 {
76     filelog("finish %ls at %zi, total %zi", transfer->path, transfer->curpos, transfer->curpos - data->startpos);
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 {
98     if(transfer->curpos > data->startpos)
99         finish(transfer, data);
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);
120     if(var->val.str[0] == L'0') {
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
129 static int init(int hup)
130 {
131     if(!hup) {
132         GCBREG(newtransfercb, reg, NULL);
133         CBREG(confgetvar("reqstat", "file"), conf_update, chfile, NULL, NULL);
134     }
135     return(0);
136 }
137
138 static struct configvar myvars[] = {
139     /** The name of a file to log upload request information to. If
140      * unspecified, upload requests will not be logged. */
141     {CONF_VAR_STRING, "file", {.str = L""}},
142     {CONF_VAR_END}
143 };
144
145 static struct module me = {
146     .conf = {
147         .vars = myvars
148     },
149     .init = init,
150     .name = "reqstat"
151 };
152
153 MODULE(me)