mail_utils/mail: Reformat & fix a few error handling problems.
This commit is contained in:
+47
-46
@@ -1,36 +1,35 @@
|
|||||||
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
|
/*
|
||||||
//
|
* Copyright 2003-2016, Haiku Inc. All rights reserved.
|
||||||
// Copyright (c) 2003, OpenBeOS
|
* Distributed under the terms of the MIT License.
|
||||||
//
|
*
|
||||||
// This software is part of the OpenBeOS distribution and is covered
|
* Authors:
|
||||||
// by the OpenBeOS license.
|
* Santiago (Jacques) Lema
|
||||||
//
|
* Jérôme Duval, [email protected]
|
||||||
//
|
* Augustin Cavalier, <waddlesplash>
|
||||||
// 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
|
|
||||||
//
|
|
||||||
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <Application.h>
|
#include <Application.h>
|
||||||
#include <String.h>
|
#include <String.h>
|
||||||
#include <E-mail.h>
|
#include <E-mail.h>
|
||||||
|
|
||||||
#define APP_SIG "application/x-vnd.OBos-cmd-mail"
|
#include <stdio.h>
|
||||||
|
|
||||||
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);
|
BApplication mailApp(APP_SIG);
|
||||||
|
|
||||||
// No arguments, show usage
|
// No arguments, show usage
|
||||||
if (argc==1) {
|
if (argc < 2) {
|
||||||
fprintf(stdout,"[OBOS-mail] Sorry: This program can only send mail, not read it.\n");
|
fprintf(stdout,"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");
|
fprintf(stdout,"usage: %s [-v] [-s subject] [-c cc-addr] "
|
||||||
|
"[-b bcc-addr] to-addr ...\n", argv[0]);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
return B_OK;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *subject = "No title";
|
char *subject = "No title";
|
||||||
@@ -40,9 +39,9 @@ int main( int argc, char* argv[] )
|
|||||||
BString body = "";
|
BString body = "";
|
||||||
|
|
||||||
bool verbose =false;
|
bool verbose =false;
|
||||||
//parse arguments
|
// Parse arguments
|
||||||
for (int i=1; i<argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
if (strcmp (argv[i], "-v") == 0)
|
if (strcmp(argv[i], "-v") == 0)
|
||||||
verbose = true;
|
verbose = true;
|
||||||
else if (strcmp(argv[i], "-s") == 0) {
|
else if (strcmp(argv[i], "-s") == 0) {
|
||||||
subject = argv[i+1];
|
subject = argv[i+1];
|
||||||
@@ -61,26 +60,26 @@ int main( int argc, char* argv[] )
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
fprintf(stdout,"\n");
|
fprintf(stdout, "\n");
|
||||||
fprintf(stdout,"To:\t<%s> \n",to.String());
|
fprintf(stdout, "To:\t<%s> \n", to.String());
|
||||||
fprintf(stdout,"Cc:\t<%s> \n",cc);
|
fprintf(stdout, "Cc:\t<%s> \n", cc);
|
||||||
fprintf(stdout,"Bcc:\t<%s> \n",bcc);
|
fprintf(stdout, "Bcc:\t<%s> \n", bcc);
|
||||||
fprintf(stdout,"Subj:\t<%s> \n",subject);
|
fprintf(stdout, "Subj:\t<%s> \n", subject);
|
||||||
fprintf(stdout,"Body:\t<%s> \n",body.String());
|
fprintf(stdout, "Body:\t<%s> \n", body.String());
|
||||||
fprintf(stdout,"\n");
|
fprintf(stdout, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
//check if valid recipients
|
// Check if recipients are valid
|
||||||
if (strcmp(to.String(), "") == 0
|
if (strcmp(to.String(), "") == 0 &&
|
||||||
&& strcmp(cc, "") == 0
|
strcmp(cc, "") == 0 &&
|
||||||
&& strcmp(bcc, "") == 0) {
|
strcmp(bcc, "") == 0) {
|
||||||
|
|
||||||
fprintf(stdout,
|
fprintf(stdout, "[Error]: You must specify at least one recipient "
|
||||||
"[Error]\nYou must specify at least one recipient in to,cc or bcc fields.\n");
|
"in to, cc or bcc fields.\n");
|
||||||
return B_ERROR;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//read each line until we get a single dot "." on a line
|
// Read each line until we get a single dot "." on a line
|
||||||
char line[32768] = "";
|
char line[32768] = "";
|
||||||
|
|
||||||
printf("Now type your message.\nType '.' alone on a line to send it.\n");
|
printf("Now type your message.\nType '.' alone on a line to send it.\n");
|
||||||
@@ -90,7 +89,7 @@ int main( int argc, char* argv[] )
|
|||||||
if (strcmp(line, ".") != 0) {
|
if (strcmp(line, ".") != 0) {
|
||||||
body.Append(line).Append("\n");
|
body.Append(line).Append("\n");
|
||||||
}
|
}
|
||||||
//fprintf(stdout,"Line: %s \n",line);
|
// fprintf(stdout,"Line: %s \n",line);
|
||||||
} while (strcmp(line, ".") != 0);
|
} while (strcmp(line, ".") != 0);
|
||||||
|
|
||||||
|
|
||||||
@@ -99,8 +98,6 @@ int main( int argc, char* argv[] )
|
|||||||
|
|
||||||
if (verbose)
|
if (verbose)
|
||||||
fprintf(stdout, "\nSending E-mail...\n");
|
fprintf(stdout, "\nSending E-mail...\n");
|
||||||
|
|
||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
BMailMessage mail;
|
BMailMessage mail;
|
||||||
@@ -108,10 +105,14 @@ int main( int argc, char* argv[] )
|
|||||||
mail.AddHeaderField(B_MAIL_CC, cc);
|
mail.AddHeaderField(B_MAIL_CC, cc);
|
||||||
mail.AddHeaderField(B_MAIL_BCC, bcc);
|
mail.AddHeaderField(B_MAIL_BCC, bcc);
|
||||||
mail.AddHeaderField(B_MAIL_SUBJECT, subject);
|
mail.AddHeaderField(B_MAIL_SUBJECT, subject);
|
||||||
mail.AddContent(body.String(),strlen(body.String()));
|
mail.AddContent(body.String(), strlen(body.String()));
|
||||||
status_t result = mail.Send();
|
status_t result = mail.Send();
|
||||||
|
|
||||||
if (result==B_OK)
|
if (result == B_OK) {
|
||||||
fprintf(stdout, "\nMessage was sent successfully.\n");
|
fprintf(stdout, "\nMessage was sent successfully.\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stdout, "Message failed to send: %s", strerror(result));
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user