Remove obsolete $Cambridge$ CVS revision strings.
[exim.git] / src / src / routers / rf_get_munge_headers.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8 #include "../exim.h"
9 #include "rf_functions.h"
10
11
12 /*************************************************
13 *      Get additional headers for a router       *
14 *************************************************/
15
16 /* This function is called by both routers to sort out the additional headers
17 and header remove list for a particular address.
18
19 Arguments:
20   addr           the input address
21   rblock         the router instance
22   extra_headers  points to where to hang the header chain
23   remove_headers points to where to hang the remove list
24
25 Returns:         OK if no problem
26                  DEFER if expanding a string caused a deferment
27                    or a big disaster (e.g. expansion failure)
28 */
29
30 int
31 rf_get_munge_headers(address_item *addr, router_instance *rblock,
32   header_line **extra_headers, uschar **remove_headers)
33 {
34 /* Default is to retain existing headers */
35
36 *extra_headers = addr->p.extra_headers;
37
38 if (rblock->extra_headers != NULL)
39   {
40   header_line *h;
41   uschar *s = expand_string(rblock->extra_headers);
42
43   if (s == NULL)
44     {
45     if (!expand_string_forcedfail)
46       {
47       addr->message = string_sprintf("%s router failed to expand \"%s\": %s",
48         rblock->name, rblock->extra_headers, expand_string_message);
49       return DEFER;
50       }
51     }
52
53   /* Expand succeeded. Put extra header at the start of the chain because
54   further down it may point to headers from other routers, which may be
55   shared with other addresses. The output function outputs them in reverse
56   order. */
57
58   else
59     {
60     int slen = Ustrlen(s);
61     if (slen > 0)
62       {
63       h = store_get(sizeof(header_line));
64
65       /* We used to use string_sprintf() to add the newline if needed, but that
66       causes problems if the header line is exceedingly long (e.g. adding
67       something to a pathologically long line). So avoid it. */
68
69       if (s[slen-1] == '\n')
70         {
71         h->text = s;
72         }
73       else
74         {
75         h->text = store_get(slen+2);
76         memcpy(h->text, s, slen);
77         h->text[slen++] = '\n';
78         h->text[slen] = 0;
79         }
80
81       h->next = addr->p.extra_headers;
82       h->type = htype_other;
83       h->slen = slen;
84       *extra_headers = h;
85       }
86     }
87   }
88
89 /* Default is to retain existing removes */
90
91 *remove_headers = addr->p.remove_headers;
92
93 if (rblock->remove_headers != NULL)
94   {
95   uschar *s = expand_string(rblock->remove_headers);
96   if (s == NULL)
97     {
98     if (!expand_string_forcedfail)
99       {
100       addr->message = string_sprintf("%s router failed to expand \"%s\": %s",
101         rblock->name, rblock->remove_headers, expand_string_message);
102       return DEFER;
103       }
104     }
105   else if (*s != 0)
106     {
107     if (addr->p.remove_headers == NULL)
108       *remove_headers = s;
109     else
110       *remove_headers = string_sprintf("%s : %s", addr->p.remove_headers, s);
111     }
112   }
113
114 return OK;
115 }
116
117 /* End of rf_get_munge_headers.c */