summaryrefslogtreecommitdiff
path: root/src/char_sql/int_mail.c
diff options
context:
space:
mode:
authorultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec>2007-10-24 12:14:18 +0000
committerultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec>2007-10-24 12:14:18 +0000
commit3ecde465534c93315a1f88430129e8400f4d9fe2 (patch)
treefb1153e7be48da7d89b9697be733124650045fdf /src/char_sql/int_mail.c
parent04580084c4ac2113d06a23fdf2ca4596dd04011b (diff)
downloadhercules-3ecde465534c93315a1f88430129e8400f4d9fe2.tar.gz
hercules-3ecde465534c93315a1f88430129e8400f4d9fe2.tar.bz2
hercules-3ecde465534c93315a1f88430129e8400f4d9fe2.tar.xz
hercules-3ecde465534c93315a1f88430129e8400f4d9fe2.zip
* Cleaned up some messy guild code (more to come)
* Cleaned up the mail code, no more pointless dynamic allocation * Added upgrade_svn11548.sql to convert the mail table to new format * Updated vs7 and vs6 project files * Increased the max. send buffer size to 5M since 1M is not enough * Please complain if something stops working ^^; git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@11571 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/char_sql/int_mail.c')
-rw-r--r--src/char_sql/int_mail.c295
1 files changed, 132 insertions, 163 deletions
diff --git a/src/char_sql/int_mail.c b/src/char_sql/int_mail.c
index 00d79d91f..b1a1bff5f 100644
--- a/src/char_sql/int_mail.c
+++ b/src/char_sql/int_mail.c
@@ -14,15 +14,14 @@
#include <string.h>
#include <stdlib.h>
-struct mail_data *mail_data_pt = NULL;
-
-time_t calc_times(void)
+static time_t calc_times(void)
{
time_t temp = time(NULL);
return mktime(localtime(&temp));
}
-int mail_fromsql(int char_id, struct mail_data *md)
+
+static int mail_fromsql(int char_id, struct mail_data* md)
{
int i, j;
struct mail_message *msg;
@@ -80,11 +79,11 @@ int mail_fromsql(int char_id, struct mail_data *md)
Sql_FreeResult(sql_handle);
md->unchecked = 0;
- md->unreaded = 0;
+ md->unread = 0;
for (i = 0; i < md->amount; i++)
{
msg = &md->msg[i];
- if (!msg->read)
+ if( msg->read == 0 )
{
if ( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `read_flag` = '1' WHERE `id` = '%d'", mail_db, msg->id) )
Sql_ShowDebug(sql_handle);
@@ -92,7 +91,7 @@ int mail_fromsql(int char_id, struct mail_data *md)
md->unchecked++;
}
else if ( msg->read == 1 )
- md->unreaded++;
+ md->unread++;
msg->read = (msg->read < 2)?0:1;
}
@@ -101,151 +100,144 @@ int mail_fromsql(int char_id, struct mail_data *md)
return 1;
}
-int mail_savemessage(struct mail_message *msg)
+/// Stores a single message in the database.
+/// Returns the message's ID if successful (or 0 if it fails).
+static int mail_savemessage(struct mail_message* msg)
{
StringBuf buf;
+ SqlStmt* stmt;
int j;
- char esc_send_name[NAME_LENGTH*2+1], esc_dest_name[NAME_LENGTH*2+1];
- char esc_title[MAIL_TITLE_LENGTH*2+1], esc_body[MAIL_BODY_LENGTH*2+1];
-
- if (!msg)
- return 0;
-
- Sql_EscapeStringLen(sql_handle, esc_send_name, msg->send_name, strnlen(msg->send_name, NAME_LENGTH));
- Sql_EscapeStringLen(sql_handle, esc_dest_name, msg->dest_name, strnlen(msg->dest_name, NAME_LENGTH));
- Sql_EscapeStringLen(sql_handle, esc_title, msg->title, strnlen(msg->title, MAIL_TITLE_LENGTH));
- Sql_EscapeStringLen(sql_handle, esc_body, msg->body, strnlen(msg->body, MAIL_BODY_LENGTH));
+ // build message save query
StringBuf_Init(&buf);
StringBuf_Printf(&buf, "INSERT INTO `%s` (`send_name`, `send_id`, `dest_name`, `dest_id`, `title`, `message`, `time`, `read_flag`, `zeny`, `amount`, `nameid`, `refine`, `attribute`, `identify`", mail_db);
for (j = 0; j < MAX_SLOTS; j++)
StringBuf_Printf(&buf, ", `card%d`", j);
- StringBuf_Printf(&buf, ") VALUES ('%s', '%d', '%s', '%d', '%s', '%s', '%d', '0', '%d', '%d', '%d', '%d', '%d', '%d'",
- esc_send_name, msg->send_id, esc_dest_name, msg->dest_id, esc_title, esc_body, msg->timestamp, msg->zeny, msg->item.amount, msg->item.nameid, msg->item.refine, msg->item.attribute, msg->item.identify);
+ StringBuf_Printf(&buf, ") VALUES (?, '%d', ?, '%d', ?, ?, '%d', '0', '%d', '%d', '%d', '%d', '%d', '%d'",
+ msg->send_id, msg->dest_id, msg->timestamp, msg->zeny, msg->item.amount, msg->item.nameid, msg->item.refine, msg->item.attribute, msg->item.identify);
for (j = 0; j < MAX_SLOTS; j++)
StringBuf_Printf(&buf, ", '%d'", msg->item.card[j]);
StringBuf_AppendStr(&buf, ")");
- if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
+ // prepare and execute query
+ stmt = SqlStmt_Malloc(sql_handle);
+ if( SQL_SUCCESS != SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
+ || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, msg->send_name, strnlen(msg->send_name, NAME_LENGTH))
+ || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, msg->dest_name, strnlen(msg->dest_name, NAME_LENGTH))
+ || SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, msg->title, strnlen(msg->title, NAME_LENGTH))
+ || SQL_SUCCESS != SqlStmt_BindParam(stmt, 3, SQLDT_STRING, msg->body, strnlen(msg->body, NAME_LENGTH))
+ || SQL_SUCCESS != SqlStmt_Execute(stmt) )
{
Sql_ShowDebug(sql_handle);
j = 0;
- }
- else
+ } else
j = (int)Sql_LastInsertId(sql_handle);
StringBuf_Destroy(&buf);
+ // return the ID of the new mail
return j;
}
-int mail_loadmessage(int char_id, int mail_id, struct mail_message *message, short flag)
+/// Retrieves a single message from the database.
+/// Returns true if the operation succeeds (or false if it fails).
+static bool mail_loadmessage(int char_id, int mail_id, struct mail_message* msg)
{
- char *data;
- struct item *item;
- int j = 0;
+ int j;
StringBuf buf;
StringBuf_Init(&buf);
StringBuf_AppendStr(&buf, "SELECT `id`,`send_name`,`send_id`,`dest_name`,`dest_id`,`title`,`message`,`time`,`read_flag`,"
"`zeny`,`amount`,`nameid`,`refine`,`attribute`,`identify`");
- for (j = 0; j < MAX_SLOTS; j++)
+ for( j = 0; j < MAX_SLOTS; j++ )
StringBuf_Printf(&buf, ",`card%d`", j);
StringBuf_Printf(&buf, " FROM `%s` WHERE `dest_id` = '%d' AND `id` = '%d'", mail_db, char_id, mail_id);
- if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) )
+ if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf))
+ || SQL_SUCCESS != Sql_NextRow(sql_handle) )
+ {
Sql_ShowDebug(sql_handle);
- else if( Sql_NumRows(sql_handle) == 0 )
- ShowWarning("Char %d trying to read an invalid mail.\n", char_id);
+ Sql_FreeResult(sql_handle);
+ StringBuf_Destroy(&buf);
+ return false;
+ }
else
{
- Sql_NextRow(sql_handle);
-
- Sql_GetData(sql_handle, 0, &data, NULL); message->id = atoi(data);
- Sql_GetData(sql_handle, 1, &data, NULL); safestrncpy(message->send_name, data, NAME_LENGTH);
- Sql_GetData(sql_handle, 2, &data, NULL); message->send_id = atoi(data);
- Sql_GetData(sql_handle, 3, &data, NULL); safestrncpy(message->dest_name, data, NAME_LENGTH);
- Sql_GetData(sql_handle, 4, &data, NULL); message->dest_id = atoi(data);
- Sql_GetData(sql_handle, 5, &data, NULL); safestrncpy(message->title, data, MAIL_TITLE_LENGTH);
- Sql_GetData(sql_handle, 6, &data, NULL); safestrncpy(message->body, data, MAIL_BODY_LENGTH);
- Sql_GetData(sql_handle, 7, &data, NULL); message->timestamp = atoi(data);
- Sql_GetData(sql_handle, 8, &data, NULL); message->read = atoi(data);
- Sql_GetData(sql_handle, 9, &data, NULL); message->zeny = atoi(data);
- item = &message->item;
- Sql_GetData(sql_handle,10, &data, NULL); item->amount = (short)atoi(data);
- Sql_GetData(sql_handle,11, &data, NULL); item->nameid = atoi(data);
- Sql_GetData(sql_handle,12, &data, NULL); item->refine = atoi(data);
- Sql_GetData(sql_handle,13, &data, NULL); item->attribute = atoi(data);
- Sql_GetData(sql_handle,14, &data, NULL); item->identify = atoi(data);
- for (j = 0; j < MAX_SLOTS; j++)
+ char* data;
+
+ Sql_GetData(sql_handle, 0, &data, NULL); msg->id = atoi(data);
+ Sql_GetData(sql_handle, 1, &data, NULL); safestrncpy(msg->send_name, data, NAME_LENGTH);
+ Sql_GetData(sql_handle, 2, &data, NULL); msg->send_id = atoi(data);
+ Sql_GetData(sql_handle, 3, &data, NULL); safestrncpy(msg->dest_name, data, NAME_LENGTH);
+ Sql_GetData(sql_handle, 4, &data, NULL); msg->dest_id = atoi(data);
+ Sql_GetData(sql_handle, 5, &data, NULL); safestrncpy(msg->title, data, MAIL_TITLE_LENGTH);
+ Sql_GetData(sql_handle, 6, &data, NULL); safestrncpy(msg->body, data, MAIL_BODY_LENGTH);
+ Sql_GetData(sql_handle, 7, &data, NULL); msg->timestamp = atoi(data);
+ Sql_GetData(sql_handle, 8, &data, NULL); msg->read = atoi(data);
+ Sql_GetData(sql_handle, 9, &data, NULL); msg->zeny = atoi(data);
+ Sql_GetData(sql_handle,10, &data, NULL); msg->item.amount = (short)atoi(data);
+ Sql_GetData(sql_handle,11, &data, NULL); msg->item.nameid = atoi(data);
+ Sql_GetData(sql_handle,12, &data, NULL); msg->item.refine = atoi(data);
+ Sql_GetData(sql_handle,13, &data, NULL); msg->item.attribute = atoi(data);
+ Sql_GetData(sql_handle,14, &data, NULL); msg->item.identify = atoi(data);
+ for( j = 0; j < MAX_SLOTS; j++ )
{
Sql_GetData(sql_handle,15 + j, &data, NULL);
- item->card[j] = atoi(data);
+ msg->item.card[j] = atoi(data);
}
-
- j = 1;
}
StringBuf_Destroy(&buf);
Sql_FreeResult(sql_handle);
- if (message->read == 1)
+ ShowDebug("Loaded message (had read flag %d)\n", msg->read);
+ if (msg->read == 1)
{
- message->read = 0;
- if (flag)
- if ( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `read_flag` = '2' WHERE `id` = '%d'", mail_db, message->id) )
- Sql_ShowDebug(sql_handle);
+ msg->read = 0;
}
else
- message->read = 1;
+ msg->read = 1;
- return j;
+ return true;
}
/*==========================================
* Client Inbox Request
*------------------------------------------*/
-int mapif_Mail_sendinbox(int fd, int char_id, unsigned char flag)
+static void mapif_Mail_sendinbox(int fd, int char_id, unsigned char flag)
{
- WFIFOHEAD(fd, sizeof(struct mail_data) + 9);
- mail_fromsql(char_id, mail_data_pt);
+ struct mail_data md;
+ mail_fromsql(char_id, &md);
+
+ //FIXME: dumping the whole structure like this is unsafe [ultramage]
+ WFIFOHEAD(fd, sizeof(md) + 9);
WFIFOW(fd,0) = 0x3848;
- WFIFOW(fd,2) = sizeof(struct mail_data) + 9;
+ WFIFOW(fd,2) = sizeof(md) + 9;
WFIFOL(fd,4) = char_id;
WFIFOB(fd,8) = flag;
- memcpy(WFIFOP(fd,9),mail_data_pt,sizeof(struct mail_data));
+ memcpy(WFIFOP(fd,9),&md,sizeof(md));
WFIFOSET(fd,WFIFOW(fd,2));
-
- return 0;
}
-int mapif_parse_Mail_requestinbox(int fd)
+static void mapif_parse_Mail_requestinbox(int fd)
{
- RFIFOHEAD(fd);
mapif_Mail_sendinbox(fd, RFIFOL(fd,2), RFIFOB(fd,6));
-
- return 0;
}
/*==========================================
- * Mail Readed Mark
+ * 'Mail read' Mark
*------------------------------------------*/
-int mapif_parse_Mail_read(int fd)
+static void mapif_parse_Mail_read(int fd)
{
- int mail_id;
- RFIFOHEAD(fd);
-
- mail_id = RFIFOL(fd,2);
+ int mail_id = RFIFOL(fd,2);
if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `read_flag` = '2' WHERE `id` = '%d'", mail_db, mail_id) )
Sql_ShowDebug(sql_handle);
-
- return 0;
}
/*==========================================
* Client Attachment Request
*------------------------------------------*/
-int mail_DeleteAttach(int mail_id)
+static bool mail_DeleteAttach(int mail_id)
{
StringBuf buf;
int i;
@@ -261,86 +253,74 @@ int mail_DeleteAttach(int mail_id)
Sql_ShowDebug(sql_handle);
StringBuf_Destroy(&buf);
- return 0;
+ return false;
}
StringBuf_Destroy(&buf);
- return 1;
+ return true;
}
-int mapif_Mail_getattach(int fd, int char_id, int mail_id)
+static void mapif_Mail_getattach(int fd, int char_id, int mail_id)
{
- struct mail_message *message = (struct mail_message*)aCalloc(sizeof(struct mail_message), 1);
+ struct mail_message msg;
- if( mail_loadmessage(char_id, mail_id, message, 0) )
- {
- if( (message->item.nameid < 1 || message->item.amount < 1) && message->zeny < 1 )
- {
- aFree(message);
- return 0; // No Attachment
- }
+ if( !mail_loadmessage(char_id, mail_id, &msg) )
+ return;
- if( mail_DeleteAttach(mail_id) )
- {
- WFIFOHEAD(fd, sizeof(struct item) + 12);
- WFIFOW(fd,0) = 0x384a;
- WFIFOW(fd,2) = sizeof(struct item) + 12;
- WFIFOL(fd,4) = char_id;
- WFIFOL(fd,8) = (message->zeny > 0)?message->zeny:0;
- memcpy(WFIFOP(fd,12), &message->item, sizeof(struct item));
- WFIFOSET(fd,WFIFOW(fd,2));
- }
- }
+ if( (msg.item.nameid < 1 || msg.item.amount < 1) && msg.zeny < 1 )
+ return; // No Attachment
+
+ if( !mail_DeleteAttach(mail_id) )
+ return;
- aFree(message);
- return 0;
+ WFIFOHEAD(fd, sizeof(struct item) + 12);
+ WFIFOW(fd,0) = 0x384a;
+ WFIFOW(fd,2) = sizeof(struct item) + 12;
+ WFIFOL(fd,4) = char_id;
+ WFIFOL(fd,8) = (msg.zeny > 0)?msg.zeny:0;
+ memcpy(WFIFOP(fd,12), &msg.item, sizeof(struct item));
+ WFIFOSET(fd,WFIFOW(fd,2));
}
-int mapif_parse_Mail_getattach(int fd)
+static void mapif_parse_Mail_getattach(int fd)
{
- RFIFOHEAD(fd);
mapif_Mail_getattach(fd, RFIFOL(fd,2), RFIFOL(fd,6));
- return 0;
}
/*==========================================
* Delete Mail
*------------------------------------------*/
-int mapif_Mail_delete(int fd, int char_id, int mail_id)
+static void mapif_Mail_delete(int fd, int char_id, int mail_id)
{
- short flag = 0;
-
+ bool failed = false;
if ( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '%d'", mail_db, mail_id) )
{
Sql_ShowDebug(sql_handle);
- flag = 1;
+ failed = true;
}
WFIFOHEAD(fd,11);
WFIFOW(fd,0) = 0x384b;
WFIFOL(fd,2) = char_id;
WFIFOL(fd,6) = mail_id;
- WFIFOW(fd,10) = flag;
- WFIFOSET(fd,12);
-
- return 0;
+ WFIFOB(fd,10) = failed;
+ WFIFOSET(fd,11);
}
-int mapif_parse_Mail_delete(int fd)
+static void mapif_parse_Mail_delete(int fd)
{
- RFIFOHEAD(fd);
mapif_Mail_delete(fd, RFIFOL(fd,2), RFIFOL(fd,6));
- return 0;
}
+
/*==========================================
* Return Mail
*------------------------------------------*/
-int mapif_Mail_return(int fd, int char_id, int mail_id)
+static void mapif_Mail_return(int fd, int char_id, int mail_id)
{
- struct mail_message *msg = (struct mail_message*)aCalloc(sizeof(struct mail_message), 1);
+ struct mail_message msg;
int new_mail = 0;
- if( mail_loadmessage(char_id, mail_id, msg, 0) )
+ if( mail_loadmessage(char_id, mail_id, &msg) )
{
if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '%d'", mail_db, mail_id) )
Sql_ShowDebug(sql_handle);
@@ -348,97 +328,88 @@ int mapif_Mail_return(int fd, int char_id, int mail_id)
{
char temp_[MAIL_TITLE_LENGTH];
- swap(msg->send_id, msg->dest_id);
- safestrncpy(temp_, msg->send_name, NAME_LENGTH);
- safestrncpy(msg->send_name, msg->dest_name, NAME_LENGTH);
- safestrncpy(msg->dest_name, temp_, NAME_LENGTH);
+ // swap sender and receiver
+ swap(msg.send_id, msg.dest_id);
+ safestrncpy(temp_, msg.send_name, NAME_LENGTH);
+ safestrncpy(msg.send_name, msg.dest_name, NAME_LENGTH);
+ safestrncpy(msg.dest_name, temp_, NAME_LENGTH);
+
+ // set reply message title
+ snprintf(temp_, MAIL_TITLE_LENGTH, "RE:%s", msg.title);
+ safestrncpy(msg.title, temp_, MAIL_TITLE_LENGTH);
- snprintf(temp_, MAIL_TITLE_LENGTH, "RE:%s", msg->title);
- safestrncpy(msg->title, temp_, MAIL_TITLE_LENGTH);
- msg->timestamp = (unsigned int)calc_times();
+ msg.timestamp = (unsigned int)calc_times();
- new_mail = mail_savemessage(msg);
+ new_mail = mail_savemessage(&msg);
}
}
- aFree(msg);
-
WFIFOHEAD(fd,14);
WFIFOW(fd,0) = 0x384c;
WFIFOL(fd,2) = char_id;
WFIFOL(fd,6) = mail_id;
WFIFOL(fd,10) = new_mail;
WFIFOSET(fd,14);
-
- return 0;
}
-int mapif_parse_Mail_return(int fd)
+static void mapif_parse_Mail_return(int fd)
{
- RFIFOHEAD(fd);
mapif_Mail_return(fd, RFIFOL(fd,2), RFIFOL(fd,6));
- return 0;
}
-int mapif_Mail_send(int fd, struct mail_message *msg)
+static void mapif_Mail_send(int fd, struct mail_message* msg)
{
- int len = strlen(msg->title) + 16;
+ int len = strlen(msg->title);
WFIFOHEAD(fd,len);
WFIFOW(fd,0) = 0x384d;
- WFIFOW(fd,2) = len;
+ WFIFOW(fd,2) = len + 16;
WFIFOL(fd,4) = msg->send_id;
WFIFOL(fd,8) = msg->id;
WFIFOL(fd,12) = msg->dest_id;
- memcpy(WFIFOP(fd,16), msg->title, strlen(msg->title));
- WFIFOSET(fd,len);
-
- return 0;
+ safestrncpy((char*)WFIFOP(fd,16), msg->title, len);
+ WFIFOSET(fd,WFIFOW(fd,2));
}
-int mapif_parse_Mail_send(int fd)
+static void mapif_parse_Mail_send(int fd)
{
- struct mail_message *msg;
+ struct mail_message msg;
int mail_id = 0, account_id = 0;
if(RFIFOW(fd,2) != 8 + sizeof(struct mail_message))
- return 0;
+ return;
- msg = (struct mail_message*)aCalloc(sizeof(struct mail_message), 1);
- memcpy(msg, RFIFOP(fd,8), sizeof(struct mail_message));
+ memcpy(&msg, RFIFOP(fd,8), sizeof(struct mail_message));
account_id = RFIFOL(fd,4);
- if( !msg->dest_id )
+ if( !msg.dest_id )
{
// Try to find the Dest Char by Name
char esc_name[NAME_LENGTH*2+1];
- Sql_EscapeStringLen(sql_handle, esc_name, msg->dest_name, strnlen(msg->dest_name, NAME_LENGTH));
+ Sql_EscapeStringLen(sql_handle, esc_name, msg.dest_name, strnlen(msg.dest_name, NAME_LENGTH));
if ( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`, `char_id` FROM `%s` WHERE `name` = '%s'", char_db, esc_name) )
Sql_ShowDebug(sql_handle);
- else if ( Sql_NumRows(sql_handle) > 0 )
+ else
+ if ( SQL_SUCCESS == Sql_NextRow(sql_handle) )
{
char *data;
- Sql_NextRow(sql_handle);
Sql_GetData(sql_handle, 0, &data, NULL);
if (atoi(data) != account_id)
- { // Cannot sends mail to char in the same account
+ { // Cannot send mail to char in the same account
Sql_GetData(sql_handle, 1, &data, NULL);
- msg->dest_id = atoi(data);
+ msg.dest_id = atoi(data);
}
}
Sql_FreeResult(sql_handle);
}
- if( msg->dest_id > 0 )
- mail_id = mail_savemessage(msg);
-
- msg->id = mail_id;
- mapif_Mail_send(fd, msg);
+ if( msg.dest_id > 0 )
+ mail_id = mail_savemessage(&msg);
- aFree(msg);
- return 0;
+ msg.id = mail_id;
+ mapif_Mail_send(fd, &msg);
}
/*==========================================
@@ -462,12 +433,10 @@ int inter_mail_parse_frommap(int fd)
int inter_mail_sql_init(void)
{
- mail_data_pt = (struct mail_data*)aCalloc(sizeof(struct mail_data), 1);
return 1;
}
void inter_mail_sql_final(void)
{
- if (mail_data_pt) aFree(mail_data_pt);
return;
}