Updated Copyright year to 2013
[getmangos.git] / src / game / Mail.h
blob7afe12d52368779d803c305756c3933f7d8bd021
1 /*
2 * Copyright (C) 2005-2013 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 /**
20 * @addtogroup mailing The mail system
21 * The mailing system in MaNGOS consists of mostly 4 files:
22 * - Mail.h
23 * - Mail.cpp
24 * - MassMailMgr.h
25 * - MassMailMgr.cpp
27 * @{
29 * @file Mail.h
30 * This file contains the the headers needed for MaNGOS to handle mails.
34 #ifndef MANGOS_MAIL_H
35 #define MANGOS_MAIL_H
37 #include "Common.h"
38 #include "ObjectGuid.h"
39 #include <map>
41 struct AuctionEntry;
42 class Item;
43 class Object;
44 class Player;
46 #define MAIL_BODY_ITEM_TEMPLATE 8383 ///< - plain letter, A Dusty Unsent Letter: 889
47 /// The maximal amount of items a mail can contain.
48 #define MAX_MAIL_ITEMS 12
49 /**
50 * The type of the mail.
51 * A mail can have 5 Different Types.
53 enum MailMessageType
55 MAIL_NORMAL = 0,
56 MAIL_AUCTION = 2,
57 MAIL_CREATURE = 3, /// client send CMSG_CREATURE_QUERY on this mailmessagetype
58 MAIL_GAMEOBJECT = 4, /// client send CMSG_GAMEOBJECT_QUERY on this mailmessagetype
59 MAIL_ITEM = 5, /// client send CMSG_ITEM_QUERY on this mailmessagetype
61 /**
62 * A Mask representing the status of the mail.
64 enum MailCheckMask
66 MAIL_CHECK_MASK_NONE = 0x00, /// Nothing.
67 MAIL_CHECK_MASK_READ = 0x01, /// This mail was read.
68 MAIL_CHECK_MASK_RETURNED = 0x02, /// This mail was returned. No allow return mail.
69 MAIL_CHECK_MASK_COPIED = 0x04, /// This mail was copied. No allow make item copy from mail text.
70 MAIL_CHECK_MASK_COD_PAYMENT = 0x08, /// This mail is payable on delivery.
71 MAIL_CHECK_MASK_HAS_BODY = 0x10, /// This mail has body text.
74 /**
75 * The different types of Stationaries that exist for mails.
76 * They have been gathered from Stationery.dbc
78 enum MailStationery
80 MAIL_STATIONERY_TEST = 1,
81 MAIL_STATIONERY_DEFAULT = 41,
82 MAIL_STATIONERY_GM = 61,
83 MAIL_STATIONERY_AUCTION = 62,
84 MAIL_STATIONERY_VAL = 64,
85 MAIL_STATIONERY_CHR = 65,
86 MAIL_STATIONERY_ORP = 67, // new in 3.2.2
88 /**
89 * Representation of the State of a mail.
91 enum MailState
93 MAIL_STATE_UNCHANGED = 1,
94 MAIL_STATE_CHANGED = 2,
95 MAIL_STATE_DELETED = 3
97 /**
98 * Answers contained in mails from auctionhouses.
100 enum MailAuctionAnswers
102 AUCTION_OUTBIDDED = 0,
103 AUCTION_WON = 1,
104 AUCTION_SUCCESSFUL = 2,
105 AUCTION_EXPIRED = 3,
106 AUCTION_CANCELLED_TO_BIDDER = 4,
107 AUCTION_CANCELED = 5,
108 AUCTION_SALE_PENDING = 6
111 * A class to represent the sender of a mail.
113 class MailSender
115 public: // Constructors
116 MailSender() : m_messageType(MAIL_NORMAL), m_senderId(0), m_stationery(MAIL_STATIONERY_DEFAULT) {}
119 * Creates a new MailSender object.
121 * @param messageType the type of the mail.
122 * @param sender_guidlow_or_entry The lower part of the GUID of the player sending
123 * this mail, or the Entry of the non-player object.
124 * @param stationery The stationary associated with this MailSender.
127 MailSender(MailMessageType messageType, uint32 sender_guidlow_or_entry, MailStationery stationery = MAIL_STATIONERY_DEFAULT)
128 : m_messageType(messageType), m_senderId(sender_guidlow_or_entry), m_stationery(stationery)
131 MailSender(Object* sender, MailStationery stationery = MAIL_STATIONERY_DEFAULT);
132 MailSender(AuctionEntry* sender);
133 public: // Accessors
134 /// The Messagetype of this MailSender.
135 MailMessageType GetMailMessageType() const { return m_messageType; }
136 /// The GUID of the player represented by this MailSender, or the Entry of the non-player object.
137 uint32 GetSenderId() const { return m_senderId; }
138 /// The stationary associated with this MailSender
139 MailStationery GetStationery() const { return m_stationery; }
140 private:
141 // Trap for wrong used guid as low guid, no body
142 MailSender(MailMessageType messageType, uint64 wrong_guid, MailStationery stationery = MAIL_STATIONERY_DEFAULT);
144 MailMessageType m_messageType;
145 uint32 m_senderId; // player low guid or other object entry
146 MailStationery m_stationery;
149 * A class to represent the receiver of a mail.
151 class MailReceiver
153 public: // Constructors
154 explicit MailReceiver(ObjectGuid receiver_guid) : m_receiver(NULL), m_receiver_guid(receiver_guid) {}
155 MailReceiver(Player* receiver);
156 MailReceiver(Player* receiver, ObjectGuid receiver_guid);
157 public: // Accessors
159 * Gets the player associated with this MailReciever
161 * @see Player
163 * @returns a pointer to the Player this mail is for.
166 Player* GetPlayer() const { return m_receiver; }
168 * Gets the low part of the recievers GUID.
170 * @returns the low part of the GUID of the player associated with this MailReciever
172 ObjectGuid const& GetPlayerGuid() const { return m_receiver_guid; }
173 private:
174 Player* m_receiver;
175 ObjectGuid m_receiver_guid;
178 * The class to represent the draft of a mail.
180 class MailDraft
183 * Holds a Map of GUIDs of items and pointers to the items.
185 typedef std::map<uint32, Item*> MailItemMap;
187 public: // Constructors
189 * Creates a new blank MailDraft object
192 MailDraft()
193 : m_mailTemplateId(0), m_mailTemplateItemsNeed(false), m_money(0), m_COD(0) {}
195 * Creates a new MailDraft object using mail template id.
197 * @param mailTemplateId The ID of the Template to be used.
198 * @param a boolean specifying whether the mail needs items or not.
201 explicit MailDraft(uint16 mailTemplateId, bool need_items = true)
202 : m_mailTemplateId(mailTemplateId), m_mailTemplateItemsNeed(need_items), m_money(0), m_COD(0)
205 * Creates a new MailDraft object using subject and content texts.
207 * @param subject The subject of the mail.
208 * @param itemText The text of the body of the mail.
210 MailDraft(std::string subject, std::string body)
211 : m_mailTemplateId(0), m_mailTemplateItemsNeed(false), m_subject(subject), m_body(body), m_money(0), m_COD(0) {}
212 public: // Accessors
213 /// Returns the template ID used for this MailDraft.
214 uint16 GetMailTemplateId() const { return m_mailTemplateId; }
215 /// Returns the subject of this MailDraft.
216 std::string const& GetSubject() const { return m_subject; }
217 /// Returns the subject of this MailDraft.
218 std::string const& GetBody() const { return m_body; }
219 /// Returns the amount of money in this MailDraft.
220 uint64 GetMoney() const { return m_money; }
221 /// Returns the Cost of delivery of this MailDraft.
222 uint64 GetCOD() const { return m_COD; }
223 public: // modifiers
225 // this two modifiers expected to be applied in normal case to blank draft and exclusively, it will work and with mixed cases but this will be not normal way use.
226 MailDraft& SetSubjectAndBody(std::string subject, std::string body) { m_subject = subject; m_body = body; return *this; }
227 MailDraft& SetMailTemplate(uint16 mailTemplateId, bool need_items = true) { m_mailTemplateId = mailTemplateId, m_mailTemplateItemsNeed = need_items; return *this; }
229 MailDraft& AddItem(Item* item);
231 * Modifies the amount of money in a MailDraft.
233 * @param money The amount of money included in this MailDraft.
235 MailDraft& SetMoney(uint64 money) { m_money = money; return *this; }
237 * Modifies the cost of delivery of the MailDraft.
239 * @param COD the amount to which the cod should be set.
241 MailDraft& SetCOD(uint64 COD) { m_COD = COD; return *this; }
243 void CloneFrom(MailDraft const& draft);
244 public: // finishers
245 void SendReturnToSender(uint32 sender_acc, ObjectGuid sender_guid, ObjectGuid receiver_guid);
246 void SendMailTo(MailReceiver const& receiver, MailSender const& sender, MailCheckMask checked = MAIL_CHECK_MASK_NONE, uint32 deliver_delay = 0);
247 private:
248 MailDraft(MailDraft const&); // trap decl, no body, mail draft must cloned only explicitly...
249 MailDraft& operator=(MailDraft const&); // trap decl, no body, ...because items clone is high price operation
251 void deleteIncludedItems(bool inDB = false);
252 bool prepareItems(Player* receiver); ///< called from SendMailTo for generate mailTemplateBase items
254 /// The ID of the template associated with this MailDraft.
255 uint16 m_mailTemplateId;
256 /// Boolean specifying whether items are required or not.
257 bool m_mailTemplateItemsNeed;
258 /// The subject of the MailDraft.
259 std::string m_subject;
260 /// The body of the MailDraft.
261 std::string m_body;
262 /// A map of items in this MailDraft.
263 MailItemMap m_items; ///< Keep the items in a map to avoid duplicate guids (which can happen), store only low part of guid
265 /// The amount of money in this MailDraft.
266 uint64 m_money;
267 /// The cod amount of this MailDraft.
268 uint64 m_COD;
271 * Structure holding information about an item in the mail.
273 struct MailItemInfo
275 uint32 item_guid; ///< the GUID of the item.
276 uint32 item_template; ///< the ID of the template of the item.
279 typedef std::vector<MailItemInfo> MailItemInfoVec;
281 * Structure that holds an actual mail.
283 struct Mail
285 /// the ID of the message contained in the mail.
286 uint32 messageID;
287 /// the type of the message
288 uint8 messageType;
289 /// the stationary used in this mail.
290 uint8 stationery;
291 /// the ID of the template this mail is based on.
292 uint16 mailTemplateId;
293 /// the LowGUID of the player that sent this mail, or creature low guid, or other id
294 uint32 sender;
295 /// the GUID of the player that this mail is sent to.
296 ObjectGuid receiverGuid;
297 /// the subject of the mail
298 std::string subject;
299 /// the body of the mail
300 std::string body;
301 /// flag mark mail that already has items, or already generate none items for template
302 bool has_items;
303 /// A vector containing Information about the items in this mail.
304 MailItemInfoVec items;
305 /// A vector containing Information about the items that where already take from this mail.
306 std::vector<uint32> removedItems;
307 /// The time at which this mail will expire
308 time_t expire_time;
309 /// The time at which this mail (was/will be) delivered
310 time_t deliver_time;
311 /// The amount of money contained in this mail.
312 uint64 money;
313 /// The amount of money the receiver has to pay to get this mail.
314 uint64 COD;
315 /// The time at which this mail was read.
316 uint32 checked;
317 /// The state of this mail.
318 MailState state;
321 * Adds an item to the mail.
322 * This method adds an item to mail represented by this structure.
323 * There is no checking done whether this is a legal action or not; it is up
324 * to the caller to make sure there is still room for more items in the mail.
326 * @param itemGuidLow the GUID(low) of the item added to the mail.
327 * @param item_template the ID of the template of the item.
330 void AddItem(uint32 itemGuidLow, uint32 item_template)
332 MailItemInfo mii;
333 mii.item_guid = itemGuidLow;
334 mii.item_template = item_template;
335 items.push_back(mii);
336 has_items = true;
340 * Removes an item from the mail.
341 * This method removes an item from the mail.
343 * @see MailItemInfo
345 * @param item_guid The GUID of the item to be removed.
346 * @returns true if the item was removed, or false if no item with that GUID was found.
349 bool RemoveItem(uint32 item_guid)
351 for (MailItemInfoVec::iterator itr = items.begin(); itr != items.end(); ++itr)
353 if (itr->item_guid == item_guid)
355 items.erase(itr);
356 return true;
359 return false;
363 * Checks whether a mail contains items (including case none items generated from template already) or not.
364 * HasItems() checks whether the mail contains items or not.
366 * @returns true if the mail contains items or template items already generated possible none, false otherwise.
369 bool HasItems() const { return has_items; }
372 * Generate items for template if items not genereated before (receiver has been offline, has_items == false)
375 void prepareTemplateItems(Player* receiver); ///< called from _LoadMails for generate mailTemplateBase items not generated for offline player
378 #endif
379 /*! @} */