From 83082f2e9c3454bf618823f8ab9e3bb5b20ecc59 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 19 Jan 2016 06:09:34 -0500 Subject: [PATCH] mail_utils/mail: Reformat & fix a few error handling problems. --- src/bin/mail_utils/mail.cpp | 137 ++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 68 deletions(-) diff --git a/src/bin/mail_utils/mail.cpp b/src/bin/mail_utils/mail.cpp index ff0ddf8ca6..f33bc07234 100644 --- a/src/bin/mail_utils/mail.cpp +++ b/src/bin/mail_utils/mail.cpp @@ -1,117 +1,118 @@ -// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -// -// Copyright (c) 2003, OpenBeOS -// -// This software is part of the OpenBeOS distribution and is covered -// by the OpenBeOS license. -// -// -// File: mail.cpp -// Author: Santiago (Jacques) Lema -// Description: sends an e-mail from the command-line -// Created : May 23, 2003 -// Modified by: Jérome Duval -// -// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ +/* + * Copyright 2003-2016, Haiku Inc. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Santiago (Jacques) Lema + * Jérôme Duval, jerome.duval@gmail.com + * Augustin Cavalier, + */ + -#include #include #include #include -#define APP_SIG "application/x-vnd.OBos-cmd-mail" +#include -int main( int argc, char* argv[] ) + +#define APP_SIG "application/x-vnd.Haiku-mail_utils-mail" + + +int main(int argc, char* argv[]) { BApplication mailApp(APP_SIG); // No arguments, show usage - if (argc==1) { - fprintf(stdout,"[OBOS-mail] Sorry: This program can only send mail, not read it.\n"); - fprintf(stdout,"usage: /bin/mail [-v] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n"); - fflush(stdout); - return B_OK; + if (argc < 2) { + fprintf(stdout,"This program can only send mail, not read it.\n"); + fprintf(stdout,"usage: %s [-v] [-s subject] [-c cc-addr] " + "[-b bcc-addr] to-addr ...\n", argv[0]); + fflush(stdout); + return 0; } - + char *subject = "No title"; - char *cc = ""; + char *cc = ""; char *bcc = ""; BString to = ""; BString body = ""; - bool verbose =false; - //parse arguments - for (int i=1; i \n",to.String()); - fprintf(stdout,"Cc:\t<%s> \n",cc); - fprintf(stdout,"Bcc:\t<%s> \n",bcc); - fprintf(stdout,"Subj:\t<%s> \n",subject); - fprintf(stdout,"Body:\t<%s> \n",body.String()); - fprintf(stdout,"\n"); + fprintf(stdout, "\n"); + fprintf(stdout, "To:\t<%s> \n", to.String()); + fprintf(stdout, "Cc:\t<%s> \n", cc); + fprintf(stdout, "Bcc:\t<%s> \n", bcc); + fprintf(stdout, "Subj:\t<%s> \n", subject); + fprintf(stdout, "Body:\t<%s> \n", body.String()); + fprintf(stdout, "\n"); } - - //check if valid recipients - if (strcmp(to.String(), "") == 0 - && strcmp(cc, "") == 0 - && strcmp(bcc, "") == 0) { - - fprintf(stdout, - "[Error]\nYou must specify at least one recipient in to,cc or bcc fields.\n"); - return B_ERROR; - } - - //read each line until we get a single dot "." on a line - char line[32768] = ""; - printf("Now type your message.\nType '.' alone on a line to send it.\n"); - do { + // Check if recipients are valid + if (strcmp(to.String(), "") == 0 && + strcmp(cc, "") == 0 && + strcmp(bcc, "") == 0) { + + fprintf(stdout, "[Error]: You must specify at least one recipient " + "in to, cc or bcc fields.\n"); + return -1; + } + + // Read each line until we get a single dot "." on a line + char line[32768] = ""; + + printf("Now type your message.\nType '.' alone on a line to send it.\n"); + do { gets(line); - + if (strcmp(line, ".") != 0) { body.Append(line).Append("\n"); - } - //fprintf(stdout,"Line: %s \n",line); + } + // fprintf(stdout,"Line: %s \n",line); } while (strcmp(line, ".") != 0); - + if (verbose) fprintf(stdout, "\nBody:\n%s\n", body.String()); if (verbose) fprintf(stdout, "\nSending E-mail...\n"); - - fflush(stdout); - + BMailMessage mail; mail.AddHeaderField(B_MAIL_TO, to.String()); mail.AddHeaderField(B_MAIL_CC, cc); mail.AddHeaderField(B_MAIL_BCC, bcc); - mail.AddHeaderField(B_MAIL_SUBJECT, subject); - mail.AddContent(body.String(),strlen(body.String())); - status_t result = mail.Send(); - - if (result==B_OK) + mail.AddHeaderField(B_MAIL_SUBJECT, subject); + mail.AddContent(body.String(), strlen(body.String())); + status_t result = mail.Send(); + + if (result == B_OK) { fprintf(stdout, "\nMessage was sent successfully.\n"); - + return 0; + } + + fprintf(stdout, "Message failed to send: %s", strerror(result)); + return result; }