summaryrefslogtreecommitdiff
path: root/src/char/inter.c
diff options
context:
space:
mode:
authorgepard1984 <gepard1984@54d463be-8e91-2dee-dedb-b68131a5f0ec>2012-01-20 20:33:32 +0000
committergepard1984 <gepard1984@54d463be-8e91-2dee-dedb-b68131a5f0ec>2012-01-20 20:33:32 +0000
commit44d7606656a650dc43018b5c63bb56ad1f70e77c (patch)
treea34a45286714803911fe2bf55315c611a444e605 /src/char/inter.c
parenta02514bc54c044f141520118ffd7e0b6ff5ecae2 (diff)
downloadhercules-44d7606656a650dc43018b5c63bb56ad1f70e77c.tar.gz
hercules-44d7606656a650dc43018b5c63bb56ad1f70e77c.tar.bz2
hercules-44d7606656a650dc43018b5c63bb56ad1f70e77c.tar.xz
hercules-44d7606656a650dc43018b5c63bb56ad1f70e77c.zip
Merged TXT removal branch back to trunk.
* TXT save engine is removed and no longer supported. * See also tid:53926, tid:57717. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@15503 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/char/inter.c')
-rw-r--r--src/char/inter.c762
1 files changed, 384 insertions, 378 deletions
diff --git a/src/char/inter.c b/src/char/inter.c
index 298ed38a5..2137579be 100644
--- a/src/char/inter.c
+++ b/src/char/inter.c
@@ -1,300 +1,327 @@
// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder
-#include "../common/db.h"
#include "../common/mmo.h"
-#include "../common/socket.h"
-#include "../common/timer.h"
+#include "../common/db.h"
#include "../common/malloc.h"
-#include "../common/lock.h"
-#include "../common/showmsg.h"
#include "../common/strlib.h"
+#include "../common/showmsg.h"
+#include "../common/socket.h"
+#include "../common/timer.h"
#include "char.h"
#include "inter.h"
#include "int_party.h"
#include "int_guild.h"
-#include "int_status.h"
#include "int_storage.h"
#include "int_pet.h"
#include "int_homun.h"
+#include "int_mercenary.h"
+#include "int_mail.h"
+#include "int_auction.h"
+#include "int_quest.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
-#define WISDATA_TTL (60*1000) // Existence time of Wisp/page data (60 seconds)
- // that is the waiting time of answers of all map-servers
-#define WISDELLIST_MAX 256 // Number of elements of Wisp/page data deletion list
+#define WISDATA_TTL (60*1000) // Wisデータの生存時間(60秒)
+#define WISDELLIST_MAX 256 // Wisデータ削除リストの要素数
-char accreg_txt[1024] = "save/accreg.txt";
-#ifndef TXT_SQL_CONVERT
-char inter_log_filename[1024] = "log/inter.log";
-char main_chat_nick[16] = "Main";
-static DBMap* accreg_db = NULL; // int account_id -> struct accreg*
+Sql* sql_handle = NULL;
+
+int char_server_port = 3306;
+char char_server_ip[32] = "127.0.0.1";
+char char_server_id[32] = "ragnarok";
+char char_server_pw[32] = "ragnarok";
+char char_server_db[32] = "ragnarok";
+char default_codepage[32] = ""; //Feature by irmin.
+static struct accreg *accreg_pt;
unsigned int party_share_level = 10;
+char main_chat_nick[16] = "Main";
-// sending packet list
-// NOTE: This variable ain't used at all! And it's confusing.. where do I add that the length of packet 0x2b07 is 10? x.x [Skotlex]
-int inter_send_packet_length[]={
- -1,-1,27,-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3000-0x300f
- -1, 7, 0, 0, 0, 0, 0, 0, -1,11, 0, 0, 0, 0, 0, 0,
- 35,-1,11,15, 34,29, 7,-1, 0, 0, 0, 0, 0, 0, 0, 0,
- 10,-1,15, 0, 79,19, 7,-1, 0,-1,-1,-1, 14,67,186,-1,
- 9, 9,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 11,-1, 7, 3, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-};
// recv. packet list
-int inter_recv_packet_length[]={
- -1,-1, 7,-1, -1,13,36, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3000-0x300f
- 6,-1, 0, 0, 0, 0, 0, 0, 10,-1, 0, 0, 0, 0, 0, 0, //0x3010-0x301f
- -1,10,-1,14, 14,19, 6,-1, 14,14, 0, 0, 0, 0, 0, 0, //0x3020-0x302f Party
- -1, 6,-1,-1, 55,19, 6,-1, 14,-1,-1,-1, 18,19,186,-1, //0x3030-0x303f
- 5, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3040-0x304f
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3050-0x305f
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3060-0x306f
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3070-0x307f
- 48,14,-1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3080-0x308f
- -1,10,-1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3090-0x309f Homunculus packets [albator]
+int inter_recv_packet_length[] = {
+ -1,-1, 7,-1, -1,13,36, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3000-
+ 6,-1, 0, 0, 0, 0, 0, 0, 10,-1, 0, 0, 0, 0, 0, 0, // 3010-
+ -1,10,-1,14, 14,19, 6,-1, 14,14, 0, 0, 0, 0, 0, 0, // 3020- Party
+ -1, 6,-1,-1, 55,19, 6,-1, 14,-1,-1,-1, 18,19,186,-1, // 3030-
+ 5, 9, 0, 0, 0, 0, 0, 0, 7, 6,10,10, 10,-1, 0, 0, // 3040-
+ -1,-1,10,10, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3050- Auction System [Zephyrus]
+ 6,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3060- Quest system [Kevin] [Inkfish]
+ -1,10, 6,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3070- Mercenary packets [Zephyrus]
+ 48,14,-1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3080-
+ -1,10,-1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3090- Homunculus packets [albator]
};
struct WisData {
int id, fd, count, len;
unsigned long tick;
- unsigned char src[24], dst[24], msg[1024];
+ unsigned char src[24], dst[24], msg[512];
};
static DBMap* wis_db = NULL; // int wis_id -> struct WisData*
static int wis_dellist[WISDELLIST_MAX], wis_delnum;
-
//--------------------------------------------------------
+// Save registry to sql
+int inter_accreg_tosql(int account_id, int char_id, struct accreg* reg, int type)
+{
+ struct global_reg* r;
+ SqlStmt* stmt;
+ int i;
-// アカウント変数を文字列へ変換
-int inter_accreg_tostr(char *str, struct accreg *reg) {
- int j;
- char *p = str;
-
- p += sprintf(p, "%d\t", reg->account_id);
- for(j = 0; j < reg->reg_num; j++) {
- p += sprintf(p,"%s,%s ", reg->reg[j].str, reg->reg[j].value);
- }
-
- return 0;
-}
-#endif //TXT_SQL_CONVERT
-// アカウント変数を文字列から変換
-int inter_accreg_fromstr(const char *str, struct accreg *reg) {
- int j, n;
- const char *p = str;
-
- if (sscanf(p, "%d\t%n", &reg->account_id, &n ) != 1 || reg->account_id <= 0)
- return 1;
+ if( account_id <= 0 )
+ return 0;
+ reg->account_id = account_id;
+ reg->char_id = char_id;
- for(j = 0, p += n; j < ACCOUNT_REG_NUM; j++, p += n) {
- if (sscanf(p, "%[^,],%[^ ] %n", reg->reg[j].str, reg->reg[j].value, &n) != 2)
- break;
+ //`global_reg_value` (`type`, `account_id`, `char_id`, `str`, `value`)
+ switch( type )
+ {
+ case 3: //Char Reg
+ if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `type`=3 AND `char_id`='%d'", reg_db, char_id) )
+ Sql_ShowDebug(sql_handle);
+ account_id = 0;
+ break;
+ case 2: //Account Reg
+ if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `type`=2 AND `account_id`='%d'", reg_db, account_id) )
+ Sql_ShowDebug(sql_handle);
+ char_id = 0;
+ break;
+ case 1: //Account2 Reg
+ ShowError("inter_accreg_tosql: Char server shouldn't handle type 1 registry values (##). That is the login server's work!\n");
+ return 0;
+ default:
+ ShowError("inter_accreg_tosql: Invalid type %d\n", type);
+ return 0;
}
- reg->reg_num = j;
- return 0;
-}
-#ifndef TXT_SQL_CONVERT
-// アカウント変数の読み込み
-int inter_accreg_init(void) {
- char line[8192];
- FILE *fp;
- int c = 0;
- struct accreg *reg;
-
- accreg_db = idb_alloc(DB_OPT_RELEASE_DATA);
+ if( reg->reg_num <= 0 )
+ return 0;
- if( (fp = fopen(accreg_txt, "r")) == NULL)
- return 1;
- while(fgets(line, sizeof(line), fp))
+ stmt = SqlStmt_Malloc(sql_handle);
+ if( SQL_ERROR == SqlStmt_Prepare(stmt, "INSERT INTO `%s` (`type`, `account_id`, `char_id`, `str`, `value`) VALUES ('%d','%d','%d',?,?)", reg_db, type, account_id, char_id) )
+ SqlStmt_ShowDebug(stmt);
+ for( i = 0; i < reg->reg_num; ++i )
{
- reg = (struct accreg*)aCalloc(sizeof(struct accreg), 1);
- if (reg == NULL) {
- ShowFatalError("inter: accreg: out of memory!\n");
- exit(EXIT_FAILURE);
+ r = &reg->reg[i];
+ if( r->str[0] != '\0' && r->value != '\0' )
+ {
+ // str
+ SqlStmt_BindParam(stmt, 0, SQLDT_STRING, r->str, strnlen(r->str, sizeof(r->str)));
+ // value
+ SqlStmt_BindParam(stmt, 1, SQLDT_STRING, r->value, strnlen(r->value, sizeof(r->value)));
+
+ if( SQL_ERROR == SqlStmt_Execute(stmt) )
+ SqlStmt_ShowDebug(stmt);
}
- if (inter_accreg_fromstr(line, reg) == 0 && reg->account_id > 0) {
- idb_put(accreg_db, reg->account_id, reg);
- } else {
- ShowError("inter: accreg: broken data [%s] line %d\n", accreg_txt, c);
- aFree(reg);
- }
- c++;
}
- fclose(fp);
-
- return 0;
+ SqlStmt_Free(stmt);
+ return 1;
}
-// アカウント変数のセーブ用
-int inter_accreg_save_sub(DBKey key, void *data, va_list ap) {
- char line[8192];
- FILE *fp;
- struct accreg *reg = (struct accreg *)data;
-
- if (reg->reg_num > 0) {
- inter_accreg_tostr(line,reg);
- fp = va_arg(ap, FILE *);
- fprintf(fp, "%s\n", line);
- }
+// Load account_reg from sql (type=2)
+int inter_accreg_fromsql(int account_id,int char_id, struct accreg *reg, int type)
+{
+ struct global_reg* r;
+ char* data;
+ size_t len;
+ int i;
- return 0;
-}
+ if( reg == NULL)
+ return 0;
-// アカウント変数のセーブ
-int inter_accreg_save(void) {
- FILE *fp;
- int lock;
+ memset(reg, 0, sizeof(struct accreg));
+ reg->account_id = account_id;
+ reg->char_id = char_id;
- if ((fp = lock_fopen(accreg_txt,&lock)) == NULL) {
- ShowError("int_accreg: can't write [%s] !!! data is lost !!!\n", accreg_txt);
- return 1;
+ //`global_reg_value` (`type`, `account_id`, `char_id`, `str`, `value`)
+ switch( type )
+ {
+ case 3: //char reg
+ if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `str`, `value` FROM `%s` WHERE `type`=3 AND `char_id`='%d'", reg_db, char_id) )
+ Sql_ShowDebug(sql_handle);
+ break;
+ case 2: //account reg
+ if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `str`, `value` FROM `%s` WHERE `type`=2 AND `account_id`='%d'", reg_db, account_id) )
+ Sql_ShowDebug(sql_handle);
+ break;
+ case 1: //account2 reg
+ ShowError("inter_accreg_fromsql: Char server shouldn't handle type 1 registry values (##). That is the login server's work!\n");
+ return 0;
+ default:
+ ShowError("inter_accreg_fromsql: Invalid type %d\n", type);
+ return 0;
+ }
+ for( i = 0; i < MAX_REG_NUM && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
+ {
+ r = &reg->reg[i];
+ // str
+ Sql_GetData(sql_handle, 0, &data, &len);
+ memcpy(r->str, data, min(len, sizeof(r->str)));
+ // value
+ Sql_GetData(sql_handle, 1, &data, &len);
+ memcpy(r->value, data, min(len, sizeof(r->value)));
}
- accreg_db->foreach(accreg_db, inter_accreg_save_sub,fp);
- lock_fclose(fp, accreg_txt, &lock);
+ reg->reg_num = i;
+ Sql_FreeResult(sql_handle);
+ return 1;
+}
+// Initialize
+int inter_accreg_sql_init(void)
+{
+ CREATE(accreg_pt, struct accreg, 1);
return 0;
+
}
-//--------------------------------------------------------
-#endif //TXT_SQL_CONVERT
/*==========================================
- * 設定ファイルを読み込む
+ * read config file
*------------------------------------------*/
-static int inter_config_read(const char *cfgName) {
+static int inter_config_read(const char* cfgName)
+{
+ int i;
char line[1024], w1[1024], w2[1024];
- FILE *fp;
+ FILE* fp;
fp = fopen(cfgName, "r");
- if (fp == NULL) {
+ if(fp == NULL) {
ShowError("file not found: %s\n", cfgName);
return 1;
}
+
+ ShowInfo("reading file %s...\n", cfgName);
+
while(fgets(line, sizeof(line), fp))
{
- if (line[0] == '/' && line[1] == '/')
+ i = sscanf(line, "%[^:]: %[^\r\n]", w1, w2);
+ if(i != 2)
continue;
- if (sscanf(line,"%[^:]: %[^\r\n]", w1, w2) != 2)
- continue;
-
- if (strcmpi(w1, "storage_txt") == 0) {
- strncpy(storage_txt, w2, sizeof(storage_txt));
- } else if (strcmpi(w1, "party_txt") == 0) {
- strncpy(party_txt, w2, sizeof(party_txt));
- } else if (strcmpi(w1, "pet_txt") == 0) {
- strncpy(pet_txt, w2, sizeof(pet_txt));
- } else if (strcmpi(w1, "accreg_txt") == 0) {
- strncpy(accreg_txt, w2, sizeof(accreg_txt));
- } else if (strcmpi(w1, "guild_txt") == 0) {
- strncpy(guild_txt, w2, sizeof(guild_txt));
- } else if (strcmpi(w1, "castle_txt") == 0) {
- strncpy(castle_txt, w2, sizeof(castle_txt));
- } else if (strcmpi(w1, "guild_storage_txt") == 0) {
- strncpy(guild_storage_txt, w2, sizeof(guild_storage_txt));
-#ifndef TXT_SQL_CONVERT
- } else if (strcmpi(w1, "homun_txt") == 0) {
- strncpy(homun_txt, w2, sizeof(homun_txt));
- } else if (strcmpi(w1, "party_share_level") == 0) {
- party_share_level = (unsigned int)atof(w2);
- } else if (strcmpi(w1, "inter_log_filename") == 0) {
- strncpy(inter_log_filename, w2, sizeof(inter_log_filename));
- } else if(strcmpi(w1,"log_inter")==0) {
+ if(!strcmpi(w1,"char_server_ip")) {
+ strcpy(char_server_ip,w2);
+ ShowStatus ("set char_server_ip : %s\n", w2);
+ } else
+ if(!strcmpi(w1,"char_server_port")) {
+ char_server_port = atoi(w2);
+ ShowStatus ("set char_server_port : %s\n", w2);
+ } else
+ if(!strcmpi(w1,"char_server_id")) {
+ strcpy(char_server_id,w2);
+ ShowStatus ("set char_server_id : %s\n", w2);
+ } else
+ if(!strcmpi(w1,"char_server_pw")) {
+ strcpy(char_server_pw,w2);
+ ShowStatus ("set char_server_pw : %s\n", w2);
+ } else
+ if(!strcmpi(w1,"char_server_db")) {
+ strcpy(char_server_db,w2);
+ ShowStatus ("set char_server_db : %s\n", w2);
+ } else
+ if(!strcmpi(w1,"default_codepage")) {
+ strcpy(default_codepage,w2);
+ ShowStatus ("set default_codepage : %s\n", w2);
+ }
+ else if(!strcmpi(w1,"party_share_level"))
+ party_share_level = atoi(w2);
+ else if(!strcmpi(w1,"log_inter"))
log_inter = atoi(w2);
- } else if(strcmpi(w1, "main_chat_nick")==0){ // Main chat nick [LuzZza]
+ else if(!strcmpi(w1,"main_chat_nick"))
safestrncpy(main_chat_nick, w2, sizeof(main_chat_nick));
-#endif //TXT_SQL_CONVERT
- } else if (strcmpi(w1, "import") == 0) {
+ else if(!strcmpi(w1,"import"))
inter_config_read(w2);
- }
}
fclose(fp);
+ ShowInfo ("done reading %s.\n", cfgName);
+
return 0;
}
-#ifndef TXT_SQL_CONVERT
-// ログ書き出し
-int inter_log(char *fmt,...) {
- FILE *logfp;
+
+// Save interlog into sql
+int inter_log(char* fmt, ...)
+{
+ char str[255];
+ char esc_str[sizeof(str)*2+1];// escaped str
va_list ap;
va_start(ap,fmt);
- logfp = fopen(inter_log_filename, "a");
- if (logfp) {
- vfprintf(logfp, fmt, ap);
- fclose(logfp);
- }
+ vsnprintf(str, sizeof(str), fmt, ap);
va_end(ap);
- return 0;
-}
-
-// セーブ
-int inter_save(void) {
-#ifdef ENABLE_SC_SAVING
- inter_status_save();
-#endif
- inter_party_save();
- inter_guild_save();
- inter_storage_save();
- inter_guild_storage_save();
- inter_pet_save();
- inter_homun_save();
- inter_accreg_save();
+ Sql_EscapeStringLen(sql_handle, esc_str, str, strnlen(str, sizeof(str)));
+ if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`time`, `log`) VALUES (NOW(), '%s')", interlog_db, esc_str) )
+ Sql_ShowDebug(sql_handle);
return 0;
}
-#endif //TXT_SQL_CONVERT
-// 初期化
-int inter_init_txt(const char *file)
+// initialize
+int inter_init_sql(const char *file)
{
+ //int i;
+
+ ShowInfo ("interserver initialize...\n");
inter_config_read(file);
-#ifndef TXT_SQL_CONVERT
+ //DB connection initialized
+ sql_handle = Sql_Malloc();
+ ShowInfo("Connect Character DB server.... (Character Server)\n");
+ if( SQL_ERROR == Sql_Connect(sql_handle, char_server_id, char_server_pw, char_server_ip, (uint16)char_server_port, char_server_db) )
+ {
+ Sql_ShowDebug(sql_handle);
+ Sql_Free(sql_handle);
+ exit(EXIT_FAILURE);
+ }
+
+ if( *default_codepage ) {
+ if( SQL_ERROR == Sql_SetEncoding(sql_handle, default_codepage) )
+ Sql_ShowDebug(sql_handle);
+ }
+
wis_db = idb_alloc(DB_OPT_RELEASE_DATA);
+ inter_guild_sql_init();
+ inter_storage_sql_init();
+ inter_party_sql_init();
+ inter_pet_sql_init();
+ inter_homunculus_sql_init();
+ inter_mercenary_sql_init();
+ inter_accreg_sql_init();
+ inter_mail_sql_init();
+ inter_auction_sql_init();
- inter_party_init();
- inter_guild_init();
- inter_storage_init();
- inter_pet_init();
- inter_homun_init();
- inter_accreg_init();
-#endif //TXT_SQL_CONVERT
return 0;
}
-#ifndef TXT_SQL_CONVERT
+
// finalize
-void inter_final(void) {
- accreg_db->destroy(accreg_db, NULL);
+void inter_final(void)
+{
wis_db->destroy(wis_db, NULL);
- inter_party_final();
- inter_guild_final();
- inter_storage_final();
- inter_pet_final();
- inter_homun_final();
-
+ inter_guild_sql_final();
+ inter_storage_sql_final();
+ inter_party_sql_final();
+ inter_pet_sql_final();
+ inter_homunculus_sql_final();
+ inter_mercenary_sql_final();
+ inter_mail_sql_final();
+ inter_auction_sql_final();
+
+ if (accreg_pt) aFree(accreg_pt);
return;
}
-// マップサーバー接続
-int inter_mapif_init(int fd) {
+int inter_mapif_init(int fd)
+{
inter_guild_mapif_init(fd);
return 0;
}
+
//--------------------------------------------------------
-// sended packets to map-server
// broadcast sending
int mapif_broadcast(unsigned char *mes, int len, unsigned long fontColor, short fontType, short fontSize, short fontAlign, short fontY, int sfd)
@@ -316,56 +343,32 @@ int mapif_broadcast(unsigned char *mes, int len, unsigned long fontColor, short
return 0;
}
-// Wisp/page transmission to one map-server
-int mapif_wis_message2(struct WisData *wd, int fd) {
- WFIFOHEAD(fd, 56+wd->len);
- WFIFOW(fd, 0) = 0x3801;
- WFIFOW(fd, 2) = 56 + wd->len;
- WFIFOL(fd, 4) = wd->id;
- memcpy(WFIFOP(fd, 8), wd->src, NAME_LENGTH);
- memcpy(WFIFOP(fd,32), wd->dst, NAME_LENGTH);
- memcpy(WFIFOP(fd,56), wd->msg, wd->len);
- wd->count = 1;
- WFIFOSET(fd,WFIFOW(fd,2));
- return 1;
-}
-
-// Wisp/page transmission to all map-server
-int mapif_wis_message(struct WisData *wd) {
+// Wis sending
+int mapif_wis_message(struct WisData *wd)
+{
unsigned char buf[2048];
if (wd->len > 2047-56) wd->len = 2047-56; //Force it to fit to avoid crashes. [Skotlex]
WBUFW(buf, 0) = 0x3801;
- WBUFW(buf, 2) = 56 + wd->len;
+ WBUFW(buf, 2) = 56 +wd->len;
WBUFL(buf, 4) = wd->id;
memcpy(WBUFP(buf, 8), wd->src, NAME_LENGTH);
memcpy(WBUFP(buf,32), wd->dst, NAME_LENGTH);
memcpy(WBUFP(buf,56), wd->msg, wd->len);
- wd->count = mapif_sendall(buf, WBUFW(buf,2));
+ wd->count = mapif_sendall(buf,WBUFW(buf,2));
return 0;
}
-int mapif_wis_fail(int fd, char *src) {
- unsigned char buf[27];
- WBUFW(buf, 0) = 0x3802;
- memcpy(WBUFP(buf, 2), src, NAME_LENGTH);
- WBUFB(buf,26) = 1; // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target
- mapif_send(fd, buf, 27);
- return 0;
-
-}
-
-// Wisp/page transmission result to map-server
+// Wis sending result
int mapif_wis_end(struct WisData *wd, int flag)
{
unsigned char buf[27];
- WBUFW(buf, 0) = 0x3802;
- memcpy(WBUFP(buf, 2), wd->src, 24);
- WBUFB(buf,26) = flag; // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target
- mapif_send(wd->fd, buf, 27);
-
+ WBUFW(buf, 0)=0x3802;
+ memcpy(WBUFP(buf, 2),wd->src,24);
+ WBUFB(buf,26)=flag;
+ mapif_send(wd->fd,buf,27);
return 0;
}
@@ -376,25 +379,28 @@ static void mapif_account_reg(int fd, unsigned char *src)
mapif_sendallwos(fd, src, WBUFW(src,2));
}
-// アカウント変数要求返信
-int mapif_account_reg_reply(int fd,int account_id, int char_id)
+// Send the requested account_reg
+int mapif_account_reg_reply(int fd,int account_id,int char_id, int type)
{
- struct accreg *reg = (struct accreg*)idb_get(accreg_db,account_id);
-
- WFIFOHEAD(fd, ACCOUNT_REG_NUM * 288+ 13);
- WFIFOW(fd,0) = 0x3804;
- WFIFOL(fd,4) = account_id;
- WFIFOL(fd,8) = char_id;
- WFIFOB(fd,12) = 2; //Acc Reg
- if (reg == NULL) {
- WFIFOW(fd,2) = 13;
- } else {
- int i, p;
- for (p=13,i = 0; i < reg->reg_num; i++) {
+ struct accreg *reg=accreg_pt;
+ WFIFOHEAD(fd, 13 + 5000);
+ inter_accreg_fromsql(account_id,char_id,reg,type);
+
+ WFIFOW(fd,0)=0x3804;
+ WFIFOL(fd,4)=account_id;
+ WFIFOL(fd,8)=char_id;
+ WFIFOB(fd,12)=type;
+ if(reg->reg_num==0){
+ WFIFOW(fd,2)=13;
+ }else{
+ int i,p;
+ for (p=13,i = 0; i < reg->reg_num && p < 5000; i++) {
p+= sprintf((char*)WFIFOP(fd,p), "%s", reg->reg[i].str)+1; //We add 1 to consider the '\0' in place.
p+= sprintf((char*)WFIFOP(fd,p), "%s", reg->reg[i].value)+1;
}
WFIFOW(fd,2)=p;
+ if (p>= 5000)
+ ShowWarning("Too many acc regs for %d:%d, not all values were loaded.\n", account_id, char_id);
}
WFIFOSET(fd,WFIFOW(fd,2));
return 0;
@@ -403,22 +409,23 @@ int mapif_account_reg_reply(int fd,int account_id, int char_id)
//Request to kick char from a certain map server. [Skotlex]
int mapif_disconnectplayer(int fd, int account_id, int char_id, int reason)
{
- if (fd < 0)
- return -1;
-
- WFIFOHEAD(fd, 7);
- WFIFOW(fd,0) = 0x2b1f;
- WFIFOL(fd,2) = account_id;
- WFIFOB(fd,6) = reason;
- WFIFOSET(fd,7);
-
- return 0;
+ if (fd >= 0)
+ {
+ WFIFOHEAD(fd,7);
+ WFIFOW(fd,0) = 0x2b1f;
+ WFIFOL(fd,2) = account_id;
+ WFIFOB(fd,6) = reason;
+ WFIFOSET(fd,7);
+ return 0;
+ }
+ return -1;
}
//--------------------------------------------------------
// Existence check of WISP data
-int check_ttl_wisdata_sub(DBKey key, void *data, va_list ap) {
+int check_ttl_wisdata_sub(DBKey key, void *data, va_list ap)
+{
unsigned long tick;
struct WisData *wd = (struct WisData *)data;
tick = va_arg(ap, unsigned long);
@@ -429,7 +436,8 @@ int check_ttl_wisdata_sub(DBKey key, void *data, va_list ap) {
return 0;
}
-int check_ttl_wisdata(void) {
+int check_ttl_wisdata(void)
+{
unsigned long tick = gettick();
int i;
@@ -449,41 +457,27 @@ int check_ttl_wisdata(void) {
}
//--------------------------------------------------------
-// received packets from map-server
// broadcast sending
int mapif_parse_broadcast(int fd)
{
- RFIFOHEAD(fd);
mapif_broadcast(RFIFOP(fd,16), RFIFOW(fd,2), RFIFOL(fd,4), RFIFOW(fd,8), RFIFOW(fd,10), RFIFOW(fd,12), RFIFOW(fd,14), fd);
return 0;
}
-static struct WisData* mapif_create_whisper(int fd, char* src, char* dst, char* mes, int meslen)
-{
- static int wisid = 0;
- struct WisData* wd = (struct WisData *)aCalloc(sizeof(struct WisData), 1);
- if (wd == NULL){
- ShowFatalError("inter: WisRequest: out of memory !\n");
- return NULL;
- }
- wd->id = ++wisid;
- wd->fd = fd;
- wd->len= meslen;
- memcpy(wd->src, src, NAME_LENGTH);
- memcpy(wd->dst, dst, NAME_LENGTH);
- memcpy(wd->msg, mes, meslen);
- wd->tick = gettick();
- return wd;
-}
// Wisp/page request to send
int mapif_parse_WisRequest(int fd)
{
- struct mmo_charstatus* char_status;
struct WisData* wd;
+ static int wisid = 0;
char name[NAME_LENGTH];
- int fd2;
+ char esc_name[NAME_LENGTH*2+1];// escaped name
+ char* data;
+ size_t len;
+
+
+ if ( fd <= 0 ) {return 0;} // check if we have a valid fd
if (RFIFOW(fd,2)-52 >= sizeof(wd->msg)) {
ShowWarning("inter: Wis message size too long.\n");
@@ -492,46 +486,73 @@ int mapif_parse_WisRequest(int fd)
ShowError("inter: Wis message doesn't exist.\n");
return 0;
}
-
+
safestrncpy(name, (char*)RFIFOP(fd,28), NAME_LENGTH); //Received name may be too large and not contain \0! [Skotlex]
+ Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
+ if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name` FROM `%s` WHERE `name`='%s'", char_db, esc_name) )
+ Sql_ShowDebug(sql_handle);
+
// search if character exists before to ask all map-servers
- char_status = search_character_byname(name);
- if (char_status == NULL)
- return mapif_wis_fail(fd, (char*)RFIFOP(fd, 4));
-
- // Character exists.
- // to be sure of the correct name, rewrite it
- memset(name, 0, NAME_LENGTH);
- strncpy(name, char_status->name, NAME_LENGTH);
- // if source is destination, don't ask other servers.
- if (strcmp((char*)RFIFOP(fd,4),name) == 0)
- return mapif_wis_fail(fd, (char*)RFIFOP(fd, 4));
-
- //Look for online character.
- fd2 = search_character_online(char_status->account_id, char_status->char_id);
- if (fd2 >= 0) { //Character online, send whisper.
- wd = mapif_create_whisper(fd, (char*)RFIFOP(fd, 4), (char*)RFIFOP(fd,28), (char*)RFIFOP(fd,52), RFIFOW(fd,2)-52);
- if (!wd) return 1;
- idb_put(wis_db, wd->id, wd);
- mapif_wis_message2(wd, fd2);
- return 0;
+ if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
+ {
+ unsigned char buf[27];
+ WBUFW(buf, 0) = 0x3802;
+ memcpy(WBUFP(buf, 2), RFIFOP(fd, 4), NAME_LENGTH);
+ WBUFB(buf,26) = 1; // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target
+ mapif_send(fd, buf, 27);
+ }
+ else
+ {// Character exists. So, ask all map-servers
+ // to be sure of the correct name, rewrite it
+ Sql_GetData(sql_handle, 0, &data, &len);
+ memset(name, 0, NAME_LENGTH);
+ memcpy(name, data, min(len, NAME_LENGTH));
+ // if source is destination, don't ask other servers.
+ if( strncmp((const char*)RFIFOP(fd,4), name, NAME_LENGTH) == 0 )
+ {
+ uint8 buf[27];
+ WBUFW(buf, 0) = 0x3802;
+ memcpy(WBUFP(buf, 2), RFIFOP(fd, 4), NAME_LENGTH);
+ WBUFB(buf,26) = 1; // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target
+ mapif_send(fd, buf, 27);
+ }
+ else
+ {
+
+ CREATE(wd, struct WisData, 1);
+
+ // Whether the failure of previous wisp/page transmission (timeout)
+ check_ttl_wisdata();
+
+ wd->id = ++wisid;
+ wd->fd = fd;
+ wd->len= RFIFOW(fd,2)-52;
+ memcpy(wd->src, RFIFOP(fd, 4), NAME_LENGTH);
+ memcpy(wd->dst, RFIFOP(fd,28), NAME_LENGTH);
+ memcpy(wd->msg, RFIFOP(fd,52), wd->len);
+ wd->tick = gettick();
+ idb_put(wis_db, wd->id, wd);
+ mapif_wis_message(wd);
+ }
}
- //Not found.
- return mapif_wis_fail(fd, (char*)RFIFOP(fd,4));
+
+ Sql_FreeResult(sql_handle);
+ return 0;
}
+
// Wisp/page transmission result
-int mapif_parse_WisReply(int fd) {
+int mapif_parse_WisReply(int fd)
+{
int id, flag;
struct WisData *wd;
- RFIFOHEAD(fd);
+
id = RFIFOL(fd,2);
flag = RFIFOB(fd,6);
wd = (struct WisData*)idb_get(wis_db, id);
-
if (wd == NULL)
- return 0; // This wisp was probably suppress before, because it was timeout or because of target was found on another map-server
+ return 0; // This wisp was probably suppress before, because it was timeout of because of target was found on another map-server
if ((--wd->count) <= 0 || flag != 1) {
mapif_wis_end(wd, flag); // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target
@@ -542,10 +563,10 @@ int mapif_parse_WisReply(int fd) {
}
// Received wisp message from map-server for ALL gm (just copy the message and resends it to ALL map-servers)
-int mapif_parse_WisToGM(int fd) {
+int mapif_parse_WisToGM(int fd)
+{
unsigned char buf[2048]; // 0x3003/0x3803 <packet_len>.w <wispname>.24B <min_gm_level>.w <message>.?B
-
- ShowDebug("Sent packet back!\n");
+
memcpy(WBUFP(buf,0), RFIFOP(fd,0), RFIFOW(fd,2));
WBUFW(buf, 0) = 0x3803;
mapif_sendall(buf, RFIFOW(fd,2));
@@ -553,32 +574,26 @@ int mapif_parse_WisToGM(int fd) {
return 0;
}
-static void* create_accreg(DBKey key, va_list args) {
- struct accreg *reg;
- reg = (struct accreg*)aCalloc(sizeof(struct accreg), 1);
- reg->account_id = key.i;
- return reg;
-}
-
-// アカウント変数保存要求
-int mapif_parse_Registry(int fd) {
- int j, p, len;
- struct accreg *reg;
- RFIFOHEAD(fd);
-
- switch (RFIFOB(fd,12)) {
- case 3: //Character registry
- return char_parse_Registry(RFIFOL(fd,4), RFIFOL(fd,8), RFIFOP(fd,13), RFIFOW(fd,2)-13);
- case 2: //Acc Reg
- break;
- case 1: //Acc Reg2, forward to login
- return save_accreg2(RFIFOP(fd,4), RFIFOW(fd,2)-4);
- default: //Error?
- return 1;
+// Save account_reg into sql (type=2)
+int mapif_parse_Registry(int fd)
+{
+ int j,p,len, max;
+ struct accreg *reg=accreg_pt;
+
+ memset(accreg_pt,0,sizeof(struct accreg));
+ switch (RFIFOB(fd, 12)) {
+ case 3: //Character registry
+ max = GLOBAL_REG_NUM;
+ break;
+ case 2: //Account Registry
+ max = ACCOUNT_REG_NUM;
+ break;
+ case 1: //Account2 registry, must be sent over to login server.
+ return save_accreg2(RFIFOP(fd,4), RFIFOW(fd,2)-4);
+ default:
+ return 1;
}
- reg = (struct accreg*)idb_ensure(accreg_db, RFIFOL(fd,4), create_accreg);
-
- for(j=0,p=13;j<ACCOUNT_REG_NUM && p<RFIFOW(fd,2);j++){
+ for(j=0,p=13;j<max && p<RFIFOW(fd,2);j++){
sscanf((char*)RFIFOP(fd,p), "%31c%n",reg->reg[j].str,&len);
reg->reg[j].str[len]='\0';
p +=len+1; //+1 to skip the '\0' between strings.
@@ -587,34 +602,32 @@ int mapif_parse_Registry(int fd) {
p +=len+1;
}
reg->reg_num=j;
- mapif_account_reg(fd, RFIFOP(fd,0)); // 他のMAPサーバーに送信
+ inter_accreg_tosql(RFIFOL(fd,4),RFIFOL(fd,8),reg, RFIFOB(fd,12));
+ mapif_account_reg(fd,RFIFOP(fd,0)); // Send updated accounts to other map servers.
return 0;
}
// Request the value of all registries.
int mapif_parse_RegistryRequest(int fd)
-{
- RFIFOHEAD(fd);
+{
//Load Char Registry
- if (RFIFOB(fd,12))
- char_account_reg_reply(fd,RFIFOL(fd,2),RFIFOL(fd,6));
+ if (RFIFOB(fd,12)) mapif_account_reg_reply(fd,RFIFOL(fd,2),RFIFOL(fd,6),3);
//Load Account Registry
- if (RFIFOB(fd,11))
- mapif_account_reg_reply(fd,RFIFOL(fd,2),RFIFOL(fd,6));
+ if (RFIFOB(fd,11)) mapif_account_reg_reply(fd,RFIFOL(fd,2),RFIFOL(fd,6),2);
//Ask Login Server for Account2 values.
- if (RFIFOB(fd,10))
- request_accreg2(RFIFOL(fd,2),RFIFOL(fd,6));
+ if (RFIFOB(fd,10)) request_accreg2(RFIFOL(fd,2),RFIFOL(fd,6));
return 1;
}
-static void mapif_namechange_ack(int fd, int account_id, int char_id, int type, int flag, char *name){
+static void mapif_namechange_ack(int fd, int account_id, int char_id, int type, int flag, char *name)
+{
WFIFOHEAD(fd, NAME_LENGTH+13);
- WFIFOW(fd, 0) =0x3806;
- WFIFOL(fd, 2) =account_id;
- WFIFOL(fd, 6) =char_id;
- WFIFOB(fd,10) =type;
- WFIFOB(fd,11) =flag;
+ WFIFOW(fd, 0) = 0x3806;
+ WFIFOL(fd, 2) = account_id;
+ WFIFOL(fd, 6) = char_id;
+ WFIFOB(fd,10) = type;
+ WFIFOB(fd,11) = flag;
memcpy(WFIFOP(fd, 12), name, NAME_LENGTH);
WFIFOSET(fd, NAME_LENGTH+13);
}
@@ -625,11 +638,10 @@ int mapif_parse_NameChangeRequest(int fd)
char* name;
int i;
- RFIFOHEAD(fd);
- account_id = RFIFOL(fd, 2);
- char_id = RFIFOL(fd, 6);
- type = RFIFOB(fd, 10);
- name = (char*)RFIFOP(fd, 11);
+ account_id = RFIFOL(fd,2);
+ char_id = RFIFOL(fd,6);
+ type = RFIFOB(fd,10);
+ name = (char*)RFIFOP(fd,11);
// Check Authorised letters/symbols in the name
if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised
@@ -675,24 +687,17 @@ int inter_check_length(int fd, int length)
return length;
}
-// map server からの通信(1パケットのみ解析すること)
-// エラーなら0(false)、処理できたなら1、
-// パケット長が足りなければ2をかえさなければならない
-int inter_parse_frommap(int fd) {
- int cmd, len;
- RFIFOHEAD(fd);
+int inter_parse_frommap(int fd)
+{
+ int cmd;
+ int len = 0;
cmd = RFIFOW(fd,0);
- len = 0;
-
// inter鯖管轄かを調べる
- if (cmd < 0x3000 || cmd >= 0x3000 + ARRAYLENGTH(inter_recv_packet_length))
- return 0;
-
- if (inter_recv_packet_length[cmd-0x3000] == 0) //This is necessary, because otherwise we return 2 and the char server will just hang waiting for packets! [Skotlex]
+ if(cmd < 0x3000 || cmd >= 0x3000 + ARRAYLENGTH(inter_recv_packet_length) || inter_recv_packet_length[cmd - 0x3000] == 0)
return 0;
// パケット長を調べる
- if ((len = inter_check_length(fd, inter_recv_packet_length[cmd - 0x3000])) == 0)
+ if((len = inter_check_length(fd, inter_recv_packet_length[cmd - 0x3000])) == 0)
return 2;
switch(cmd) {
@@ -704,20 +709,21 @@ int inter_parse_frommap(int fd) {
case 0x3005: mapif_parse_RegistryRequest(fd); break;
case 0x3006: mapif_parse_NameChangeRequest(fd); break;
default:
- if (inter_party_parse_frommap(fd))
- break;
- if (inter_guild_parse_frommap(fd))
- break;
- if (inter_storage_parse_frommap(fd))
- break;
- if (inter_pet_parse_frommap(fd))
- break;
- if (inter_homun_parse_frommap(fd))
+ if( inter_party_parse_frommap(fd)
+ || inter_guild_parse_frommap(fd)
+ || inter_storage_parse_frommap(fd)
+ || inter_pet_parse_frommap(fd)
+ || inter_homunculus_parse_frommap(fd)
+ || inter_mercenary_parse_frommap(fd)
+ || inter_mail_parse_frommap(fd)
+ || inter_auction_parse_frommap(fd)
+ || inter_quest_parse_frommap(fd)
+ )
break;
- return 0;
+ else
+ return 0;
}
+
RFIFOSKIP(fd, len);
return 1;
}
-
-#endif //TXT_SQL_CONVERT