summaryrefslogtreecommitdiff
path: root/src/map/mail.c
blob: 0a6603a453f60e925cc6dc478ced63e5317a086a (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
 * This file is part of Hercules.
 * http://herc.ws - http://github.com/HerculesWS/Hercules
 *
 * Copyright (C) 2012-2018  Hercules Dev Team
 * Copyright (C)  Athena Dev Teams
 *
 * Hercules 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#define HERCULES_CORE

#include "mail.h"

#include "map/atcommand.h"
#include "map/clif.h"
#include "map/itemdb.h"
#include "map/log.h"
#include "map/pc.h"
#include "map/storage.h"
#include "common/nullpo.h"
#include "common/showmsg.h"

#include <time.h>
#include <string.h>

static struct mail_interface mail_s;
struct mail_interface *mail;

static void mail_clear(struct map_session_data *sd)
{
	nullpo_retv(sd);
	sd->mail.nameid = 0;
	sd->mail.index = 0;
	sd->mail.amount = 0;
	sd->mail.zeny = 0;

	return;
}

static int mail_removeitem(struct map_session_data *sd, short flag)
{
	nullpo_ret(sd);

	if( sd->mail.amount )
	{
		if (flag) // Item send
			pc->delitem(sd, sd->mail.index, sd->mail.amount, 1, DELITEM_NORMAL, LOG_TYPE_MAIL);
		else
			clif->additem(sd, sd->mail.index, sd->mail.amount, 0);
	}

	sd->mail.nameid = 0;
	sd->mail.index = 0;
	sd->mail.amount = 0;
	return 1;
}

static int mail_removezeny(struct map_session_data *sd, short flag)
{
	nullpo_ret(sd);

	if (flag && sd->mail.zeny > 0)
	{  //Zeny send
		pc->payzeny(sd,sd->mail.zeny,LOG_TYPE_MAIL, NULL);
	}
	sd->mail.zeny = 0;

	return 1;
}

static unsigned char mail_setitem(struct map_session_data *sd, int idx, int amount)
{

	nullpo_retr(1, sd);
	if( pc_istrading(sd) )
		return 1;

	if( idx == 0 ) { // Zeny Transfer
		if( amount < 0 || !pc_can_give_items(sd) )
			return 1;

		if( amount > sd->status.zeny )
			amount = sd->status.zeny;

		sd->mail.zeny = amount;
		// clif->updatestatus(sd, SP_ZENY);
		return 0;
	} else { // Item Transfer
		idx -= 2;
		mail->removeitem(sd, 0);

		if (idx < 0 || idx >= sd->status.inventorySize)
			return 1;
		if( amount <= 0 || amount > sd->status.inventory[idx].amount )
			return 1;
		if( !pc_can_give_items(sd) || sd->status.inventory[idx].expire_time ||
			!itemdb_canmail(&sd->status.inventory[idx],pc_get_group_level(sd)) ||
			(sd->status.inventory[idx].bound && !pc_can_give_bound_items(sd)) )
			return 1;

		sd->mail.index = idx;
		sd->mail.nameid = sd->status.inventory[idx].nameid;
		sd->mail.amount = amount;

		return 0;
	}
}

static bool mail_setattachment(struct map_session_data *sd, struct mail_message *msg)
{
	int n;

	nullpo_retr(false,sd);
	nullpo_retr(false,msg);

	if( sd->mail.zeny < 0 || sd->mail.zeny > sd->status.zeny )
		return false;

	n = sd->mail.index;
	Assert_retr(false, n >= 0 && n < sd->status.inventorySize);
	if( sd->mail.amount )
	{
		if( sd->status.inventory[n].nameid != sd->mail.nameid )
			return false;

		if( sd->status.inventory[n].amount < sd->mail.amount )
			return false;

		if( sd->weight > sd->max_weight )
			return false;

		memcpy(&msg->item, &sd->status.inventory[n], sizeof(struct item));
		msg->item.amount = sd->mail.amount;
		if (msg->item.amount != sd->mail.amount)  // check for amount overflow
			return false;
	}
	else
		memset(&msg->item, 0x00, sizeof(struct item));

	msg->zeny = sd->mail.zeny;

	// Removes the attachment from sender
	mail->removeitem(sd,1);
	mail->removezeny(sd,1);

	return true;
}

static void mail_getattachment(struct map_session_data *sd, int zeny, struct item *item)
{
	nullpo_retv(sd);
	nullpo_retv(item);
	if( item->nameid > 0 && item->amount > 0 )
	{
		pc->additem(sd, item, item->amount, LOG_TYPE_MAIL);
		clif->mail_getattachment(sd->fd, 0);
	}

	if( zeny > 0 )
	{  //Zeny receive
		pc->getzeny(sd, zeny,LOG_TYPE_MAIL, NULL);
	}
}

static int mail_openmail(struct map_session_data *sd)
{
	nullpo_ret(sd);

	if (sd->state.storage_flag != STORAGE_FLAG_CLOSED || sd->state.vending || sd->state.prevend || sd->state.buyingstore || sd->state.trading)
		return 0;

	clif->mail_window(sd->fd, 0);

	return 1;
}

static void mail_deliveryfail(struct map_session_data *sd, struct mail_message *msg)
{
	nullpo_retv(sd);
	nullpo_retv(msg);

	if( msg->item.amount > 0 )
	{
		// Item receive (due to failure)
		pc->additem(sd, &msg->item, msg->item.amount, LOG_TYPE_MAIL);
	}

	if( msg->zeny > 0 )
	{
		pc->getzeny(sd,msg->zeny,LOG_TYPE_MAIL, NULL); //Zeny receive (due to failure)
	}

	clif->mail_send(sd->fd, true);
}

// This function only check if the mail operations are valid
static bool mail_invalid_operation(struct map_session_data *sd)
{
	nullpo_retr(false, sd);
	if( !map->list[sd->bl.m].flag.town && !pc->can_use_command(sd, "@mail") ) {
		ShowWarning("clif->parse_Mail: char '%s' trying to do invalid mail operations.\n", sd->status.name);
		return true;
	}

	return false;
}

void mail_defaults(void)
{
	mail = &mail_s;

	mail->clear = mail_clear;
	mail->removeitem = mail_removeitem;
	mail->removezeny = mail_removezeny;
	mail->setitem = mail_setitem;
	mail->setattachment = mail_setattachment;
	mail->getattachment = mail_getattachment;
	mail->openmail = mail_openmail;
	mail->deliveryfail = mail_deliveryfail;
	mail->invalid_operation = mail_invalid_operation;
}