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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* 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.
*
* 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/>.
*/
#include "ScriptMgr.h"
#include "Chat.h"
#include "ChatCommand.h"
#include "DatabaseEnv.h"
#include "Item.h"
#include "Language.h"
#include "Mail.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "RBAC.h"
#include "WorldSession.h"
#if TRINITY_COMPILER == TRINITY_COMPILER_GNU
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
using namespace Trinity::ChatCommands;
class send_commandscript : public CommandScript
{
public:
send_commandscript() : CommandScript("send_commandscript") { }
ChatCommandTable GetCommands() const override
{
static ChatCommandTable sendCommandTable =
{
{ "items", HandleSendItemsCommand, rbac::RBAC_PERM_COMMAND_SEND_ITEMS, Console::Yes },
{ "mail", HandleSendMailCommand, rbac::RBAC_PERM_COMMAND_SEND_MAIL, Console::Yes },
{ "message", HandleSendMessageCommand, rbac::RBAC_PERM_COMMAND_SEND_MESSAGE, Console::Yes },
{ "money", HandleSendMoneyCommand, rbac::RBAC_PERM_COMMAND_SEND_MONEY, Console::Yes },
};
static ChatCommandTable commandTable =
{
{ "send", sendCommandTable },
};
return commandTable;
}
// Send mail by command
static bool HandleSendMailCommand(ChatHandler* handler, char const* args)
{
// format: name "subject text" "mail text"
Player* target;
ObjectGuid targetGuid;
std::string targetName;
if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
return false;
char* tail1 = strtok(nullptr, "");
if (!tail1)
return false;
char const* msgSubject = handler->extractQuotedArg(tail1);
if (!msgSubject)
return false;
char* tail2 = strtok(nullptr, "");
if (!tail2)
return false;
char const* msgText = handler->extractQuotedArg(tail2);
if (!msgText)
return false;
// msgSubject, msgText isn't NUL after prev. check
std::string subject = msgSubject;
std::string text = msgText;
// from console, use non-existing sender
MailSender sender(MAIL_NORMAL, handler->GetSession() ? handler->GetSession()->GetPlayer()->GetGUID().GetCounter() : UI64LIT(0), MAIL_STATIONERY_GM);
/// @todo Fix poor design
CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
MailDraft(subject, text)
.SendMailTo(trans, MailReceiver(target, targetGuid.GetCounter()), sender);
CharacterDatabase.CommitTransaction(trans);
std::string nameLink = handler->playerLink(targetName);
handler->PSendSysMessage(LANG_MAIL_SENT, nameLink.c_str());
return true;
}
// Send items by mail
static bool HandleSendItemsCommand(ChatHandler* handler, char const* args)
{
// format: name "subject text" "mail text" item1[:count1] item2[:count2] ... item12[:count12]
Player* receiver;
ObjectGuid receiverGuid;
std::string receiverName;
if (!handler->extractPlayerTarget((char*)args, &receiver, &receiverGuid, &receiverName))
return false;
char* tail1 = strtok(nullptr, "");
if (!tail1)
return false;
char const* msgSubject = handler->extractQuotedArg(tail1);
if (!msgSubject)
return false;
char* tail2 = strtok(nullptr, "");
if (!tail2)
return false;
char const* msgText = handler->extractQuotedArg(tail2);
if (!msgText)
return false;
// msgSubject, msgText isn't NUL after prev. check
std::string subject = msgSubject;
std::string text = msgText;
// extract items
typedef std::pair<uint32, uint32> ItemPair;
typedef std::list< ItemPair > ItemPairs;
ItemPairs items;
// get all tail string
char* tail = strtok(nullptr, "");
// get from tail next item str
while (char* itemStr = strtok(tail, " "))
{
// and get new tail
tail = strtok(nullptr, "");
// parse item str
char const* itemIdStr = strtok(itemStr, ":");
char const* itemCountStr = strtok(nullptr, " ");
Optional<uint32> itemId = Trinity::StringTo<uint32>(itemIdStr);
if (!itemId)
return false;
ItemTemplate const* item_proto = sObjectMgr->GetItemTemplate(*itemId);
if (!item_proto)
{
handler->PSendSysMessage(LANG_COMMAND_ITEMIDINVALID, *itemId);
handler->SetSentErrorMessage(true);
return false;
}
uint32 itemCount = itemCountStr ? atoi(itemCountStr) : 1;
if (itemCount < 1 || (item_proto->GetMaxCount() > 0 && itemCount > uint32(item_proto->GetMaxCount())))
{
handler->PSendSysMessage(LANG_COMMAND_INVALID_ITEM_COUNT, itemCount, *itemId);
handler->SetSentErrorMessage(true);
return false;
}
while (itemCount > item_proto->GetMaxStackSize())
{
items.push_back(ItemPair(*itemId, item_proto->GetMaxStackSize()));
itemCount -= item_proto->GetMaxStackSize();
}
items.push_back(ItemPair(*itemId, itemCount));
if (items.size() > MAX_MAIL_ITEMS)
{
handler->PSendSysMessage(LANG_COMMAND_MAIL_ITEMS_LIMIT, MAX_MAIL_ITEMS);
handler->SetSentErrorMessage(true);
return false;
}
}
// from console show nonexisting sender
MailSender sender(MAIL_NORMAL, handler->GetSession() ? handler->GetSession()->GetPlayer()->GetGUID().GetCounter() : UI64LIT(0), MAIL_STATIONERY_GM);
// fill mail
MailDraft draft(subject, text);
CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
for (ItemPairs::const_iterator itr = items.begin(); itr != items.end(); ++itr)
{
if (Item* item = Item::CreateItem(itr->first, itr->second, ItemContext::NONE, handler->GetSession() ? handler->GetSession()->GetPlayer() : nullptr))
{
item->SaveToDB(trans); // Save to prevent being lost at next mail load. If send fails, the item will be deleted.
draft.AddItem(item);
}
}
draft.SendMailTo(trans, MailReceiver(receiver, receiverGuid.GetCounter()), sender);
CharacterDatabase.CommitTransaction(trans);
std::string nameLink = handler->playerLink(receiverName);
handler->PSendSysMessage(LANG_MAIL_SENT, nameLink.c_str());
return true;
}
/// Send money by mail
static bool HandleSendMoneyCommand(ChatHandler* handler, PlayerIdentifier const& receiver, QuotedString const& subject, QuotedString const& text, int64 money)
{
/// format: name "subject text" "mail text" money
// from console show nonexisting sender
MailSender sender(MAIL_NORMAL, handler->GetSession() ? handler->GetSession()->GetPlayer()->GetGUID().GetCounter() : UI64LIT(0), MAIL_STATIONERY_GM);
CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
MailDraft(subject, text)
.AddMoney(money)
.SendMailTo(trans, MailReceiver(receiver.GetConnectedPlayer(), receiver.GetGUID().GetCounter()), sender);
CharacterDatabase.CommitTransaction(trans);
std::string nameLink = handler->playerLink(receiver.GetName());
handler->PSendSysMessage(LANG_MAIL_SENT, nameLink.c_str());
return true;
}
/// Send a message to a player in game
static bool HandleSendMessageCommand(ChatHandler* handler, char const* args)
{
/// - Find the player
Player* player;
if (!handler->extractPlayerTarget((char*)args, &player))
return false;
char* msgStr = strtok(nullptr, "");
if (!msgStr)
return false;
/// - Check if player is logging out.
if (player->GetSession()->isLogingOut())
{
handler->SendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true);
return false;
}
/// - Send the message
player->GetSession()->SendNotification("%s", msgStr);
player->GetSession()->SendNotification("|cffff0000[Message from administrator]:|r");
// Confirmation message
std::string nameLink = handler->GetNameLink(player);
handler->PSendSysMessage(LANG_SENDMESSAGE, nameLink.c_str(), msgStr);
return true;
}
};
void AddSC_send_commandscript()
{
new send_commandscript();
}
|