Clarify License => GNU General Public License version 2.
[inoclam.git] / src / smtp.cxx
blob7deae293dd49c6289e2c4f9f8c5e8aec43be5429
1 /*
2 * inoclam - Inotify+ClamAV virus scanner
3 * Copyright (C) 2007 Vermont Department of Taxes
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Contributor(s):
19 * Tom Cort <tom.cort@state.vt.us>
20 * Matt Gagne <matt.gagne@state.vt.us>
23 #include <exception>
24 #include <jwsmtp/jwsmtp.h>
25 #include <libdaemon/dlog.h>
26 #include <stdlib.h>
27 #include <string>
29 #include "smtp.hxx"
31 /**
32 * Returns a banner to go at the bottom of e-mails. Remember to delete it when done.
33 * @return footer string
35 std::string * smtp_get_banner()
38 * " (o_ Powered by: inoclam v1.0 (Bender)\n"
39 * " //\ Homepage: http://www.inoclam.org/\n"
40 * " V_/_ Author: Vermont Department of Taxes\n"
43 std::string * banner;
44 banner = new std::string();
45 banner->append(" (o_ Powered by inoclam v");
47 std::string * version;
48 version = new std::string(VERSION);
49 banner->append(version->c_str());
50 delete version;
52 banner->append(" (");
54 std::string * codename;
55 codename = new std::string(CODENAME);
56 banner->append(codename->c_str());
57 delete codename;
59 banner->append(")\n");
61 banner->append(" //\\ Developed by the Vermont Department of Taxes\n");
62 banner->append(" V_/_ Project Homepage: http://www.inoclam.org/\n");
64 return banner;
67 /**
68 * Returns an RFC 2822 Compliant Date String
69 * @return timestamp
71 std::string * smtp_get_timestamp()
73 char *timestamp;
74 const struct tm *tm;
75 time_t now;
77 now = time(NULL);
78 tm = localtime(&now);
80 timestamp = (char *) malloc(sizeof(char) * 41);
81 if (!timestamp) {
82 daemon_log(LOG_ERR, "malloc() failed!");
83 return NULL;
86 memset(timestamp, '\0', 41); /* RFC 2822 Date */
87 strftime(timestamp, 40, "%a, %d %b %Y %H:%M:%S", tm);
89 std::string * tstamp;
90 tstamp = new std::string(timestamp);
92 if (timestamp) {
93 free(timestamp);
94 timestamp = NULL;
97 return tstamp;
101 * Sends an E-Mail.
103 void smtp_send(char *subject, std::string * body, config * conf)
105 char *smtp_host = conf->getHost();
106 int smtp_port = conf->getPort();
107 char *smtp_from = conf->getFrom();
108 const char *smtp_body = body->c_str();
110 if (smtp_host == NULL || smtp_from == NULL || smtp_port < 1 || smtp_port > 65535 || subject == NULL || smtp_body == NULL) {
111 daemon_log(LOG_INFO, "Incomplete Configuration or Message -- Not Sending E-Mail");
112 return;
115 for (std::list < char *>::iterator itr = conf->getTo()->begin(); itr != conf->getTo()->end(); ++itr) {
116 char *smtp_to;
117 smtp_to = *itr;
119 daemon_log(LOG_INFO, "Mailing '%s' ", smtp_to);
121 /* Why are there no C libraries for sending mail that are this simple? */
122 jwsmtp::mailer * mail;
123 mail = new jwsmtp::mailer(smtp_to, smtp_from, subject, smtp_body, smtp_host, smtp_port, false);
124 mail->send();
125 delete mail;