cgi/listvdomain.cc
changeset 0 6f7a81934006
child 2 b3afb9f1e801
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     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 "cgi/cgi-base.h"
       
    20 #include "misc/server.h"
       
    21 #include "vpwentry/vpwentry.h"
       
    22 #include "fdbuf/fdbuf.h"
       
    23 
       
    24 static mystring domain;
       
    25 static mystring userlink;
       
    26 
       
    27 static mystring cell_pre;
       
    28 static mystring cell_post;
       
    29 
       
    30 static mystring p_pre;
       
    31 
       
    32 static mystring table_align;
       
    33 
       
    34 mystring do_subst(mystring link, const mystring& ref)
       
    35 {
       
    36   int pos;
       
    37   int start = 0;
       
    38   unsigned advance = ref.length();
       
    39   
       
    40   while((pos = link.find_first('%', start)) >= 0) {
       
    41     link = link.left(pos) + ref + link.right(pos+1);
       
    42     start = pos + advance;
       
    43   }
       
    44   return link;
       
    45 }
       
    46 
       
    47 struct user
       
    48 {
       
    49   mystring name;
       
    50   mystring code;
       
    51   user* next;
       
    52   user(const mystring& n, const mystring& c);
       
    53 };
       
    54 
       
    55 user::user(const mystring& n, const mystring& c)
       
    56   : name(n), code(c), next(0)
       
    57 {
       
    58 }
       
    59 
       
    60 void show_user(const user* user)
       
    61 {
       
    62   vpwentry vpw;
       
    63   vpw.from_record(user->name, user->code);
       
    64   
       
    65   mystring link;
       
    66   mystring elink;
       
    67 
       
    68   if(!!userlink) {
       
    69     link = "<a href=\"" + do_subst(userlink, vpw.name) + "\">";
       
    70     elink = "</a>";
       
    71   }
       
    72   
       
    73   fout << "<tr>"
       
    74        << cell_pre << link << vpw.name << elink << cell_post
       
    75        << cell_pre << vpw.mailbox << cell_post;
       
    76   for(mystring_iter iter = vpw.forwards; iter; ++iter)
       
    77     fout << cell_pre << *iter << cell_post;
       
    78   fout << "</tr>\n";
       
    79 }
       
    80 
       
    81 mystring load_users(int fd, unsigned& count, user**& array)
       
    82 {
       
    83   user* tail = 0;
       
    84   user* head = 0;
       
    85   for(count = 0; ; count++) {
       
    86     response r(response::read(fd));
       
    87     if(!r)
       
    88       return r.message();
       
    89     if(!r.msg)
       
    90       break;
       
    91     mystring name(r.msg.c_str());
       
    92     mystring code(r.msg.c_str() + name.length()+1,
       
    93 		  r.msg.length() - name.length()-1);
       
    94     user* node = new user(name, code);
       
    95     if(!head)
       
    96       head = node;
       
    97     else
       
    98       tail->next = node;
       
    99     tail = node;
       
   100   }
       
   101   if(count) {
       
   102     array = new user*[count];
       
   103     unsigned i = 0;
       
   104     for(user* node = head; node; node = node->next)
       
   105       array[i++] = node;
       
   106   }
       
   107   return "";
       
   108 }
       
   109 
       
   110 static int user_cmp(const void* ptra, const void* ptrb)
       
   111 {
       
   112   const user* a = *(const user**)ptra;
       
   113   const user* b = *(const user**)ptrb;
       
   114   int res = a->name != b->name;
       
   115   return res;
       
   116 }
       
   117 
       
   118 void show_domain(int fd)
       
   119 {
       
   120   unsigned count = 0;
       
   121   user** users;
       
   122   mystring msg = load_users(fd, count, users);
       
   123   if(count == 0) {
       
   124     fout << p_pre << "This domain contains no users.</p>\n";
       
   125     return;
       
   126   }
       
   127 
       
   128   if(!!msg) {
       
   129     fout << p_pre << "<samp>" << msg << "</samp></p>\n";
       
   130     return;
       
   131   }
       
   132 
       
   133   qsort(users, count, sizeof users[0], user_cmp);
       
   134   
       
   135   fout << p_pre << "This domain contains " << count << " users:</p>\n";
       
   136   fout << "<table border=1 align=\"" << table_align << "\">\n"
       
   137     "<tr><td><u>Username</u></td>"
       
   138     "<td><u>Mailbox</u></td>"
       
   139     "<td><u>Forwards</u></td></tr>\n";
       
   140   for(unsigned i = 0; i < count; i++)
       
   141     show_user(users[i]);
       
   142   fout << "</table>\n";
       
   143 }
       
   144 
       
   145 void setup_format(const CGIArgs& args)
       
   146 {
       
   147   p_pre = "<p align=\"" + args.get("p-align", "left") + "\">";
       
   148 
       
   149   cell_pre = args["cell-pre"];
       
   150   cell_post = args["cell-post"];
       
   151   
       
   152   cell_pre = "<td align=\"" + args.get("cell-align", "left") + "\">"
       
   153     + cell_pre;
       
   154   cell_post = cell_post + "</td>";
       
   155 
       
   156   userlink = args["userlink"];
       
   157 
       
   158   table_align = args.get("table-align", "left");
       
   159 }
       
   160 
       
   161 CGI_MAIN 
       
   162 {
       
   163   mystring body_flags = args["body-flags"];
       
   164   mystring title_pre = args.get("title-pre", "<h1>");
       
   165   mystring title_post = args.get("title-post", "</h1>");
       
   166 
       
   167   setup_format(args);
       
   168 
       
   169   domain = vdomain;
       
   170   server_call call("listdomain", vdomain, password);
       
   171   response resp = call.call();
       
   172   if(!resp)
       
   173     error(resp.msg);
       
   174   else {
       
   175     content_type("text/html");
       
   176     fout << "<html>"
       
   177       "<head>"
       
   178       "<title>Listing of Virtual Domain '" << vdomain << "'</title>"
       
   179       "</head>\n"
       
   180       "<body " << body_flags << ">\n" <<
       
   181       p_pre << title_pre << "Listing of Virtual Domain "
       
   182       "'" << vdomain << "'" << title_post << "</p>"
       
   183       "<hr>\n";
       
   184     show_domain(call.socket());
       
   185     const mystring referer = getenv("HTTP_REFERER");
       
   186     if(!!referer)
       
   187       fout << p_pre << "<a href=\"" << referer << "\">Back</a></p>\n";
       
   188     fout << "</body>\n"
       
   189       "</html>\n";
       
   190   }
       
   191 }