Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
cs_send.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "Chat.h"
7#include "Language.h"
8#include "ObjectMgr.h"
9#include "Pet.h"
10#include "Player.h"
11#include "ScriptMgr.h"
12
14{
15public:
16 send_commandscript() : CommandScript("send_commandscript") { }
17
18 std::vector<ChatCommand> GetCommands() const OVERRIDE
19 {
20 static std::vector<ChatCommand> sendCommandTable =
21 {
26 };
27
28 static std::vector<ChatCommand> commandTable =
29 {
30 { "send", rbac::RBAC_PERM_COMMAND_SEND, false, NULL, "", sendCommandTable },
31 };
32 return commandTable;
33 }
34
35 // Send mail by command
36 static bool HandleSendMailCommand(ChatHandler* handler, char const* args)
37 {
38 // format: name "subject text" "mail text"
39 Player* target;
40 uint64 targetGuid;
41 std::string targetName;
42 if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
43 return false;
44
45 char* tail1 = strtok(NULL, "");
46 if (!tail1)
47 return false;
48
49 char const* msgSubject = handler->extractQuotedArg(tail1);
50 if (!msgSubject)
51 return false;
52
53 char* tail2 = strtok(NULL, "");
54 if (!tail2)
55 return false;
56
57 char const* msgText = handler->extractQuotedArg(tail2);
58 if (!msgText)
59 return false;
60
61 // msgSubject, msgText isn't NUL after prev. check
62 std::string subject = msgSubject;
63 std::string text = msgText;
64
65 // from console show not existed sender
66 MailSender sender(MAIL_NORMAL, handler->GetSession() ? handler->GetSession()->GetPlayer()->GetGUIDLow() : 0, MAIL_STATIONERY_GM);
67
69 SQLTransaction trans = CharacterDatabase.BeginTransaction();
70 MailDraft(subject, text)
71 .SendMailTo(trans, MailReceiver(target, GUID_LOPART(targetGuid)), sender);
72
73 CharacterDatabase.CommitTransaction(trans);
74
75 std::string nameLink = handler->playerLink(targetName);
76 handler->PSendSysMessage(LANG_MAIL_SENT, nameLink.c_str());
77 return true;
78 }
79
80 // Send items by mail
81 static bool HandleSendItemsCommand(ChatHandler* handler, char const* args)
82 {
83 // format: name "subject text" "mail text" item1[:count1] item2[:count2] ... item12[:count12]
84 Player* receiver;
85 uint64 receiverGuid;
86 std::string receiverName;
87 if (!handler->extractPlayerTarget((char*)args, &receiver, &receiverGuid, &receiverName))
88 return false;
89
90 char* tail1 = strtok(NULL, "");
91 if (!tail1)
92 return false;
93
94 char const* msgSubject = handler->extractQuotedArg(tail1);
95 if (!msgSubject)
96 return false;
97
98 char* tail2 = strtok(NULL, "");
99 if (!tail2)
100 return false;
101
102 char const* msgText = handler->extractQuotedArg(tail2);
103 if (!msgText)
104 return false;
105
106 // msgSubject, msgText isn't NUL after prev. check
107 std::string subject = msgSubject;
108 std::string text = msgText;
109
110 // extract items
111 typedef std::pair<uint32, uint32> ItemPair;
112 typedef std::list< ItemPair > ItemPairs;
113 ItemPairs items;
114
115 // get all tail string
116 char* tail = strtok(NULL, "");
117
118 // get from tail next item str
119 while (char* itemStr = strtok(tail, " "))
120 {
121 // and get new tail
122 tail = strtok(NULL, "");
123
124 // parse item str
125 char const* itemIdStr = strtok(itemStr, ":");
126 char const* itemCountStr = strtok(NULL, " ");
127
128 uint32 itemId = atoi(itemIdStr);
129 if (!itemId)
130 return false;
131
132 ItemTemplate const* item_proto = sObjectMgr->GetItemTemplate(itemId);
133 if (!item_proto)
134 {
136 handler->SetSentErrorMessage(true);
137 return false;
138 }
139
140 uint32 itemCount = itemCountStr ? atoi(itemCountStr) : 1;
141 if (itemCount < 1 || (item_proto->MaxCount > 0 && itemCount > uint32(item_proto->MaxCount)))
142 {
143 handler->PSendSysMessage(LANG_COMMAND_INVALID_ITEM_COUNT, itemCount, itemId);
144 handler->SetSentErrorMessage(true);
145 return false;
146 }
147
148 while (itemCount > item_proto->GetMaxStackSize())
149 {
150 items.push_back(ItemPair(itemId, item_proto->GetMaxStackSize()));
151 itemCount -= item_proto->GetMaxStackSize();
152 }
153
154 items.push_back(ItemPair(itemId, itemCount));
155
156 if (items.size() > MAX_MAIL_ITEMS)
157 {
159 handler->SetSentErrorMessage(true);
160 return false;
161 }
162 }
163
164 // from console show not existed sender
165 MailSender sender(MAIL_NORMAL, handler->GetSession() ? handler->GetSession()->GetPlayer()->GetGUIDLow() : 0, MAIL_STATIONERY_GM);
166
167 // fill mail
168 MailDraft draft(subject, text);
169
170 SQLTransaction trans = CharacterDatabase.BeginTransaction();
171
172 for (ItemPairs::const_iterator itr = items.begin(); itr != items.end(); ++itr)
173 {
174 if (Item* item = Item::CreateItem(itr->first, itr->second, handler->GetSession() ? handler->GetSession()->GetPlayer() : 0))
175 {
176 item->SaveToDB(trans); // save for prevent lost at next mail load, if send fail then item will deleted
177 draft.AddItem(item);
178 }
179 }
180
181 draft.SendMailTo(trans, MailReceiver(receiver, GUID_LOPART(receiverGuid)), sender);
182 CharacterDatabase.CommitTransaction(trans);
183
184 std::string nameLink = handler->playerLink(receiverName);
185 handler->PSendSysMessage(LANG_MAIL_SENT, nameLink.c_str());
186 return true;
187 }
188
189 static bool HandleSendMoneyCommand(ChatHandler* handler, char const* args)
190 {
192
193 Player* receiver;
194 uint64 receiverGuid;
195 std::string receiverName;
196 if (!handler->extractPlayerTarget((char*)args, &receiver, &receiverGuid, &receiverName))
197 return false;
198
199 char* tail1 = strtok(NULL, "");
200 if (!tail1)
201 return false;
202
203 char* msgSubject = handler->extractQuotedArg(tail1);
204 if (!msgSubject)
205 return false;
206
207 char* tail2 = strtok(NULL, "");
208 if (!tail2)
209 return false;
210
211 char* msgText = handler->extractQuotedArg(tail2);
212 if (!msgText)
213 return false;
214
215 char* moneyStr = strtok(NULL, "");
216 int32 money = moneyStr ? atoi(moneyStr) : 0;
217 if (money <= 0)
218 return false;
219
220 // msgSubject, msgText isn't NUL after prev. check
221 std::string subject = msgSubject;
222 std::string text = msgText;
223
224 // from console show not existed sender
225 MailSender sender(MAIL_NORMAL, handler->GetSession() ? handler->GetSession()->GetPlayer()->GetGUIDLow() : 0, MAIL_STATIONERY_GM);
226
227 SQLTransaction trans = CharacterDatabase.BeginTransaction();
228
229 MailDraft(subject, text)
230 .AddMoney(money)
231 .SendMailTo(trans, MailReceiver(receiver, GUID_LOPART(receiverGuid)), sender);
232
233 CharacterDatabase.CommitTransaction(trans);
234
235 std::string nameLink = handler->playerLink(receiverName);
236 handler->PSendSysMessage(LANG_MAIL_SENT, nameLink.c_str());
237 return true;
238 }
239
240 static bool HandleSendMessageCommand(ChatHandler* handler, char const* args)
241 {
243 Player* player;
244 if (!handler->extractPlayerTarget((char*)args, &player))
245 return false;
246
247 char* msgStr = strtok(NULL, "");
248 if (!msgStr)
249 return false;
250
252 if (player->GetSession()->isLogingOut())
253 {
255 handler->SetSentErrorMessage(true);
256 return false;
257 }
258
260 // Use SendAreaTriggerMessage for fastest delivery.
261 player->GetSession()->SendNotification("%s", msgStr);
262 player->GetSession()->SendNotification("|cffff0000[Message from administrator]:|r");
263
264 // Confirmation message
265 std::string nameLink = handler->GetNameLink(player);
266 handler->PSendSysMessage(LANG_SENDMESSAGE, nameLink.c_str(), msgStr);
267
268 return true;
269 }
270};
271
std::int32_t int32
Definition Define.h:73
std::uint32_t uint32
Definition Define.h:77
#define OVERRIDE
Definition Define.h:60
std::uint64_t uint64
Definition Define.h:76
@ LANG_COMMAND_INVALID_ITEM_COUNT
Definition Language.h:65
@ LANG_COMMAND_MAIL_ITEMS_LIMIT
Definition Language.h:66
@ LANG_MAIL_SENT
Definition Language.h:179
@ LANG_SENDMESSAGE
Definition Language.h:892
@ LANG_PLAYER_NOT_FOUND
Definition Language.h:487
@ LANG_COMMAND_ITEMIDINVALID
Definition Language.h:415
#define MAX_MAIL_ITEMS
Definition Mail.h:20
@ MAIL_STATIONERY_GM
Definition Mail.h:47
@ MAIL_NORMAL
Definition Mail.h:24
uint32 GUID_LOPART(uint64 x)
#define sObjectMgr
Definition ObjectMgr.h:1617
Skyfire::AutoPtr< Transaction, Skyfire::Mutex > SQLTransaction
Definition Transaction.h:42
virtual std::string GetNameLink() const
Definition Chat.h:79
std::string playerLink(std::string const &name) const
Definition Chat.h:108
char * extractQuotedArg(char *args)
Definition Chat.cpp:1255
WorldSession * GetSession()
Definition Chat.h:43
void PSendSysMessage(const char *format,...) ATTR_PRINTF(2
Definition Chat.cpp:221
void SetSentErrorMessage(bool val)
Definition Chat.h:114
bool extractPlayerTarget(char *args, Player **player, uint64 *player_guid=NULL, std::string *player_name=NULL)
Definition Chat.cpp:1184
virtual void SendSysMessage(const char *str)
Definition Chat.cpp:153
CommandScript(const char *name)
Definition Item.h:202
static Item * CreateItem(uint32 itemEntry, uint32 count, Player const *player=NULL)
Definition Item.cpp:1012
void SendMailTo(SQLTransaction &trans, MailReceiver const &receiver, MailSender const &sender, MailCheckMask checked=MAIL_CHECK_MASK_NONE, uint32 deliver_delay=0)
Definition Mail.cpp:162
MailDraft & AddItem(Item *item)
Definition Mail.cpp:70
MailDraft & AddMoney(uint64 money)
Definition Mail.h:126
uint32 GetGUIDLow() const
Definition Object.h:120
WorldSession * GetSession() const
Definition Player.h:2417
bool isLogingOut() const
Is the user engaged in a log out process?
void SendNotification(const char *format,...) ATTR_PRINTF(2
Player * GetPlayer() const
static bool HandleSendItemsCommand(ChatHandler *handler, char const *args)
Definition cs_send.cpp:81
std::vector< ChatCommand > GetCommands() const OVERRIDE
Definition cs_send.cpp:18
static bool HandleSendMailCommand(ChatHandler *handler, char const *args)
Definition cs_send.cpp:36
static bool HandleSendMoneyCommand(ChatHandler *handler, char const *args)
Send money by mail.
Definition cs_send.cpp:189
static bool HandleSendMessageCommand(ChatHandler *handler, char const *args)
Send a message to a player in game.
Definition cs_send.cpp:240
void AddSC_send_commandscript()
Definition cs_send.cpp:272
CharacterDatabaseWorkerPool CharacterDatabase
Accessor to the character database.
Definition Main.cpp:41
@ RBAC_PERM_COMMAND_SEND
Definition RBAC.h:371
@ RBAC_PERM_COMMAND_SEND_MESSAGE
Definition RBAC.h:374
@ RBAC_PERM_COMMAND_SEND_MAIL
Definition RBAC.h:373
@ RBAC_PERM_COMMAND_SEND_MONEY
Definition RBAC.h:375
@ RBAC_PERM_COMMAND_SEND_ITEMS
Definition RBAC.h:372
uint32 GetMaxStackSize() const