📄 smtp.c
字号:
/* * libsms smtp - Implementation of the Simple Mail Transfer Protocol (RFC821) * * Authors: Michael Jochum <e9725005@stud3.tuwien.ac.at> * * TODO: do better logging of errors * * Fixes: * * For license terms, see the file COPYING in the project directory. */#include <string.h>#include <glib.h>#include <mail.h>#include <sock.h>#include <smtp.h>#include <misc.h>static gint smtp_ok(GSmsSocket * s, gchar ** message){ gchar buf[GSMS_SMTP_BUFSIZE]; gint res; gboolean error = FALSE; gboolean positive = FALSE; g_return_val_if_fail(s != NULL, -1); if (message != NULL) *message = NULL; while ((res = gsms_socket_read_line(s, buf, GSMS_SMTP_BUFSIZE)) >= 0) { gint n; gsms_str_strip_crlf(buf); n = strlen(buf); if (n < 4) { error = 1; break; } if (buf[0] == '1' || buf[0] == '2' || buf[0] == '3') { /* positiv reply */ positive = TRUE; } else if (buf[0] != '4' && buf[0] != '5') { error = TRUE; break; } if (buf[3] == ' ') { if (message != NULL) *message = g_strdup(buf + 4); break; } else if (buf[3] != '-') { /* error if not multilined */ error = TRUE; break; } } if (res < 0) { /* network error */ return -1; } if (!positive || error) return -1; return 0;}gint gsms_smtp_start(GSmsSocket * s, gchar * host){ gint res; gchar *smtperr; /* get greeting message */ if ((res = smtp_ok(s, &smtperr)) < 0) return -1; /* send HELO */ if (gsms_socket_printf(s, "HELO %s\r\n", host) < 0) return -1; if ((res = smtp_ok(s, &smtperr)) < 0) return -1; return 0;}gint gsms_smtp_send(GSmsSocket * s, GSmsMail * m){ gint res; gchar *smtperr; gchar *from = m->from; /* send MAIL FROM: */ if (strchr(from, '<') != NULL) res = gsms_socket_printf(s, "MAIL FROM: %s\r\n", from); else res = gsms_socket_printf(s, "MAIL FROM:<%s>\r\n", from); if (res < 0) return res; if ((res = smtp_ok(s, &smtperr)) < 0) { return res; } /* send RCPT TO: */ if (strchr(from, '<') != NULL) res = gsms_socket_printf(s, "RCPT TO: %s\r\n", m->to); else res = gsms_socket_printf(s, "RCPT TO:<%s>\r\n", m->to); if (res < 0) return res; if ((res = smtp_ok(s, &smtperr)) < 0) return res; /* send DATA */ if (gsms_socket_write_str(s, "DATA\r\n") < 0) return -1; if ((res = smtp_ok(s, &smtperr)) < 0) return res; /* send Subject */ if (gsms_socket_printf(s, "Subject: %s\r\n", m->subject) < 0) return -1; if (m->head != NULL) if (gsms_socket_write_str(s, m->head) < 0) return -1; /* now let's start with the body */ if (gsms_socket_write_str(s, "\r\n") < 0) return -1; if (m->body != NULL) { GString *body; gchar *str, *nextline = m->body; body = g_string_sized_new(1024); while (nextline != NULL) { str = gsms_str_get_line(nextline, &nextline); if (*str == '.') body = g_string_append_c(body, '.'); gsms_str_strip_crlf(str); body = g_string_append(body, str); body = g_string_append(body, "\r\n"); g_free(str); } if (gsms_socket_write_str(s, body->str) < 0) { g_string_free(body, TRUE); return -1; } g_string_free(body, TRUE); } /* end of data */ if (gsms_socket_write_str(s, ".\r\n") < 0) return -1; if ((res = smtp_ok(s, &smtperr)) < 0) return -1; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -