aboutsummaryrefslogtreecommitdiff
path: root/src/mail.c
blob: 81bf08578aa5d43aeb0ce385e7d623b5ad1c47d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* wydawca - FTP release synchronization daemon
   Copyright (C) 2007 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"
#include <mail.h>

int mailer_opened;
mu_mailer_t mailer;
mu_address_t admin_address;
mu_address_t from_address;
unsigned long mail_admin_mask;
char *admin_stat_message_template;
unsigned long owner_notification_flags;
char *user_message_template[MAX_EVENT];

void
mail_init ()
{
  if (!mailer)
    {
      int rc;
      if ((rc = mu_mailer_create (&mailer, NULL)))
	{
	  const char *url = NULL;
	  mu_mailer_get_url_default (&url);
	  logmsg (LOG_ERR, "cannot create default mailer `%s': %s",
		  url, mu_strerror (rc));
	}
    }
}

void
mail_send_message (mu_address_t rcpt, const char *text)
{
  int rc;
  mu_message_t msg;
  mu_stream_t stream = NULL;
  mu_header_t hdr;
  int mailer_flags = 0;
  static char *x_mailer = "wydawca (" PACKAGE_STRING ")";
  size_t size;
  char *buf;

  mu_message_create (&msg, NULL);

  mu_message_get_stream (msg, &stream);
  mu_stream_write (stream, text, strlen (text), 0, NULL);
  mu_message_get_header (msg, &hdr);
  mu_header_append (hdr, "X-Mailer", x_mailer);

  mu_address_to_string (rcpt, NULL, 0, &size);
  buf = xmalloc (size + 1);
  mu_address_to_string (rcpt, buf, size + 1, NULL);

  mu_header_set_value (hdr, "To", buf, 1);
  free (buf);
  
  if (debug_level > 1)
    {
      mu_debug_t debug;
      mu_mailer_get_debug (mailer, &debug);
      mu_debug_set_level (debug, MU_DEBUG_TRACE | MU_DEBUG_PROT);
      if (debug_level > 2)
	mailer_flags = MAILER_FLAG_DEBUG_DATA;
    }

  if (!mailer_opened)
    {
      if ((rc = mu_mailer_open (mailer, mailer_flags)))
	{
	  const char *url = NULL;
	  mu_mailer_get_url_default (&url);
	  logmsg (LOG_CRIT, "opening mailer `%s' failed: %s",
		  url, mu_strerror (rc));
	  return;
	}
      mailer_opened = 1;
    }
  
  rc = mu_mailer_send_message (mailer, msg, from_address, rcpt);
  if (rc)
    logmsg (LOG_CRIT, "cannot send message: %s", mu_strerror (rc));

  mu_message_destroy (&msg, mu_message_get_owner (msg));
}

void
mail_stats ()
{
  struct kw_expansion exp[MAX_STAT + 1];
  time_t t;
  char *text;
  
  if (!stat_mask_p (mail_admin_mask) || !mailer)
    return;

  if (debug_level)
    {
      size_t size;
      char *buf;
      mu_address_to_string (admin_address, NULL, 0, &size);
      buf = xmalloc (size + 1);
      mu_address_to_string (admin_address, buf, size + 1, NULL);
      logmsg (LOG_DEBUG, "Sending stats to %s", buf);
      free (buf);
    }
  
  if (dry_run_mode)
    return;

  time (&t);
  exp[0].kw = "date";
  exp[0].value = xstrdup (ctime (&t));
  exp[0].value [strlen (exp[0].value) - 1] = 0;
  exp[0].expand = NULL;
  
  make_stat_expansion (exp + 1);

  text = expand_param (admin_stat_message_template,
		       exp, sizeof (exp) / sizeof (exp[0]), NULL);

  mail_send_message (admin_address, text);

  free (text);
  free_kwexp (exp, sizeof (exp) / sizeof (exp[0]));
}

void
mail_finish ()
{
  if (mailer_opened)
    mu_mailer_close (mailer);
}  

void
notify_owner (struct file_triplet *trp, enum notification_event ev)
{
  int rc;
  struct access_method *method = trp->dpair->project_owner_method;
  char *text;
  mu_address_t rcpt = NULL;
  unsigned nrows, ncols, i;
  struct kw_expansion kwexp[4];
  
  if (method->type == method_none)
    {
      logmsg (LOG_NOTICE,
	      "NOT notifying project admins: project-owner not configured in "
	      "[%s, %s]", trp->dpair->source_dir, trp->dpair->dest_dir);
      return;
    }

  if (method_init (method))
    {
      logmsg (LOG_ERR,
	      "failed to initialize project-owner method: "
	      "NOT notifying project admins");
      return;
    }

  make_default_kwexp (kwexp, NULL, trp->project);
  escape_kwexp (method, kwexp, NITEMS (kwexp));
  text = expand_param (method->param[1], kwexp, NITEMS (kwexp), NULL);

  rc = method_run (method, text);
  free (text);
  if (rc)
    {
      logmsg (LOG_ERR, "cannot obtain owner emails for %s",
	      trp->project);
      return;
    }
  
  nrows = method_num_rows (method);
  ncols = method_num_cols (method);

  for (i = 0; i < nrows; i++)
    {
      mu_address_t addr;
      const char *str = method_result (method, i, 0);
      if (mu_address_create (&addr, str))
	continue;
      if (ncols > 0)
	{
	  str = method_result (method, i, 1);
	  if (str)
	    mu_address_set_personal (addr, 1, str);
	}
      mu_address_union (&rcpt, addr);
      mu_address_destroy (&addr);
    }

  if (debug_level)
    {
      size_t size;
      char *buf;
      mu_address_to_string (rcpt, NULL, 0, &size);
      buf = xmalloc (size + 1);
      mu_address_to_string (rcpt, buf, size + 1, NULL);
      logmsg (LOG_DEBUG, "Notifying admins of %s: %s", trp->project,
	      buf);
      free (buf);
    }

  if (!dry_run_mode)
    {
      text = triplet_expand_param (user_message_template[ev], trp);
      mail_send_message (rcpt, text);
      free (text);
    }
  
  mu_address_destroy (&rcpt);
}

void
notify (struct file_triplet *trp, enum notification_event ev)
{
  if (owner_notification_flags & STAT_MASK (ev))
    notify_owner (trp, ev);
  /* FIXME */
}

Return to:

Send suggestions and report system problems to the System administrator.