1 /*
2    mime.c
3 
4    This file is part of GNU Anubis.
5    Copyright (C) 2001-2014 The Anubis Team.
6 
7    GNU Anubis is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by the
9    Free Software Foundation; either version 3 of the License, or (at your
10    option) any later version.
11 
12    GNU Anubis is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License along
18    with GNU Anubis.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "headers.h"
22 #include "extern.h"
23 
24 /*
25   FIXME: Will add here further MIME support (see TODO file).
26 */
27 
28 struct append_closure
29 {
30   const char *prefix;
31   const char *filename;
32 };
33 
34 static int
_append_proc(char ** output,char * input,void * param)35 _append_proc (char **output, char *input, void *param)
36 {
37   struct append_closure *clos = param;
38   FILE *fptxt;
39   char buf[LINEBUFFER + 1];
40   size_t nbytes;
41   size_t nlines = 0;
42   char *p;
43 
44   fptxt = fopen (clos->filename, "r");
45   if (fptxt == 0)
46     {
47       anubis_error (0, errno, "%s", clos->filename);
48       return -1;
49     }
50   while (fgets (buf, LINEBUFFER, fptxt) != 0)
51     nlines++;
52 
53   fseek (fptxt, 0L, SEEK_END);
54   clearerr (fptxt);
55   nbytes = ftell (fptxt);
56   rewind (fptxt);
57 
58   nbytes = strlen (input)
59             + (clos->prefix ? strlen (clos->prefix) : 0) + nbytes + nlines + 1;
60 
61   input = xrealloc (input, nbytes);
62   *output = input;
63   p = input + strlen (input);
64   if (clos->prefix)
65     {
66       strcpy (p, clos->prefix);
67       p += strlen (clos->prefix);
68     }
69   while (fgets (buf, LINEBUFFER - 1, fptxt) != 0)
70     {
71       strcpy (p, buf);
72       p += strlen (buf);
73     }
74   *p = 0;
75   return 0;
76 }
77 
78 void
message_append_text_file(MESSAGE msg,char * filename,char * prefix)79 message_append_text_file (MESSAGE msg, char *filename, char *prefix)
80 {
81   struct append_closure clos;
82   clos.filename = filename;
83   clos.prefix = prefix;
84   message_proc_body (msg, _append_proc, &clos);
85 }
86 
87 void
message_append_signature_file(MESSAGE msg)88 message_append_signature_file (MESSAGE msg)
89 {
90   char homedir[MAXPATHLEN + 1];
91   char signature_file[] = DEFAULT_SIGFILE;
92   char *signature_path;
93   size_t n;
94 
95   get_homedir (session.clientname, homedir, sizeof (homedir));
96 
97   n = strlen (homedir) + strlen (signature_file) + 2;
98   signature_path = xmalloc (n);
99   snprintf (signature_path, n - 1, "%s/%s", homedir, signature_file);
100 
101   message_append_text_file (msg, signature_path, "-- \n");
102   free (signature_path);
103   return;
104 }
105 
106 /* EOF */
107