summaryrefslogtreecommitdiffstats
path: root/libmail/nntpadd.C
blob: 7c055a1d77b05b65a8cc9517ec6c2bd1ddbf63bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
** Copyright 2003-2008, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "nntpadd.H"
#include "nntppost.H"
#include <errno.h>
#include <cstring>

using namespace std;

mail::nntp::add::add(mail::nntp *myServerArg, mail::callback *callbackArg)
	: addMessage(myServerArg), myServer(myServerArg),
	  origCallback(callbackArg), tfile(tmpfile()), byteCount(0)
{
}

mail::nntp::add::~add()
{
	if (tfile)
		fclose(tfile);
}

void mail::nntp::add::saveMessageContents(std::string s)
{
	byteCount += s.size();

	if (tfile)
		if (fwrite(s.c_str(), s.size(), 1, tfile) != 1)
			; // Ignore gcc warning
}

void mail::nntp::add::go()
{
	if (!tfile || fflush(tfile) < 0 || ferror(tfile) < 0)
	{
		fail(strerror(errno));
		return;
	}

	if (!checkServer())
		return;

	PostTask *p=new PostTask(origCallback, *myServer, tfile);

	if (!p)
	{
		fail(strerror(errno));
		return;
	}

	try {
		myServer->installTask(p);
		tfile=NULL;
		origCallback=NULL;
	} catch (...) {
		tfile=p->msg;
		p->msg=NULL;
		delete p;
		throw;
	}
	delete this;
}

void mail::nntp::add::fail(string errmsg)
{
	origCallback->fail(errmsg);
	delete this;
}