bulkmail.cpp 5.47 KB
Newer Older
1 2 3 4 5 6
/* Synchronet bulk e-mail functions */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"

24
bool sbbs_t::bulkmail(uchar *ar)
25
{
26 27 28
	char		str[256],title[LEN_TITLE+1];
	char		msgpath[MAX_PATH+1];
	char*		msgbuf;
29 30
	const char*	editor=NULL;
	const char*	charset=NULL;
31 32 33 34
	char 		tmp[512];
	int 		i,j,x;
	long		msgs=0;
	long		length;
35
	long		wm_mode=WM_EMAIL;
36 37 38 39 40 41 42
	FILE*		fp;
	smb_t		smb;
	smbmsg_t	msg;
	user_t		user;

	memset(&msg,0,sizeof(msg));
	
43 44 45 46
	title[0]=0;
	action=NODE_SMAL;
	nodesync();

47
	if(cfg.sys_misc&SM_ANON_EM && useron.exempt&FLAG('A')
48
		&& !noyes(text[AnonymousQ])) {
49
		msg.hdr.attr|=MSG_ANONYMOUS;
50 51
		wm_mode|=WM_ANON;
	}
52

53
	msg_tmp_fname(useron.xedit, msgpath, sizeof(msgpath));
54 55
	if(!writemsg(msgpath,nulstr,title,wm_mode,INVALID_SUB,"Bulk Mailing"
		,/* From: */useron.alias
56 57
		,&editor
		,&charset)) {
58
		bputs(text[Aborted]);
59 60 61 62 63 64 65 66
		return(false); 
	}

	if((fp=fopen(msgpath,"r"))==NULL) {
		errormsg(WHERE,ERR_OPEN,msgpath,O_RDONLY);
		return(false); 
	}

67
	if((length=(long)filelength(fileno(fp)))<=0) {
68 69 70
		fclose(fp);
		return(false);
	}
71 72 73 74

	bputs(text[WritingIndx]);
	CRLF;

75
	if((msgbuf=(char*)malloc(length+1))==NULL) {
76
		fclose(fp);
77 78 79 80 81 82 83 84 85 86 87 88
		errormsg(WHERE,ERR_ALLOC,msgpath,length+1);
		return(false);
	}
	length=fread(msgbuf,sizeof(char),length,fp);
	fclose(fp);
	if(length<0) {
		free(msgbuf);
		errormsg(WHERE,ERR_READ,msgpath,length);
		return(false);
	}
	msgbuf[length]=0;	/* ASCIIZ */

89
	smb_hfield_str(&msg,SENDER,useron.alias);
90

91
	SAFEPRINTF(str,"%u",useron.number);
92
	smb_hfield_str(&msg,SENDEREXT,str);
93

94
	smb_hfield_str(&msg,SUBJECT,title);
95

96
	msg.hdr.when_written.time=time32(NULL);
97 98
	msg.hdr.when_written.zone=sys_timezone(&cfg);

99
	editor_info_to_msg(&msg, editor, charset);
100

101 102
	memset(&smb,0,sizeof(smb));
	smb.subnum=INVALID_SUB;	/* mail database */
103
	i=savemsg(&cfg, &smb, &msg, &client, server_host_name(), msgbuf, /* remsg: */NULL);
104 105
	free(msgbuf);
	if(i!=0) {
106
		smb_close(&smb);
107 108 109
		smb_freemsgmem(&msg);
		return(false);
	}
110 111 112

	j=lastuser(&cfg);

113
	if(ar && *ar)
114 115
		for(i=1;i<=j;i++) {
			user.number=i;
116 117
			if(getuserdat(&cfg, &user)!=0)
				continue;
118 119
			if(user.misc&(DELETED|INACTIVE))
				continue;
rswindell's avatar
rswindell committed
120
			if(chk_ar(ar,&user,/* client: */NULL)) {
121
				if((x=bulkmailhdr(&smb, &msg, i))!=SMB_SUCCESS) {
122
					errormsg(WHERE,ERR_WRITE,smb.file,x);
123
					break;
124
				}
125
				msgs++;
126 127
			} 
		}
128
	else
129
		while(online) {
130
			bputs(text[EnterAfterLastDestUser]);
131
			if(!getstr(str,LEN_ALIAS,cfg.uq&UQ_NOUPRLWR ? K_NONE:K_UPRLWR))
132 133
				break;
			if((i=finduser(str))!=0) {
134
				if((x=bulkmailhdr(&smb, &msg, i))!=SMB_SUCCESS) {
135
					errormsg(WHERE,ERR_WRITE,smb.file,x);
136
					break;
137 138 139 140 141
				}
				msgs++; 
			}
		}

142
	if((i=smb_open_da(&smb))==SMB_SUCCESS) {
143 144 145 146 147 148
		if(!msgs)
			smb_freemsg_dfields(&smb,&msg,SMB_ALL_REFS);
		else if(msgs>1)
			smb_incmsg_dfields(&smb,&msg,(ushort)msgs-1);
		smb_close_da(&smb);
	}
149 150 151

	smb_close(&smb);
	smb_freemsgmem(&msg);
152

153
	if(i!=SMB_SUCCESS) {
154 155 156
		errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
		return(false);
	}
157

158 159
	putuserrec(&cfg,useron.number,U_EMAILS,5,ultoa(useron.emails,tmp,10));
	putuserrec(&cfg,useron.number,U_ETODAY,5,ultoa(useron.etoday,tmp,10));
160 161

	return(true);
162 163 164
}


165
int sbbs_t::bulkmailhdr(smb_t* smb, smbmsg_t* msg, uint usernum)
166
{
167 168
    char		str[256];
    int			i,j;
169
	ushort		nettype=NET_UNKNOWN;
170 171 172 173 174 175 176 177
    node_t		node;
	user_t		user;
	smbmsg_t	newmsg;

	user.number=usernum;
	if(getuserdat(&cfg, &user)!=0)
		return(0);

178
	if((i=smb_copymsgmem(NULL,&newmsg,msg))!=SMB_SUCCESS)
179 180 181
		return(i);

	SAFECOPY(str,user.alias);
182
	smb_hfield_str(&newmsg,RECIPIENT,str);
183 184 185

	if(cfg.sys_misc&SM_FWDTONET && user.misc&NETMAIL && user.netmail[0]) {
		bprintf(text[UserNetMail],user.netmail);
186
		smb_hfield_netaddr(&newmsg,RECIPIENTNETADDR,user.netmail,&nettype);
187
		smb_hfield_bin(&newmsg,RECIPIENTNETTYPE,nettype);
188
	} else {
189
		SAFEPRINTF(str,"%u",usernum);
190
		smb_hfield_str(&newmsg,RECIPIENTEXT,str);
191 192
	}

193
	j=smb_addmsghdr(smb,&newmsg,smb_storage_mode(&cfg, smb));
194
	smb_freemsgmem(&newmsg);
195
	if(j!=SMB_SUCCESS)
196 197 198
		return(j);

	lncntr=0;
199
	bprintf(text[Emailing],user.alias,usernum);
200
	SAFEPRINTF2(str,"bulk-mailed %s #%d"
201
		,user.alias,usernum);
202 203 204 205 206 207 208 209
	logline("E+",str);
	useron.emails++;
	logon_emails++;
	useron.etoday++;
	for(i=1;i<=cfg.sys_nodes;i++) { /* Tell user, if online */
		getnodedat(i,&node,0);
		if(node.useron==usernum && !(node.misc&NODE_POFF)
			&& (node.status==NODE_INUSE || node.status==NODE_QUIET)) {
210
			SAFEPRINTF2(str,text[EmailNodeMsg],cfg.node_num,useron.alias);
211
			putnmsg(&cfg,i,str);
212 213 214
			break; 
		} 
	}
215
	if(i>cfg.sys_nodes) {   /* User wasn't online, so leave short msg */
216
		SAFEPRINTF(str,text[UserSentYouMail],useron.alias);
217 218
		putsmsg(&cfg,usernum,str); 
	}
219 220 221
	return(0);
}