Add a cheat sheet for inoclam developers.
[inoclam.git] / smtp.cxx
blob37b4860fead53f1e2882c6498bb8947524e40468
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 as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Contributor(s):
20 * Tom Cort <tom.cort@state.vt.us>
21 * Matt Gagne <matt.gagne@state.vt.us>
24 #include <exception>
25 #include <jwsmtp/jwsmtp.h>
26 #include <libdaemon/dlog.h>
27 #include <stdlib.h>
28 #include <string>
30 #include "smtp.hxx"
32 /**
33 * Returns a banner to go at the bottom of e-mails. Remember to delete it when done.
34 * @return footer string
36 std::string * smtp_get_banner()
39 * " (o_ Powered by: inoclam v1.0 (Bender)\n"
40 * " //\ Homepage: http://www.inoclam.org/\n"
41 * " V_/_ Author: Vermont Department of Taxes\n"
44 std::string * banner;
45 banner = new std::string();
46 banner->append(" (o_ Powered by inoclam v");
48 std::string * version;
49 version = new std::string(VERSION);
50 banner->append(version->c_str());
51 delete version;
53 banner->append(" (");
55 std::string * codename;
56 codename = new std::string(CODENAME);
57 banner->append(codename->c_str());
58 delete codename;
60 banner->append(")\n");
62 banner->append(" //\\ Developed by the Vermont Department of Taxes\n");
63 banner->append(" V_/_ Project Homepage: http://www.inoclam.org/\n");
65 return banner;
68 /**
69 * Returns an RFC 2822 Compliant Date String
70 * @return timestamp
72 std::string * smtp_get_timestamp()
74 char *timestamp;
75 const struct tm *tm;
76 time_t now;
78 now = time(NULL);
79 tm = localtime(&now);
81 timestamp = (char *) malloc(sizeof(char) * 41);
82 if (!timestamp) {
83 daemon_log(LOG_ERR, "malloc() failed!");
84 return NULL;
87 memset(timestamp, '\0', 41); /* RFC 2822 Date */
88 strftime(timestamp, 40, "%a, %d %b %Y %H:%M:%S", tm);
90 std::string * tstamp;
91 tstamp = new std::string(timestamp);
93 if (timestamp) {
94 free(timestamp);
95 timestamp = NULL;
98 return tstamp;
102 * Sends an E-Mail.
104 void smtp_send(char *subject, std::string * body, config * conf)
106 char *smtp_host = conf->getHost();
107 int smtp_port = conf->getPort();
108 char *smtp_to = conf->getTo();
109 char *smtp_from = conf->getFrom();
110 const char *smtp_body = body->c_str();
112 if (smtp_host == NULL || smtp_to == NULL || smtp_from == NULL || smtp_port < 1 || smtp_port > 65535 || subject == NULL || smtp_body == NULL) {
113 daemon_log(LOG_INFO, "Incomplete Configuration or Message -- Not Sending E-Mail");
114 return;
117 /* Why are there no C libraries for sending mail that are this simple? */
118 jwsmtp::mailer * mail;
119 mail = new jwsmtp::mailer(smtp_to, smtp_from, subject, smtp_body, smtp_host, smtp_port, false);
120 mail->send();
121 delete mail;