[1] | 1 | // Copyright (c) Athena Dev Teams - Licensed under GNU GPL |
---|
| 2 | // For more information, see LICENCE in the main folder |
---|
| 3 | |
---|
| 4 | #include "../common/mmo.h" |
---|
| 5 | #include "../common/malloc.h" |
---|
| 6 | #include "../common/showmsg.h" |
---|
| 7 | #include "../common/socket.h" |
---|
| 8 | #include "../common/strlib.h" |
---|
| 9 | #include "../common/sql.h" |
---|
| 10 | #include "../common/timer.h" |
---|
| 11 | #include "char.h" |
---|
| 12 | #include "inter.h" |
---|
| 13 | |
---|
| 14 | #include <stdio.h> |
---|
| 15 | #include <string.h> |
---|
| 16 | #include <stdlib.h> |
---|
| 17 | |
---|
| 18 | static int mail_fromsql(int char_id, struct mail_data* md) |
---|
| 19 | { |
---|
| 20 | int i, j; |
---|
| 21 | struct mail_message *msg; |
---|
| 22 | struct item *item; |
---|
| 23 | char *data; |
---|
| 24 | StringBuf buf; |
---|
| 25 | |
---|
| 26 | memset(md, 0, sizeof(struct mail_data)); |
---|
| 27 | md->amount = 0; |
---|
| 28 | md->full = false; |
---|
| 29 | |
---|
| 30 | StringBuf_Init(&buf); |
---|
| 31 | StringBuf_AppendStr(&buf, "SELECT `id`,`send_name`,`send_id`,`dest_name`,`dest_id`,`title`,`message`,`time`,`status`," |
---|
| 32 | "`zeny`,`amount`,`nameid`,`refine`,`attribute`,`identify`"); |
---|
| 33 | for (i = 0; i < MAX_SLOTS; i++) |
---|
| 34 | StringBuf_Printf(&buf, ",`card%d`", i); |
---|
| 35 | |
---|
| 36 | // I keep the `status` < 3 just in case someone forget to apply the sqlfix |
---|
| 37 | StringBuf_Printf(&buf, " FROM `%s` WHERE `dest_id`='%d' AND `status` < 3 ORDER BY `id` LIMIT %d", |
---|
| 38 | mail_db, char_id, MAIL_MAX_INBOX + 1); |
---|
| 39 | |
---|
| 40 | if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) ) |
---|
| 41 | Sql_ShowDebug(sql_handle); |
---|
| 42 | |
---|
| 43 | StringBuf_Destroy(&buf); |
---|
| 44 | |
---|
| 45 | for (i = 0; i < MAIL_MAX_INBOX && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i ) |
---|
| 46 | { |
---|
| 47 | msg = &md->msg[i]; |
---|
| 48 | Sql_GetData(sql_handle, 0, &data, NULL); msg->id = atoi(data); |
---|
| 49 | Sql_GetData(sql_handle, 1, &data, NULL); safestrncpy(msg->send_name, data, NAME_LENGTH); |
---|
| 50 | Sql_GetData(sql_handle, 2, &data, NULL); msg->send_id = atoi(data); |
---|
| 51 | Sql_GetData(sql_handle, 3, &data, NULL); safestrncpy(msg->dest_name, data, NAME_LENGTH); |
---|
| 52 | Sql_GetData(sql_handle, 4, &data, NULL); msg->dest_id = atoi(data); |
---|
| 53 | Sql_GetData(sql_handle, 5, &data, NULL); safestrncpy(msg->title, data, MAIL_TITLE_LENGTH); |
---|
| 54 | Sql_GetData(sql_handle, 6, &data, NULL); safestrncpy(msg->body, data, MAIL_BODY_LENGTH); |
---|
| 55 | Sql_GetData(sql_handle, 7, &data, NULL); msg->timestamp = atoi(data); |
---|
| 56 | Sql_GetData(sql_handle, 8, &data, NULL); msg->status = (mail_status)atoi(data); |
---|
| 57 | Sql_GetData(sql_handle, 9, &data, NULL); msg->zeny = atoi(data); |
---|
| 58 | item = &msg->item; |
---|
| 59 | Sql_GetData(sql_handle,10, &data, NULL); item->amount = (short)atoi(data); |
---|
| 60 | Sql_GetData(sql_handle,11, &data, NULL); item->nameid = atoi(data); |
---|
| 61 | Sql_GetData(sql_handle,12, &data, NULL); item->refine = atoi(data); |
---|
| 62 | Sql_GetData(sql_handle,13, &data, NULL); item->attribute = atoi(data); |
---|
| 63 | Sql_GetData(sql_handle,14, &data, NULL); item->identify = atoi(data); |
---|
| 64 | |
---|
| 65 | for (j = 0; j < MAX_SLOTS; j++) |
---|
| 66 | { |
---|
| 67 | Sql_GetData(sql_handle, 15 + j, &data, NULL); |
---|
| 68 | item->card[j] = atoi(data); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | md->full = ( Sql_NumRows(sql_handle) > MAIL_MAX_INBOX ); |
---|
| 73 | |
---|
| 74 | md->amount = i; |
---|
| 75 | md->changed = false; |
---|
| 76 | Sql_FreeResult(sql_handle); |
---|
| 77 | |
---|
| 78 | md->unchecked = 0; |
---|
| 79 | md->unread = 0; |
---|
| 80 | for (i = 0; i < md->amount; i++) |
---|
| 81 | { |
---|
| 82 | msg = &md->msg[i]; |
---|
| 83 | if( msg->status == MAIL_NEW ) |
---|
| 84 | { |
---|
| 85 | if ( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `status` = '%d' WHERE `id` = '%d'", mail_db, MAIL_UNREAD, msg->id) ) |
---|
| 86 | Sql_ShowDebug(sql_handle); |
---|
| 87 | |
---|
| 88 | msg->status = MAIL_UNREAD; |
---|
| 89 | md->unchecked++; |
---|
| 90 | } |
---|
| 91 | else if ( msg->status == MAIL_UNREAD ) |
---|
| 92 | md->unread++; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | ShowInfo("mail load complete from DB - id: %d (total: %d)\n", char_id, md->amount); |
---|
| 96 | return 1; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /// Stores a single message in the database. |
---|
| 100 | /// Returns the message's ID if successful (or 0 if it fails). |
---|
| 101 | int mail_savemessage(struct mail_message* msg) |
---|
| 102 | { |
---|
| 103 | StringBuf buf; |
---|
| 104 | SqlStmt* stmt; |
---|
| 105 | int j; |
---|
| 106 | |
---|
| 107 | // build message save query |
---|
| 108 | StringBuf_Init(&buf); |
---|
| 109 | StringBuf_Printf(&buf, "INSERT INTO `%s` (`send_name`, `send_id`, `dest_name`, `dest_id`, `title`, `message`, `time`, `status`, `zeny`, `amount`, `nameid`, `refine`, `attribute`, `identify`", mail_db); |
---|
| 110 | for (j = 0; j < MAX_SLOTS; j++) |
---|
| 111 | StringBuf_Printf(&buf, ", `card%d`", j); |
---|
| 112 | StringBuf_Printf(&buf, ") VALUES (?, '%d', ?, '%d', ?, ?, '%lu', '%d', '%d', '%d', '%d', '%d', '%d', '%d'", |
---|
| 113 | msg->send_id, msg->dest_id, (unsigned long)msg->timestamp, msg->status, msg->zeny, msg->item.amount, msg->item.nameid, msg->item.refine, msg->item.attribute, msg->item.identify); |
---|
| 114 | for (j = 0; j < MAX_SLOTS; j++) |
---|
| 115 | StringBuf_Printf(&buf, ", '%d'", msg->item.card[j]); |
---|
| 116 | StringBuf_AppendStr(&buf, ")"); |
---|
| 117 | |
---|
| 118 | // prepare and execute query |
---|
| 119 | stmt = SqlStmt_Malloc(sql_handle); |
---|
| 120 | if( SQL_SUCCESS != SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) |
---|
| 121 | || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, msg->send_name, strnlen(msg->send_name, NAME_LENGTH)) |
---|
| 122 | || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, msg->dest_name, strnlen(msg->dest_name, NAME_LENGTH)) |
---|
| 123 | || SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, msg->title, strnlen(msg->title, MAIL_TITLE_LENGTH)) |
---|
| 124 | || SQL_SUCCESS != SqlStmt_BindParam(stmt, 3, SQLDT_STRING, msg->body, strnlen(msg->body, MAIL_BODY_LENGTH)) |
---|
| 125 | || SQL_SUCCESS != SqlStmt_Execute(stmt) ) |
---|
| 126 | { |
---|
| 127 | SqlStmt_ShowDebug(stmt); |
---|
| 128 | msg->id = 0; |
---|
| 129 | } else |
---|
| 130 | msg->id = (int)SqlStmt_LastInsertId(stmt); |
---|
| 131 | |
---|
| 132 | SqlStmt_Free(stmt); |
---|
| 133 | StringBuf_Destroy(&buf); |
---|
| 134 | |
---|
| 135 | return msg->id; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /// Retrieves a single message from the database. |
---|
| 139 | /// Returns true if the operation succeeds (or false if it fails). |
---|
| 140 | static bool mail_loadmessage(int mail_id, struct mail_message* msg) |
---|
| 141 | { |
---|
| 142 | int j; |
---|
| 143 | StringBuf buf; |
---|
| 144 | |
---|
| 145 | StringBuf_Init(&buf); |
---|
| 146 | StringBuf_AppendStr(&buf, "SELECT `id`,`send_name`,`send_id`,`dest_name`,`dest_id`,`title`,`message`,`time`,`status`," |
---|
| 147 | "`zeny`,`amount`,`nameid`,`refine`,`attribute`,`identify`"); |
---|
| 148 | for( j = 0; j < MAX_SLOTS; j++ ) |
---|
| 149 | StringBuf_Printf(&buf, ",`card%d`", j); |
---|
| 150 | StringBuf_Printf(&buf, " FROM `%s` WHERE `id` = '%d'", mail_db, mail_id); |
---|
| 151 | |
---|
| 152 | if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) |
---|
| 153 | || SQL_SUCCESS != Sql_NextRow(sql_handle) ) |
---|
| 154 | { |
---|
| 155 | Sql_ShowDebug(sql_handle); |
---|
| 156 | Sql_FreeResult(sql_handle); |
---|
| 157 | StringBuf_Destroy(&buf); |
---|
| 158 | return false; |
---|
| 159 | } |
---|
| 160 | else |
---|
| 161 | { |
---|
| 162 | char* data; |
---|
| 163 | |
---|
| 164 | Sql_GetData(sql_handle, 0, &data, NULL); msg->id = atoi(data); |
---|
| 165 | Sql_GetData(sql_handle, 1, &data, NULL); safestrncpy(msg->send_name, data, NAME_LENGTH); |
---|
| 166 | Sql_GetData(sql_handle, 2, &data, NULL); msg->send_id = atoi(data); |
---|
| 167 | Sql_GetData(sql_handle, 3, &data, NULL); safestrncpy(msg->dest_name, data, NAME_LENGTH); |
---|
| 168 | Sql_GetData(sql_handle, 4, &data, NULL); msg->dest_id = atoi(data); |
---|
| 169 | Sql_GetData(sql_handle, 5, &data, NULL); safestrncpy(msg->title, data, MAIL_TITLE_LENGTH); |
---|
| 170 | Sql_GetData(sql_handle, 6, &data, NULL); safestrncpy(msg->body, data, MAIL_BODY_LENGTH); |
---|
| 171 | Sql_GetData(sql_handle, 7, &data, NULL); msg->timestamp = atoi(data); |
---|
| 172 | Sql_GetData(sql_handle, 8, &data, NULL); msg->status = (mail_status)atoi(data); |
---|
| 173 | Sql_GetData(sql_handle, 9, &data, NULL); msg->zeny = atoi(data); |
---|
| 174 | Sql_GetData(sql_handle,10, &data, NULL); msg->item.amount = (short)atoi(data); |
---|
| 175 | Sql_GetData(sql_handle,11, &data, NULL); msg->item.nameid = atoi(data); |
---|
| 176 | Sql_GetData(sql_handle,12, &data, NULL); msg->item.refine = atoi(data); |
---|
| 177 | Sql_GetData(sql_handle,13, &data, NULL); msg->item.attribute = atoi(data); |
---|
| 178 | Sql_GetData(sql_handle,14, &data, NULL); msg->item.identify = atoi(data); |
---|
| 179 | for( j = 0; j < MAX_SLOTS; j++ ) |
---|
| 180 | { |
---|
| 181 | Sql_GetData(sql_handle,15 + j, &data, NULL); |
---|
| 182 | msg->item.card[j] = atoi(data); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | StringBuf_Destroy(&buf); |
---|
| 187 | Sql_FreeResult(sql_handle); |
---|
| 188 | |
---|
| 189 | return true; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | /*========================================== |
---|
| 193 | * Client Inbox Request |
---|
| 194 | *------------------------------------------*/ |
---|
| 195 | static void mapif_Mail_sendinbox(int fd, int char_id, unsigned char flag) |
---|
| 196 | { |
---|
| 197 | struct mail_data md; |
---|
| 198 | mail_fromsql(char_id, &md); |
---|
| 199 | |
---|
| 200 | //FIXME: dumping the whole structure like this is unsafe [ultramage] |
---|
| 201 | WFIFOHEAD(fd, sizeof(md) + 9); |
---|
| 202 | WFIFOW(fd,0) = 0x3848; |
---|
| 203 | WFIFOW(fd,2) = sizeof(md) + 9; |
---|
| 204 | WFIFOL(fd,4) = char_id; |
---|
| 205 | WFIFOB(fd,8) = flag; |
---|
| 206 | memcpy(WFIFOP(fd,9),&md,sizeof(md)); |
---|
| 207 | WFIFOSET(fd,WFIFOW(fd,2)); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | static void mapif_parse_Mail_requestinbox(int fd) |
---|
| 211 | { |
---|
| 212 | mapif_Mail_sendinbox(fd, RFIFOL(fd,2), RFIFOB(fd,6)); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /*========================================== |
---|
| 216 | * Mark mail as 'Read' |
---|
| 217 | *------------------------------------------*/ |
---|
| 218 | static void mapif_parse_Mail_read(int fd) |
---|
| 219 | { |
---|
| 220 | int mail_id = RFIFOL(fd,2); |
---|
| 221 | if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `status` = '%d' WHERE `id` = '%d'", mail_db, MAIL_READ, mail_id) ) |
---|
| 222 | Sql_ShowDebug(sql_handle); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /*========================================== |
---|
| 226 | * Client Attachment Request |
---|
| 227 | *------------------------------------------*/ |
---|
| 228 | static bool mail_DeleteAttach(int mail_id) |
---|
| 229 | { |
---|
| 230 | StringBuf buf; |
---|
| 231 | int i; |
---|
| 232 | |
---|
| 233 | StringBuf_Init(&buf); |
---|
| 234 | StringBuf_Printf(&buf, "UPDATE `%s` SET `zeny` = '0', `nameid` = '0', `amount` = '0', `refine` = '0', `attribute` = '0', `identify` = '0'", mail_db); |
---|
| 235 | for (i = 0; i < MAX_SLOTS; i++) |
---|
| 236 | StringBuf_Printf(&buf, ", `card%d` = '0'", i); |
---|
| 237 | StringBuf_Printf(&buf, " WHERE `id` = '%d'", mail_id); |
---|
| 238 | |
---|
| 239 | if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) ) |
---|
| 240 | { |
---|
| 241 | Sql_ShowDebug(sql_handle); |
---|
| 242 | StringBuf_Destroy(&buf); |
---|
| 243 | |
---|
| 244 | return false; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | StringBuf_Destroy(&buf); |
---|
| 248 | return true; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | static void mapif_Mail_getattach(int fd, int char_id, int mail_id) |
---|
| 252 | { |
---|
| 253 | struct mail_message msg; |
---|
| 254 | |
---|
| 255 | if( !mail_loadmessage(mail_id, &msg) ) |
---|
| 256 | return; |
---|
| 257 | |
---|
| 258 | if( msg.dest_id != char_id ) |
---|
| 259 | return; |
---|
| 260 | |
---|
| 261 | if( msg.status != MAIL_READ ) |
---|
| 262 | return; |
---|
| 263 | |
---|
| 264 | if( (msg.item.nameid < 1 || msg.item.amount < 1) && msg.zeny < 1 ) |
---|
| 265 | return; // No Attachment |
---|
| 266 | |
---|
| 267 | if( !mail_DeleteAttach(mail_id) ) |
---|
| 268 | return; |
---|
| 269 | |
---|
| 270 | WFIFOHEAD(fd, sizeof(struct item) + 12); |
---|
| 271 | WFIFOW(fd,0) = 0x384a; |
---|
| 272 | WFIFOW(fd,2) = sizeof(struct item) + 12; |
---|
| 273 | WFIFOL(fd,4) = char_id; |
---|
| 274 | WFIFOL(fd,8) = (msg.zeny > 0)?msg.zeny:0; |
---|
| 275 | memcpy(WFIFOP(fd,12), &msg.item, sizeof(struct item)); |
---|
| 276 | WFIFOSET(fd,WFIFOW(fd,2)); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | static void mapif_parse_Mail_getattach(int fd) |
---|
| 280 | { |
---|
| 281 | mapif_Mail_getattach(fd, RFIFOL(fd,2), RFIFOL(fd,6)); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | /*========================================== |
---|
| 285 | * Delete Mail |
---|
| 286 | *------------------------------------------*/ |
---|
| 287 | static void mapif_Mail_delete(int fd, int char_id, int mail_id) |
---|
| 288 | { |
---|
| 289 | bool failed = false; |
---|
| 290 | if ( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '%d'", mail_db, mail_id) ) |
---|
| 291 | { |
---|
| 292 | Sql_ShowDebug(sql_handle); |
---|
| 293 | failed = true; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | WFIFOHEAD(fd,11); |
---|
| 297 | WFIFOW(fd,0) = 0x384b; |
---|
| 298 | WFIFOL(fd,2) = char_id; |
---|
| 299 | WFIFOL(fd,6) = mail_id; |
---|
| 300 | WFIFOB(fd,10) = failed; |
---|
| 301 | WFIFOSET(fd,11); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | static void mapif_parse_Mail_delete(int fd) |
---|
| 305 | { |
---|
| 306 | mapif_Mail_delete(fd, RFIFOL(fd,2), RFIFOL(fd,6)); |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | /*========================================== |
---|
| 310 | * Report New Mail to Map Server |
---|
| 311 | *------------------------------------------*/ |
---|
| 312 | void mapif_Mail_new(struct mail_message *msg) |
---|
| 313 | { |
---|
| 314 | unsigned char buf[74]; |
---|
| 315 | |
---|
| 316 | if( !msg || !msg->id ) |
---|
| 317 | return; |
---|
| 318 | |
---|
| 319 | WBUFW(buf,0) = 0x3849; |
---|
| 320 | WBUFL(buf,2) = msg->dest_id; |
---|
| 321 | WBUFL(buf,6) = msg->id; |
---|
| 322 | memcpy(WBUFP(buf,10), msg->send_name, NAME_LENGTH); |
---|
| 323 | memcpy(WBUFP(buf,34), msg->title, MAIL_TITLE_LENGTH); |
---|
| 324 | mapif_sendall(buf, 74); |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | /*========================================== |
---|
| 328 | * Return Mail |
---|
| 329 | *------------------------------------------*/ |
---|
| 330 | static void mapif_Mail_return(int fd, int char_id, int mail_id) |
---|
| 331 | { |
---|
| 332 | struct mail_message msg; |
---|
| 333 | int new_mail = 0; |
---|
| 334 | |
---|
| 335 | if( mail_loadmessage(mail_id, &msg) ) |
---|
| 336 | { |
---|
| 337 | if( msg.dest_id != char_id) |
---|
| 338 | return; |
---|
| 339 | else if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '%d'", mail_db, mail_id) ) |
---|
| 340 | Sql_ShowDebug(sql_handle); |
---|
| 341 | else |
---|
| 342 | { |
---|
| 343 | char temp_[MAIL_TITLE_LENGTH]; |
---|
| 344 | |
---|
| 345 | // swap sender and receiver |
---|
| 346 | swap(msg.send_id, msg.dest_id); |
---|
| 347 | safestrncpy(temp_, msg.send_name, NAME_LENGTH); |
---|
| 348 | safestrncpy(msg.send_name, msg.dest_name, NAME_LENGTH); |
---|
| 349 | safestrncpy(msg.dest_name, temp_, NAME_LENGTH); |
---|
| 350 | |
---|
| 351 | // set reply message title |
---|
| 352 | snprintf(temp_, MAIL_TITLE_LENGTH, "RE:%s", msg.title); |
---|
| 353 | safestrncpy(msg.title, temp_, MAIL_TITLE_LENGTH); |
---|
| 354 | |
---|
| 355 | msg.status = MAIL_NEW; |
---|
| 356 | msg.timestamp = time(NULL); |
---|
| 357 | |
---|
| 358 | new_mail = mail_savemessage(&msg); |
---|
| 359 | mapif_Mail_new(&msg); |
---|
| 360 | } |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | WFIFOHEAD(fd,11); |
---|
| 364 | WFIFOW(fd,0) = 0x384c; |
---|
| 365 | WFIFOL(fd,2) = char_id; |
---|
| 366 | WFIFOL(fd,6) = mail_id; |
---|
| 367 | WFIFOB(fd,10) = (new_mail == 0); |
---|
| 368 | WFIFOSET(fd,11); |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | static void mapif_parse_Mail_return(int fd) |
---|
| 372 | { |
---|
| 373 | mapif_Mail_return(fd, RFIFOL(fd,2), RFIFOL(fd,6)); |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | /*========================================== |
---|
| 377 | * Send Mail |
---|
| 378 | *------------------------------------------*/ |
---|
| 379 | static void mapif_Mail_send(int fd, struct mail_message* msg) |
---|
| 380 | { |
---|
| 381 | int len = sizeof(struct mail_message) + 4; |
---|
| 382 | |
---|
| 383 | WFIFOHEAD(fd,len); |
---|
| 384 | WFIFOW(fd,0) = 0x384d; |
---|
| 385 | WFIFOW(fd,2) = len; |
---|
| 386 | memcpy(WFIFOP(fd,4), msg, sizeof(struct mail_message)); |
---|
| 387 | WFIFOSET(fd,len); |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | static void mapif_parse_Mail_send(int fd) |
---|
| 391 | { |
---|
| 392 | struct mail_message msg; |
---|
| 393 | char esc_name[NAME_LENGTH*2+1]; |
---|
| 394 | int account_id = 0; |
---|
| 395 | |
---|
| 396 | if(RFIFOW(fd,2) != 8 + sizeof(struct mail_message)) |
---|
| 397 | return; |
---|
| 398 | |
---|
| 399 | account_id = RFIFOL(fd,4); |
---|
| 400 | memcpy(&msg, RFIFOP(fd,8), sizeof(struct mail_message)); |
---|
| 401 | |
---|
| 402 | // Try to find the Dest Char by Name |
---|
| 403 | Sql_EscapeStringLen(sql_handle, esc_name, msg.dest_name, strnlen(msg.dest_name, NAME_LENGTH)); |
---|
| 404 | if ( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`, `char_id` FROM `%s` WHERE `name` = '%s'", char_db, esc_name) ) |
---|
| 405 | Sql_ShowDebug(sql_handle); |
---|
| 406 | else |
---|
| 407 | if ( SQL_SUCCESS == Sql_NextRow(sql_handle) ) |
---|
| 408 | { |
---|
| 409 | char *data; |
---|
| 410 | Sql_GetData(sql_handle, 0, &data, NULL); |
---|
| 411 | if (atoi(data) != account_id) |
---|
| 412 | { // Cannot send mail to char in the same account |
---|
| 413 | Sql_GetData(sql_handle, 1, &data, NULL); |
---|
| 414 | msg.dest_id = atoi(data); |
---|
| 415 | } |
---|
| 416 | } |
---|
| 417 | Sql_FreeResult(sql_handle); |
---|
| 418 | msg.status = MAIL_NEW; |
---|
| 419 | |
---|
| 420 | if( msg.dest_id > 0 ) |
---|
| 421 | msg.id = mail_savemessage(&msg); |
---|
| 422 | |
---|
| 423 | mapif_Mail_send(fd, &msg); |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | void mail_sendmail(int send_id, const char* send_name, int dest_id, const char* dest_name, const char* title, const char* body, int zeny, struct item *item) |
---|
| 427 | { |
---|
| 428 | struct mail_message msg; |
---|
| 429 | memset(&msg, 0, sizeof(struct mail_message)); |
---|
| 430 | |
---|
| 431 | msg.send_id = send_id; |
---|
| 432 | safestrncpy(msg.send_name, send_name, NAME_LENGTH); |
---|
| 433 | msg.dest_id = dest_id; |
---|
| 434 | safestrncpy(msg.dest_name, dest_name, NAME_LENGTH); |
---|
| 435 | safestrncpy(msg.title, title, MAIL_TITLE_LENGTH); |
---|
| 436 | safestrncpy(msg.body, body, MAIL_BODY_LENGTH); |
---|
| 437 | msg.zeny = zeny; |
---|
| 438 | if( item != NULL ) |
---|
| 439 | memcpy(&msg.item, item, sizeof(struct item)); |
---|
| 440 | |
---|
| 441 | msg.timestamp = time(NULL); |
---|
| 442 | |
---|
| 443 | mail_savemessage(&msg); |
---|
| 444 | mapif_Mail_new(&msg); |
---|
| 445 | } |
---|
| 446 | |
---|
| 447 | /*========================================== |
---|
| 448 | * Packets From Map Server |
---|
| 449 | *------------------------------------------*/ |
---|
| 450 | int inter_mail_parse_frommap(int fd) |
---|
| 451 | { |
---|
| 452 | switch(RFIFOW(fd,0)) |
---|
| 453 | { |
---|
| 454 | case 0x3048: mapif_parse_Mail_requestinbox(fd); break; |
---|
| 455 | case 0x3049: mapif_parse_Mail_read(fd); break; |
---|
| 456 | case 0x304a: mapif_parse_Mail_getattach(fd); break; |
---|
| 457 | case 0x304b: mapif_parse_Mail_delete(fd); break; |
---|
| 458 | case 0x304c: mapif_parse_Mail_return(fd); break; |
---|
| 459 | case 0x304d: mapif_parse_Mail_send(fd); break; |
---|
| 460 | default: |
---|
| 461 | return 0; |
---|
| 462 | } |
---|
| 463 | return 1; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | int inter_mail_sql_init(void) |
---|
| 467 | { |
---|
| 468 | return 1; |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | void inter_mail_sql_final(void) |
---|
| 472 | { |
---|
| 473 | return; |
---|
| 474 | } |
---|