1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16 
17 #include <config.h>
18 #include <stdlib.h>
19 #include "daemon.h"
20 #include "misc/pwentry.h"
21 #include "misc/lookup.h"
22 #include "misc/utoa.h"
23 #include "misc/stat_fns.h"
24 #include "ac/dirent.h"
25 
26 struct count_size
27 {
28   unsigned long count;
29   unsigned long size;
count_sizecount_size30   count_size() : count(0), size(0) { }
31 };
32 
33 struct stats
34 {
35   count_size unseen_new;
36   count_size unseen;
37   count_size seen;
38 
39   mystring rep() const;
40 };
41 
rep() const42 mystring stats::rep() const
43 {
44   return utoa(unseen_new.count) + mystring::NUL +
45     utoa(unseen_new.size)  + mystring::NUL +
46     utoa(unseen.count) + mystring::NUL +
47     utoa(unseen.size) + mystring::NUL +
48     utoa(seen.count) + mystring::NUL +
49     utoa(seen.size) + mystring::NUL;
50 }
51 
52 class statdir
53 {
54   mystring path;
55   DIR* dir;
56   struct dirent* curr;
57   struct stat statbuf;
58 public:
59   statdir(const mystring& dirname);
~statdir()60   ~statdir() { close(); }
61   void close();
operator void*() const62   operator void*() const { return (void*)dir; }
operator !() const63   bool operator!() const { return !dir; }
operator ->() const64   const struct stat* operator->() const { return &statbuf; }
currname() const65   const char* currname() const { return curr->d_name; }
operator ++()66   void operator++() { advance(); }
67   void advance();
68 };
69 
statdir(const mystring & dirname)70 statdir::statdir(const mystring& dirname)
71   : path(dirname),
72     dir(opendir(dirname.c_str()))
73 {
74   advance();
75 }
76 
close()77 void statdir::close()
78 {
79   if(dir)
80     closedir(dir);
81   dir = NULL;
82 }
83 
advance()84 void statdir::advance()
85 {
86   if(dir) {
87     while((curr = readdir(dir)) != 0) {
88       if(curr->d_name[0] == '.')
89 	continue;
90       break;
91     }
92   }
93   if(!curr)
94     close();
95   else {
96     mystring fullpath = path + "/" + curr->d_name;
97     if(stat(fullpath.c_str(), &statbuf) == -1)
98       close();
99   }
100 }
101 
stat_new_dir(const mystring & basename,stats & stats)102 bool stat_new_dir(const mystring& basename, stats& stats)
103 {
104   statdir dir(basename + "/new");
105   if(!dir)
106     return false;
107   while(dir) {
108     if(S_ISREG(dir->st_mode)) {
109       ++stats.unseen_new.count;
110       stats.unseen_new.size += dir->st_blocks * 512;
111     }
112     ++dir;
113   }
114   return true;
115 }
116 
stat_cur_dir(const mystring & basename,stats & stats)117 bool stat_cur_dir(const mystring& basename, stats& stats)
118 {
119   statdir dir(basename + "/cur");
120   if(!dir)
121     return false;
122   while(dir) {
123     if(S_ISREG(dir->st_mode)) {
124       count_size* stat = &stats.unseen;
125       const char* colon = strchr(dir.currname(), ':');
126       if(colon) {
127 	++colon;
128 	if(*colon++ == '2' && *colon++ == ',' && !strchr(colon, 'S'))
129 	  stat = &stats.seen;
130       }
131       ++stat->count;
132       stat->size += dir->st_blocks * 512;
133     }
134     ++dir;
135   }
136   return true;
137 }
138 
stat_dir(const mystring & basename,stats & stats)139 bool stat_dir(const mystring& basename, stats& stats)
140 {
141   return stat_new_dir(basename, stats) &&
142     stat_cur_dir(basename, stats);
143 }
144 
CMD(stat)145 CMD(stat)
146   // Usage: stat baseuser-virtuser pass
147 {
148   mystring user = args[0];
149   mystring pass = args[1];
150 
151   pwentry* pw;
152   vpwentry* vpw;
153 
154   OK_RESPONSE(lookup_and_validate(user, pw, vpw, pass, true, false));
155 
156   if(!vpw->has_mailbox)
157     RETURN(err, "User is alias");
158 
159   mystring dirname = pw->home + "/" + vpw->directory;
160   stats stats;
161   if(!stat_dir(dirname, stats))
162     RETURN(err, "Failed to stat maildir");
163 
164   RETURN(ok, mystring::NUL + stats.rep());
165 }
166