From 97bb21a79949779df76269b087f3bce7ef8179ee Mon Sep 17 00:00:00 2001 From: Dennis Friis Date: Wed, 2 Apr 2008 15:09:18 +0000 Subject: initial checkin --- misc/src/Makefile | 126 + misc/src/Makefile-optimized | 49 + misc/src/char/GNUmakefile | 17 + misc/src/char/Makefile | 17 + misc/src/char/char.c | 3149 +++++++ misc/src/char/char.h | 31 + misc/src/char/diff.diff | 4242 +++++++++ misc/src/char/int_guild.c | 1441 +++ misc/src/char/int_guild.h | 16 + misc/src/char/int_party.c | 595 ++ misc/src/char/int_party.h | 14 + misc/src/char/int_pet.c | 364 + misc/src/char/int_pet.h | 13 + misc/src/char/int_storage.c | 504 ++ misc/src/char/int_storage.h | 16 + misc/src/char/inter.c | 561 ++ misc/src/char/inter.h | 19 + misc/src/char_sql/GNUmakefile | 20 + misc/src/char_sql/Makefile | 20 + misc/src/char_sql/char.c | 2917 +++++++ misc/src/char_sql/char.h | 82 + misc/src/char_sql/int_guild.c | 1604 ++++ misc/src/char_sql/int_guild.h | 10 + misc/src/char_sql/int_party.c | 755 ++ misc/src/char_sql/int_party.h | 8 + misc/src/char_sql/int_pet.c | 324 + misc/src/char_sql/int_pet.h | 12 + misc/src/char_sql/int_storage.c | 366 + misc/src/char_sql/int_storage.h | 13 + misc/src/char_sql/inter.c | 573 ++ misc/src/char_sql/inter.h | 44 + misc/src/char_sql/itemdb.c | 247 + misc/src/char_sql/itemdb.h | 34 + misc/src/char_sql/make.sh | 11 + misc/src/char_sql/readme.txt | 250 + misc/src/char_sql/strlib.c | 79 + misc/src/char_sql/strlib.h | 10 + misc/src/common/GNUmakefile | 13 + misc/src/common/Makefile | 13 + misc/src/common/core.c | 152 + misc/src/common/core.h | 12 + misc/src/common/db.c | 500 ++ misc/src/common/db.h | 47 + misc/src/common/grfio.c | 948 ++ misc/src/common/grfio.h | 16 + misc/src/common/lock.c | 37 + misc/src/common/lock.h | 8 + misc/src/common/malloc.c | 44 + misc/src/common/malloc.h | 25 + misc/src/common/mmo.h | 304 + misc/src/common/nullpo.c | 90 + misc/src/common/nullpo.h | 222 + misc/src/common/socket.c | 439 + misc/src/common/socket.h | 96 + misc/src/common/timer.c | 312 + misc/src/common/timer.h | 45 + misc/src/common/utils.c | 108 + misc/src/common/utils.h | 33 + misc/src/common/version.h | 27 + misc/src/ladmin/GNUmakefile | 14 + misc/src/ladmin/Makefile | 14 + misc/src/ladmin/ladmin.c | 4385 ++++++++++ misc/src/ladmin/ladmin.h | 11 + misc/src/ladmin/md5calc.c | 237 + misc/src/ladmin/md5calc.h | 8 + misc/src/login/GNUmakefile | 13 + misc/src/login/Makefile | 13 + misc/src/login/login.c | 3698 ++++++++ misc/src/login/login.h | 38 + misc/src/login/md5calc.c | 237 + misc/src/login/md5calc.h | 8 + misc/src/login_sql/GNUmakefile | 17 + misc/src/login_sql/Makefile | 17 + misc/src/login_sql/login.c | 1682 ++++ misc/src/login_sql/login.h | 41 + misc/src/login_sql/make.sh | 6 + misc/src/login_sql/md5calc.c | 236 + misc/src/login_sql/md5calc.h | 7 + misc/src/login_sql/readme.txt | 120 + misc/src/login_sql/strlib.c | 58 + misc/src/login_sql/strlib.h | 9 + misc/src/login_sql/timer.h | 43 + misc/src/map/GNUmakefile | 72 + misc/src/map/Makefile | 72 + misc/src/map/atcommand.c | 7753 +++++++++++++++++ misc/src/map/atcommand.h | 245 + misc/src/map/battle.c | 5374 ++++++++++++ misc/src/map/battle.h | 342 + misc/src/map/chat.c | 373 + misc/src/map/chat.h | 22 + misc/src/map/chrif.c | 1016 +++ misc/src/map/chrif.h | 29 + misc/src/map/clif.c | 9826 +++++++++++++++++++++ misc/src/map/clif.h | 280 + misc/src/map/guild.c | 1543 ++++ misc/src/map/guild.h | 87 + misc/src/map/intif.c | 1042 +++ misc/src/map/intif.h | 56 + misc/src/map/itemdb.c | 882 ++ misc/src/map/itemdb.h | 84 + misc/src/map/mail.c | 324 + misc/src/map/mail.h | 9 + misc/src/map/map.c | 2009 +++++ misc/src/map/map.h | 705 ++ misc/src/map/mob.c | 4216 +++++++++ misc/src/map/mob.h | 137 + misc/src/map/npc.c | 2035 +++++ misc/src/map/npc.h | 48 + misc/src/map/party.c | 644 ++ misc/src/map/party.h | 47 + misc/src/map/path.c | 404 + misc/src/map/pc.c | 7485 ++++++++++++++++ misc/src/map/pc.h | 186 + misc/src/map/pet.c | 1651 ++++ misc/src/map/pet.h | 69 + misc/src/map/script.c | 6700 ++++++++++++++ misc/src/map/script.h | 39 + misc/src/map/skill.c | 10637 +++++++++++++++++++++++ misc/src/map/skill.h | 842 ++ misc/src/map/storage.c | 576 ++ misc/src/map/storage.h | 38 + misc/src/map/trade.c | 255 + misc/src/map/trade.h | 13 + misc/src/map/vending.c | 163 + misc/src/map/vending.h | 12 + misc/src/mra.patch | 69 + misc/src/tmw-server.dev | 169 + misc/src/tmw-server.layout | 43 + misc/src/tool/Makefile | 6 + misc/src/tool/adduser.c | 96 + misc/src/tool/backup | 100 + misc/src/tool/cgi/addaccount.cgi | 204 + misc/src/tool/checkversion | 85 + misc/src/tool/convert.c | 296 + misc/src/tool/getlogincount | 122 + misc/src/tool/ladmin | 3793 ++++++++ misc/src/tool/mapcheck.sh | 34 + misc/src/tool/mapchecker.sh | 56 + misc/src/txt-converter/char/GNUmakefile | 13 + misc/src/txt-converter/char/Makefile | 13 + misc/src/txt-converter/char/char-converter.c | 842 ++ misc/src/txt-converter/char/char.h | 38 + misc/src/txt-converter/char/int_guild.h | 10 + misc/src/txt-converter/char/int_party.h | 11 + misc/src/txt-converter/char/int_pet.h | 12 + misc/src/txt-converter/char/int_storage.h | 11 + misc/src/txt-converter/char/inter.h | 28 + misc/src/txt-converter/char/strlib.c | 66 + misc/src/txt-converter/char/strlib.h | 9 + misc/src/txt-converter/common/mmo.h | 280 + misc/src/txt-converter/login/GNUmakefile | 11 + misc/src/txt-converter/login/Makefile | 11 + misc/src/txt-converter/login/login-converter.c | 252 + misc/src/webserver/Makefile | 26 + misc/src/webserver/doc/API.txt | 50 + misc/src/webserver/doc/README | 11 + misc/src/webserver/generate.c | 38 + misc/src/webserver/htmlstyle.c | 51 + misc/src/webserver/logs.c | 8 + misc/src/webserver/main.c | 142 + misc/src/webserver/pages/about.c | 6 + misc/src/webserver/pages/notdone.c | 5 + misc/src/webserver/pages/sample.c | 24 + misc/src/webserver/parse.c | 135 + misc/src/xand1_1.patch | 132 + 165 files changed, 108606 insertions(+) create mode 100644 misc/src/Makefile create mode 100644 misc/src/Makefile-optimized create mode 100644 misc/src/char/GNUmakefile create mode 100644 misc/src/char/Makefile create mode 100644 misc/src/char/char.c create mode 100644 misc/src/char/char.h create mode 100644 misc/src/char/diff.diff create mode 100644 misc/src/char/int_guild.c create mode 100644 misc/src/char/int_guild.h create mode 100644 misc/src/char/int_party.c create mode 100644 misc/src/char/int_party.h create mode 100644 misc/src/char/int_pet.c create mode 100644 misc/src/char/int_pet.h create mode 100644 misc/src/char/int_storage.c create mode 100644 misc/src/char/int_storage.h create mode 100644 misc/src/char/inter.c create mode 100644 misc/src/char/inter.h create mode 100644 misc/src/char_sql/GNUmakefile create mode 100644 misc/src/char_sql/Makefile create mode 100644 misc/src/char_sql/char.c create mode 100644 misc/src/char_sql/char.h create mode 100644 misc/src/char_sql/int_guild.c create mode 100644 misc/src/char_sql/int_guild.h create mode 100644 misc/src/char_sql/int_party.c create mode 100644 misc/src/char_sql/int_party.h create mode 100644 misc/src/char_sql/int_pet.c create mode 100644 misc/src/char_sql/int_pet.h create mode 100644 misc/src/char_sql/int_storage.c create mode 100644 misc/src/char_sql/int_storage.h create mode 100644 misc/src/char_sql/inter.c create mode 100644 misc/src/char_sql/inter.h create mode 100644 misc/src/char_sql/itemdb.c create mode 100644 misc/src/char_sql/itemdb.h create mode 100644 misc/src/char_sql/make.sh create mode 100644 misc/src/char_sql/readme.txt create mode 100644 misc/src/char_sql/strlib.c create mode 100644 misc/src/char_sql/strlib.h create mode 100644 misc/src/common/GNUmakefile create mode 100644 misc/src/common/Makefile create mode 100644 misc/src/common/core.c create mode 100644 misc/src/common/core.h create mode 100644 misc/src/common/db.c create mode 100644 misc/src/common/db.h create mode 100644 misc/src/common/grfio.c create mode 100644 misc/src/common/grfio.h create mode 100644 misc/src/common/lock.c create mode 100644 misc/src/common/lock.h create mode 100644 misc/src/common/malloc.c create mode 100644 misc/src/common/malloc.h create mode 100644 misc/src/common/mmo.h create mode 100644 misc/src/common/nullpo.c create mode 100644 misc/src/common/nullpo.h create mode 100644 misc/src/common/socket.c create mode 100644 misc/src/common/socket.h create mode 100644 misc/src/common/timer.c create mode 100644 misc/src/common/timer.h create mode 100644 misc/src/common/utils.c create mode 100644 misc/src/common/utils.h create mode 100644 misc/src/common/version.h create mode 100644 misc/src/ladmin/GNUmakefile create mode 100644 misc/src/ladmin/Makefile create mode 100644 misc/src/ladmin/ladmin.c create mode 100644 misc/src/ladmin/ladmin.h create mode 100644 misc/src/ladmin/md5calc.c create mode 100644 misc/src/ladmin/md5calc.h create mode 100644 misc/src/login/GNUmakefile create mode 100644 misc/src/login/Makefile create mode 100644 misc/src/login/login.c create mode 100644 misc/src/login/login.h create mode 100644 misc/src/login/md5calc.c create mode 100644 misc/src/login/md5calc.h create mode 100644 misc/src/login_sql/GNUmakefile create mode 100644 misc/src/login_sql/Makefile create mode 100644 misc/src/login_sql/login.c create mode 100644 misc/src/login_sql/login.h create mode 100644 misc/src/login_sql/make.sh create mode 100644 misc/src/login_sql/md5calc.c create mode 100644 misc/src/login_sql/md5calc.h create mode 100644 misc/src/login_sql/readme.txt create mode 100644 misc/src/login_sql/strlib.c create mode 100644 misc/src/login_sql/strlib.h create mode 100644 misc/src/login_sql/timer.h create mode 100644 misc/src/map/GNUmakefile create mode 100644 misc/src/map/Makefile create mode 100644 misc/src/map/atcommand.c create mode 100644 misc/src/map/atcommand.h create mode 100644 misc/src/map/battle.c create mode 100644 misc/src/map/battle.h create mode 100644 misc/src/map/chat.c create mode 100644 misc/src/map/chat.h create mode 100644 misc/src/map/chrif.c create mode 100644 misc/src/map/chrif.h create mode 100644 misc/src/map/clif.c create mode 100644 misc/src/map/clif.h create mode 100644 misc/src/map/guild.c create mode 100644 misc/src/map/guild.h create mode 100644 misc/src/map/intif.c create mode 100644 misc/src/map/intif.h create mode 100644 misc/src/map/itemdb.c create mode 100644 misc/src/map/itemdb.h create mode 100644 misc/src/map/mail.c create mode 100644 misc/src/map/mail.h create mode 100644 misc/src/map/map.c create mode 100644 misc/src/map/map.h create mode 100644 misc/src/map/mob.c create mode 100644 misc/src/map/mob.h create mode 100644 misc/src/map/npc.c create mode 100644 misc/src/map/npc.h create mode 100644 misc/src/map/party.c create mode 100644 misc/src/map/party.h create mode 100644 misc/src/map/path.c create mode 100644 misc/src/map/pc.c create mode 100644 misc/src/map/pc.h create mode 100644 misc/src/map/pet.c create mode 100644 misc/src/map/pet.h create mode 100644 misc/src/map/script.c create mode 100644 misc/src/map/script.h create mode 100644 misc/src/map/skill.c create mode 100644 misc/src/map/skill.h create mode 100644 misc/src/map/storage.c create mode 100644 misc/src/map/storage.h create mode 100644 misc/src/map/trade.c create mode 100644 misc/src/map/trade.h create mode 100644 misc/src/map/vending.c create mode 100644 misc/src/map/vending.h create mode 100644 misc/src/mra.patch create mode 100644 misc/src/tmw-server.dev create mode 100644 misc/src/tmw-server.layout create mode 100644 misc/src/tool/Makefile create mode 100644 misc/src/tool/adduser.c create mode 100644 misc/src/tool/backup create mode 100644 misc/src/tool/cgi/addaccount.cgi create mode 100644 misc/src/tool/checkversion create mode 100644 misc/src/tool/convert.c create mode 100644 misc/src/tool/getlogincount create mode 100644 misc/src/tool/ladmin create mode 100644 misc/src/tool/mapcheck.sh create mode 100644 misc/src/tool/mapchecker.sh create mode 100644 misc/src/txt-converter/char/GNUmakefile create mode 100644 misc/src/txt-converter/char/Makefile create mode 100644 misc/src/txt-converter/char/char-converter.c create mode 100644 misc/src/txt-converter/char/char.h create mode 100644 misc/src/txt-converter/char/int_guild.h create mode 100644 misc/src/txt-converter/char/int_party.h create mode 100644 misc/src/txt-converter/char/int_pet.h create mode 100644 misc/src/txt-converter/char/int_storage.h create mode 100644 misc/src/txt-converter/char/inter.h create mode 100644 misc/src/txt-converter/char/strlib.c create mode 100644 misc/src/txt-converter/char/strlib.h create mode 100644 misc/src/txt-converter/common/mmo.h create mode 100644 misc/src/txt-converter/login/GNUmakefile create mode 100644 misc/src/txt-converter/login/Makefile create mode 100644 misc/src/txt-converter/login/login-converter.c create mode 100644 misc/src/webserver/Makefile create mode 100644 misc/src/webserver/doc/API.txt create mode 100644 misc/src/webserver/doc/README create mode 100644 misc/src/webserver/generate.c create mode 100644 misc/src/webserver/htmlstyle.c create mode 100644 misc/src/webserver/logs.c create mode 100644 misc/src/webserver/main.c create mode 100644 misc/src/webserver/pages/about.c create mode 100644 misc/src/webserver/pages/notdone.c create mode 100644 misc/src/webserver/pages/sample.c create mode 100644 misc/src/webserver/parse.c create mode 100644 misc/src/xand1_1.patch (limited to 'misc/src') diff --git a/misc/src/Makefile b/misc/src/Makefile new file mode 100644 index 0000000..00ec3b9 --- /dev/null +++ b/misc/src/Makefile @@ -0,0 +1,126 @@ +# $Id: Makefile 158 2004-10-01 03:45:15Z PoW $ + +CC = gcc -pipe +PACKETDEF = -DPACKETVER=5 -DNEW_006b +#PACKETDEF = -DPACKETVER=4 -DNEW_006b +#PACKETDEF = -DPACKETVER=3 -DNEW_006b +#PACKETDEF = -DPACKETVER=2 -DNEW_006b +#PACKETDEF = -DPACKETVER=1 -DNEW_006b + +PLATFORM = $(shell uname) + +ifeq ($(findstring FreeBSD,$(PLATFORM)), FreeBSD) +MAKE = gmake +else +MAKE = make +endif + +OPT = -g -O2 + +ifeq ($(findstring CYGWIN,$(PLATFORM)), CYGWIN) +OS_TYPE = -DCYGWIN +CFLAGS = $(OPT) -Wall -DFD_SETSIZE=4096 -I../common $(PACKETDEF) $(OS_TYPE) +else +OS_TYPE = +CFLAGS = $(OPT) -Wall -I../common $(PACKETDEF) $(OS_TYPE) +endif + +MYSQLFLAG_INCLUDE_DEFAULT = /usr/local/include/mysql + +ifdef SQLFLAG +MYSQLFLAG_CONFIG = $(shell which mysql_config) +ifeq ($(findstring /,$(MYSQLFLAG_CONFIG)), /) +MYSQLFLAG_VERSION = $(shell $(MYSQLFLAG_CONFIG) --version | sed s:\\..*::) +endif + +ifeq ($(findstring 5,$(MYSQLFLAG_VERSION)), 5) +MYSQLFLAG_CONFIG_ARGUMENT = --include +endif +ifeq ($(findstring 4,$(MYSQLFLAG_VERSION)), 4) +MYSQLFLAG_CONFIG_ARGUMENT = --include +endif +ifndef MYSQLFLAG_CONFIG_ARGUMENT +MYSQLFLAG_CONFIG_ARGUMENT = --cflags +endif + +ifeq ($(findstring /,$(MYSQLFLAG_CONFIG)), /) +MYSQLFLAG_INCLUDE = $(shell $(MYSQLFLAG_CONFIG) $(MYSQLFLAG_CONFIG_ARGUMENT)) +else +MYSQLFLAG_INCLUDE = -I$(MYSQLFLAG_INCLUDE_DEFAULT) +endif + +LIB_S_DEFAULT = -L/usr/local/lib/mysql -lmysqlclient -lz +MYSQLFLAG_CONFIG = $(shell which mysql_config) +ifeq ($(findstring /,$(MYSQLFLAG_CONFIG)), /) +LIB_S = $(shell $(MYSQLFLAG_CONFIG) --libs) +else +LIB_S = $(LIB_S_DEFAULT) +endif + +MYLIB = CC="$(CC)" CFLAGS="$(CFLAGS) $(MYSQLFLAG_INCLUDE)" LIB_S="$(LIB_S)" + +endif + +MKDEF = CC="$(CC)" CFLAGS="$(CFLAGS)" + +all: conf txt + +conf: + cp -r conf-tmpl conf + rm -rf conf/.svn conf/*/.svn + +txt : src/common/GNUmakefile src/login/GNUmakefile src/char/GNUmakefile src/map/GNUmakefile src/ladmin/GNUmakefile conf + cd src ; cd common ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd login ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd char ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd map ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd ladmin ; $(MAKE) $(MKDEF) $@ ; cd .. + + +ifdef SQLFLAG +sql: src/common/GNUmakefile src/login_sql/GNUmakefile src/char_sql/GNUmakefile src/map/GNUmakefile src/ladmin/GNUmakefile src/txt-converter/login/GNUmakefile src/txt-converter/char/GNUmakefile conf + cd src ; cd common ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd login_sql ; $(MAKE) $(MYLIB) $@ ; cd .. + cd src ; cd char_sql ; $(MAKE) $(MYLIB) $@ ; cd .. + cd src ; cd map ; $(MAKE) $(MYLIB) $@ ; cd .. + cd src ; cd ladmin ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd txt-converter ; cd login ; $(MAKE) $(MYLIB) $@ ; cd .. + cd src ; cd txt-converter ; cd char ; $(MAKE) $(MYLIB) $@ ; cd .. +else +sql: + $(MAKE) CC="$(CC)" OPT="$(OPT)" SQLFLAG=1 $@ +endif + +clean: src/common/GNUmakefile src/login/GNUmakefile src/char/GNUmakefile src/map/GNUmakefile src/ladmin/GNUmakefile src/txt-converter/login/GNUmakefile src/txt-converter/char/GNUmakefile + cd src ; cd common ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd login ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd login_sql ; $(MAKE) $(MKLIB) $@ ; cd .. + cd src ; cd char ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd char_sql ; $(MAKE) $(MKLIB) $@ ; cd .. + cd src ; cd map ; $(MAKE) $(MKLIB) $@ ; cd .. + cd src ; cd ladmin ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd txt-converter ; cd login ; $(MAKE) $(MKLIB) $@ ; cd .. + cd src ; cd txt-converter ; cd char ; $(MAKE) $(MKLIB) $@ ; cd .. + +tools: + cd tool && $(MAKE) $(MKDEF) && cd .. + $(CC) -o setupwizard setupwizard.c + +src/common/GNUmakefile: src/common/Makefile + sed -e 's/$$>/$$^/' src/common/Makefile > src/common/GNUmakefile +src/login/GNUmakefile: src/login/Makefile + sed -e 's/$$>/$$^/' src/login/Makefile > src/login/GNUmakefile +src/login_sql/GNUmakefile: src/login_sql/Makefile + sed -e 's/$$>/$$^/' src/login_sql/Makefile > src/login_sql/GNUmakefile +src/char/GNUmakefile: src/char/Makefile + sed -e 's/$$>/$$^/' src/char/Makefile > src/char/GNUmakefile +src/char_sql/GNUmakefile: src/char_sql/Makefile + sed -e 's/$$>/$$^/' src/char_sql/Makefile > src/char_sql/GNUmakefile +src/map/GNUmakefile: src/map/Makefile + sed -e 's/$$>/$$^/' src/map/Makefile > src/map/GNUmakefile +src/ladmin/GNUmakefile: src/ladmin/Makefile + sed -e 's/$$>/$$^/' src/ladmin/Makefile > src/ladmin/GNUmakefile +src/txt-converter/login/GNUmakefile: src/txt-converter/login/Makefile + sed -e 's/$$>/$$^/' src/txt-converter/login/Makefile > src/txt-converter/login/GNUmakefile +src/txt-converter/char/GNUmakefile: src/txt-converter/char/Makefile + sed -e 's/$$>/$$^/' src/txt-converter/char/Makefile > src/txt-converter/char/GNUmakefile diff --git a/misc/src/Makefile-optimized b/misc/src/Makefile-optimized new file mode 100644 index 0000000..63dc5ff --- /dev/null +++ b/misc/src/Makefile-optimized @@ -0,0 +1,49 @@ +# $Id: Makefile-optimized,v 1.7 2004/07/29 09:35:21 Yor Exp $ + +CC = gcc -pipe +PACKETDEF = -DPACKETVER=5 -DNEW_006b +#PACKETDEF = -DPACKETVER=4 -DNEW_006b +#PACKETDEF = -DPACKETVER=3 -DNEW_006b +#PACKETDEF = -DPACKETVER=2 -DNEW_006b +#PACKETDEF = -DPACKETVER=1 -DNEW_006b + +PLATFORM = $(shell uname) + +ifeq ($(findstring FreeBSD,$(PLATFORM)), FreeBSD) +MAKE = gmake +else +MAKE = make +endif + +ifeq ($(findstring CYGWIN,$(PLATFORM)), CYGWIN) +OS_TYPE = -DCYGWIN +CFLAGS = -O2 -Wall -DFD_SETSIZE=4096 -I../common $(PACKETDEF) $(OS_TYPE) +else +OS_TYPE = +CFLAGS = -O2 -Wall -I../common $(PACKETDEF) $(OS_TYPE) +endif + +MKDEF = CC="$(CC)" CFLAGS="$(CFLAGS)" + + +all clean: src/common/GNUmakefile src/login/GNUmakefile src/char_unblocked/GNUmakefile src/map/GNUmakefile src/ladmin/GNUmakefile + cd src ; cd common ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd login ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd char_unblocked ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd map ; $(MAKE) $(MKDEF) $@ ; cd .. + cd src ; cd ladmin ; $(MAKE) $(MKDEF) $@ ; cd .. + +tools: + cd tool && $(MAKE) $(MKDEF) && cd .. + $(CC) -o setupwizard setupwizard.c + +src/common/GNUmakefile: src/common/Makefile + sed -e 's/$$>/$$^/' src/common/Makefile > src/common/GNUmakefile +src/login/GNUmakefile: src/login/Makefile + sed -e 's/$$>/$$^/' src/login/Makefile > src/login/GNUmakefile +src/char_unblocked/GNUmakefile: src/char_unblocked/Makefile + sed -e 's/$$>/$$^/' src/char_unblocked/Makefile > src/char_unblocked/GNUmakefile +src/map/GNUmakefile: src/map/Makefile + sed -e 's/$$>/$$^/' src/map/Makefile > src/map/GNUmakefile +src/ladmin/GNUmakefile: src/ladmin/Makefile + sed -e 's/$$>/$$^/' src/ladmin/Makefile > src/ladmin/GNUmakefile diff --git a/misc/src/char/GNUmakefile b/misc/src/char/GNUmakefile new file mode 100644 index 0000000..dd21288 --- /dev/null +++ b/misc/src/char/GNUmakefile @@ -0,0 +1,17 @@ +all: char-server +txt: char-server + +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o ../common/lock.o ../common/malloc.o +COMMON_H = ../common/core.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/db.h ../common/lock.h ../common/timer.h ../common/malloc.h +char-server: char.o inter.o int_party.o int_guild.o int_storage.o int_pet.o $(COMMON_OBJ) + $(CC) -o ../../$@ $^ + +char.o: char.c char.h inter.h int_pet.h $(COMMON_H) ../common/version.h +inter.o: inter.c inter.h int_party.h int_guild.h int_storage.h int_pet.h char.h $(COMMON_H) +int_party.o: int_party.c int_party.h inter.h char.h $(COMMON_H) +int_guild.o: int_guild.c int_guild.h int_storage.h inter.h char.h $(COMMON_H) +int_storage.o: int_storage.c int_storage.h int_guild.h inter.h char.h $(COMMON_H) +int_pet.o: int_pet.c int_pet.h inter.h char.h $(COMMON_H) + +clean: + rm -f *.o ../../char-server diff --git a/misc/src/char/Makefile b/misc/src/char/Makefile new file mode 100644 index 0000000..6eaae48 --- /dev/null +++ b/misc/src/char/Makefile @@ -0,0 +1,17 @@ +all: char-server +txt: char-server + +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o ../common/lock.o ../common/malloc.o +COMMON_H = ../common/core.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/db.h ../common/lock.h ../common/timer.h ../common/malloc.h +char-server: char.o inter.o int_party.o int_guild.o int_storage.o int_pet.o $(COMMON_OBJ) + $(CC) -o ../../$@ $> + +char.o: char.c char.h inter.h int_pet.h $(COMMON_H) ../common/version.h +inter.o: inter.c inter.h int_party.h int_guild.h int_storage.h int_pet.h char.h $(COMMON_H) +int_party.o: int_party.c int_party.h inter.h char.h $(COMMON_H) +int_guild.o: int_guild.c int_guild.h int_storage.h inter.h char.h $(COMMON_H) +int_storage.o: int_storage.c int_storage.h int_guild.h inter.h char.h $(COMMON_H) +int_pet.o: int_pet.c int_pet.h inter.h char.h $(COMMON_H) + +clean: + rm -f *.o ../../char-server diff --git a/misc/src/char/char.c b/misc/src/char/char.c new file mode 100644 index 0000000..c279102 --- /dev/null +++ b/misc/src/char/char.c @@ -0,0 +1,3149 @@ +// $Id: char.c,v 1.3 2004/09/13 16:52:16 Yor Exp $ +// original : char2.c 2003/03/14 11:58:35 Rev.1.5 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "core.h" +#include "socket.h" +#include "timer.h" +#include "mmo.h" +#include "version.h" +#include "lock.h" +#include "char.h" + +#include "inter.h" +#include "int_pet.h" +#include "int_guild.h" +#include "int_party.h" +#include "int_storage.h" + +#ifdef MEMWATCH +#include "memwatch.h" +#endif + +struct mmo_map_server server[MAX_MAP_SERVERS]; +int server_fd[MAX_MAP_SERVERS]; +int server_freezeflag[MAX_MAP_SERVERS]; // Map-server anti-freeze system. Counter. 5 ok, 4...0 freezed +int anti_freeze_enable = 0; +int ANTI_FREEZE_INTERVAL = 6; + +int login_fd, char_fd; +char userid[24]; +char passwd[24]; +char server_name[20]; +char wisp_server_name[24] = "Server"; +char login_ip_str[16]; +int login_ip; +int login_port = 6900; +char char_ip_str[16]; +int char_ip; +int char_port = 6121; +int char_maintenance; +int char_new; +int email_creation = 0; // disabled by default +char char_txt[1024]; +char backup_txt[1024]; //By zanetheinsane +char backup_txt_flag = 0; // The backup_txt file was created because char deletion bug existed. Now it's finish and that take a lot of time to create a second file when there are a lot of characters. => option By [Yor] +char unknown_char_name[1024] = "Unknown"; +char char_log_filename[1024] = "log/char.log"; +//Added for lan support +char lan_map_ip[128]; +int subneti[4]; +int subnetmaski[4]; +int name_ignoring_case = 0; // Allow or not identical name for characters but with a different case by [Yor] +int char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor] +char char_name_letters[1024] = ""; // list of letters/symbols authorised (or not) in a character name. by [Yor] + +struct char_session_data{ + int account_id, login_id1, login_id2, sex; + int found_char[9]; + char email[40]; // e-mail (default: a@a.com) by [Yor] + time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited) +}; + +#define AUTH_FIFO_SIZE 256 +struct { + int account_id, char_id, login_id1, login_id2, ip, char_pos, delflag, sex; + time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited) +} auth_fifo[AUTH_FIFO_SIZE]; +int auth_fifo_pos = 0; + +int check_ip_flag = 1; // It's to check IP of a player between char-server and other servers (part of anti-hacking system) + +int char_id_count = 150000; +struct mmo_charstatus *char_dat; +int char_num, char_max; +int max_connect_user = 0; +int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL; +int start_zeny = 500; +int start_weapon = 1201; +int start_armor = 1202; + +// Initial position (it's possible to set it in conf file) +struct point start_point = {"new_1-1.gat", 53, 111}; + +struct gm_account *gm_account = NULL; +int GM_num = 0; + +// online players by [Yor] +char online_txt_filename[1024] = "online.txt"; +char online_html_filename[1024] = "online.html"; +int online_sorting_option = 0; // sorting option to display online players in online files +int online_display_option = 1; // display options: to know which columns must be displayed +int online_refresh_html = 20; // refresh time (in sec) of the html file in the explorer +int online_gm_display_min_level = 20; // minimum GM level to display 'GM' when we want to display it + +int *online_chars; // same size of char_dat, and id value of current server (or -1) +time_t update_online; // to update online files when we receiving information from a server (not less than 8 seconds) + +//------------------------------ +// Writing function of logs file +//------------------------------ +int char_log(char *fmt, ...) { + FILE *logfp; + va_list ap; + struct timeval tv; + char tmpstr[2048]; + + va_start(ap, fmt); + + logfp = fopen(char_log_filename, "a"); + if (logfp) { + if (fmt[0] == '\0') // jump a line if no message + fprintf(logfp, RETCODE); + else { + gettimeofday(&tv, NULL); + strftime(tmpstr, 24, "%d-%m-%Y %H:%M:%S", localtime(&(tv.tv_sec))); + sprintf(tmpstr + 19, ".%03d: %s", (int)tv.tv_usec / 1000, fmt); + vfprintf(logfp, tmpstr, ap); + } + fclose(logfp); + } + + va_end(ap); + return 0; +} + +//----------------------------------------------------- +// Function to suppress control characters in a string. +//----------------------------------------------------- +int remove_control_chars(unsigned char *str) { + int i; + int change = 0; + + for(i = 0; str[i]; i++) { + if (str[i] < 32) { + str[i] = '_'; + change = 1; + } + } + + return change; +} + +//---------------------------------------------------------------------- +// Determine if an account (id) is a GM account +// and returns its level (or 0 if it isn't a GM account or if not found) +//---------------------------------------------------------------------- +int isGM(int account_id) { + int i; + + for(i = 0; i < GM_num; i++) + if (gm_account[i].account_id == account_id) + return gm_account[i].level; + return 0; +} + +//---------------------------------------------- +// Search an character id +// (return character index or -1 (if not found)) +// If exact character name is not found, +// the function checks without case sensitive +// and returns index if only 1 character is found +// and similar to the searched name. +//---------------------------------------------- +int search_character_index(char* character_name) { + int i, quantity, index; + + quantity = 0; + index = -1; + for(i = 0; i < char_num; i++) { + // Without case sensitive check (increase the number of similar character names found) + if (stricmp(char_dat[i].name, character_name) == 0) { + // Strict comparison (if found, we finish the function immediatly with correct value) + if (strcmp(char_dat[i].name, character_name) == 0) + return i; + quantity++; + index = i; + } + } + // Here, the exact character name is not found + // We return the found index of a similar account ONLY if there is 1 similar character + if (quantity == 1) + return index; + + // Exact character name is not found and 0 or more than 1 similar characters have been found ==> we say not found + return -1; +} + +//------------------------------------- +// Return character name with the index +//------------------------------------- +char * search_character_name(int index) { + + if (index >= 0 && index < char_num) + return char_dat[index].name; + + return unknown_char_name; +} + +//------------------------------------------------- +// Function to create the character line (for save) +//------------------------------------------------- +int mmo_char_tostr(char *str, struct mmo_charstatus *p) { + int i; + char *str_p = str; + + // on multi-map server, sometimes it's posssible that last_point become void. (reason???) We check that to not lost character at restart. + if (p->last_point.map[0] == '\0') { + memcpy(p->last_point.map, "prontera.gat", 16); + p->last_point.x = 273; + p->last_point.y = 354; + } + + str_p += sprintf(str_p, "%d\t%d,%d\t%s\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" + "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" + "\t%s,%d,%d\t%s,%d,%d,%d\t", + p->char_id, p->account_id, p->char_num, p->name, // + p->class, p->base_level, p->job_level, + p->base_exp, p->job_exp, p->zeny, + p->hp, p->max_hp, p->sp, p->max_sp, + p->str, p->agi, p->vit, p->int_, p->dex, p->luk, + p->status_point, p->skill_point, + p->option, p->karma, p->manner, // + p->party_id, p->guild_id, p->pet_id, + p->hair, p->hair_color, p->clothes_color, + p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom, + p->last_point.map, p->last_point.x, p->last_point.y, // + p->save_point.map, p->save_point.x, p->save_point.y, + p->partner_id); + for(i = 0; i < 10; i++) + if (p->memo_point[i].map[0]) { + str_p += sprintf(str_p, "%s,%d,%d", p->memo_point[i].map, p->memo_point[i].x, p->memo_point[i].y); + } + *(str_p++) = '\t'; + + for(i = 0; i < MAX_INVENTORY; i++) + if (p->inventory[i].nameid) { + str_p += sprintf(str_p, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d ", + p->inventory[i].id, p->inventory[i].nameid, p->inventory[i].amount, p->inventory[i].equip, + p->inventory[i].identify, p->inventory[i].refine, p->inventory[i].attribute, + p->inventory[i].card[0], p->inventory[i].card[1], p->inventory[i].card[2], p->inventory[i].card[3], + p->inventory[i].broken); + } + *(str_p++) = '\t'; + + for(i = 0; i < MAX_CART; i++) + if (p->cart[i].nameid) { + str_p += sprintf(str_p, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d ", + p->cart[i].id, p->cart[i].nameid, p->cart[i].amount, p->cart[i].equip, + p->cart[i].identify, p->cart[i].refine, p->cart[i].attribute, + p->cart[i].card[0], p->cart[i].card[1], p->cart[i].card[2], p->cart[i].card[3], + p->cart[i].broken); + } + *(str_p++) = '\t'; + + for(i = 0; i < MAX_SKILL; i++) + if (p->skill[i].id && p->skill[i].flag != 1) { + str_p += sprintf(str_p, "%d,%d ", p->skill[i].id, (p->skill[i].flag == 0) ? p->skill[i].lv : p->skill[i].flag-2); + } + *(str_p++) = '\t'; + + for(i = 0; i < p->global_reg_num; i++) + if (p->global_reg[i].str[0]) + str_p += sprintf(str_p, "%s,%d ", p->global_reg[i].str, p->global_reg[i].value); + *(str_p++) = '\t'; + + *str_p = '\0'; + return 0; +} + +//------------------------------------------------------------------------- +// Function to set the character from the line (at read of characters file) +//------------------------------------------------------------------------- +int mmo_char_fromstr(char *str, struct mmo_charstatus *p) { + int tmp_int[256]; + int set, next, len, i; + + // initilialise character + memset(p, '\0', sizeof(struct mmo_charstatus)); + + // If it's not char structure of version 1008 and after + if ((set = sscanf(str, "%d\t%d,%d\t%[^\t]\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" + "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" + "\t%[^,],%d,%d\t%[^,],%d,%d,%d%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], p->name, // + &tmp_int[3], &tmp_int[4], &tmp_int[5], + &tmp_int[6], &tmp_int[7], &tmp_int[8], + &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], + &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], + &tmp_int[19], &tmp_int[20], + &tmp_int[21], &tmp_int[22], &tmp_int[23], // + &tmp_int[24], &tmp_int[25], &tmp_int[26], + &tmp_int[27], &tmp_int[28], &tmp_int[29], + &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], + p->last_point.map, &tmp_int[35], &tmp_int[36], // + p->save_point.map, &tmp_int[37], &tmp_int[38], &tmp_int[39], &next)) != 43) { + tmp_int[39] = 0; // partner id + // If not char structure from version 384 to 1007 + if ((set = sscanf(str, "%d\t%d,%d\t%[^\t]\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" + "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" + "\t%[^,],%d,%d\t%[^,],%d,%d%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], p->name, // + &tmp_int[3], &tmp_int[4], &tmp_int[5], + &tmp_int[6], &tmp_int[7], &tmp_int[8], + &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], + &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], + &tmp_int[19], &tmp_int[20], + &tmp_int[21], &tmp_int[22], &tmp_int[23], // + &tmp_int[24], &tmp_int[25], &tmp_int[26], + &tmp_int[27], &tmp_int[28], &tmp_int[29], + &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], + p->last_point.map, &tmp_int[35], &tmp_int[36], // + p->save_point.map, &tmp_int[37], &tmp_int[38], &next)) != 42) { + // It's char structure of a version before 384 + tmp_int[26] = 0; // pet id + set = sscanf(str, "%d\t%d,%d\t%[^\t]\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" + "\t%d,%d,%d\t%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" + "\t%[^,],%d,%d\t%[^,],%d,%d%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], p->name, // + &tmp_int[3], &tmp_int[4], &tmp_int[5], + &tmp_int[6], &tmp_int[7], &tmp_int[8], + &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], + &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], + &tmp_int[19], &tmp_int[20], + &tmp_int[21], &tmp_int[22], &tmp_int[23], // + &tmp_int[24], &tmp_int[25], // + &tmp_int[27], &tmp_int[28], &tmp_int[29], + &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], + p->last_point.map, &tmp_int[35], &tmp_int[36], // + p->save_point.map, &tmp_int[37], &tmp_int[38], &next); + set += 2; + //printf("char: old char data ver.1\n"); + // Char structure of version 1007 or older + } else { + set++; + //printf("char: old char data ver.2\n"); + } + // Char structure of version 1008+ + } else { + //printf("char: new char data ver.3\n"); + } + if (set != 43) + return 0; + + p->char_id = tmp_int[0]; + p->account_id = tmp_int[1]; + p->char_num = tmp_int[2]; + p->class = tmp_int[3]; + p->base_level = tmp_int[4]; + p->job_level = tmp_int[5]; + p->base_exp = tmp_int[6]; + p->job_exp = tmp_int[7]; + p->zeny = tmp_int[8]; + p->hp = tmp_int[9]; + p->max_hp = tmp_int[10]; + p->sp = tmp_int[11]; + p->max_sp = tmp_int[12]; + p->str = tmp_int[13]; + p->agi = tmp_int[14]; + p->vit = tmp_int[15]; + p->int_ = tmp_int[16]; + p->dex = tmp_int[17]; + p->luk = tmp_int[18]; + p->status_point = tmp_int[19]; + p->skill_point = tmp_int[20]; + p->option = tmp_int[21]; + p->karma = tmp_int[22]; + p->manner = tmp_int[23]; + p->party_id = tmp_int[24]; + p->guild_id = tmp_int[25]; + p->pet_id = tmp_int[26]; + p->hair = tmp_int[27]; + p->hair_color = tmp_int[28]; + p->clothes_color = tmp_int[29]; + p->weapon = tmp_int[30]; + p->shield = tmp_int[31]; + p->head_top = tmp_int[32]; + p->head_mid = tmp_int[33]; + p->head_bottom = tmp_int[34]; + p->last_point.x = tmp_int[35]; + p->last_point.y = tmp_int[36]; + p->save_point.x = tmp_int[37]; + p->save_point.y = tmp_int[38]; + p->partner_id = tmp_int[39]; + + // Some checks + for(i = 0; i < char_num; i++) { + if (char_dat[i].char_id == p->char_id) { + printf("\033[1;31mmmo_auth_init: ******Error: a character has an identical id to another.\n"); + printf(" character id #%d -> new character not readed.\n", p->char_id); + printf(" Character saved in log file.\033[0m\n"); + return -1; + } else if (strcmp(char_dat[i].name, p->name) == 0) { + printf("\033[1;31mmmo_auth_init: ******Error: character name already exists.\n"); + printf(" character name '%s' -> new character not readed.\n", p->name); + printf(" Character saved in log file.\033[0m\n"); + return -2; + } + } + + if (strcmpi(wisp_server_name, p->name) == 0) { + printf("mmo_auth_init: ******WARNING: character name has wisp server name.\n"); + printf(" Character name '%s' = wisp server name '%s'.\n", p->name, wisp_server_name); + printf(" Character readed. Suggestion: change the wisp server name.\n"); + char_log("mmo_auth_init: ******WARNING: character name has wisp server name: Character name '%s' = wisp server name '%s'." RETCODE, + p->name, wisp_server_name); + } + + if (str[next] == '\n' || str[next] == '\r') + return 1; // 新規データ + + next++; + + for(i = 0; str[next] && str[next] != '\t'; i++) { + if (sscanf(str+next, "%[^,],%d,%d%n", p->memo_point[i].map, &tmp_int[0], &tmp_int[1], &len) != 3) + return -3; + p->memo_point[i].x = tmp_int[0]; + p->memo_point[i].y = tmp_int[1]; + next += len; + if (str[next] == ' ') + next++; + } + + next++; + + for(i = 0; str[next] && str[next] != '\t'; i++) { + if (sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], + &tmp_int[4], &tmp_int[5], &tmp_int[6], + &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &tmp_int[11], &len) == 12) { + // do nothing, it's ok + } else if (sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], + &tmp_int[4], &tmp_int[5], &tmp_int[6], + &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &len) == 11) { + tmp_int[11] = 0; // broken doesn't exist in this version -> 0 + } else // invalid structure + return -4; + p->inventory[i].id = tmp_int[0]; + p->inventory[i].nameid = tmp_int[1]; + p->inventory[i].amount = tmp_int[2]; + p->inventory[i].equip = tmp_int[3]; + p->inventory[i].identify = tmp_int[4]; + p->inventory[i].refine = tmp_int[5]; + p->inventory[i].attribute = tmp_int[6]; + p->inventory[i].card[0] = tmp_int[7]; + p->inventory[i].card[1] = tmp_int[8]; + p->inventory[i].card[2] = tmp_int[9]; + p->inventory[i].card[3] = tmp_int[10]; + p->inventory[i].broken = tmp_int[11]; + next += len; + if (str[next] == ' ') + next++; + } + + next++; + + for(i = 0; str[next] && str[next] != '\t'; i++) { + if (sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], + &tmp_int[4], &tmp_int[5], &tmp_int[6], + &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &tmp_int[11], &len) == 12) { + // do nothing, it's ok + } else if (sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], + &tmp_int[4], &tmp_int[5], &tmp_int[6], + &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &len) == 11) { + tmp_int[11] = 0; // broken doesn't exist in this version -> 0 + } else // invalid structure + return -5; + p->cart[i].id = tmp_int[0]; + p->cart[i].nameid = tmp_int[1]; + p->cart[i].amount = tmp_int[2]; + p->cart[i].equip = tmp_int[3]; + p->cart[i].identify = tmp_int[4]; + p->cart[i].refine = tmp_int[5]; + p->cart[i].attribute = tmp_int[6]; + p->cart[i].card[0] = tmp_int[7]; + p->cart[i].card[1] = tmp_int[8]; + p->cart[i].card[2] = tmp_int[9]; + p->cart[i].card[3] = tmp_int[10]; + p->cart[i].broken = tmp_int[11]; + next += len; + if (str[next] == ' ') + next++; + } + + next++; + + for(i = 0; str[next] && str[next] != '\t'; i++) { + if (sscanf(str + next, "%d,%d%n", &tmp_int[0], &tmp_int[1], &len) != 2) + return -6; + p->skill[tmp_int[0]].id = tmp_int[0]; + p->skill[tmp_int[0]].lv = tmp_int[1]; + next += len; + if (str[next] == ' ') + next++; + } + + next++; + + for(i = 0; str[next] && str[next] != '\t' && str[next] != '\n' && str[next] != '\r'; i++) { // global_reg実装以前のathena.txt互換のため一応'\n'チェック + if (sscanf(str + next, "%[^,],%d%n", p->global_reg[i].str, &p->global_reg[i].value, &len) != 2) { + // because some scripts are not correct, the str can be "". So, we must check that. + // If it's, we must not refuse the character, but just this REG value. + // Character line will have something like: nov_2nd_cos,9 ,9 nov_1_2_cos_c,1 (here, ,9 is not good) + if (str[next] == ',' && sscanf(str + next, ",%d%n", &p->global_reg[i].value, &len) == 1) + i--; + else + return -7; + } + next += len; + if (str[next] == ' ') + next++; + } + p->global_reg_num = i; + + return 1; +} + +//--------------------------------- +// Function to read characters file +//--------------------------------- +int mmo_char_init(void) { + char line[65536]; + int i; + int ret, line_count; + FILE *fp; + + char_max = 256; + char_dat = calloc(sizeof(struct mmo_charstatus) * 256, 1); + if (!char_dat) { + printf("out of memory: mmo_char_init (calloc of char_dat).\n"); + exit(1); + } + online_chars = calloc(sizeof(int) * 256, 1); + if (!online_chars) { + printf("out of memory: mmo_char_init (calloc of online_chars).\n"); + exit(1); + } + for(i = 0; i < char_max; i++) + online_chars[i] = -1; + + char_num = 0; + + fp = fopen(char_txt, "r"); + if (fp == NULL) { + printf("Characters file not found: %s.\n", char_txt); + char_log("Characters file not found: %s." RETCODE, char_txt); + char_log("Id for the next created character: %d." RETCODE, char_id_count); + return 0; + } + + line_count = 0; + while(fgets(line, sizeof(line)-1, fp)) { + int i, j; + line_count++; + + if (line[0] == '/' && line[1] == '/') + continue; + line[sizeof(line)-1] = '\0'; + + j = 0; + if (sscanf(line, "%d\t%%newid%%%n", &i, &j) == 1 && j > 0) { + if (char_id_count < i) + char_id_count = i; + continue; + } + + if (char_num >= char_max) { + char_max += 256; + char_dat = realloc(char_dat, sizeof(struct mmo_charstatus) * char_max); + if (!char_dat) { + printf("Out of memory: mmo_char_init (realloc of char_dat).\n"); + char_log("Out of memory: mmo_char_init (realloc of char_dat)." RETCODE); + exit(1); + } + online_chars = realloc(online_chars, sizeof(int) * char_max); + if (!online_chars) { + printf("Out of memory: mmo_char_init (realloc of online_chars).\n"); + char_log("Out of memory: mmo_char_init (realloc of online_chars)." RETCODE); + exit(1); + } + for(i = char_max - 256; i < char_max; i++) + online_chars[i] = -1; + } + + ret = mmo_char_fromstr(line, &char_dat[char_num]); + if (ret > 0) { // negative value or zero for errors + if (char_dat[char_num].char_id >= char_id_count) + char_id_count = char_dat[char_num].char_id + 1; + char_num++; + } else { + printf("mmo_char_init: in characters file, unable to read the line #%d.\n", line_count); + printf(" -> Character saved in log file.\n"); + switch (ret) { + case -1: + char_log("Duplicate character id in the next character line (character not readed):" RETCODE); + break; + case -2: + char_log("Duplicate character name in the next character line (character not readed):" RETCODE); + break; + case -3: + char_log("Invalid memo point structure in the next character line (character not readed):" RETCODE); + break; + case -4: + char_log("Invalid inventory item structure in the next character line (character not readed):" RETCODE); + break; + case -5: + char_log("Invalid cart item structure in the next character line (character not readed):" RETCODE); + break; + case -6: + char_log("Invalid skill structure in the next character line (character not readed):" RETCODE); + break; + case -7: + char_log("Invalid register structure in the next character line (character not readed):" RETCODE); + break; + default: // 0 + char_log("Unabled to get a character in the next line - Basic structure of line (before inventory) is incorrect (character not readed):" RETCODE); + break; + } + char_log("%s", line); + } + } + fclose(fp); + + if (char_num == 0) { + printf("mmo_char_init: No character found in %s.\n", char_txt); + char_log("mmo_char_init: No character found in %s." RETCODE, char_txt); + } else if (char_num == 1) { + printf("mmo_char_init: 1 character read in %s.\n", char_txt); + char_log("mmo_char_init: 1 character read in %s." RETCODE, char_txt); + } else { + printf("mmo_char_init: %d characters read in %s.\n", char_num, char_txt); + char_log("mmo_char_init: %d characters read in %s." RETCODE, char_num, char_txt); + } + + char_log("Id for the next created character: %d." RETCODE, char_id_count); + + return 0; +} + +//--------------------------------------------------------- +// Function to save characters in files (speed up by [Yor]) +//--------------------------------------------------------- +void mmo_char_sync(void) { + char line[65536]; + int i, j, k; + int lock; + FILE *fp; + int id[char_num]; + + // Sorting before save (by [Yor]) + for(i = 0; i < char_num; i++) { + id[i] = i; + for(j = 0; j < i; j++) { + if ((char_dat[i].account_id < char_dat[id[j]].account_id) || + // if same account id, we sort by slot. + (char_dat[i].account_id == char_dat[id[j]].account_id && + char_dat[i].char_num < char_dat[id[j]].char_num)) { + for(k = i; k > j; k--) + id[k] = id[k-1]; + id[j] = i; // id[i] + break; + } + } + } + + // Data save + fp = lock_fopen(char_txt, &lock); + if (fp == NULL) { + printf("WARNING: Server can't not save characters.\n"); + char_log("WARNING: Server can't not save characters." RETCODE); + } else { + for(i = 0; i < char_num; i++) { + // create only once the line, and save it in the 2 files (it's speeder than repeat twice the loop and create twice the line) + mmo_char_tostr(line, &char_dat[id[i]]); // use of sorted index + fprintf(fp, "%s" RETCODE, line); + } + fprintf(fp, "%d\t%%newid%%" RETCODE, char_id_count); + lock_fclose(fp, char_txt, &lock); + } + + // Data save (backup) + if (backup_txt_flag) { // The backup_txt file was created because char deletion bug existed. Now it's finish and that take a lot of time to create a second file when there are a lot of characters. => option By [Yor] + fp = lock_fopen(backup_txt, &lock); + if (fp == NULL) { + printf("WARNING: Server can't not create backup of characters file.\n"); + char_log("WARNING: Server can't not create backup of characters file." RETCODE); + return; + } + for(i = 0; i < char_num; i++) { + // create only once the line, and save it in the 2 files (it's speeder than repeat twice the loop and create twice the line) + mmo_char_tostr(line, &char_dat[id[i]]); // use of sorted index + fprintf(fp, "%s" RETCODE, line); + } + fprintf(fp, "%d\t%%newid%%" RETCODE, char_id_count); + lock_fclose(fp, backup_txt, &lock); + } + + return; +} + +//---------------------------------------------------- +// Function to save (in a periodic way) datas in files +//---------------------------------------------------- +int mmo_char_sync_timer(int tid, unsigned int tick, int id, int data) { + mmo_char_sync(); + inter_save(); + return 0; +} + +//----------------------------------- +// Function to create a new character +//----------------------------------- +int make_new_char(int fd, unsigned char *dat) { + int i, j; + struct char_session_data *sd; + + sd = session[fd]->session_data; + + // remove control characters from the name + dat[23] = '\0'; + if (remove_control_chars(dat)) { + char_log("Make new char error (control char received in the name): (connection #%d, account: %d)." RETCODE, + fd, sd->account_id); + return -1; + } + + // check lenght of character name + if (strlen(dat) < 4) { + char_log("Make new char error (character name too small): (connection #%d, account: %d, name: '%s')." RETCODE, + fd, sd->account_id, dat); + return -1; + } + + // Check Authorised letters/symbols in the name of the character + if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised + for (i = 0; dat[i]; i++) + if (strchr(char_name_letters, dat[i]) == NULL) { + char_log("Make new char error (invalid letter in the name): (connection #%d, account: %d), name: %s, invalid letter: %c." RETCODE, + fd, sd->account_id, dat, dat[i]); + return -1; + } + } else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden + for (i = 0; dat[i]; i++) + if (strchr(char_name_letters, dat[i]) != NULL) { + char_log("Make new char error (invalid letter in the name): (connection #%d, account: %d), name: %s, invalid letter: %c." RETCODE, + fd, sd->account_id, dat, dat[i]); + return -1; + } + } // else, all letters/symbols are authorised (except control char removed before) + + if (dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29] != 5*6 || // stats + dat[30] >= 9 || // slots (dat[30] can not be negativ) + dat[33] <= 0 || dat[33] >= 20 || // hair style + dat[31] >= 12) { // hair color (dat[31] can not be negativ) + char_log("Make new char error (invalid values): (connection #%d, account: %d) slot %d, name: %s, stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d" RETCODE, + fd, sd->account_id, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + return -1; + } + + // check individual stat value + for(i = 24; i <= 29; i++) { + if (dat[i] < 1 || dat[i] > 9) { + char_log("Make new char error (invalid stat value: not between 1 to 9): (connection #%d, account: %d) slot %d, name: %s, stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d" RETCODE, + fd, sd->account_id, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + return -1; + } + } + + for(i = 0; i < char_num; i++) { + if ((name_ignoring_case != 0 && strcmp(char_dat[i].name, dat) == 0) || + (name_ignoring_case == 0 && strcmpi(char_dat[i].name, dat) == 0)) { + char_log("Make new char error (name already exists): (connection #%d, account: %d) slot %d, name: %s (actual name of other char: %d), stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d." RETCODE, + fd, sd->account_id, dat[30], dat, char_dat[i].name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + return -1; + } + if (char_dat[i].account_id == sd->account_id && char_dat[i].char_num == dat[30]) { + char_log("Make new char error (slot already used): (connection #%d, account: %d) slot %d, name: %s (actual name of other char: %d), stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d." RETCODE, + fd, sd->account_id, dat[30], dat, char_dat[i].name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + return -1; + } + } + + if (strcmp(wisp_server_name, dat) == 0) { + char_log("Make new char error (name used is wisp name for server): (connection #%d, account: %d) slot %d, name: %s (actual name of other char: %d), stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d." RETCODE, + fd, sd->account_id, dat[30], dat, char_dat[i].name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + return -1; + } + + if (char_num >= char_max) { + char_max += 256; + char_dat = realloc(char_dat, sizeof(struct mmo_charstatus) * char_max); + if (!char_dat) { + printf("Out of memory: make_new_char (realloc of char_dat).\n"); + char_log("Out of memory: make_new_char (realloc of char_dat)." RETCODE); + exit(1); + } + online_chars = realloc(online_chars, sizeof(int) * char_max); + if (!online_chars) { + printf("Out of memory: make_new_char (realloc of online_chars).\n"); + char_log("Out of memory: make_new_char (realloc of online_chars)." RETCODE); + exit(1); + } + for(j = char_max - 256; j < char_max; j++) + online_chars[j] = -1; + } + + char_log("Creation of New Character: (connection #%d, account: %d) slot %d, character Name: %s, stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d." RETCODE, + fd, sd->account_id, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + + memset(&char_dat[i], 0, sizeof(struct mmo_charstatus)); + + char_dat[i].char_id = char_id_count++; + char_dat[i].account_id = sd->account_id; + char_dat[i].char_num = dat[30]; + strcpy(char_dat[i].name, dat); + char_dat[i].class = 0; + char_dat[i].base_level = 1; + char_dat[i].job_level = 1; + char_dat[i].base_exp = 0; + char_dat[i].job_exp = 0; + char_dat[i].zeny = start_zeny; + char_dat[i].str = dat[24]; + char_dat[i].agi = dat[25]; + char_dat[i].vit = dat[26]; + char_dat[i].int_ = dat[27]; + char_dat[i].dex = dat[28]; + char_dat[i].luk = dat[29]; + char_dat[i].max_hp = 40 * (100 + char_dat[i].vit) / 100; + char_dat[i].max_sp = 11 * (100 + char_dat[i].int_) / 100; + char_dat[i].hp = char_dat[i].max_hp; + char_dat[i].sp = char_dat[i].max_sp; + char_dat[i].status_point = 0; + char_dat[i].skill_point = 0; + char_dat[i].option = 0; + char_dat[i].karma = 0; + char_dat[i].manner = 0; + char_dat[i].party_id = 0; + char_dat[i].guild_id = 0; + char_dat[i].hair = dat[33]; + char_dat[i].hair_color = dat[31]; + char_dat[i].clothes_color = 0; + char_dat[i].inventory[0].nameid = start_weapon; // Knife + char_dat[i].inventory[0].amount = 1; + char_dat[i].inventory[0].equip = 0x02; + char_dat[i].inventory[0].identify = 1; + char_dat[i].inventory[0].broken = 0; + char_dat[i].inventory[1].nameid = start_armor; // Cotton Shirt + char_dat[i].inventory[1].amount = 1; + char_dat[i].inventory[1].equip = 0x10; + char_dat[i].inventory[1].identify = 1; + char_dat[i].inventory[1].broken = 0; + char_dat[i].weapon = 1; + char_dat[i].shield = 0; + char_dat[i].head_top = 0; + char_dat[i].head_mid = 0; + char_dat[i].head_bottom = 0; + memcpy(&char_dat[i].last_point, &start_point, sizeof(start_point)); + memcpy(&char_dat[i].save_point, &start_point, sizeof(start_point)); + char_num++; + + mmo_char_sync(); + return i; +} + +//---------------------------------------------------- +// This function return the name of the job (by [Yor]) +//---------------------------------------------------- +char * job_name(int class) { + switch (class) { + case 0: return "Novice"; + case 1: return "Swordsman"; + case 2: return "Mage"; + case 3: return "Archer"; + case 4: return "Acolyte"; + case 5: return "Merchant"; + case 6: return "Thief"; + case 7: return "Knight"; + case 8: return "Priest"; + case 9: return "Wizard"; + case 10: return "Blacksmith"; + case 11: return "Hunter"; + case 12: return "Assassin"; + case 13: return "Knight 2"; + case 14: return "Crusader"; + case 15: return "Monk"; + case 16: return "Sage"; + case 17: return "Rogue"; + case 18: return "Alchemist"; + case 19: return "Bard"; + case 20: return "Dancer"; + case 21: return "Crusader 2"; + case 22: return "Wedding"; + case 23: return "Super Novice"; + case 4001: return "Novice High"; + case 4002: return "Swordsman High"; + case 4003: return "Mage High"; + case 4004: return "Archer High"; + case 4005: return "Acolyte High"; + case 4006: return "Merchant High"; + case 4007: return "Thief High"; + case 4008: return "Lord Knight"; + case 4009: return "High Priest"; + case 4010: return "High Wizard"; + case 4011: return "Whitesmith"; + case 4012: return "Sniper"; + case 4013: return "Assassin Cross"; + case 4014: return "Peko Knight"; + case 4015: return "Paladin"; + case 4016: return "Champion"; + case 4017: return "Professor"; + case 4018: return "Stalker"; + case 4019: return "Creator"; + case 4020: return "Clown"; + case 4021: return "Gypsy"; + case 4022: return "Peko Paladin"; + case 4023: return "Baby Novice"; + case 4024: return "Baby Swordsman"; + case 4025: return "Baby Mage"; + case 4026: return "Baby Archer"; + case 4027: return "Baby Acolyte"; + case 4028: return "Baby Merchant"; + case 4029: return "Baby Thief"; + case 4030: return "Baby Knight"; + case 4031: return "Baby Priest"; + case 4032: return "Baby Wizard"; + case 4033: return "Baby Blacksmith"; + case 4034: return "Baby Hunter"; + case 4035: return "Baby Assassin"; + case 4036: return "Baby Peco Knight"; + case 4037: return "Baby Crusader"; + case 4038: return "Baby Monk"; + case 4039: return "Baby Sage"; + case 4040: return "Baby Rogue"; + case 4041: return "Baby Alchemist"; + case 4042: return "Baby Bard"; + case 4043: return "Baby Dancer"; + case 4044: return "Baby Peco Crusader"; + case 4045: return "Super Baby"; + } + return "Unknown Job"; +} + +//------------------------------------------------------------- +// Function to create the online files (txt and html). by [Yor] +//------------------------------------------------------------- +void create_online_files(void) { + int i, j, k, l; // for loops + int players; // count the number of players + FILE *fp; // for the txt file + FILE *fp2; // for the html file + char temp[256]; // to prepare what we must display + time_t time_server; // for number of seconds + struct tm *datetime; // variable for time in structure ->tm_mday, ->tm_sec, ... + int id[char_num]; + + if (online_display_option == 0) // we display nothing, so return + return; + + //char_log("Creation of online players files." RETCODE); + + // Get number of online players, id of each online players + players = 0; + // sort online characters. + for(i = 0; i < char_num; i++) { + if (online_chars[i] != -1) { + id[players] = i; + // use sorting option + switch (online_sorting_option) { + case 1: // by name (without case sensitive) + { + char *p_name = char_dat[i].name; //speed up sorting when there are a lot of players. But very rarely players have same name. + for(j = 0; j < players; j++) + if (stricmp(p_name, char_dat[id[j]].name) < 0 || + // if same name, we sort with case sensitive. + (stricmp(p_name, char_dat[id[j]].name) == 0 && + strcmp(p_name, char_dat[id[j]].name) < 0)) { + for(k = players; k > j; k--) + id[k] = id[k-1]; + id[j] = i; // id[players] + break; + } + } + break; + case 2: // by zeny + for(j = 0; j < players; j++) + if (char_dat[i].zeny < char_dat[id[j]].zeny || + // if same number of zenys, we sort by name. + (char_dat[i].zeny == char_dat[id[j]].zeny && + stricmp(char_dat[i].name, char_dat[id[j]].name) < 0)) { + for(k = players; k > j; k--) + id[k] = id[k-1]; + id[j] = i; // id[players] + break; + } + break; + case 3: // by base level + for(j = 0; j < players; j++) + if (char_dat[i].base_level < char_dat[id[j]].base_level || + // if same base level, we sort by base exp. + (char_dat[i].base_level == char_dat[id[j]].base_level && + char_dat[i].base_exp < char_dat[id[j]].base_exp)) { + for(k = players; k > j; k--) + id[k] = id[k-1]; + id[j] = i; // id[players] + break; + } + break; + case 4: // by job (and job level) + for(j = 0; j < players; j++) + if (char_dat[i].class < char_dat[id[j]].class || + // if same job, we sort by job level. + (char_dat[i].class == char_dat[id[j]].class && + char_dat[i].job_level < char_dat[id[j]].job_level) || + // if same job and job level, we sort by job exp. + (char_dat[i].class == char_dat[id[j]].class && + char_dat[i].job_level == char_dat[id[j]].job_level && + char_dat[i].job_exp < char_dat[id[j]].job_exp)) { + for(k = players; k > j; k--) + id[k] = id[k-1]; + id[j] = i; // id[players] + break; + } + break; + case 5: // by location map name + { + int cpm_result; // A lot of player maps are identical. So, test if done often twice. + for(j = 0; j < players; j++) + if ((cpm_result = strcmp(char_dat[i].last_point.map, char_dat[id[j]].last_point.map)) < 0 || // no map are identical and with upper cases (not use stricmp) + // if same map name, we sort by name. + (cpm_result == 0 && + stricmp(char_dat[i].name, char_dat[id[j]].name) < 0)) { + for(k = players; k > j; k--) + id[k] = id[k-1]; + id[j] = i; // id[players] + break; + } + } + break; + default: // 0 or invalid value: no sorting + break; + } + players++; + } + } + + // write files + fp = fopen(online_txt_filename, "w"); + if (fp != NULL) { + fp2 = fopen(online_html_filename, "w"); + if (fp2 != NULL) { + // get time + time(&time_server); // get time in seconds since 1/1/1970 + datetime = localtime(&time_server); // convert seconds in structure + strftime(temp, sizeof(temp), "%d %b %Y %X", datetime); // like sprintf, but only for date/time (05 dec 2003 15:12:52) + // write heading + fprintf(fp2, "\n"); + fprintf(fp2, " \n", online_refresh_html); // update on client explorer every x seconds + fprintf(fp2, " \n"); + fprintf(fp2, " Online Players on %s\n", server_name); + fprintf(fp2, " \n"); + fprintf(fp2, " \n"); + fprintf(fp2, "

Online Players on %s (%s):

\n", server_name, temp); + fprintf(fp, "Online Players on %s (%s):\n", server_name, temp); + fprintf(fp, "\n"); + + // If we display at least 1 player + if (players > 0) { + j = 0; // count the number of characters for the txt version and to set the separate line + fprintf(fp2, " \n"); + fprintf(fp2, " \n"); + if ((online_display_option & 1) || (online_display_option & 64)) { + fprintf(fp2, " \n"); + if (online_display_option & 64) { + fprintf(fp, "Name "); // 30 + j += 30; + } else { + fprintf(fp, "Name "); // 25 + j += 25; + } + } + if ((online_display_option & 6) == 6) { + fprintf(fp2, " \n"); + fprintf(fp, "Job Levels "); // 27 + j += 27; + } else if (online_display_option & 2) { + fprintf(fp2, " \n"); + fprintf(fp, "Job "); // 19 + j += 19; + } else if (online_display_option & 4) { + fprintf(fp2, " \n"); + fprintf(fp, " Levels "); // 8 + j += 8; + } + if (online_display_option & 24) { // 8 or 16 + fprintf(fp2, " \n"); + if (online_display_option & 16) { + fprintf(fp, "Location ( x , y ) "); // 23 + j += 23; + } else { + fprintf(fp, "Location "); // 13 + j += 13; + } + } + if (online_display_option & 32) { + fprintf(fp2, " \n"); + fprintf(fp, " Zenys "); // 16 + j += 16; + } + fprintf(fp2, " \n"); + fprintf(fp, "\n"); + for (k = 0; k < j; k++) + fprintf(fp, "-"); + fprintf(fp, "\n"); + + // display each player. + for (i = 0; i < players; i++) { + // get id of the character (more speed) + j = id[i]; + fprintf(fp2, " \n"); + // displaying the character name + if ((online_display_option & 1) || (online_display_option & 64)) { // without/with 'GM' display + strcpy(temp, char_dat[j].name); + l = isGM(char_dat[j].account_id); + if (online_display_option & 64) { + if (l >= online_gm_display_min_level) + fprintf(fp, "%-24s (GM) ", temp); + else + fprintf(fp, "%-24s ", temp); + } else + fprintf(fp, "%-24s ", temp); + // name of the character in the html (no < >, because that create problem in html code) + fprintf(fp2, " \n"); + } + // displaying of the job + if (online_display_option & 6) { + char * jobname = job_name(char_dat[j].class); + if ((online_display_option & 6) == 6) { + fprintf(fp2, " \n", jobname, char_dat[j].base_level, char_dat[j].job_level); + fprintf(fp, "%-18s %3d/%3d ", jobname, char_dat[j].base_level, char_dat[j].job_level); + } else if (online_display_option & 2) { + fprintf(fp2, " \n", jobname); + fprintf(fp, "%-18s ", jobname); + } else if (online_display_option & 4) { + fprintf(fp2, " \n", char_dat[j].base_level, char_dat[j].job_level); + fprintf(fp, "%3d/%3d ", char_dat[j].base_level, char_dat[j].job_level); + } + } + // displaying of the map + if (online_display_option & 24) { // 8 or 16 + // prepare map name + memset(temp, 0, sizeof(temp)); + strncpy(temp, char_dat[j].last_point.map, 16); + if (strchr(temp, '.') != NULL) + temp[strchr(temp, '.') - temp] = '\0'; // suppress the '.gat' + // write map name + if (online_display_option & 16) { // map-name AND coordonates + fprintf(fp2, " \n", temp, char_dat[j].last_point.x, char_dat[j].last_point.y); + fprintf(fp, "%-12s (%3d,%3d) ", temp, char_dat[j].last_point.x, char_dat[j].last_point.y); + } else { + fprintf(fp2, " \n", temp); + fprintf(fp, "%-12s ", temp); + } + } + // displaying number of zenys + if (online_display_option & 32) { + // write number of zenys + if (char_dat[j].zeny == 0) { // if no zeny + fprintf(fp2, " \n"); + fprintf(fp, " no zeny "); + } else { + fprintf(fp2, " \n", char_dat[j].zeny); + fprintf(fp, "%13d z ", char_dat[j].zeny); + } + } + fprintf(fp, "\n"); + fprintf(fp2, " \n"); + } + fprintf(fp2, "
NameJob (levels)JobLevelsLocationzenys
"); + if ((online_display_option & 64) && l >= online_gm_display_min_level) + fprintf(fp2, ""); + for (k = 0; temp[k]; k++) { + switch(temp[k]) { + case '<': // < + fprintf(fp2, "<"); + break; + case '>': // > + fprintf(fp2, ">"); + break; + default: + fprintf(fp2, "%c", temp[k]); + break; + }; + } + if ((online_display_option & 64) && l >= online_gm_display_min_level) + fprintf(fp2, " (GM)"); + fprintf(fp2, "%s %d/%d%s%d/%d%s (%d, %d)%sno zeny%d z
\n"); + fprintf(fp, "\n"); + } + + // Displaying number of online players + if (players == 0) { + fprintf(fp2, "

No user is online.

\n"); + fprintf(fp, "No user is online.\n"); + // no display if only 1 player + } else if (players == 1) { + } else { + fprintf(fp2, "

%d users are online.

\n", players); + fprintf(fp, "%d users are online.\n", players); + } + fprintf(fp2, " \n"); + fprintf(fp2, "\n"); + fclose(fp2); + } + fclose(fp); + } + + return; +} + +//--------------------------------------------------------------------- +// This function return the number of online players in all map-servers +//--------------------------------------------------------------------- +int count_users(void) { + int i, users; + + users = 0; + for(i = 0; i < MAX_MAP_SERVERS; i++) + if (server_fd[i] >= 0) + users += server[i].users; + + return users; +} + +//---------------------------------------- +// Function to send characters to a player +//---------------------------------------- +int mmo_char_send006b(int fd, struct char_session_data *sd) { + int i, j, found_num; + struct mmo_charstatus *p; +#ifdef NEW_006b + const int offset = 24; +#else + const int offset = 4; +#endif + + found_num = 0; + for(i = 0; i < char_num; i++) { + if (char_dat[i].account_id == sd->account_id) { + sd->found_char[found_num] = i; + found_num++; + if (found_num == 9) + break; + } + } + for(i = found_num; i < 9; i++) + sd->found_char[i] = -1; + + memset(WFIFOP(fd,0), 0, offset + found_num * 106); + WFIFOW(fd,0) = 0x6b; + WFIFOW(fd,2) = offset + found_num * 106; + + for(i = 0; i < found_num; i++) { + p = &char_dat[sd->found_char[i]]; + j = offset + (i * 106); // increase speed of code + + WFIFOL(fd,j) = p->char_id; + WFIFOL(fd,j+4) = p->base_exp; + WFIFOL(fd,j+8) = p->zeny; + WFIFOL(fd,j+12) = p->job_exp; + WFIFOL(fd,j+16) = p->job_level; + + WFIFOL(fd,j+20) = 0; + WFIFOL(fd,j+24) = 0; + WFIFOL(fd,j+28) = p->option; + + WFIFOL(fd,j+32) = p->karma; + WFIFOL(fd,j+36) = p->manner; + + WFIFOW(fd,j+40) = p->status_point; + WFIFOW(fd,j+42) = (p->hp > 0x7fff) ? 0x7fff : p->hp; + WFIFOW(fd,j+44) = (p->max_hp > 0x7fff) ? 0x7fff : p->max_hp; + WFIFOW(fd,j+46) = (p->sp > 0x7fff) ? 0x7fff : p->sp; + WFIFOW(fd,j+48) = (p->max_sp > 0x7fff) ? 0x7fff : p->max_sp; + WFIFOW(fd,j+50) = DEFAULT_WALK_SPEED; // p->speed; + WFIFOW(fd,j+52) = p->class; + WFIFOW(fd,j+54) = p->hair; + WFIFOW(fd,j+56) = p->weapon; + WFIFOW(fd,j+58) = p->base_level; + WFIFOW(fd,j+60) = p->skill_point; + WFIFOW(fd,j+62) = p->head_bottom; + WFIFOW(fd,j+64) = p->shield; + WFIFOW(fd,j+66) = p->head_top; + WFIFOW(fd,j+68) = p->head_mid; + WFIFOW(fd,j+70) = p->hair_color; + WFIFOW(fd,j+72) = p->clothes_color; + + memcpy(WFIFOP(fd,j+74), p->name, 24); + + WFIFOB(fd,j+98) = (p->str > 255) ? 255 : p->str; + WFIFOB(fd,j+99) = (p->agi > 255) ? 255 : p->agi; + WFIFOB(fd,j+100) = (p->vit > 255) ? 255 : p->vit; + WFIFOB(fd,j+101) = (p->int_ > 255) ? 255 : p->int_; + WFIFOB(fd,j+102) = (p->dex > 255) ? 255 : p->dex; + WFIFOB(fd,j+103) = (p->luk > 255) ? 255 : p->luk; + WFIFOB(fd,j+104) = p->char_num; + } + + WFIFOSET(fd,WFIFOW(fd,2)); + + return 0; +} + +int set_account_reg2(int acc, int num, struct global_reg *reg) { + int i, c; + + c = 0; + for(i = 0; i < char_num; i++) { + if (char_dat[i].account_id == acc) { + memcpy(char_dat[i].account_reg2, reg, sizeof(char_dat[i].account_reg2)); + char_dat[i].account_reg2_num = num; + c++; + } + } + return c; +} + +// 離婚(char削除時に使用) +int char_divorce(struct mmo_charstatus *cs) { + if (cs == NULL) + return 0; + + if (cs->partner_id > 0){ + int i, j; + for(i = 0; i < char_num; i++) { + if (char_dat[i].char_id == cs->partner_id && char_dat[i].partner_id == cs->char_id) { + cs->partner_id = 0; + char_dat[i].partner_id = 0; + for(j = 0; j < MAX_INVENTORY; j++) + if (char_dat[i].inventory[j].nameid == WEDDING_RING_M || char_dat[i].inventory[j].nameid == WEDDING_RING_F) + memset(&char_dat[i].inventory[j], 0, sizeof(char_dat[i].inventory[0])); + if (cs->inventory[j].nameid == WEDDING_RING_M || cs->inventory[j].nameid == WEDDING_RING_F) + memset(&cs->inventory[j], 0, sizeof(cs->inventory[0])); + return 0; + } + } + } + return 0; +} + +//------------------------------------------------------------ +// E-mail check: return 0 (not correct) or 1 (valid). by [Yor] +//------------------------------------------------------------ +int e_mail_check(unsigned char *email) { + char ch; + unsigned char* last_arobas; + + // athena limits + if (strlen(email) < 3 || strlen(email) > 39) + return 0; + + // part of RFC limits (official reference of e-mail description) + if (strchr(email, '@') == NULL || email[strlen(email)-1] == '@') + return 0; + + if (email[strlen(email)-1] == '.') + return 0; + + last_arobas = strrchr(email, '@'); + + if (strstr(last_arobas, "@.") != NULL || + strstr(last_arobas, "..") != NULL) + return 0; + + for(ch = 1; ch < 32; ch++) { + if (strchr(last_arobas, ch) != NULL) { + return 0; + break; + } + } + + if (strchr(last_arobas, ' ') != NULL || + strchr(last_arobas, ';') != NULL) + return 0; + + // all correct + return 1; +} + +//---------------------------------------------------------------------- +// Force disconnection of an online player (with account value) by [Yor] +//---------------------------------------------------------------------- +int disconnect_player(int accound_id) { + int i; + struct char_session_data *sd; + + // disconnect player if online on char-server + for(i = 0; i < fd_max; i++) { + if (session[i] && (sd = session[i]->session_data)) { + if (sd->account_id == accound_id) { + session[i]->eof = 1; + return 1; + } + } + } + + return 0; +} + +// キャラ削除に伴うデータ削除 +static int char_delete(struct mmo_charstatus *cs) { + int j; + + // ペット削除 + if (cs->pet_id) + inter_pet_delete(cs->pet_id); + for (j = 0; j < MAX_INVENTORY; j++) + if (cs->inventory[j].card[0] == (short)0xff00) + inter_pet_delete(*((long *)(&cs->inventory[j].card[2]))); + for (j = 0; j < MAX_CART; j++) + if (cs->cart[j].card[0] == (short)0xff00) + inter_pet_delete(*((long *)(&cs->cart[j].card[2]))); + // ギルド脱退 + if (cs->guild_id) + inter_guild_leave(cs->guild_id, cs->account_id, cs->char_id); + // パーティー脱退 + if (cs->party_id) + inter_party_leave(cs->party_id, cs->account_id); + // 離婚 + if (cs->partner_id){ + // 離婚情報をmapに通知 + char buf[10]; + WBUFW(buf,0) = 0x2b12; + WBUFL(buf,2) = cs->char_id; + WBUFL(buf,6) = cs->partner_id; + mapif_sendall(buf,10); + // 離婚 + char_divorce(cs); + } + return 0; +} + +int parse_tologin(int fd) { + int i; + struct char_session_data *sd; + + // only login-server can have an access to here. + // so, if it isn't the login-server, we disconnect the session (fd != login_fd). + if (fd != login_fd || session[fd]->eof) { + if (fd == login_fd) { + printf("Char-server can't connect to login-server (connection #%d).\n", fd); + login_fd = -1; + } + close(fd); + delete_session(fd); + return 0; + } + + sd = session[fd]->session_data; + + while(RFIFOREST(fd) >= 2) { +// printf("parse_tologin: connection #%d, packet: 0x%x (with being read: %d bytes).\n", fd, RFIFOW(fd,0), RFIFOREST(fd)); + + switch(RFIFOW(fd,0)) { + case 0x2711: + if (RFIFOREST(fd) < 3) + return 0; + if (RFIFOB(fd,2)) { +// printf("connect login server error : %d\n", RFIFOB(fd,2)); + printf("Can not connect to login-server.\n"); + printf("The server communication passwords (default s1/p1) is probably invalid.\n"); + printf("Also, please make sure your accounts file (default: accounts.txt) has those values present.\n"); + printf("If you changed the communication passwords, change them back at map_athena.conf and char_athena.conf\n"); + exit(1); + } else { + printf("Connected to login-server (connection #%d).\n", fd); + // if no map-server already connected, display a message... + for(i = 0; i < MAX_MAP_SERVERS; i++) + if (server_fd[i] >= 0 && server[i].map[0][0]) // if map-server online and at least 1 map + break; + if (i == MAX_MAP_SERVERS) + printf("Awaiting maps from map-server.\n"); + } + RFIFOSKIP(fd,3); + break; + + case 0x2713: + if (RFIFOREST(fd) < 51) + return 0; +// printf("parse_tologin 2713 : %d\n", RFIFOB(fd,6)); + for(i = 0; i < fd_max; i++) { + if (session[i] && (sd = session[i]->session_data) && sd->account_id == RFIFOL(fd,2)) { + if (RFIFOB(fd,6) != 0) { + WFIFOW(i,0) = 0x6c; + WFIFOB(i,2) = 0x42; + WFIFOSET(i,3); + } else if (max_connect_user == 0 || count_users() < max_connect_user) { +// if (max_connect_user == 0) +// printf("max_connect_user (unlimited) -> accepted.\n"); +// else +// printf("count_users(): %d < max_connect_user (%d) -> accepted.\n", count_users(), max_connect_user); + memcpy(sd->email, RFIFOP(fd, 7), 40); + if (e_mail_check(sd->email) == 0) + strncpy(sd->email, "a@a.com", 40); // default e-mail + sd->connect_until_time = (time_t)RFIFOL(fd,47); + // send characters to player + mmo_char_send006b(i, sd); + } else { + // refuse connection: too much online players +// printf("count_users(): %d < max_connect_use (%d) -> fail...\n", count_users(), max_connect_user); + WFIFOW(i,0) = 0x6c; + WFIFOW(i,2) = 0; + WFIFOSET(i,3); + } + break; + } + } + RFIFOSKIP(fd,51); + break; + + // Receiving of an e-mail/time limit from the login-server (answer of a request because a player comes back from map-server to char-server) by [Yor] + case 0x2717: + if (RFIFOREST(fd) < 50) + return 0; + for(i = 0; i < fd_max; i++) { + if (session[i] && (sd = session[i]->session_data)) { + if (sd->account_id == RFIFOL(fd,2)) { + memcpy(sd->email, RFIFOP(fd,6), 40); + if (e_mail_check(sd->email) == 0) + strncpy(sd->email, "a@a.com", 40); // default e-mail + sd->connect_until_time = (time_t)RFIFOL(fd,46); + break; + } + } + } + RFIFOSKIP(fd,50); + break; + + case 0x2721: // gm reply + if (RFIFOREST(fd) < 10) + return 0; + { + unsigned char buf[10]; + WBUFW(buf,0) = 0x2b0b; + WBUFL(buf,2) = RFIFOL(fd,2); // account + WBUFL(buf,6) = RFIFOL(fd,6); // GM level + mapif_sendall(buf,10); +// printf("parse_tologin: To become GM answer: char -> map.\n"); + } + RFIFOSKIP(fd,10); + break; + + case 0x2723: // changesex reply (modified by [Yor]) + if (RFIFOREST(fd) < 7) + return 0; + { + int acc, sex, i, j; + unsigned char buf[7]; + acc = RFIFOL(fd,2); + sex = RFIFOB(fd,6); + RFIFOSKIP(fd, 7); + if (acc > 0) { + for (i = 0; i < char_num; i++) { + if (char_dat[i].account_id == acc) { + int jobclass = char_dat[i].class; + char_dat[i].sex = sex; + auth_fifo[i].sex = sex; + if (jobclass == 19 || jobclass == 20 || + jobclass == 4020 || jobclass == 4021 || + jobclass == 4042 || jobclass == 4043) { + // job modification + if (jobclass == 19 || jobclass == 20) { + char_dat[i].class = (sex) ? 19 : 20; + } else if (jobclass == 4020 || jobclass == 4021) { + char_dat[i].class = (sex) ? 4020 : 4021; + } else if (jobclass == 4042 || jobclass == 4043) { + char_dat[i].class = (sex) ? 4042 : 4043; + } + // remove specifical skills of classes 19, 4020 and 4042 + for(j = 315; j <= 322; j++) { + if (char_dat[i].skill[j].id > 0 && !char_dat[i].skill[j].flag) { + char_dat[i].skill_point += char_dat[i].skill[j].lv; + char_dat[i].skill[j].id = 0; + char_dat[i].skill[j].lv = 0; + } + } + // remove specifical skills of classes 20, 4021 and 4043 + for(j = 323; j <= 330; j++) { + if (char_dat[i].skill[j].id > 0 && !char_dat[i].skill[j].flag) { + char_dat[i].skill_point += char_dat[i].skill[j].lv; + char_dat[i].skill[j].id = 0; + char_dat[i].skill[j].lv = 0; + } + } + } + // to avoid any problem with equipment and invalid sex, equipment is unequiped. + for (j = 0; j < MAX_INVENTORY; j++) { + if (char_dat[i].inventory[j].nameid && char_dat[i].inventory[j].equip) + char_dat[i].inventory[j].equip = 0; + } + char_dat[i].weapon = 0; + char_dat[i].shield = 0; + char_dat[i].head_top = 0; + char_dat[i].head_mid = 0; + char_dat[i].head_bottom = 0; + } + } + // disconnect player if online on char-server + disconnect_player(acc); + } + WBUFW(buf,0) = 0x2b0d; + WBUFL(buf,2) = acc; + WBUFB(buf,6) = sex; + mapif_sendall(buf, 7); + } + break; + + case 0x2726: // Request to send a broadcast message (no answer) + if (RFIFOREST(fd) < 8 || RFIFOREST(fd) < (8 + RFIFOL(fd,4))) + return 0; + if (RFIFOL(fd,4) < 1) + char_log("Receiving a message for broadcast, but message is void." RETCODE); + else { + // at least 1 map-server + for(i = 0; i < MAX_MAP_SERVERS; i++) + if (server_fd[i] >= 0) + break; + if (i == MAX_MAP_SERVERS) + char_log("'ladmin': Receiving a message for broadcast, but no map-server is online." RETCODE); + else { + char buf[128]; + char message[RFIFOL(fd,4) + 1]; // +1 to add a null terminated if not exist in the packet + int lp; + char *p; + memset(message, '\0', sizeof(message)); + memcpy(message, RFIFOP(fd,8), RFIFOL(fd,4)); + message[sizeof(message)-1] = '\0'; + remove_control_chars(message); + // remove all first spaces + p = message; + while(p[0] == ' ') + p++; + // if message is only composed of spaces + if (p[0] == '\0') + char_log("Receiving a message for broadcast, but message is only a lot of spaces." RETCODE); + // else send message to all map-servers + else { + if (RFIFOW(fd,2) == 0) { + char_log("'ladmin': Receiving a message for broadcast (message (in yellow): %s)" RETCODE, + message); + lp = 4; + } else { + char_log("'ladmin': Receiving a message for broadcast (message (in blue): %s)" RETCODE, + message); + lp = 8; + } + // split message to max 80 char + while(p[0] != '\0') { // if not finish + if (p[0] == ' ') // jump if first char is a space + p++; + else { + char split[80]; + char* last_space; + sscanf(p, "%79[^\t]", split); // max 79 char, any char (\t is control char and control char was removed before) + split[sizeof(split)-1] = '\0'; // last char always \0 + if ((last_space = strrchr(split, ' ')) != NULL) { // searching space from end of the string + last_space[0] = '\0'; // replace it by NULL to have correct length of split + p++; // to jump the new NULL + } + p += strlen(split); + // send broadcast to all map-servers + WBUFW(buf,0) = 0x3800; + WBUFW(buf,2) = lp + strlen(split) + 1; + WBUFL(buf,4) = 0x65756c62; // only write if in blue (lp = 8) + memcpy(WBUFP(buf,lp), split, strlen(split) + 1); + mapif_sendall(buf, WBUFW(buf,2)); + } + } + } + } + } + RFIFOSKIP(fd,8 + RFIFOL(fd,4)); + break; + + // account_reg2変更通知 + case 0x2729: + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + { + struct global_reg reg[ACCOUNT_REG2_NUM]; + unsigned char buf[4096]; + int j, p, acc; + acc = RFIFOL(fd,4); + for (p = 8, j = 0; p < RFIFOW(fd,2) && j < ACCOUNT_REG2_NUM; p += 36, j++) { + memcpy(reg[j].str, RFIFOP(fd,p), 32); + reg[j].value = RFIFOL(fd,p+32); + } + set_account_reg2(acc, j, reg); + // 同垢ログインを禁止していれば送る必要は無い + memcpy(buf, RFIFOP(fd,0), RFIFOW(fd,2)); + WBUFW(buf,0) = 0x2b11; + mapif_sendall(buf, WBUFW(buf,2)); + RFIFOSKIP(fd,RFIFOW(fd,2)); +// printf("char: save_account_reg_reply\n"); + } + break; + + // Account deletion notification (from login-server) + case 0x2730: + if (RFIFOREST(fd) < 6) + return 0; + // Deletion of all characters of the account + for(i = 0; i < char_num; i++) { + if (char_dat[i].account_id == RFIFOL(fd,2)) { + char_delete(&char_dat[i]); + if (i < char_num - 1) { + memcpy(&char_dat[i], &char_dat[char_num-1], sizeof(struct mmo_charstatus)); + // if moved character owns to deleted account, check again it's character + if (char_dat[i].account_id == RFIFOL(fd,2)) { + i--; + // Correct moved character reference in the character's owner by [Yor] + } else { + int j, k; + struct char_session_data *sd2; + for (j = 0; j < fd_max; j++) { + if (session[j] && (sd2 = session[j]->session_data) && + sd2->account_id == char_dat[char_num-1].account_id) { + for (k = 0; k < 9; k++) { + if (sd2->found_char[k] == char_num-1) { + sd2->found_char[k] = i; + break; + } + } + break; + } + } + } + } + char_num--; + } + } + // Deletion of the storage + inter_storage_delete(RFIFOL(fd,2)); + // send to all map-servers to disconnect the player + { + unsigned char buf[6]; + WBUFW(buf,0) = 0x2b13; + WBUFL(buf,2) = RFIFOL(fd,2); + mapif_sendall(buf, 6); + } + // disconnect player if online on char-server + disconnect_player(RFIFOL(fd,2)); + RFIFOSKIP(fd,6); + break; + + // State change of account/ban notification (from login-server) by [Yor] + case 0x2731: + if (RFIFOREST(fd) < 11) + return 0; + // send to all map-servers to disconnect the player + { + unsigned char buf[11]; + WBUFW(buf,0) = 0x2b14; + WBUFL(buf,2) = RFIFOL(fd,2); + WBUFB(buf,6) = RFIFOB(fd,6); // 0: change of statut, 1: ban + WBUFL(buf,7) = RFIFOL(fd,7); // status or final date of a banishment + mapif_sendall(buf, 11); + } + // disconnect player if online on char-server + disconnect_player(RFIFOL(fd,2)); + RFIFOSKIP(fd,11); + break; + + // Receiving GM acounts info from login-server (by [Yor]) + case 0x2732: + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + { + char buf[32000]; + if (gm_account != NULL) + free(gm_account); + gm_account = calloc(sizeof(struct gm_account) * ((RFIFOW(fd,2) - 4) / 5), 1); + GM_num = 0; + for (i = 4; i < RFIFOW(fd,2); i = i + 5) { + gm_account[GM_num].account_id = RFIFOL(fd,i); + gm_account[GM_num].level = (int)RFIFOB(fd,i+4); + //printf("GM account: %d -> level %d\n", gm_account[GM_num].account_id, gm_account[GM_num].level); + GM_num++; + } + printf("From login-server: receiving of %d GM accounts information.\n", GM_num); + char_log("From login-server: receiving of %d GM accounts information." RETCODE, GM_num); + create_online_files(); // update online players files (perhaps some online players change of GM level) + // send new gm acccounts level to map-servers + memcpy(buf, RFIFOP(fd,0), RFIFOW(fd,2)); + WBUFW(buf,0) = 0x2b15; + mapif_sendall(buf, RFIFOW(fd,2)); + } + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + + default: + session[fd]->eof = 1; + return 0; + } + } + RFIFOFLUSH(fd); + + return 0; +} + +//-------------------------------- +// Map-server anti-freeze system +//-------------------------------- +int map_anti_freeze_system(int tid, unsigned int tick, int id, int data) { + int i; + + //printf("Entering in map_anti_freeze_system function to check freeze of servers.\n"); + for(i = 0; i < MAX_MAP_SERVERS; i++) { + if (server_fd[i] >= 0) {// if map-server is online + //printf("map_anti_freeze_system: server #%d, flag: %d.\n", i, server_freezeflag[i]); + if (server_freezeflag[i]-- < 1) { // Map-server anti-freeze system. Counter. 5 ok, 4...0 freezed + printf("Map-server anti-freeze system: char-server #%d is freezed -> disconnection.\n", i); + char_log("Map-server anti-freeze system: char-server #%d is freezed -> disconnection." RETCODE, + i); + session[server_fd[i]]->eof = 1; + } + } + } + + return 0; +} + +int parse_frommap(int fd) { + int i, j; + int id; + + for(id = 0; id < MAX_MAP_SERVERS; id++) + if (server_fd[id] == fd) + break; + if (id == MAX_MAP_SERVERS || session[fd]->eof) { + if (id < MAX_MAP_SERVERS) { + printf("Map-server %d (session #%d) has disconnected.\n", id, fd); + memset(&server[id], 0, sizeof(struct mmo_map_server)); + server_fd[id] = -1; + for(j = 0; j < char_num; j++) + if (online_chars[j] == fd) + online_chars[j] = -1; + create_online_files(); // update online players files (to remove all online players of this server) + } + close(fd); + delete_session(fd); + return 0; + } + + while(RFIFOREST(fd) >= 2) { +// printf("parse_frommap: connection #%d, packet: 0x%x (with being read: %d bytes).\n", fd, RFIFOW(fd,0), RFIFOREST(fd)); + + switch(RFIFOW(fd,0)) { + // request from map-server to reload GM accounts. Transmission to login-server (by Yor) + case 0x2af7: + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd,0) = 0x2709; + WFIFOSET(login_fd, 2); +// printf("char : request from map-server to reload GM accounts -> login-server.\n"); + } + RFIFOSKIP(fd,2); + break; + + // Receiving map names list from the map-server + case 0x2afa: + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + memset(server[id].map, 0, sizeof(server[id].map)); + j = 0; + for(i = 4; i < RFIFOW(fd,2); i += 16) { + memcpy(server[id].map[j], RFIFOP(fd,i), 16); +// printf("set map %d.%d : %s\n", id, j, server[id].map[j]); + j++; + } + { + unsigned char *p = (unsigned char *)&server[id].ip; + printf("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n", + id, j, p[0], p[1], p[2], p[3], server[id].port); + printf("Map-server %d loading complete.\n", id); + char_log("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d. Map-server %d loading complete." RETCODE, + id, j, p[0], p[1], p[2], p[3], server[id].port, id); + } + WFIFOW(fd,0) = 0x2afb; + WFIFOB(fd,2) = 0; + memcpy(WFIFOP(fd,3), wisp_server_name, 24); // name for wisp to player + WFIFOSET(fd,27); + { + unsigned char buf[16384]; + int x; + if (j == 0) { + printf("WARNING: Map-Server %d have NO map.\n", id); + char_log("WARNING: Map-Server %d have NO map." RETCODE, id); + // Transmitting maps information to the other map-servers + } else { + WBUFW(buf,0) = 0x2b04; + WBUFW(buf,2) = j * 16 + 10; + WBUFL(buf,4) = server[id].ip; + WBUFW(buf,8) = server[id].port; + memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 16); + mapif_sendallwos(fd, buf, WBUFW(buf,2)); + } + // Transmitting the maps of the other map-servers to the new map-server + for(x = 0; x < MAX_MAP_SERVERS; x++) { + if (server_fd[x] >= 0 && x != id) { + WFIFOW(fd,0) = 0x2b04; + WFIFOL(fd,4) = server[x].ip; + WFIFOW(fd,8) = server[x].port; + j = 0; + for(i = 0; i < MAX_MAP_PER_SERVER; i++) + if (server[x].map[i][0]) + memcpy(WFIFOP(fd,10+(j++)*16), server[x].map[i], 16); + if (j > 0) { + WFIFOW(fd,2) = j * 16 + 10; + WFIFOSET(fd,WFIFOW(fd,2)); + } + } + } + } + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + + // 認証要求 + case 0x2afc: + if (RFIFOREST(fd) < 22) + return 0; + //printf("auth_fifo search: account: %d, char: %d, secure: %08x-%08x\n", RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10), RFIFOL(fd,14)); + for(i = 0; i < AUTH_FIFO_SIZE; i++) { + if (auth_fifo[i].account_id == RFIFOL(fd,2) && + auth_fifo[i].char_id == RFIFOL(fd,6) && + auth_fifo[i].login_id1 == RFIFOL(fd,10) && +#if CMP_AUTHFIFO_LOGIN2 != 0 + // here, it's the only area where it's possible that we doesn't know login_id2 (map-server asks just after 0x72 packet, that doesn't given the value) + (auth_fifo[i].login_id2 == RFIFOL(fd,14) || RFIFOL(fd,14) == 0) && // relate to the versions higher than 18 +#endif + (!check_ip_flag || auth_fifo[i].ip == RFIFOL(fd,18)) && + !auth_fifo[i].delflag) { + auth_fifo[i].delflag = 1; + WFIFOW(fd,0) = 0x2afd; + WFIFOW(fd,2) = 16 + sizeof(struct mmo_charstatus); + WFIFOL(fd,4) = RFIFOL(fd,2); + WFIFOL(fd,8) = auth_fifo[i].login_id2; + WFIFOL(fd,12) = (unsigned long)auth_fifo[i].connect_until_time; + char_dat[auth_fifo[i].char_pos].sex = auth_fifo[i].sex; + memcpy(WFIFOP(fd,16), &char_dat[auth_fifo[i].char_pos], sizeof(struct mmo_charstatus)); + WFIFOSET(fd, WFIFOW(fd,2)); + //printf("auth_fifo search success (auth #%d, account %d, character: %d).\n", i, RFIFOL(fd,2), RFIFOL(fd,6)); + break; + } + } + if (i == AUTH_FIFO_SIZE) { + WFIFOW(fd,0) = 0x2afe; + WFIFOL(fd,2) = RFIFOL(fd,2); + WFIFOSET(fd,6); + printf("auth_fifo search error! account %d not authentified.\n", RFIFOL(fd,2)); + } + RFIFOSKIP(fd,22); + break; + + // MAPサーバー上のユーザー数受信 + case 0x2aff: + if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + server[id].users = RFIFOW(fd,4); + if(anti_freeze_enable) + server_freezeflag[id] = 5; // Map anti-freeze system. Counter. 5 ok, 4...0 freezed + // remove all previously online players of the server + for(i = 0; i < char_num; i++) + if (online_chars[i] == id) + online_chars[i] = -1; + // add online players in the list by [Yor] + for(i = 0; i < server[id].users; i++) { + int char_id = RFIFOL(fd,6+i*4); + for(j = 0; j < char_num; j++) + if (char_dat[j].char_id == char_id) { + online_chars[j] = id; + //printf("%d\n", char_id); + break; + } + } + if (update_online < time(NULL)) { // Time is done + update_online = time(NULL) + 8; + create_online_files(); // only every 8 sec. (normally, 1 server send users every 5 sec.) Don't update every time, because that takes time, but only every 2 connection. + // it set to 8 sec because is more than 5 (sec) and if we have more than 1 map-server, informations can be received in shifted. + } + RFIFOSKIP(fd,6+i*4); + break; + + // キャラデータ保存 + case 0x2b01: + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + for(i = 0; i < char_num; i++) { + if (char_dat[i].account_id == RFIFOL(fd,4) && + char_dat[i].char_id == RFIFOL(fd,8)) + break; + } + if (i != char_num) + memcpy(&char_dat[i], RFIFOP(fd,12), sizeof(struct mmo_charstatus)); + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + + // キャラセレ要求 + case 0x2b02: + if (RFIFOREST(fd) < 18) + return 0; + if (auth_fifo_pos >= AUTH_FIFO_SIZE) + auth_fifo_pos = 0; + //printf("auth_fifo set (auth #%d) - account: %d, secure: %08x-%08x\n", auth_fifo_pos, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)); + auth_fifo[auth_fifo_pos].account_id = RFIFOL(fd,2); + auth_fifo[auth_fifo_pos].char_id = 0; + auth_fifo[auth_fifo_pos].login_id1 = RFIFOL(fd,6); + auth_fifo[auth_fifo_pos].login_id2 = RFIFOL(fd,10); + auth_fifo[auth_fifo_pos].delflag = 2; + auth_fifo[auth_fifo_pos].char_pos = 0; + auth_fifo[auth_fifo_pos].connect_until_time = 0; // unlimited/unknown time by default (not display in map-server) + auth_fifo[auth_fifo_pos].ip = RFIFOL(fd,14); + auth_fifo_pos++; + WFIFOW(fd,0) = 0x2b03; + WFIFOL(fd,2) = RFIFOL(fd,2); + WFIFOB(fd,6) = 0; + WFIFOSET(fd,7); + RFIFOSKIP(fd,18); + break; + + // マップサーバー間移動要求 + case 0x2b05: + if (RFIFOREST(fd) < 49) + return 0; + if (auth_fifo_pos >= AUTH_FIFO_SIZE) + auth_fifo_pos = 0; + WFIFOW(fd,0) = 0x2b06; + memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 42); + //printf("auth_fifo set (auth#%d) - account: %d, secure: 0x%08x-0x%08x\n", auth_fifo_pos, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)); + auth_fifo[auth_fifo_pos].account_id = RFIFOL(fd,2); + auth_fifo[auth_fifo_pos].char_id = RFIFOL(fd,14); + auth_fifo[auth_fifo_pos].login_id1 = RFIFOL(fd,6); + auth_fifo[auth_fifo_pos].login_id2 = RFIFOL(fd,10); + auth_fifo[auth_fifo_pos].delflag = 0; + auth_fifo[auth_fifo_pos].sex = RFIFOB(fd,44); + auth_fifo[auth_fifo_pos].connect_until_time = 0; // unlimited/unknown time by default (not display in map-server) + auth_fifo[auth_fifo_pos].ip = RFIFOL(fd,45); + for(i = 0; i < char_num; i++) + if (char_dat[i].account_id == RFIFOL(fd,2) && + char_dat[i].char_id == RFIFOL(fd,14)) { + auth_fifo[auth_fifo_pos].char_pos = i; + auth_fifo_pos++; + WFIFOL(fd,6) = 0; + break; + } + if (i == char_num) + WFIFOW(fd,6) = 1; + WFIFOSET(fd,44); + RFIFOSKIP(fd,49); + break; + + // キャラ名検索 + case 0x2b08: + if (RFIFOREST(fd) < 6) + return 0; + for(i = 0; i < char_num; i++) { + if (char_dat[i].char_id == RFIFOL(fd,2)) + break; + } + WFIFOW(fd,0) = 0x2b09; + WFIFOL(fd,2) = RFIFOL(fd,2); + if (i != char_num) + memcpy(WFIFOP(fd,6), char_dat[i].name, 24); + else + memcpy(WFIFOP(fd,6), unknown_char_name, 24); + WFIFOSET(fd,30); + RFIFOSKIP(fd,6); + break; + + // it is a request to become GM + case 0x2b0a: + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; +// printf("parse_frommap: change gm -> login, account: %d, pass: '%s'.\n", RFIFOL(fd,4), RFIFOP(fd,8)); + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd,0) = 0x2720; + memcpy(WFIFOP(login_fd,2), RFIFOP(fd,2), RFIFOW(fd,2)-2); + WFIFOSET(login_fd, RFIFOW(fd,2)); + } else { + WFIFOW(fd,0) = 0x2b0b; + WFIFOL(fd,2) = RFIFOL(fd,4); + WFIFOL(fd,6) = 0; + WFIFOSET(fd, 10); + } + RFIFOSKIP(fd, RFIFOW(fd,2)); + break; + + // Map server send information to change an email of an account -> login-server + case 0x2b0c: + if (RFIFOREST(fd) < 86) + return 0; + if (login_fd > 0) { // don't send request if no login-server + memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0), 86); // 0x2722 .L .40B .40B + WFIFOW(login_fd,0) = 0x2722; + WFIFOSET(login_fd, 86); + } + RFIFOSKIP(fd, 86); + break; + + // Map server ask char-server about a character name to do some operations (all operations are transmitted to login-server) + case 0x2b0e: + if (RFIFOREST(fd) < 44) + return 0; + { + char character_name[24]; + int acc = RFIFOL(fd,2); // account_id of who ask (-1 if nobody) + memcpy(character_name, RFIFOP(fd,6), 24); + character_name[sizeof(character_name) -1] = '\0'; + // prepare answer + WFIFOW(fd,0) = 0x2b0f; // answer + WFIFOL(fd,2) = acc; // who want do operation + WFIFOW(fd,30) = RFIFOW(fd, 30); // type of operation: 1-block, 2-ban, 3-unblock, 4-unban, 5-changesex + // search character + i = search_character_index(character_name); + if (i >= 0) { + memcpy(WFIFOP(fd,6), search_character_name(i), 24); // put correct name if found + WFIFOW(fd,32) = 0; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + switch(RFIFOW(fd, 30)) { + case 1: // block + if (acc == -1 || isGM(acc) >= isGM(char_dat[i].account_id)) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd,0) = 0x2724; + WFIFOL(login_fd,2) = char_dat[i].account_id; // account value + WFIFOL(login_fd,6) = 5; // status of the account + WFIFOSET(login_fd, 10); +// printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 5); + } else + WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } else + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 2: // ban + if (acc == -1 || isGM(acc) >= isGM(char_dat[i].account_id)) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd, 0) = 0x2725; + WFIFOL(login_fd, 2) = char_dat[i].account_id; // account value + WFIFOW(login_fd, 6) = RFIFOW(fd,32); // year + WFIFOW(login_fd, 8) = RFIFOW(fd,34); // month + WFIFOW(login_fd,10) = RFIFOW(fd,36); // day + WFIFOW(login_fd,12) = RFIFOW(fd,38); // hour + WFIFOW(login_fd,14) = RFIFOW(fd,40); // minute + WFIFOW(login_fd,16) = RFIFOW(fd,42); // second + WFIFOSET(login_fd,18); +// printf("char : status -> login: account %d, ban: %dy %dm %dd %dh %dmn %ds\n", +// char_dat[i].account_id, (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), (short)RFIFOW(fd,38), (short)RFIFOW(fd,40), (short)RFIFOW(fd,42)); + } else + WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } else + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 3: // unblock + if (acc == -1 || isGM(acc) >= isGM(char_dat[i].account_id)) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd,0) = 0x2724; + WFIFOL(login_fd,2) = char_dat[i].account_id; // account value + WFIFOL(login_fd,6) = 0; // status of the account + WFIFOSET(login_fd, 10); +// printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 0); + } else + WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } else + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 4: // unban + if (acc == -1 || isGM(acc) >= isGM(char_dat[i].account_id)) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd, 0) = 0x272a; + WFIFOL(login_fd, 2) = char_dat[i].account_id; // account value + WFIFOSET(login_fd, 6); +// printf("char : status -> login: account %d, unban request\n", char_dat[i].account_id); + } else + WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } else + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 5: // changesex + if (acc == -1 || isGM(acc) >= isGM(char_dat[i].account_id)) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd, 0) = 0x2727; + WFIFOL(login_fd, 2) = char_dat[i].account_id; // account value + WFIFOSET(login_fd, 6); +// printf("char : status -> login: account %d, change sex request\n", char_dat[i].account_id); + } else + WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } else + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + } + } else { + // character name not found + memcpy(WFIFOP(fd,6), character_name, 24); + WFIFOW(fd,32) = 1; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } + // send answer if a player ask, not if the server ask + if (acc != -1) { + WFIFOSET(fd, 34); + } + RFIFOSKIP(fd, 44); + break; + } + +// case 0x2b0f: not more used (available for futur usage) + + // account_reg保存要求 + case 0x2b10: + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + { + struct global_reg reg[ACCOUNT_REG2_NUM]; + int p, acc; + acc = RFIFOL(fd,4); + for(p = 8, j = 0; p < RFIFOW(fd,2) && j < ACCOUNT_REG2_NUM; p += 36, j++) { + memcpy(reg[j].str, RFIFOP(fd,p), 32); + reg[j].value = RFIFOL(fd, p+32); + } + set_account_reg2(acc, j, reg); + // loginサーバーへ送る + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd, 0) = 0x2728; + memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0), RFIFOW(fd,2)); + WFIFOSET(login_fd, WFIFOW(login_fd,2)); + } + // ワールドへの同垢ログインがなければmapサーバーに送る必要はない + //memcpy(buf, RFIFOP(fd,0), RFIFOW(fd,2)); + //WBUFW(buf,0) = 0x2b11; + //mapif_sendall(buf, WBUFW(buf,2)); + RFIFOSKIP(fd, RFIFOW(fd,2)); +// printf("char: save_account_reg (from map)\n"); + break; + } + + default: + // inter server処理に渡す + { + int r = inter_parse_frommap(fd); + if (r == 1) // 処理できた + break; + if (r == 2) // パケット長が足りない + return 0; + } + // inter server処理でもない場合は切断 + printf("char: unknown packet 0x%04x (%d bytes to read in buffer)! (from map).\n", RFIFOW(fd,0), RFIFOREST(fd)); + session[fd]->eof = 1; + return 0; + } + } + return 0; +} + +int search_mapserver(char *map) { + int i, j; + char temp_map[16]; + int temp_map_len; + +// printf("Searching the map-server for map '%s'... ", map); + strncpy(temp_map, map, sizeof(temp_map)); + temp_map[sizeof(temp_map)-1] = '\0'; + if (strchr(temp_map, '.') != NULL) + temp_map[strchr(temp_map, '.') - temp_map + 1] = '\0'; // suppress the '.gat', but conserve the '.' to be sure of the name of the map + + temp_map_len = strlen(temp_map); + for(i = 0; i < MAX_MAP_SERVERS; i++) + if (server_fd[i] >= 0) + for (j = 0; server[i].map[j][0]; j++) + //printf("%s : %s = %d\n", server[i].map[j], map, strncmp(server[i].map[j], temp_map, temp_map_len)); + if (strncmp(server[i].map[j], temp_map, temp_map_len) == 0) { +// printf("found -> server #%d.\n", i); + return i; + } + +// printf("not found.\n"); + return -1; +} + +// char_mapifの初期化処理(現在はinter_mapif初期化のみ) +static int char_mapif_init(int fd) { + return inter_mapif_init(fd); +} + +//----------------------------------------------------- +// Test to know if an IP come from LAN or WAN. by [Yor] +//----------------------------------------------------- +int lan_ip_check(unsigned char *p){ + int i; + int lancheck = 1; + +// printf("lan_ip_check: to compare: %d.%d.%d.%d, network: %d.%d.%d.%d/%d.%d.%d.%d\n", +// p[0], p[1], p[2], p[3], +// subneti[0], subneti[1], subneti[2], subneti[3], +// subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]); + for(i = 0; i < 4; i++) { + if ((subneti[i] & subnetmaski[i]) != (p[i] & subnetmaski[i])) { + lancheck = 0; + break; + } + } + printf("LAN test (result): %s source\033[0m.\n", (lancheck) ? "\033[1;36mLAN" : "\033[1;32mWAN"); + return lancheck; +} + +int parse_char(int fd) { + int i, ch; + char email[40]; + struct char_session_data *sd; + unsigned char *p = (unsigned char *) &session[fd]->client_addr.sin_addr; + + if (login_fd < 0 || session[fd]->eof) { // disconnect any player (already connected to char-server or coming back from map-server) if login-server is diconnected. + if (fd == login_fd) + login_fd = -1; + close(fd); + delete_session(fd); + return 0; + } + + sd = session[fd]->session_data; + + while (RFIFOREST(fd) >= 2) { +// if (RFIFOW(fd,0) < 30000) +// printf("parse_char: connection #%d, packet: 0x%x (with being read: %d bytes).\n", fd, RFIFOW(fd,0), RFIFOREST(fd)); + + switch(RFIFOW(fd,0)) { + case 0x20b: //20040622暗号化ragexe対応 + if (RFIFOREST(fd) < 19) + return 0; + RFIFOSKIP(fd,19); + break; + + case 0x65: // 接続要求 + if (RFIFOREST(fd) < 17) + return 0; + { + int GM_value; + if ((GM_value = isGM(RFIFOL(fd,2)))) + printf("Account Logged On; Account ID: %d (GM level %d).\n", RFIFOL(fd,2), GM_value); + else + printf("Account Logged On; Account ID: %d.\n", RFIFOL(fd,2)); + if (sd == NULL) { + sd = session[fd]->session_data = calloc(sizeof(struct char_session_data), 1); + memset(sd, 0, sizeof(struct char_session_data)); + memcpy(sd->email, "no mail", 40); // put here a mail without '@' to refuse deletion if we don't receive the e-mail + sd->connect_until_time = 0; // unknow or illimited (not displaying on map-server) + } + sd->account_id = RFIFOL(fd,2); + sd->login_id1 = RFIFOL(fd,6); + sd->login_id2 = RFIFOL(fd,10); + sd->sex = RFIFOB(fd,16); + // send back account_id + WFIFOL(fd,0) = RFIFOL(fd,2); + WFIFOSET(fd,4); + // search authentification + for(i = 0; i < AUTH_FIFO_SIZE; i++) { + if (auth_fifo[i].account_id == sd->account_id && + auth_fifo[i].login_id1 == sd->login_id1 && +#if CMP_AUTHFIFO_LOGIN2 != 0 + auth_fifo[i].login_id2 == sd->login_id2 && // relate to the versions higher than 18 +#endif + (!check_ip_flag || auth_fifo[i].ip == session[fd]->client_addr.sin_addr.s_addr) && + auth_fifo[i].delflag == 2) { + auth_fifo[i].delflag = 1; + if (max_connect_user == 0 || count_users() < max_connect_user) { + if (login_fd > 0) { // don't send request if no login-server + // request to login-server to obtain e-mail/time limit + WFIFOW(login_fd,0) = 0x2716; + WFIFOL(login_fd,2) = sd->account_id; + WFIFOSET(login_fd,6); + } + // send characters to player + mmo_char_send006b(fd, sd); + } else { + // refuse connection (over populated) + WFIFOW(fd,0) = 0x6c; + WFIFOW(fd,2) = 0; + WFIFOSET(fd,3); + } + break; + } + } + // authentification not found + if (i == AUTH_FIFO_SIZE) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd,0) = 0x2712; // ask login-server to authentify an account + WFIFOL(login_fd,2) = sd->account_id; + WFIFOL(login_fd,6) = sd->login_id1; + WFIFOL(login_fd,10) = sd->login_id2; // relate to the versions higher than 18 + WFIFOB(login_fd,14) = sd->sex; + WFIFOL(login_fd,15) = session[fd]->client_addr.sin_addr.s_addr; + WFIFOSET(login_fd,19); + } else { // if no login-server, we must refuse connection + WFIFOW(fd,0) = 0x6c; + WFIFOW(fd,2) = 0; + WFIFOSET(fd,3); + } + } + } + RFIFOSKIP(fd,17); + break; + + case 0x66: // キャラ選択 + if (RFIFOREST(fd) < 3) + return 0; + + // if we activated email creation and email is default email + if (email_creation != 0 && strcmp(sd->email, "a@a.com") == 0 && login_fd > 0) { // to modify an e-mail, login-server must be online + WFIFOW(fd, 0) = 0x70; + WFIFOB(fd, 2) = 0; // 00 = Incorrect Email address + WFIFOSET(fd, 3); + + // otherwise, load the character + } else { + for (ch = 0; ch < 9; ch++) + if (sd->found_char[ch] >= 0 && char_dat[sd->found_char[ch]].char_num == RFIFOB(fd,2)) + break; + if (ch != 9) { + char_log("Character Selected, Account ID: %d, Character Slot: %d, Character Name: %s." RETCODE, + sd->account_id, RFIFOB(fd,2), char_dat[sd->found_char[ch]].name); + // searching map server + i = search_mapserver(char_dat[sd->found_char[ch]].last_point.map); + // if map is not found, we check major cities + if (i < 0) { + if ((i = search_mapserver("prontera.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[sd->found_char[ch]].last_point.map, "prontera.gat", 16); + char_dat[sd->found_char[ch]].last_point.x = 273; // savepoint coordonates + char_dat[sd->found_char[ch]].last_point.y = 354; + } else if ((i = search_mapserver("geffen.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[sd->found_char[ch]].last_point.map, "geffen.gat", 16); + char_dat[sd->found_char[ch]].last_point.x = 120; // savepoint coordonates + char_dat[sd->found_char[ch]].last_point.y = 100; + } else if ((i = search_mapserver("morocc.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[sd->found_char[ch]].last_point.map, "morocc.gat", 16); + char_dat[sd->found_char[ch]].last_point.x = 160; // savepoint coordonates + char_dat[sd->found_char[ch]].last_point.y = 94; + } else if ((i = search_mapserver("alberta.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[sd->found_char[ch]].last_point.map, "alberta.gat", 16); + char_dat[sd->found_char[ch]].last_point.x = 116; // savepoint coordonates + char_dat[sd->found_char[ch]].last_point.y = 57; + } else if ((i = search_mapserver("payon.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[sd->found_char[ch]].last_point.map, "payon.gat", 16); + char_dat[sd->found_char[ch]].last_point.x = 87; // savepoint coordonates + char_dat[sd->found_char[ch]].last_point.y = 117; + } else if ((i = search_mapserver("izlude.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[sd->found_char[ch]].last_point.map, "izlude.gat", 16); + char_dat[sd->found_char[ch]].last_point.x = 94; // savepoint coordonates + char_dat[sd->found_char[ch]].last_point.y = 103; + } else { + int j; + // get first online server (with a map) + i = 0; + for(j = 0; j < MAX_MAP_SERVERS; j++) + if (server_fd[j] >= 0 && server[j].map[0][0]) { // change save point to one of map found on the server (the first) + i = j; + memcpy(char_dat[sd->found_char[ch]].last_point.map, server[j].map[0], 16); + printf("Map-server #%d found with a map: '%s'.\n", j, server[j].map[0]); + // coordonates are unknown + break; + } + // if no map-server is connected, we send: server closed + if (j == MAX_MAP_SERVERS) { + WFIFOW(fd,0) = 0x81; + WFIFOL(fd,2) = 1; // 01 = Server closed + WFIFOSET(fd,3); + RFIFOSKIP(fd,3); + break; + } + } + } + WFIFOW(fd,0) = 0x71; + WFIFOL(fd,2) = char_dat[sd->found_char[ch]].char_id; + memcpy(WFIFOP(fd,6), char_dat[sd->found_char[ch]].last_point.map, 16); + printf("Character selection '%s' (account: %d, slot: %d).\n", char_dat[sd->found_char[ch]].name, sd->account_id, ch); + printf("--Send IP of map-server. "); + if (lan_ip_check(p)) + WFIFOL(fd, 22) = inet_addr(lan_map_ip); + else + WFIFOL(fd, 22) = server[i].ip; + WFIFOW(fd,26) = server[i].port; + WFIFOSET(fd,28); + if (auth_fifo_pos >= AUTH_FIFO_SIZE) + auth_fifo_pos = 0; + //printf("auth_fifo set #%d - account %d, char: %d, secure: %08x-%08x\n", auth_fifo_pos, sd->account_id, char_dat[sd->found_char[ch]].char_id, sd->login_id1, sd->login_id2); + auth_fifo[auth_fifo_pos].account_id = sd->account_id; + auth_fifo[auth_fifo_pos].char_id = char_dat[sd->found_char[ch]].char_id; + auth_fifo[auth_fifo_pos].login_id1 = sd->login_id1; + auth_fifo[auth_fifo_pos].login_id2 = sd->login_id2; + auth_fifo[auth_fifo_pos].delflag = 0; + auth_fifo[auth_fifo_pos].char_pos = sd->found_char[ch]; + auth_fifo[auth_fifo_pos].sex = sd->sex; + auth_fifo[auth_fifo_pos].connect_until_time = sd->connect_until_time; + auth_fifo[auth_fifo_pos].ip = session[fd]->client_addr.sin_addr.s_addr; + auth_fifo_pos++; + } + } + RFIFOSKIP(fd,3); + break; + + case 0x67: // 作成 + if (RFIFOREST(fd) < 37) + return 0; + i = make_new_char(fd, RFIFOP(fd,2)); + if (i < 0) { + WFIFOW(fd,0) = 0x6e; + WFIFOB(fd,2) = 0x00; + WFIFOSET(fd,3); + RFIFOSKIP(fd,37); + break; + } + + WFIFOW(fd,0) = 0x6d; + memset(WFIFOP(fd,2), 0, 106); + + WFIFOL(fd,2) = char_dat[i].char_id; + WFIFOL(fd,2+4) = char_dat[i].base_exp; + WFIFOL(fd,2+8) = char_dat[i].zeny; + WFIFOL(fd,2+12) = char_dat[i].job_exp; + WFIFOL(fd,2+16) = char_dat[i].job_level; + + WFIFOL(fd,2+28) = char_dat[i].karma; + WFIFOL(fd,2+32) = char_dat[i].manner; + + WFIFOW(fd,2+40) = 0x30; + WFIFOW(fd,2+42) = (char_dat[i].hp > 0x7fff) ? 0x7fff : char_dat[i].hp; + WFIFOW(fd,2+44) = (char_dat[i].max_hp > 0x7fff) ? 0x7fff : char_dat[i].max_hp; + WFIFOW(fd,2+46) = (char_dat[i].sp > 0x7fff) ? 0x7fff : char_dat[i].sp; + WFIFOW(fd,2+48) = (char_dat[i].max_sp > 0x7fff) ? 0x7fff : char_dat[i].max_sp; + WFIFOW(fd,2+50) = DEFAULT_WALK_SPEED; // char_dat[i].speed; + WFIFOW(fd,2+52) = char_dat[i].class; + WFIFOW(fd,2+54) = char_dat[i].hair; + + WFIFOW(fd,2+58) = char_dat[i].base_level; + WFIFOW(fd,2+60) = char_dat[i].skill_point; + + WFIFOW(fd,2+64) = char_dat[i].shield; + WFIFOW(fd,2+66) = char_dat[i].head_top; + WFIFOW(fd,2+68) = char_dat[i].head_mid; + WFIFOW(fd,2+70) = char_dat[i].hair_color; + + memcpy(WFIFOP(fd,2+74), char_dat[i].name, 24); + + WFIFOB(fd,2+98) = (char_dat[i].str > 255) ? 255 : char_dat[i].str; + WFIFOB(fd,2+99) = (char_dat[i].agi > 255) ? 255 : char_dat[i].agi; + WFIFOB(fd,2+100) = (char_dat[i].vit > 255) ? 255 : char_dat[i].vit; + WFIFOB(fd,2+101) = (char_dat[i].int_ > 255) ? 255 : char_dat[i].int_; + WFIFOB(fd,2+102) = (char_dat[i].dex > 255) ? 255 : char_dat[i].dex; + WFIFOB(fd,2+103) = (char_dat[i].luk > 255) ? 255 : char_dat[i].luk; + WFIFOB(fd,2+104) = char_dat[i].char_num; + + WFIFOSET(fd,108); + RFIFOSKIP(fd,37); + for(ch = 0; ch < 9; ch++) { + if (sd->found_char[ch] == -1) { + sd->found_char[ch] = i; + break; + } + } + + case 0x68: // delete char //Yor's Fix + if (RFIFOREST(fd) < 46) + return 0; + memcpy(email, RFIFOP(fd,6), 40); + if (e_mail_check(email) == 0) + strncpy(email, "a@a.com", 40); // default e-mail + + // if we activated email creation and email is default email + if (email_creation != 0 && strcmp(sd->email, "a@a.com") == 0 && login_fd > 0) { // to modify an e-mail, login-server must be online + // if sended email is incorrect e-mail + if (strcmp(email, "a@a.com") == 0) { + WFIFOW(fd, 0) = 0x70; + WFIFOB(fd, 2) = 0; // 00 = Incorrect Email address + WFIFOSET(fd, 3); + RFIFOSKIP(fd,46); + // we act like we have selected a character + } else { + // we change the packet to set it like selection. + for (i = 0; i < 9; i++) + if (char_dat[sd->found_char[i]].char_id == RFIFOL(fd,2)) { + // we save new e-mail + memcpy(sd->email, email, 40); + // we send new e-mail to login-server ('online' login-server is checked before) + WFIFOW(login_fd,0) = 0x2715; + WFIFOL(login_fd,2) = sd->account_id; + memcpy(WFIFOP(login_fd, 6), email, 40); + WFIFOSET(login_fd,46); + // skip part of the packet! (46, but leave the size of select packet: 3) + RFIFOSKIP(fd,43); + // change value to put new packet (char selection) + RFIFOW(fd, 0) = 0x66; + RFIFOB(fd, 2) = char_dat[sd->found_char[i]].char_num; + // not send packet, it's modify of actual packet + break; + } + if (i == 9) { + WFIFOW(fd, 0) = 0x70; + WFIFOB(fd, 2) = 0; // 00 = Incorrect Email address + WFIFOSET(fd, 3); + RFIFOSKIP(fd,46); + } + } + + // otherwise, we delete the character + } else { + if (strcmpi(email, sd->email) != 0) { // if it's an invalid email + WFIFOW(fd, 0) = 0x70; + WFIFOB(fd, 2) = 0; // 00 = Incorrect Email address + WFIFOSET(fd, 3); + // if mail is correct + } else { + for (i = 0; i < 9; i++) { + struct mmo_charstatus *cs = NULL; + if ((cs = &char_dat[sd->found_char[i]])->char_id == RFIFOL(fd,2)) { + char_delete(cs); // deletion process + + if (sd->found_char[i] != char_num - 1) { + memcpy(&char_dat[sd->found_char[i]], &char_dat[char_num-1], sizeof(struct mmo_charstatus)); + // Correct moved character reference in the character's owner + { + int j, k; + struct char_session_data *sd2; + for (j = 0; j < fd_max; j++) { + if (session[j] && (sd2 = session[j]->session_data) && + sd2->account_id == char_dat[char_num-1].account_id) { + for (k = 0; k < 9; k++) { + if (sd2->found_char[k] == char_num-1) { + sd2->found_char[k] = sd->found_char[i]; + break; + } + } + break; + } + } + } + } + + char_num--; + for(ch = i; ch < 9-1; ch++) + sd->found_char[ch] = sd->found_char[ch+1]; + sd->found_char[8] = -1; + WFIFOW(fd,0) = 0x6f; + WFIFOSET(fd,2); + break; + } + } + + if (i == 9) { + WFIFOW(fd,0) = 0x70; + WFIFOB(fd,2) = 0; + WFIFOSET(fd,3); + } + } + RFIFOSKIP(fd,46); + } + break; + + case 0x2af8: // マップサーバーログイン + if (RFIFOREST(fd) < 60) + return 0; + WFIFOW(fd,0) = 0x2af9; + for(i = 0; i < MAX_MAP_SERVERS; i++) { + if (server_fd[i] < 0) + break; + } + if (i == MAX_MAP_SERVERS || strcmp(RFIFOP(fd,2), userid) || strcmp(RFIFOP(fd,26), passwd)){ + WFIFOB(fd,2) = 3; + WFIFOSET(fd,3); + RFIFOSKIP(fd,60); + } else { + int len; + WFIFOB(fd,2) = 0; + session[fd]->func_parse = parse_frommap; + server_fd[i] = fd; + if(anti_freeze_enable) + server_freezeflag[i] = 5; // Map anti-freeze system. Counter. 5 ok, 4...0 freezed + server[i].ip = RFIFOL(fd,54); + server[i].port = RFIFOW(fd,58); + server[i].users = 0; + memset(server[i].map, 0, sizeof(server[i].map)); + WFIFOSET(fd,3); + RFIFOSKIP(fd,60); + realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK); + char_mapif_init(fd); + // send gm acccounts level to map-servers + len = 4; + WFIFOW(fd,0) = 0x2b15; + for(i = 0; i < GM_num; i++) { + WFIFOL(fd,len) = gm_account[i].account_id; + WFIFOB(fd,len+4) = (unsigned char)gm_account[i].level; + len += 5; + } + WFIFOW(fd,2) = len; + WFIFOSET(fd,len); + return 0; + } + break; + + case 0x187: // Alive信号? + if (RFIFOREST(fd) < 6) + return 0; + RFIFOSKIP(fd, 6); + break; + + case 0x7530: // Athena情報所得 + WFIFOW(fd,0) = 0x7531; + WFIFOB(fd,2) = ATHENA_MAJOR_VERSION; + WFIFOB(fd,3) = ATHENA_MINOR_VERSION; + WFIFOB(fd,4) = ATHENA_REVISION; + WFIFOB(fd,5) = ATHENA_RELEASE_FLAG; + WFIFOB(fd,6) = ATHENA_OFFICIAL_FLAG; + WFIFOB(fd,7) = ATHENA_SERVER_INTER | ATHENA_SERVER_CHAR; + WFIFOW(fd,8) = ATHENA_MOD_VERSION; + WFIFOSET(fd,10); + RFIFOSKIP(fd,2); + return 0; + + case 0x7532: // 接続の切断(defaultと処理は一緒だが明示的にするため) + session[fd]->eof = 1; + return 0; + + default: + session[fd]->eof = 1; + return 0; + } + } + RFIFOFLUSH(fd); + return 0; +} + +// 全てのMAPサーバーにデータ送信(送信したmap鯖の数を返す) +int mapif_sendall(unsigned char *buf, unsigned int len) { + int i, c; + + c = 0; + for(i = 0; i < MAX_MAP_SERVERS; i++) { + int fd; + if ((fd = server_fd[i]) >= 0) { + memcpy(WFIFOP(fd,0), buf, len); + WFIFOSET(fd,len); + c++; + } + } + return c; +} + +// 自分以外の全てのMAPサーバーにデータ送信(送信したmap鯖の数を返す) +int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len) { + int i, c; + + c = 0; + for(i = 0; i < MAX_MAP_SERVERS; i++) { + int fd; + if ((fd = server_fd[i]) >= 0 && fd != sfd) { + memcpy(WFIFOP(fd,0), buf, len); + WFIFOSET(fd, len); + c++; + } + } + return c; +} +// MAPサーバーにデータ送信(map鯖生存確認有り) +int mapif_send(int fd, unsigned char *buf, unsigned int len) { + int i; + + if (fd >= 0) { + for(i = 0; i < MAX_MAP_SERVERS; i++) { + if (fd == server_fd[i]) { + memcpy(WFIFOP(fd,0), buf, len); + WFIFOSET(fd,len); + return 1; + } + } + } + return 0; +} + +int send_users_tologin(int tid, unsigned int tick, int id, int data) { + int users = count_users(); + char buf[16]; + + if (login_fd > 0 && session[login_fd]) { + // send number of user to login server + WFIFOW(login_fd,0) = 0x2714; + WFIFOL(login_fd,2) = users; + WFIFOSET(login_fd,6); + } + // send number of players to all map-servers + WBUFW(buf,0) = 0x2b00; + WBUFL(buf,2) = users; + mapif_sendall(buf, 6); + + return 0; +} + +int check_connect_login_server(int tid, unsigned int tick, int id, int data) { + if (login_fd <= 0 || session[login_fd] == NULL) { + printf("Attempt to connect to login-server...\n"); + login_fd = make_connection(login_ip, login_port); + session[login_fd]->func_parse = parse_tologin; + realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK); + WFIFOW(login_fd,0) = 0x2710; + memset(WFIFOP(login_fd,2), 0, 24); + memcpy(WFIFOP(login_fd,2), userid, strlen(userid) < 24 ? strlen(userid) : 24); + memset(WFIFOP(login_fd,26), 0, 24); + memcpy(WFIFOP(login_fd,26), passwd, strlen(passwd) < 24 ? strlen(passwd) : 24); + WFIFOL(login_fd,50) = 0; + WFIFOL(login_fd,54) = char_ip; + WFIFOL(login_fd,58) = char_port; + memset(WFIFOP(login_fd,60), 0, 20); + memcpy(WFIFOP(login_fd,60), server_name, strlen(server_name) < 20 ? strlen(server_name) : 20); + WFIFOW(login_fd,80) = 0; + WFIFOW(login_fd,82) = char_maintenance; + WFIFOW(login_fd,84) = char_new; + WFIFOSET(login_fd,86); + } + return 0; +} + +//---------------------------------------------------------- +// Return numerical value of a switch configuration by [Yor] +// on/off, english, fran軋is, deutsch, espaol +//---------------------------------------------------------- +int config_switch(const char *str) { + if (strcmpi(str, "on") == 0 || strcmpi(str, "yes") == 0 || strcmpi(str, "oui") == 0 || strcmpi(str, "ja") == 0 || strcmpi(str, "si") == 0) + return 1; + if (strcmpi(str, "off") == 0 || strcmpi(str, "no") == 0 || strcmpi(str, "non") == 0 || strcmpi(str, "nein") == 0) + return 0; + + return atoi(str); +} + +//------------------------------------------- +// Reading Lan Support configuration by [Yor] +//------------------------------------------- +int lan_config_read(const char *lancfgName) { + int j; + struct hostent * h = NULL; + char line[1024], w1[1024], w2[1024]; + FILE *fp; + + // set default configuration + strncpy(lan_map_ip, "127.0.0.1", sizeof(lan_map_ip)); + subneti[0] = 127; + subneti[1] = 0; + subneti[2] = 0; + subneti[3] = 1; + for(j = 0; j < 4; j++) + subnetmaski[j] = 255; + + fp = fopen(lancfgName, "r"); + + if (fp == NULL) { + printf("LAN support configuration file not found: %s\n", lancfgName); + return 1; + } + + printf ("---start reading of Lan Support configuration...\n"); + + while(fgets(line, sizeof(line)-1, fp)) { + if (line[0] == '/' && line[1] == '/') + continue; + + line[sizeof(line)-1] = '\0'; + if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2) + continue; + + remove_control_chars(w1); + remove_control_chars(w2); + if (strcmpi(w1, "lan_map_ip") == 0) { // Read map-server Lan IP Address + h = gethostbyname(w2); + if (h != NULL) { + sprintf(lan_map_ip, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } else { + strncpy(lan_map_ip, w2, sizeof(lan_map_ip)); + lan_map_ip[sizeof(lan_map_ip)-1] = 0; + } + printf("LAN IP of map-server: %s.\n", lan_map_ip); + } else if (strcmpi(w1, "subnet") == 0) { // Read Subnetwork + for(j = 0; j < 4; j++) + subneti[j] = 0; + h = gethostbyname(w2); + if (h != NULL) { + for(j = 0; j < 4; j++) + subneti[j] = (unsigned char)h->h_addr[j]; + } else { + sscanf(w2, "%d.%d.%d.%d", &subneti[0], &subneti[1], &subneti[2], &subneti[3]); + } + printf("Sub-network of the map-server: %d.%d.%d.%d.\n", subneti[0], subneti[1], subneti[2], subneti[3]); + } else if (strcmpi(w1, "subnetmask") == 0){ // Read Subnetwork Mask + for(j = 0; j < 4; j++) + subnetmaski[j] = 255; + h = gethostbyname(w2); + if (h != NULL) { + for(j = 0; j < 4; j++) + subnetmaski[j] = (unsigned char)h->h_addr[j]; + } else { + sscanf(w2, "%d.%d.%d.%d", &subnetmaski[0], &subnetmaski[1], &subnetmaski[2], &subnetmaski[3]); + } + printf("Sub-network mask of the map-server: %d.%d.%d.%d.\n", subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]); + } + } + fclose(fp); + + // sub-network check of the map-server + { + unsigned int a0, a1, a2, a3; + unsigned char p[4]; + sscanf(lan_map_ip, "%d.%d.%d.%d", &a0, &a1, &a2, &a3); + p[0] = a0; p[1] = a1; p[2] = a2; p[3] = a3; + printf("LAN test of LAN IP of the map-server: "); + if (lan_ip_check(p) == 0) { + printf("\033[1;31m***ERROR: LAN IP of the map-server doesn't belong to the specified Sub-network.\033[0m\n"); + } + } + + printf("---End reading of Lan Support configuration...\n"); + + return 0; +} + +int char_config_read(const char *cfgName) { + struct hostent *h = NULL; + char line[1024], w1[1024], w2[1024]; + FILE *fp = fopen(cfgName, "r"); + + if (fp == NULL) { + printf("Configuration file not found: %s.\n", cfgName); + exit(1); + } + + while(fgets(line, sizeof(line)-1, fp)) { + if (line[0] == '/' && line[1] == '/') + continue; + + line[sizeof(line)-1] = '\0'; + if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2) + continue; + + remove_control_chars(w1); + remove_control_chars(w2); + if (strcmpi(w1, "userid") == 0) { + memcpy(userid, w2, 24); + } else if (strcmpi(w1, "passwd") == 0) { + memcpy(passwd, w2, 24); + } else if (strcmpi(w1, "server_name") == 0) { + memcpy(server_name, w2, sizeof(server_name)); + server_name[sizeof(server_name) - 1] = '\0'; + printf("%s server has been intialized\n", w2); + } else if (strcmpi(w1, "wisp_server_name") == 0) { + if (strlen(w2) >= 4) { + memcpy(wisp_server_name, w2, sizeof(wisp_server_name)); + wisp_server_name[sizeof(wisp_server_name) - 1] = '\0'; + } + } else if (strcmpi(w1, "login_ip") == 0) { + h = gethostbyname(w2); + if (h != NULL) { + printf("Login server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + sprintf(login_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } else + memcpy(login_ip_str, w2, 16); + } else if (strcmpi(w1, "login_port") == 0) { + login_port = atoi(w2); + } else if (strcmpi(w1, "char_ip") == 0) { + h = gethostbyname(w2); + if (h != NULL) { + printf("Character server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + sprintf(char_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } else + memcpy(char_ip_str, w2, 16); + } else if (strcmpi(w1, "char_port") == 0) { + char_port = atoi(w2); + } else if (strcmpi(w1, "char_maintenance") == 0) { + char_maintenance = atoi(w2); + } else if (strcmpi(w1, "char_new") == 0) { + char_new = atoi(w2); + } else if (strcmpi(w1, "email_creation") == 0) { + email_creation = config_switch(w2); + } else if (strcmpi(w1, "char_txt") == 0) { + strcpy(char_txt, w2); + } else if (strcmpi(w1, "backup_txt") == 0) { //By zanetheinsane + strcpy(backup_txt, w2); + } else if (strcmpi(w1, "backup_txt_flag") == 0) { // The backup_txt file was created because char deletion bug existed. Now it's finish and that take a lot of time to create a second file when there are a lot of characters. By [Yor] + backup_txt_flag = config_switch(w2); + } else if (strcmpi(w1, "max_connect_user") == 0) { + max_connect_user = atoi(w2); + if (max_connect_user < 0) + max_connect_user = 0; // unlimited online players + } else if (strcmpi(w1, "check_ip_flag") == 0) { + check_ip_flag = config_switch(w2); + } else if (strcmpi(w1, "autosave_time") == 0) { + autosave_interval = atoi(w2)*1000; + if (autosave_interval <= 0) + autosave_interval = DEFAULT_AUTOSAVE_INTERVAL; + } else if (strcmpi(w1, "start_point") == 0) { + char map[32]; + int x, y; + if (sscanf(w2, "%[^,],%d,%d", map, &x, &y) < 3) + continue; + if (strstr(map, ".gat") != NULL) { // Verify at least if '.gat' is in the map name + memcpy(start_point.map, map, 16); + start_point.x = x; + start_point.y = y; + } + } else if (strcmpi(w1, "start_zeny") == 0) { + start_zeny = atoi(w2); + if (start_zeny < 0) + start_zeny = 0; + } else if (strcmpi(w1, "start_weapon") == 0) { + start_zeny = atoi(w2); + if (start_weapon < 0) + start_weapon = 0; + } else if (strcmpi(w1, "start_armor") == 0) { + start_zeny = atoi(w2); + if (start_armor < 0) + start_armor = 0; + } else if (strcmpi(w1, "unknown_char_name") == 0) { + strcpy(unknown_char_name, w2); + unknown_char_name[24] = 0; + } else if (strcmpi(w1, "char_log_filename") == 0) { + strcpy(char_log_filename, w2); + } else if (strcmpi(w1, "name_ignoring_case") == 0) { + name_ignoring_case = config_switch(w2); + } else if (strcmpi(w1, "char_name_option") == 0) { + char_name_option = atoi(w2); + } else if (strcmpi(w1, "char_name_letters") == 0) { + strcpy(char_name_letters, w2); +// online files options + } else if (strcmpi(w1, "online_txt_filename") == 0) { + strcpy(online_txt_filename, w2); + } else if (strcmpi(w1, "online_html_filename") == 0) { + strcpy(online_html_filename, w2); + } else if (strcmpi(w1, "online_sorting_option") == 0) { + online_sorting_option = atoi(w2); + } else if (strcmpi(w1, "online_display_option") == 0) { + online_display_option = atoi(w2); + } else if (strcmpi(w1, "online_gm_display_min_level") == 0) { // minimum GM level to display 'GM' when we want to display it + online_gm_display_min_level = atoi(w2); + if (online_gm_display_min_level < 5) // send online file every 5 seconds to player is enough + online_gm_display_min_level = 5; + } else if (strcmpi(w1, "online_refresh_html") == 0) { + online_refresh_html = atoi(w2); + if (online_refresh_html < 1) + online_refresh_html = 1; + } else if(strcmpi(w1,"anti_freeze_enable")==0){ + anti_freeze_enable = config_switch(w2); + } else if (strcmpi(w1, "anti_freeze_interval") == 0) { + ANTI_FREEZE_INTERVAL = atoi(w2); + if (ANTI_FREEZE_INTERVAL < 5) + ANTI_FREEZE_INTERVAL = 5; // minimum 5 seconds + } else if (strcmpi(w1, "import") == 0) { + char_config_read(w2); + } + } + fclose(fp); + + return 0; +} + +void do_final(void) { + int i; + + // write online players files with no player + for(i = 0; i < char_num; i++) + online_chars[i] = -1; + create_online_files(); + free(online_chars); + + mmo_char_sync(); + inter_save(); + + if (gm_account != NULL) + free(gm_account); + + free(char_dat); + delete_session(login_fd); + delete_session(char_fd); + + char_log("----End of char-server (normal end with closing of all files)." RETCODE); +} + +int do_init(int argc, char **argv) { + int i; + + // a newline in the log... + char_log(""); + char_log("The char-server starting..." RETCODE); + + char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]); + lan_config_read((argc > 1) ? argv[1] : LOGIN_LAN_CONF_NAME); + + login_ip = inet_addr(login_ip_str); + char_ip = inet_addr(char_ip_str); + + for(i = 0; i < MAX_MAP_SERVERS; i++) { + memset(&server[i], 0, sizeof(struct mmo_map_server)); + server_fd[i] = -1; + } + + mmo_char_init(); + + update_online = time(NULL); + create_online_files(); // update online players files at start of the server + + inter_init((argc > 2) ? argv[2] : inter_cfgName); // inter server 初期化 + + set_termfunc(do_final); + set_defaultparse(parse_char); + + char_fd = make_listen_port(char_port); + + add_timer_func_list(check_connect_login_server, "check_connect_login_server"); + add_timer_func_list(send_users_tologin, "send_users_tologin"); + add_timer_func_list(mmo_char_sync_timer, "mmo_char_sync_timer"); + + i = add_timer_interval(gettick() + 1000, check_connect_login_server, 0, 0, 10 * 1000); + i = add_timer_interval(gettick() + 1000, send_users_tologin, 0, 0, 5 * 1000); + i = add_timer_interval(gettick() + autosave_interval, mmo_char_sync_timer, 0, 0, autosave_interval); + + if(anti_freeze_enable > 0) { + add_timer_func_list(map_anti_freeze_system, "map_anti_freeze_system"); + i = add_timer_interval(gettick() + 1000, map_anti_freeze_system, 0, 0, ANTI_FREEZE_INTERVAL * 1000); // checks every X seconds user specifies + } + + char_log("The char-server is ready (Server is listening on the port %d)." RETCODE, char_port); + + printf("The char-server is \033[1;32mready\033[0m (Server is listening on the port %d).\n\n", char_port); + + return 0; +} diff --git a/misc/src/char/char.h b/misc/src/char/char.h new file mode 100644 index 0000000..989ca2f --- /dev/null +++ b/misc/src/char/char.h @@ -0,0 +1,31 @@ +// $Id: char.h,v 1.1.1.1 2004/09/10 17:26:50 MagicalTux Exp $ +#ifndef _CHAR_H_ +#define _CHAR_H_ + +#define MAX_MAP_SERVERS 30 + +#define CHAR_CONF_NAME "conf/char_athena.conf" + +#define LOGIN_LAN_CONF_NAME "conf/lan_support.conf" + +#define DEFAULT_AUTOSAVE_INTERVAL 300*1000 + +struct mmo_map_server{ + long ip; + short port; + int users; + char map[MAX_MAP_PER_SERVER][16]; +}; + +int search_character_index(char* character_name); +char * search_character_name(int index); + +int mapif_sendall(unsigned char *buf, unsigned int len); +int mapif_sendallwos(int fd,unsigned char *buf, unsigned int len); +int mapif_send(int fd,unsigned char *buf, unsigned int len); + +int char_log(char *fmt, ...); + +extern int autosave_interval; + +#endif diff --git a/misc/src/char/diff.diff b/misc/src/char/diff.diff new file mode 100644 index 0000000..61e91c7 --- /dev/null +++ b/misc/src/char/diff.diff @@ -0,0 +1,4242 @@ +--- char.c 2006-02-17 04:15:48.000000000 +0100 ++++ newchar/char.c 2006-03-15 19:55:35.000000000 +0100 +@@ -1,73 +1,100 @@ +-// $Id: char.c,v 1.3 2004/09/13 16:52:16 Yor Exp $ +-// original : char2.c 2003/03/14 11:58:35 Rev.1.5 ++// Copyright (c) Athena Dev Teams - Licensed under GNU GPL ++// For more information, see LICENCE in the main folder + + #include +-#include + #include + #include ++ ++#ifdef _WIN32 ++#include ++typedef long in_addr_t; ++#else ++#include + #include +-#include +-#include ++#include ++#include + #include ++#include + #include ++#endif ++ ++#include + #include + #include + #include +-#include +-#include + #include ++#include + +-#include "core.h" +-#include "socket.h" +-#include "timer.h" +-#include "mmo.h" +-#include "version.h" +-#include "lock.h" +-#include "char.h" ++#include "../common/strlib.h" ++#include "../common/core.h" ++#include "../common/socket.h" ++#include "../common/timer.h" ++#include "../common/mmo.h" ++#include "../common/db.h" ++#include "../common/version.h" ++#include "../common/lock.h" ++#include "../common/showmsg.h" ++#include "../common/malloc.h" + ++#include "char.h" + #include "inter.h" + #include "int_pet.h" + #include "int_guild.h" + #include "int_party.h" + #include "int_storage.h" +- +-#ifdef MEMWATCH +-#include "memwatch.h" ++#ifdef ENABLE_SC_SAVING ++#include "int_status.h" + #endif + + struct mmo_map_server server[MAX_MAP_SERVERS]; + int server_fd[MAX_MAP_SERVERS]; +-int server_freezeflag[MAX_MAP_SERVERS]; // Map-server anti-freeze system. Counter. 5 ok, 4...0 freezed +-int anti_freeze_enable = 0; +-int ANTI_FREEZE_INTERVAL = 6; + + int login_fd, char_fd; + char userid[24]; + char passwd[24]; + char server_name[20]; +-char wisp_server_name[24] = "Server"; ++char wisp_server_name[NAME_LENGTH] = "Server"; ++int login_ip_set_ = 0; + char login_ip_str[16]; +-int login_ip; ++in_addr_t login_ip; + int login_port = 6900; ++int char_ip_set_ = 0; + char char_ip_str[16]; +-int char_ip; ++int bind_ip_set_ = 0; ++char bind_ip_str[16]; ++in_addr_t char_ip; + int char_port = 6121; + int char_maintenance; + int char_new; ++int char_new_display; + int email_creation = 0; // disabled by default +-char char_txt[1024]; +-char backup_txt[1024]; //By zanetheinsane ++char char_txt[1024]="save/athena.txt"; ++char backup_txt[1024]="save/backup.txt"; //By zanetheinsane ++char friends_txt[1024]="save/friends.txt"; // davidsiaw + char backup_txt_flag = 0; // The backup_txt file was created because char deletion bug existed. Now it's finish and that take a lot of time to create a second file when there are a lot of characters. => option By [Yor] + char unknown_char_name[1024] = "Unknown"; + char char_log_filename[1024] = "log/char.log"; +-//Added for lan support +-char lan_map_ip[128]; +-int subneti[4]; +-int subnetmaski[4]; ++char db_path[1024]="db"; ++ ++// Advanced subnet check [LuzZza] ++struct _subnet { ++ long subnet; ++ long mask; ++ long char_ip; ++ long map_ip; ++} subnet[16]; ++ ++int subnet_count = 0; ++ + int name_ignoring_case = 0; // Allow or not identical name for characters but with a different case by [Yor] + int char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor] ++//The following are characters that are trimmed regardless because they cause confusion and problems on the servers. [Skotlex] ++#define TRIM_CHARS "\032\t\n " + char char_name_letters[1024] = ""; // list of letters/symbols authorised (or not) in a character name. by [Yor] + ++int log_char = 1; // loggin char or not [devil] ++int log_inter = 1; // loggin inter or not [devil] ++ + struct char_session_data{ + int account_id, login_id1, login_id2, sex; + int found_char[9]; +@@ -83,18 +110,31 @@ + int auth_fifo_pos = 0; + + int check_ip_flag = 1; // It's to check IP of a player between char-server and other servers (part of anti-hacking system) ++static int online_check = 1; //If one, it won't let players connect when their account is already registered online and will send the relevant map server a kick user request. [Skotlex] ++ ++int char_id_count = START_CHAR_NUM; ++struct character_data { ++ struct mmo_charstatus status; ++ int global_num; ++ struct global_reg global[GLOBAL_REG_NUM]; ++} *char_dat; + +-int char_id_count = 150000; +-struct mmo_charstatus *char_dat; + int char_num, char_max; + int max_connect_user = 0; ++int gm_allow_level = 99; + int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL; ++int save_log = 1; + int start_zeny = 500; + int start_weapon = 1201; +-int start_armor = 1202; ++int start_armor = 2301; ++ ++//Custom limits for the fame lists. [Skotlex] ++int fame_list_size_chemist = MAX_FAME_LIST; ++int fame_list_size_smith = MAX_FAME_LIST; ++int fame_list_size_taekwon = MAX_FAME_LIST; + + // Initial position (it's possible to set it in conf file) +-struct point start_point = {"new_1-1.gat", 53, 111}; ++struct point start_point = { 0, 53, 111}; + + struct gm_account *gm_account = NULL; + int GM_num = 0; +@@ -107,16 +147,31 @@ + int online_refresh_html = 20; // refresh time (in sec) of the html file in the explorer + int online_gm_display_min_level = 20; // minimum GM level to display 'GM' when we want to display it + +-int *online_chars; // same size of char_dat, and id value of current server (or -1) ++//These are used to aid the map server in identifying valid clients. [Skotlex] ++static int max_account_id = DEFAULT_MAX_ACCOUNT_ID, max_char_id = DEFAULT_MAX_CHAR_ID; ++ ++struct online_char_data { ++ int account_id; ++ int char_id; ++ short server; ++ unsigned waiting_disconnect :1; ++}; ++ ++struct dbt *online_char_db; //Holds all online characters. ++ + time_t update_online; // to update online files when we receiving information from a server (not less than 8 seconds) + ++int console = 0; ++ + //------------------------------ + // Writing function of logs file + //------------------------------ + int char_log(char *fmt, ...) { ++ if(log_char) ++ { + FILE *logfp; + va_list ap; +- struct timeval tv; ++ time_t raw_time; + char tmpstr[2048]; + + va_start(ap, fmt); +@@ -126,33 +181,16 @@ + if (fmt[0] == '\0') // jump a line if no message + fprintf(logfp, RETCODE); + else { +- gettimeofday(&tv, NULL); +- strftime(tmpstr, 24, "%d-%m-%Y %H:%M:%S", localtime(&(tv.tv_sec))); +- sprintf(tmpstr + 19, ".%03d: %s", (int)tv.tv_usec / 1000, fmt); ++ time(&raw_time); ++ strftime(tmpstr, 24, "%d-%m-%Y %H:%M:%S", localtime(&raw_time)); ++ sprintf(tmpstr + 19, ": %s", fmt); + vfprintf(logfp, tmpstr, ap); + } + fclose(logfp); + } +- + va_end(ap); +- return 0; +-} +- +-//----------------------------------------------------- +-// Function to suppress control characters in a string. +-//----------------------------------------------------- +-int remove_control_chars(unsigned char *str) { +- int i; +- int change = 0; +- +- for(i = 0; str[i]; i++) { +- if (str[i] < 32) { +- str[i] = '_'; +- change = 1; +- } + } +- +- return change; ++ return 0; + } + + //---------------------------------------------------------------------- +@@ -183,9 +221,9 @@ + index = -1; + for(i = 0; i < char_num; i++) { + // Without case sensitive check (increase the number of similar character names found) +- if (stricmp(char_dat[i].name, character_name) == 0) { ++ if (stricmp(char_dat[i].status.name, character_name) == 0) { + // Strict comparison (if found, we finish the function immediatly with correct value) +- if (strcmp(char_dat[i].name, character_name) == 0) ++ if (strcmp(char_dat[i].status.name, character_name) == 0) + return i; + quantity++; + index = i; +@@ -206,30 +244,140 @@ + char * search_character_name(int index) { + + if (index >= 0 && index < char_num) +- return char_dat[index].name; ++ return char_dat[index].status.name; + + return unknown_char_name; + } + ++static void * create_online_char_data(DBKey key, va_list args) { ++ struct online_char_data* character; ++ character = aCalloc(1, sizeof(struct online_char_data)); ++ character->account_id = key.i; ++ character->char_id = -1; ++ character->server = -1; ++ return character; ++} ++ ++static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, int data); ++ + //------------------------------------------------- +-// Function to create the character line (for save) ++// Set Character online/offline [Wizputer] + //------------------------------------------------- +-int mmo_char_tostr(char *str, struct mmo_charstatus *p) { ++ ++void set_char_online(int map_id, int char_id, int account_id) { ++ struct online_char_data* character; ++ ++ if ( char_id != 99 && (max_account_id < account_id || max_char_id < char_id)) ++ { //Notify map-server of the new max IDs [Skotlex] ++ if (account_id > max_account_id) ++ max_account_id = account_id; ++ if (char_id > max_char_id) ++ max_char_id = char_id; ++ mapif_send_maxid(max_account_id, max_char_id); ++ } ++ character = idb_ensure(online_char_db, account_id, create_online_char_data); ++ if (online_check && character->char_id != -1 && character->server > -1 && character->server != map_id) ++ { ++ ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n", ++ character->account_id, character->char_id, character->server, map_id, account_id, char_id); ++ mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2); ++ } ++ character->waiting_disconnect = 0; ++ character->char_id = (char_id==99)?-1:char_id; ++ character->server = (char_id==99)?-1:map_id; ++ ++ if (login_fd <= 0 || session[login_fd]->eof) ++ return; ++ WFIFOHEAD(login_fd, 6); ++ WFIFOW(login_fd,0) = 0x272b; ++ WFIFOL(login_fd,2) = account_id; ++ WFIFOSET(login_fd,6); ++ ++ //printf ("set online\n"); ++} ++void set_char_offline(int char_id, int account_id) { ++ struct online_char_data* character; ++ ++ if ((character = idb_get(online_char_db, account_id)) != NULL) ++ { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex] ++ character->char_id = -1; ++ character->server = -1; ++ character->waiting_disconnect = 0; ++ } ++ if (login_fd <= 0 || session[login_fd]->eof) ++ return; ++ WFIFOHEAD(login_fd, 6); ++ WFIFOW(login_fd,0) = 0x272c; ++ WFIFOL(login_fd,2) = account_id; ++ WFIFOSET(login_fd,6); ++ ++} ++ ++static int char_db_setoffline(DBKey key, void* data, va_list ap) { ++ struct online_char_data* character = (struct online_char_data*)data; ++ int server = va_arg(ap, int); ++ if (server == -1) { ++ character->char_id = -1; ++ character->server = -1; ++ character->waiting_disconnect = 0; ++ } else if (character->server == server) ++ character->server = -2; //In some map server that we aren't connected to. ++ return 0; ++} ++ ++void set_all_offline(void) { ++ online_char_db->foreach(online_char_db,char_db_setoffline,-1); ++ if (login_fd <= 0 || session[login_fd]->eof) ++ return; ++ WFIFOHEAD(login_fd, 6); ++ WFIFOW(login_fd,0) = 0x272c; ++ WFIFOL(login_fd,2) = 99; ++ WFIFOSET(login_fd,6); ++ ++ //printf ("set all offline\n"); ++} ++ ++/*--------------------------------------------------- ++ Make a data line for friends list ++ --------------------------------------------------*/ ++ ++int mmo_friends_list_data_str(char *str, struct mmo_charstatus *p) { + int i; + char *str_p = str; ++ str_p += sprintf(str_p, "%d", p->char_id); ++ ++ for (i=0;ifriends[i].account_id > 0 && p->friends[i].char_id > 0 && p->friends[i].name[0]) ++ str_p += sprintf(str_p, ",%d,%d,%s", p->friends[i].account_id, p->friends[i].char_id, p->friends[i].name); ++ else ++ str_p += sprintf(str_p,",,,"); ++ } ++ ++ str_p += '\0'; ++ ++ return 0; ++} ++ ++//------------------------------------------------- ++// Function to create the character line (for save) ++//------------------------------------------------- ++int mmo_char_tostr(char *str, struct mmo_charstatus *p, struct global_reg *reg, int reg_num) { ++ int i,j; ++ char *str_p = str; + ++ /* We shouldn't need this anymore... [Skotlex] + // on multi-map server, sometimes it's posssible that last_point become void. (reason???) We check that to not lost character at restart. +- if (p->last_point.map[0] == '\0') { +- memcpy(p->last_point.map, "prontera.gat", 16); ++ if (!p->last_point.map) { ++ p->last_point.map = mapindex_name2id(MAP_PRONTERA); + p->last_point.x = 273; + p->last_point.y = 354; + } +- +- str_p += sprintf(str_p, "%d\t%d,%d\t%s\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" ++ */ ++ str_p += sprintf(str_p, "%d\t%d,%d\t%s\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" + "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" +- "\t%s,%d,%d\t%s,%d,%d,%d\t", ++ "\t%s,%d,%d\t%s,%d,%d,%d,%d,%d,%d,%d\t", + p->char_id, p->account_id, p->char_num, p->name, // +- p->class, p->base_level, p->job_level, ++ p->class_, p->base_level, p->job_level, + p->base_exp, p->job_exp, p->zeny, + p->hp, p->max_hp, p->sp, p->max_sp, + p->str, p->agi, p->vit, p->int_, p->dex, p->luk, +@@ -238,32 +386,34 @@ + p->party_id, p->guild_id, p->pet_id, + p->hair, p->hair_color, p->clothes_color, + p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom, +- p->last_point.map, p->last_point.x, p->last_point.y, // +- p->save_point.map, p->save_point.x, p->save_point.y, +- p->partner_id); ++ mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y, // ++ mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y, ++ p->partner_id,p->father,p->mother,p->child,p->fame); + for(i = 0; i < 10; i++) +- if (p->memo_point[i].map[0]) { +- str_p += sprintf(str_p, "%s,%d,%d", p->memo_point[i].map, p->memo_point[i].x, p->memo_point[i].y); ++ if (p->memo_point[i].map) { ++ str_p += sprintf(str_p, "%s,%d,%d", mapindex_id2name(p->memo_point[i].map), p->memo_point[i].x, p->memo_point[i].y); + } + *(str_p++) = '\t'; + + for(i = 0; i < MAX_INVENTORY; i++) + if (p->inventory[i].nameid) { +- str_p += sprintf(str_p, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d ", ++ str_p += sprintf(str_p,"%d,%d,%d,%d,%d,%d,%d", + p->inventory[i].id, p->inventory[i].nameid, p->inventory[i].amount, p->inventory[i].equip, +- p->inventory[i].identify, p->inventory[i].refine, p->inventory[i].attribute, +- p->inventory[i].card[0], p->inventory[i].card[1], p->inventory[i].card[2], p->inventory[i].card[3], +- p->inventory[i].broken); ++ p->inventory[i].identify,p->inventory[i].refine,p->inventory[i].attribute); ++ for(j=0; jinventory[i].card[j]); ++ str_p += sprintf(str_p," "); + } + *(str_p++) = '\t'; + + for(i = 0; i < MAX_CART; i++) + if (p->cart[i].nameid) { +- str_p += sprintf(str_p, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d ", ++ str_p += sprintf(str_p,"%d,%d,%d,%d,%d,%d,%d", + p->cart[i].id, p->cart[i].nameid, p->cart[i].amount, p->cart[i].equip, +- p->cart[i].identify, p->cart[i].refine, p->cart[i].attribute, +- p->cart[i].card[0], p->cart[i].card[1], p->cart[i].card[2], p->cart[i].card[3], +- p->cart[i].broken); ++ p->cart[i].identify,p->cart[i].refine,p->cart[i].attribute); ++ for(j=0; jcart[i].card[j]); ++ str_p += sprintf(str_p," "); + } + *(str_p++) = '\t'; + +@@ -273,9 +423,9 @@ + } + *(str_p++) = '\t'; + +- for(i = 0; i < p->global_reg_num; i++) +- if (p->global_reg[i].str[0]) +- str_p += sprintf(str_p, "%s,%d ", p->global_reg[i].str, p->global_reg[i].value); ++ for(i = 0; i < reg_num; i++) ++ if (reg[i].str[0]) ++ str_p += sprintf(str_p, "%s,%s ", reg[i].str, reg[i].value); + *(str_p++) = '\t'; + + *str_p = '\0'; +@@ -285,20 +435,62 @@ + //------------------------------------------------------------------------- + // Function to set the character from the line (at read of characters file) + //------------------------------------------------------------------------- +-int mmo_char_fromstr(char *str, struct mmo_charstatus *p) { ++int mmo_char_fromstr(char *str, struct mmo_charstatus *p, struct global_reg *reg, int *reg_num) { ++ char tmp_str[3][128]; //To avoid deleting chars with too long names. + int tmp_int[256]; +- int set, next, len, i; ++ unsigned int tmp_uint[2]; //To read exp.... ++ int set, next, len, i, j; + + // initilialise character + memset(p, '\0', sizeof(struct mmo_charstatus)); + +- // If it's not char structure of version 1008 and after +- if ((set = sscanf(str, "%d\t%d,%d\t%[^\t]\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" ++ // If it's not char structure of version 1488 and after ++ if ((set = sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" ++ "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" ++ "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d,%d,%d,%d,%d%n", ++ &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], ++ &tmp_int[3], &tmp_int[4], &tmp_int[5], ++ &tmp_uint[0], &tmp_uint[1], &tmp_int[8], ++ &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], ++ &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], ++ &tmp_int[19], &tmp_int[20], ++ &tmp_int[21], &tmp_int[22], &tmp_int[23], // ++ &tmp_int[24], &tmp_int[25], &tmp_int[26], ++ &tmp_int[27], &tmp_int[28], &tmp_int[29], ++ &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], ++ tmp_str[1], &tmp_int[35], &tmp_int[36], ++ tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39], ++ &tmp_int[40], &tmp_int[41], &tmp_int[42], &tmp_int[43], &next)) != 47) ++ { ++ tmp_int[43] = 0; ++ // If it's not char structure of version 1363 and after ++ if ((set = sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" ++ "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" ++ "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d,%d,%d,%d%n", ++ &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], // ++ &tmp_int[3], &tmp_int[4], &tmp_int[5], ++ &tmp_uint[0], &tmp_uint[1], &tmp_int[8], ++ &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], ++ &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], ++ &tmp_int[19], &tmp_int[20], ++ &tmp_int[21], &tmp_int[22], &tmp_int[23], // ++ &tmp_int[24], &tmp_int[25], &tmp_int[26], ++ &tmp_int[27], &tmp_int[28], &tmp_int[29], ++ &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], ++ tmp_str[1], &tmp_int[35], &tmp_int[36], // ++ tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39], ++ &tmp_int[40], &tmp_int[41], &tmp_int[42], &next)) != 46) ++ { ++ tmp_int[40] = 0; // father ++ tmp_int[41] = 0; // mother ++ tmp_int[42] = 0; // child ++ // If it's not char structure of version 1008 and before 1363 ++ if ((set = sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" + "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" +- "\t%[^,],%d,%d\t%[^,],%d,%d,%d%n", +- &tmp_int[0], &tmp_int[1], &tmp_int[2], p->name, // ++ "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d%n", ++ &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], // + &tmp_int[3], &tmp_int[4], &tmp_int[5], +- &tmp_int[6], &tmp_int[7], &tmp_int[8], ++ &tmp_uint[0], &tmp_uint[1], &tmp_int[8], + &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], + &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], + &tmp_int[19], &tmp_int[20], +@@ -306,16 +498,17 @@ + &tmp_int[24], &tmp_int[25], &tmp_int[26], + &tmp_int[27], &tmp_int[28], &tmp_int[29], + &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], +- p->last_point.map, &tmp_int[35], &tmp_int[36], // +- p->save_point.map, &tmp_int[37], &tmp_int[38], &tmp_int[39], &next)) != 43) { ++ tmp_str[1], &tmp_int[35], &tmp_int[36], // ++ tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39], &next)) != 43) ++ { + tmp_int[39] = 0; // partner id + // If not char structure from version 384 to 1007 +- if ((set = sscanf(str, "%d\t%d,%d\t%[^\t]\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" ++ if ((set = sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" + "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" +- "\t%[^,],%d,%d\t%[^,],%d,%d%n", +- &tmp_int[0], &tmp_int[1], &tmp_int[2], p->name, // ++ "\t%127[^,],%d,%d\t%127[^,],%d,%d%n", ++ &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], // + &tmp_int[3], &tmp_int[4], &tmp_int[5], +- &tmp_int[6], &tmp_int[7], &tmp_int[8], ++ &tmp_uint[0], &tmp_uint[1], &tmp_int[8], + &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], + &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], + &tmp_int[19], &tmp_int[20], +@@ -323,16 +516,17 @@ + &tmp_int[24], &tmp_int[25], &tmp_int[26], + &tmp_int[27], &tmp_int[28], &tmp_int[29], + &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], +- p->last_point.map, &tmp_int[35], &tmp_int[36], // +- p->save_point.map, &tmp_int[37], &tmp_int[38], &next)) != 42) { ++ tmp_str[1], &tmp_int[35], &tmp_int[36], // ++ tmp_str[2], &tmp_int[37], &tmp_int[38], &next)) != 42) ++ { + // It's char structure of a version before 384 + tmp_int[26] = 0; // pet id +- set = sscanf(str, "%d\t%d,%d\t%[^\t]\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" ++ set = sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" + "\t%d,%d,%d\t%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d" +- "\t%[^,],%d,%d\t%[^,],%d,%d%n", +- &tmp_int[0], &tmp_int[1], &tmp_int[2], p->name, // ++ "\t%127[^,],%d,%d\t%127[^,],%d,%d%n", ++ &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], // + &tmp_int[3], &tmp_int[4], &tmp_int[5], +- &tmp_int[6], &tmp_int[7], &tmp_int[8], ++ &tmp_uint[0], &tmp_uint[1], &tmp_int[8], + &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], + &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], + &tmp_int[19], &tmp_int[20], +@@ -340,8 +534,8 @@ + &tmp_int[24], &tmp_int[25], // + &tmp_int[27], &tmp_int[28], &tmp_int[29], + &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34], +- p->last_point.map, &tmp_int[35], &tmp_int[36], // +- p->save_point.map, &tmp_int[37], &tmp_int[38], &next); ++ tmp_str[1], &tmp_int[35], &tmp_int[36], // ++ tmp_str[2], &tmp_int[37], &tmp_int[38], &next); + set += 2; + //printf("char: old char data ver.1\n"); + // Char structure of version 1007 or older +@@ -351,19 +545,30 @@ + } + // Char structure of version 1008+ + } else { ++ set += 3; + //printf("char: new char data ver.3\n"); + } +- if (set != 43) ++ // Char structture of version 1363+ ++ } else { ++ set++; ++ //printf("char: new char data ver.4\n"); ++ } ++ // Char structure of version 1488+ ++ } else { ++ //printf("char: new char data ver.5\n"); ++ } ++ if (set != 47) + return 0; + ++ memcpy(p->name, tmp_str[0], NAME_LENGTH-1); //Overflow protection [Skotlex] + p->char_id = tmp_int[0]; + p->account_id = tmp_int[1]; + p->char_num = tmp_int[2]; +- p->class = tmp_int[3]; ++ p->class_ = tmp_int[3]; + p->base_level = tmp_int[4]; + p->job_level = tmp_int[5]; +- p->base_exp = tmp_int[6]; +- p->job_exp = tmp_int[7]; ++ p->base_exp = tmp_uint[0]; ++ p->job_exp = tmp_uint[1]; + p->zeny = tmp_int[8]; + p->hp = tmp_int[9]; + p->max_hp = tmp_int[10]; +@@ -391,31 +596,37 @@ + p->head_top = tmp_int[32]; + p->head_mid = tmp_int[33]; + p->head_bottom = tmp_int[34]; ++ p->last_point.map = mapindex_name2id(tmp_str[1]); + p->last_point.x = tmp_int[35]; + p->last_point.y = tmp_int[36]; ++ p->save_point.map = mapindex_name2id(tmp_str[2]); + p->save_point.x = tmp_int[37]; + p->save_point.y = tmp_int[38]; + p->partner_id = tmp_int[39]; ++ p->father = tmp_int[40]; ++ p->mother = tmp_int[41]; ++ p->child = tmp_int[42]; ++ p->fame = tmp_int[43]; + + // Some checks + for(i = 0; i < char_num; i++) { +- if (char_dat[i].char_id == p->char_id) { +- printf("\033[1;31mmmo_auth_init: ******Error: a character has an identical id to another.\n"); +- printf(" character id #%d -> new character not readed.\n", p->char_id); +- printf(" Character saved in log file.\033[0m\n"); ++ if (char_dat[i].status.char_id == p->char_id) { ++ ShowError(CL_RED"mmmo_auth_init: a character has an identical id to another.\n"); ++ ShowError(" character id #%d -> new character not readed.\n", p->char_id); ++ ShowError(" Character saved in log file."CL_RESET"\n"); + return -1; +- } else if (strcmp(char_dat[i].name, p->name) == 0) { +- printf("\033[1;31mmmo_auth_init: ******Error: character name already exists.\n"); +- printf(" character name '%s' -> new character not readed.\n", p->name); +- printf(" Character saved in log file.\033[0m\n"); ++ } else if (strcmp(char_dat[i].status.name, p->name) == 0) { ++ ShowError(CL_RED"mmmo_auth_init: a character name already exists.\n"); ++ ShowError(" character name '%s' -> new character not read.\n", p->name); ++ ShowError(" Character saved in log file."CL_RESET"\n"); + return -2; + } + } + + if (strcmpi(wisp_server_name, p->name) == 0) { +- printf("mmo_auth_init: ******WARNING: character name has wisp server name.\n"); +- printf(" Character name '%s' = wisp server name '%s'.\n", p->name, wisp_server_name); +- printf(" Character readed. Suggestion: change the wisp server name.\n"); ++ ShowWarning("mmo_auth_init: ******WARNING: character name has wisp server name.\n"); ++ ShowWarning(" Character name '%s' = wisp server name '%s'.\n", p->name, wisp_server_name); ++ ShowWarning(" Character readed. Suggestion: change the wisp server name.\n"); + char_log("mmo_auth_init: ******WARNING: character name has wisp server name: Character name '%s' = wisp server name '%s'." RETCODE, + p->name, wisp_server_name); + } +@@ -426,8 +637,9 @@ + next++; + + for(i = 0; str[next] && str[next] != '\t'; i++) { +- if (sscanf(str+next, "%[^,],%d,%d%n", p->memo_point[i].map, &tmp_int[0], &tmp_int[1], &len) != 3) ++ if (sscanf(str+next, "%[^,],%d,%d%n", tmp_str[0], &tmp_int[0], &tmp_int[1], &len) != 3) + return -3; ++ p->memo_point[i].map = mapindex_name2id(tmp_str[0]); + p->memo_point[i].x = tmp_int[0]; + p->memo_point[i].y = tmp_int[1]; + next += len; +@@ -438,18 +650,10 @@ + next++; + + for(i = 0; str[next] && str[next] != '\t'; i++) { +- if (sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", ++ if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d%[0-9,-]%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], +- &tmp_int[4], &tmp_int[5], &tmp_int[6], +- &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &tmp_int[11], &len) == 12) { +- // do nothing, it's ok +- } else if (sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", +- &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], +- &tmp_int[4], &tmp_int[5], &tmp_int[6], +- &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &len) == 11) { +- tmp_int[11] = 0; // broken doesn't exist in this version -> 0 +- } else // invalid structure +- return -4; ++ &tmp_int[4], &tmp_int[5], &tmp_int[6], tmp_str[0], &len) == 8) ++ { + p->inventory[i].id = tmp_int[0]; + p->inventory[i].nameid = tmp_int[1]; + p->inventory[i].amount = tmp_int[2]; +@@ -457,31 +661,23 @@ + p->inventory[i].identify = tmp_int[4]; + p->inventory[i].refine = tmp_int[5]; + p->inventory[i].attribute = tmp_int[6]; +- p->inventory[i].card[0] = tmp_int[7]; +- p->inventory[i].card[1] = tmp_int[8]; +- p->inventory[i].card[2] = tmp_int[9]; +- p->inventory[i].card[3] = tmp_int[10]; +- p->inventory[i].broken = tmp_int[11]; ++ ++ for(j = 0; j < MAX_SLOTS && tmp_str[0] && sscanf(tmp_str[0], ",%d%[0-9,-]",&tmp_int[0], tmp_str[0]) > 0; j++) ++ p->inventory[i].card[j] = tmp_int[0]; ++ + next += len; + if (str[next] == ' ') + next++; ++ } else // invalid structure ++ return -4; + } +- + next++; + + for(i = 0; str[next] && str[next] != '\t'; i++) { +- if (sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", +- &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], +- &tmp_int[4], &tmp_int[5], &tmp_int[6], +- &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &tmp_int[11], &len) == 12) { +- // do nothing, it's ok +- } else if (sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", ++ if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d%[0-9,-]%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], +- &tmp_int[4], &tmp_int[5], &tmp_int[6], +- &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &len) == 11) { +- tmp_int[11] = 0; // broken doesn't exist in this version -> 0 +- } else // invalid structure +- return -5; ++ &tmp_int[4], &tmp_int[5], &tmp_int[6], tmp_str[0], &len) == 8) ++ { + p->cart[i].id = tmp_int[0]; + p->cart[i].nameid = tmp_int[1]; + p->cart[i].amount = tmp_int[2]; +@@ -489,14 +685,15 @@ + p->cart[i].identify = tmp_int[4]; + p->cart[i].refine = tmp_int[5]; + p->cart[i].attribute = tmp_int[6]; +- p->cart[i].card[0] = tmp_int[7]; +- p->cart[i].card[1] = tmp_int[8]; +- p->cart[i].card[2] = tmp_int[9]; +- p->cart[i].card[3] = tmp_int[10]; +- p->cart[i].broken = tmp_int[11]; ++ ++ for(j = 0; j < MAX_SLOTS && tmp_str && sscanf(tmp_str[0], ",%d%[0-9,-]",&tmp_int[0], tmp_str[0]) > 0; j++) ++ p->cart[i].card[j] = tmp_int[0]; ++ + next += len; + if (str[next] == ' ') + next++; ++ } else // invalid structure ++ return -5; + } + + next++; +@@ -514,11 +711,11 @@ + next++; + + for(i = 0; str[next] && str[next] != '\t' && str[next] != '\n' && str[next] != '\r'; i++) { // global_reg実装以前のathena.txt互換のため一応'\n'チェック +- if (sscanf(str + next, "%[^,],%d%n", p->global_reg[i].str, &p->global_reg[i].value, &len) != 2) { ++ if (sscanf(str + next, "%[^,],%[^ ] %n", reg[i].str, reg[i].value, &len) != 2) { + // because some scripts are not correct, the str can be "". So, we must check that. + // If it's, we must not refuse the character, but just this REG value. + // Character line will have something like: nov_2nd_cos,9 ,9 nov_1_2_cos_c,1 (here, ,9 is not good) +- if (str[next] == ',' && sscanf(str + next, ",%d%n", &p->global_reg[i].value, &len) == 1) ++ if (str[next] == ',' && sscanf(str + next, ",%[^ ] %n", reg[i].value, &len) == 1) + i--; + else + return -7; +@@ -527,39 +724,127 @@ + if (str[next] == ' ') + next++; + } +- p->global_reg_num = i; ++ *reg_num = i; + + return 1; + } ++//--------------------------------- ++// Function to read friend list ++//--------------------------------- ++ ++int parse_friend_txt(struct mmo_charstatus *p) ++{ ++ char line[1024], temp[1024]; ++ int pos = 0, count = 0, next; ++ int i,len; ++ FILE *fp; ++ ++ // Open the file and look for the ID ++ fp = fopen(friends_txt, "r"); ++ ++ if(fp == NULL) ++ return -1; ++ ++ while(fgets(line, sizeof(line)-1, fp)) { ++ ++ if(line[0] == '/' && line[1] == '/') ++ continue; ++ if (sscanf(line, "%d%n",&i, &pos) < 1 || i != p->char_id) ++ continue; //Not this line... ++ //Read friends ++ len = strlen(line); ++ next = pos; ++ for (count = 0; next < len && count < MAX_FRIENDS; count++) ++ { //Read friends. ++ if (sscanf(line+next, ",%d,%d,%23[^,]%n",&p->friends[count].account_id,&p->friends[count].char_id, p->friends[count].name, &pos) < 3) ++ { //Invalid friend? ++ memset(&p->friends[count], 0, sizeof(p->friends[count])); ++ break; ++ } ++ next+=pos; ++ //What IF the name contains a comma? while the next field is not a ++ //number, we assume it belongs to the current name. [Skotlex] ++ //NOTE: Of course, this will fail if someone sets their name to something like ++ //Bob,2005 but... meh, it's the problem of parsing a text file (encasing it in " ++ //won't do as quotes are also valid name chars!) ++ while(next < len && sscanf(line+next, ",%23[^,]%n", temp, &len) > 0) ++ { ++ if (atoi(temp)) ++ { //We read the next friend, just continue. ++ break; ++ } else { //Append the name. ++ next+=len; ++ if (strlen(p->friends[count].name) + strlen(temp) +1 < NAME_LENGTH) ++ { ++ strcat(p->friends[count].name, ","); ++ strcat(p->friends[count].name, temp); ++ } ++ } ++ } //End Guess Block ++ } //Friend's for. ++ break; //Finished reading. ++ } ++ /* ++ //Character names must not exceed the 23+\0 limit. [Skotlex] ++ sscanf(line, "%d,%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23[^,],%d,%23s",&cid, ++ &temp[0],p->friend_name[0], ++ &temp[1],p->friend_name[1], ++ &temp[2],p->friend_name[2], ++ &temp[3],p->friend_name[3], ++ &temp[4],p->friend_name[4], ++ &temp[5],p->friend_name[5], ++ &temp[6],p->friend_name[6], ++ &temp[7],p->friend_name[7], ++ &temp[8],p->friend_name[8], ++ &temp[9],p->friend_name[9], ++ &temp[10],p->friend_name[10], ++ &temp[11],p->friend_name[11], ++ &temp[12],p->friend_name[12], ++ &temp[13],p->friend_name[13], ++ &temp[14],p->friend_name[14], ++ &temp[15],p->friend_name[15], ++ &temp[16],p->friend_name[16], ++ &temp[17],p->friend_name[17], ++ &temp[18],p->friend_name[18], ++ &temp[19],p->friend_name[19]); ++ if (cid == p->char_id) ++ break; ++ } ++ // No register of friends list ++ if (cid == 0) { ++ fclose(fp); ++ return 0; ++ } ++ ++ // Fill in the list ++ ++ for (i=0; ifriend_id[i] = temp[i]; ++*/ ++ fclose(fp); ++ return count; ++} + + //--------------------------------- + // Function to read characters file + //--------------------------------- + int mmo_char_init(void) { + char line[65536]; +- int i; + int ret, line_count; + FILE *fp; + + char_max = 256; +- char_dat = calloc(sizeof(struct mmo_charstatus) * 256, 1); ++ char_dat = (struct character_data*)aCalloc(sizeof(struct character_data) * 256, 1); + if (!char_dat) { +- printf("out of memory: mmo_char_init (calloc of char_dat).\n"); +- exit(1); +- } +- online_chars = calloc(sizeof(int) * 256, 1); +- if (!online_chars) { +- printf("out of memory: mmo_char_init (calloc of online_chars).\n"); ++ ShowFatalError("out of memory: mmo_char_init (calloc of char_dat).\n"); + exit(1); + } +- for(i = 0; i < char_max; i++) +- online_chars[i] = -1; +- + char_num = 0; + + fp = fopen(char_txt, "r"); ++ + if (fp == NULL) { +- printf("Characters file not found: %s.\n", char_txt); ++ ShowError("Characters file not found: %s.\n", char_txt); + char_log("Characters file not found: %s." RETCODE, char_txt); + char_log("Id for the next created character: %d." RETCODE, char_id_count); + return 0; +@@ -583,30 +868,26 @@ + + if (char_num >= char_max) { + char_max += 256; +- char_dat = realloc(char_dat, sizeof(struct mmo_charstatus) * char_max); ++ char_dat = (struct character_data*)aRealloc(char_dat, sizeof(struct character_data) * char_max); + if (!char_dat) { +- printf("Out of memory: mmo_char_init (realloc of char_dat).\n"); ++ ShowFatalError("Out of memory: mmo_char_init (realloc of char_dat).\n"); + char_log("Out of memory: mmo_char_init (realloc of char_dat)." RETCODE); + exit(1); + } +- online_chars = realloc(online_chars, sizeof(int) * char_max); +- if (!online_chars) { +- printf("Out of memory: mmo_char_init (realloc of online_chars).\n"); +- char_log("Out of memory: mmo_char_init (realloc of online_chars)." RETCODE); +- exit(1); +- } +- for(i = char_max - 256; i < char_max; i++) +- online_chars[i] = -1; + } + +- ret = mmo_char_fromstr(line, &char_dat[char_num]); ++ ret = mmo_char_fromstr(line, &char_dat[char_num].status, char_dat[char_num].global, &char_dat[char_num].global_num); ++ ++ // Initialize friends list ++ parse_friend_txt(&char_dat[char_num].status); // Grab friends for the character ++ + if (ret > 0) { // negative value or zero for errors +- if (char_dat[char_num].char_id >= char_id_count) +- char_id_count = char_dat[char_num].char_id + 1; ++ if (char_dat[char_num].status.char_id >= char_id_count) ++ char_id_count = char_dat[char_num].status.char_id + 1; + char_num++; + } else { +- printf("mmo_char_init: in characters file, unable to read the line #%d.\n", line_count); +- printf(" -> Character saved in log file.\n"); ++ ShowError("mmo_char_init: in characters file, unable to read the line #%d.\n", line_count); ++ ShowError(" -> Character saved in log file.\n"); + switch (ret) { + case -1: + char_log("Duplicate character id in the next character line (character not readed):" RETCODE); +@@ -639,13 +920,13 @@ + fclose(fp); + + if (char_num == 0) { +- printf("mmo_char_init: No character found in %s.\n", char_txt); ++ ShowNotice("mmo_char_init: No character found in %s.\n", char_txt); + char_log("mmo_char_init: No character found in %s." RETCODE, char_txt); + } else if (char_num == 1) { +- printf("mmo_char_init: 1 character read in %s.\n", char_txt); ++ ShowStatus("mmo_char_init: 1 character read in %s.\n", char_txt); + char_log("mmo_char_init: 1 character read in %s." RETCODE, char_txt); + } else { +- printf("mmo_char_init: %d characters read in %s.\n", char_num, char_txt); ++ ShowStatus("mmo_char_init: %d characters read in %s.\n", char_num, char_txt); + char_log("mmo_char_init: %d characters read in %s." RETCODE, char_num, char_txt); + } + +@@ -658,20 +939,21 @@ + // Function to save characters in files (speed up by [Yor]) + //--------------------------------------------------------- + void mmo_char_sync(void) { +- char line[65536]; ++ char line[65536],f_line[1024]; + int i, j, k; + int lock; +- FILE *fp; +- int id[char_num]; ++ FILE *fp,*f_fp; ++ //int *id = (int *) aMalloc(sizeof(int) * char_num); ++ CREATE_BUFFER(id, int, char_num); + + // Sorting before save (by [Yor]) + for(i = 0; i < char_num; i++) { + id[i] = i; + for(j = 0; j < i; j++) { +- if ((char_dat[i].account_id < char_dat[id[j]].account_id) || ++ if ((char_dat[i].status.account_id < char_dat[id[j]].status.account_id) || + // if same account id, we sort by slot. +- (char_dat[i].account_id == char_dat[id[j]].account_id && +- char_dat[i].char_num < char_dat[id[j]].char_num)) { ++ (char_dat[i].status.account_id == char_dat[id[j]].status.account_id && ++ char_dat[i].status.char_num < char_dat[id[j]].status.char_num)) { + for(k = i; k > j; k--) + id[k] = id[k-1]; + id[j] = i; // id[i] +@@ -683,12 +965,12 @@ + // Data save + fp = lock_fopen(char_txt, &lock); + if (fp == NULL) { +- printf("WARNING: Server can't not save characters.\n"); ++ ShowWarning("Server can't not save characters.\n"); + char_log("WARNING: Server can't not save characters." RETCODE); + } else { + for(i = 0; i < char_num; i++) { + // create only once the line, and save it in the 2 files (it's speeder than repeat twice the loop and create twice the line) +- mmo_char_tostr(line, &char_dat[id[i]]); // use of sorted index ++ mmo_char_tostr(line, &char_dat[id[i]].status, char_dat[id[i]].global, char_dat[id[i]].global_num); // use of sorted index + fprintf(fp, "%s" RETCODE, line); + } + fprintf(fp, "%d\t%%newid%%" RETCODE, char_id_count); +@@ -699,19 +981,33 @@ + if (backup_txt_flag) { // The backup_txt file was created because char deletion bug existed. Now it's finish and that take a lot of time to create a second file when there are a lot of characters. => option By [Yor] + fp = lock_fopen(backup_txt, &lock); + if (fp == NULL) { +- printf("WARNING: Server can't not create backup of characters file.\n"); ++ ShowWarning("Server can't not create backup of characters file.\n"); + char_log("WARNING: Server can't not create backup of characters file." RETCODE); ++ //aFree(id); // free up the memory before leaving -.- [Ajarn] ++ DELETE_BUFFER(id); + return; + } + for(i = 0; i < char_num; i++) { + // create only once the line, and save it in the 2 files (it's speeder than repeat twice the loop and create twice the line) +- mmo_char_tostr(line, &char_dat[id[i]]); // use of sorted index ++ mmo_char_tostr(line, &char_dat[id[i]].status,char_dat[id[i]].global, char_dat[id[i]].global_num); // use of sorted index + fprintf(fp, "%s" RETCODE, line); + } + fprintf(fp, "%d\t%%newid%%" RETCODE, char_id_count); + lock_fclose(fp, backup_txt, &lock); + } + ++ // Friends List data save (davidsiaw) ++ f_fp = lock_fopen(friends_txt, &lock); ++ for(i = 0; i < char_num; i++) { ++ mmo_friends_list_data_str(f_line, &char_dat[id[i]].status); ++ fprintf(f_fp, "%s" RETCODE, f_line); ++ } ++ ++ lock_fclose(f_fp, friends_txt, &lock); ++ ++ //aFree(id); ++ DELETE_BUFFER(id); ++ + return; + } + +@@ -719,6 +1015,8 @@ + // Function to save (in a periodic way) datas in files + //---------------------------------------------------- + int mmo_char_sync_timer(int tid, unsigned int tick, int id, int data) { ++ if (save_log) ++ ShowInfo("Saving all files...\n"); + mmo_char_sync(); + inter_save(); + return 0; +@@ -728,21 +1026,33 @@ + // Function to create a new character + //----------------------------------- + int make_new_char(int fd, unsigned char *dat) { +- int i, j; ++ int i; + struct char_session_data *sd; ++ char name[NAME_LENGTH]; + +- sd = session[fd]->session_data; ++ sd = (struct char_session_data*)session[fd]->session_data; + + // remove control characters from the name +- dat[23] = '\0'; +- if (remove_control_chars(dat)) { ++ strncpy(name, dat, NAME_LENGTH); ++ name[NAME_LENGTH-1] = '\0'; //Trunc name to max possible value (23) ++ ++ trim(name,TRIM_CHARS); //Trim character name. [Skotlex] ++ ++ //check name != main chat nick [LuzZza] ++ if(strcmpi(name, main_chat_nick) == 0) { ++ char_log("Create char failed (%d): this nick (%s) reserved for mainchat messages." RETCODE, ++ sd->account_id, name); ++ return -1; ++ } ++ ++ if (remove_control_chars((unsigned char *)name)) { + char_log("Make new char error (control char received in the name): (connection #%d, account: %d)." RETCODE, + fd, sd->account_id); + return -1; + } + + // check lenght of character name +- if (strlen(dat) < 4) { ++ if (strlen(name) < 4) { + char_log("Make new char error (character name too small): (connection #%d, account: %d, name: '%s')." RETCODE, + fd, sd->account_id, dat); + return -1; +@@ -750,15 +1060,15 @@ + + // Check Authorised letters/symbols in the name of the character + if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised +- for (i = 0; dat[i]; i++) +- if (strchr(char_name_letters, dat[i]) == NULL) { ++ for (i = 0; i < NAME_LENGTH && name[i]; i++) ++ if (strchr(char_name_letters, name[i]) == NULL) { + char_log("Make new char error (invalid letter in the name): (connection #%d, account: %d), name: %s, invalid letter: %c." RETCODE, +- fd, sd->account_id, dat, dat[i]); ++ fd, sd->account_id, name, name[i]); + return -1; + } + } else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden +- for (i = 0; dat[i]; i++) +- if (strchr(char_name_letters, dat[i]) != NULL) { ++ for (i = 0; i < NAME_LENGTH && name[i]; i++) ++ if (strchr(char_name_letters, name[i]) != NULL) { + char_log("Make new char error (invalid letter in the name): (connection #%d, account: %d), name: %s, invalid letter: %c." RETCODE, + fd, sd->account_id, dat, dat[i]); + return -1; +@@ -767,8 +1077,8 @@ + + if (dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29] != 5*6 || // stats + dat[30] >= 9 || // slots (dat[30] can not be negativ) +- dat[33] <= 0 || dat[33] >= 20 || // hair style +- dat[31] >= 12) { // hair color (dat[31] can not be negativ) ++ dat[33] <= 0 || dat[33] >= 24 || // hair style ++ dat[31] >= 9) { // hair color (dat[31] can not be negativ) + char_log("Make new char error (invalid values): (connection #%d, account: %d) slot %d, name: %s, stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d" RETCODE, + fd, sd->account_id, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + return -1; +@@ -781,98 +1091,96 @@ + fd, sd->account_id, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + return -1; + } ++ } // now we know that every stat has proper value but we have to check if str/int agi/luk vit/dex pairs are correct ++ ++ if( ((dat[24]+dat[27]) > 10) || ((dat[25]+dat[29]) > 10) || ((dat[26]+dat[28]) > 10) ) { ++ if (log_char) { ++ char_log("Make new char error (invalid stat value): (connection #%d, account: %d) slot %d, name: %s, stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d" RETCODE, ++ fd, sd->account_id, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); ++ return -1; + } ++ } // now when we have passed all stat checks + + for(i = 0; i < char_num; i++) { +- if ((name_ignoring_case != 0 && strcmp(char_dat[i].name, dat) == 0) || +- (name_ignoring_case == 0 && strcmpi(char_dat[i].name, dat) == 0)) { ++ if ((name_ignoring_case != 0 && strncmp(char_dat[i].status.name, name, NAME_LENGTH) == 0) || ++ (name_ignoring_case == 0 && strncmpi(char_dat[i].status.name, name, NAME_LENGTH) == 0)) { + char_log("Make new char error (name already exists): (connection #%d, account: %d) slot %d, name: %s (actual name of other char: %d), stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d." RETCODE, +- fd, sd->account_id, dat[30], dat, char_dat[i].name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); ++ fd, sd->account_id, dat[30], dat, char_dat[i].status.name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + return -1; + } +- if (char_dat[i].account_id == sd->account_id && char_dat[i].char_num == dat[30]) { ++ if (char_dat[i].status.account_id == sd->account_id && char_dat[i].status.char_num == dat[30]) { + char_log("Make new char error (slot already used): (connection #%d, account: %d) slot %d, name: %s (actual name of other char: %d), stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d." RETCODE, +- fd, sd->account_id, dat[30], dat, char_dat[i].name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); ++ fd, sd->account_id, dat[30], dat, char_dat[i].status.name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + return -1; + } + } + +- if (strcmp(wisp_server_name, dat) == 0) { ++ if (strcmp(wisp_server_name, name) == 0) { + char_log("Make new char error (name used is wisp name for server): (connection #%d, account: %d) slot %d, name: %s (actual name of other char: %d), stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d." RETCODE, +- fd, sd->account_id, dat[30], dat, char_dat[i].name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); ++ fd, sd->account_id, dat[30], name, char_dat[i].status.name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + return -1; + } + + if (char_num >= char_max) { + char_max += 256; +- char_dat = realloc(char_dat, sizeof(struct mmo_charstatus) * char_max); ++ char_dat = (struct character_data*)aRealloc(char_dat, sizeof(struct character_data) * char_max); + if (!char_dat) { +- printf("Out of memory: make_new_char (realloc of char_dat).\n"); ++ ShowFatalError("Out of memory: make_new_char (realloc of char_dat).\n"); + char_log("Out of memory: make_new_char (realloc of char_dat)." RETCODE); + exit(1); + } +- online_chars = realloc(online_chars, sizeof(int) * char_max); +- if (!online_chars) { +- printf("Out of memory: make_new_char (realloc of online_chars).\n"); +- char_log("Out of memory: make_new_char (realloc of online_chars)." RETCODE); +- exit(1); +- } +- for(j = char_max - 256; j < char_max; j++) +- online_chars[j] = -1; + } + + char_log("Creation of New Character: (connection #%d, account: %d) slot %d, character Name: %s, stats: %d+%d+%d+%d+%d+%d=%d, hair: %d, hair color: %d." RETCODE, +- fd, sd->account_id, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); ++ fd, sd->account_id, dat[30], name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[24] + dat[25] + dat[26] + dat[27] + dat[28] + dat[29], dat[33], dat[31]); + +- memset(&char_dat[i], 0, sizeof(struct mmo_charstatus)); ++ memset(&char_dat[i], 0, sizeof(struct character_data)); + +- char_dat[i].char_id = char_id_count++; +- char_dat[i].account_id = sd->account_id; +- char_dat[i].char_num = dat[30]; +- strcpy(char_dat[i].name, dat); +- char_dat[i].class = 0; +- char_dat[i].base_level = 1; +- char_dat[i].job_level = 1; +- char_dat[i].base_exp = 0; +- char_dat[i].job_exp = 0; +- char_dat[i].zeny = start_zeny; +- char_dat[i].str = dat[24]; +- char_dat[i].agi = dat[25]; +- char_dat[i].vit = dat[26]; +- char_dat[i].int_ = dat[27]; +- char_dat[i].dex = dat[28]; +- char_dat[i].luk = dat[29]; +- char_dat[i].max_hp = 40 * (100 + char_dat[i].vit) / 100; +- char_dat[i].max_sp = 11 * (100 + char_dat[i].int_) / 100; +- char_dat[i].hp = char_dat[i].max_hp; +- char_dat[i].sp = char_dat[i].max_sp; +- char_dat[i].status_point = 0; +- char_dat[i].skill_point = 0; +- char_dat[i].option = 0; +- char_dat[i].karma = 0; +- char_dat[i].manner = 0; +- char_dat[i].party_id = 0; +- char_dat[i].guild_id = 0; +- char_dat[i].hair = dat[33]; +- char_dat[i].hair_color = dat[31]; +- char_dat[i].clothes_color = 0; +- char_dat[i].inventory[0].nameid = start_weapon; // Knife +- char_dat[i].inventory[0].amount = 1; +- char_dat[i].inventory[0].equip = 0x02; +- char_dat[i].inventory[0].identify = 1; +- char_dat[i].inventory[0].broken = 0; +- char_dat[i].inventory[1].nameid = start_armor; // Cotton Shirt +- char_dat[i].inventory[1].amount = 1; +- char_dat[i].inventory[1].equip = 0x10; +- char_dat[i].inventory[1].identify = 1; +- char_dat[i].inventory[1].broken = 0; +- char_dat[i].weapon = 1; +- char_dat[i].shield = 0; +- char_dat[i].head_top = 0; +- char_dat[i].head_mid = 0; +- char_dat[i].head_bottom = 0; +- memcpy(&char_dat[i].last_point, &start_point, sizeof(start_point)); +- memcpy(&char_dat[i].save_point, &start_point, sizeof(start_point)); ++ char_dat[i].status.char_id = char_id_count++; ++ char_dat[i].status.account_id = sd->account_id; ++ char_dat[i].status.char_num = dat[30]; ++ strcpy(char_dat[i].status.name,name); ++ char_dat[i].status.class_ = 0; ++ char_dat[i].status.base_level = 1; ++ char_dat[i].status.job_level = 1; ++ char_dat[i].status.base_exp = 0; ++ char_dat[i].status.job_exp = 0; ++ char_dat[i].status.zeny = start_zeny; ++ char_dat[i].status.str = dat[24]; ++ char_dat[i].status.agi = dat[25]; ++ char_dat[i].status.vit = dat[26]; ++ char_dat[i].status.int_ = dat[27]; ++ char_dat[i].status.dex = dat[28]; ++ char_dat[i].status.luk = dat[29]; ++ char_dat[i].status.max_hp = 40 * (100 + char_dat[i].status.vit) / 100; ++ char_dat[i].status.max_sp = 11 * (100 + char_dat[i].status.int_) / 100; ++ char_dat[i].status.hp = char_dat[i].status.max_hp; ++ char_dat[i].status.sp = char_dat[i].status.max_sp; ++ char_dat[i].status.status_point = 0; ++ char_dat[i].status.skill_point = 0; ++ char_dat[i].status.option = 0; ++ char_dat[i].status.karma = 0; ++ char_dat[i].status.manner = 0; ++ char_dat[i].status.party_id = 0; ++ char_dat[i].status.guild_id = 0; ++ char_dat[i].status.hair = dat[33]; ++ char_dat[i].status.hair_color = dat[31]; ++ char_dat[i].status.clothes_color = 0; ++ char_dat[i].status.inventory[0].nameid = start_weapon; // Knife ++ char_dat[i].status.inventory[0].amount = 1; ++ char_dat[i].status.inventory[0].equip = 0x02; ++ char_dat[i].status.inventory[0].identify = 1; ++ char_dat[i].status.inventory[1].nameid = start_armor; // Cotton Shirt ++ char_dat[i].status.inventory[1].amount = 1; ++ char_dat[i].status.inventory[1].equip = 0x10; ++ char_dat[i].status.inventory[1].identify = 1; ++ char_dat[i].status.weapon = 1; ++ char_dat[i].status.shield = 0; ++ char_dat[i].status.head_top = 0; ++ char_dat[i].status.head_mid = 0; ++ char_dat[i].status.head_bottom = 0; ++ memcpy(&char_dat[i].status.last_point, &start_point, sizeof(start_point)); ++ memcpy(&char_dat[i].status.save_point, &start_point, sizeof(start_point)); + char_num++; + + mmo_char_sync(); +@@ -882,8 +1190,8 @@ + //---------------------------------------------------- + // This function return the name of the job (by [Yor]) + //---------------------------------------------------- +-char * job_name(int class) { +- switch (class) { ++char * job_name(int class_) { ++ switch (class_) { + case 0: return "Novice"; + case 1: return "Swordsman"; + case 2: return "Mage"; +@@ -957,108 +1265,138 @@ + return "Unknown Job"; + } + +-//------------------------------------------------------------- +-// Function to create the online files (txt and html). by [Yor] +-//------------------------------------------------------------- +-void create_online_files(void) { +- int i, j, k, l; // for loops +- int players; // count the number of players +- FILE *fp; // for the txt file +- FILE *fp2; // for the html file +- char temp[256]; // to prepare what we must display +- time_t time_server; // for number of seconds +- struct tm *datetime; // variable for time in structure ->tm_mday, ->tm_sec, ... +- int id[char_num]; +- +- if (online_display_option == 0) // we display nothing, so return +- return; ++static int create_online_files_sub(DBKey key, void* data, va_list va) ++{ ++ struct online_char_data *character; ++ int* players; ++ int *id; ++ int j,k,l; ++ character = (struct online_char_data*) data; ++ players = va_arg(va, int*); ++ id = va_arg(va, int*); + +- //char_log("Creation of online players files." RETCODE); ++ // check if map-server is online ++ if (character->server == -1 || character->char_id == -1) { //Character not currently online. ++ return -1; ++ } + +- // Get number of online players, id of each online players +- players = 0; +- // sort online characters. +- for(i = 0; i < char_num; i++) { +- if (online_chars[i] != -1) { +- id[players] = i; ++ j = character->server; ++ if (server_fd[j] < 0) { ++ server[j].users = 0; ++ if (kick_on_disconnect) ++ { ++ character->char_id = -1; ++ character->server = -1; ++ } ++ return -1; ++ } ++ // search position of character in char_dat and sort online characters. ++ for(j = 0; j < char_num; j++) { ++ if (char_dat[j].status.char_id != character->char_id) ++ continue; ++ id[*players] = j; + // use sorting option + switch (online_sorting_option) { + case 1: // by name (without case sensitive) +- { +- char *p_name = char_dat[i].name; //speed up sorting when there are a lot of players. But very rarely players have same name. +- for(j = 0; j < players; j++) +- if (stricmp(p_name, char_dat[id[j]].name) < 0 || ++ for(k = 0; k < *players; k++) ++ if (stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0 || + // if same name, we sort with case sensitive. +- (stricmp(p_name, char_dat[id[j]].name) == 0 && +- strcmp(p_name, char_dat[id[j]].name) < 0)) { +- for(k = players; k > j; k--) +- id[k] = id[k-1]; +- id[j] = i; // id[players] ++ (stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) == 0 && ++ strcmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) { ++ for(l = *players; l > k; l--) ++ id[l] = id[l-1]; ++ id[k] = j; // id[*players] + break; + } +- } + break; + case 2: // by zeny +- for(j = 0; j < players; j++) +- if (char_dat[i].zeny < char_dat[id[j]].zeny || ++ for(k = 0; k < *players; k++) ++ if (char_dat[j].status.zeny < char_dat[id[k]].status.zeny || + // if same number of zenys, we sort by name. +- (char_dat[i].zeny == char_dat[id[j]].zeny && +- stricmp(char_dat[i].name, char_dat[id[j]].name) < 0)) { +- for(k = players; k > j; k--) +- id[k] = id[k-1]; +- id[j] = i; // id[players] ++ (char_dat[j].status.zeny == char_dat[id[k]].status.zeny && ++ stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) { ++ for(l = *players; l > k; l--) ++ id[l] = id[l-1]; ++ id[k] = j; // id[*players] + break; + } + break; + case 3: // by base level +- for(j = 0; j < players; j++) +- if (char_dat[i].base_level < char_dat[id[j]].base_level || ++ for(k = 0; k < *players; k++) ++ if (char_dat[j].status.base_level < char_dat[id[k]].status.base_level || + // if same base level, we sort by base exp. +- (char_dat[i].base_level == char_dat[id[j]].base_level && +- char_dat[i].base_exp < char_dat[id[j]].base_exp)) { +- for(k = players; k > j; k--) +- id[k] = id[k-1]; +- id[j] = i; // id[players] ++ (char_dat[j].status.base_level == char_dat[id[k]].status.base_level && ++ char_dat[j].status.base_exp < char_dat[id[k]].status.base_exp)) { ++ for(l = *players; l > k; l--) ++ id[l] = id[l-1]; ++ id[k] = j; // id[*players] + break; + } + break; + case 4: // by job (and job level) +- for(j = 0; j < players; j++) +- if (char_dat[i].class < char_dat[id[j]].class || ++ for(k = 0; k < *players; k++) ++ if (char_dat[j].status.class_ < char_dat[id[k]].status.class_ || + // if same job, we sort by job level. +- (char_dat[i].class == char_dat[id[j]].class && +- char_dat[i].job_level < char_dat[id[j]].job_level) || ++ (char_dat[j].status.class_ == char_dat[id[k]].status.class_ && ++ char_dat[j].status.job_level < char_dat[id[k]].status.job_level) || + // if same job and job level, we sort by job exp. +- (char_dat[i].class == char_dat[id[j]].class && +- char_dat[i].job_level == char_dat[id[j]].job_level && +- char_dat[i].job_exp < char_dat[id[j]].job_exp)) { +- for(k = players; k > j; k--) +- id[k] = id[k-1]; +- id[j] = i; // id[players] ++ (char_dat[j].status.class_ == char_dat[id[k]].status.class_ && ++ char_dat[j].status.job_level == char_dat[id[k]].status.job_level && ++ char_dat[j].status.job_exp < char_dat[id[k]].status.job_exp)) { ++ for(l = *players; l > k; l--) ++ id[l] = id[l-1]; ++ id[k] = j; // id[*players] + break; + } + break; + case 5: // by location map name + { +- int cpm_result; // A lot of player maps are identical. So, test if done often twice. +- for(j = 0; j < players; j++) +- if ((cpm_result = strcmp(char_dat[i].last_point.map, char_dat[id[j]].last_point.map)) < 0 || // no map are identical and with upper cases (not use stricmp) ++ const char *map1, *map2; ++ map1 = mapindex_id2name(char_dat[j].status.last_point.map); ++ ++ for(k = 0; k < *players; k++) { ++ map2 = mapindex_id2name(char_dat[id[k]].status.last_point.map); ++ if (!map1 || !map2 || //Avoid sorting if either one failed to resolve. ++ stricmp(map1, map2) < 0 || + // if same map name, we sort by name. +- (cpm_result == 0 && +- stricmp(char_dat[i].name, char_dat[id[j]].name) < 0)) { +- for(k = players; k > j; k--) +- id[k] = id[k-1]; +- id[j] = i; // id[players] ++ (stricmp(map1, map2) == 0 && ++ stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) { ++ for(l = *players; l > k; l--) ++ id[l] = id[l-1]; ++ id[k] = j; // id[*players] + break; + } + } ++ } + break; + default: // 0 or invalid value: no sorting + break; + } +- players++; ++ (*players)++; ++ break; + } ++ return 0; + } ++//------------------------------------------------------------- ++// Function to create the online files (txt and html). by [Yor] ++//------------------------------------------------------------- ++void create_online_files(void) { ++ unsigned int k, j; // for loop with strlen comparing ++ int i, l; // for loops ++ int players; // count the number of players ++ FILE *fp; // for the txt file ++ FILE *fp2; // for the html file ++ char temp[256]; // to prepare what we must display ++ time_t time_server; // for number of seconds ++ struct tm *datetime; // variable for time in structure ->tm_mday, ->tm_sec, ... ++ int id[4096]; ++ ++ if (online_display_option == 0) // we display nothing, so return ++ return; ++ ++ // Get number of online players, id of each online players, and verify if a server is offline ++ players = 0; ++ online_char_db->foreach(online_char_db, create_online_files_sub, &players, &id); + + // write files + fp = fopen(online_txt_filename, "w"); +@@ -1080,8 +1418,9 @@ + fprintf(fp, "Online Players on %s (%s):\n", server_name, temp); + fprintf(fp, "\n"); + +- // If we display at least 1 player +- if (players > 0) { ++ for (i = 0; i < players; i++) { ++ // if it's the first player ++ if (i == 0) { + j = 0; // count the number of characters for the txt version and to set the separate line + fprintf(fp2, " \n"); + fprintf(fp2, " \n"); +@@ -1128,16 +1467,14 @@ + for (k = 0; k < j; k++) + fprintf(fp, "-"); + fprintf(fp, "\n"); +- +- // display each player. +- for (i = 0; i < players; i++) { ++ } ++ fprintf(fp2, " \n"); + // get id of the character (more speed) + j = id[i]; +- fprintf(fp2, " \n"); + // displaying the character name + if ((online_display_option & 1) || (online_display_option & 64)) { // without/with 'GM' display +- strcpy(temp, char_dat[j].name); +- l = isGM(char_dat[j].account_id); ++ strcpy(temp, char_dat[j].status.name); ++ l = isGM(char_dat[j].status.account_id); + if (online_display_option & 64) { + if (l >= online_gm_display_min_level) + fprintf(fp, "%-24s (GM) ", temp); +@@ -1149,7 +1486,7 @@ + fprintf(fp2, " \n", jobname, char_dat[j].base_level, char_dat[j].job_level); +- fprintf(fp, "%-18s %3d/%3d ", jobname, char_dat[j].base_level, char_dat[j].job_level); ++ fprintf(fp2, " \n", jobname, char_dat[j].status.base_level, char_dat[j].status.job_level); ++ fprintf(fp, "%-18s %3d/%3d ", jobname, char_dat[j].status.base_level, char_dat[j].status.job_level); + } else if (online_display_option & 2) { + fprintf(fp2, " \n", jobname); + fprintf(fp, "%-18s ", jobname); + } else if (online_display_option & 4) { +- fprintf(fp2, " \n", char_dat[j].base_level, char_dat[j].job_level); +- fprintf(fp, "%3d/%3d ", char_dat[j].base_level, char_dat[j].job_level); ++ fprintf(fp2, " \n", char_dat[j].status.base_level, char_dat[j].status.job_level); ++ fprintf(fp, "%3d/%3d ", char_dat[j].status.base_level, char_dat[j].status.job_level); + } + } + // displaying of the map + if (online_display_option & 24) { // 8 or 16 + // prepare map name +- memset(temp, 0, sizeof(temp)); +- strncpy(temp, char_dat[j].last_point.map, 16); +- if (strchr(temp, '.') != NULL) +- temp[strchr(temp, '.') - temp] = '\0'; // suppress the '.gat' ++ memcpy(temp, mapindex_id2name(char_dat[j].status.last_point.map), MAP_NAME_LENGTH); ++ temp[MAP_NAME_LENGTH] = '\0'; ++ if (strstr(temp, ".gat") != NULL) { ++ temp[strstr(temp, ".gat") - temp] = 0; // suppress the '.gat' ++ } + // write map name +- if (online_display_option & 16) { // map-name AND coordonates +- fprintf(fp2, " \n", temp, char_dat[j].last_point.x, char_dat[j].last_point.y); +- fprintf(fp, "%-12s (%3d,%3d) ", temp, char_dat[j].last_point.x, char_dat[j].last_point.y); ++ if (online_display_option & 16) { // map-name AND coordinates ++ fprintf(fp2, " \n", temp, char_dat[j].status.last_point.x, char_dat[j].status.last_point.y); ++ fprintf(fp, "%-12s (%3d,%3d) ", temp, char_dat[j].status.last_point.x, char_dat[j].status.last_point.y); + } else { + fprintf(fp2, " \n", temp); + fprintf(fp, "%-12s ", temp); + } + } +- // displaying number of zenys ++ // displaying nimber of zenys + if (online_display_option & 32) { + // write number of zenys +- if (char_dat[j].zeny == 0) { // if no zeny ++ if (char_dat[j].status.zeny == 0) { // if no zeny + fprintf(fp2, " \n"); + fprintf(fp, " no zeny "); + } else { +- fprintf(fp2, " \n", char_dat[j].zeny); +- fprintf(fp, "%13d z ", char_dat[j].zeny); ++ fprintf(fp2, " \n", char_dat[j].status.zeny); ++ fprintf(fp, "%13d z ", char_dat[j].status.zeny); + } + } + fprintf(fp, "\n"); + fprintf(fp2, " \n"); + } ++ // If we display at least 1 player ++ if (players > 0) { + fprintf(fp2, "
"); + if ((online_display_option & 64) && l >= online_gm_display_min_level) + fprintf(fp2, ""); +- for (k = 0; temp[k]; k++) { ++ for (k = 0; k < strlen(temp); k++) { + switch(temp[k]) { + case '<': // < + fprintf(fp2, "<"); +@@ -1168,48 +1505,51 @@ + } + // displaying of the job + if (online_display_option & 6) { +- char * jobname = job_name(char_dat[j].class); ++ char * jobname = job_name(char_dat[j].status.class_); + if ((online_display_option & 6) == 6) { +- fprintf(fp2, " %s %d/%d%s %d/%d%s%d/%d%d/%d%s (%d, %d)%s (%d, %d)%sno zeny%d z%d z
\n"); + fprintf(fp, "\n"); + } +@@ -1218,8 +1558,9 @@ + if (players == 0) { + fprintf(fp2, "

No user is online.

\n"); + fprintf(fp, "No user is online.\n"); +- // no display if only 1 player + } else if (players == 1) { ++ fprintf(fp2, "

%d user is online.

\n", players); ++ fprintf(fp, "%d user is online.\n", players); + } else { + fprintf(fp2, "

%d users are online.

\n", players); + fprintf(fp, "%d users are online.\n", players); +@@ -1254,15 +1595,17 @@ + int mmo_char_send006b(int fd, struct char_session_data *sd) { + int i, j, found_num; + struct mmo_charstatus *p; +-#ifdef NEW_006b ++//#ifdef NEW_006b + const int offset = 24; +-#else +- const int offset = 4; +-#endif ++//#else ++// const int offset = 4; ++//#endif ++ ++ set_char_online(0, 99,sd->account_id); + + found_num = 0; + for(i = 0; i < char_num; i++) { +- if (char_dat[i].account_id == sd->account_id) { ++ if (char_dat[i].status.account_id == sd->account_id) { + sd->found_char[found_num] = i; + found_num++; + if (found_num == 9) +@@ -1272,18 +1615,19 @@ + for(i = found_num; i < 9; i++) + sd->found_char[i] = -1; + ++ WFIFOHEAD(fd, offset + found_num * 106); + memset(WFIFOP(fd,0), 0, offset + found_num * 106); + WFIFOW(fd,0) = 0x6b; + WFIFOW(fd,2) = offset + found_num * 106; + + for(i = 0; i < found_num; i++) { +- p = &char_dat[sd->found_char[i]]; ++ p = &char_dat[sd->found_char[i]].status; + j = offset + (i * 106); // increase speed of code + + WFIFOL(fd,j) = p->char_id; +- WFIFOL(fd,j+4) = p->base_exp; ++ WFIFOL(fd,j+4) = p->base_exp>LONG_MAX?LONG_MAX:p->base_exp; + WFIFOL(fd,j+8) = p->zeny; +- WFIFOL(fd,j+12) = p->job_exp; ++ WFIFOL(fd,j+12) = p->job_exp>LONG_MAX?LONG_MAX:p->job_exp; + WFIFOL(fd,j+16) = p->job_level; + + WFIFOL(fd,j+20) = 0; +@@ -1299,9 +1643,16 @@ + WFIFOW(fd,j+46) = (p->sp > 0x7fff) ? 0x7fff : p->sp; + WFIFOW(fd,j+48) = (p->max_sp > 0x7fff) ? 0x7fff : p->max_sp; + WFIFOW(fd,j+50) = DEFAULT_WALK_SPEED; // p->speed; +- WFIFOW(fd,j+52) = p->class; ++ WFIFOW(fd,j+52) = p->class_; + WFIFOW(fd,j+54) = p->hair; +- WFIFOW(fd,j+56) = p->weapon; ++ ++ // pecopeco knights/crusaders crash fix ++ if (p->class_ == 13 || p->class_ == 21 || ++ p->class_ == 4014 || p->class_ == 4022 || ++ p->class_ == 4036 || p->class_ == 4044) ++ WFIFOW(fd,j+56) = 0; ++ else WFIFOW(fd,j+56) = p->weapon; ++ + WFIFOW(fd,j+58) = p->base_level; + WFIFOW(fd,j+60) = p->skill_point; + WFIFOW(fd,j+62) = p->head_bottom; +@@ -1311,7 +1662,7 @@ + WFIFOW(fd,j+70) = p->hair_color; + WFIFOW(fd,j+72) = p->clothes_color; + +- memcpy(WFIFOP(fd,j+74), p->name, 24); ++ memcpy(WFIFOP(fd,j+74), p->name, NAME_LENGTH); + + WFIFOB(fd,j+98) = (p->str > 255) ? 255 : p->str; + WFIFOB(fd,j+99) = (p->agi > 255) ? 255 : p->agi; +@@ -1327,34 +1678,20 @@ + return 0; + } + +-int set_account_reg2(int acc, int num, struct global_reg *reg) { +- int i, c; ++// 離婚(char削除時に使用) ++int char_divorce(struct mmo_charstatus *cs) { ++ if (cs == NULL) ++ return 0; + +- c = 0; ++ if (cs->partner_id > 0){ ++ int i, j; + for(i = 0; i < char_num; i++) { +- if (char_dat[i].account_id == acc) { +- memcpy(char_dat[i].account_reg2, reg, sizeof(char_dat[i].account_reg2)); +- char_dat[i].account_reg2_num = num; +- c++; +- } +- } +- return c; +-} +- +-// 離婚(char削除時に使用) +-int char_divorce(struct mmo_charstatus *cs) { +- if (cs == NULL) +- return 0; +- +- if (cs->partner_id > 0){ +- int i, j; +- for(i = 0; i < char_num; i++) { +- if (char_dat[i].char_id == cs->partner_id && char_dat[i].partner_id == cs->char_id) { ++ if (char_dat[i].status.char_id == cs->partner_id && char_dat[i].status.partner_id == cs->char_id) { + cs->partner_id = 0; +- char_dat[i].partner_id = 0; ++ char_dat[i].status.partner_id = 0; + for(j = 0; j < MAX_INVENTORY; j++) +- if (char_dat[i].inventory[j].nameid == WEDDING_RING_M || char_dat[i].inventory[j].nameid == WEDDING_RING_F) +- memset(&char_dat[i].inventory[j], 0, sizeof(char_dat[i].inventory[0])); ++ if (char_dat[i].status.inventory[j].nameid == WEDDING_RING_M || char_dat[i].status.inventory[j].nameid == WEDDING_RING_F) ++ memset(&char_dat[i].status.inventory[j], 0, sizeof(char_dat[i].status.inventory[0])); + if (cs->inventory[j].nameid == WEDDING_RING_M || cs->inventory[j].nameid == WEDDING_RING_F) + memset(&cs->inventory[j], 0, sizeof(cs->inventory[0])); + return 0; +@@ -1364,12 +1701,22 @@ + return 0; + } + ++int char_married(int pl1,int pl2) { ++ return (char_dat[pl1].status.char_id == char_dat[pl2].status.partner_id && char_dat[pl2].status.char_id == char_dat[pl1].status.partner_id); ++} ++ ++int char_child(int parent_id, int child_id) { ++ return (char_dat[parent_id].status.child == char_dat[child_id].status.char_id && ++ ((char_dat[parent_id].status.char_id == char_dat[child_id].status.father) || ++ (char_dat[parent_id].status.char_id == char_dat[child_id].status.mother))); ++} ++ + //------------------------------------------------------------ + // E-mail check: return 0 (not correct) or 1 (valid). by [Yor] + //------------------------------------------------------------ +-int e_mail_check(unsigned char *email) { ++int e_mail_check(char *email) { + char ch; +- unsigned char* last_arobas; ++ char* last_arobas; + + // athena limits + if (strlen(email) < 3 || strlen(email) > 39) +@@ -1412,7 +1759,7 @@ + + // disconnect player if online on char-server + for(i = 0; i < fd_max; i++) { +- if (session[i] && (sd = session[i]->session_data)) { ++ if (session[i] && (sd = (struct char_session_data*)session[i]->session_data)) { + if (sd->account_id == accound_id) { + session[i]->eof = 1; + return 1; +@@ -1432,20 +1779,20 @@ + inter_pet_delete(cs->pet_id); + for (j = 0; j < MAX_INVENTORY; j++) + if (cs->inventory[j].card[0] == (short)0xff00) +- inter_pet_delete(*((long *)(&cs->inventory[j].card[2]))); ++ inter_pet_delete(MakeDWord(cs->inventory[j].card[1],cs->inventory[j].card[2])); + for (j = 0; j < MAX_CART; j++) + if (cs->cart[j].card[0] == (short)0xff00) +- inter_pet_delete(*((long *)(&cs->cart[j].card[2]))); ++ inter_pet_delete( MakeDWord(cs->cart[j].card[1],cs->cart[j].card[2]) ); + // ギルド脱退 + if (cs->guild_id) + inter_guild_leave(cs->guild_id, cs->account_id, cs->char_id); + // パーティー脱退 + if (cs->party_id) +- inter_party_leave(cs->party_id, cs->account_id); ++ inter_party_leave(cs->party_id, cs->account_id, cs->char_id); + // 離婚 + if (cs->partner_id){ + // 離婚情報をmapに通知 +- char buf[10]; ++ unsigned char buf[10]; + WBUFW(buf,0) = 0x2b12; + WBUFL(buf,2) = cs->char_id; + WBUFL(buf,6) = cs->partner_id; +@@ -1459,22 +1806,24 @@ + int parse_tologin(int fd) { + int i; + struct char_session_data *sd; ++ RFIFOHEAD(fd); + + // only login-server can have an access to here. + // so, if it isn't the login-server, we disconnect the session (fd != login_fd). +- if (fd != login_fd || session[fd]->eof) { ++ if (fd != login_fd) ++ session[fd]->eof = 1; ++ if(session[fd]->eof) { + if (fd == login_fd) { +- printf("Char-server can't connect to login-server (connection #%d).\n", fd); ++ ShowWarning("Connection to login-server lost (connection #%d).\n", fd); + login_fd = -1; + } +- close(fd); +- delete_session(fd); ++ do_close(fd); + return 0; + } + +- sd = session[fd]->session_data; ++ sd = (struct char_session_data*)session[fd]->session_data; + +- while(RFIFOREST(fd) >= 2) { ++ while(RFIFOREST(fd) >= 2 && !session[fd]->eof) { + // printf("parse_tologin: connection #%d, packet: 0x%x (with being read: %d bytes).\n", fd, RFIFOW(fd,0), RFIFOREST(fd)); + + switch(RFIFOW(fd,0)) { +@@ -1483,19 +1832,21 @@ + return 0; + if (RFIFOB(fd,2)) { + // printf("connect login server error : %d\n", RFIFOB(fd,2)); +- printf("Can not connect to login-server.\n"); +- printf("The server communication passwords (default s1/p1) is probably invalid.\n"); +- printf("Also, please make sure your accounts file (default: accounts.txt) has those values present.\n"); +- printf("If you changed the communication passwords, change them back at map_athena.conf and char_athena.conf\n"); ++ ShowError("Can not connect to the login-server.\n"); ++ ShowError("The server communication passwords (default s1/p1) are probably invalid.\n"); ++ ShowInfo("Also, please make sure your accounts file (default: accounts.txt) has those values present.\n"); ++ ShowInfo("The communication passwords can be changed in map_athena.conf and char_athena.conf\n"); + exit(1); + } else { +- printf("Connected to login-server (connection #%d).\n", fd); ++ ShowStatus("Connected to login-server (connection #%d).\n", fd); ++ if (kick_on_disconnect) ++ set_all_offline(); + // if no map-server already connected, display a message... + for(i = 0; i < MAX_MAP_SERVERS; i++) +- if (server_fd[i] >= 0 && server[i].map[0][0]) // if map-server online and at least 1 map ++ if (server_fd[i] >= 0 && server[i].map[0]) // if map-server online and at least 1 map + break; + if (i == MAX_MAP_SERVERS) +- printf("Awaiting maps from map-server.\n"); ++ ShowStatus("Awaiting maps from map-server.\n"); + } + RFIFOSKIP(fd,3); + break; +@@ -1505,8 +1856,9 @@ + return 0; + // printf("parse_tologin 2713 : %d\n", RFIFOB(fd,6)); + for(i = 0; i < fd_max; i++) { +- if (session[i] && (sd = session[i]->session_data) && sd->account_id == RFIFOL(fd,2)) { ++ if (session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == RFIFOL(fd,2)) { + if (RFIFOB(fd,6) != 0) { ++ WFIFOHEAD(i, 3); + WFIFOW(i,0) = 0x6c; + WFIFOB(i,2) = 0x42; + WFIFOSET(i,3); +@@ -1521,9 +1873,14 @@ + sd->connect_until_time = (time_t)RFIFOL(fd,47); + // send characters to player + mmo_char_send006b(i, sd); ++ } else if(isGM(sd->account_id) >= gm_allow_level) { ++ sd->connect_until_time = (time_t)RFIFOL(fd,47); ++ // send characters to player ++ mmo_char_send006b(i, sd); + } else { + // refuse connection: too much online players + // printf("count_users(): %d < max_connect_use (%d) -> fail...\n", count_users(), max_connect_user); ++ WFIFOHEAD(fd, 3); + WFIFOW(i,0) = 0x6c; + WFIFOW(i,2) = 0; + WFIFOSET(i,3); +@@ -1539,7 +1896,7 @@ + if (RFIFOREST(fd) < 50) + return 0; + for(i = 0; i < fd_max; i++) { +- if (session[i] && (sd = session[i]->session_data)) { ++ if (session[i] && (sd = (struct char_session_data*)session[i]->session_data)) { + if (sd->account_id == RFIFOL(fd,2)) { + memcpy(sd->email, RFIFOP(fd,6), 40); + if (e_mail_check(sd->email) == 0) +@@ -1552,6 +1909,40 @@ + RFIFOSKIP(fd,50); + break; + ++ // login-server alive packet ++ case 0x2718: ++ if (RFIFOREST(fd) < 2) ++ return 0; ++ RFIFOSKIP(fd,2); ++ break; ++ ++ // Receiving authentification from Freya-type login server (to avoid char->login->char) ++ case 0x2719: ++ if (RFIFOREST(fd) < 18) ++ return 0; ++ // to conserv a maximum of authentification, search if account is already authentified and replace it ++ // that will reduce multiple connection too ++ for(i = 0; i < AUTH_FIFO_SIZE; i++) ++ if (auth_fifo[i].account_id == RFIFOL(fd,2)) ++ break; ++ // if not found, use next value ++ if (i == AUTH_FIFO_SIZE) { ++ if (auth_fifo_pos >= AUTH_FIFO_SIZE) ++ auth_fifo_pos = 0; ++ i = auth_fifo_pos; ++ auth_fifo_pos++; ++ } ++ auth_fifo[i].account_id = RFIFOL(fd,2); ++ auth_fifo[i].char_id = 0; ++ auth_fifo[i].login_id1 = RFIFOL(fd,6); ++ auth_fifo[i].login_id2 = RFIFOL(fd,10); ++ auth_fifo[i].delflag = 2; // 0: auth_fifo canceled/void, 2: auth_fifo received from login/map server in memory, 1: connection authentified ++ auth_fifo[i].char_pos = 0; ++ auth_fifo[i].connect_until_time = 0; // unlimited/unknown time by default (not display in map-server) ++ auth_fifo[i].ip = RFIFOL(fd,14); ++ RFIFOSKIP(fd,18); ++ break; ++ + case 0x2721: // gm reply + if (RFIFOREST(fd) < 10) + return 0; +@@ -1576,49 +1967,55 @@ + sex = RFIFOB(fd,6); + RFIFOSKIP(fd, 7); + if (acc > 0) { +- for (i = 0; i < char_num; i++) { +- if (char_dat[i].account_id == acc) { +- int jobclass = char_dat[i].class; +- char_dat[i].sex = sex; ++ for(i = 0; i < AUTH_FIFO_SIZE; i++) { ++ if (auth_fifo[i].account_id == acc) + auth_fifo[i].sex = sex; ++ } ++ for (i = 0; i < char_num; i++) { ++ if (char_dat[i].status.account_id == acc) { ++ int jobclass = char_dat[i].status.class_; ++ char_dat[i].status.sex = sex; + if (jobclass == 19 || jobclass == 20 || + jobclass == 4020 || jobclass == 4021 || + jobclass == 4042 || jobclass == 4043) { + // job modification + if (jobclass == 19 || jobclass == 20) { +- char_dat[i].class = (sex) ? 19 : 20; ++ char_dat[i].status.class_ = (sex) ? 19 : 20; + } else if (jobclass == 4020 || jobclass == 4021) { +- char_dat[i].class = (sex) ? 4020 : 4021; ++ char_dat[i].status.class_ = (sex) ? 4020 : 4021; + } else if (jobclass == 4042 || jobclass == 4043) { +- char_dat[i].class = (sex) ? 4042 : 4043; ++ char_dat[i].status.class_ = (sex) ? 4042 : 4043; + } + // remove specifical skills of classes 19, 4020 and 4042 + for(j = 315; j <= 322; j++) { +- if (char_dat[i].skill[j].id > 0 && !char_dat[i].skill[j].flag) { +- char_dat[i].skill_point += char_dat[i].skill[j].lv; +- char_dat[i].skill[j].id = 0; +- char_dat[i].skill[j].lv = 0; ++ if (char_dat[i].status.skill[j].id > 0 && !char_dat[i].status.skill[j].flag) { ++ char_dat[i].status.skill_point += char_dat[i].status.skill[j].lv; ++ char_dat[i].status.skill[j].id = 0; ++ char_dat[i].status.skill[j].lv = 0; + } + } + // remove specifical skills of classes 20, 4021 and 4043 + for(j = 323; j <= 330; j++) { +- if (char_dat[i].skill[j].id > 0 && !char_dat[i].skill[j].flag) { +- char_dat[i].skill_point += char_dat[i].skill[j].lv; +- char_dat[i].skill[j].id = 0; +- char_dat[i].skill[j].lv = 0; ++ if (char_dat[i].status.skill[j].id > 0 && !char_dat[i].status.skill[j].flag) { ++ char_dat[i].status.skill_point += char_dat[i].status.skill[j].lv; ++ char_dat[i].status.skill[j].id = 0; ++ char_dat[i].status.skill[j].lv = 0; + } + } + } + // to avoid any problem with equipment and invalid sex, equipment is unequiped. + for (j = 0; j < MAX_INVENTORY; j++) { +- if (char_dat[i].inventory[j].nameid && char_dat[i].inventory[j].equip) +- char_dat[i].inventory[j].equip = 0; ++ if (char_dat[i].status.inventory[j].nameid && char_dat[i].status.inventory[j].equip) ++ char_dat[i].status.inventory[j].equip = 0; + } +- char_dat[i].weapon = 0; +- char_dat[i].shield = 0; +- char_dat[i].head_top = 0; +- char_dat[i].head_mid = 0; +- char_dat[i].head_bottom = 0; ++ char_dat[i].status.weapon = 0; ++ char_dat[i].status.shield = 0; ++ char_dat[i].status.head_top = 0; ++ char_dat[i].status.head_mid = 0; ++ char_dat[i].status.head_bottom = 0; ++ ++ if (char_dat[i].status.guild_id) //If there is a guild, update the guild_member data [Skotlex] ++ inter_guild_sex_changed(char_dat[i].status.guild_id, acc, char_dat[i].status.char_id, sex); + } + } + // disconnect player if online on char-server +@@ -1644,14 +2041,14 @@ + if (i == MAX_MAP_SERVERS) + char_log("'ladmin': Receiving a message for broadcast, but no map-server is online." RETCODE); + else { +- char buf[128]; +- char message[RFIFOL(fd,4) + 1]; // +1 to add a null terminated if not exist in the packet ++ unsigned char buf[128]; ++ char message[4096]; // +1 to add a null terminated if not exist in the packet + int lp; + char *p; + memset(message, '\0', sizeof(message)); + memcpy(message, RFIFOP(fd,8), RFIFOL(fd,4)); + message[sizeof(message)-1] = '\0'; +- remove_control_chars(message); ++ remove_control_chars((unsigned char *)message); + // remove all first spaces + p = message; + while(p[0] == ' ') +@@ -1702,22 +2099,13 @@ + case 0x2729: + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; +- { +- struct global_reg reg[ACCOUNT_REG2_NUM]; +- unsigned char buf[4096]; +- int j, p, acc; +- acc = RFIFOL(fd,4); +- for (p = 8, j = 0; p < RFIFOW(fd,2) && j < ACCOUNT_REG2_NUM; p += 36, j++) { +- memcpy(reg[j].str, RFIFOP(fd,p), 32); +- reg[j].value = RFIFOL(fd,p+32); +- } +- set_account_reg2(acc, j, reg); +- // 同垢ログインを禁止していれば送る必要は無い ++ { //Receive account_reg2 registry, forward to map servers. ++ unsigned char buf[ACCOUNT_REG2_NUM*(256+32+2)+16]; + memcpy(buf, RFIFOP(fd,0), RFIFOW(fd,2)); +- WBUFW(buf,0) = 0x2b11; ++// WBUFW(buf,0) = 0x2b11; ++ WBUFW(buf,0) = 0x3804; //Map server can now receive all kinds of reg values with the same packet. [Skotlex] + mapif_sendall(buf, WBUFW(buf,2)); + RFIFOSKIP(fd,RFIFOW(fd,2)); +-// printf("char: save_account_reg_reply\n"); + } + break; + +@@ -1727,20 +2115,20 @@ + return 0; + // Deletion of all characters of the account + for(i = 0; i < char_num; i++) { +- if (char_dat[i].account_id == RFIFOL(fd,2)) { +- char_delete(&char_dat[i]); ++ if (char_dat[i].status.account_id == RFIFOL(fd,2)) { ++ char_delete(&char_dat[i].status); + if (i < char_num - 1) { +- memcpy(&char_dat[i], &char_dat[char_num-1], sizeof(struct mmo_charstatus)); ++ memcpy(&char_dat[i], &char_dat[char_num-1], sizeof(struct character_data)); + // if moved character owns to deleted account, check again it's character +- if (char_dat[i].account_id == RFIFOL(fd,2)) { ++ if (char_dat[i].status.account_id == RFIFOL(fd,2)) { + i--; + // Correct moved character reference in the character's owner by [Yor] + } else { + int j, k; + struct char_session_data *sd2; + for (j = 0; j < fd_max; j++) { +- if (session[j] && (sd2 = session[j]->session_data) && +- sd2->account_id == char_dat[char_num-1].account_id) { ++ if (session[j] && (sd2 = (struct char_session_data*)session[j]->session_data) && ++ sd2->account_id == char_dat[char_num-1].status.account_id) { + for (k = 0; k < 9; k++) { + if (sd2->found_char[k] == char_num-1) { + sd2->found_char[k] = i; +@@ -1792,10 +2180,10 @@ + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + { +- char buf[32000]; ++ unsigned char buf[32000]; + if (gm_account != NULL) +- free(gm_account); +- gm_account = calloc(sizeof(struct gm_account) * ((RFIFOW(fd,2) - 4) / 5), 1); ++ aFree(gm_account); ++ gm_account = (struct gm_account*)aCalloc(sizeof(struct gm_account) * ((RFIFOW(fd,2) - 4) / 5), 1); + GM_num = 0; + for (i = 4; i < RFIFOW(fd,2); i = i + 5) { + gm_account[GM_num].account_id = RFIFOL(fd,i); +@@ -1803,8 +2191,8 @@ + //printf("GM account: %d -> level %d\n", gm_account[GM_num].account_id, gm_account[GM_num].level); + GM_num++; + } +- printf("From login-server: receiving of %d GM accounts information.\n", GM_num); +- char_log("From login-server: receiving of %d GM accounts information." RETCODE, GM_num); ++ ShowStatus("From login-server: receiving information of %d GM accounts.\n", GM_num); ++ char_log("From login-server: receiving information of %d GM accounts." RETCODE, GM_num); + create_online_files(); // update online players files (perhaps some online players change of GM level) + // send new gm acccounts level to map-servers + memcpy(buf, RFIFOP(fd,0), RFIFOW(fd,2)); +@@ -1814,7 +2202,101 @@ + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + ++ // Receive GM accounts [Freya login server packet by Yor] ++ case 0x2733: ++ // add test here to remember that the login-server is Freya-type ++ // sprintf (login_server_type, "Freya"); ++ if (RFIFOREST(fd) < 7) ++ return 0; ++ { ++ unsigned char buf[32000]; ++ int new_level = 0; ++ for(i = 0; i < GM_num; i++) ++ if (gm_account[i].account_id == RFIFOL(fd,2)) { ++ if (gm_account[i].level != (int)RFIFOB(fd,6)) { ++ gm_account[i].level = (int)RFIFOB(fd,6); ++ new_level = 1; ++ } ++ break; ++ } ++ // if not found, add it ++ if (i == GM_num) { ++ // limited to 4000, because we send information to char-servers (more than 4000 GM accounts???) ++ // int (id) + int (level) = 8 bytes * 4000 = 32k (limit of packets in windows) ++ if (((int)RFIFOB(fd,6)) > 0 && GM_num < 4000) { ++ if (GM_num == 0) { ++ gm_account = (struct gm_account*)aMalloc(sizeof(struct gm_account)); ++ } else { ++ gm_account = (struct gm_account*)aRealloc(gm_account, sizeof(struct gm_account) * (GM_num + 1)); ++ } ++ gm_account[GM_num].account_id = RFIFOL(fd,2); ++ gm_account[GM_num].level = (int)RFIFOB(fd,6); ++ new_level = 1; ++ GM_num++; ++ if (GM_num >= 4000) { ++ ShowWarning("4000 GM accounts found. Next GM accounts are not readed.\n"); ++ char_log("***WARNING: 4000 GM accounts found. Next GM accounts are not readed." RETCODE); ++ } ++ } ++ } ++ if (new_level == 1) { ++ int len; ++ ShowStatus("From login-server: receiving GM account information (%d: level %d).\n", RFIFOL(fd,2), (int)RFIFOB(fd,6)); ++ char_log("From login-server: receiving a GM account information (%d: level %d)." RETCODE, RFIFOL(fd,2), (int)RFIFOB(fd,6)); ++ //create_online_files(); // not change online file for only 1 player (in next timer, that will be done ++ // send gm acccounts level to map-servers ++ len = 4; ++ WBUFW(buf,0) = 0x2b15; ++ ++ for(i = 0; i < GM_num; i++) { ++ WBUFL(buf, len) = gm_account[i].account_id; ++ WBUFB(buf, len+4) = (unsigned char)gm_account[i].level; ++ len += 5; ++ } ++ WBUFW(buf, 2) = len; ++ mapif_sendall(buf, len); ++ } ++ } ++ RFIFOSKIP(fd,7); ++ break; ++ ++ //Login server request to kick a character out. [Skotlex] ++ case 0x2734: ++ if (RFIFOREST(fd) < 6) ++ return 0; ++ { ++ struct online_char_data* character; ++ int aid = RFIFOL(fd,2); ++ if ((character = idb_get(online_char_db, aid)) != NULL) ++ { //Kick out this player. ++ if (character->server > -1) ++ { //Kick it from the map server it is on. ++ mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2); ++ if (!character->waiting_disconnect) ++ add_timer(gettick()+15000, chardb_waiting_disconnect, character->account_id, 0); ++ character->waiting_disconnect = 1; ++ } else { //Manual kick from char server. ++ struct char_session_data *tsd; ++ int i; ++ for(i = 0; i < fd_max; i++) { ++ if (session[i] && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == aid) ++ { ++ WFIFOHEAD(fd, 3); ++ WFIFOW(i,0) = 0x81; ++ WFIFOB(i,2) = 2; ++ WFIFOSET(i,3); ++ break; ++ } ++ } ++ if (i == fd_max) //Shouldn't happen, but just in case. ++ set_char_offline(99, aid); ++ } ++ } ++ RFIFOSKIP(fd,6); ++ } ++ break; + default: ++ ShowWarning("Unknown packet 0x%04x received from login-server, disconnecting.\n", RFIFOW(fd,0)); + session[fd]->eof = 1; + return 0; + } +@@ -1824,57 +2306,130 @@ + return 0; + } + +-//-------------------------------- +-// Map-server anti-freeze system +-//-------------------------------- +-int map_anti_freeze_system(int tid, unsigned int tick, int id, int data) { +- int i; ++int request_accreg2(int account_id, int char_id) { ++ if (login_fd > 0) { ++ WFIFOW(login_fd, 0) = 0x272e; ++ WFIFOL(login_fd, 2) = account_id; ++ WFIFOL(login_fd, 6) = char_id; ++ WFIFOSET(login_fd, 10); ++ return 1; ++ } ++ return 0; ++} + +- //printf("Entering in map_anti_freeze_system function to check freeze of servers.\n"); +- for(i = 0; i < MAX_MAP_SERVERS; i++) { +- if (server_fd[i] >= 0) {// if map-server is online +- //printf("map_anti_freeze_system: server #%d, flag: %d.\n", i, server_freezeflag[i]); +- if (server_freezeflag[i]-- < 1) { // Map-server anti-freeze system. Counter. 5 ok, 4...0 freezed +- printf("Map-server anti-freeze system: char-server #%d is freezed -> disconnection.\n", i); +- char_log("Map-server anti-freeze system: char-server #%d is freezed -> disconnection." RETCODE, +- i); +- session[server_fd[i]]->eof = 1; ++//Send packet forward to login-server for account saving ++int save_accreg2(unsigned char* buf, int len) { ++ if (login_fd > 0) { ++ WFIFOHEAD(login_fd, len+4); ++ memcpy(WFIFOP(login_fd,4), buf, len); ++ WFIFOW(login_fd,0) = 0x2728; ++ WFIFOW(login_fd,2) = len+4; ++ WFIFOSET(login_fd,len+4); ++ return 1; + } ++ return 0; + } ++ ++//Receive Registry information for a character. ++int char_parse_Registry(int account_id, int char_id, unsigned char *buf, int buf_len) { ++ int i,j,p,len; ++ for (i = 0; i < char_num; i++) { ++ if (char_dat[i].status.account_id == account_id && char_dat[i].status.char_id == char_id) ++ break; ++ } ++ if(i >= char_num) //Character not found? ++ return 1; ++ for(j=0,p=0;j= char_num){ //Character not found? Sent empty packet. ++ WFIFOW(fd,2)=13; ++ }else{ ++ for (p=13,j = 0; j < char_dat[i].global_num; j++) { ++ if (char_dat[i].global[j].str[0]) { ++ p+= sprintf(WFIFOP(fd,p), "%s", char_dat[i].global[j].str)+1; //We add 1 to consider the '\0' in place. ++ p+= sprintf(WFIFOP(fd,p), "%s", char_dat[i].global[j].value)+1; ++ } ++ } ++ WFIFOW(fd,2)=p; ++ } ++ WFIFOSET(fd,WFIFOW(fd,2)); + return 0; + } + ++int search_mapserver(unsigned short map, long ip, short port); ++ + int parse_frommap(int fd) { + int i, j; + int id; ++ RFIFOHEAD(fd); + + for(id = 0; id < MAX_MAP_SERVERS; id++) + if (server_fd[id] == fd) + break; +- if (id == MAX_MAP_SERVERS || session[fd]->eof) { ++ if(id==MAX_MAP_SERVERS) ++ session[fd]->eof=1; ++ if(session[fd]->eof){ + if (id < MAX_MAP_SERVERS) { +- printf("Map-server %d (session #%d) has disconnected.\n", id, fd); +- memset(&server[id], 0, sizeof(struct mmo_map_server)); ++ unsigned char buf[16384]; ++ ShowStatus("Map-server %d has disconnected.\n", id); ++ //Notify other map servers that this one is gone. [Skotlex] ++ WBUFW(buf,0) = 0x2b20; ++ WBUFL(buf,4) = server[id].ip; ++ WBUFW(buf,8) = server[id].port; ++ j = 0; ++ for(i = 0; i < MAX_MAP_PER_SERVER; i++) ++ if (server[id].map[i]) ++ WBUFW(buf,10+(j++)*4) = server[id].map[i]; ++ if (j > 0) { ++ WBUFW(buf,2) = j * 4 + 10; ++ mapif_sendallwos(fd, buf, WBUFW(buf,2)); ++ } + server_fd[id] = -1; +- for(j = 0; j < char_num; j++) +- if (online_chars[j] == fd) +- online_chars[j] = -1; +- create_online_files(); // update online players files (to remove all online players of this server) ++ online_char_db->foreach(online_char_db,char_db_setoffline,i); //Tag relevant chars as 'in disconnected' server. + } +- close(fd); +- delete_session(fd); ++ do_close(fd); ++ create_online_files(); + return 0; + } + +- while(RFIFOREST(fd) >= 2) { ++ while(RFIFOREST(fd) >= 2 && !session[fd]->eof) { + // printf("parse_frommap: connection #%d, packet: 0x%x (with being read: %d bytes).\n", fd, RFIFOW(fd,0), RFIFOREST(fd)); + + switch(RFIFOW(fd,0)) { ++ ++ // map-server alive packet ++ case 0x2718: ++ if (RFIFOREST(fd) < 2) ++ return 0; ++ RFIFOSKIP(fd,2); ++ break; ++ + // request from map-server to reload GM accounts. Transmission to login-server (by Yor) + case 0x2af7: + if (login_fd > 0) { // don't send request if no login-server ++ WFIFOHEAD(login_fd, 2); + WFIFOW(login_fd,0) = 0x2709; + WFIFOSET(login_fd, 2); + // printf("char : request from map-server to reload GM accounts -> login-server.\n"); +@@ -1888,36 +2443,41 @@ + return 0; + memset(server[id].map, 0, sizeof(server[id].map)); + j = 0; +- for(i = 4; i < RFIFOW(fd,2); i += 16) { +- memcpy(server[id].map[j], RFIFOP(fd,i), 16); +-// printf("set map %d.%d : %s\n", id, j, server[id].map[j]); ++ for(i = 4; i < RFIFOW(fd,2); i += 4) { ++ server[id].map[j] = RFIFOW(fd,i); + j++; + } + { + unsigned char *p = (unsigned char *)&server[id].ip; +- printf("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n", ++ ShowStatus("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n", + id, j, p[0], p[1], p[2], p[3], server[id].port); +- printf("Map-server %d loading complete.\n", id); ++ ShowStatus("Map-server %d loading complete.\n", id); + char_log("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d. Map-server %d loading complete." RETCODE, + id, j, p[0], p[1], p[2], p[3], server[id].port, id); ++ if (kick_on_disconnect) ++ set_all_offline(); ++ if (max_account_id != DEFAULT_MAX_ACCOUNT_ID || max_char_id != DEFAULT_MAX_CHAR_ID) ++ mapif_send_maxid(max_account_id, max_char_id); //Send the current max ids to the server to keep in sync [Skotlex] + } ++ WFIFOHEAD(fd, 3 + NAME_LENGTH); + WFIFOW(fd,0) = 0x2afb; + WFIFOB(fd,2) = 0; +- memcpy(WFIFOP(fd,3), wisp_server_name, 24); // name for wisp to player +- WFIFOSET(fd,27); ++ memcpy(WFIFOP(fd,3), wisp_server_name, NAME_LENGTH); // name for wisp to player ++ WFIFOSET(fd,3+NAME_LENGTH); ++ //WFIFOSET(fd,27); + { + unsigned char buf[16384]; + int x; + if (j == 0) { +- printf("WARNING: Map-Server %d have NO map.\n", id); ++ ShowWarning("Map-Server %d have NO map.\n", id); + char_log("WARNING: Map-Server %d have NO map." RETCODE, id); + // Transmitting maps information to the other map-servers + } else { + WBUFW(buf,0) = 0x2b04; +- WBUFW(buf,2) = j * 16 + 10; ++ WBUFW(buf,2) = j * 4 + 10; + WBUFL(buf,4) = server[id].ip; + WBUFW(buf,8) = server[id].port; +- memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 16); ++ memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 4); + mapif_sendallwos(fd, buf, WBUFW(buf,2)); + } + // Transmitting the maps of the other map-servers to the new map-server +@@ -1928,10 +2488,10 @@ + WFIFOW(fd,8) = server[x].port; + j = 0; + for(i = 0; i < MAX_MAP_PER_SERVER; i++) +- if (server[x].map[i][0]) +- memcpy(WFIFOP(fd,10+(j++)*16), server[x].map[i], 16); ++ if (server[x].map[i]) ++ WFIFOW(fd,10+(j++)*4) = server[x].map[i]; + if (j > 0) { +- WFIFOW(fd,2) = j * 16 + 10; ++ WFIFOW(fd,2) = j * 4 + 10; + WFIFOSET(fd,WFIFOW(fd,2)); + } + } +@@ -1940,83 +2500,95 @@ + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + +- // 認証要求 ++ //Packet command is now used for sc_data request. [Skotlex] + case 0x2afc: +- if (RFIFOREST(fd) < 22) ++ if (RFIFOREST(fd) < 10) + return 0; +- //printf("auth_fifo search: account: %d, char: %d, secure: %08x-%08x\n", RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10), RFIFOL(fd,14)); +- for(i = 0; i < AUTH_FIFO_SIZE; i++) { +- if (auth_fifo[i].account_id == RFIFOL(fd,2) && +- auth_fifo[i].char_id == RFIFOL(fd,6) && +- auth_fifo[i].login_id1 == RFIFOL(fd,10) && +-#if CMP_AUTHFIFO_LOGIN2 != 0 +- // here, it's the only area where it's possible that we doesn't know login_id2 (map-server asks just after 0x72 packet, that doesn't given the value) +- (auth_fifo[i].login_id2 == RFIFOL(fd,14) || RFIFOL(fd,14) == 0) && // relate to the versions higher than 18 ++ { ++#ifdef ENABLE_SC_SAVING ++ int aid, cid; ++ struct scdata *data; ++ aid = RFIFOL(fd,2); ++ cid = RFIFOL(fd,6); + #endif +- (!check_ip_flag || auth_fifo[i].ip == RFIFOL(fd,18)) && +- !auth_fifo[i].delflag) { +- auth_fifo[i].delflag = 1; +- WFIFOW(fd,0) = 0x2afd; +- WFIFOW(fd,2) = 16 + sizeof(struct mmo_charstatus); +- WFIFOL(fd,4) = RFIFOL(fd,2); +- WFIFOL(fd,8) = auth_fifo[i].login_id2; +- WFIFOL(fd,12) = (unsigned long)auth_fifo[i].connect_until_time; +- char_dat[auth_fifo[i].char_pos].sex = auth_fifo[i].sex; +- memcpy(WFIFOP(fd,16), &char_dat[auth_fifo[i].char_pos], sizeof(struct mmo_charstatus)); ++ RFIFOSKIP(fd, 10); ++#ifdef ENABLE_SC_SAVING ++ data = status_search_scdata(aid, cid); ++ if (data->count > 0) ++ { //Deliver status change data. ++ int i; ++ ++ WFIFOW(fd,0) = 0x2b1d; ++ WFIFOW(fd,2) = 14 + data->count*sizeof(struct status_change_data); ++ WFIFOL(fd,4) = aid; ++ WFIFOL(fd,8) = cid; ++ WFIFOW(fd,12) = data->count; ++ for (i = 0; i < data->count; i++) ++ memcpy(WFIFOP(fd,14+i*sizeof(struct status_change_data)), &data->data[i], sizeof(struct status_change_data)); + WFIFOSET(fd, WFIFOW(fd,2)); +- //printf("auth_fifo search success (auth #%d, account %d, character: %d).\n", i, RFIFOL(fd,2), RFIFOL(fd,6)); +- break; ++ status_delete_scdata(aid, cid); //Data sent, so it needs be discarded now. + } ++#endif ++ break; + } +- if (i == AUTH_FIFO_SIZE) { +- WFIFOW(fd,0) = 0x2afe; +- WFIFOL(fd,2) = RFIFOL(fd,2); +- WFIFOSET(fd,6); +- printf("auth_fifo search error! account %d not authentified.\n", RFIFOL(fd,2)); ++ ++ //set MAP user count ++ case 0x2afe: ++ if (RFIFOREST(fd) < 4) ++ return 0; ++ if (RFIFOW(fd,2) != server[id].users) { ++ server[id].users = RFIFOW(fd,2); ++ ShowInfo("User Count: %d (Server: %d)\n", server[id].users, id); + } +- RFIFOSKIP(fd,22); ++ RFIFOSKIP(fd, 4); + break; +- +- // MAPサーバー上のユーザー数受信 ++ //set MAP users + case 0x2aff: + if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + server[id].users = RFIFOW(fd,4); +- if(anti_freeze_enable) +- server_freezeflag[id] = 5; // Map anti-freeze system. Counter. 5 ok, 4...0 freezed +- // remove all previously online players of the server +- for(i = 0; i < char_num; i++) +- if (online_chars[i] == id) +- online_chars[i] = -1; +- // add online players in the list by [Yor] ++ // add online players in the list by [Yor], adapted to use dbs by [Skotlex] ++ j = 0; ++ online_char_db->foreach(online_char_db,char_db_setoffline,id); //Set all chars from this server as 'unknown' + for(i = 0; i < server[id].users; i++) { +- int char_id = RFIFOL(fd,6+i*4); +- for(j = 0; j < char_num; j++) +- if (char_dat[j].char_id == char_id) { +- online_chars[j] = id; +- //printf("%d\n", char_id); +- break; ++ int aid, cid; ++ struct online_char_data* character; ++ aid = RFIFOL(fd,6+i*8); ++ cid = RFIFOL(fd,6+i*8+4); ++ character = idb_ensure(online_char_db, aid, create_online_char_data); ++ if (online_check && character->server > -1 && character->server != id) ++ { ++ ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n", ++ character->account_id, character->char_id, character->server, id, aid, cid); ++ mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2); + } ++ character->char_id = cid; ++ character->server = id; + } + if (update_online < time(NULL)) { // Time is done + update_online = time(NULL) + 8; + create_online_files(); // only every 8 sec. (normally, 1 server send users every 5 sec.) Don't update every time, because that takes time, but only every 2 connection. + // it set to 8 sec because is more than 5 (sec) and if we have more than 1 map-server, informations can be received in shifted. + } +- RFIFOSKIP(fd,6+i*4); ++ //If any chars remain in -2, they will be cleaned in the cleanup timer. ++ RFIFOSKIP(fd,6+i*8); + break; + + // キャラデータ保存 ++ // Recieve character data from map-server + case 0x2b01: + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + for(i = 0; i < char_num; i++) { +- if (char_dat[i].account_id == RFIFOL(fd,4) && +- char_dat[i].char_id == RFIFOL(fd,8)) ++ if (char_dat[i].status.account_id == RFIFOL(fd,4) && ++ char_dat[i].status.char_id == RFIFOL(fd,8)) + break; + } + if (i != char_num) +- memcpy(&char_dat[i], RFIFOP(fd,12), sizeof(struct mmo_charstatus)); ++ memcpy(&char_dat[i].status, RFIFOP(fd,13), sizeof(struct mmo_charstatus)); ++ if (RFIFOB(fd,12)) { //Flag, set character offline. [Skotlex] ++ set_char_offline(RFIFOL(fd,8),RFIFOL(fd,4)); ++ } + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + +@@ -2026,7 +2598,6 @@ + return 0; + if (auth_fifo_pos >= AUTH_FIFO_SIZE) + auth_fifo_pos = 0; +- //printf("auth_fifo set (auth #%d) - account: %d, secure: %08x-%08x\n", auth_fifo_pos, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)); + auth_fifo[auth_fifo_pos].account_id = RFIFOL(fd,2); + auth_fifo[auth_fifo_pos].char_id = 0; + auth_fifo[auth_fifo_pos].login_id1 = RFIFOL(fd,6); +@@ -2043,35 +2614,58 @@ + RFIFOSKIP(fd,18); + break; + +- // マップサーバー間移動要求 ++ // request "change map server" + case 0x2b05: +- if (RFIFOREST(fd) < 49) ++ if (RFIFOREST(fd) < 35) + return 0; +- if (auth_fifo_pos >= AUTH_FIFO_SIZE) +- auth_fifo_pos = 0; +- WFIFOW(fd,0) = 0x2b06; +- memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 42); +- //printf("auth_fifo set (auth#%d) - account: %d, secure: 0x%08x-0x%08x\n", auth_fifo_pos, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)); +- auth_fifo[auth_fifo_pos].account_id = RFIFOL(fd,2); +- auth_fifo[auth_fifo_pos].char_id = RFIFOL(fd,14); +- auth_fifo[auth_fifo_pos].login_id1 = RFIFOL(fd,6); +- auth_fifo[auth_fifo_pos].login_id2 = RFIFOL(fd,10); +- auth_fifo[auth_fifo_pos].delflag = 0; +- auth_fifo[auth_fifo_pos].sex = RFIFOB(fd,44); +- auth_fifo[auth_fifo_pos].connect_until_time = 0; // unlimited/unknown time by default (not display in map-server) +- auth_fifo[auth_fifo_pos].ip = RFIFOL(fd,45); +- for(i = 0; i < char_num; i++) +- if (char_dat[i].account_id == RFIFOL(fd,2) && +- char_dat[i].char_id == RFIFOL(fd,14)) { +- auth_fifo[auth_fifo_pos].char_pos = i; +- auth_fifo_pos++; +- WFIFOL(fd,6) = 0; ++ { ++ unsigned short name; ++ int map_id, map_fd = -1, i; ++ struct online_char_data* data; ++ struct mmo_charstatus* char_data; ++ ++ name = RFIFOW(fd,18); ++ map_id = search_mapserver(name, RFIFOL(fd,24), RFIFOW(fd,28)); //Locate mapserver by ip and port. ++ if (map_id >= 0) ++ map_fd = server_fd[map_id]; ++ for(i = 0; i < char_num; i++) { ++ if (char_dat[i].status.account_id == RFIFOL(fd,2) && ++ char_dat[i].status.char_id == RFIFOL(fd,14)) + break; + } +- if (i == char_num) +- WFIFOW(fd,6) = 1; +- WFIFOSET(fd,44); +- RFIFOSKIP(fd,49); ++ char_data = i< char_num? &char_dat[i].status:NULL; ++ //Tell the new map server about this player using Kevin's new auth packet. [Skotlex] ++ if (map_fd>=0 && session[map_fd] && char_data) ++ { //Send the map server the auth of this player. ++ //Update the "last map" as this is where the player must be spawned on the new map server. ++ char_data->last_point.map = RFIFOW(fd,18); ++ char_data->last_point.x = RFIFOW(fd,20); ++ char_data->last_point.y = RFIFOW(fd,22); ++ ++ WFIFOW(map_fd,0) = 0x2afd; ++ WFIFOW(map_fd,2) = 20 + sizeof(struct mmo_charstatus); ++ WFIFOL(map_fd,4) = RFIFOL(fd, 2); //Account ID ++ WFIFOL(map_fd,8) = RFIFOL(fd, 6); //Login1 ++ WFIFOL(map_fd,16) = RFIFOL(fd,10); //Login2 ++ WFIFOL(map_fd,12) = (unsigned long)0; //TODO: connect_until_time, how do I figure it out right now? ++ memcpy(WFIFOP(map_fd,20), char_data, sizeof(struct mmo_charstatus)); ++ WFIFOSET(map_fd, WFIFOW(map_fd,2)); ++ data = idb_ensure(online_char_db, RFIFOL(fd, 2), create_online_char_data); ++ data->char_id = char_data->char_id; ++ data->server = map_id; //Update server where char is. ++ ++ //Reply with an ack. ++ WFIFOW(fd, 0) = 0x2b06; ++ memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28); ++ WFIFOSET(fd, 30); ++ } else { //Reply with nak ++ WFIFOW(fd, 0) = 0x2b06; ++ memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28); ++ WFIFOL(fd, 6) = 0; //Set login1 to 0. ++ WFIFOSET(fd, 30); ++ } ++ RFIFOSKIP(fd, 35); ++ } + break; + + // キャラ名検索 +@@ -2079,16 +2673,17 @@ + if (RFIFOREST(fd) < 6) + return 0; + for(i = 0; i < char_num; i++) { +- if (char_dat[i].char_id == RFIFOL(fd,2)) ++ if (char_dat[i].status.char_id == RFIFOL(fd,2)) + break; + } + WFIFOW(fd,0) = 0x2b09; + WFIFOL(fd,2) = RFIFOL(fd,2); + if (i != char_num) +- memcpy(WFIFOP(fd,6), char_dat[i].name, 24); ++ memcpy(WFIFOP(fd,6), char_dat[i].status.name, NAME_LENGTH); + else +- memcpy(WFIFOP(fd,6), unknown_char_name, 24); +- WFIFOSET(fd,30); ++ memcpy(WFIFOP(fd,6), unknown_char_name, NAME_LENGTH); ++ WFIFOSET(fd,6+NAME_LENGTH); ++ //WFIFOSET(fd,30); + RFIFOSKIP(fd,6); + break; + +@@ -2127,10 +2722,10 @@ + if (RFIFOREST(fd) < 44) + return 0; + { +- char character_name[24]; ++ char character_name[NAME_LENGTH]; + int acc = RFIFOL(fd,2); // account_id of who ask (-1 if nobody) +- memcpy(character_name, RFIFOP(fd,6), 24); +- character_name[sizeof(character_name) -1] = '\0'; ++ memcpy(character_name, RFIFOP(fd,6), NAME_LENGTH-1); ++ character_name[NAME_LENGTH -1] = '\0'; + // prepare answer + WFIFOW(fd,0) = 0x2b0f; // answer + WFIFOL(fd,2) = acc; // who want do operation +@@ -2138,14 +2733,15 @@ + // search character + i = search_character_index(character_name); + if (i >= 0) { +- memcpy(WFIFOP(fd,6), search_character_name(i), 24); // put correct name if found +- WFIFOW(fd,32) = 0; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline ++ memcpy(WFIFOP(fd,6), search_character_name(i), NAME_LENGTH); // put correct name if found ++ WFIFOW(fd,6+NAME_LENGTH) = 0; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline ++ //WFIFOW(fd,32) = 0; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + switch(RFIFOW(fd, 30)) { + case 1: // block +- if (acc == -1 || isGM(acc) >= isGM(char_dat[i].account_id)) { ++ if (acc == -1 || isGM(acc) >= isGM(char_dat[i].status.account_id)) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd,0) = 0x2724; +- WFIFOL(login_fd,2) = char_dat[i].account_id; // account value ++ WFIFOL(login_fd,2) = char_dat[i].status.account_id; // account value + WFIFOL(login_fd,6) = 5; // status of the account + WFIFOSET(login_fd, 10); + // printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 5); +@@ -2155,10 +2751,10 @@ + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 2: // ban +- if (acc == -1 || isGM(acc) >= isGM(char_dat[i].account_id)) { ++ if (acc == -1 || isGM(acc) >= isGM(char_dat[i].status.account_id)) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd, 0) = 0x2725; +- WFIFOL(login_fd, 2) = char_dat[i].account_id; // account value ++ WFIFOL(login_fd, 2) = char_dat[i].status.account_id; // account value + WFIFOW(login_fd, 6) = RFIFOW(fd,32); // year + WFIFOW(login_fd, 8) = RFIFOW(fd,34); // month + WFIFOW(login_fd,10) = RFIFOW(fd,36); // day +@@ -2174,10 +2770,10 @@ + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 3: // unblock +- if (acc == -1 || isGM(acc) >= isGM(char_dat[i].account_id)) { ++ if (acc == -1 || isGM(acc) >= isGM(char_dat[i].status.account_id)) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd,0) = 0x2724; +- WFIFOL(login_fd,2) = char_dat[i].account_id; // account value ++ WFIFOL(login_fd,2) = char_dat[i].status.account_id; // account value + WFIFOL(login_fd,6) = 0; // status of the account + WFIFOSET(login_fd, 10); + // printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 0); +@@ -2187,10 +2783,10 @@ + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 4: // unban +- if (acc == -1 || isGM(acc) >= isGM(char_dat[i].account_id)) { ++ if (acc == -1 || isGM(acc) >= isGM(char_dat[i].status.account_id)) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd, 0) = 0x272a; +- WFIFOL(login_fd, 2) = char_dat[i].account_id; // account value ++ WFIFOL(login_fd, 2) = char_dat[i].status.account_id; // account value + WFIFOSET(login_fd, 6); + // printf("char : status -> login: account %d, unban request\n", char_dat[i].account_id); + } else +@@ -2199,10 +2795,10 @@ + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 5: // changesex +- if (acc == -1 || isGM(acc) >= isGM(char_dat[i].account_id)) { ++ if (acc == -1 || isGM(acc) >= isGM(char_dat[i].status.account_id)) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd, 0) = 0x2727; +- WFIFOL(login_fd, 2) = char_dat[i].account_id; // account value ++ WFIFOL(login_fd, 2) = char_dat[i].status.account_id; // account value + WFIFOSET(login_fd, 6); + // printf("char : status -> login: account %d, change sex request\n", char_dat[i].account_id); + } else +@@ -2213,12 +2809,14 @@ + } + } else { + // character name not found +- memcpy(WFIFOP(fd,6), character_name, 24); +- WFIFOW(fd,32) = 1; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline ++ memcpy(WFIFOP(fd,6), character_name, NAME_LENGTH); ++ WFIFOW(fd,8+NAME_LENGTH) = 1; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline ++ //WFIFOW(fd,32) = 1; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } + // send answer if a player ask, not if the server ask + if (acc != -1) { +- WFIFOSET(fd, 34); ++ //WFIFOSET(fd, 34); ++ WFIFOSET(fd, 10+NAME_LENGTH); + } + RFIFOSKIP(fd, 44); + break; +@@ -2226,34 +2824,157 @@ + + // case 0x2b0f: not more used (available for futur usage) + +- // account_reg保存要求 +- case 0x2b10: +- if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) ++ //Packet 0x2b10 deprecated in favor of packet 0x3004 for registry saving. [Skotlex] ++ //case 0x2b10: ++ ++ // Recieve rates [Wizputer] ++ case 0x2b16: ++ if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,8)) ++ return 0; ++ // Txt doesn't need this packet, so just skip it ++ RFIFOSKIP(fd,RFIFOW(fd,8)); ++ break; ++ ++ // Character disconnected set online 0 [Wizputer] ++ case 0x2b17: ++ if (RFIFOREST(fd) < 6) ++ return 0; ++ //printf("Setting %d char offline\n",RFIFOL(fd,2)); ++ set_char_offline(RFIFOL(fd,2),RFIFOL(fd,6)); ++ RFIFOSKIP(fd,10); ++ break; ++ ++ // Reset all chars to offline [Wizputer] ++ case 0x2b18: ++ ShowNotice("Map server [%d] requested to set all characters offline.\n", id); ++ set_all_offline(); ++ RFIFOSKIP(fd,2); ++ break; ++ ++ // Character set online [Wizputer] ++ case 0x2b19: ++ if (RFIFOREST(fd) < 6) ++ return 0; ++ //printf("Setting %d char online\n",RFIFOL(fd,2)); ++ set_char_online(id, RFIFOL(fd,2),RFIFOL(fd,6)); ++ RFIFOSKIP(fd,10); ++ break; ++ ++ // Request sending of fame list ++ case 0x2b1a: ++ if (RFIFOREST(fd) < 2) + return 0; + { +- struct global_reg reg[ACCOUNT_REG2_NUM]; +- int p, acc; +- acc = RFIFOL(fd,4); +- for(p = 8, j = 0; p < RFIFOW(fd,2) && j < ACCOUNT_REG2_NUM; p += 36, j++) { +- memcpy(reg[j].str, RFIFOP(fd,p), 32); +- reg[j].value = RFIFOL(fd, p+32); ++ int i, j, k, len = 8; ++ unsigned char buf[32000]; ++ struct fame_list fame_item; ++ //struct mmo_charstatus *dat; ++ //dat = (struct mmo_charstatus *)aCalloc(char_num, sizeof(struct mmo_charstatus *)); ++ CREATE_BUFFER(id, int, char_num); ++ ++ // copy character list into buffer ++ //for (i = 0; i < char_num; i++) ++ // dat[i] = char_dat[i]; ++ // sort according to fame ++ // qsort(dat, char_num, sizeof(struct mmo_charstatus *), sort_fame); ++ ++ for(i = 0; i < char_num; i++) { ++ id[i] = i; ++ for(j = 0; j < i; j++) { ++ if (char_dat[i].status.fame > char_dat[id[j]].status.fame) { ++ for(k = i; k > j; k--) ++ id[k] = id[k-1]; ++ id[j] = i; // id[i] ++ break; + } +- set_account_reg2(acc, j, reg); +- // loginサーバーへ送る +- if (login_fd > 0) { // don't send request if no login-server +- WFIFOW(login_fd, 0) = 0x2728; +- memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0), RFIFOW(fd,2)); +- WFIFOSET(login_fd, WFIFOW(login_fd,2)); + } +- // ワールドへの同垢ログインがなければmapサーバーに送る必要はない +- //memcpy(buf, RFIFOP(fd,0), RFIFOW(fd,2)); +- //WBUFW(buf,0) = 0x2b11; +- //mapif_sendall(buf, WBUFW(buf,2)); ++ } ++ ++ // starting to send to map ++ WBUFW(buf,0) = 0x2b1b; ++ // add list for blacksmiths ++ for (i = 0, j = 0; i < char_num && j < fame_list_size_smith; i++) { ++ if (char_dat[id[i]].status.fame && (char_dat[id[i]].status.class_ == 10 || ++ char_dat[id[i]].status.class_ == 4011 || ++ char_dat[id[i]].status.class_ == 4033)) ++ { ++ fame_item.id = char_dat[id[i]].status.char_id; ++ fame_item.fame = char_dat[id[i]].status.fame; ++ strncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH); ++ ++ memcpy(WBUFP(buf, len), &fame_item, sizeof(struct fame_list)); ++ len += sizeof(struct fame_list); ++ j++; ++ } ++ } ++ // add blacksmith's block length ++ WBUFW(buf, 6) = len; ++ ++ // add list for alchemists ++ for (i = 0, j = 0; i < char_num && j < fame_list_size_chemist; i++) { ++ if (char_dat[id[i]].status.fame && (char_dat[id[i]].status.class_ == 18 || ++ char_dat[id[i]].status.class_ == 4019 || ++ char_dat[id[i]].status.class_ == 4041)) ++ { ++ fame_item.id = char_dat[id[i]].status.char_id; ++ fame_item.fame = char_dat[id[i]].status.fame; ++ strncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH); ++ ++ memcpy(WBUFP(buf, len), &fame_item, sizeof(struct fame_list)); ++ len += sizeof(struct fame_list); ++ j++; ++ } ++ } ++ // add alchemist's block length ++ WBUFW(buf, 4) = len; ++ ++ // adding list for taekwons ++ for (i = 0, j = 0; i < char_num && j < fame_list_size_taekwon; i++) { ++ if (char_dat[id[i]].status.fame && char_dat[id[i]].status.class_ == 4046) ++ { ++ fame_item.id = char_dat[id[i]].status.char_id; ++ fame_item.fame = char_dat[id[i]].status.fame; ++ strncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH); ++ ++ memcpy(WBUFP(buf, len), &fame_item, sizeof(struct fame_list)); ++ len += sizeof(struct fame_list); ++ j++; ++ } ++ } ++ // add total packet length ++ WBUFW(buf, 2) = len; ++ ++ // sending to all maps ++ mapif_sendall(buf, len); ++ // done! ++ //aFree(dat); ++ DELETE_BUFFER(id); ++ RFIFOSKIP(fd,2); ++ break; ++ } ++ //Request to save status change data. [Skotlex] ++ case 0x2b1c: ++ if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) ++ return 0; ++ { ++#ifdef ENABLE_SC_SAVING ++ int count, aid, cid, i; ++ struct scdata *data; ++ aid = RFIFOL(fd, 4); ++ cid = RFIFOL(fd, 8); ++ count = RFIFOW(fd, 12); ++ data = status_search_scdata(aid, cid); ++ if (data->count != count) ++ { ++ data->count = count; ++ data->data = aRealloc(data->data, count*sizeof(struct status_change_data)); ++ } ++ for (i = 0; i < count; i++) ++ memcpy (&data->data[i], RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data)); ++#endif + RFIFOSKIP(fd, RFIFOW(fd,2)); +-// printf("char: save_account_reg (from map)\n"); + break; + } +- + default: + // inter server処理に渡す + { +@@ -2264,7 +2985,7 @@ + return 0; + } + // inter server処理でもない場合は切断 +- printf("char: unknown packet 0x%04x (%d bytes to read in buffer)! (from map).\n", RFIFOW(fd,0), RFIFOREST(fd)); ++ ShowError("Unknown packet 0x%04x from map server, disconnecting.\n", RFIFOW(fd,0)); + session[fd]->eof = 1; + return 0; + } +@@ -2272,28 +2993,20 @@ + return 0; + } + +-int search_mapserver(char *map) { ++int search_mapserver(unsigned short map, long ip, short port) { + int i, j; +- char temp_map[16]; +- int temp_map_len; +- +-// printf("Searching the map-server for map '%s'... ", map); +- strncpy(temp_map, map, sizeof(temp_map)); +- temp_map[sizeof(temp_map)-1] = '\0'; +- if (strchr(temp_map, '.') != NULL) +- temp_map[strchr(temp_map, '.') - temp_map + 1] = '\0'; // suppress the '.gat', but conserve the '.' to be sure of the name of the map + +- temp_map_len = strlen(temp_map); + for(i = 0; i < MAX_MAP_SERVERS; i++) + if (server_fd[i] >= 0) +- for (j = 0; server[i].map[j][0]; j++) +- //printf("%s : %s = %d\n", server[i].map[j], map, strncmp(server[i].map[j], temp_map, temp_map_len)); +- if (strncmp(server[i].map[j], temp_map, temp_map_len) == 0) { +-// printf("found -> server #%d.\n", i); ++ for (j = 0; server[i].map[j]; j++) ++ if (server[i].map[j] == map) { ++ if (ip > 0 && server[i].ip != ip) ++ continue; ++ if (port > 0 && server[i].port != port) ++ continue; + return i; + } + +-// printf("not found.\n"); + return -1; + } + +@@ -2302,48 +3015,81 @@ + return inter_mapif_init(fd); + } + +-//----------------------------------------------------- +-// Test to know if an IP come from LAN or WAN. by [Yor] +-//----------------------------------------------------- +-int lan_ip_check(unsigned char *p){ ++//-------------------------------------------- ++// Test to know if an IP come from LAN or WAN. ++// Rewrote: Adnvanced subnet check [LuzZza] ++//-------------------------------------------- ++int lan_subnetcheck(long *p) { ++ + int i; +- int lancheck = 1; ++ unsigned char *sbn, *msk, *src = (unsigned char *)p; + +-// printf("lan_ip_check: to compare: %d.%d.%d.%d, network: %d.%d.%d.%d/%d.%d.%d.%d\n", +-// p[0], p[1], p[2], p[3], +-// subneti[0], subneti[1], subneti[2], subneti[3], +-// subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]); +- for(i = 0; i < 4; i++) { +- if ((subneti[i] & subnetmaski[i]) != (p[i] & subnetmaski[i])) { +- lancheck = 0; +- break; ++ for(i=0; iclient_addr.sin_addr; ++ long subnet_map_ip; ++ ++ RFIFOHEAD(fd); ++ ++ sd = (struct char_session_data*)session[fd]->session_data; + +- if (login_fd < 0 || session[fd]->eof) { // disconnect any player (already connected to char-server or coming back from map-server) if login-server is diconnected. ++ if (login_fd < 0) ++ session[fd]->eof = 1; ++ if(session[fd]->eof) { // disconnect any player (already connected to char-server or coming back from map-server) if login-server is diconnected. + if (fd == login_fd) + login_fd = -1; +- close(fd); +- delete_session(fd); ++ if (sd != NULL) ++ { ++ struct online_char_data* data = idb_get(online_char_db, sd->account_id); ++ if (!data || data->server== -1) //If it is not in any server, send it offline. [Skotlex] ++ set_char_offline(99,sd->account_id); ++ } ++ do_close(fd); + return 0; + } + +- sd = session[fd]->session_data; ++ while(RFIFOREST(fd) >= 2 && !session[fd]->eof) { ++ cmd = RFIFOW(fd,0); ++ // crc32のスキップ用 ++ if( sd==NULL && // 未ログインor管理パケット ++ RFIFOREST(fd)>=4 && // 最低バイト数制限 & 0x7530,0x7532管理パケ除去 ++ RFIFOREST(fd)<=21 && // 最大バイト数制限 & サーバーログイン除去 ++ cmd!=0x20b && // md5通知パケット除去 ++ (RFIFOREST(fd)<6 || RFIFOW(fd,4)==0x65) ){ // 次に何かパケットが来てるなら、接続でないとだめ ++ RFIFOSKIP(fd,4); ++ cmd = RFIFOW(fd,0); ++ ShowDebug("parse_char : %d crc32 skipped\n",fd); ++ if(RFIFOREST(fd)==0) ++ return 0; ++ } + +- while (RFIFOREST(fd) >= 2) { +-// if (RFIFOW(fd,0) < 30000) +-// printf("parse_char: connection #%d, packet: 0x%x (with being read: %d bytes).\n", fd, RFIFOW(fd,0), RFIFOREST(fd)); ++//For use in packets that depend on an sd being present [Skotlex] ++#define FIFOSD_CHECK(rest) { if(RFIFOREST(fd) < rest) return 0; if (sd==NULL) { RFIFOSKIP(fd,rest); return 0; } } + +- switch(RFIFOW(fd,0)) { ++ switch(cmd){ + case 0x20b: //20040622暗号化ragexe対応 + if (RFIFOREST(fd) < 19) + return 0; +@@ -2356,13 +3102,15 @@ + { + int GM_value; + if ((GM_value = isGM(RFIFOL(fd,2)))) +- printf("Account Logged On; Account ID: %d (GM level %d).\n", RFIFOL(fd,2), GM_value); ++ ShowInfo("Account Logged On; Account ID: %d (GM level %d).\n", RFIFOL(fd,2), GM_value); + else +- printf("Account Logged On; Account ID: %d.\n", RFIFOL(fd,2)); ++ ShowInfo("Account Logged On; Account ID: %d.\n", RFIFOL(fd,2)); + if (sd == NULL) { +- sd = session[fd]->session_data = calloc(sizeof(struct char_session_data), 1); +- memset(sd, 0, sizeof(struct char_session_data)); +- memcpy(sd->email, "no mail", 40); // put here a mail without '@' to refuse deletion if we don't receive the e-mail ++ sd = (struct char_session_data*)aCalloc(sizeof(struct char_session_data), 1); ++ session[fd]->session_data = sd; ++ ++// memset(sd, 0, sizeof(struct char_session_data)); aCalloc does this [Skotlex] ++ strncpy(sd->email, "no mail", 40); // put here a mail without '@' to refuse deletion if we don't receive the e-mail + sd->connect_until_time = 0; // unknow or illimited (not displaying on map-server) + } + sd->account_id = RFIFOL(fd,2); +@@ -2370,6 +3118,7 @@ + sd->login_id2 = RFIFOL(fd,10); + sd->sex = RFIFOB(fd,16); + // send back account_id ++ WFIFOHEAD(fd, 4); + WFIFOL(fd,0) = RFIFOL(fd,2); + WFIFOSET(fd,4); + // search authentification +@@ -2382,6 +3131,44 @@ + (!check_ip_flag || auth_fifo[i].ip == session[fd]->client_addr.sin_addr.s_addr) && + auth_fifo[i].delflag == 2) { + auth_fifo[i].delflag = 1; ++ ++ if (online_check) ++ { // check if character is not online already. [Skotlex] ++ struct online_char_data* character; ++ character = idb_get(online_char_db, sd->account_id); ++ ++ if (character) ++ { ++ if(character->server > -1) ++ { //Kick it from the map server it is on. ++ mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2); ++ if (!character->waiting_disconnect) ++ add_timer(gettick()+20000, chardb_waiting_disconnect, character->account_id, 0); ++ character->waiting_disconnect = 1; ++ /* Not a good idea because this would trigger when you do a char-change from the map server! [Skotlex] ++ } else { //Manual kick from char server. ++ struct char_session_data *tsd; ++ int i; ++ for(i = 0; i < fd_max; i++) { ++ if (session[i] && i!=fd && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == sd->account_id) ++ { ++ WFIFOW(i,0) = 0x81; ++ WFIFOB(i,2) = 2; ++ WFIFOSET(i,3); ++ break; ++ } ++ } ++ if (i == fd_max) //Shouldn't happen, but just in case. ++ set_char_offline(99, sd->account_id); ++ */ ++ WFIFOW(fd,0) = 0x81; ++ WFIFOB(fd,2) = 8; ++ WFIFOSET(fd,3); ++ break; ++ } ++ } ++ } ++ + if (max_connect_user == 0 || count_users() < max_connect_user) { + if (login_fd > 0) { // don't send request if no login-server + // request to login-server to obtain e-mail/time limit +@@ -2421,89 +3208,103 @@ + break; + + case 0x66: // キャラ選択 +- if (RFIFOREST(fd) < 3) +- return 0; ++ FIFOSD_CHECK(3); ++ { ++ int char_num = RFIFOB(fd,2); ++ struct mmo_charstatus *cd; ++ RFIFOSKIP(fd,3); + + // if we activated email creation and email is default email + if (email_creation != 0 && strcmp(sd->email, "a@a.com") == 0 && login_fd > 0) { // to modify an e-mail, login-server must be online ++ WFIFOHEAD(fd, 3); + WFIFOW(fd, 0) = 0x70; + WFIFOB(fd, 2) = 0; // 00 = Incorrect Email address + WFIFOSET(fd, 3); +- ++ break; ++ } + // otherwise, load the character +- } else { + for (ch = 0; ch < 9; ch++) +- if (sd->found_char[ch] >= 0 && char_dat[sd->found_char[ch]].char_num == RFIFOB(fd,2)) ++ if (sd->found_char[ch] >= 0 && char_dat[sd->found_char[ch]].status.char_num == char_num) + break; +- if (ch != 9) { ++ if (ch == 9) ++ { //Not found?? May be forged packet. ++ break; ++ } ++ cd = &char_dat[sd->found_char[ch]].status; + char_log("Character Selected, Account ID: %d, Character Slot: %d, Character Name: %s." RETCODE, +- sd->account_id, RFIFOB(fd,2), char_dat[sd->found_char[ch]].name); ++ sd->account_id, char_num, cd->name); ++ ++ cd->sex = sd->sex; ++ + // searching map server +- i = search_mapserver(char_dat[sd->found_char[ch]].last_point.map); ++ i = search_mapserver(cd->last_point.map,-1,-1); + // if map is not found, we check major cities + if (i < 0) { +- if ((i = search_mapserver("prontera.gat")) >= 0) { // check is done without 'gat'. +- memcpy(char_dat[sd->found_char[ch]].last_point.map, "prontera.gat", 16); +- char_dat[sd->found_char[ch]].last_point.x = 273; // savepoint coordonates +- char_dat[sd->found_char[ch]].last_point.y = 354; +- } else if ((i = search_mapserver("geffen.gat")) >= 0) { // check is done without 'gat'. +- memcpy(char_dat[sd->found_char[ch]].last_point.map, "geffen.gat", 16); +- char_dat[sd->found_char[ch]].last_point.x = 120; // savepoint coordonates +- char_dat[sd->found_char[ch]].last_point.y = 100; +- } else if ((i = search_mapserver("morocc.gat")) >= 0) { // check is done without 'gat'. +- memcpy(char_dat[sd->found_char[ch]].last_point.map, "morocc.gat", 16); +- char_dat[sd->found_char[ch]].last_point.x = 160; // savepoint coordonates +- char_dat[sd->found_char[ch]].last_point.y = 94; +- } else if ((i = search_mapserver("alberta.gat")) >= 0) { // check is done without 'gat'. +- memcpy(char_dat[sd->found_char[ch]].last_point.map, "alberta.gat", 16); +- char_dat[sd->found_char[ch]].last_point.x = 116; // savepoint coordonates +- char_dat[sd->found_char[ch]].last_point.y = 57; +- } else if ((i = search_mapserver("payon.gat")) >= 0) { // check is done without 'gat'. +- memcpy(char_dat[sd->found_char[ch]].last_point.map, "payon.gat", 16); +- char_dat[sd->found_char[ch]].last_point.x = 87; // savepoint coordonates +- char_dat[sd->found_char[ch]].last_point.y = 117; +- } else if ((i = search_mapserver("izlude.gat")) >= 0) { // check is done without 'gat'. +- memcpy(char_dat[sd->found_char[ch]].last_point.map, "izlude.gat", 16); +- char_dat[sd->found_char[ch]].last_point.x = 94; // savepoint coordonates +- char_dat[sd->found_char[ch]].last_point.y = 103; ++ unsigned short j; ++ ShowWarning("Unable to find map-server for '%s', resorting to sending to a major city.\n", mapindex_id2name(cd->last_point.map)); ++ if ((i = search_mapserver((j=mapindex_name2id(MAP_PRONTERA)),-1,-1)) >= 0) { ++ cd->last_point.map = j; ++ cd->last_point.x = 273; // savepoint coordinates ++ cd->last_point.y = 354; ++ } else if ((i = search_mapserver((j=mapindex_name2id(MAP_GEFFEN)),-1,-1)) >= 0) { ++ cd->last_point.map = j; ++ cd->last_point.x = 120; // savepoint coordinates ++ cd->last_point.y = 100; ++ } else if ((i = search_mapserver((j=mapindex_name2id(MAP_MORROC)),-1,-1)) >= 0) { ++ cd->last_point.map = j; ++ cd->last_point.x = 160; // savepoint coordinates ++ cd->last_point.y = 94; ++ } else if ((i = search_mapserver((j=mapindex_name2id(MAP_ALBERTA)),-1,-1)) >= 0) { ++ cd->last_point.map = j; ++ cd->last_point.x = 116; // savepoint coordinates ++ cd->last_point.y = 57; ++ } else if ((i = search_mapserver((j=mapindex_name2id(MAP_PAYON)),-1,-1)) >= 0) { ++ cd->last_point.map = j; ++ cd->last_point.x = 87; // savepoint coordinates ++ cd->last_point.y = 117; ++ } else if ((i = search_mapserver((j=mapindex_name2id(MAP_IZLUDE)),-1,-1)) >= 0) { ++ cd->last_point.map = j; ++ cd->last_point.x = 94; // savepoint coordinates ++ cd->last_point.y = 103; + } else { +- int j; + // get first online server (with a map) + i = 0; + for(j = 0; j < MAX_MAP_SERVERS; j++) +- if (server_fd[j] >= 0 && server[j].map[0][0]) { // change save point to one of map found on the server (the first) ++ if (server_fd[j] >= 0 && server[j].map[0]) { // change save point to one of map found on the server (the first) + i = j; +- memcpy(char_dat[sd->found_char[ch]].last_point.map, server[j].map[0], 16); +- printf("Map-server #%d found with a map: '%s'.\n", j, server[j].map[0]); +- // coordonates are unknown ++ cd->last_point.map = server[j].map[0]; ++ ShowInfo("Map-server #%d found with a map: '%s'.\n", j, mapindex_id2name(server[j].map[0])); ++ // coordinates are unknown + break; + } + // if no map-server is connected, we send: server closed + if (j == MAX_MAP_SERVERS) { ++ WFIFOHEAD(fd, 3); + WFIFOW(fd,0) = 0x81; +- WFIFOL(fd,2) = 1; // 01 = Server closed ++ WFIFOB(fd,2) = 1; // 01 = Server closed + WFIFOSET(fd,3); +- RFIFOSKIP(fd,3); + break; + } + } + } ++ WFIFOHEAD(fd, 28); + WFIFOW(fd,0) = 0x71; +- WFIFOL(fd,2) = char_dat[sd->found_char[ch]].char_id; +- memcpy(WFIFOP(fd,6), char_dat[sd->found_char[ch]].last_point.map, 16); +- printf("Character selection '%s' (account: %d, slot: %d).\n", char_dat[sd->found_char[ch]].name, sd->account_id, ch); +- printf("--Send IP of map-server. "); +- if (lan_ip_check(p)) +- WFIFOL(fd, 22) = inet_addr(lan_map_ip); ++ WFIFOL(fd,2) = cd->char_id; ++ memcpy(WFIFOP(fd,6), mapindex_id2name(cd->last_point.map), MAP_NAME_LENGTH); ++ ShowInfo("Character selection '%s' (account: %d, slot: %d).\n", cd->name, sd->account_id, ch); ++ ++ // Andvanced subnet check [LuzZza] ++ if((subnet_map_ip = lan_subnetcheck((long *)p))) ++ WFIFOL(fd,22) = subnet_map_ip; + else + WFIFOL(fd, 22) = server[i].ip; ++ + WFIFOW(fd,26) = server[i].port; + WFIFOSET(fd,28); + if (auth_fifo_pos >= AUTH_FIFO_SIZE) + auth_fifo_pos = 0; +- //printf("auth_fifo set #%d - account %d, char: %d, secure: %08x-%08x\n", auth_fifo_pos, sd->account_id, char_dat[sd->found_char[ch]].char_id, sd->login_id1, sd->login_id2); + auth_fifo[auth_fifo_pos].account_id = sd->account_id; +- auth_fifo[auth_fifo_pos].char_id = char_dat[sd->found_char[ch]].char_id; ++ auth_fifo[auth_fifo_pos].char_id = cd->char_id; + auth_fifo[auth_fifo_pos].login_id1 = sd->login_id1; + auth_fifo[auth_fifo_pos].login_id2 = sd->login_id2; + auth_fifo[auth_fifo_pos].delflag = 0; +@@ -2511,62 +3312,109 @@ + auth_fifo[auth_fifo_pos].sex = sd->sex; + auth_fifo[auth_fifo_pos].connect_until_time = sd->connect_until_time; + auth_fifo[auth_fifo_pos].ip = session[fd]->client_addr.sin_addr.s_addr; +- auth_fifo_pos++; ++ ++ //Send NEW auth packet [Kevin] ++ if ((map_fd = server_fd[i]) < 1 || session[map_fd] == NULL) ++ { //0 Should not be a valid server_fd [Skotlex] ++ ShowError("parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, i); ++ server_fd[i] = -1; ++ memset(&server[i], 0, sizeof(struct mmo_map_server)); ++ //Send server closed. ++ WFIFOW(fd,0) = 0x81; ++ WFIFOB(fd,2) = 1; // 01 = Server closed ++ WFIFOSET(fd,3); ++ break; + } ++ WFIFOW(map_fd,0) = 0x2afd; ++ WFIFOW(map_fd,2) = 20 + sizeof(struct mmo_charstatus); ++ WFIFOL(map_fd,4) = auth_fifo[auth_fifo_pos].account_id; ++ WFIFOL(map_fd,8) = auth_fifo[auth_fifo_pos].login_id1; ++ WFIFOL(map_fd,16) = auth_fifo[auth_fifo_pos].login_id2; ++ WFIFOL(map_fd,12) = (unsigned long)auth_fifo[auth_fifo_pos].connect_until_time; ++ memcpy(WFIFOP(map_fd,20), cd, sizeof(struct mmo_charstatus)); ++ WFIFOSET(map_fd, WFIFOW(map_fd,2)); ++ ++ set_char_online(i, cd->char_id, cd->account_id); ++ //Sets char online in the party and breaks even share if needed. ++ inter_party_logged(cd->party_id, cd->account_id, cd->char_id); ++ ++ auth_fifo_pos++; + } +- RFIFOSKIP(fd,3); + break; + + case 0x67: // 作成 +- if (RFIFOREST(fd) < 37) +- return 0; ++ FIFOSD_CHECK(37); ++ ++ if(char_new == 0) //turn character creation on/off [Kevin] ++ i = -2; ++ else + i = make_new_char(fd, RFIFOP(fd,2)); +- if (i < 0) { ++ ++ if(i == -1){ //added some better faile reporting to client on the txt version [Kevin] ++ //already exists ++ WFIFOHEAD(fd, 3); + WFIFOW(fd,0) = 0x6e; + WFIFOB(fd,2) = 0x00; + WFIFOSET(fd,3); + RFIFOSKIP(fd,37); + break; ++ }else if(i == -2){ ++ //denied ++ WFIFOHEAD(fd, 3); ++ WFIFOW(fd, 0) = 0x6e; ++ WFIFOB(fd, 2) = 0x02; ++ WFIFOSET(fd, 3); ++ RFIFOSKIP(fd, 37); ++ break; ++ }else if(i == -3){ ++ //underaged XD ++ WFIFOHEAD(fd, 3); ++ WFIFOW(fd, 0) = 0x6e; ++ WFIFOB(fd, 2) = 0x01; ++ WFIFOSET(fd, 3); ++ RFIFOSKIP(fd, 37); ++ break; + } + ++ WFIFOHEAD(fd, 108); + WFIFOW(fd,0) = 0x6d; + memset(WFIFOP(fd,2), 0, 106); + +- WFIFOL(fd,2) = char_dat[i].char_id; +- WFIFOL(fd,2+4) = char_dat[i].base_exp; +- WFIFOL(fd,2+8) = char_dat[i].zeny; +- WFIFOL(fd,2+12) = char_dat[i].job_exp; +- WFIFOL(fd,2+16) = char_dat[i].job_level; ++ WFIFOL(fd,2) = char_dat[i].status.char_id; ++ WFIFOL(fd,2+4) = char_dat[i].status.base_exp>LONG_MAX?LONG_MAX:char_dat[i].status.base_exp; ++ WFIFOL(fd,2+8) = char_dat[i].status.zeny; ++ WFIFOL(fd,2+12) = char_dat[i].status.job_exp>LONG_MAX?LONG_MAX:char_dat[i].status.job_exp; ++ WFIFOL(fd,2+16) = char_dat[i].status.job_level; + +- WFIFOL(fd,2+28) = char_dat[i].karma; +- WFIFOL(fd,2+32) = char_dat[i].manner; ++ WFIFOL(fd,2+28) = char_dat[i].status.karma; ++ WFIFOL(fd,2+32) = char_dat[i].status.manner; + + WFIFOW(fd,2+40) = 0x30; +- WFIFOW(fd,2+42) = (char_dat[i].hp > 0x7fff) ? 0x7fff : char_dat[i].hp; +- WFIFOW(fd,2+44) = (char_dat[i].max_hp > 0x7fff) ? 0x7fff : char_dat[i].max_hp; +- WFIFOW(fd,2+46) = (char_dat[i].sp > 0x7fff) ? 0x7fff : char_dat[i].sp; +- WFIFOW(fd,2+48) = (char_dat[i].max_sp > 0x7fff) ? 0x7fff : char_dat[i].max_sp; +- WFIFOW(fd,2+50) = DEFAULT_WALK_SPEED; // char_dat[i].speed; +- WFIFOW(fd,2+52) = char_dat[i].class; +- WFIFOW(fd,2+54) = char_dat[i].hair; +- +- WFIFOW(fd,2+58) = char_dat[i].base_level; +- WFIFOW(fd,2+60) = char_dat[i].skill_point; +- +- WFIFOW(fd,2+64) = char_dat[i].shield; +- WFIFOW(fd,2+66) = char_dat[i].head_top; +- WFIFOW(fd,2+68) = char_dat[i].head_mid; +- WFIFOW(fd,2+70) = char_dat[i].hair_color; +- +- memcpy(WFIFOP(fd,2+74), char_dat[i].name, 24); +- +- WFIFOB(fd,2+98) = (char_dat[i].str > 255) ? 255 : char_dat[i].str; +- WFIFOB(fd,2+99) = (char_dat[i].agi > 255) ? 255 : char_dat[i].agi; +- WFIFOB(fd,2+100) = (char_dat[i].vit > 255) ? 255 : char_dat[i].vit; +- WFIFOB(fd,2+101) = (char_dat[i].int_ > 255) ? 255 : char_dat[i].int_; +- WFIFOB(fd,2+102) = (char_dat[i].dex > 255) ? 255 : char_dat[i].dex; +- WFIFOB(fd,2+103) = (char_dat[i].luk > 255) ? 255 : char_dat[i].luk; +- WFIFOB(fd,2+104) = char_dat[i].char_num; ++ WFIFOW(fd,2+42) = (char_dat[i].status.hp > 0x7fff) ? 0x7fff : char_dat[i].status.hp; ++ WFIFOW(fd,2+44) = (char_dat[i].status.max_hp > 0x7fff) ? 0x7fff : char_dat[i].status.max_hp; ++ WFIFOW(fd,2+46) = (char_dat[i].status.sp > 0x7fff) ? 0x7fff : char_dat[i].status.sp; ++ WFIFOW(fd,2+48) = (char_dat[i].status.max_sp > 0x7fff) ? 0x7fff : char_dat[i].status.max_sp; ++ WFIFOW(fd,2+50) = DEFAULT_WALK_SPEED; // char_dat[i].status.speed; ++ WFIFOW(fd,2+52) = char_dat[i].status.class_; ++ WFIFOW(fd,2+54) = char_dat[i].status.hair; ++ ++ WFIFOW(fd,2+58) = char_dat[i].status.base_level; ++ WFIFOW(fd,2+60) = char_dat[i].status.skill_point; ++ ++ WFIFOW(fd,2+64) = char_dat[i].status.shield; ++ WFIFOW(fd,2+66) = char_dat[i].status.head_top; ++ WFIFOW(fd,2+68) = char_dat[i].status.head_mid; ++ WFIFOW(fd,2+70) = char_dat[i].status.hair_color; ++ ++ memcpy(WFIFOP(fd,2+74), char_dat[i].status.name, NAME_LENGTH); ++ ++ WFIFOB(fd,2+98) = (char_dat[i].status.str > 255) ? 255 : char_dat[i].status.str; ++ WFIFOB(fd,2+99) = (char_dat[i].status.agi > 255) ? 255 : char_dat[i].status.agi; ++ WFIFOB(fd,2+100) = (char_dat[i].status.vit > 255) ? 255 : char_dat[i].status.vit; ++ WFIFOB(fd,2+101) = (char_dat[i].status.int_ > 255) ? 255 : char_dat[i].status.int_; ++ WFIFOB(fd,2+102) = (char_dat[i].status.dex > 255) ? 255 : char_dat[i].status.dex; ++ WFIFOB(fd,2+103) = (char_dat[i].status.luk > 255) ? 255 : char_dat[i].status.luk; ++ WFIFOB(fd,2+104) = char_dat[i].status.char_num; + + WFIFOSET(fd,108); + RFIFOSKIP(fd,37); +@@ -2578,8 +3426,8 @@ + } + + case 0x68: // delete char //Yor's Fix +- if (RFIFOREST(fd) < 46) +- return 0; ++ FIFOSD_CHECK(46); ++ + memcpy(email, RFIFOP(fd,6), 40); + if (e_mail_check(email) == 0) + strncpy(email, "a@a.com", 40); // default e-mail +@@ -2596,7 +3444,7 @@ + } else { + // we change the packet to set it like selection. + for (i = 0; i < 9; i++) +- if (char_dat[sd->found_char[i]].char_id == RFIFOL(fd,2)) { ++ if (char_dat[sd->found_char[i]].status.char_id == RFIFOL(fd,2)) { + // we save new e-mail + memcpy(sd->email, email, 40); + // we send new e-mail to login-server ('online' login-server is checked before) +@@ -2608,7 +3456,7 @@ + RFIFOSKIP(fd,43); + // change value to put new packet (char selection) + RFIFOW(fd, 0) = 0x66; +- RFIFOB(fd, 2) = char_dat[sd->found_char[i]].char_num; ++ RFIFOB(fd, 2) = char_dat[sd->found_char[i]].status.char_num; + // not send packet, it's modify of actual packet + break; + } +@@ -2630,7 +3478,7 @@ + } else { + for (i = 0; i < 9; i++) { + struct mmo_charstatus *cs = NULL; +- if ((cs = &char_dat[sd->found_char[i]])->char_id == RFIFOL(fd,2)) { ++ if ((cs = &char_dat[sd->found_char[i]].status)->char_id == RFIFOL(fd,2)) { + char_delete(cs); // deletion process + + if (sd->found_char[i] != char_num - 1) { +@@ -2640,8 +3488,8 @@ + int j, k; + struct char_session_data *sd2; + for (j = 0; j < fd_max; j++) { +- if (session[j] && (sd2 = session[j]->session_data) && +- sd2->account_id == char_dat[char_num-1].account_id) { ++ if (session[j] && (sd2 = (struct char_session_data*)session[j]->session_data) && ++ sd2->account_id == char_dat[char_num-1].status.account_id) { + for (k = 0; k < 9; k++) { + if (sd2->found_char[k] == char_num-1) { + sd2->found_char[k] = sd->found_char[i]; +@@ -2682,7 +3530,7 @@ + if (server_fd[i] < 0) + break; + } +- if (i == MAX_MAP_SERVERS || strcmp(RFIFOP(fd,2), userid) || strcmp(RFIFOP(fd,26), passwd)){ ++ if (i == MAX_MAP_SERVERS || strcmp((char*)RFIFOP(fd,2), userid) || strcmp((char*)RFIFOP(fd,26), passwd)){ + WFIFOB(fd,2) = 3; + WFIFOSET(fd,3); + RFIFOSKIP(fd,60); +@@ -2691,8 +3539,6 @@ + WFIFOB(fd,2) = 0; + session[fd]->func_parse = parse_frommap; + server_fd[i] = fd; +- if(anti_freeze_enable) +- server_freezeflag[i] = 5; // Map anti-freeze system. Counter. 5 ok, 4...0 freezed + server[i].ip = RFIFOL(fd,54); + server[i].port = RFIFOW(fd,58); + server[i].users = 0; +@@ -2735,9 +3581,6 @@ + return 0; + + case 0x7532: // 接続の切断(defaultと処理は一緒だが明示的にするため) +- session[fd]->eof = 1; +- return 0; +- + default: + session[fd]->eof = 1; + return 0; +@@ -2747,6 +3590,30 @@ + return 0; + } + ++// Console Command Parser [Wizputer] ++int parse_console(char *buf) { ++ char *type,*command; ++ ++ type = (char *)aCalloc(64,1); ++ command = (char *)aCalloc(64,1); ++ ++// memset(type,0,64); ++// memset(command,0,64); ++ ++ ShowStatus("Console: %s\n",buf); ++ ++ if ( sscanf(buf, "%[^:]:%[^\n]", type , command ) < 2 ) ++ sscanf(buf,"%[^\n]",type); ++ ++ ShowDebug("Type of command: %s || Command: %s \n",type,command); ++ ++ if(buf) aFree(buf); ++ if(type) aFree(type); ++ if(command) aFree(command); ++ ++ return 0; ++} ++ + // 全てのMAPサーバーにデータ送信(送信したmap鯖の数を返す) + int mapif_sendall(unsigned char *buf, unsigned int len) { + int i, c; +@@ -2755,6 +3622,16 @@ + for(i = 0; i < MAX_MAP_SERVERS; i++) { + int fd; + if ((fd = server_fd[i]) >= 0) { ++ if (session[fd] == NULL) ++ { //Could this be the crash's source? [Skotlex] ++ ShowError("mapif_sendall: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", fd, i); ++ server_fd[i] = -1; ++ memset(&server[i], 0, sizeof(struct mmo_map_server)); ++ continue; ++ } ++ WFIFOHEAD(fd, len); ++ if (WFIFOSPACE(fd) < len) //Increase buffer size. ++ realloc_writefifo(fd, len); + memcpy(WFIFOP(fd,0), buf, len); + WFIFOSET(fd,len); + c++; +@@ -2771,6 +3648,9 @@ + for(i = 0; i < MAX_MAP_SERVERS; i++) { + int fd; + if ((fd = server_fd[i]) >= 0 && fd != sfd) { ++ WFIFOHEAD(fd, len); ++ if (WFIFOSPACE(fd) < len) //Increase buffer size. ++ realloc_writefifo(fd, len); + memcpy(WFIFOP(fd,0), buf, len); + WFIFOSET(fd, len); + c++; +@@ -2785,6 +3665,9 @@ + if (fd >= 0) { + for(i = 0; i < MAX_MAP_SERVERS; i++) { + if (fd == server_fd[i]) { ++ WFIFOHEAD(fd, len); ++ if (WFIFOSPACE(fd) < len) //Increase buffer size. ++ realloc_writefifo(fd, len); + memcpy(WFIFOP(fd,0), buf, len); + WFIFOSET(fd,len); + return 1; +@@ -2796,10 +3679,11 @@ + + int send_users_tologin(int tid, unsigned int tick, int id, int data) { + int users = count_users(); +- char buf[16]; ++ unsigned char buf[16]; + + if (login_fd > 0 && session[login_fd]) { + // send number of user to login server ++ WFIFOHEAD(login_fd, 6); + WFIFOW(login_fd,0) = 0x2714; + WFIFOL(login_fd,2) = users; + WFIFOSET(login_fd,6); +@@ -2812,17 +3696,52 @@ + return 0; + } + ++static int send_accounts_tologin_sub(DBKey key, void* data, va_list ap) { ++ struct online_char_data* character = (struct online_char_data*)data; ++ int *i = va_arg(ap, int*); ++ int count = va_arg(ap, int); ++ if ((*i) >= count) ++ return 0; //This is an error that shouldn't happen.... ++ if(character->server > -1) { ++ WFIFOHEAD(login_fd, 8+count*4); ++ WFIFOL(login_fd, 8+(*i)*4) =character->account_id; ++ (*i)++; ++ return 1; ++ } ++ return 0; ++} ++ ++int send_accounts_tologin(int tid, unsigned int tick, int id, int data) { ++ int users = count_users(), i=0; ++ ++ if (login_fd > 0 && session[login_fd]) { ++ // send account list to login server ++ WFIFOHEAD(login_fd, 8+users*4); ++ WFIFOW(login_fd,0) = 0x272d; ++ WFIFOL(login_fd,4) = users; ++ online_char_db->foreach(online_char_db, send_accounts_tologin_sub, &i); ++ WFIFOW(login_fd,2) = 8+ i*4; ++ if (i > 0) ++ WFIFOSET(login_fd,WFIFOW(login_fd,2)); ++ } ++ return 0; ++} ++ + int check_connect_login_server(int tid, unsigned int tick, int id, int data) { + if (login_fd <= 0 || session[login_fd] == NULL) { +- printf("Attempt to connect to login-server...\n"); ++ ShowInfo("Attempt to connect to login-server...\n"); + login_fd = make_connection(login_ip, login_port); ++ if (login_fd == -1) ++ { //Try again later... [Skotlex] ++ login_fd = 0; ++ return 0; ++ } + session[login_fd]->func_parse = parse_tologin; + realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK); ++ WFIFOHEAD(login_fd, 86); + WFIFOW(login_fd,0) = 0x2710; +- memset(WFIFOP(login_fd,2), 0, 24); +- memcpy(WFIFOP(login_fd,2), userid, strlen(userid) < 24 ? strlen(userid) : 24); +- memset(WFIFOP(login_fd,26), 0, 24); +- memcpy(WFIFOP(login_fd,26), passwd, strlen(passwd) < 24 ? strlen(passwd) : 24); ++ memcpy(WFIFOP(login_fd,2), userid, 24); ++ memcpy(WFIFOP(login_fd,26), passwd, 24); + WFIFOL(login_fd,50) = 0; + WFIFOL(login_fd,54) = char_ip; + WFIFOL(login_fd,58) = char_port; +@@ -2830,12 +3749,28 @@ + memcpy(WFIFOP(login_fd,60), server_name, strlen(server_name) < 20 ? strlen(server_name) : 20); + WFIFOW(login_fd,80) = 0; + WFIFOW(login_fd,82) = char_maintenance; +- WFIFOW(login_fd,84) = char_new; ++ ++ WFIFOW(login_fd,84) = char_new_display; //only display (New) if they want to [Kevin] ++ + WFIFOSET(login_fd,86); + } + return 0; + } + ++//------------------------------------------------ ++//Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't ++//replies/disconnect the player we tried to kick. [Skotlex] ++//------------------------------------------------ ++static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, int data) ++{ ++ struct online_char_data* character; ++ if ((character = idb_get(online_char_db, id)) != NULL && character->waiting_disconnect) ++ { //Mark it offline due to timeout. ++ set_char_offline(character->char_id, character->account_id); ++ } ++ return 0; ++} ++ + //---------------------------------------------------------- + // Return numerical value of a switch configuration by [Yor] + // on/off, english, fran軋is, deutsch, espaol +@@ -2849,92 +3784,56 @@ + return atoi(str); + } + +-//------------------------------------------- +-// Reading Lan Support configuration by [Yor] +-//------------------------------------------- +-int lan_config_read(const char *lancfgName) { +- int j; +- struct hostent * h = NULL; +- char line[1024], w1[1024], w2[1024]; +- FILE *fp; ++//---------------------------------- ++// Reading Lan Support configuration ++// Rewrote: Anvanced subnet check [LuzZza] ++//---------------------------------- ++int char_lan_config_read(const char *lancfgName) { + +- // set default configuration +- strncpy(lan_map_ip, "127.0.0.1", sizeof(lan_map_ip)); +- subneti[0] = 127; +- subneti[1] = 0; +- subneti[2] = 0; +- subneti[3] = 1; +- for(j = 0; j < 4; j++) +- subnetmaski[j] = 255; +- +- fp = fopen(lancfgName, "r"); ++ FILE *fp; ++ int line_num = 0; ++ char line[1024], w1[64], w2[64], w3[64], w4[64], w5[64]; + +- if (fp == NULL) { +- printf("LAN support configuration file not found: %s\n", lancfgName); ++ if((fp = fopen(lancfgName, "r")) == NULL) { ++ ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName); + return 1; + } + +- printf ("---start reading of Lan Support configuration...\n"); ++ ShowInfo("Reading the configuration file %s...\n", lancfgName); + + while(fgets(line, sizeof(line)-1, fp)) { +- if (line[0] == '/' && line[1] == '/') ++ ++ line_num++; ++ if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n') + continue; + + line[sizeof(line)-1] = '\0'; +- if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2) +- continue; ++ if(sscanf(line,"%[^:]: %[^/]/%[^:]:%[^:]:%[^\r\n]", w1, w2, w3, w4, w5) != 5) { + +- remove_control_chars(w1); +- remove_control_chars(w2); +- if (strcmpi(w1, "lan_map_ip") == 0) { // Read map-server Lan IP Address +- h = gethostbyname(w2); +- if (h != NULL) { +- sprintf(lan_map_ip, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); +- } else { +- strncpy(lan_map_ip, w2, sizeof(lan_map_ip)); +- lan_map_ip[sizeof(lan_map_ip)-1] = 0; +- } +- printf("LAN IP of map-server: %s.\n", lan_map_ip); +- } else if (strcmpi(w1, "subnet") == 0) { // Read Subnetwork +- for(j = 0; j < 4; j++) +- subneti[j] = 0; +- h = gethostbyname(w2); +- if (h != NULL) { +- for(j = 0; j < 4; j++) +- subneti[j] = (unsigned char)h->h_addr[j]; +- } else { +- sscanf(w2, "%d.%d.%d.%d", &subneti[0], &subneti[1], &subneti[2], &subneti[3]); +- } +- printf("Sub-network of the map-server: %d.%d.%d.%d.\n", subneti[0], subneti[1], subneti[2], subneti[3]); +- } else if (strcmpi(w1, "subnetmask") == 0){ // Read Subnetwork Mask +- for(j = 0; j < 4; j++) +- subnetmaski[j] = 255; +- h = gethostbyname(w2); +- if (h != NULL) { +- for(j = 0; j < 4; j++) +- subnetmaski[j] = (unsigned char)h->h_addr[j]; +- } else { +- sscanf(w2, "%d.%d.%d.%d", &subnetmaski[0], &subnetmaski[1], &subnetmaski[2], &subnetmaski[3]); +- } +- printf("Sub-network mask of the map-server: %d.%d.%d.%d.\n", subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]); +- } ++ ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num); ++ continue; + } +- fclose(fp); + +- // sub-network check of the map-server +- { +- unsigned int a0, a1, a2, a3; +- unsigned char p[4]; +- sscanf(lan_map_ip, "%d.%d.%d.%d", &a0, &a1, &a2, &a3); +- p[0] = a0; p[1] = a1; p[2] = a2; p[3] = a3; +- printf("LAN test of LAN IP of the map-server: "); +- if (lan_ip_check(p) == 0) { +- printf("\033[1;31m***ERROR: LAN IP of the map-server doesn't belong to the specified Sub-network.\033[0m\n"); +- } ++ remove_control_chars((unsigned char *)w1); ++ remove_control_chars((unsigned char *)w2); ++ remove_control_chars((unsigned char *)w3); ++ remove_control_chars((unsigned char *)w4); ++ remove_control_chars((unsigned char *)w5); ++ ++ if(strcmpi(w1, "subnet") == 0) { ++ ++ subnet[subnet_count].subnet = inet_addr(w2); ++ subnet[subnet_count].mask = inet_addr(w3); ++ subnet[subnet_count].char_ip = inet_addr(w4); ++ subnet[subnet_count].map_ip = inet_addr(w5); ++ ++ subnet_count++; + } + +- printf("---End reading of Lan Support configuration...\n"); ++ ShowStatus("Information about %d subnetworks readen.\n", subnet_count); ++ } + ++ fclose(fp); + return 0; + } + +@@ -2944,10 +3843,11 @@ + FILE *fp = fopen(cfgName, "r"); + + if (fp == NULL) { +- printf("Configuration file not found: %s.\n", cfgName); ++ ShowFatalError("Configuration file not found: %s.\n", cfgName); + exit(1); + } + ++ ShowInfo("Reading configuration file %s...\n", cfgName); + while(fgets(line, sizeof(line)-1, fp)) { + if (line[0] == '/' && line[1] == '/') + continue; +@@ -2956,86 +3856,122 @@ + if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2) + continue; + +- remove_control_chars(w1); +- remove_control_chars(w2); +- if (strcmpi(w1, "userid") == 0) { +- memcpy(userid, w2, 24); ++ remove_control_chars((unsigned char *)w1); ++ remove_control_chars((unsigned char *)w2); ++ if(strcmpi(w1,"timestamp_format") == 0) { ++ strncpy(timestamp_format, w2, 20); ++ } else if(strcmpi(w1,"console_silent")==0){ ++ msg_silent = 0; //To always allow the next line to show up. ++ ShowInfo("Console Silent Setting: %d\n", atoi(w2)); ++ msg_silent = atoi(w2); ++ } else if (strcmpi(w1, "userid") == 0) { ++ strncpy(userid, w2, 24); + } else if (strcmpi(w1, "passwd") == 0) { +- memcpy(passwd, w2, 24); ++ strncpy(passwd, w2, 24); + } else if (strcmpi(w1, "server_name") == 0) { + memcpy(server_name, w2, sizeof(server_name)); + server_name[sizeof(server_name) - 1] = '\0'; +- printf("%s server has been intialized\n", w2); ++ ShowStatus("%s server has been initialized\n", w2); + } else if (strcmpi(w1, "wisp_server_name") == 0) { + if (strlen(w2) >= 4) { + memcpy(wisp_server_name, w2, sizeof(wisp_server_name)); + wisp_server_name[sizeof(wisp_server_name) - 1] = '\0'; + } + } else if (strcmpi(w1, "login_ip") == 0) { ++ login_ip_set_ = 1; + h = gethostbyname(w2); + if (h != NULL) { +- printf("Login server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); ++ ShowStatus("Login server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + sprintf(login_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } else + memcpy(login_ip_str, w2, 16); + } else if (strcmpi(w1, "login_port") == 0) { + login_port = atoi(w2); + } else if (strcmpi(w1, "char_ip") == 0) { ++ char_ip_set_ = 1; + h = gethostbyname(w2); + if (h != NULL) { +- printf("Character server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); ++ ShowStatus("Character server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + sprintf(char_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } else + memcpy(char_ip_str, w2, 16); ++ } else if (strcmpi(w1, "bind_ip") == 0) { ++ bind_ip_set_ = 1; ++ h = gethostbyname(w2); ++ if (h != NULL) { ++ ShowStatus("Character server binding IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); ++ sprintf(bind_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); ++ } else ++ memcpy(bind_ip_str, w2, 16); + } else if (strcmpi(w1, "char_port") == 0) { + char_port = atoi(w2); + } else if (strcmpi(w1, "char_maintenance") == 0) { + char_maintenance = atoi(w2); + } else if (strcmpi(w1, "char_new") == 0) { + char_new = atoi(w2); ++ } else if (strcmpi(w1, "char_new_display") == 0) { ++ char_new_display = atoi(w2); + } else if (strcmpi(w1, "email_creation") == 0) { + email_creation = config_switch(w2); + } else if (strcmpi(w1, "char_txt") == 0) { + strcpy(char_txt, w2); ++ } else if (strcmpi(w1, "scdata_txt") == 0) { //By Skotlex ++ strcpy(scdata_txt, w2); + } else if (strcmpi(w1, "backup_txt") == 0) { //By zanetheinsane + strcpy(backup_txt, w2); ++ } else if (strcmpi(w1, "friends_txt") == 0) { //By davidsiaw ++ strcpy(friends_txt, w2); + } else if (strcmpi(w1, "backup_txt_flag") == 0) { // The backup_txt file was created because char deletion bug existed. Now it's finish and that take a lot of time to create a second file when there are a lot of characters. By [Yor] + backup_txt_flag = config_switch(w2); + } else if (strcmpi(w1, "max_connect_user") == 0) { + max_connect_user = atoi(w2); + if (max_connect_user < 0) + max_connect_user = 0; // unlimited online players ++ } else if(strcmpi(w1, "gm_allow_level") == 0) { ++ gm_allow_level = atoi(w2); ++ if(gm_allow_level < 0) ++ gm_allow_level = 99; + } else if (strcmpi(w1, "check_ip_flag") == 0) { + check_ip_flag = config_switch(w2); ++ } else if (strcmpi(w1, "online_check") == 0) { ++ online_check = config_switch(w2); + } else if (strcmpi(w1, "autosave_time") == 0) { + autosave_interval = atoi(w2)*1000; + if (autosave_interval <= 0) + autosave_interval = DEFAULT_AUTOSAVE_INTERVAL; ++ } else if (strcmpi(w1, "save_log") == 0) { ++ save_log = config_switch(w2); + } else if (strcmpi(w1, "start_point") == 0) { + char map[32]; + int x, y; + if (sscanf(w2, "%[^,],%d,%d", map, &x, &y) < 3) + continue; + if (strstr(map, ".gat") != NULL) { // Verify at least if '.gat' is in the map name +- memcpy(start_point.map, map, 16); ++ start_point.map = mapindex_name2id(map); ++ if (!start_point.map) { ++ ShowError("Specified start_point %s not found in map-index cache.\n", map); ++ start_point.map = 0; ++ } + start_point.x = x; + start_point.y = y; + } ++ } else if(strcmpi(w1,"log_char")==0) { //log char or not [devil] ++ log_char = atoi(w2); + } else if (strcmpi(w1, "start_zeny") == 0) { + start_zeny = atoi(w2); + if (start_zeny < 0) + start_zeny = 0; + } else if (strcmpi(w1, "start_weapon") == 0) { +- start_zeny = atoi(w2); ++ start_weapon = atoi(w2); + if (start_weapon < 0) + start_weapon = 0; + } else if (strcmpi(w1, "start_armor") == 0) { +- start_zeny = atoi(w2); ++ start_armor = atoi(w2); + if (start_armor < 0) + start_armor = 0; + } else if (strcmpi(w1, "unknown_char_name") == 0) { + strcpy(unknown_char_name, w2); +- unknown_char_name[24] = 0; ++ unknown_char_name[NAME_LENGTH-1] = '\0'; + } else if (strcmpi(w1, "char_log_filename") == 0) { + strcpy(char_log_filename, w2); + } else if (strcmpi(w1, "name_ignoring_case") == 0) { +@@ -3061,52 +3997,130 @@ + online_refresh_html = atoi(w2); + if (online_refresh_html < 1) + online_refresh_html = 1; +- } else if(strcmpi(w1,"anti_freeze_enable")==0){ +- anti_freeze_enable = config_switch(w2); +- } else if (strcmpi(w1, "anti_freeze_interval") == 0) { +- ANTI_FREEZE_INTERVAL = atoi(w2); +- if (ANTI_FREEZE_INTERVAL < 5) +- ANTI_FREEZE_INTERVAL = 5; // minimum 5 seconds ++ } else if(strcmpi(w1,"db_path")==0) { ++ strcpy(db_path,w2); ++ } else if (strcmpi(w1, "console") == 0) { ++ if(strcmpi(w2,"on") == 0 || strcmpi(w2,"yes") == 0 ) ++ console = 1; ++ } else if (strcmpi(w1, "fame_list_alchemist") == 0) { ++ fame_list_size_chemist = atoi(w2); ++ if (fame_list_size_chemist > MAX_FAME_LIST) { ++ ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST); ++ fame_list_size_chemist = MAX_FAME_LIST; ++ } ++ } else if (strcmpi(w1, "fame_list_blacksmith") == 0) { ++ fame_list_size_smith = atoi(w2); ++ if (fame_list_size_smith > MAX_FAME_LIST) { ++ ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST); ++ fame_list_size_smith = MAX_FAME_LIST; ++ } ++ } else if (strcmpi(w1, "fame_list_taekwon") == 0) { ++ fame_list_size_taekwon = atoi(w2); ++ if (fame_list_size_taekwon > MAX_FAME_LIST) { ++ ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST); ++ fame_list_size_taekwon = MAX_FAME_LIST; ++ } + } else if (strcmpi(w1, "import") == 0) { + char_config_read(w2); + } + } + fclose(fp); + ++ ShowInfo("done reading %s.\n", cfgName); + return 0; + } + ++int chardb_final(int key, void* data, va_list va) ++{ ++ aFree(data); ++ return 0; ++} + void do_final(void) { +- int i; +- ++ ShowStatus("Terminating server.\n"); + // write online players files with no player +- for(i = 0; i < char_num; i++) +- online_chars[i] = -1; ++ online_char_db->clear(online_char_db, NULL); //clean the db... + create_online_files(); +- free(online_chars); ++ online_char_db->destroy(online_char_db, NULL); //dispose the db... + + mmo_char_sync(); + inter_save(); ++ set_all_offline(); + +- if (gm_account != NULL) +- free(gm_account); ++ if(gm_account) aFree(gm_account); ++ if(char_dat) aFree(char_dat); + +- free(char_dat); + delete_session(login_fd); + delete_session(char_fd); + ++#ifdef ENABLE_SC_SAVING ++ status_final(); ++#endif ++ inter_final(); ++ mapindex_final(); ++ + char_log("----End of char-server (normal end with closing of all files)." RETCODE); + } + ++void set_server_type(void) ++{ ++ SERVER_TYPE = ATHENA_SERVER_CHAR; ++} ++ ++static int online_data_cleanup_sub(DBKey key, void *data, va_list ap) ++{ ++ struct online_char_data *character= (struct online_char_data*)data; ++ if (character->server == -2) //Unknown server.. set them offline ++ set_char_offline(character->char_id, character->account_id); ++ if (character->server < 0) ++ //Free data from players that have not been online for a while. ++ db_remove(online_char_db, key); ++ return 0; ++} ++ ++static int online_data_cleanup(int tid, unsigned int tick, int id, int data) ++{ ++ online_char_db->foreach(online_char_db, online_data_cleanup_sub); ++ return 0; ++} ++ + int do_init(int argc, char **argv) { + int i; + ++ mapindex_init(); //Needed here for the start-point reading. ++ start_point.map = mapindex_name2id("new_1-1.gat"); ++ char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]); ++ char_lan_config_read((argc > 3) ? argv[3] : LOGIN_LAN_CONF_NAME); ++ ++ if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) { ++ ShowError("Using the default user/password s1/p1 is NOT RECOMMENDED.\n"); ++ ShowNotice("Please edit your save/account.txt file to create a proper inter-server user/password (gender 'S')\n"); ++ ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n"); ++ } ++ + // a newline in the log... + char_log(""); ++ // moved behind char_config_read in case we changed the filename [celest] + char_log("The char-server starting..." RETCODE); + +- char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]); +- lan_config_read((argc > 1) ? argv[1] : LOGIN_LAN_CONF_NAME); ++ if ((naddr_ != 0) && (login_ip_set_ == 0 || char_ip_set_ == 0)) { ++ // The char server should know what IP address it is running on ++ // - MouseJstr ++ int localaddr = ntohl(addr_[0]); ++ unsigned char *ptr = (unsigned char *) &localaddr; ++ char buf[16]; ++ sprintf(buf, "%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]);; ++ if (naddr_ != 1) ++ ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", buf); ++ else ++ ShowStatus("Defaulting to %s as our IP address\n", buf); ++ if (login_ip_set_ == 0) ++ strcpy(login_ip_str, buf); ++ if (char_ip_set_ == 0) ++ strcpy(char_ip_str, buf); ++ ++ if (ptr[0] == 192 && ptr[1] == 168) ++ ShowWarning("Firewall detected.. edit lan_support.conf and char_athena.conf\n"); ++ } + + login_ip = inet_addr(login_ip_str); + char_ip = inet_addr(char_ip_str); +@@ -3116,34 +4130,45 @@ + server_fd[i] = -1; + } + +- mmo_char_init(); ++ online_char_db = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int)); + ++ mmo_char_init(); ++#ifdef ENABLE_SC_SAVING ++ status_init(); ++#endif + update_online = time(NULL); + create_online_files(); // update online players files at start of the server + + inter_init((argc > 2) ? argv[2] : inter_cfgName); // inter server 初期化 + +- set_termfunc(do_final); + set_defaultparse(parse_char); + +- char_fd = make_listen_port(char_port); ++ if (bind_ip_set_) ++ char_fd = make_listen_bind(inet_addr(bind_ip_str),char_port); ++ else ++ char_fd = make_listen_bind(INADDR_ANY,char_port); + + add_timer_func_list(check_connect_login_server, "check_connect_login_server"); + add_timer_func_list(send_users_tologin, "send_users_tologin"); ++ add_timer_func_list(send_accounts_tologin, "send_accounts_tologin"); + add_timer_func_list(mmo_char_sync_timer, "mmo_char_sync_timer"); ++ add_timer_func_list(chardb_waiting_disconnect, "chardb_waiting_disconnect"); ++ add_timer_func_list(online_data_cleanup, "online_data_cleanup"); + +- i = add_timer_interval(gettick() + 1000, check_connect_login_server, 0, 0, 10 * 1000); +- i = add_timer_interval(gettick() + 1000, send_users_tologin, 0, 0, 5 * 1000); +- i = add_timer_interval(gettick() + autosave_interval, mmo_char_sync_timer, 0, 0, autosave_interval); +- +- if(anti_freeze_enable > 0) { +- add_timer_func_list(map_anti_freeze_system, "map_anti_freeze_system"); +- i = add_timer_interval(gettick() + 1000, map_anti_freeze_system, 0, 0, ANTI_FREEZE_INTERVAL * 1000); // checks every X seconds user specifies ++ add_timer_interval(gettick() + 600*1000, online_data_cleanup, 0, 0, 600 * 1000); ++ add_timer_interval(gettick() + 1000, check_connect_login_server, 0, 0, 10 * 1000); ++ add_timer_interval(gettick() + 1000, send_users_tologin, 0, 0, 5 * 1000); ++ add_timer_interval(gettick() + 3600*1000, send_accounts_tologin, 0, 0, 3600*1000); //Sync online accounts every hour ++ add_timer_interval(gettick() + autosave_interval, mmo_char_sync_timer, 0, 0, autosave_interval); ++ ++ if(console) { ++ set_defaultconsoleparse(parse_console); ++ start_console(); + } + + char_log("The char-server is ready (Server is listening on the port %d)." RETCODE, char_port); + +- printf("The char-server is \033[1;32mready\033[0m (Server is listening on the port %d).\n\n", char_port); ++ ShowStatus("The char-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %d).\n\n", char_port); + + return 0; + } diff --git a/misc/src/char/int_guild.c b/misc/src/char/int_guild.c new file mode 100644 index 0000000..665e017 --- /dev/null +++ b/misc/src/char/int_guild.c @@ -0,0 +1,1441 @@ +// $Id: int_guild.c,v 1.2 2004/09/25 19:36:53 Akitasha Exp $ +#include "inter.h" +#include "int_guild.h" +#include "int_storage.h" +#include "mmo.h" +#include "char.h" +#include "socket.h" +#include "db.h" +#include "lock.h" + +#include +#include +#include + +char guild_txt[1024] = "save/guild.txt"; +char castle_txt[1024] = "save/castle.txt"; + +static struct dbt *guild_db; +static struct dbt *castle_db; + +static int guild_newid = 10000; + +static int guild_exp[100]; + +int mapif_parse_GuildLeave(int fd, int guild_id, int account_id, int char_id, int flag, const char *mes); +int mapif_guild_broken(int guild_id, int flag); +int guild_check_empty(struct guild *g); +int guild_calcinfo(struct guild *g); +int mapif_guild_basicinfochanged(int guild_id, int type, const void *data, int len); +int mapif_guild_info(int fd, struct guild *g); +int guild_break_sub(void *key, void *data, va_list ap); + +// ギルドデータの文字列への変換 +int inter_guild_tostr(char *str, struct guild *g) { + int i, c, len; + + // 基本データ + len = sprintf(str, "%d\t%s\t%s\t%d,%d,%d,%d,%d\t%s#\t%s#\t", + g->guild_id, g->name, g->master, + g->guild_lv, g->max_member, g->exp, g->skill_point, g->castle_id, + g->mes1, g->mes2); + // メンバー + for(i = 0; i < g->max_member; i++) { + struct guild_member *m = &g->member[i]; + len += sprintf(str + len, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\t%s\t", + m->account_id, m->char_id, + m->hair, m->hair_color, m->gender, + m->class, m->lv, m->exp, m->exp_payper, m->position, + ((m->account_id > 0) ? m->name : "-")); + } + // 役職 + for(i = 0; i < MAX_GUILDPOSITION; i++) { + struct guild_position *p = &g->position[i]; + len += sprintf(str + len, "%d,%d\t%s#\t", p->mode, p->exp_mode, p->name); + } + // エンブレム + len += sprintf(str + len, "%d,%d,", g->emblem_len, g->emblem_id); + for(i = 0; i < g->emblem_len; i++) { + len += sprintf(str + len, "%02x", (unsigned char)(g->emblem_data[i])); + } + len += sprintf(str + len, "$\t"); + // 同盟リスト + c = 0; + for(i = 0; i < MAX_GUILDALLIANCE; i++) + if (g->alliance[i].guild_id > 0) + c++; + len += sprintf(str + len, "%d\t", c); + for(i = 0; i < MAX_GUILDALLIANCE; i++) { + struct guild_alliance *a = &g->alliance[i]; + if (a->guild_id > 0) + len += sprintf(str + len, "%d,%d\t%s\t", a->guild_id, a->opposition, a->name); + } + // 追放リスト + c = 0; + for(i = 0; i < MAX_GUILDEXPLUSION; i++) + if (g->explusion[i].account_id > 0) + c++; + len += sprintf(str + len, "%d\t", c); + for(i = 0; i < MAX_GUILDEXPLUSION; i++) { + struct guild_explusion *e = &g->explusion[i]; + if (e->account_id > 0) + len += sprintf(str + len, "%d,%d,%d,%d\t%s\t%s\t%s#\t", + e->account_id, e->rsv1, e->rsv2, e->rsv3, + e->name, e->acc, e->mes ); + } + // ギルドスキル + for(i = 0; i < MAX_GUILDSKILL; i++) { + len += sprintf(str + len, "%d,%d ", g->skill[i].id, g->skill[i].lv); + } + len += sprintf(str + len, "\t"); + + return 0; +} + +// ギルドデータの文字列からの変換 +int inter_guild_fromstr(char *str, struct guild *g) { + int i, j, c; + int tmp_int[16]; + char tmp_str[4][256]; + char tmp_str2[4096]; + char *pstr; + + // 基本データ + memset(g, 0, sizeof(struct guild)); + if (sscanf(str, "%d\t%[^\t]\t%[^\t]\t%d,%d,%d,%d,%d\t%[^\t]\t%[^\t]\t", &tmp_int[0], + tmp_str[0], tmp_str[1], + &tmp_int[1], &tmp_int[2], &tmp_int[3], &tmp_int[4], &tmp_int[5], + tmp_str[2], tmp_str[3]) < 8) + return 1; + + g->guild_id = tmp_int[0]; + g->guild_lv = tmp_int[1]; + g->max_member = tmp_int[2]; + g->exp = tmp_int[3]; + g->skill_point = tmp_int[4]; + g->castle_id = tmp_int[5]; + memcpy(g->name, tmp_str[0], 24); + memcpy(g->master, tmp_str[1], 24); + memcpy(g->mes1, tmp_str[2], 60); + memcpy(g->mes2, tmp_str[3], 120); + g->mes1[strlen(g->mes1)-1] = 0; + g->mes2[strlen(g->mes2)-1] = 0; + + for(j = 0; j < 6 && str != NULL; j++) // 位置スキップ + str = strchr(str + 1, '\t'); +// printf("GuildBaseInfo OK\n"); + + // メンバー + for(i = 0; i < g->max_member; i++) { + struct guild_member *m = &g->member[i]; + if (sscanf(str + 1, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\t%[^\t]\t", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], &tmp_int[4], + &tmp_int[5], &tmp_int[6], &tmp_int[7], &tmp_int[8], &tmp_int[9], + tmp_str[0]) < 11) + return 1; + m->account_id = tmp_int[0]; + m->char_id = tmp_int[1]; + m->hair = tmp_int[2]; + m->hair_color = tmp_int[3]; + m->gender = tmp_int[4]; + m->class = tmp_int[5]; + m->lv = tmp_int[6]; + m->exp = tmp_int[7]; + m->exp_payper = tmp_int[8]; + m->position = tmp_int[9]; + memcpy(m->name, tmp_str[0], 24); + + for(j = 0; j < 2 && str != NULL; j++) // 位置スキップ + str = strchr(str + 1, '\t'); + } +// printf("GuildMemberInfo OK\n"); + // 役職 + i = 0; + while (sscanf(str+1, "%d,%d%n", &tmp_int[0], &tmp_int[1], &j) == 2 && str[1+j] == '\t') { + struct guild_position *p = &g->position[i]; + if (sscanf(str+1, "%d,%d\t%[^\t]\t", &tmp_int[0], &tmp_int[1], tmp_str[0]) < 3) + return 1; + p->mode = tmp_int[0]; + p->exp_mode = tmp_int[1]; + tmp_str[0][strlen(tmp_str[0])-1] = 0; + memcpy(p->name, tmp_str[0], 24); + + for(j = 0; j < 2 && str != NULL; j++) // 位置スキップ + str = strchr(str+1, '\t'); + i++; + } +// printf("GuildPositionInfo OK\n"); + // エンブレム + tmp_int[1] = 0; + if (sscanf(str + 1, "%d,%d,%[^\t]\t", &tmp_int[0], &tmp_int[1], tmp_str2)< 3 && + sscanf(str + 1, "%d,%[^\t]\t", &tmp_int[0], tmp_str2) < 2) + return 1; + g->emblem_len = tmp_int[0]; + g->emblem_id = tmp_int[1]; + for(i = 0, pstr = tmp_str2; i < g->emblem_len; i++, pstr += 2) { + int c1 = pstr[0], c2 = pstr[1], x1 = 0, x2 = 0; + if (c1 >= '0' && c1 <= '9') x1 = c1 - '0'; + if (c1 >= 'a' && c1 <= 'f') x1 = c1 - 'a' + 10; + if (c1 >= 'A' && c1 <= 'F') x1 = c1 - 'A' + 10; + if (c2 >= '0' && c2 <= '9') x2 = c2 - '0'; + if (c2 >= 'a' && c2 <= 'f') x2 = c2 - 'a' + 10; + if (c2 >= 'A' && c2 <= 'F') x2 = c2 - 'A' + 10; + g->emblem_data[i] = (x1<<4) | x2; + } +// printf("GuildEmblemInfo OK\n"); + str=strchr(str + 1, '\t'); // 位置スキップ + + // 同盟リスト + if (sscanf(str + 1, "%d\t", &c) < 1) + return 1; + str = strchr(str + 1, '\t'); // 位置スキップ + for(i = 0; i < c; i++) { + struct guild_alliance *a = &g->alliance[i]; + if (sscanf(str + 1, "%d,%d\t%[^\t]\t", &tmp_int[0], &tmp_int[1], tmp_str[0]) < 3) + return 1; + a->guild_id = tmp_int[0]; + a->opposition = tmp_int[1]; + memcpy(a->name, tmp_str[0], 24); + + for(j = 0; j < 2 && str != NULL; j++) // 位置スキップ + str = strchr(str + 1, '\t'); + } +// printf("GuildAllianceInfo OK\n"); + // 追放リスト + if (sscanf(str+1, "%d\t", &c) < 1) + return 1; + str = strchr(str + 1, '\t'); // 位置スキップ + for(i = 0; i < c; i++) { + struct guild_explusion *e = &g->explusion[i]; + if (sscanf(str + 1, "%d,%d,%d,%d\t%[^\t]\t%[^\t]\t%[^\t]\t", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], + tmp_str[0], tmp_str[1], tmp_str[2]) < 6) + return 1; + e->account_id = tmp_int[0]; + e->rsv1 = tmp_int[1]; + e->rsv2 = tmp_int[2]; + e->rsv3 = tmp_int[3]; + memcpy(e->name, tmp_str[0], 24); + memcpy(e->acc, tmp_str[1], 24); + tmp_str[2][strlen(tmp_str[2])-1] = 0; + memcpy(e->mes, tmp_str[2], 40); + + for(j = 0; j < 4 && str != NULL; j++) // 位置スキップ + str = strchr(str + 1, '\t'); + } +// printf("GuildExplusionInfo OK\n"); + // ギルドスキル + for(i = 0; i < MAX_GUILDSKILL; i++) { + if (sscanf(str+1,"%d,%d ", &tmp_int[0], &tmp_int[1]) < 2) + break; + g->skill[i].id = tmp_int[0]; + g->skill[i].lv = tmp_int[1]; + str = strchr(str + 1, ' '); + } + str = strchr(str + 1, '\t'); +// printf("GuildSkillInfo OK\n"); + + return 0; +} + +// ギルド城データの文字列への変換 +int inter_guildcastle_tostr(char *str, struct guild_castle *gc) { + int len; + + len = sprintf(str, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", // added Guardian HP [Valaris] + gc->castle_id, gc->guild_id, gc->economy, gc->defense, gc->triggerE, + gc->triggerD, gc->nextTime, gc->payTime, gc->createTime, gc->visibleC, + gc->visibleG0, gc->visibleG1, gc->visibleG2, gc->visibleG3, gc->visibleG4, + gc->visibleG5, gc->visibleG6, gc->visibleG7, gc->Ghp0, gc->Ghp1, gc->Ghp2, + gc->Ghp3, gc->Ghp4, gc->Ghp5, gc->Ghp6, gc->Ghp7); + + return 0; +} + +// ギルド城データの文字列からの変換 +int inter_guildcastle_fromstr(char *str, struct guild_castle *gc) { + int tmp_int[26]; + + memset(gc, 0, sizeof(struct guild_castle)); + // new structure of guild castle + if (sscanf(str, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], &tmp_int[4], &tmp_int[5], &tmp_int[6], + &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], &tmp_int[13], + &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18], &tmp_int[19], &tmp_int[20], + &tmp_int[21], &tmp_int[22], &tmp_int[23], &tmp_int[24], &tmp_int[25]) == 26) { + gc->castle_id = tmp_int[0]; + gc->guild_id = tmp_int[1]; + gc->economy = tmp_int[2]; + gc->defense = tmp_int[3]; + gc->triggerE = tmp_int[4]; + gc->triggerD = tmp_int[5]; + gc->nextTime = tmp_int[6]; + gc->payTime = tmp_int[7]; + gc->createTime = tmp_int[8]; + gc->visibleC = tmp_int[9]; + gc->visibleG0 = tmp_int[10]; + gc->visibleG1 = tmp_int[11]; + gc->visibleG2 = tmp_int[12]; + gc->visibleG3 = tmp_int[13]; + gc->visibleG4 = tmp_int[14]; + gc->visibleG5 = tmp_int[15]; + gc->visibleG6 = tmp_int[16]; + gc->visibleG7 = tmp_int[17]; + gc->Ghp0 = tmp_int[18]; + gc->Ghp1 = tmp_int[19]; + gc->Ghp2 = tmp_int[20]; + gc->Ghp3 = tmp_int[21]; + gc->Ghp4 = tmp_int[22]; + gc->Ghp5 = tmp_int[23]; + gc->Ghp6 = tmp_int[24]; + gc->Ghp7 = tmp_int[25]; // end additions [Valaris] + // old structure of guild castle + } else if (sscanf(str, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], &tmp_int[4], &tmp_int[5], &tmp_int[6], + &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12], &tmp_int[13], + &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17]) == 18) { + gc->castle_id = tmp_int[0]; + gc->guild_id = tmp_int[1]; + gc->economy = tmp_int[2]; + gc->defense = tmp_int[3]; + gc->triggerE = tmp_int[4]; + gc->triggerD = tmp_int[5]; + gc->nextTime = tmp_int[6]; + gc->payTime = tmp_int[7]; + gc->createTime = tmp_int[8]; + gc->visibleC = tmp_int[9]; + gc->visibleG0 = tmp_int[10]; + gc->visibleG1 = tmp_int[11]; + gc->visibleG2 = tmp_int[12]; + gc->visibleG3 = tmp_int[13]; + gc->visibleG4 = tmp_int[14]; + gc->visibleG5 = tmp_int[15]; + gc->visibleG6 = tmp_int[16]; + gc->visibleG7 = tmp_int[17]; + if (gc->visibleG0 == 1) + gc->Ghp0 = 15670 + 2000 * gc->defense; + else + gc->Ghp0 = 0; + if (gc->visibleG1 == 1) + gc->Ghp1 = 15670 + 2000 * gc->defense; + else + gc->Ghp1 = 0; + if (gc->visibleG2 == 1) + gc->Ghp2 = 15670 + 2000 * gc->defense; + else + gc->Ghp2 = 0; + if (gc->visibleG3 == 1) + gc->Ghp3 = 30214 + 2000 * gc->defense; + else + gc->Ghp3 = 0; + if (gc->visibleG4 == 1) + gc->Ghp4 = 30214 + 2000 * gc->defense; + else + gc->Ghp4 = 0; + if (gc->visibleG5 == 1) + gc->Ghp5 = 28634 + 2000 * gc->defense; + else + gc->Ghp5 = 0; + if (gc->visibleG6 == 1) + gc->Ghp6 = 28634 + 2000 * gc->defense; + else + gc->Ghp6 = 0; + if (gc->visibleG7 == 1) + gc->Ghp7 = 28634 + 2000 * gc->defense; + else + gc->Ghp7 = 0; + } else { + return 1; + } + + return 0; +} + +// ギルド関連データベース読み込み +int inter_guild_readdb() { + int i; + FILE *fp; + char line[1024]; + + fp = fopen("db/exp_guild.txt", "r"); + if (fp == NULL) { + printf("can't read db/exp_guild.txt\n"); + return 1; + } + i = 0; + while(fgets(line, sizeof(line)-1, fp) && i < 100){ + if (line[0] == '/' && line[1] == '/') + continue; + guild_exp[i] = atoi(line); + i++; + } + fclose(fp); + + return 0; +} + +// ギルドデータの読み込み +int inter_guild_init() { + char line[16384]; + struct guild *g; + struct guild_castle *gc; + FILE *fp; + int i, j, c = 0; + + inter_guild_readdb(); + + guild_db = numdb_init(); + castle_db = numdb_init(); + + if ((fp = fopen(guild_txt,"r")) == NULL) + return 1; + while(fgets(line, sizeof(line)-1, fp)) { + j = 0; + if (sscanf(line, "%d\t%%newid%%\n%n", &i, &j) == 1 && j > 0 && guild_newid <= i) { + guild_newid = i; + continue; + } + + g = calloc(sizeof(struct guild), 1); + if(g == NULL){ + printf("int_guild: out of memory!\n"); + exit(0); + } + memset(g, 0, sizeof(struct guild)); + if (inter_guild_fromstr(line, g) == 0 && g->guild_id > 0) { + if (g->guild_id >= guild_newid) + guild_newid = g->guild_id + 1; + numdb_insert(guild_db, g->guild_id, g); + guild_check_empty(g); + guild_calcinfo(g); + } else { + printf("int_guild: broken data [%s] line %d\n", guild_txt, c); + free(g); + } + c++; + } + fclose(fp); +// printf("int_guild: %s read done (%d guilds)\n", guild_txt, c); + + c = 0;//カウンタ初期化 + + if ((fp = fopen(castle_txt, "r")) == NULL) { + return 1; + } + + while(fgets(line, sizeof(line)-1, fp)) { + gc = calloc(sizeof(struct guild_castle), 1); + if(gc == NULL){ + printf("int_guild: out of memory!\n"); + exit(0); + } + memset(gc, 0, sizeof(struct guild_castle)); + if (inter_guildcastle_fromstr(line, gc) == 0) { + numdb_insert(castle_db, gc->castle_id, gc); + } else { + printf("int_guild: broken data [%s] line %d\n", castle_txt, c); + free(gc); + } + c++; + } + + if (!c) { + printf(" %s - making Default Data...\n", castle_txt); + //デフォルトデータを作成 + for(i = 0; i < MAX_GUILDCASTLE; i++) { + gc = calloc(sizeof(struct guild_castle), 1); + if (gc == NULL) { + printf("int_guild: out of memory!\n"); + exit(0); + } + memset(gc, 0, sizeof(struct guild_castle)); + gc->castle_id = i; + gc->guild_id = 0; + gc->economy = 0; + gc->defense = 0; + gc->triggerE = 0; + gc->triggerD = 0; + gc->nextTime = 0; + gc->payTime = 0; + gc->createTime = 0; + gc->visibleC = 0; + gc->visibleG0 = 0; + gc->visibleG1 = 0; + gc->visibleG2 = 0; + gc->visibleG3 = 0; + gc->visibleG4 = 0; + gc->visibleG5 = 0; + gc->visibleG6 = 0; + gc->visibleG7 = 0; + gc->Ghp0 = 0; // guardian HP [Valaris] + gc->Ghp1 = 0; + gc->Ghp2 = 0; + gc->Ghp3 = 0; + gc->Ghp4 = 0; + gc->Ghp5 = 0; + gc->Ghp6 = 0; + gc->Ghp7 = 0; // end additions [Valaris] + numdb_insert(castle_db, gc->castle_id, gc); + } + printf(" %s - making done\n",castle_txt); + return 0; + } + + fclose(fp); + + return 0; +} + +struct guild *inter_guild_search(int guild_id) { + struct guild *g; + + g=numdb_search(guild_db, guild_id); + + return g; +} + +// ギルドデータのセーブ用 +int inter_guild_save_sub(void *key,void *data,va_list ap) { + char line[16384]; + FILE *fp; + + inter_guild_tostr(line,(struct guild *)data); + fp=va_arg(ap,FILE *); + fprintf(fp,"%s" RETCODE,line); + + return 0; +} + +// ギルド城データのセーブ用 +int inter_castle_save_sub(void *key, void *data, va_list ap) { + char line[16384]; + FILE *fp; + + inter_guildcastle_tostr(line, (struct guild_castle *)data); + fp = va_arg(ap, FILE *); + fprintf(fp, "%s" RETCODE, line); + + return 0; +} + +// ギルドデータのセーブ +int inter_guild_save() { + FILE *fp; + int lock; + + if ((fp = lock_fopen(guild_txt, &lock)) == NULL) { + printf("int_guild: cant write [%s] !!! data is lost !!!\n", guild_txt); + return 1; + } + numdb_foreach(guild_db, inter_guild_save_sub, fp); +// fprintf(fp, "%d\t%%newid%%\n", guild_newid); + lock_fclose(fp, guild_txt, &lock); +// printf("int_guild: %s saved.\n", guild_txt); + + if ((fp = lock_fopen(castle_txt,&lock)) == NULL) { + printf("int_guild: cant write [%s] !!! data is lost !!!\n", castle_txt); + return 1; + } + numdb_foreach(castle_db, inter_castle_save_sub, fp); + lock_fclose(fp, castle_txt, &lock); + + return 0; +} + +// ギルド名検索用 +int search_guildname_sub(void *key, void *data, va_list ap) { + struct guild *g = (struct guild *)data, **dst; + char *str; + + str = va_arg(ap, char *); + dst = va_arg(ap, struct guild **); + if (strcmpi(g->name, str) == 0) + *dst = g; + return 0; +} + +// ギルド名検索 +struct guild* search_guildname(char *str) { + struct guild *g = NULL; + numdb_foreach(guild_db, search_guildname_sub, str, &g); + return g; +} + +// ギルドが空かどうかチェック +int guild_check_empty(struct guild *g) { + int i; + + for(i = 0; i < g->max_member; i++) { + if (g->member[i].account_id > 0) { + return 0; + } + } + // 誰もいないので解散 + numdb_foreach(guild_db, guild_break_sub, g->guild_id); + numdb_erase(guild_db, g->guild_id); + inter_guild_storage_delete(g->guild_id); + mapif_guild_broken(g->guild_id, 0); + free(g); + + return 1; +} + +// キャラの競合がないかチェック用 +int guild_check_conflict_sub(void *key, void *data, va_list ap) { + struct guild *g = (struct guild *)data; + int guild_id, account_id, char_id, i; + + guild_id = va_arg(ap, int); + account_id = va_arg(ap, int); + char_id = va_arg(ap, int); + + if (g->guild_id == guild_id) // 本来の所属なので問題なし + return 0; + + for(i = 0; i < MAX_GUILD; i++) { + if (g->member[i].account_id == account_id && g->member[i].char_id == char_id) { + // 別のギルドに偽の所属データがあるので脱退 + printf("int_guild: guild conflict! %d,%d %d!=%d\n", account_id, char_id, guild_id, g->guild_id); + mapif_parse_GuildLeave(-1, g->guild_id, account_id, char_id, 0, "**データ競合**"); + } + } + + return 0; +} +// キャラの競合がないかチェック +int guild_check_conflict(int guild_id, int account_id, int char_id) { + numdb_foreach(guild_db, guild_check_conflict_sub, guild_id, account_id, char_id); + + return 0; +} + +int guild_nextexp(int level) { + if (level < 100) + return guild_exp[level-1]; + + return 0; +} + +// ギルドスキルがあるか確認 +int guild_checkskill(struct guild *g, int id){ + return g->skill[id-10000].lv; +} + +// ギルドの情報の再計算 +int guild_calcinfo(struct guild *g) { + int i, c, nextexp; + struct guild before = *g; + + // スキルIDの設定 + for(i = 0; i < 5; i++) + g->skill[i].id = i + 10000; + + // ギルドレベル + if (g->guild_lv <= 0) + g->guild_lv = 1; + nextexp = guild_nextexp(g->guild_lv); + if (nextexp > 0) { + while(g->exp >= nextexp) { // レベルアップ処理 + g->exp -= nextexp; + g->guild_lv++; + g->skill_point++; + nextexp = guild_nextexp(g->guild_lv); + } + } + + // ギルドの次の経験値 + g->next_exp = guild_nextexp(g->guild_lv); + + // メンバ上限(ギルド拡張適用) + g->max_member = 16 + guild_checkskill(g, 10004) * 2; + + // 平均レベルとオンライン人数 + g->average_lv = 0; + g->connect_member = 0; + c = 0; + for(i = 0; i < g->max_member; i++) { + if (g->member[i].account_id > 0) { + g->average_lv += g->member[i].lv; + c++; + if (g->member[i].online > 0) + g->connect_member++; + } + } + g->average_lv /= c; + + // 全データを送る必要がありそう + if (g->max_member != before.max_member || + g->guild_lv != before.guild_lv || + g->skill_point != before.skill_point) { + mapif_guild_info(-1, g); + return 1; + } + + return 0; +} + +//------------------------------------------------------------------- +// map serverへの通信 + +// ギルド作成可否 +int mapif_guild_created(int fd, int account_id, struct guild *g) { + WFIFOW(fd,0) = 0x3830; + WFIFOL(fd,2) = account_id; + if (g != NULL) { + WFIFOL(fd,6) = g->guild_id; + printf("int_guild: created! %d %s\n", g->guild_id, g->name); + }else{ + WFIFOL(fd,6) = 0; + } + WFIFOSET(fd,10); + return 0; +} + +// ギルド情報見つからず +int mapif_guild_noinfo(int fd, int guild_id) { + WFIFOW(fd,0) = 0x3831; + WFIFOW(fd,2) = 8; + WFIFOL(fd,4) = guild_id; + WFIFOSET(fd,8); + printf("int_guild: info not found %d\n", guild_id); + + return 0; +} + +// ギルド情報まとめ送り +int mapif_guild_info(int fd, struct guild *g) { + unsigned char buf[4 + sizeof(struct guild)]; + + WBUFW(buf,0) = 0x3831; + memcpy(buf + 4, g, sizeof(struct guild)); + WBUFW(buf,2) = 4 + sizeof(struct guild); +// printf("int_guild: sizeof(guild)=%d\n", sizeof(struct guild)); + if (fd < 0) + mapif_sendall(buf, WBUFW(buf,2)); + else + mapif_send(fd, buf, WBUFW(buf,2)); +// printf("int_guild: info %d %s\n", p->guild_id, p->name); + + return 0; +} + +// メンバ追加可否 +int mapif_guild_memberadded(int fd, int guild_id, int account_id, int char_id, int flag) { + WFIFOW(fd,0) = 0x3832; + WFIFOL(fd,2) = guild_id; + WFIFOL(fd,6) = account_id; + WFIFOL(fd,10) = char_id; + WFIFOB(fd,14) = flag; + WFIFOSET(fd, 15); + + return 0; +} + +// 脱退/追放通知 +int mapif_guild_leaved(int guild_id, int account_id, int char_id, int flag, const char *name, const char *mes) { + unsigned char buf[79]; + + WBUFW(buf, 0) = 0x3834; + WBUFL(buf, 2) = guild_id; + WBUFL(buf, 6) = account_id; + WBUFL(buf,10) = char_id; + WBUFB(buf,14) = flag; + memcpy(WBUFP(buf,15), mes, 40); + memcpy(WBUFP(buf,55), name, 24); + mapif_sendall(buf, 79); + printf("int_guild: guild leaved %d %d %s %s\n", guild_id, account_id, name, mes); + + return 0; +} + +// オンライン状態とLv更新通知 +int mapif_guild_memberinfoshort(struct guild *g, int idx) { + unsigned char buf[19]; + + WBUFW(buf, 0) = 0x3835; + WBUFL(buf, 2) = g->guild_id; + WBUFL(buf, 6) = g->member[idx].account_id; + WBUFL(buf,10) = g->member[idx].char_id; + WBUFB(buf,14) = g->member[idx].online; + WBUFW(buf,15) = g->member[idx].lv; + WBUFW(buf,17) = g->member[idx].class; + mapif_sendall(buf, 19); + return 0; +} + +// 解散通知 +int mapif_guild_broken(int guild_id, int flag) { + unsigned char buf[7]; + + WBUFW(buf,0) = 0x3836; + WBUFL(buf,2) = guild_id; + WBUFB(buf,6) = flag; + mapif_sendall(buf, 7); + printf("int_guild: broken %d\n", guild_id); + + return 0; +} + +// ギルド内発言 +int mapif_guild_message(int guild_id, int account_id, char *mes, int len) { + unsigned char buf[len+12]; + + WBUFW(buf,0) = 0x3837; + WBUFW(buf,2) = len + 12; + WBUFL(buf,4) = guild_id; + WBUFL(buf,8) = account_id; + memcpy(WBUFP(buf,12), mes, len); + mapif_sendall(buf, len + 12); + + return 0; +} + +// ギルド基本情報変更通知 +int mapif_guild_basicinfochanged(int guild_id, int type, const void *data, int len) { + unsigned char buf[2048]; + + WBUFW(buf,0) = 0x3839; + WBUFW(buf,2) = len+10; + WBUFL(buf,4) = guild_id; + WBUFW(buf,8) = type; + memcpy(WBUFP(buf,10),data,len); + mapif_sendall(buf,len+10); + return 0; +} + +// ギルドメンバ情報変更通知 +int mapif_guild_memberinfochanged(int guild_id, int account_id, int char_id, int type, const void *data, int len) { + unsigned char buf[len + 18]; + + WBUFW(buf, 0) = 0x383a; + WBUFW(buf, 2) = len + 18; + WBUFL(buf, 4) = guild_id; + WBUFL(buf, 8) = account_id; + WBUFL(buf,12) = char_id; + WBUFW(buf,16) = type; + memcpy(WBUFP(buf,18), data, len); + mapif_sendall(buf,len+18); + + return 0; +} + +// ギルドスキルアップ通知 +int mapif_guild_skillupack(int guild_id, int skill_num, int account_id) { + unsigned char buf[14]; + + WBUFW(buf, 0) = 0x383c; + WBUFL(buf, 2) = guild_id; + WBUFL(buf, 6) = skill_num; + WBUFL(buf,10) = account_id; + mapif_sendall(buf, 14); + + return 0; +} + +// ギルド同盟/敵対通知 +int mapif_guild_alliance(int guild_id1, int guild_id2, int account_id1, int account_id2, int flag, const char *name1, const char *name2) { + unsigned char buf[67]; + + WBUFW(buf, 0) = 0x383d; + WBUFL(buf, 2) = guild_id1; + WBUFL(buf, 6) = guild_id2; + WBUFL(buf,10) = account_id1; + WBUFL(buf,14) = account_id2; + WBUFB(buf,18) = flag; + memcpy(WBUFP(buf,19), name1, 24); + memcpy(WBUFP(buf,43), name2, 24); + mapif_sendall(buf, 67); + + return 0; +} + +// ギルド役職変更通知 +int mapif_guild_position(struct guild *g, int idx) { + unsigned char buf[sizeof(struct guild_position) + 12]; + + WBUFW(buf,0) = 0x383b; + WBUFW(buf,2) = sizeof(struct guild_position) + 12; + WBUFL(buf,4) = g->guild_id; + WBUFL(buf,8) = idx; + memcpy(WBUFP(buf,12), &g->position[idx], sizeof(struct guild_position)); + mapif_sendall(buf, WBUFW(buf,2)); + + return 0; +} + +// ギルド告知変更通知 +int mapif_guild_notice(struct guild *g) { + unsigned char buf[186]; + + WBUFW(buf,0) = 0x383e; + WBUFL(buf,2) = g->guild_id; + memcpy(WBUFP(buf,6), g->mes1, 60); + memcpy(WBUFP(buf,66), g->mes2, 120); + mapif_sendall(buf, 186); + + return 0; +} + +// ギルドエンブレム変更通知 +int mapif_guild_emblem(struct guild *g) { + unsigned char buf[2048]; + + WBUFW(buf,0) = 0x383f; + WBUFW(buf,2) = g->emblem_len + 12; + WBUFL(buf,4) = g->guild_id; + WBUFL(buf,8) = g->emblem_id; + memcpy(WBUFP(buf,12), g->emblem_data, g->emblem_len); + mapif_sendall(buf, WBUFW(buf,2)); + + return 0; +} + +int mapif_guild_castle_dataload(int castle_id, int index, int value) { + unsigned char buf[9]; + + WBUFW(buf,0) = 0x3840; + WBUFW(buf,2) = castle_id; + WBUFB(buf,4) = index; + WBUFL(buf,5) = value; + mapif_sendall(buf,9); + + return 0; +} + +int mapif_guild_castle_datasave(int castle_id, int index, int value) { + unsigned char buf[9]; + + WBUFW(buf,0) = 0x3841; + WBUFW(buf,2) = castle_id; + WBUFB(buf,4) = index; + WBUFL(buf,5) = value; + mapif_sendall(buf,9); + + return 0; +} + +int mapif_guild_castle_alldataload_sub(void *key, void *data, va_list ap) { + int fd = va_arg(ap, int); + int *p = va_arg(ap, int*); + + memcpy(WFIFOP(fd,*p), (struct guild_castle*)data, sizeof(struct guild_castle)); + (*p) += sizeof(struct guild_castle); + + return 0; +} + +int mapif_guild_castle_alldataload(int fd) { + int len = 4; + + WFIFOW(fd,0) = 0x3842; + numdb_foreach(castle_db, mapif_guild_castle_alldataload_sub, fd, &len); + WFIFOW(fd,2) = len; + WFIFOSET(fd, len); + + return 0; +} + +//------------------------------------------------------------------- +// map serverからの通信 + +// ギルド作成要求 +int mapif_parse_CreateGuild(int fd, int account_id, char *name, struct guild_member *master) { + struct guild *g; + int i; + + for(i = 0; i < 24 && name[i]; i++) { + if (!(name[i] & 0xe0) || name[i] == 0x7f) { + printf("int_guild: illeagal guild name [%s]\n", name); + mapif_guild_created(fd, account_id, NULL); + return 0; + } + } + + if ((g = search_guildname(name)) != NULL) { + printf("int_guild: same name guild exists [%s]\n", name); + mapif_guild_created(fd, account_id, NULL); + return 0; + } + g = calloc(sizeof(struct guild), 1); + if (g == NULL) { + printf("int_guild: CreateGuild: out of memory !\n"); + mapif_guild_created(fd, account_id, NULL); + exit(0); + } + memset(g, 0, sizeof(struct guild)); + g->guild_id = guild_newid++; + memcpy(g->name, name, 24); + memcpy(g->master, master->name, 24); + memcpy(&g->member[0], master, sizeof(struct guild_member)); + + g->position[0].mode = 0x11; + strcpy(g->position[ 0].name, "GuildMaster"); + strcpy(g->position[MAX_GUILDPOSITION-1].name, "Newbie"); + for(i = 1; i < MAX_GUILDPOSITION-1; i++) + sprintf(g->position[i].name, "Position %d", i + 1); + + // ここでギルド情報計算が必要と思われる + g->max_member = 16; + g->average_lv = master->lv; + for(i = 0; i < 5; i++) + g->skill[i].id = i + 10000; + + numdb_insert(guild_db, g->guild_id, g); + + mapif_guild_created(fd, account_id, g); + mapif_guild_info(fd, g); + + inter_log("guild %s (id=%d) created by master %s (id=%d)" RETCODE, + name, g->guild_id, master->name, master->account_id); + + return 0; +} + +// ギルド情報要求 +int mapif_parse_GuildInfo(int fd, int guild_id) { + struct guild *g; + + g = numdb_search(guild_db, guild_id); + if (g != NULL){ + guild_calcinfo(g); + mapif_guild_info(fd, g); + } else + mapif_guild_noinfo(fd, guild_id); + + return 0; +} + +// ギルドメンバ追加要求 +int mapif_parse_GuildAddMember(int fd, int guild_id, struct guild_member *m) { + struct guild *g; + int i; + + g = numdb_search(guild_db, guild_id); + if (g == NULL) { + mapif_guild_memberadded(fd, guild_id, m->account_id, m->char_id, 1); + return 0; + } + + for(i = 0; i < g->max_member; i++) { + if (g->member[i].account_id == 0) { + memcpy(&g->member[i], m, sizeof(struct guild_member)); + mapif_guild_memberadded(fd, guild_id, m->account_id, m->char_id, 0); + guild_calcinfo(g); + mapif_guild_info(-1, g); + + return 0; + } + } + mapif_guild_memberadded(fd, guild_id, m->account_id, m->char_id, 1); + + return 0; +} + +// ギルド脱退/追放要求 +int mapif_parse_GuildLeave(int fd, int guild_id, int account_id, int char_id, int flag, const char *mes) { + struct guild *g = NULL; + int i, j; + + g = numdb_search(guild_db, guild_id); + if (g != NULL) { + for(i = 0; i < MAX_GUILD; i++) { + if (g->member[i].account_id == account_id && g->member[i].char_id == char_id) { +// printf("%d %d\n", i, (int)(&g->member[i])); +// printf("%d %s\n", i, g->member[i].name); + + if (flag) { // 追放の場合追放リストに入れる + for(j = 0; j < MAX_GUILDEXPLUSION; j++) { + if (g->explusion[j].account_id == 0) + break; + } + if (j == MAX_GUILDEXPLUSION) { // 一杯なので古いのを消す + for(j = 0; j < MAX_GUILDEXPLUSION - 1; j++) + g->explusion[j] = g->explusion[j+1]; + j = MAX_GUILDEXPLUSION - 1; + } + g->explusion[j].account_id = account_id; + memcpy(g->explusion[j].acc, "dummy", 24); + memcpy(g->explusion[j].name, g->member[i].name, 24); + memcpy(g->explusion[j].mes, mes, 40); + } + + mapif_guild_leaved(guild_id, account_id, char_id, flag, g->member[i].name, mes); +// printf("%d %d\n", i, (int)(&g->member[i])); +// printf("%d %s\n", i, (&g->member[i])->name); + memset(&g->member[i], 0, sizeof(struct guild_member)); + + if (guild_check_empty(g) == 0) + mapif_guild_info(-1,g);// まだ人がいるのでデータ送信 + + return 0; + } + } + } + return 0; +} + +// オンライン/Lv更新 +int mapif_parse_GuildChangeMemberInfoShort(int fd, int guild_id, int account_id, int char_id, int online, int lv, int class) { + struct guild *g; + int i, alv, c; + + g = numdb_search(guild_db, guild_id); + if (g == NULL) + return 0; + + g->connect_member = 0; + + alv = 0; + c = 0; + for(i = 0; i < MAX_GUILD; i++) { + if (g->member[i].account_id == account_id && g->member[i].char_id == char_id) { + g->member[i].online = online; + g->member[i].lv = lv; + g->member[i].class = class; + mapif_guild_memberinfoshort(g, i); + } + if (g->member[i].account_id > 0) { + alv += g->member[i].lv; + c++; + } + if (g->member[i].online) + g->connect_member++; + } + // 平均レベル + g->average_lv = alv / c; + + return 0; +} + +// ギルド解散処理用(同盟/敵対を解除) +int guild_break_sub(void *key, void *data, va_list ap) { + struct guild *g = (struct guild *)data; + int guild_id = va_arg(ap, int); + int i; + + for(i = 0; i < MAX_GUILDALLIANCE; i++) { + if (g->alliance[i].guild_id == guild_id) + g->alliance[i].guild_id = 0; + } + return 0; +} + +// ギルド解散要求 +int mapif_parse_BreakGuild(int fd, int guild_id) { + struct guild *g; + + g = numdb_search(guild_db, guild_id); + if(g == NULL) + return 0; + + numdb_foreach(guild_db, guild_break_sub, guild_id); + numdb_erase(guild_db, guild_id); + inter_guild_storage_delete(guild_id); + mapif_guild_broken(guild_id, 0); + + inter_log("guild %s (id=%d) broken" RETCODE, g->name, guild_id); + free(g); + + return 0; +} + +// ギルドメッセージ送信 +int mapif_parse_GuildMessage(int fd, int guild_id, int account_id, char *mes, int len) { + return mapif_guild_message(guild_id, account_id, mes, len); +} + +// ギルド基本データ変更要求 +int mapif_parse_GuildBasicInfoChange(int fd, int guild_id, int type, const char *data, int len) { + struct guild *g; + short dw = *((short *)data); + + g = numdb_search(guild_db, guild_id); + if (g == NULL) + return 0; + + switch(type) { + case GBI_GUILDLV: + if (dw > 0 && g->guild_lv + dw <= 50) { + g->guild_lv+=dw; + g->skill_point+=dw; + } else if (dw < 0 && g->guild_lv + dw >= 1) + g->guild_lv += dw; + mapif_guild_info(-1, g); + return 0; + default: + printf("int_guild: GuildBasicInfoChange: Unknown type %d\n", type); + break; + } + mapif_guild_basicinfochanged(guild_id, type, data, len); + + return 0; +} + +// ギルドメンバデータ変更要求 +int mapif_parse_GuildMemberInfoChange(int fd, int guild_id, int account_id, int char_id, int type, const char *data, int len) { + int i; + struct guild *g; + + g = numdb_search(guild_db, guild_id); + if(g == NULL) + return 0; + + for(i = 0; i < g->max_member; i++) + if (g->member[i].account_id == account_id && g->member[i].char_id == char_id) + break; + if (i == g->max_member) { + printf("int_guild: GuildMemberChange: Not found %d,%d in %d[%s]\n", account_id, char_id, guild_id, g->name); + return 0; + } + switch(type) { + case GMI_POSITION: // 役職 + g->member[i].position = *((int *)data); + break; + case GMI_EXP: // EXP + { + int exp, oldexp = g->member[i].exp; + exp = g->member[i].exp = *((unsigned int *)data); + g->exp += (exp - oldexp); + guild_calcinfo(g); // Lvアップ判断 + mapif_guild_basicinfochanged(guild_id, GBI_EXP, &g->exp, 4); + } + break; + default: + printf("int_guild: GuildMemberInfoChange: Unknown type %d\n", type); + break; + } + mapif_guild_memberinfochanged(guild_id, account_id, char_id, type, data, len); + + return 0; +} + +// ギルド役職名変更要求 +int mapif_parse_GuildPosition(int fd, int guild_id, int idx, struct guild_position *p) { + struct guild *g = numdb_search(guild_db, guild_id); + + if (g == NULL || idx < 0 || idx >= MAX_GUILDPOSITION) { + return 0; + } + memcpy(&g->position[idx], p, sizeof(struct guild_position)); + mapif_guild_position(g, idx); + printf("int_guild: position changed %d\n", idx); + + return 0; +} + +// ギルドスキルアップ要求 +int mapif_parse_GuildSkillUp(int fd, int guild_id, int skill_num, int account_id) { + struct guild *g = numdb_search(guild_db, guild_id); + int idx = skill_num - 10000; + + if (g == NULL || skill_num < 10000) + return 0; + + if (g->skill_point > 0 && g->skill[idx].id > 0 && g->skill[idx].lv < 10) { + g->skill[idx].lv++; + g->skill_point--; + if (guild_calcinfo(g) == 0) + mapif_guild_info(-1, g); + mapif_guild_skillupack(guild_id, skill_num, account_id); + printf("int_guild: skill %d up\n", skill_num); + } + + return 0; +} + +// ギルド同盟要求 +int mapif_parse_GuildAlliance(int fd, int guild_id1, int guild_id2, int account_id1, int account_id2, int flag) { + struct guild *g[2]; + int j, i; + + g[0] = numdb_search(guild_db, guild_id1); + g[1] = numdb_search(guild_db, guild_id2); + if (g[0] == NULL || g[1] == NULL) + return 0; + + if (!(flag & 0x8)) { + for(i = 0; i < 2 - (flag & 1); i++) { + for(j = 0; j < MAX_GUILDALLIANCE; j++) + if (g[i]->alliance[j].guild_id == 0) { + g[i]->alliance[j].guild_id = g[1-i]->guild_id; + memcpy(g[i]->alliance[j].name, g[1-i]->name, 24); + g[i]->alliance[j].opposition = flag & 1; + break; + } + } + } else { // 関係解消 + for(i = 0; i < 2 - (flag & 1); i++) { + for(j = 0; j < MAX_GUILDALLIANCE; j++) + if (g[i]->alliance[j].guild_id == g[1-i]->guild_id && g[i]->alliance[j].opposition == (flag & 1)) { + g[i]->alliance[j].guild_id = 0; + break; + } + } + } + mapif_guild_alliance(guild_id1, guild_id2, account_id1, account_id2, flag, g[0]->name, g[1]->name); + + return 0; +} + +// ギルド告知変更要求 +int mapif_parse_GuildNotice(int fd, int guild_id, const char *mes1, const char *mes2) { + struct guild *g; + + g = numdb_search(guild_db, guild_id); + if (g == NULL) + return 0; + memcpy(g->mes1, mes1, 60); + memcpy(g->mes2, mes2, 120); + + return mapif_guild_notice(g); +} + +// ギルドエンブレム変更要求 +int mapif_parse_GuildEmblem(int fd, int len, int guild_id, int dummy, const char *data) { + struct guild *g; + + g = numdb_search(guild_db, guild_id); + if (g == NULL) + return 0; + memcpy(g->emblem_data, data, len); + g->emblem_len = len; + g->emblem_id++; + + return mapif_guild_emblem(g); +} + +int mapif_parse_GuildCastleDataLoad(int fd, int castle_id, int index) { + struct guild_castle *gc = numdb_search(castle_db, castle_id); + + if (gc == NULL) { + return mapif_guild_castle_dataload(castle_id, 0, 0); + } + switch(index) { + case 1: return mapif_guild_castle_dataload(gc->castle_id, index, gc->guild_id); + case 2: return mapif_guild_castle_dataload(gc->castle_id, index, gc->economy); + case 3: return mapif_guild_castle_dataload(gc->castle_id, index, gc->defense); + case 4: return mapif_guild_castle_dataload(gc->castle_id, index, gc->triggerE); + case 5: return mapif_guild_castle_dataload(gc->castle_id, index, gc->triggerD); + case 6: return mapif_guild_castle_dataload(gc->castle_id, index, gc->nextTime); + case 7: return mapif_guild_castle_dataload(gc->castle_id, index, gc->payTime); + case 8: return mapif_guild_castle_dataload(gc->castle_id, index, gc->createTime); + case 9: return mapif_guild_castle_dataload(gc->castle_id, index, gc->visibleC); + case 10: return mapif_guild_castle_dataload(gc->castle_id, index, gc->visibleG0); + case 11: return mapif_guild_castle_dataload(gc->castle_id, index, gc->visibleG1); + case 12: return mapif_guild_castle_dataload(gc->castle_id, index, gc->visibleG2); + case 13: return mapif_guild_castle_dataload(gc->castle_id, index, gc->visibleG3); + case 14: return mapif_guild_castle_dataload(gc->castle_id, index, gc->visibleG4); + case 15: return mapif_guild_castle_dataload(gc->castle_id, index, gc->visibleG5); + case 16: return mapif_guild_castle_dataload(gc->castle_id, index, gc->visibleG6); + case 17: return mapif_guild_castle_dataload(gc->castle_id, index, gc->visibleG7); + case 18: return mapif_guild_castle_dataload(gc->castle_id, index, gc->Ghp0); // guardian HP [Valaris] + case 19: return mapif_guild_castle_dataload(gc->castle_id, index, gc->Ghp1); + case 20: return mapif_guild_castle_dataload(gc->castle_id, index, gc->Ghp2); + case 21: return mapif_guild_castle_dataload(gc->castle_id, index, gc->Ghp3); + case 22: return mapif_guild_castle_dataload(gc->castle_id, index, gc->Ghp4); + case 23: return mapif_guild_castle_dataload(gc->castle_id, index, gc->Ghp5); + case 24: return mapif_guild_castle_dataload(gc->castle_id, index, gc->Ghp6); + case 25: return mapif_guild_castle_dataload(gc->castle_id, index, gc->Ghp7); // end additions [Valaris] + + default: + printf("mapif_parse_GuildCastleDataLoad ERROR!! (Not found index=%d)\n", index); + return 0; + } + + return 0; +} + +int mapif_parse_GuildCastleDataSave(int fd, int castle_id, int index, int value) { + struct guild_castle *gc=numdb_search(castle_db, castle_id); + + if (gc == NULL) { + return mapif_guild_castle_datasave(castle_id, index, value); + } + switch(index) { + case 1: + if (gc->guild_id != value) { + int gid = (value) ? value : gc->guild_id; + struct guild *g = numdb_search(guild_db, gid); + inter_log("guild %s (id=%d) %s castle id=%d" RETCODE, + (g) ? g->name : "??", gid, (value) ? "occupy" : "abandon", index); + } + gc->guild_id = value; + break; + case 2: gc->economy = value; break; + case 3: gc->defense = value; break; + case 4: gc->triggerE = value; break; + case 5: gc->triggerD = value; break; + case 6: gc->nextTime = value; break; + case 7: gc->payTime = value; break; + case 8: gc->createTime = value; break; + case 9: gc->visibleC = value; break; + case 10: gc->visibleG0 = value; break; + case 11: gc->visibleG1 = value; break; + case 12: gc->visibleG2 = value; break; + case 13: gc->visibleG3 = value; break; + case 14: gc->visibleG4 = value; break; + case 15: gc->visibleG5 = value; break; + case 16: gc->visibleG6 = value; break; + case 17: gc->visibleG7 = value; break; + case 18: gc->Ghp0 = value; break; // guardian HP [Valaris] + case 19: gc->Ghp1 = value; break; + case 20: gc->Ghp2 = value; break; + case 21: gc->Ghp3 = value; break; + case 22: gc->Ghp4 = value; break; + case 23: gc->Ghp5 = value; break; + case 24: gc->Ghp6 = value; break; + case 25: gc->Ghp7 = value; break; // end additions [Valaris] + default: + printf("mapif_parse_GuildCastleDataSave ERROR!! (Not found index=%d)\n", index); + return 0; + } + + return mapif_guild_castle_datasave(gc->castle_id, index, value); +} + +// ギルドチェック要求 +int mapif_parse_GuildCheck(int fd, int guild_id, int account_id, int char_id) { + return guild_check_conflict(guild_id, account_id, char_id); +} + +// map server からの通信 +// ・1パケットのみ解析すること +// ・パケット長データはinter.cにセットしておくこと +// ・パケット長チェックや、RFIFOSKIPは呼び出し元で行われるので行ってはならない +// ・エラーなら0(false)、そうでないなら1(true)をかえさなければならない +int inter_guild_parse_frommap(int fd) { + switch(RFIFOW(fd,0)) { + case 0x3030: mapif_parse_CreateGuild(fd, RFIFOL(fd,4), RFIFOP(fd,8), (struct guild_member *)RFIFOP(fd,32)); break; + case 0x3031: mapif_parse_GuildInfo(fd, RFIFOL(fd,2)); break; + case 0x3032: mapif_parse_GuildAddMember(fd, RFIFOL(fd,4), (struct guild_member *)RFIFOP(fd,8)); break; + case 0x3034: mapif_parse_GuildLeave(fd, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10), RFIFOB(fd,14), RFIFOP(fd,15)); break; + case 0x3035: mapif_parse_GuildChangeMemberInfoShort(fd, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10), RFIFOB(fd,14), RFIFOW(fd,15), RFIFOW(fd,17)); break; + case 0x3036: mapif_parse_BreakGuild(fd, RFIFOL(fd,2)); break; + case 0x3037: mapif_parse_GuildMessage(fd, RFIFOL(fd,4), RFIFOL(fd,8), RFIFOP(fd,12), RFIFOW(fd,2)-12); break; + case 0x3038: mapif_parse_GuildCheck(fd, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)); break; + case 0x3039: mapif_parse_GuildBasicInfoChange(fd, RFIFOL(fd,4), RFIFOW(fd,8), RFIFOP(fd,10), RFIFOW(fd,2)-10); break; + case 0x303A: mapif_parse_GuildMemberInfoChange(fd, RFIFOL(fd,4), RFIFOL(fd,8), RFIFOL(fd,12), RFIFOW(fd,16), RFIFOP(fd,18), RFIFOW(fd,2)-18); break; + case 0x303B: mapif_parse_GuildPosition(fd, RFIFOL(fd,4), RFIFOL(fd,8), (struct guild_position *)RFIFOP(fd,12)); break; + case 0x303C: mapif_parse_GuildSkillUp(fd, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)); break; + case 0x303D: mapif_parse_GuildAlliance(fd, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10), RFIFOL(fd,14), RFIFOB(fd,18)); break; + case 0x303E: mapif_parse_GuildNotice(fd, RFIFOL(fd,2), RFIFOP(fd,6), RFIFOP(fd,66)); break; + case 0x303F: mapif_parse_GuildEmblem(fd, RFIFOW(fd,2)-12, RFIFOL(fd,4), RFIFOL(fd,8), RFIFOP(fd,12)); break; + case 0x3040: mapif_parse_GuildCastleDataLoad(fd, RFIFOW(fd,2), RFIFOB(fd,4)); break; + case 0x3041: mapif_parse_GuildCastleDataSave(fd, RFIFOW(fd,2), RFIFOB(fd,4), RFIFOL(fd,5)); break; + + default: + return 0; + } + + return 1; +} + +// マップサーバーの接続時処理 +int inter_guild_mapif_init(int fd) { + return mapif_guild_castle_alldataload(fd); +} + +// サーバーから脱退要求(キャラ削除用) +int inter_guild_leave(int guild_id, int account_id, int char_id) { + return mapif_parse_GuildLeave(-1, guild_id, account_id, char_id, 0, "**サーバー命令**"); +} diff --git a/misc/src/char/int_guild.h b/misc/src/char/int_guild.h new file mode 100644 index 0000000..555f5e1 --- /dev/null +++ b/misc/src/char/int_guild.h @@ -0,0 +1,16 @@ +// $Id: int_guild.h,v 1.1.1.1 2004/09/10 17:26:51 MagicalTux Exp $ +#ifndef _INT_GUILD_H_ +#define _INT_GUILD_H_ + +int inter_guild_init(); +int inter_guild_save(); +int inter_guild_parse_frommap(int fd); +struct guild *inter_guild_search(int guild_id); +int inter_guild_mapif_init(int fd); + +int inter_guild_leave(int guild_id,int account_id,int char_id); + +extern char guild_txt[1024]; +extern char castle_txt[1024]; + +#endif diff --git a/misc/src/char/int_party.c b/misc/src/char/int_party.c new file mode 100644 index 0000000..0fd58fa --- /dev/null +++ b/misc/src/char/int_party.c @@ -0,0 +1,595 @@ +// $Id: int_party.c,v 1.1.1.1 2004/09/10 17:26:51 MagicalTux Exp $ +#include "inter.h" +#include "int_party.h" +#include "mmo.h" +#include "char.h" +#include "socket.h" +#include "db.h" +#include "lock.h" +#include +#include +#include + +char party_txt[1024] = "save/party.txt"; + +static struct dbt *party_db; +static int party_newid = 100; + +int mapif_party_broken(int party_id, int flag); +int party_check_empty(struct party *p); +int mapif_parse_PartyLeave(int fd, int party_id, int account_id); + +// パーティデータの文字列への変換 +int inter_party_tostr(char *str, struct party *p) { + int i, len; + + len = sprintf(str, "%d\t%s\t%d,%d\t", p->party_id, p->name, p->exp, p->item); + for(i = 0; i < MAX_PARTY; i++) { + struct party_member *m = &p->member[i]; + len += sprintf(str + len, "%d,%d\t%s\t", m->account_id, m->leader, ((m->account_id > 0) ? m->name : "NoMember")); + } + + return 0; +} + +// パーティデータの文字列からの変換 +int inter_party_fromstr(char *str, struct party *p) { + int i, j; + int tmp_int[16]; + char tmp_str[256]; + + memset(p, 0, sizeof(struct party)); + +// printf("sscanf party main info\n"); + if (sscanf(str, "%d\t%[^\t]\t%d,%d\t", &tmp_int[0], tmp_str, &tmp_int[1], &tmp_int[2]) != 4) + return 1; + + p->party_id = tmp_int[0]; + strcpy(p->name, tmp_str); + p->exp = tmp_int[1]; + p->item = tmp_int[2]; +// printf("%d [%s] %d %d\n", tmp_int[0], tmp_str[0], tmp_int[1], tmp_int[2]); + + for(j = 0; j < 3 && str != NULL; j++) + str = strchr(str + 1, '\t'); + + for(i = 0; i < MAX_PARTY; i++) { + struct party_member *m = &p->member[i]; + if (str == NULL) + return 1; +// printf("sscanf party member info %d\n", i); + + if (sscanf(str + 1, "%d,%d\t%[^\t]\t", &tmp_int[0], &tmp_int[1], tmp_str) != 3) + return 1; + + m->account_id = tmp_int[0]; + m->leader = tmp_int[1]; + strncpy(m->name, tmp_str, sizeof(m->name)); +// printf(" %d %d [%s]\n", tmp_int[0], tmp_int[1], tmp_str); + + for(j = 0; j < 2 && str != NULL; j++) + str = strchr(str + 1, '\t'); + } + + return 0; +} + +// パーティデータのロード +int inter_party_init() { + char line[8192]; + struct party *p; + FILE *fp; + int c = 0; + int i, j; + + party_db = numdb_init(); + + if ((fp = fopen(party_txt, "r")) == NULL) + return 1; + + while(fgets(line, sizeof(line) - 1, fp)) { + j = 0; + if (sscanf(line, "%d\t%%newid%%\n%n", &i, &j) == 1 && j > 0 && party_newid <= i) { + party_newid = i; + continue; + } + + p = calloc(sizeof(struct party), 1); + if (p == NULL){ + printf("int_party: out of memory!\n"); + exit(0); + } + memset(p, 0, sizeof(struct party)); + if (inter_party_fromstr(line, p) == 0 && p->party_id > 0) { + if (p->party_id >= party_newid) + party_newid = p->party_id + 1; + numdb_insert(party_db, p->party_id, p); + party_check_empty(p); + } else { + printf("int_party: broken data [%s] line %d\n", party_txt, c + 1); + free(p); + } + c++; + } + fclose(fp); +// printf("int_party: %s read done (%d parties)\n", party_txt, c); + + return 0; +} + +// パーティーデータのセーブ用 +int inter_party_save_sub(void *key, void *data, va_list ap) { + char line[8192]; + FILE *fp; + + inter_party_tostr(line, (struct party *)data); + fp = va_arg(ap, FILE *); + fprintf(fp, "%s" RETCODE, line); + + return 0; +} + +// パーティーデータのセーブ +int inter_party_save() { + FILE *fp; + int lock; + + if ((fp = lock_fopen(party_txt, &lock)) == NULL) { + printf("int_party: cant write [%s] !!! data is lost !!!\n", party_txt); + return 1; + } + numdb_foreach(party_db, inter_party_save_sub, fp); +// fprintf(fp, "%d\t%%newid%%\n", party_newid); + lock_fclose(fp,party_txt, &lock); +// printf("int_party: %s saved.\n", party_txt); + + return 0; +} + +// パーティ名検索用 +int search_partyname_sub(void *key,void *data,va_list ap) { + struct party *p = (struct party *)data,**dst; + char *str; + + str = va_arg(ap, char *); + dst = va_arg(ap, struct party **); + if (strcmpi(p->name, str) == 0) + *dst = p; + + return 0; +} + +// パーティ名検索 +struct party* search_partyname(char *str) { + struct party *p = NULL; + numdb_foreach(party_db, search_partyname_sub, str, &p); + + return p; +} + +// EXP公平分配できるかチェック +int party_check_exp_share(struct party *p) { + int i; + int maxlv = 0, minlv = 0x7fffffff; + + for(i = 0; i < MAX_PARTY; i++) { + int lv = p->member[i].lv; + if (p->member[i].online) { + if (lv < minlv) + minlv = lv; + if (maxlv < lv) + maxlv = lv; + } + } + + return (maxlv == 0 || maxlv-minlv <= party_share_level); +} + +// パーティが空かどうかチェック +int party_check_empty(struct party *p) { + int i; + +// printf("party check empty %08X\n", (int)p); + for(i = 0; i < MAX_PARTY; i++) { +// printf("%d acc=%d\n", i, p->member[i].account_id); + if (p->member[i].account_id > 0) { + return 0; + } + } + // 誰もいないので解散 + mapif_party_broken(p->party_id, 0); + numdb_erase(party_db, p->party_id); + free(p); + + return 1; +} + +// キャラの競合がないかチェック用 +int party_check_conflict_sub(void *key, void *data, va_list ap) { + struct party *p = (struct party *)data; + int party_id, account_id, i; + char *nick; + + party_id=va_arg(ap, int); + account_id=va_arg(ap, int); + nick=va_arg(ap, char *); + + if (p->party_id == party_id) // 本来の所属なので問題なし + return 0; + + for(i = 0; i < MAX_PARTY; i++) { + if (p->member[i].account_id == account_id && strcmp(p->member[i].name, nick) == 0) { + // 別のパーティに偽の所属データがあるので脱退 + printf("int_party: party conflict! %d %d %d\n", account_id, party_id, p->party_id); + mapif_parse_PartyLeave(-1, p->party_id, account_id); + } + } + + return 0; +} + +// キャラの競合がないかチェック +int party_check_conflict(int party_id, int account_id, char *nick) { + numdb_foreach(party_db, party_check_conflict_sub, party_id, account_id, nick); + + return 0; +} + +//------------------------------------------------------------------- +// map serverへの通信 + +// パーティ作成可否 +int mapif_party_created(int fd,int account_id, struct party *p) { + WFIFOW(fd,0) = 0x3820; + WFIFOL(fd,2) = account_id; + if (p != NULL) { + WFIFOB(fd,6) = 0; + WFIFOL(fd,7) = p->party_id; + memcpy(WFIFOP(fd,11), p->name, 24); + printf("int_party: created! %d %s\n", p->party_id, p->name); + } else { + WFIFOB(fd,6) = 1; + WFIFOL(fd,7) = 0; + memcpy(WFIFOP(fd,11), "error", 24); + } + WFIFOSET(fd,35); + + return 0; +} + +// パーティ情報見つからず +int mapif_party_noinfo(int fd, int party_id) { + WFIFOW(fd,0) = 0x3821; + WFIFOW(fd,2) = 8; + WFIFOL(fd,4) = party_id; + WFIFOSET(fd,8); + printf("int_party: info not found %d\n", party_id); + + return 0; +} + +// パーティ情報まとめ送り +int mapif_party_info(int fd, struct party *p) { + unsigned char buf[4 + sizeof(struct party)]; + + WBUFW(buf,0) = 0x3821; + memcpy(buf + 4, p, sizeof(struct party)); + WBUFW(buf,2) = 4 + sizeof(struct party); + if (fd < 0) + mapif_sendall(buf, WBUFW(buf,2)); + else + mapif_send(fd, buf, WBUFW(buf,2)); +// printf("int_party: info %d %s\n", p->party_id, p->name); + + return 0; +} + +// パーティメンバ追加可否 +int mapif_party_memberadded(int fd, int party_id, int account_id, int flag) { + WFIFOW(fd,0) = 0x3822; + WFIFOL(fd,2) = party_id; + WFIFOL(fd,6) = account_id; + WFIFOB(fd,10) = flag; + WFIFOSET(fd,11); + + return 0; +} + +// パーティ設定変更通知 +int mapif_party_optionchanged(int fd,struct party *p, int account_id, int flag) { + unsigned char buf[15]; + + WBUFW(buf,0) = 0x3823; + WBUFL(buf,2) = p->party_id; + WBUFL(buf,6) = account_id; + WBUFW(buf,10) = p->exp; + WBUFW(buf,12) = p->item; + WBUFB(buf,14) = flag; + if (flag == 0) + mapif_sendall(buf, 15); + else + mapif_send(fd, buf, 15); + printf("int_party: option changed %d %d %d %d %d\n", p->party_id, account_id, p->exp, p->item, flag); + + return 0; +} + +// パーティ脱退通知 +int mapif_party_leaved(int party_id,int account_id, char *name) { + unsigned char buf[34]; + + WBUFW(buf,0) = 0x3824; + WBUFL(buf,2) = party_id; + WBUFL(buf,6) = account_id; + memcpy(WBUFP(buf,10), name, 24); + mapif_sendall(buf, 34); + printf("int_party: party leaved %d %d %s\n", party_id, account_id, name); + + return 0; +} + +// パーティマップ更新通知 +int mapif_party_membermoved(struct party *p, int idx) { + unsigned char buf[29]; + + WBUFW(buf,0) = 0x3825; + WBUFL(buf,2) = p->party_id; + WBUFL(buf,6) = p->member[idx].account_id; + memcpy(WBUFP(buf,10), p->member[idx].map, 16); + WBUFB(buf,26) = p->member[idx].online; + WBUFW(buf,27) = p->member[idx].lv; + mapif_sendall(buf, 29); + + return 0; +} + +// パーティ解散通知 +int mapif_party_broken(int party_id, int flag) { + unsigned char buf[7]; + WBUFW(buf,0) = 0x3826; + WBUFL(buf,2) = party_id; + WBUFB(buf,6) = flag; + mapif_sendall(buf, 7); + printf("int_party: broken %d\n", party_id); + + return 0; +} + +// パーティ内発言 +int mapif_party_message(int party_id, int account_id, char *mes, int len) { + unsigned char buf[len+12]; + + WBUFW(buf,0) = 0x3827; + WBUFW(buf,2) = len + 12; + WBUFL(buf,4) = party_id; + WBUFL(buf,8) = account_id; + memcpy(WBUFP(buf,12), mes, len); + mapif_sendall(buf,len + 12); + + return 0; +} + +//------------------------------------------------------------------- +// map serverからの通信 + + +// パーティ +int mapif_parse_CreateParty(int fd, int account_id, char *name, char *nick, char *map, int lv) { + struct party *p; + int i; + + for(i = 0; i < 24 && name[i]; i++) { + if (!(name[i] & 0xe0) || name[i] == 0x7f) { + printf("int_party: illegal party name [%s]\n", name); + mapif_party_created(fd, account_id, NULL); + return 0; + } + } + + if ((p = search_partyname(name)) != NULL) { + printf("int_party: same name party exists [%s]\n", name); + mapif_party_created(fd, account_id, NULL); + return 0; + } + p = calloc(sizeof(struct party), 1); + if (p == NULL) { + printf("int_party: out of memory !\n"); + mapif_party_created(fd,account_id,NULL); + return 0; + } + memset(p, 0, sizeof(struct party)); + p->party_id = party_newid++; + memcpy(p->name, name, 24); + p->exp = 0; + p->item = 0; + p->member[0].account_id = account_id; + memcpy(p->member[0].name, nick, 24); + memcpy(p->member[0].map, map, 16); + p->member[0].leader = 1; + p->member[0].online = 1; + p->member[0].lv = lv; + + numdb_insert(party_db, p->party_id, p); + + mapif_party_created(fd, account_id, p); + mapif_party_info(fd, p); + + return 0; +} + +// パーティ情報要求 +int mapif_parse_PartyInfo(int fd, int party_id) { + struct party *p; + + p = numdb_search(party_db, party_id); + if (p != NULL) + mapif_party_info(fd, p); + else + mapif_party_noinfo(fd, party_id); + + return 0; +} + +// パーティ追加要求 +int mapif_parse_PartyAddMember(int fd, int party_id, int account_id, char *nick, char *map, int lv) { + struct party *p; + int i; + + p = numdb_search(party_db, party_id); + if (p == NULL) { + mapif_party_memberadded(fd, party_id, account_id, 1); + return 0; + } + + for(i = 0; i < MAX_PARTY; i++) { + if (p->member[i].account_id == 0) { + int flag = 0; + + p->member[i].account_id = account_id; + memcpy(p->member[i].name, nick, 24); + memcpy(p->member[i].map, map, 16); + p->member[i].leader = 0; + p->member[i].online = 1; + p->member[i].lv = lv; + mapif_party_memberadded(fd, party_id, account_id, 0); + mapif_party_info(-1, p); + + if (p->exp > 0 && !party_check_exp_share(p)) { + p->exp = 0; + flag = 0x01; + } + if (flag) + mapif_party_optionchanged(fd, p, 0, 0); + return 0; + } + } + mapif_party_memberadded(fd, party_id, account_id, 1); + + return 0; +} + +// パーティー設定変更要求 +int mapif_parse_PartyChangeOption(int fd, int party_id, int account_id, int exp, int item) { + struct party *p; + int flag = 0; + + p = numdb_search(party_db, party_id); + if (p == NULL) + return 0; + + p->exp = exp; + if (exp>0 && !party_check_exp_share(p)) { + flag |= 0x01; + p->exp = 0; + } + + p->item = item; + + mapif_party_optionchanged(fd, p, account_id, flag); + return 0; +} + +// パーティ脱退要求 +int mapif_parse_PartyLeave(int fd, int party_id, int account_id) { + struct party *p; + int i; + + p = numdb_search(party_db, party_id); + if (p != NULL) { + for(i = 0; i < MAX_PARTY; i++) { + if (p->member[i].account_id == account_id) { + mapif_party_leaved(party_id, account_id, p->member[i].name); + + memset(&p->member[i], 0, sizeof(struct party_member)); + if (party_check_empty(p) == 0) + mapif_party_info(-1, p);// まだ人がいるのでデータ送信 + return 0; + } + } + } + + return 0; +} + +// パーティマップ更新要求 +int mapif_parse_PartyChangeMap(int fd, int party_id, int account_id, char *map, int online, int lv) { + struct party *p; + int i; + + p = numdb_search(party_db, party_id); + if (p == NULL) + return 0; + + for(i = 0; i < MAX_PARTY; i++) { + if (p->member[i].account_id == account_id) { + int flag = 0; + + memcpy(p->member[i].map, map, 16); + p->member[i].online = online; + p->member[i].lv = lv; + mapif_party_membermoved(p, i); + + if (p->exp > 0 && !party_check_exp_share(p)) { + p->exp = 0; + flag = 1; + } + if (flag) + mapif_party_optionchanged(fd, p, 0, 0); + break; + } + } + + return 0; +} + +// パーティ解散要求 +int mapif_parse_BreakParty(int fd, int party_id) { + struct party *p; + + p = numdb_search(party_db, party_id); + if (p == NULL) + return 0; + + numdb_erase(party_db, party_id); + mapif_party_broken(fd, party_id); + + return 0; +} + +// パーティメッセージ送信 +int mapif_parse_PartyMessage(int fd, int party_id, int account_id, char *mes, int len) { + return mapif_party_message(party_id, account_id, mes, len); +} +// パーティチェック要求 +int mapif_parse_PartyCheck(int fd, int party_id, int account_id, char *nick) { + return party_check_conflict(party_id, account_id, nick); +} + +// map server からの通信 +// ・1パケットのみ解析すること +// ・パケット長データはinter.cにセットしておくこと +// ・パケット長チェックや、RFIFOSKIPは呼び出し元で行われるので行ってはならない +// ・エラーなら0(false)、そうでないなら1(true)をかえさなければならない +int inter_party_parse_frommap(int fd) { + switch(RFIFOW(fd,0)) { + case 0x3020: mapif_parse_CreateParty(fd, RFIFOL(fd,2), RFIFOP(fd,6), RFIFOP(fd,30), RFIFOP(fd,54), RFIFOW(fd,70)); break; + case 0x3021: mapif_parse_PartyInfo(fd, RFIFOL(fd,2)); break; + case 0x3022: mapif_parse_PartyAddMember(fd, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOP(fd,10), RFIFOP(fd,34), RFIFOW(fd,50)); break; + case 0x3023: mapif_parse_PartyChangeOption(fd, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOW(fd,10), RFIFOW(fd,12)); break; + case 0x3024: mapif_parse_PartyLeave(fd, RFIFOL(fd,2), RFIFOL(fd,6)); break; + case 0x3025: mapif_parse_PartyChangeMap(fd, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOP(fd,10), RFIFOB(fd,26), RFIFOW(fd,27)); break; + case 0x3026: mapif_parse_BreakParty(fd, RFIFOL(fd,2)); break; + case 0x3027: mapif_parse_PartyMessage(fd, RFIFOL(fd,4), RFIFOL(fd,8), RFIFOP(fd,12), RFIFOW(fd,2)-12); break; + case 0x3028: mapif_parse_PartyCheck(fd, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOP(fd,10)); break; + default: + return 0; + } + + return 1; +} + +// サーバーから脱退要求(キャラ削除用) +int inter_party_leave(int party_id, int account_id) { + return mapif_parse_PartyLeave(-1, party_id, account_id); +} + diff --git a/misc/src/char/int_party.h b/misc/src/char/int_party.h new file mode 100644 index 0000000..b265b4c --- /dev/null +++ b/misc/src/char/int_party.h @@ -0,0 +1,14 @@ +// $Id: int_party.h,v 1.1.1.1 2004/09/10 17:26:51 MagicalTux Exp $ +#ifndef _INT_PARTY_H_ +#define _INT_PARTY_H_ + +int inter_party_init(); +int inter_party_save(); + +int inter_party_parse_frommap(int fd); + +int inter_party_leave(int party_id,int account_id); + +extern char party_txt[1024]; + +#endif diff --git a/misc/src/char/int_pet.c b/misc/src/char/int_pet.c new file mode 100644 index 0000000..cff1e43 --- /dev/null +++ b/misc/src/char/int_pet.c @@ -0,0 +1,364 @@ +// $Id: int_pet.c,v 1.1.1.1 2004/09/10 17:26:51 MagicalTux Exp $ +#include "inter.h" +#include "int_pet.h" +#include "mmo.h" +#include "char.h" +#include "socket.h" +#include "db.h" +#include "lock.h" +#include +#include +#include + + +char pet_txt[1024]="save/pet.txt"; + +static struct dbt *pet_db; +static int pet_newid = 100; + +int inter_pet_tostr(char *str,struct s_pet *p) +{ + int len; + + if(p->hungry < 0) + p->hungry = 0; + else if(p->hungry > 100) + p->hungry = 100; + if(p->intimate < 0) + p->intimate = 0; + else if(p->intimate > 1000) + p->intimate = 1000; + + len=sprintf(str,"%d,%d,%s\t%d,%d,%d,%d,%d,%d,%d,%d,%d", + p->pet_id,p->class,p->name,p->account_id,p->char_id,p->level,p->egg_id, + p->equip,p->intimate,p->hungry,p->rename_flag,p->incuvate); + + return 0; +} + +int inter_pet_fromstr(char *str,struct s_pet *p) +{ + int s; + int tmp_int[16]; + char tmp_str[256]; + + memset(p,0,sizeof(struct s_pet)); + +// printf("sscanf pet main info\n"); + s=sscanf(str,"%d,%d,%[^\t]\t%d,%d,%d,%d,%d,%d,%d,%d,%d",&tmp_int[0],&tmp_int[1],tmp_str,&tmp_int[2], + &tmp_int[3],&tmp_int[4],&tmp_int[5],&tmp_int[6],&tmp_int[7],&tmp_int[8],&tmp_int[9],&tmp_int[10]); + + if(s!=12) + return 1; + + p->pet_id = tmp_int[0]; + p->class = tmp_int[1]; + memcpy(p->name,tmp_str,24); + p->account_id = tmp_int[2]; + p->char_id = tmp_int[3]; + p->level = tmp_int[4]; + p->egg_id = tmp_int[5]; + p->equip = tmp_int[6]; + p->intimate = tmp_int[7]; + p->hungry = tmp_int[8]; + p->rename_flag = tmp_int[9]; + p->incuvate = tmp_int[10]; + + if(p->hungry < 0) + p->hungry = 0; + else if(p->hungry > 100) + p->hungry = 100; + if(p->intimate < 0) + p->intimate = 0; + else if(p->intimate > 1000) + p->intimate = 1000; + + return 0; +} + +int inter_pet_init() +{ + char line[8192]; + struct s_pet *p; + FILE *fp; + int c=0; + + pet_db=numdb_init(); + + if( (fp=fopen(pet_txt,"r"))==NULL ) + return 1; + while(fgets(line,sizeof(line),fp)){ + p=calloc(sizeof(struct s_pet), 1); + if(p==NULL){ + printf("int_pet: out of memory!\n"); + exit(0); + } + memset(p,0,sizeof(struct s_pet)); + if(inter_pet_fromstr(line,p)==0 && p->pet_id>0){ + if( p->pet_id >= pet_newid) + pet_newid=p->pet_id+1; + numdb_insert(pet_db,p->pet_id,p); + }else{ + printf("int_pet: broken data [%s] line %d\n",pet_txt,c); + free(p); + } + c++; + } + fclose(fp); +// printf("int_pet: %s read done (%d pets)\n",pet_txt,c); + return 0; +} + +int inter_pet_save_sub(void *key,void *data,va_list ap) +{ + char line[8192]; + FILE *fp; + inter_pet_tostr(line,(struct s_pet *)data); + fp=va_arg(ap,FILE *); + fprintf(fp,"%s" RETCODE,line); + return 0; +} + +int inter_pet_save() +{ + FILE *fp; + int lock; + if( (fp=lock_fopen(pet_txt,&lock))==NULL ){ + printf("int_pet: cant write [%s] !!! data is lost !!!\n",pet_txt); + return 1; + } + numdb_foreach(pet_db,inter_pet_save_sub,fp); + lock_fclose(fp,pet_txt,&lock); +// printf("int_pet: %s saved.\n",pet_txt); + return 0; +} + +int inter_pet_delete(int pet_id) +{ + struct s_pet *p; + p = numdb_search(pet_db,pet_id); + if( p == NULL) + return 1; + else { + numdb_erase(pet_db,pet_id); + printf("pet_id: %d deleted\n",pet_id); + } + return 0; +} + +int mapif_pet_created(int fd,int account_id,struct s_pet *p) +{ + WFIFOW(fd,0)=0x3880; + WFIFOL(fd,2)=account_id; + if(p!=NULL){ + WFIFOB(fd,6)=0; + WFIFOL(fd,7)=p->pet_id; + printf("int_pet: created! %d %s\n",p->pet_id,p->name); + }else{ + WFIFOB(fd,6)=1; + WFIFOL(fd,7)=0; + } + WFIFOSET(fd,11); + + return 0; +} + +int mapif_pet_info(int fd,int account_id,struct s_pet *p) +{ + WFIFOW(fd,0)=0x3881; + WFIFOW(fd,2)=sizeof(struct s_pet) + 9; + WFIFOL(fd,4)=account_id; + WFIFOB(fd,8)=0; + memcpy(WFIFOP(fd,9),p,sizeof(struct s_pet)); + WFIFOSET(fd,WFIFOW(fd,2)); + + return 0; +} + +int mapif_pet_noinfo(int fd,int account_id) +{ + WFIFOW(fd,0)=0x3881; + WFIFOW(fd,2)=sizeof(struct s_pet) + 9; + WFIFOL(fd,4)=account_id; + WFIFOB(fd,8)=1; + memset(WFIFOP(fd,9),0,sizeof(struct s_pet)); + WFIFOSET(fd,WFIFOW(fd,2)); + + return 0; +} + +int mapif_save_pet_ack(int fd,int account_id,int flag) +{ + WFIFOW(fd,0)=0x3882; + WFIFOL(fd,2)=account_id; + WFIFOB(fd,6)=flag; + WFIFOSET(fd,7); + + return 0; +} + +int mapif_delete_pet_ack(int fd,int flag) +{ + WFIFOW(fd,0)=0x3883; + WFIFOB(fd,2)=flag; + WFIFOSET(fd,3); + + return 0; +} + +int mapif_create_pet(int fd,int account_id,int char_id,short pet_class,short pet_lv,short pet_egg_id, + short pet_equip,short intimate,short hungry,char rename_flag,char incuvate,char *pet_name) +{ + struct s_pet *p; + p=malloc(sizeof(struct s_pet)); + if(p==NULL){ + printf("int_pet: out of memory !\n"); + mapif_pet_created(fd,account_id,NULL); + return 0; + } + memset(p,0,sizeof(struct s_pet)); + p->pet_id = pet_newid++; + memcpy(p->name,pet_name,24); + if(incuvate == 1) + p->account_id = p->char_id = 0; + else { + p->account_id = account_id; + p->char_id = char_id; + } + p->class = pet_class; + p->level = pet_lv; + p->egg_id = pet_egg_id; + p->equip = pet_equip; + p->intimate = intimate; + p->hungry = hungry; + p->rename_flag = rename_flag; + p->incuvate = incuvate; + + if(p->hungry < 0) + p->hungry = 0; + else if(p->hungry > 100) + p->hungry = 100; + if(p->intimate < 0) + p->intimate = 0; + else if(p->intimate > 1000) + p->intimate = 1000; + + numdb_insert(pet_db,p->pet_id,p); + + mapif_pet_created(fd,account_id,p); + + return 0; +} + +int mapif_load_pet(int fd,int account_id,int char_id,int pet_id) +{ + struct s_pet *p; + p=numdb_search(pet_db,pet_id); + if(p!=NULL) { + if(p->incuvate == 1) { + p->account_id = p->char_id = 0; + mapif_pet_info(fd,account_id,p); + } + else if(account_id == p->account_id && char_id == p->char_id) + mapif_pet_info(fd,account_id,p); + else + mapif_pet_noinfo(fd,account_id); + } + else + mapif_pet_noinfo(fd,account_id); + + return 0; +} + +int mapif_save_pet(int fd,int account_id,struct s_pet *data) +{ + struct s_pet *p; + int pet_id; + int len=RFIFOW(fd,2); + if(sizeof(struct s_pet)!=len-8) { + printf("inter pet: data size error %d %d\n",sizeof(struct s_pet),len-8); + } + else{ + pet_id = data->pet_id; + p=numdb_search(pet_db,pet_id); + if(p == NULL) { + p=malloc(sizeof(struct s_pet)); + if(p==NULL){ + printf("int_pet: out of memory !\n"); + mapif_save_pet_ack(fd,account_id,1); + return 0; + } + memset(p,0,sizeof(struct s_pet)); + p->pet_id = data->pet_id; + if(p->pet_id == 0) + data->pet_id = p->pet_id = pet_newid++; + numdb_insert(pet_db,p->pet_id,p); + } + if(data->hungry < 0) + data->hungry = 0; + else if(data->hungry > 100) + data->hungry = 100; + if(data->intimate < 0) + data->intimate = 0; + else if(data->intimate > 1000) + data->intimate = 1000; + memcpy(p,data,sizeof(struct s_pet)); + if(p->incuvate == 1) + p->account_id = p->char_id = 0; + + mapif_save_pet_ack(fd,account_id,0); + } + + return 0; +} + +int mapif_delete_pet(int fd,int pet_id) +{ + mapif_delete_pet_ack(fd,inter_pet_delete(pet_id)); + + return 0; +} + +int mapif_parse_CreatePet(int fd) +{ + mapif_create_pet(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOW(fd,10),RFIFOW(fd,12),RFIFOW(fd,14),RFIFOW(fd,16),RFIFOL(fd,18), + RFIFOL(fd,20),RFIFOB(fd,22),RFIFOB(fd,23),RFIFOP(fd,24)); + return 0; +} + +int mapif_parse_LoadPet(int fd) +{ + mapif_load_pet(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10)); + return 0; +} + +int mapif_parse_SavePet(int fd) +{ + mapif_save_pet(fd,RFIFOL(fd,4),(struct s_pet *)RFIFOP(fd,8)); + return 0; +} + +int mapif_parse_DeletePet(int fd) +{ + mapif_delete_pet(fd,RFIFOL(fd,2)); + return 0; +} + +// map server からの通信 +// ・1パケットのみ解析すること +// ・パケット長データはinter.cにセットしておくこと +// ・パケット長チェックや、RFIFOSKIPは呼び出し元で行われるので行ってはならない +// ・エラーなら0(false)、そうでないなら1(true)をかえさなければならない +int inter_pet_parse_frommap(int fd) +{ + switch(RFIFOW(fd,0)){ + case 0x3080: mapif_parse_CreatePet(fd); break; + case 0x3081: mapif_parse_LoadPet(fd); break; + case 0x3082: mapif_parse_SavePet(fd); break; + case 0x3083: mapif_parse_DeletePet(fd); break; + default: + return 0; + } + return 1; +} + diff --git a/misc/src/char/int_pet.h b/misc/src/char/int_pet.h new file mode 100644 index 0000000..993f913 --- /dev/null +++ b/misc/src/char/int_pet.h @@ -0,0 +1,13 @@ +// $Id: int_pet.h,v 1.1.1.1 2004/09/10 17:26:51 MagicalTux Exp $ +#ifndef _INT_PET_H_ +#define _INT_PET_H_ + +int inter_pet_init(); +int inter_pet_save(); +int inter_pet_delete(int pet_id); + +int inter_pet_parse_frommap(int fd); + +extern char pet_txt[1024]; + +#endif diff --git a/misc/src/char/int_storage.c b/misc/src/char/int_storage.c new file mode 100644 index 0000000..8b656fc --- /dev/null +++ b/misc/src/char/int_storage.c @@ -0,0 +1,504 @@ +// $Id: int_storage.c,v 1.1.1.1 2004/09/10 17:26:51 MagicalTux Exp $ +#include "inter.h" +#include "int_storage.h" +#include "int_pet.h" +#include "int_guild.h" +#include "mmo.h" +#include "char.h" +#include "socket.h" +#include "db.h" +#include "lock.h" + +#include +#include + +// ファイル名のデフォルト +// inter_config_read()で再設定される +char storage_txt[1024]="save/storage.txt"; +char guild_storage_txt[1024]="save/g_storage.txt"; + +static struct dbt *storage_db; +static struct dbt *guild_storage_db; + +// 倉庫データを文字列に変換 +int storage_tostr(char *str,struct storage *p) +{ + int i,f=0; + char *str_p = str; + str_p += sprintf(str_p,"%d,%d\t",p->account_id,p->storage_amount); + + for(i=0;istorage[i].nameid) && (p->storage[i].amount) ){ + str_p += sprintf(str_p,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d ", + p->storage[i].id,p->storage[i].nameid,p->storage[i].amount,p->storage[i].equip, + p->storage[i].identify,p->storage[i].refine,p->storage[i].attribute, + p->storage[i].card[0],p->storage[i].card[1],p->storage[i].card[2],p->storage[i].card[3],p->storage[i].broken); + f++; + } + + *(str_p++)='\t'; + + *str_p='\0'; + if(!f) + str[0]=0; + return 0; +} + +// 文字列を倉庫データに変換 +int storage_fromstr(char *str,struct storage *p) +{ + int tmp_int[256]; + int set,next,len,i; + + set=sscanf(str,"%d,%d%n",&tmp_int[0],&tmp_int[1],&next); + p->storage_amount=tmp_int[1]; + + if(set!=2) + return 1; + if(str[next]=='\n' || str[next]=='\r') + return 0; + next++; + for(i=0;str[next] && str[next]!='\t';i++){ + if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], + &tmp_int[4], &tmp_int[5], &tmp_int[6], + &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &tmp_int[11], &len) == 12) { + p->storage[i].id = tmp_int[0]; + p->storage[i].nameid = tmp_int[1]; + p->storage[i].amount = tmp_int[2]; + p->storage[i].equip = tmp_int[3]; + p->storage[i].identify = tmp_int[4]; + p->storage[i].refine = tmp_int[5]; + p->storage[i].attribute = tmp_int[6]; + p->storage[i].card[0] = tmp_int[7]; + p->storage[i].card[1] = tmp_int[8]; + p->storage[i].card[2] = tmp_int[9]; + p->storage[i].card[3] = tmp_int[10]; + p->storage[i].broken = tmp_int[11]; + next += len; + if (str[next] == ' ') + next++; + } + + else if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], + &tmp_int[4], &tmp_int[5], &tmp_int[6], + &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &len) == 11) { + p->storage[i].id = tmp_int[0]; + p->storage[i].nameid = tmp_int[1]; + p->storage[i].amount = tmp_int[2]; + p->storage[i].equip = tmp_int[3]; + p->storage[i].identify = tmp_int[4]; + p->storage[i].refine = tmp_int[5]; + p->storage[i].attribute = tmp_int[6]; + p->storage[i].card[0] = tmp_int[7]; + p->storage[i].card[1] = tmp_int[8]; + p->storage[i].card[2] = tmp_int[9]; + p->storage[i].card[3] = tmp_int[10]; + p->storage[i].broken = 0; + next += len; + if (str[next] == ' ') + next++; + } + + else return 1; + } + return 0; +} + +int guild_storage_tostr(char *str,struct guild_storage *p) +{ + int i,f=0; + char *str_p = str; + str_p+=sprintf(str,"%d,%d\t",p->guild_id,p->storage_amount); + + for(i=0;istorage[i].nameid) && (p->storage[i].amount) ){ + str_p += sprintf(str_p,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d ", + p->storage[i].id,p->storage[i].nameid,p->storage[i].amount,p->storage[i].equip, + p->storage[i].identify,p->storage[i].refine,p->storage[i].attribute, + p->storage[i].card[0],p->storage[i].card[1],p->storage[i].card[2],p->storage[i].card[3],p->storage[i].broken); + f++; + } + + *(str_p++)='\t'; + + *str_p='\0'; + if(!f) + str[0]=0; + return 0; +} + +int guild_storage_fromstr(char *str,struct guild_storage *p) +{ + int tmp_int[256]; + int set,next,len,i; + + set=sscanf(str,"%d,%d%n",&tmp_int[0],&tmp_int[1],&next); + p->storage_amount=tmp_int[1]; + + if(set!=2) + return 1; + if(str[next]=='\n' || str[next]=='\r') + return 0; + next++; + for(i=0;str[next] && str[next]!='\t';i++){ + if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], + &tmp_int[4], &tmp_int[5], &tmp_int[6], + &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &tmp_int[11], &len) == 12) { + p->storage[i].id = tmp_int[0]; + p->storage[i].nameid = tmp_int[1]; + p->storage[i].amount = tmp_int[2]; + p->storage[i].equip = tmp_int[3]; + p->storage[i].identify = tmp_int[4]; + p->storage[i].refine = tmp_int[5]; + p->storage[i].attribute = tmp_int[6]; + p->storage[i].card[0] = tmp_int[7]; + p->storage[i].card[1] = tmp_int[8]; + p->storage[i].card[2] = tmp_int[9]; + p->storage[i].card[3] = tmp_int[10]; + p->storage[i].broken = tmp_int[11]; + next += len; + if (str[next] == ' ') + next++; + } + + else if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d%n", + &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3], + &tmp_int[4], &tmp_int[5], &tmp_int[6], + &tmp_int[7], &tmp_int[8], &tmp_int[9], &tmp_int[10], &len) == 11) { + p->storage[i].id = tmp_int[0]; + p->storage[i].nameid = tmp_int[1]; + p->storage[i].amount = tmp_int[2]; + p->storage[i].equip = tmp_int[3]; + p->storage[i].identify = tmp_int[4]; + p->storage[i].refine = tmp_int[5]; + p->storage[i].attribute = tmp_int[6]; + p->storage[i].card[0] = tmp_int[7]; + p->storage[i].card[1] = tmp_int[8]; + p->storage[i].card[2] = tmp_int[9]; + p->storage[i].card[3] = tmp_int[10]; + p->storage[i].broken = 0; + next += len; + if (str[next] == ' ') + next++; + } + + else return 1; + } + return 0; +} + +// アカウントから倉庫データインデックスを得る(新規倉庫追加可能) +struct storage *account2storage(int account_id) +{ + struct storage *s; + s=numdb_search(storage_db,account_id); + if(s == NULL) { + s = calloc(sizeof(struct storage), 1); + if(s==NULL){ + printf("int_storage: out of memory!\n"); + exit(0); + } + memset(s,0,sizeof(struct storage)); + s->account_id=account_id; + numdb_insert(storage_db,s->account_id,s); + } + return s; +} + +struct guild_storage *guild2storage(int guild_id) +{ + struct guild_storage *gs = NULL; + if(inter_guild_search(guild_id) != NULL) { + gs=numdb_search(guild_storage_db,guild_id); + if(gs == NULL) { + gs = calloc(sizeof(struct guild_storage), 1); + if(gs==NULL){ + printf("int_storage: out of memory!\n"); + exit(0); + } + memset(gs,0,sizeof(struct guild_storage)); + gs->guild_id=guild_id; + numdb_insert(guild_storage_db,gs->guild_id,gs); + } + } + return gs; +} + +//--------------------------------------------------------- +// 倉庫データを読み込む +int inter_storage_init() +{ + char line[65536]; + int c=0,tmp_int; + struct storage *s; + struct guild_storage *gs; + FILE *fp; + + storage_db = numdb_init(); + + fp=fopen(storage_txt,"r"); + if(fp==NULL){ + printf("cant't read : %s\n",storage_txt); + return 1; + } + while(fgets(line,65535,fp)){ + sscanf(line,"%d",&tmp_int); + s=calloc(sizeof(struct storage), 1); + if(s==NULL){ + printf("int_storage: out of memory!\n"); + exit(0); + } + memset(s,0,sizeof(struct storage)); + s->account_id=tmp_int; + if(s->account_id > 0 && storage_fromstr(line,s) == 0) { + numdb_insert(storage_db,s->account_id,s); + } + else{ + printf("int_storage: broken data [%s] line %d\n",storage_txt,c); + free(s); + } + c++; + } + fclose(fp); + + c = 0; + guild_storage_db = numdb_init(); + + fp=fopen(guild_storage_txt,"r"); + if(fp==NULL){ + printf("cant't read : %s\n",guild_storage_txt); + return 1; + } + while(fgets(line,65535,fp)){ + sscanf(line,"%d",&tmp_int); + gs=calloc(sizeof(struct guild_storage), 1); + if(gs==NULL){ + printf("int_storage: out of memory!\n"); + exit(0); + } + memset(gs,0,sizeof(struct guild_storage)); + gs->guild_id=tmp_int; + if(gs->guild_id > 0 && guild_storage_fromstr(line,gs) == 0) { + numdb_insert(guild_storage_db,gs->guild_id,gs); + } + else{ + printf("int_storage: broken data [%s] line %d\n",guild_storage_txt,c); + free(gs); + } + c++; + } + fclose(fp); + + return 0; +} + +int inter_storage_save_sub(void *key,void *data,va_list ap) +{ + char line[65536]; + FILE *fp; + storage_tostr(line,(struct storage *)data); + fp=va_arg(ap,FILE *); + if(*line) + fprintf(fp,"%s" RETCODE,line); + return 0; +} +//--------------------------------------------------------- +// 倉庫データを書き込む +int inter_storage_save() +{ + FILE *fp; + int lock; + if( (fp=lock_fopen(storage_txt,&lock))==NULL ){ + printf("int_storage: cant write [%s] !!! data is lost !!!\n",storage_txt); + return 1; + } + numdb_foreach(storage_db,inter_storage_save_sub,fp); + lock_fclose(fp,storage_txt,&lock); +// printf("int_storage: %s saved.\n",storage_txt); + return 0; +} + +int inter_guild_storage_save_sub(void *key,void *data,va_list ap) +{ + char line[65536]; + FILE *fp; + if(inter_guild_search(((struct guild_storage *)data)->guild_id) != NULL) { + guild_storage_tostr(line,(struct guild_storage *)data); + fp=va_arg(ap,FILE *); + if(*line) + fprintf(fp,"%s" RETCODE,line); + } + return 0; +} +//--------------------------------------------------------- +// 倉庫データを書き込む +int inter_guild_storage_save() +{ + FILE *fp; + int lock; + if( (fp=lock_fopen(guild_storage_txt,&lock))==NULL ){ + printf("int_storage: cant write [%s] !!! data is lost !!!\n",guild_storage_txt); + return 1; + } + numdb_foreach(guild_storage_db,inter_guild_storage_save_sub,fp); + lock_fclose(fp,guild_storage_txt,&lock); +// printf("int_storage: %s saved.\n",guild_storage_txt); + return 0; +} + +// 倉庫データ削除 +int inter_storage_delete(int account_id) +{ + struct storage *s = numdb_search(storage_db,account_id); + if(s) { + int i; + for(i=0;istorage_amount;i++){ + if(s->storage[i].card[0] == (short)0xff00) + inter_pet_delete(*((long *)(&s->storage[i].card[2]))); + } + numdb_erase(storage_db,account_id); + free(s); + } + return 0; +} + +// ギルド倉庫データ削除 +int inter_guild_storage_delete(int guild_id) +{ + struct guild_storage *gs = numdb_search(guild_storage_db,guild_id); + if(gs) { + int i; + for(i=0;istorage_amount;i++){ + if(gs->storage[i].card[0] == (short)0xff00) + inter_pet_delete(*((long *)(&gs->storage[i].card[2]))); + } + numdb_erase(guild_storage_db,guild_id); + free(gs); + } + return 0; +} + +//--------------------------------------------------------- +// map serverへの通信 + +// 倉庫データの送信 +int mapif_load_storage(int fd,int account_id) +{ + struct storage *s=account2storage(account_id); + WFIFOW(fd,0)=0x3810; + WFIFOW(fd,2)=sizeof(struct storage)+8; + WFIFOL(fd,4)=account_id; + memcpy(WFIFOP(fd,8),s,sizeof(struct storage)); + WFIFOSET(fd,WFIFOW(fd,2)); + return 0; +} +// 倉庫データ保存完了送信 +int mapif_save_storage_ack(int fd,int account_id) +{ + WFIFOW(fd,0)=0x3811; + WFIFOL(fd,2)=account_id; + WFIFOB(fd,6)=0; + WFIFOSET(fd,7); + return 0; +} + +int mapif_load_guild_storage(int fd,int account_id,int guild_id) +{ + struct guild_storage *gs=guild2storage(guild_id); + WFIFOW(fd,0)=0x3818; + if(gs) { + WFIFOW(fd,2)=sizeof(struct guild_storage)+12; + WFIFOL(fd,4)=account_id; + WFIFOL(fd,8)=guild_id; + memcpy(WFIFOP(fd,12),gs,sizeof(struct guild_storage)); + } + else { + WFIFOW(fd,2)=12; + WFIFOL(fd,4)=account_id; + WFIFOL(fd,8)=0; + } + WFIFOSET(fd,WFIFOW(fd,2)); + + return 0; +} +int mapif_save_guild_storage_ack(int fd,int account_id,int guild_id,int fail) +{ + WFIFOW(fd,0)=0x3819; + WFIFOL(fd,2)=account_id; + WFIFOL(fd,6)=guild_id; + WFIFOB(fd,10)=fail; + WFIFOSET(fd,11); + return 0; +} + +//--------------------------------------------------------- +// map serverからの通信 + +// 倉庫データ要求受信 +int mapif_parse_LoadStorage(int fd) +{ + mapif_load_storage(fd,RFIFOL(fd,2)); + return 0; +} +// 倉庫データ受信&保存 +int mapif_parse_SaveStorage(int fd) +{ + struct storage *s; + int account_id=RFIFOL(fd,4); + int len=RFIFOW(fd,2); + if(sizeof(struct storage)!=len-8){ + printf("inter storage: data size error %d %d\n",sizeof(struct storage),len-8); + } + else { + s=account2storage(account_id); + memcpy(s,RFIFOP(fd,8),sizeof(struct storage)); + mapif_save_storage_ack(fd,account_id); + } + return 0; +} + +int mapif_parse_LoadGuildStorage(int fd) +{ + mapif_load_guild_storage(fd,RFIFOL(fd,2),RFIFOL(fd,6)); + return 0; +} +int mapif_parse_SaveGuildStorage(int fd) +{ + struct guild_storage *gs; + int guild_id=RFIFOL(fd,8); + int len=RFIFOW(fd,2); + if(sizeof(struct guild_storage)!=len-12){ + printf("inter storage: data size error %d %d\n",sizeof(struct guild_storage),len-12); + } + else { + gs=guild2storage(guild_id); + if(gs) { + memcpy(gs,RFIFOP(fd,12),sizeof(struct guild_storage)); + mapif_save_guild_storage_ack(fd,RFIFOL(fd,4),guild_id,0); + } + else + mapif_save_guild_storage_ack(fd,RFIFOL(fd,4),guild_id,1); + } + return 0; +} + +// map server からの通信 +// ・1パケットのみ解析すること +// ・パケット長データはinter.cにセットしておくこと +// ・パケット長チェックや、RFIFOSKIPは呼び出し元で行われるので行ってはならない +// ・エラーなら0(false)、そうでないなら1(true)をかえさなければならない +int inter_storage_parse_frommap(int fd) +{ + switch(RFIFOW(fd,0)){ + case 0x3010: mapif_parse_LoadStorage(fd); break; + case 0x3011: mapif_parse_SaveStorage(fd); break; + case 0x3018: mapif_parse_LoadGuildStorage(fd); break; + case 0x3019: mapif_parse_SaveGuildStorage(fd); break; + default: + return 0; + } + return 1; +} diff --git a/misc/src/char/int_storage.h b/misc/src/char/int_storage.h new file mode 100644 index 0000000..d918f5f --- /dev/null +++ b/misc/src/char/int_storage.h @@ -0,0 +1,16 @@ +// $Id: int_storage.h,v 1.1.1.1 2004/09/10 17:26:51 MagicalTux Exp $ +#ifndef _INT_STORAGE_H_ +#define _INT_STORAGE_H_ + +int inter_storage_init(); +int inter_storage_save(); +int inter_guild_storage_save(); +int inter_storage_delete(int account_id); +int inter_guild_storage_delete(int guild_id); + +int inter_storage_parse_frommap(int fd); + +extern char storage_txt[1024]; +extern char guild_storage_txt[1024]; + +#endif diff --git a/misc/src/char/inter.c b/misc/src/char/inter.c new file mode 100644 index 0000000..29ec57b --- /dev/null +++ b/misc/src/char/inter.c @@ -0,0 +1,561 @@ +// $Id: inter.c,v 1.1.1.1 2004/09/10 17:26:51 MagicalTux Exp $ +#include "mmo.h" +#include "char.h" +#include "socket.h" +#include "timer.h" +#include "db.h" +#include +#include + +#include "inter.h" +#include "int_party.h" +#include "int_guild.h" +#include "int_storage.h" +#include "int_pet.h" +#include "lock.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 + +char inter_log_filename[1024] = "log/inter.log"; + +char accreg_txt[1024] = "save/accreg.txt"; +static struct dbt *accreg_db = NULL; + +struct accreg { + int account_id, reg_num; + struct global_reg reg[ACCOUNT_REG_NUM]; +}; + +int party_share_level = 10; + + +// 送信パケット長リスト +int inter_send_packet_length[] = { + -1,-1,27,-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + -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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; +// 受信パケット長リスト +int inter_recv_packet_length[] = { + -1,-1, 7,-1, -1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 6,-1, 0, 0, 0, 0, 0, 0, 10,-1, 0, 0, 0, 0, 0, 0, + 72, 6,52,14, 10,29, 6,-1, 34, 0, 0, 0, 0, 0, 0, 0, + -1, 6,-1, 0, 55,19, 6,-1, 14,-1,-1,-1, 14,19,186,-1, + 5, 9, 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, 0, + 48,14,-1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; + +struct WisData { + int id, fd, count, len; + unsigned long tick; + unsigned char src[24], dst[24], msg[1024]; +}; +static struct dbt * wis_db = NULL; +static int wis_dellist[WISDELLIST_MAX], wis_delnum; + + +//-------------------------------------------------------- + +// アカウント変数を文字列へ変換 +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,%d ", reg->reg[j].str, reg->reg[j].value); + } + + return 0; +} + +// アカウント変数を文字列から変換 +int inter_accreg_fromstr(const char *str, struct accreg *reg) { + int j, v, n; + char buf[128]; + const char *p = str; + + if (sscanf(p, "%d\t%n", ®->account_id, &n ) != 1 || reg->account_id <= 0) + return 1; + + for(j = 0, p += n; j < ACCOUNT_REG_NUM; j++, p += n) { + if (sscanf(p, "%[^,],%d %n", buf, &v, &n) != 2) + break; + memcpy(reg->reg[j].str, buf, 32); + reg->reg[j].value = v; + } + reg->reg_num = j; + + return 0; +} + +// アカウント変数の読み込み +int inter_accreg_init() { + char line[8192]; + FILE *fp; + int c = 0; + struct accreg *reg; + + accreg_db = numdb_init(); + + if( (fp = fopen(accreg_txt, "r")) == NULL) + return 1; + while(fgets(line, sizeof(line)-1, fp)){ + line[sizeof(line)-1] = '\0'; + + reg = calloc(sizeof(struct accreg), 1); + if (reg == NULL) { + printf("inter: accreg: out of memory!\n"); + exit(0); + } + if (inter_accreg_fromstr(line, reg) == 0 && reg->account_id > 0) { + numdb_insert(accreg_db, reg->account_id, reg); + } else { + printf("inter: accreg: broken data [%s] line %d\n", accreg_txt, c); + free(reg); + } + c++; + } + fclose(fp); +// printf("inter: %s read done (%d)\n", accreg_txt, c); + + return 0; +} + +// アカウント変数のセーブ用 +int inter_accreg_save_sub(void *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" RETCODE, line); + } + + return 0; +} + +// アカウント変数のセーブ +int inter_accreg_save() { + FILE *fp; + int lock; + + if ((fp = lock_fopen(accreg_txt,&lock)) == NULL) { + printf("int_accreg: cant write [%s] !!! data is lost !!!\n", accreg_txt); + return 1; + } + numdb_foreach(accreg_db, inter_accreg_save_sub,fp); + lock_fclose(fp, accreg_txt, &lock); +// printf("inter: %s saved.\n", accreg_txt); + + return 0; +} + +//-------------------------------------------------------- + +/*========================================== + * 設定ファイルを読み込む + *------------------------------------------ + */ +int inter_config_read(const char *cfgName) { + char line[1024], w1[1024], w2[1024]; + FILE *fp; + + fp = fopen(cfgName, "r"); + if (fp == NULL) { + printf("file not found: %s\n", cfgName); + return 1; + } + while(fgets(line, sizeof(line) - 1, fp)) { + if (line[0] == '/' && line[1] == '/') + continue; + line[sizeof(line)-1] = '\0'; + + 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, "guild_txt") == 0) { + strncpy(guild_txt, w2, sizeof(guild_txt)); + } else if (strcmpi(w1, "pet_txt") == 0) { + strncpy(pet_txt, w2, sizeof(pet_txt)); + } else if (strcmpi(w1, "castle_txt") == 0) { + strncpy(castle_txt, w2, sizeof(castle_txt)); + } else if (strcmpi(w1, "accreg_txt") == 0) { + strncpy(accreg_txt, w2, sizeof(accreg_txt)); + } else if (strcmpi(w1, "guild_storage_txt") == 0) { + strncpy(guild_storage_txt, w2, sizeof(guild_storage_txt)); + } else if (strcmpi(w1, "party_share_level") == 0) { + party_share_level = atoi(w2); + if (party_share_level < 0) + party_share_level = 0; + } else if (strcmpi(w1, "inter_log_filename") == 0) { + strncpy(inter_log_filename, w2, sizeof(inter_log_filename)); + } else if (strcmpi(w1, "import") == 0) { + inter_config_read(w2); + } + } + fclose(fp); + + return 0; +} + +// ログ書き出し +int inter_log(char *fmt,...) { + FILE *logfp; + va_list ap; + + va_start(ap,fmt); + logfp = fopen(inter_log_filename, "a"); + if (logfp) { + vfprintf(logfp, fmt, ap); + fclose(logfp); + } + va_end(ap); + + return 0; +} + +// セーブ +int inter_save() { + inter_party_save(); + inter_guild_save(); + inter_storage_save(); + inter_guild_storage_save(); + inter_pet_save(); + inter_accreg_save(); + + return 0; +} + +// 初期化 +int inter_init(const char *file) { + inter_config_read(file); + + wis_db = numdb_init(); + + inter_party_init(); + inter_guild_init(); + inter_storage_init(); + inter_pet_init(); + inter_accreg_init(); + + return 0; +} + +// マップサーバー接続 +int inter_mapif_init(int fd) { + inter_guild_mapif_init(fd); + + return 0; +} + +//-------------------------------------------------------- +// sended packets to map-server + +// GMメッセージ送信 +int mapif_GMmessage(unsigned char *mes, int len) { + unsigned char buf[len]; + + WBUFW(buf,0) = 0x3800; + WBUFW(buf,2) = len; + memcpy(WBUFP(buf,4), mes, len - 4); + mapif_sendall(buf, len); +// printf("inter server: GM:%d %s\n", len, mes); + + return 0; +} + +// Wisp/page transmission to all map-server +int mapif_wis_message(struct WisData *wd) { + unsigned char buf[56 + wd->len]; + + WBUFW(buf, 0) = 0x3801; + WBUFW(buf, 2) = 56 + wd->len; + WBUFL(buf, 4) = wd->id; + memcpy(WBUFP(buf, 8), wd->src, 24); + memcpy(WBUFP(buf,32), wd->dst, 24); + memcpy(WBUFP(buf,56), wd->msg, wd->len); + wd->count = mapif_sendall(buf, WBUFW(buf,2)); + + return 0; +} + +// Wisp/page transmission result to map-server +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); +// printf("inter server wis_end: flag: %d\n", flag); + + return 0; +} + +// アカウント変数送信 +int mapif_account_reg(int fd, unsigned char *src) { + unsigned char buf[WBUFW(src,2)]; + + memcpy(WBUFP(buf,0),src,WBUFW(src,2)); + WBUFW(buf, 0) = 0x3804; + mapif_sendallwos(fd, buf, WBUFW(buf,2)); + + return 0; +} + +// アカウント変数要求返信 +int mapif_account_reg_reply(int fd,int account_id) { + struct accreg *reg = numdb_search(accreg_db,account_id); + + WFIFOW(fd,0) = 0x3804; + WFIFOL(fd,4) = account_id; + if (reg == NULL) { + WFIFOW(fd,2) = 8; + } else { + int j, p; + for(j = 0, p = 8; j < reg->reg_num; j++, p += 36) { + memcpy(WFIFOP(fd,p), reg->reg[j].str, 32); + WFIFOL(fd,p+32) = reg->reg[j].value; + } + WFIFOW(fd,2) = p; + } + WFIFOSET(fd,WFIFOW(fd,2)); + + return 0; +} + +//-------------------------------------------------------- + +// Existence check of WISP data +int check_ttl_wisdata_sub(void *key, void *data, va_list ap) { + unsigned long tick; + struct WisData *wd = (struct WisData *)data; + tick = va_arg(ap, unsigned long); + + if (DIFF_TICK(tick, wd->tick) > WISDATA_TTL && wis_delnum < WISDELLIST_MAX) + wis_dellist[wis_delnum++] = wd->id; + + return 0; +} + +int check_ttl_wisdata() { + unsigned long tick = gettick(); + int i; + + do { + wis_delnum = 0; + numdb_foreach(wis_db, check_ttl_wisdata_sub, tick); + for(i = 0; i < wis_delnum; i++) { + struct WisData *wd = numdb_search(wis_db, wis_dellist[i]); + printf("inter: wis data id=%d time out : from %s to %s\n", wd->id, wd->src, wd->dst); + // removed. not send information after a timeout. Just no answer for the player + //mapif_wis_end(wd, 1); // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target + numdb_erase(wis_db, wd->id); + free(wd); + } + } while(wis_delnum >= WISDELLIST_MAX); + + return 0; +} + +//-------------------------------------------------------- +// received packets from map-server + +// GMメッセージ送信 +int mapif_parse_GMmessage(int fd) { + mapif_GMmessage(RFIFOP(fd,4), RFIFOW(fd,2)); + + return 0; +} + +// Wisp/page request to send +int mapif_parse_WisRequest(int fd) { + struct WisData* wd; + static int wisid = 0; + int index; + + if (RFIFOW(fd,2)-52 >= sizeof(wd->msg)) { + printf("inter: Wis message size too long.\n"); + return 0; + } else if (RFIFOW(fd,2)-52 <= 0) { // normaly, impossible, but who knows... + printf("inter: Wis message doesn't exist.\n"); + return 0; + } + + // search if character exists before to ask all map-servers + if ((index = search_character_index(RFIFOP(fd,28))) == -1) { + unsigned char buf[27]; + WBUFW(buf, 0) = 0x3802; + memcpy(WBUFP(buf, 2), RFIFOP(fd, 4), 24); + 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); + // Character exists. So, ask all map-servers + } else { + // to be sure of the correct name, rewrite it + memset(RFIFOP(fd,28), 0, 24); + strncpy(RFIFOP(fd,28), search_character_name(index), 24); + // if source is destination, don't ask other servers. + if (strcmp(RFIFOP(fd,4),RFIFOP(fd,28)) == 0) { + unsigned char buf[27]; + WBUFW(buf, 0) = 0x3802; + memcpy(WBUFP(buf, 2), RFIFOP(fd, 4), 24); + 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 { + + wd = (struct WisData *)calloc(sizeof(struct WisData), 1); + if (wd == NULL){ + printf("inter: WisRequest: out of memory !\n"); + return 0; + } + + // 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), 24); + memcpy(wd->dst, RFIFOP(fd,28), 24); + memcpy(wd->msg, RFIFOP(fd,52), wd->len); + wd->tick = gettick(); + numdb_insert(wis_db, wd->id, wd); + mapif_wis_message(wd); + } + } + + return 0; +} + +// Wisp/page transmission result +int mapif_parse_WisReply(int fd) { + int id = RFIFOL(fd,2), flag = RFIFOB(fd,6); + struct WisData *wd = numdb_search(wis_db, id); + + if (wd == NULL) + 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 + numdb_erase(wis_db, id); + free(wd); + } + + return 0; +} + +// 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) { + unsigned char buf[RFIFOW(fd,2)]; // 0x3003/0x3803 .w .24B .w .?B + + memcpy(WBUFP(buf,0), RFIFOP(fd,0), RFIFOW(fd,2)); + WBUFW(buf, 0) = 0x3803; + mapif_sendall(buf, RFIFOW(fd,2)); + + return 0; +} + +// アカウント変数保存要求 +int mapif_parse_AccReg(int fd) { + int j, p; + struct accreg *reg = numdb_search(accreg_db, RFIFOL(fd,4)); + + if (reg == NULL) { + if ((reg = calloc(sizeof(struct accreg), 1)) == NULL) { + printf("inter: accreg: out of memory !\n"); + exit(0); + } + reg->account_id = RFIFOL(fd,4); + numdb_insert(accreg_db, RFIFOL(fd,4), reg); + } + + for(j = 0, p = 8; j < ACCOUNT_REG_NUM && p < RFIFOW(fd,2); j++, p += 36) { + memcpy(reg->reg[j].str, RFIFOP(fd,p), 32); + reg->reg[j].value = RFIFOL(fd, p + 32); + } + reg->reg_num = j; + + mapif_account_reg(fd, RFIFOP(fd,0)); // 他のMAPサーバーに送信 + + return 0; +} + +// アカウント変数送信要求 +int mapif_parse_AccRegRequest(int fd) { +// printf("mapif: accreg request\n"); + return mapif_account_reg_reply(fd, RFIFOL(fd,2)); +} + +//-------------------------------------------------------- + +// map server からの通信(1パケットのみ解析すること) +// エラーなら0(false)、処理できたなら1、 +// パケット長が足りなければ2をかえさなければならない +int inter_parse_frommap(int fd) { + int cmd = RFIFOW(fd,0); + int len = 0; + + // inter鯖管轄かを調べる + if (cmd < 0x3000 || cmd >= 0x3000 + (sizeof(inter_recv_packet_length) / sizeof(inter_recv_packet_length[0]))) + return 0; + + // パケット長を調べる + if ((len = inter_check_length(fd, inter_recv_packet_length[cmd - 0x3000])) == 0) + return 2; + + switch(cmd) { + case 0x3000: mapif_parse_GMmessage(fd); break; + case 0x3001: mapif_parse_WisRequest(fd); break; + case 0x3002: mapif_parse_WisReply(fd); break; + case 0x3003: mapif_parse_WisToGM(fd); break; + case 0x3004: mapif_parse_AccReg(fd); break; + case 0x3005: mapif_parse_AccRegRequest(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; + return 0; + } + RFIFOSKIP(fd, len); + + return 1; +} + +// RFIFOのパケット長確認 +// 必要パケット長があればパケット長、まだ足りなければ0 +int inter_check_length(int fd, int length) { + if (length == -1) { // 可変パケット長 + if (RFIFOREST(fd) < 4) // パケット長が未着 + return 0; + length = RFIFOW(fd,2); + } + + if (RFIFOREST(fd) < length) // パケットが未着 + return 0; + + return length; +} + diff --git a/misc/src/char/inter.h b/misc/src/char/inter.h new file mode 100644 index 0000000..b004c9b --- /dev/null +++ b/misc/src/char/inter.h @@ -0,0 +1,19 @@ +// $Id: inter.h,v 1.1.1.1 2004/09/10 17:26:51 MagicalTux Exp $ +#ifndef _INTER_H_ +#define _INTER_H_ + +int inter_init(const char *file); +int inter_save(); +int inter_parse_frommap(int fd); +int inter_mapif_init(int fd); + +int inter_check_length(int fd,int length); + +int inter_log(char *fmt,...); + +#define inter_cfgName "conf/inter_athena.conf" + +extern int party_share_level; +extern char inter_log_filename[1024]; + +#endif diff --git a/misc/src/char_sql/GNUmakefile b/misc/src/char_sql/GNUmakefile new file mode 100644 index 0000000..9c7b5e5 --- /dev/null +++ b/misc/src/char_sql/GNUmakefile @@ -0,0 +1,20 @@ +all: char-server_sql +sql: char-server_sql + +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o ../common/malloc.o +COMMON_H = ../common/core.h ../common/socket.h ../common/timer.h ../common/db.h ../common/malloc.h + +char-server_sql: char.o inter.o int_party.o int_guild.o int_storage.o int_pet.o strlib.o itemdb.o $(COMMON_OBJ) + $(CC) -o ../../$@ $^ $(LIB_S) + +char.o: char.c char.h strlib.h itemdb.h +inter.o: inter.c inter.h int_party.h int_guild.h int_storage.h int_pet.h ../common/mmo.h char.h ../common/socket.h +int_party.o: int_party.c int_party.h inter.h ../common/mmo.h char.h ../common/socket.h ../common/timer.h ../common/db.h +int_guild.o: int_guild.c int_guild.h inter.h ../common/mmo.h char.h ../common/socket.h ../common/db.h +int_storage.o: int_storage.c int_storage.h char.h itemdb.h +int_pet.o: int_pet.c int_pet.h inter.h char.h ../common/mmo.h ../common/socket.h ../common/db.h +strlib.o: strlib.c strlib.h +itemdb.o: itemdb.c itemdb.h ../common/db.h ../common/mmo.h + +clean: + rm -f *.o ../../char-server_sql diff --git a/misc/src/char_sql/Makefile b/misc/src/char_sql/Makefile new file mode 100644 index 0000000..cefe36e --- /dev/null +++ b/misc/src/char_sql/Makefile @@ -0,0 +1,20 @@ +all: char-server_sql +sql: char-server_sql + +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o ../common/malloc.o +COMMON_H = ../common/core.h ../common/socket.h ../common/timer.h ../common/db.h ../common/malloc.h + +char-server_sql: char.o inter.o int_party.o int_guild.o int_storage.o int_pet.o strlib.o itemdb.o $(COMMON_OBJ) + $(CC) -o ../../$@ $^ $(LIB_S) + +char.o: char.c char.h strlib.h itemdb.h +inter.o: inter.c inter.h int_party.h int_guild.h int_storage.h int_pet.h ../common/mmo.h char.h ../common/socket.h +int_party.o: int_party.c int_party.h inter.h ../common/mmo.h char.h ../common/socket.h ../common/timer.h ../common/db.h +int_guild.o: int_guild.c int_guild.h inter.h ../common/mmo.h char.h ../common/socket.h ../common/db.h +int_storage.o: int_storage.c int_storage.h char.h itemdb.h +int_pet.o: int_pet.c int_pet.h inter.h char.h ../common/mmo.h ../common/socket.h ../common/db.h +strlib.o: strlib.c strlib.h +itemdb.o: itemdb.c itemdb.h ../common/db.h ../common/mmo.h + +clean: + rm -f *.o ../../char-server_sql diff --git a/misc/src/char_sql/char.c b/misc/src/char_sql/char.c new file mode 100644 index 0000000..11b6a49 --- /dev/null +++ b/misc/src/char_sql/char.c @@ -0,0 +1,2917 @@ +// $Id: char.c,v 1.16 2004/09/23 18:31:16 MouseJstr Exp $ +// original : char2.c 2003/03/14 11:58:35 Rev.1.5 +// +// original code from athena +// SQL conversion by Jioh L. Jung +// TXT 1.105 +#include + +#ifdef LCCWIN32 +#include +#pragma lib +#else +#include +#include +#include +#include +#include +#include +#endif + +#include "utils.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "char.h" +#include "strlib.h" +#include "itemdb.h" +#include "inter.h" + +#ifdef MEMWATCH +#include "memwatch.h" +#endif + +char char_db[256] = "char"; +char cart_db[256] = "cart_inventory"; +char inventory_db[256] = "inventory"; +char charlog_db[256] = "charlog"; +char storage_db[256] = "storage"; +char interlog_db[256] = "interlog"; +char reg_db[256] = "global_reg_value"; +char skill_db[256] = "skill"; +char memo_db[256] = "memo"; +char guild_db[256] = "guild"; +char guild_alliance_db[256] = "guild_alliance"; +char guild_castle_db[256] = "guild_castle"; +char guild_expulsion_db[256] = "guild_expulsion"; +char guild_member_db[256] = "guild_member"; +char guild_position_db[256] = "guild_position"; +char guild_skill_db[256] = "guild_skill"; +char guild_storage_db[256] = "guild_storage"; +char party_db[256] = "party"; +char pet_db[256] = "pet"; +char login_db[256] = "login"; + +char login_db_account_id[32] = "account_id"; +char login_db_level[32] = "level"; + +int lowest_gm_level = 1; + +unsigned char *SQL_CONF_NAME = "conf/inter_athena.conf"; + +struct mmo_map_server server[MAX_MAP_SERVERS]; +int server_fd[MAX_MAP_SERVERS]; +int server_freezeflag[MAX_MAP_SERVERS]; // Map-server anti-freeze system. Counter. 5 ok, 4...0 freezed + +int anti_freeze_enable = 0; +int ANTI_FREEZE_INTERVAL = 6; + +int login_fd, char_fd; +char userid[24]; +char passwd[24]; +char server_name[20]; +char wisp_server_name[24] = "Server"; +char login_ip_str[128]; +int login_ip; +int login_port = 6900; +char char_ip_str[128]; +int char_ip; +int char_port = 6121; +int char_maintenance; +int char_new; +int name_ignoring_case = 0; // Allow or not identical name for characters but with a different case by [Yor] +int char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor] +char char_name_letters[1024] = ""; // list of letters/symbols used to authorise or not a name of a character. by [Yor] + +char lan_map_ip[128]; // Lan map ip added by kashy +int subnetmaski[4]; // Subnetmask added by kashy +char unknown_char_name[1024] = "Unknown"; + +struct char_session_data{ + int account_id,login_id1,login_id2,sex; + int found_char[9]; + char email[40]; // e-mail (default: a@a.com) by [Yor] + time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited) +}; + +#define AUTH_FIFO_SIZE 256 +struct { + int account_id,char_id,login_id1,login_id2,ip,char_pos,delflag,sex; + time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited) +} auth_fifo[AUTH_FIFO_SIZE]; +int auth_fifo_pos = 0; + +int check_ip_flag = 1; // It's to check IP of a player between char-server and other servers (part of anti-hacking system) + +int char_id_count = 150000; +struct mmo_charstatus *char_dat; +int char_num,char_max; +int max_connect_user = 0; +int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL; +int start_zeny = 500; +int start_weapon = 1201; +int start_armor = 2301; + +// check for exit signal +// 0 is saving complete +// other is char_id +unsigned int save_flag = 0; + +// start point (you can reset point on conf file) +struct point start_point = {"new_1-1.gat", 53, 111}; + +struct gm_account *gm_account = NULL; +int GM_num = 0; + +//----------------------------------------------------- +// Function to suppress control characters in a string. +//----------------------------------------------------- +int remove_control_chars(unsigned char *str) { + int i; + int change = 0; + + for(i = 0; i < strlen(str); i++) { + if (str[i] < 32) { + str[i] = '_'; + change = 1; + } + } + + return change; +} + +//---------------------------------------------------------------------- +// Determine if an account (id) is a GM account +// and returns its level (or 0 if it isn't a GM account or if not found) +//---------------------------------------------------------------------- +// Removed since nothing GM related goes on in the char server [CLOWNISIUS] +int isGM(int account_id) { + int i; + + for(i = 0; i < GM_num; i++) + if (gm_account[i].account_id == account_id) + return gm_account[i].level; + return 0; +} + +void read_gm_account(void) { + if (gm_account != NULL) + free(gm_account); + GM_num = 0; + + sprintf(tmp_lsql, "SELECT `%s`,`%s` FROM `%s` WHERE `%s`>='%d'",login_db_account_id,login_db_level,login_db,login_db_level,lowest_gm_level); + if (mysql_query(&lmysql_handle, tmp_lsql)) { + printf("DB server Error (select %s to Memory)- %s\n",login_db,mysql_error(&lmysql_handle)); + } + lsql_res = mysql_store_result(&lmysql_handle); + if (lsql_res) { + gm_account = calloc(sizeof(struct gm_account) * mysql_num_rows(lsql_res), 1); + while ((lsql_row = mysql_fetch_row(lsql_res))) { + gm_account[GM_num].account_id = atoi(lsql_row[0]); + gm_account[GM_num].level = atoi(lsql_row[1]); + GM_num++; + } + } + + mysql_free_result(lsql_res); +} + +//===================================================================================================== +int mmo_char_tosql(int char_id, struct mmo_charstatus *p){ + int i=0,party_exist,guild_exist; + int eqcount=1; + int noteqcount=1; + char temp_str[32]; + + struct itemtemp mapitem; + if (char_id!=p->char_id) return 0; + + save_flag = p->char_id; + printf("(\033[1;32m%d\033[0m) %s \trequest save char data - ",char_id,char_dat[0].name); + + + +//for(testcount=1;testcount<50;testcount++){//---------------------------test count-------------------- +// printf("test count : %d\n", testcount); +// eqcount=1; +// noteqcount=1; +// dbeqcount=1; +// dbnoteqcount=1; +//----------------------------------------------------------------------------------------------------- + +//=========================================map inventory data > memory =============================== + //map inventory data + for(i=0;iinventory[i].nameid>0){ + if(itemdb_isequip(p->inventory[i].nameid)==1){ + mapitem.equip[eqcount].flag=0; + mapitem.equip[eqcount].id = p->inventory[i].id; + mapitem.equip[eqcount].nameid=p->inventory[i].nameid; + mapitem.equip[eqcount].amount = p->inventory[i].amount; + mapitem.equip[eqcount].equip = p->inventory[i].equip; + mapitem.equip[eqcount].identify = p->inventory[i].identify; + mapitem.equip[eqcount].refine = p->inventory[i].refine; + mapitem.equip[eqcount].attribute = p->inventory[i].attribute; + mapitem.equip[eqcount].card[0] = p->inventory[i].card[0]; + mapitem.equip[eqcount].card[1] = p->inventory[i].card[1]; + mapitem.equip[eqcount].card[2] = p->inventory[i].card[2]; + mapitem.equip[eqcount].card[3] = p->inventory[i].card[3]; + mapitem.equip[eqcount].broken = p->inventory[i].broken; + eqcount++; + } + else if(itemdb_isequip(p->inventory[i].nameid)==0){ + mapitem.notequip[noteqcount].flag=0; + mapitem.notequip[noteqcount].id = p->inventory[i].id; + mapitem.notequip[noteqcount].nameid=p->inventory[i].nameid; + mapitem.notequip[noteqcount].amount = p->inventory[i].amount; + mapitem.notequip[noteqcount].equip = p->inventory[i].equip; + mapitem.notequip[noteqcount].identify = p->inventory[i].identify; + mapitem.notequip[noteqcount].refine = p->inventory[i].refine; + mapitem.notequip[noteqcount].attribute = p->inventory[i].attribute; + mapitem.notequip[noteqcount].card[0] = p->inventory[i].card[0]; + mapitem.notequip[noteqcount].card[1] = p->inventory[i].card[1]; + mapitem.notequip[noteqcount].card[2] = p->inventory[i].card[2]; + mapitem.notequip[noteqcount].card[3] = p->inventory[i].card[3]; + mapitem.notequip[noteqcount].broken = p->inventory[i].broken; + noteqcount++; + } + } + } + //printf("- Save item data to MySQL!\n"); + memitemdata_to_sql(mapitem, eqcount, noteqcount, p->char_id,TABLE_INVENTORY); + +//=========================================map cart data > memory ==================================== + eqcount=1; + noteqcount=1; + + //map cart data + for(i=0;icart[i].nameid>0){ + if(itemdb_isequip(p->cart[i].nameid)==1){ + mapitem.equip[eqcount].flag=0; + mapitem.equip[eqcount].id = p->cart[i].id; + mapitem.equip[eqcount].nameid=p->cart[i].nameid; + mapitem.equip[eqcount].amount = p->cart[i].amount; + mapitem.equip[eqcount].equip = p->cart[i].equip; + mapitem.equip[eqcount].identify = p->cart[i].identify; + mapitem.equip[eqcount].refine = p->cart[i].refine; + mapitem.equip[eqcount].attribute = p->cart[i].attribute; + mapitem.equip[eqcount].card[0] = p->cart[i].card[0]; + mapitem.equip[eqcount].card[1] = p->cart[i].card[1]; + mapitem.equip[eqcount].card[2] = p->cart[i].card[2]; + mapitem.equip[eqcount].card[3] = p->cart[i].card[3]; + mapitem.equip[eqcount].broken = p->cart[i].broken; + eqcount++; + } + else if(itemdb_isequip(p->cart[i].nameid)==0){ + mapitem.notequip[noteqcount].flag=0; + mapitem.notequip[noteqcount].id = p->cart[i].id; + mapitem.notequip[noteqcount].nameid=p->cart[i].nameid; + mapitem.notequip[noteqcount].amount = p->cart[i].amount; + mapitem.notequip[noteqcount].equip = p->cart[i].equip; + mapitem.notequip[noteqcount].identify = p->cart[i].identify; + mapitem.notequip[noteqcount].refine = p->cart[i].refine; + mapitem.notequip[noteqcount].attribute = p->cart[i].attribute; + mapitem.notequip[noteqcount].card[0] = p->cart[i].card[0]; + mapitem.notequip[noteqcount].card[1] = p->cart[i].card[1]; + mapitem.notequip[noteqcount].card[2] = p->cart[i].card[2]; + mapitem.notequip[noteqcount].card[3] = p->cart[i].card[3]; + mapitem.notequip[noteqcount].broken = p->cart[i].broken; + noteqcount++; + } + } + } + + //printf("- Save cart data to MySQL!\n"); + memitemdata_to_sql(mapitem, eqcount, noteqcount, p->char_id,TABLE_CART); + +//===================================================================================================== + +//}//---------------------------test count------------------------------ + //check party_exist + party_exist=0; + sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `party_id` = '%d'",party_db, p->party_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + sql_row = mysql_fetch_row(sql_res); + if (sql_row) party_exist = atoi(sql_row[0]); + mysql_free_result(sql_res); + + //check guild_exist + guild_exist=0; + sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `guild_id` = '%d'",guild_db, p->guild_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + sql_row = mysql_fetch_row(sql_res); + if (sql_row) guild_exist = atoi(sql_row[0]); + mysql_free_result(sql_res); + + if (guild_exist==0) p->guild_id=0; + if (party_exist==0) p->party_id=0; + + //sql query + //`char`( `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`, //9 + //`str`,`agi`,`vit`,`int`,`dex`,`luk`, //15 + //`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`, //21 + //`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`, //27 + //`hair`,`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`, //35 + //`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`) + //printf("- Save char data to MySQL!\n"); + sprintf(tmp_sql ,"UPDATE `%s` SET `class`='%d', `base_level`='%d', `job_level`='%d'," + "`base_exp`='%d', `job_exp`='%d', `zeny`='%d'," + "`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d'," + "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d'," + "`option`='%d',`karma`='%d',`manner`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d'," + "`hair`='%d',`hair_color`='%d',`clothes_color`='%d',`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d'," + "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d',`partner_id`='%d' WHERE `account_id`='%d' AND `char_id` = '%d'", + char_db, p->class, p->base_level, p->job_level, + p->base_exp, p->job_exp, p->zeny, + p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point, + p->str, p->agi, p->vit, p->int_, p->dex, p->luk, + p->option, p->karma, p->manner, p->party_id, p->guild_id, p->pet_id, + p->hair, p->hair_color, p->clothes_color, + p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom, + p->last_point.map, p->last_point.x, p->last_point.y, + p->save_point.map, p->save_point.x, p->save_point.y, p->partner_id, p->account_id, p->char_id + ); + + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle)); + } + + //printf("- Save memo data to MySQL!\n"); + //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`) + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",memo_db, p->char_id); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (delete `memo`)- %s\n", mysql_error(&mysql_handle)); + } + + //insert here. + for(i=0;i<10;i++){ + if(p->memo_point[i].map[0]){ + sprintf(tmp_sql,"INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ('%d', '%s', '%d', '%d')", + memo_db, char_id, p->memo_point[i].map, p->memo_point[i].x, p->memo_point[i].y); + if(mysql_query(&mysql_handle, tmp_sql)) + printf("DB server Error (insert `memo`)- %s\n", mysql_error(&mysql_handle)); + } + } + + //printf("- Save skill data to MySQL!\n"); + //`skill` (`char_id`, `id`, `lv`) + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",skill_db, p->char_id); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (delete `skill`)- %s\n", mysql_error(&mysql_handle)); + } + //printf("- Insert skill \n"); + //insert here. + for(i=0;iskill[i].id){ + if (p->skill[i].id && p->skill[i].flag!=1) { + sprintf(tmp_sql,"INSERT delayed INTO `%s`(`char_id`, `id`, `lv`) VALUES ('%d', '%d','%d')", + skill_db, char_id, p->skill[i].id, (p->skill[i].flag==0)?p->skill[i].lv:p->skill[i].flag-2); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (insert `skill`)- %s\n", mysql_error(&mysql_handle)); + } + } + } + } + + + //printf("- Save global_reg_value data to MySQL!\n"); + //`global_reg_value` (`char_id`, `str`, `value`) + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `type`=3 AND `char_id`='%d'",reg_db, p->char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (delete `global_reg_value`)- %s\n", mysql_error(&mysql_handle)); + } + + //insert here. + for(i=0;iglobal_reg_num;i++){ + if (p->global_reg[i].str) { + if(p->global_reg[i].value !=0){ + sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`, `str`, `value`) VALUES ('%d', '%s','%d')", + reg_db, char_id, jstrescapecpy(temp_str,(unsigned char*)p->global_reg[i].str), p->global_reg[i].value); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (insert `global_reg_value`)- %s\n", mysql_error(&mysql_handle)); + } + } + } + } + printf("saving char is done.\n"); + save_flag = 0; + + return 0; +} + +int memitemdata_to_sql(struct itemtemp mapitem, int eqcount, int noteqcount, int char_id, int tableswitch){ + //equ + int i, j; + int dbeqcount = 1; + int dbnoteqcount = 1; + struct itemtemp dbitem; + char tablename[16]; + char selectoption[16]; + + switch (tableswitch){ + case TABLE_INVENTORY: + sprintf(tablename,"%s",inventory_db); + sprintf(selectoption,"char_id"); + break; + case TABLE_CART: + sprintf(tablename,"%s",cart_db); + sprintf(selectoption,"char_id"); + break; + case TABLE_STORAGE: + sprintf(tablename,"%s",storage_db); + sprintf(selectoption,"account_id"); + break; + case TABLE_GUILD_STORAGE: + sprintf(tablename,"%s",guild_storage_db); + sprintf(selectoption,"guild_id"); + break; + } + //printf("Working Table : %s \n",tablename); + + //=======================================mysql database data > memory=============================================== + + sprintf(tmp_sql, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3` , `broken` " + "FROM `%s` WHERE `%s`='%d'",tablename ,selectoption ,char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `%s` to Memory)- %s\n",tablename ,mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){ + if (itemdb_isequip(atoi(sql_row[1]))==1){ + dbitem.equip[dbeqcount].flag=0; + dbitem.equip[dbeqcount].id = atoi(sql_row[0]); + dbitem.equip[dbeqcount].nameid = atoi(sql_row[1]); + dbitem.equip[dbeqcount].amount = atoi(sql_row[2]); + dbitem.equip[dbeqcount].equip = atoi(sql_row[3]); + dbitem.equip[dbeqcount].identify = atoi(sql_row[4]); + dbitem.equip[dbeqcount].refine = atoi(sql_row[5]); + dbitem.equip[dbeqcount].attribute = atoi(sql_row[6]); + dbitem.equip[dbeqcount].card[0] = atoi(sql_row[7]); + dbitem.equip[dbeqcount].card[1] = atoi(sql_row[8]); + dbitem.equip[dbeqcount].card[2] = atoi(sql_row[9]); + dbitem.equip[dbeqcount].card[3] = atoi(sql_row[10]); + dbitem.equip[dbeqcount].broken = atoi(sql_row[11]); + dbeqcount++; + }else { + dbitem.notequip[dbnoteqcount].flag=0; + dbitem.notequip[dbnoteqcount].id = atoi(sql_row[0]); + dbitem.notequip[dbnoteqcount].nameid = atoi(sql_row[1]); + dbitem.notequip[dbnoteqcount].amount = atoi(sql_row[2]); + dbitem.notequip[dbnoteqcount].equip = atoi(sql_row[3]); + dbitem.notequip[dbnoteqcount].identify = atoi(sql_row[4]); + dbitem.notequip[dbnoteqcount].refine = atoi(sql_row[5]); + dbitem.notequip[dbnoteqcount].attribute = atoi(sql_row[6]); + dbitem.notequip[dbnoteqcount].card[0] = atoi(sql_row[7]); + dbitem.notequip[dbnoteqcount].card[1] = atoi(sql_row[8]); + dbitem.notequip[dbnoteqcount].card[2] = atoi(sql_row[9]); + dbitem.notequip[dbnoteqcount].card[3] = atoi(sql_row[10]); + dbitem.notequip[dbnoteqcount].broken = atoi(sql_row[11]); + dbnoteqcount++; + } + } + mysql_free_result(sql_res); + } + + //==============================================Memory data > SQL =============================== + //======================================Equip ITEM======================================= + if((eqcount==1) && (dbeqcount==1)){//printf("%s Equip Empty\n",tablename); + //item empty + } else { + + for(i=1;ichar_id = char_id; + printf("Loaded: "); + //`char`( `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`, //9 + //`str`,`agi`,`vit`,`int`,`dex`,`luk`, //15 + //`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`, //21 + //`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`, //27 + //`hair`,`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`, //35 + //`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`) + //splite 2 parts. cause veeeery long SQL syntax + + sprintf(tmp_sql, "SELECT `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`," + "`str`,`agi`,`vit`,`int`,`dex`,`luk`, `max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point` FROM `%s` WHERE `char_id` = '%d'",char_db, char_id); + + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle)); + } + + sql_res = mysql_store_result(&mysql_handle); + + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); + + p->char_id = char_id; + p->account_id = atoi(sql_row[1]); + p->char_num = atoi(sql_row[2]); + strcpy(p->name, sql_row[3]); + p->class = atoi(sql_row[4]); + p->base_level = atoi(sql_row[5]); + p->job_level = atoi(sql_row[6]); + p->base_exp = atoi(sql_row[7]); + p->job_exp = atoi(sql_row[8]); + p->zeny = atoi(sql_row[9]); + p->str = atoi(sql_row[10]); + p->agi = atoi(sql_row[11]); + p->vit = atoi(sql_row[12]); + p->int_ = atoi(sql_row[13]); + p->dex = atoi(sql_row[14]); + p->luk = atoi(sql_row[15]); + p->max_hp = atoi(sql_row[16]); + p->hp = atoi(sql_row[17]); + p->max_sp = atoi(sql_row[18]); + p->sp = atoi(sql_row[19]); + p->status_point = atoi(sql_row[20]); + p->skill_point = atoi(sql_row[21]); + //free mysql result. + mysql_free_result(sql_res); + } else + printf("char1 - failed\n"); //Error?! ERRRRRR WHAT THAT SAY!? + printf("(\033[1;32m%d\033[0m)\033[1;32m%s\033[0m\t[",p->char_id,p->name); + printf("char1 "); + + sprintf(tmp_sql, "SELECT `option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`hair`,`hair_color`," + "`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`," + "`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`, `partner_id` FROM `%s` WHERE `char_id` = '%d'",char_db, char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `char2`)- %s\n", mysql_error(&mysql_handle)); + } + + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); + + + p->option = atoi(sql_row[0]); p->karma = atoi(sql_row[1]); p->manner = atoi(sql_row[2]); + p->party_id = atoi(sql_row[3]); p->guild_id = atoi(sql_row[4]); p->pet_id = atoi(sql_row[5]); + + p->hair = atoi(sql_row[6]); p->hair_color = atoi(sql_row[7]); p->clothes_color = atoi(sql_row[8]); + p->weapon = atoi(sql_row[9]); p->shield = atoi(sql_row[10]); + p->head_top = atoi(sql_row[11]); p->head_mid = atoi(sql_row[12]); p->head_bottom = atoi(sql_row[13]); + strcpy(p->last_point.map,sql_row[14]); p->last_point.x = atoi(sql_row[15]); p->last_point.y = atoi(sql_row[16]); + strcpy(p->save_point.map,sql_row[17]); p->save_point.x = atoi(sql_row[18]); p->save_point.y = atoi(sql_row[19]); + p->partner_id = atoi(sql_row[20]); + + //free mysql result. + mysql_free_result(sql_res); + } else + printf("char2 - failed\n"); //Error?! ERRRRRR WHAT THAT SAY!? + + printf("char2 "); + + //read memo data + //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`) + sprintf(tmp_sql, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`='%d'",memo_db, char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `memo`)- %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + + if (sql_res) { + for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){ + strcpy (p->memo_point[i].map,sql_row[0]); + p->memo_point[i].x=atoi(sql_row[1]); + p->memo_point[i].y=atoi(sql_row[2]); + //i ++; + } + mysql_free_result(sql_res); + } + printf("memo "); + + //read inventory + //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`) + sprintf(tmp_sql, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`, `broken` " + "FROM `%s` WHERE `char_id`='%d'",inventory_db, char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `inventory`)- %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){ + p->inventory[i].id = atoi(sql_row[0]); + p->inventory[i].nameid = atoi(sql_row[1]); + p->inventory[i].amount = atoi(sql_row[2]); + p->inventory[i].equip = atoi(sql_row[3]); + p->inventory[i].identify = atoi(sql_row[4]); + p->inventory[i].refine = atoi(sql_row[5]); + p->inventory[i].attribute = atoi(sql_row[6]); + p->inventory[i].card[0] = atoi(sql_row[7]); + p->inventory[i].card[1] = atoi(sql_row[8]); + p->inventory[i].card[2] = atoi(sql_row[9]); + p->inventory[i].card[3] = atoi(sql_row[10]); + p->inventory[i].broken = atoi(sql_row[11]); + } + mysql_free_result(sql_res); + } + printf("inventory "); + + + //read cart. + //`cart_inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`) + sprintf(tmp_sql, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`, `broken` " + "FROM `%s` WHERE `char_id`='%d'",cart_db, char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `cart_inventory`)- %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){ + p->cart[i].id = atoi(sql_row[0]); + p->cart[i].nameid = atoi(sql_row[1]); + p->cart[i].amount = atoi(sql_row[2]); + p->cart[i].equip = atoi(sql_row[3]); + p->cart[i].identify = atoi(sql_row[4]); + p->cart[i].refine = atoi(sql_row[5]); + p->cart[i].attribute = atoi(sql_row[6]); + p->cart[i].card[0] = atoi(sql_row[7]); + p->cart[i].card[1] = atoi(sql_row[8]); + p->cart[i].card[2] = atoi(sql_row[9]); + p->cart[i].card[3] = atoi(sql_row[10]); + p->cart[i].broken = atoi(sql_row[11]); + } + mysql_free_result(sql_res); + } + printf("cart "); + + //read skill + //`skill` (`char_id`, `id`, `lv`) + sprintf(tmp_sql, "SELECT `id`, `lv` FROM `%s` WHERE `char_id`='%d'",skill_db, char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `skill`)- %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){ + n = atoi(sql_row[0]); + p->skill[n].id = n; //memory!? shit!. + p->skill[n].lv = atoi(sql_row[1]); + } + mysql_free_result(sql_res); + } + printf("skill "); + + //global_reg + //`global_reg_value` (`char_id`, `str`, `value`) + sprintf(tmp_sql, "SELECT `str`, `value` FROM `%s` WHERE `type`=3 AND `char_id`='%d'",reg_db, char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `global_reg_value`)- %s\n", mysql_error(&mysql_handle)); + } + i = 0; + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){ + strcpy (p->global_reg[i].str, sql_row[0]); + p->global_reg[i].value = atoi (sql_row[1]); + } + mysql_free_result(sql_res); + } + p->global_reg_num=i; + + if (online) { + sprintf(tmp_sql, "UPDATE `%s` SET `online`='%d' WHERE `char_id`='%d'",char_db,online,char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (set char online)- %s\n", mysql_error(&mysql_handle)); + } + } + + printf("global_reg]\n"); //ok. all data load successfuly! + + //printf("char cloade"); + + return 1; +} +//========================================================================================================== +int mmo_char_sql_init(void) { + int i; + + printf("init start.......\n"); + // memory initialize + // no need to set twice size in this routine. but some cause segmentation error. :P + printf("initializing char memory...(%d byte)\n",sizeof(struct mmo_charstatus)*2); + CREATE(char_dat, struct mmo_charstatus, 2); + + memset(char_dat, 0, sizeof(struct mmo_charstatus)*2); +/* Initialized in inter.c already [Wizputer] + // DB connection initialized + // for char-server session only + mysql_init(&mysql_handle); + printf("Connect DB server....(char server)\n"); + if(!mysql_real_connect(&mysql_handle, char_server_ip, char_server_id, char_server_pw, char_server_db ,char_server_port, (char *)NULL, 0)) { + // SQL connection pointer check + printf("%s\n",mysql_error(&mysql_handle)); + exit(1); + } else { + printf("connect success! (char server)\n"); + } +*/ + sprintf(tmp_sql , "SELECT count(*) FROM `%s`", char_db); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle) ; + sql_row = mysql_fetch_row(sql_res); + printf("total char data -> '%s'.......\n",sql_row[0]); + i = atoi (sql_row[0]); + mysql_free_result(sql_res); + + if (i !=0) { + sprintf(tmp_sql , "SELECT max(`char_id`) FROM `%s`", char_db); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle) ; + sql_row = mysql_fetch_row(sql_res); + char_id_count = atoi (sql_row[0]); + + mysql_free_result(sql_res); + } else + printf("set char_id_count: %d.......\n",char_id_count); + + sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `online`='1'", char_db); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (reset_online `%s`)- %s\n", char_db, mysql_error(&mysql_handle)); + } + printf("init end.......\n"); + + return 0; +} + +//========================================================================================================== + +int make_new_char_sql(int fd, unsigned char *dat) { + struct char_session_data *sd; + char t_name[100]; + int i; + //aphostropy error check! - fixed! + jstrescapecpy(t_name, dat); + printf("making new char -"); + + sd = session[fd]->session_data; + + // Check Authorised letters/symbols in the name of the character + if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised + for (i = 0; i < strlen(dat); i++) + if (strchr(char_name_letters, dat[i]) == NULL) + return -1; + } else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden + for (i = 0; i < strlen(dat); i++) + if (strchr(char_name_letters, dat[i]) != NULL) + return -1; + } // else, all letters/symbols are authorised (except control char removed before) + + //check stat error + if ((dat[24]+dat[25]+dat[26]+dat[27]+dat[28]+dat[29]!=5*6 ) || + (dat[30] >= 9) || + (dat[33] <= 0) || (dat[33] >= 20) || + (dat[31] >= 9)) { + + // check individual stat value + for(i = 24; i <= 29; i++) { + if (dat[i] < 1 || dat[i] > 9) { + return -1; + } + } + + // char.log to charlog + sprintf(tmp_sql,"INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)" + "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')", + charlog_db,"make new char error", sd->account_id, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]); + //query + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + printf("make new char error %d-%d %s %d, %d, %d, %d, %d, %d %d, %d" RETCODE, + fd, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]); + return -1; + } + + // char.log to charlog + sprintf(tmp_sql,"INSERT INTO `%s`(`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)" + "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')", + charlog_db,"make new char", sd->account_id, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]); + //query + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + printf("make new char %d-%d %s %d, %d, %d, %d, %d, %d - %d, %d" RETCODE, + fd, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]); + + sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `name` = '%s'",char_db, t_name); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + return -1; + } + sql_res = mysql_store_result(&mysql_handle); + sql_row = mysql_fetch_row(sql_res); + printf("\033[1;32m name check result : %s -\033[0m ",sql_row[0]); + if (atoi(sql_row[0]) > 0) { + mysql_free_result(sql_res); + return -1; + } else + mysql_free_result(sql_res); + + // check char slot. + sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d'",char_db, sd->account_id, dat[30]); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + sql_row = mysql_fetch_row(sql_res); + + //printf("slot check result : %s\n",sql_row[0]); + if (atoi(sql_row[0]) > 0) { + mysql_free_result(sql_res); + return -1; + } else + mysql_free_result(sql_res); + + char_id_count++; + + // make new char. + sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`,`account_id`,`char_num`,`name`,`zeny`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,`hair`,`hair_color`)" + " VALUES ('%d', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d')", + char_db, char_id_count, sd->account_id , dat[30] , t_name, start_zeny, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], + (40 * (100 + dat[26])/100) , (40 * (100 + dat[26])/100 ), (11 * (100 + dat[27])/100), (11 * (100 + dat[27])/100), dat[33], dat[31]); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (insert `char`)- %s\n", mysql_error(&mysql_handle)); + } + + //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`) + sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%d', '%d', '%d', '%d')", + inventory_db, char_id_count, 1201,1,0x02,1); //add Knife + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (insert `inventory`)- %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%d', '%d', '%d', '%d')", + inventory_db, char_id_count, 2301,1,0x10,1); //add Cotton Shirt + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (insert `inventory`)- %s\n", mysql_error(&mysql_handle)); + } + // respawn map and start point set + sprintf(tmp_sql,"UPDATE `%s` SET `last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d' WHERE `char_id` = '%d'", + char_db, start_point.map,start_point.x,start_point.y, start_point.map,start_point.x,start_point.y, char_id_count); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle)); + } + printf("making new char success - id:(\033[1;32m%d\033[0m\tname:\033[1;32%s\033[0m\n", char_id_count, t_name); + return char_id_count; +} + +//========================================================================================================== + +void mmo_char_sync(void){ + printf("mmo_char_sync() - nothing to do\n"); +} + +// to do +/////////////////////////// + +int mmo_char_sync_timer(int tid, unsigned int tick, int id, int data) { + printf("mmo_char_sync_timer() tic - no works to do\n"); + return 0; +} + +int count_users(void) { + int i, users; + + if (login_fd > 0 && session[login_fd]){ + users = 0; + for(i = 0; i < MAX_MAP_SERVERS; i++) { + if (server_fd[i] >= 0) { + users += server[i].users; + } + } + return users; + } + return 0; +} + +int mmo_char_send006b(int fd, struct char_session_data *sd) { + int i, j, found_num = 0; + struct mmo_charstatus *p = NULL; +// hehe. commented other. anyway there's no need to use older version. +// if use older packet version just uncomment that! +//#ifdef NEW_006b + const int offset = 24; +//#else +// int offset = 4; +//#endif + + printf("mmo_char_send006b start.. (account:%d)\n",sd->account_id); +// printf("offset -> %d...\n",offset); + + //search char. + sprintf(tmp_sql, "SELECT `char_id` FROM `%s` WHERE `account_id` = '%d'",char_db, sd->account_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + found_num = mysql_num_rows(sql_res); + printf("number of chars: %d\n", found_num); + i = 0; + while((sql_row = mysql_fetch_row(sql_res))) { + sd->found_char[i] = atoi(sql_row[0]); + i++; + } + mysql_free_result(sql_res); + } + +// printf("char fetching end (total: %d)....\n", found_num); + + for(i = found_num; i < 9; i++) + sd->found_char[i] = -1; + + memset(WFIFOP(fd, 0), 0, offset + found_num * 106); + WFIFOW(fd, 0) = 0x6b; + WFIFOW(fd, 2) = offset + found_num * 106; + + printf("(\033[1;13m%d\033[0m) Request Char Data:\n",sd->account_id); + + for(i = 0; i < found_num; i++) { + mmo_char_fromsql(sd->found_char[i], char_dat, 0); + + p = &char_dat[0]; + + j = offset + (i * 106); // increase speed of code + + WFIFOL(fd,j) = p->char_id; + WFIFOL(fd,j+4) = p->base_exp; + WFIFOL(fd,j+8) = p->zeny; + WFIFOL(fd,j+12) = p->job_exp; + WFIFOL(fd,j+16) = p->job_level; + + WFIFOL(fd,j+20) = 0; + WFIFOL(fd,j+24) = 0; + WFIFOL(fd,j+28) = p->option; + + WFIFOL(fd,j+32) = p->karma; + WFIFOL(fd,j+36) = p->manner; + + WFIFOW(fd,j+40) = p->status_point; + WFIFOW(fd,j+42) = (p->hp > 0x7fff) ? 0x7fff : p->hp; + WFIFOW(fd,j+44) = (p->max_hp > 0x7fff) ? 0x7fff : p->max_hp; + WFIFOW(fd,j+46) = (p->sp > 0x7fff) ? 0x7fff : p->sp; + WFIFOW(fd,j+48) = (p->max_sp > 0x7fff) ? 0x7fff : p->max_sp; + WFIFOW(fd,j+50) = DEFAULT_WALK_SPEED; // p->speed; + WFIFOW(fd,j+52) = p->class; + WFIFOW(fd,j+54) = p->hair; + WFIFOW(fd,j+56) = p->weapon; + WFIFOW(fd,j+58) = p->base_level; + WFIFOW(fd,j+60) = p->skill_point; + WFIFOW(fd,j+62) = p->head_bottom; + WFIFOW(fd,j+64) = p->shield; + WFIFOW(fd,j+66) = p->head_top; + WFIFOW(fd,j+68) = p->head_mid; + WFIFOW(fd,j+70) = p->hair_color; + WFIFOW(fd,j+72) = p->clothes_color; + + memcpy(WFIFOP(fd,j+74), p->name, 24); + + WFIFOB(fd,j+98) = (p->str > 255) ? 255 : p->str; + WFIFOB(fd,j+99) = (p->agi > 255) ? 255 : p->agi; + WFIFOB(fd,j+100) = (p->vit > 255) ? 255 : p->vit; + WFIFOB(fd,j+101) = (p->int_ > 255) ? 255 : p->int_; + WFIFOB(fd,j+102) = (p->dex > 255) ? 255 : p->dex; + WFIFOB(fd,j+103) = (p->luk > 255) ? 255 : p->luk; + WFIFOB(fd,j+104) = p->char_num; + } + + WFIFOSET(fd,WFIFOW(fd,2)); +// printf("mmo_char_send006b end..\n"); + return 0; +} + +int parse_tologin(int fd) { + int i; + struct char_session_data *sd; + + // only login-server can have an access to here. + // so, if it isn't the login-server, we disconnect the session. + //session eof check! + if (fd != login_fd || session[fd]->eof) { + if (fd == login_fd) { + printf("Char-server can't connect to login-server (connection #%d).\n", fd); + login_fd = -1; + } + close(fd); + delete_session(fd); + return 0; + } + + sd = session[fd]->session_data; + + // hehe. no need to set user limite on SQL version. :P + // but char limitation is good way to maintain server. :D + while(RFIFOREST(fd) >= 2) { +// printf("parse_tologin : %d %d %x\n", fd, RFIFOREST(fd), RFIFOW(fd, 0)); + + switch(RFIFOW(fd, 0)){ + case 0x2711: + if (RFIFOREST(fd) < 3) + return 0; + if (RFIFOB(fd, 2)) { + //printf("connect login server error : %d\n", RFIFOB(fd, 2)); + printf("Can not connect to login-server.\n"); + printf("The server communication passwords (default s1/p1) is probably invalid.\n"); + printf("Also, please make sure your login db has the username/password present and the sex of the account is S.\n"); + printf("If you changed the communication passwords, change them back at map_athena.conf and char_athena.conf\n"); + return 0; + //exit(1); //fixed for server shutdown. + }else { + printf("Connected to login-server (connection #%d).\n", fd); + // if no map-server already connected, display a message... + for(i = 0; i < MAX_MAP_SERVERS; i++) + if (server_fd[i] >= 0 && server[i].map[0][0]) // if map-server online and at least 1 map + break; + if (i == MAX_MAP_SERVERS) + printf("Awaiting maps from map-server.\n"); + } + RFIFOSKIP(fd, 3); + break; + + case 0x2713: + if(RFIFOREST(fd)<51) + return 0; + for(i = 0; i < fd_max; i++) { + if (session[i] && (sd = session[i]->session_data) && sd->account_id == RFIFOL(fd,2)) { + if (RFIFOB(fd,6) != 0) { + WFIFOW(i,0) = 0x6c; + WFIFOB(i,2) = 0x42; + WFIFOSET(i,3); + } else if (max_connect_user == 0 || count_users() < max_connect_user) { +// if (max_connect_user == 0) +// printf("max_connect_user (unlimited) -> accepted.\n"); +// else +// printf("count_users(): %d < max_connect_user (%d) -> accepted.\n", count_users(), max_connect_user); + sd->connect_until_time = (time_t)RFIFOL(fd,47); + // send characters to player + mmo_char_send006b(i, sd); + } else { + // refuse connection: too much online players +// printf("count_users(): %d < max_connect_use (%d) -> fail...\n", count_users(), max_connect_user); + WFIFOW(i,0) = 0x6c; + WFIFOW(i,2) = 0; + WFIFOSET(i,3); + } + } + } + RFIFOSKIP(fd,51); + break; + + case 0x2717: + if (RFIFOREST(fd) < 50) + return 0; + for(i = 0; i < fd_max; i++) { + if (session[i] && (sd = session[i]->session_data)) { + if (sd->account_id == RFIFOL(fd,2)) { + sd->connect_until_time = (time_t)RFIFOL(fd,46); + break; + } + } + } + RFIFOSKIP(fd,50); + break; + +/* case 0x2721: // gm reply. I don't want to support this function. + printf("0x2721:GM reply\n"); + { + int oldacc, newacc; + unsigned char buf[64]; + if (RFIFOREST(fd) < 10) + return 0; + oldacc = RFIFOL(fd, 2); + newacc = RFIFOL(fd, 6); + RFIFOSKIP(fd, 10); + if (newacc > 0) { + for(i=0;i map\n"); + } + break; +*/ + case 0x2723: // changesex reply (modified by [Yor]) + if (RFIFOREST(fd) < 7) + return 0; + { + int acc, sex; + unsigned char buf[16]; + + acc = RFIFOL(fd,2); + sex = RFIFOB(fd,6); + RFIFOSKIP(fd, 7); + if (acc > 0) { + sprintf(tmp_sql, "SELECT `char_id`,`class`,`skill_point` FROM `%s` WHERE `account_id` = '%d'",char_db, acc); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); + int char_id = atoi(sql_row[0]); + int jobclass = atoi(sql_row[1]); + int skill_point = atoi(sql_row[2]); + int class = jobclass; + if (jobclass == 19 || jobclass == 20 || + jobclass == 4020 || jobclass == 4021 || + jobclass == 4042 || jobclass == 4043) { + // job modification + if (jobclass == 19 || jobclass == 20) { + class = (sex) ? 19 : 20; + } else if (jobclass == 4020 || jobclass == 4021) { + class = (sex) ? 4020 : 4021; + } else if (jobclass == 4042 || jobclass == 4043) { + class = (sex) ? 4042 : 4043; + } + // remove specifical skills of classes 19,20 4020,4021 and 4042,4043 + sprintf(tmp_sql, "SELECT `lv` FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330'",skill_db, char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + while(( sql_row = mysql_fetch_row(sql_res))) { + skill_point += atoi(sql_row[0]); + } + } + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330'",skill_db, char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle)); + } + } + // to avoid any problem with equipment and invalid sex, equipment is unequiped. + sprintf(tmp_sql, "UPDATE `%s` SET `equip` = '0' WHERE `char_id` = '%d'",inventory_db, char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle)); + } + sprintf(tmp_sql, "UPDATE `%s` SET `class`='%d' , `skill_point`='%d' , `weapon`='0' , `shield='0' , `head_top`='0' , `head_mid`='0' , `head_bottom`='0' WHERE `char_id` = '%d'",char_db, class, skill_point, char_id); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle)); + } + } + } + // disconnect player if online on char-server + for(i = 0; i < fd_max; i++) { + if (session[i] && (sd = session[i]->session_data)) { + if (sd->account_id == acc) { + session[i]->eof = 1; + break; + } + } + } + + WBUFW(buf,0) = 0x2b0d; + WBUFL(buf,2) = acc; + WBUFB(buf,6) = sex; + + mapif_sendall(buf, 7); + } + break; + + // account_reg2変更通知 + case 0x2729: + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + { + struct global_reg reg[ACCOUNT_REG2_NUM]; + unsigned char buf[4096]; + int j, p, acc; + acc = RFIFOL(fd,4); + for(p = 8, j = 0; p < RFIFOW(fd,2) && j < ACCOUNT_REG2_NUM; p += 36, j++) { + memcpy(reg[j].str, RFIFOP(fd,p), 32); + reg[j].value = RFIFOL(fd,p+32); + } + // set_account_reg2(acc,j,reg); + // 同垢ログインを禁止していれば送る必要は無い + memcpy(buf,RFIFOP(fd,0), RFIFOW(fd,2)); + WBUFW(buf,0) = 0x2b11; + mapif_sendall(buf, WBUFW(buf,2)); + RFIFOSKIP(fd, RFIFOW(fd,2)); +// printf("char: save_account_reg_reply\n"); + } + break; + + // State change of account/ban notification (from login-server) by [Yor] + case 0x2731: + if (RFIFOREST(fd) < 11) + return 0; + // send to all map-servers to disconnect the player + { + unsigned char buf[16]; + WBUFW(buf,0) = 0x2b14; + WBUFL(buf,2) = RFIFOL(fd,2); + WBUFB(buf,6) = RFIFOB(fd,6); // 0: change of statut, 1: ban + WBUFL(buf,7) = RFIFOL(fd,7); // status or final date of a banishment + mapif_sendall(buf, 11); + } + // disconnect player if online on char-server + for(i = 0; i < fd_max; i++) { + if (session[i] && (sd = session[i]->session_data)) { + if (sd->account_id == RFIFOL(fd,2)) { + session[i]->eof = 1; + break; + } + } + } + RFIFOSKIP(fd,11); + break; + + default: + printf("set eof.\n"); + session[fd]->eof = 1; + return 0; + } + } + + RFIFOFLUSH(fd); + + return 0; +} + +//-------------------------------- +// Map-server anti-freeze system +//-------------------------------- +int map_anti_freeze_system(int tid, unsigned int tick, int id, int data) { + int i; + + for(i = 0; i < MAX_MAP_SERVERS; i++) { + if (server_fd[i] >= 0) {// if map-server is online + printf("map_anti_freeze_system: server #%d, flag: %d.\n", i, server_freezeflag[i]); + if (server_freezeflag[i]-- < 1) {// Map-server anti-freeze system. Counter. 5 ok, 4...0 freezed + printf("Map-server anti-freeze system: char-server #%d is frozen -> disconnection.\n", i); + session[server_fd[i]]->eof = 1; + sprintf(tmp_sql, "DELETE FROM `ragsrvinfo` WHERE `index`='%d'", server_fd[i]); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + } + } + } + + return 0; +} + +int parse_frommap(int fd) { + int i = 0, j = 0; + int id; + + // Sometimes fd=0, and it will cause server crash. Don't know why. :( + if (fd <= 0) { + printf("parse_frommap error fd=0\n"); + return 0; + } + + for(id = 0; id < MAX_MAP_SERVERS; id++) + if (server_fd[id] == fd) + break; + if(id == MAX_MAP_SERVERS || session[fd]->eof) { + if (id < MAX_MAP_SERVERS) { + memset(&server[id], 0, sizeof(struct mmo_map_server)); + printf("Map-server %d (session #%d) has disconnected.\n", id, fd); + sprintf(tmp_sql, "DELETE FROM `ragsrvinfo` WHERE `index`='%d'", server_fd[id]); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + server_fd[id] = -1; + } + close(fd); + delete_session(fd); + return 0; + } + + while(RFIFOREST(fd) >= 2) { +// printf("parse_frommap : %d %d %x\n", fd, RFIFOREST(fd), RFIFOW(fd,0)); + + switch(RFIFOW(fd, 0)) { + case 0x2af7: + RFIFOSKIP(fd,2); + read_gm_account(); + break; + + // mapserver -> map names recv. + case 0x2afa: + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + memset(server[id].map, 0, sizeof(server[id].map)); + j = 0; + for(i = 4; i < RFIFOW(fd,2); i += 16) { + memcpy(server[id].map[j], RFIFOP(fd,i), 16); +// printf("set map %d.%d : %s\n", id, j, server[id].map[j]); + j++; + } + i = server[id].ip; + { + unsigned char *p = (unsigned char *)&server[id].ip; + printf("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n", + id, j, p[0], p[1], p[2], p[3], server[id].port); + printf("Map-server %d loading complete.\n", id); + } + WFIFOW(fd,0) = 0x2afb; + WFIFOB(fd,2) = 0; + memcpy(WFIFOP(fd,3), wisp_server_name, 24); // name for wisp to player + WFIFOSET(fd,27); + { + unsigned char buf[16384]; + int x; + if (j == 0) { + printf("WARNING: Map-Server %d have NO maps.\n", id); + // Transmitting maps information to the other map-servers + } else { + WBUFW(buf,0) = 0x2b04; + WBUFW(buf,2) = j * 16 + 10; + WBUFL(buf,4) = server[id].ip; + WBUFW(buf,8) = server[id].port; + memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 16); + mapif_sendallwos(fd, buf, WBUFW(buf,2)); + } + // Transmitting the maps of the other map-servers to the new map-server + for(x = 0; x < MAX_MAP_SERVERS; x++) { + if (server_fd[x] >= 0 && x != id) { + WFIFOW(fd,0) = 0x2b04; + WFIFOL(fd,4) = server[x].ip; + WFIFOW(fd,8) = server[x].port; + j = 0; + for(i = 0; i < MAX_MAP_PER_SERVER; i++) + if (server[x].map[i][0]) + memcpy(WFIFOP(fd,10+(j++)*16), server[x].map[i], 16); + if (j > 0) { + WFIFOW(fd,2) = j * 16 + 10; + WFIFOSET(fd,WFIFOW(fd,2)); + } + } + } + } + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + + // auth request + case 0x2afc: + if (RFIFOREST(fd) < 22) + return 0; +// printf("(AUTH request) auth_fifo search %d %d %d\n", RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOL(fd, 10)); + for(i = 0; i < AUTH_FIFO_SIZE; i++) { + if (auth_fifo[i].account_id == RFIFOL(fd,2) && + auth_fifo[i].char_id == RFIFOL(fd,6) && + auth_fifo[i].login_id1 == RFIFOL(fd,10) && +#if CMP_AUTHFIFO_LOGIN2 != 0 + // here, it's the only area where it's possible that we doesn't know login_id2 (map-server asks just after 0x72 packet, that doesn't given the value) + (auth_fifo[i].login_id2 == RFIFOL(fd,14) || RFIFOL(fd,14) == 0) && // relate to the versions higher than 18 +#endif + (!check_ip_flag || auth_fifo[i].ip == RFIFOL(fd,18)) && + !auth_fifo[i].delflag) { + auth_fifo[i].delflag = 1; + WFIFOW(fd,0) = 0x2afd; + WFIFOW(fd,2) = 16 + sizeof(struct mmo_charstatus); + WFIFOL(fd,4) = RFIFOL(fd,2); + WFIFOL(fd,8) = auth_fifo[i].login_id2; + WFIFOL(fd,12) = (unsigned long)auth_fifo[i].connect_until_time; + mmo_char_fromsql(auth_fifo[i].char_id, char_dat, 1); + char_dat[0].sex = auth_fifo[i].sex; + memcpy(WFIFOP(fd,16), &char_dat[0], sizeof(struct mmo_charstatus)); + WFIFOSET(fd, WFIFOW(fd,2)); + //printf("auth_fifo search success (auth #%d, account %d, character: %d).\n", i, RFIFOL(fd,2), RFIFOL(fd,6)); + break; + } + } + if (i == AUTH_FIFO_SIZE) { + WFIFOW(fd,0) = 0x2afe; + WFIFOL(fd,2) = RFIFOL(fd,2); + WFIFOSET(fd,6); +// printf("(AUTH request) auth_fifo search error!\n"); + } + RFIFOSKIP(fd,22); + break; + + // set MAP user + case 0x2aff: + if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + if (RFIFOW(fd,4) != server[id].users) + printf("map user: %d\n", RFIFOW(fd,4)); + server[id].users = RFIFOW(fd,4); + if(anti_freeze_enable) + server_freezeflag[id] = 5; // Map anti-freeze system. Counter. 5 ok, 4...0 freezed + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + + // char saving + case 0x2b01: + i = 0; + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + //check account + sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",char_db, RFIFOL(fd,4),RFIFOL(fd,8)); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); + if (sql_row) + i = atoi(sql_row[0]); + } + mysql_free_result(sql_res); + + if (i == 1) { + memcpy(&char_dat[0], RFIFOP(fd,12), sizeof(struct mmo_charstatus)); + mmo_char_tosql(RFIFOL(fd,8), char_dat); + //save to DB + } + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + + // req char selection + case 0x2b02: + if (RFIFOREST(fd) < 18) + return 0; + + if (auth_fifo_pos >= AUTH_FIFO_SIZE) + auth_fifo_pos = 0; + +// printf("(charselect) auth_fifo set %d - account_id:%08x login_id1:%08x\n", auth_fifo_pos, RFIFOL(fd, 2), RFIFOL(fd, 6)); + auth_fifo[auth_fifo_pos].account_id = RFIFOL(fd, 2); + auth_fifo[auth_fifo_pos].char_id = 0; + auth_fifo[auth_fifo_pos].login_id1 = RFIFOL(fd, 6); + auth_fifo[auth_fifo_pos].login_id2 = RFIFOL(fd,10); + auth_fifo[auth_fifo_pos].delflag = 2; + auth_fifo[auth_fifo_pos].char_pos = 0; + auth_fifo[auth_fifo_pos].connect_until_time = 0; // unlimited/unknown time by default (not display in map-server) + auth_fifo[auth_fifo_pos].ip = RFIFOL(fd,14); + auth_fifo_pos++; + + WFIFOW(fd, 0) = 0x2b03; + WFIFOL(fd, 2) = RFIFOL(fd, 2); + WFIFOB(fd, 6) = 0; + WFIFOSET(fd, 7); + + RFIFOSKIP(fd, 18); + break; + + // request "change map server" + case 0x2b05: + if (RFIFOREST(fd) < 49) + return 0; + + if (auth_fifo_pos >= AUTH_FIFO_SIZE) + auth_fifo_pos = 0; + + WFIFOW(fd, 0) = 0x2b06; + memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 42); +// printf("(map change) auth_fifo set %d - account_id:%08x login_id1:%08x\n", auth_fifo_pos, RFIFOL(fd, 2), RFIFOL(fd, 6)); + auth_fifo[auth_fifo_pos].account_id = RFIFOL(fd, 2); + auth_fifo[auth_fifo_pos].login_id1 = RFIFOL(fd, 6); + auth_fifo[auth_fifo_pos].login_id2 = RFIFOL(fd,10); + auth_fifo[auth_fifo_pos].char_id = RFIFOL(fd,14); + auth_fifo[auth_fifo_pos].delflag = 0; + auth_fifo[auth_fifo_pos].sex = RFIFOB(fd,44); + auth_fifo[auth_fifo_pos].ip = RFIFOL(fd,45); + + sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", char_db, RFIFOL(fd,2), RFIFOL(fd,14)); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + + if (( sql_row = mysql_fetch_row(sql_res))) { + i = atoi(sql_row[0]); + mysql_free_result(sql_res); + + auth_fifo[auth_fifo_pos].char_pos = auth_fifo[auth_fifo_pos].char_id; + auth_fifo_pos++; + + WFIFOL(fd,6) = 0; + break; + } + if (i == 0) + WFIFOW(fd,6) = 1; + + WFIFOSET(fd,44); + RFIFOSKIP(fd,49); + break; + + // char name check + case 0x2b08: + if (RFIFOREST(fd) < 6) + return 0; + + sprintf(tmp_sql, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", char_db, RFIFOL(fd,2)); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + + sql_row = mysql_fetch_row(sql_res); + + WFIFOW(fd,0) = 0x2b09; + WFIFOL(fd,2) = RFIFOL(fd,2); + + if (sql_row) + memcpy(WFIFOP(fd,6), sql_row[0], 24); + else + memcpy(WFIFOP(fd,6), unknown_char_name, 24); + mysql_free_result(sql_res); + + WFIFOSET(fd,30); + + RFIFOSKIP(fd,6); + break; + +/* // I want become GM - fuck! + case 0x2b0a: + if(RFIFOREST(fd)<4) + return 0; + if(RFIFOREST(fd) login %d %s %d\n", RFIFOL(fd, 4), RFIFOP(fd, 8), RFIFOW(fd, 2)); + RFIFOSKIP(fd, RFIFOW(fd, 2)); + break; + */ + + // account_reg保存要求 + case 0x2b10: + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + { + struct global_reg reg[ACCOUNT_REG2_NUM]; + int j,p,acc; + acc=RFIFOL(fd,4); + for(p=8,j=0;p 0) { // don't send request if no login-server + WFIFOW(login_fd, 0) = 0x2728; + memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0), RFIFOW(fd,2)); + WFIFOSET(login_fd, WFIFOW(login_fd,2)); + } + // ワールドへの同垢ログインがなければmapサーバーに送る必要はない + //memcpy(buf,RFIFOP(fd,0),RFIFOW(fd,2)); + //WBUFW(buf,0)=0x2b11; + //mapif_sendall(buf,WBUFW(buf,2)); + RFIFOSKIP(fd,RFIFOW(fd,2)); +// printf("char: save_account_reg (from map)\n"); + } + break; + + // Map server send information to change an email of an account -> login-server + case 0x2b0c: + if (RFIFOREST(fd) < 86) + return 0; + if (login_fd > 0) { // don't send request if no login-server + memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0), 86); // 0x2722 .L .40B .40B + WFIFOW(login_fd,0) = 0x2722; + WFIFOSET(login_fd, 86); + } + RFIFOSKIP(fd, 86); + break; + + // Receiving from map-server a status change resquest. Transmission to login-server (by Yor) + case 0x2b0e: + if (RFIFOREST(fd) < 44) + return 0; + { + char character_name[24]; + int acc = RFIFOL(fd,2); // account_id of who ask (-1 if nobody) + memcpy(character_name, RFIFOP(fd,6), 24); + character_name[sizeof(character_name) -1] = '\0'; + // prepare answer + WFIFOW(fd,0) = 0x2b0f; // answer + WFIFOL(fd,2) = acc; // who want do operation + WFIFOW(fd,30) = RFIFOW(fd, 30); // type of operation: 1-block, 2-ban, 3-unblock, 4-unban + sprintf(tmp_sql, "SELECT `account_id`,`name` FROM `%s` WHERE `name` = '%s'",char_db, character_name); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (select `char`)- %s\n", mysql_error(&mysql_handle)); + } + + sql_res = mysql_store_result(&mysql_handle); + + if (sql_res) { + if (mysql_num_rows(sql_res)) { + sql_row = mysql_fetch_row(sql_res); + memcpy(WFIFOP(fd,6), sql_row[1], 24); // put correct name if found + WFIFOW(fd,32) = 0; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + switch(RFIFOW(fd, 30)) { + case 1: // block + if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd,0) = 0x2724; + WFIFOL(login_fd,2) = atoi(sql_row[0]); // account value + WFIFOL(login_fd,6) = 5; // status of the account + WFIFOSET(login_fd, 10); +// printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 5); + } else + WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } else + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 2: // ban + if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd, 0) = 0x2725; + WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value + WFIFOW(login_fd, 6) = RFIFOW(fd,32); // year + WFIFOW(login_fd, 8) = RFIFOW(fd,34); // month + WFIFOW(login_fd,10) = RFIFOW(fd,36); // day + WFIFOW(login_fd,12) = RFIFOW(fd,38); // hour + WFIFOW(login_fd,14) = RFIFOW(fd,40); // minute + WFIFOW(login_fd,16) = RFIFOW(fd,42); // second + WFIFOSET(login_fd,18); +// printf("char : status -> login: account %d, ban: %dy %dm %dd %dh %dmn %ds\n", +// char_dat[i].account_id, (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), (short)RFIFOW(fd,38), (short)RFIFOW(fd,40), (short)RFIFOW(fd,42)); + } else + WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } else + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 3: // unblock + if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd,0) = 0x2724; + WFIFOL(login_fd,2) = atoi(sql_row[0]); // account value + WFIFOL(login_fd,6) = 0; // status of the account + WFIFOSET(login_fd, 10); +// printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 0); + } else + WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } else + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 4: // unban + if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd, 0) = 0x272a; + WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value + WFIFOSET(login_fd, 6); +// printf("char : status -> login: account %d, unban request\n", char_dat[i].account_id); + } else + WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } else + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + case 5: // changesex + if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd, 0) = 0x2727; + WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value + WFIFOSET(login_fd, 6); +// printf("char : status -> login: account %d, change sex request\n", char_dat[i].account_id); + } else + WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } else + WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + break; + } + } else { + // character name not found + memcpy(WFIFOP(fd,6), character_name, 24); + WFIFOW(fd,32) = 1; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline + } + // send answer if a player ask, not if the server ask + if (acc != -1) { + WFIFOSET(fd, 34); + } + } + } + RFIFOSKIP(fd, 44); + break; + + // Recieve rates [Wizputer] + case 0x2b16: + if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,8)) + return 0; + sprintf(tmp_sql, "INSERT INTO `ragsrvinfo` SET `index`='%d',`name`='%s',`exp`='%d',`jexp`='%d',`drop`='%d',`motd`='%s'", + fd, server_name, RFIFOW(fd,2), RFIFOW(fd,4), RFIFOW(fd,6), RFIFOP(fd,10)); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + RFIFOSKIP(fd,RFIFOW(fd,8)); + break; + + // Character disconnected set online 0 [Wizputer] + case 0x2b17: + if (RFIFOREST(fd) < 6 ) + return 0; + //printf("Setting %d char offline\n",RFIFOL(fd,2)); + sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `char_id`='%d'", char_db, RFIFOL(fd,2)); + if (mysql_query(&mysql_handle, tmp_sql)) + printf("DB server Error (update online `%s`)- %s\n", char_db, mysql_error(&mysql_handle)); + RFIFOSKIP(fd,6); + break; + + default: + // inter server - packet + { + int r = inter_parse_frommap(fd); + if (r == 1) break; // processed + if (r == 2) return 0; // need more packet + } + + // no inter server packet. no char server packet -> disconnect + printf("parse_frommap: unknown packet %x! \n", RFIFOW(fd,0)); + session[fd]->eof = 1; + return 0; + } + } + return 0; +} + +int search_mapserver(char *map) { + int i, j; + char temp_map[16]; + int temp_map_len; + +// printf("Searching the map-server for map '%s'... ", map); + strncpy(temp_map, map, sizeof(temp_map)); + temp_map[sizeof(temp_map)-1] = '\0'; + if (strchr(temp_map, '.') != NULL) + temp_map[strchr(temp_map, '.') - temp_map + 1] = '\0'; // suppress the '.gat', but conserve the '.' to be sure of the name of the map + + temp_map_len = strlen(temp_map); + for(i = 0; i < MAX_MAP_SERVERS; i++) + if (server_fd[i] >= 0) + for (j = 0; server[i].map[j][0]; j++) + //printf("%s : %s = %d\n", server[i].map[j], map, strncmp(server[i].map[j], temp_map, temp_map_len)); + if (strncmp(server[i].map[j], temp_map, temp_map_len) == 0) { +// printf("found -> server #%d.\n", i); + return i; + } + +// printf("not found.\n"); + return -1; +} + +int char_mapif_init(int fd) { + return inter_mapif_init(fd); +} + +//----------------------------------------------------- +// Test to know if an IP come from LAN or WAN. by [Yor] +//----------------------------------------------------- +int lan_ip_check(unsigned char *p){ + int i; + int lancheck = 1; + int subneti[4]; + unsigned int k0, k1, k2, k3; + + sscanf(lan_map_ip, "%d.%d.%d.%d", &k0, &k1, &k2, &k3); + subneti[0] = k0; subneti[1] = k1; subneti[2] = k2; subneti[3] = k3; + +// printf("lan_ip_check: to compare: %d.%d.%d.%d, network: %d.%d.%d.%d/%d.%d.%d.%d\n", +// p[0], p[1], p[2], p[3], +// subneti[0], subneti[1], subneti[2], subneti[3], +// subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]); + for(i = 0; i < 4; i++) { + if ((subneti[i] & subnetmaski[i]) != (p[i] & subnetmaski[i])) { + lancheck = 0; + break; + } + } +// printf("LAN test (result): %s source\033[0m.\n", (lancheck) ? "\033[1;36mLAN" : "\033[1;32mWAN"); + return lancheck; +} + +int parse_char(int fd) { + int i, ch = 0; + char email[40]; + struct char_session_data *sd; + unsigned char *p = (unsigned char *) &session[fd]->client_addr.sin_addr; + + if(login_fd < 0 || session[fd]->eof) { + if (fd == login_fd) + login_fd = -1; + close(fd); + delete_session(fd); + return 0; + } + + sd = session[fd]->session_data; + + while(RFIFOREST(fd) >= 2) { +// if (RFIFOW(fd, 0) < 30000) +// printf("parse_char : %d %d %x\n", fd, RFIFOREST(fd), RFIFOW(fd, 0)); + + switch(RFIFOW(fd, 0)) { + case 0x20b: //20040622 encryption ragexe correspondence + if (RFIFOREST(fd) < 19) + return 0; + RFIFOSKIP(fd,19); + break; + + case 0x65: // request to connect + printf("request connect - account_id:%d/login_id1:%d/login_id2:%d\n", RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOL(fd, 10)); + if (RFIFOREST(fd) < 17) + return 0; + { +/*removed from isGM setup + if (isGM(RFIFOL(fd,2))) + printf("Account Logged On; Account ID: %d (GM level %d).\n", RFIFOL(fd,2), isGM(RFIFOL(fd,2))); + else + printf("Account Logged On; Account ID: %d.\n", RFIFOL(fd,2)); +*/ + if (sd == NULL) { + CREATE(session[fd]->session_data, struct char_session_data, 1); + sd = session[fd]->session_data; + sd->connect_until_time = 0; // unknow or illimited (not displaying on map-server) + } + sd->account_id = RFIFOL(fd, 2); + sd->login_id1 = RFIFOL(fd, 6); + sd->login_id2 = RFIFOL(fd, 10); + sd->sex = RFIFOB(fd, 16); + + WFIFOL(fd, 0) = RFIFOL(fd, 2); + WFIFOSET(fd, 4); + + for(i = 0; i < AUTH_FIFO_SIZE; i++) { + if (auth_fifo[i].account_id == sd->account_id && + auth_fifo[i].login_id1 == sd->login_id1 && +#if CMP_AUTHFIFO_LOGIN2 != 0 + auth_fifo[i].login_id2 == sd->login_id2 && // relate to the versions higher than 18 +#endif + (!check_ip_flag || auth_fifo[i].ip == session[fd]->client_addr.sin_addr.s_addr) && + auth_fifo[i].delflag == 2) { + auth_fifo[i].delflag = 1; + + if (max_connect_user == 0 || count_users() < max_connect_user) { + if (login_fd > 0) { // don't send request if no login-server + // request to login-server to obtain e-mail/time limit + WFIFOW(login_fd,0) = 0x2716; + WFIFOL(login_fd,2) = sd->account_id; + WFIFOSET(login_fd,6); + } + // send characters to player + mmo_char_send006b(fd, sd); + } else { + // refuse connection (over populated) + WFIFOW(fd,0) = 0x6c; + WFIFOW(fd,2) = 0; + WFIFOSET(fd,3); + } +// printf("connection request> set delflag 1(o:2)- account_id:%d/login_id1:%d(fifo_id:%d)\n", sd->account_id, sd->login_id1, i); + break; + } + } + if (i == AUTH_FIFO_SIZE) { + if (login_fd > 0) { // don't send request if no login-server + WFIFOW(login_fd,0) = 0x2712; // ask login-server to authentify an account + WFIFOL(login_fd,2) = sd->account_id; + WFIFOL(login_fd,6) = sd->login_id1; + WFIFOL(login_fd,10) = sd->login_id2; + WFIFOB(login_fd,14) = sd->sex; + WFIFOL(login_fd,15) = session[fd]->client_addr.sin_addr.s_addr; + WFIFOSET(login_fd,19); + } else { // if no login-server, we must refuse connection + WFIFOW(fd,0) = 0x6c; + WFIFOW(fd,2) = 0; + WFIFOSET(fd,3); + } + } + } + RFIFOSKIP(fd, 17); + break; + + case 0x66: // char select +// printf("0x66> request connect - account_id:%d/char_num:%d\n",sd->account_id,RFIFOB(fd, 2)); + if (RFIFOREST(fd) < 3) + return 0; + + sprintf(tmp_sql, "SELECT `char_id` FROM `%s` WHERE `account_id`='%d' AND `char_num`='%d'",char_db, sd->account_id, RFIFOB(fd, 2)); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + + sql_row = mysql_fetch_row(sql_res); + + if (sql_row) + mmo_char_fromsql(atoi(sql_row[0]), char_dat, 0); + else { + mysql_free_result(sql_res); + RFIFOSKIP(fd, 3); + break; + } + + sprintf(tmp_sql,"INSERT INTO `%s`(`time`, `account_id`,`char_num`,`name`) VALUES (NOW(), '%d', '%d', '%s')", + charlog_db, sd->account_id, RFIFOB(fd, 2), char_dat[0].name); + //query + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + printf("(\033[1;64m%d\033[0m) char selected (\033[1;32m%d\033[0m) \033[1;32m%s\033[0m" RETCODE, sd->account_id, RFIFOB(fd, 2), char_dat[0].name); + + i = search_mapserver(char_dat[0].last_point.map); + + // if map is not found, we check major cities + if (i < 0) { + if ((i = search_mapserver("prontera.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[0].last_point.map, "prontera.gat", 16); + char_dat[0].last_point.x = 273; // savepoint coordonates + char_dat[0].last_point.y = 354; + } else if ((i = search_mapserver("geffen.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[0].last_point.map, "geffen.gat", 16); + char_dat[0].last_point.x = 120; // savepoint coordonates + char_dat[0].last_point.y = 100; + } else if ((i = search_mapserver("morocc.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[0].last_point.map, "morocc.gat", 16); + char_dat[0].last_point.x = 160; // savepoint coordonates + char_dat[0].last_point.y = 94; + } else if ((i = search_mapserver("alberta.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[0].last_point.map, "alberta.gat", 16); + char_dat[0].last_point.x = 116; // savepoint coordonates + char_dat[0].last_point.y = 57; + } else if ((i = search_mapserver("payon.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[0].last_point.map, "payon.gat", 16); + char_dat[0].last_point.x = 87; // savepoint coordonates + char_dat[0].last_point.y = 117; + } else if ((i = search_mapserver("izlude.gat")) >= 0) { // check is done without 'gat'. + memcpy(char_dat[0].last_point.map, "izlude.gat", 16); + char_dat[0].last_point.x = 94; // savepoint coordonates + char_dat[0].last_point.y = 103; + } else { + int j; + // get first online server + i = 0; + for(j = 0; j < MAX_MAP_SERVERS; j++) + if (server_fd[j] >= 0 && server[j].map[0][0]) { + i = j; + printf("Map-server #%d found with a map: '%s'.\n", j, server[j].map[0]); + break; + } + // if no map-servers are connected, we send: server closed + if (j == MAX_MAP_SERVERS) { + WFIFOW(fd,0) = 0x81; + WFIFOL(fd,2) = 1; // 01 = Server closed + WFIFOSET(fd,3); + RFIFOSKIP(fd,3); + break; + } + } + } + WFIFOW(fd, 0) =0x71; + WFIFOL(fd, 2) =char_dat[0].char_id; + memcpy(WFIFOP(fd, 6), char_dat[0].last_point.map, 16); + //Lan check added by Kashy + if (lan_ip_check(p)) + WFIFOL(fd, 22) = inet_addr(lan_map_ip); + else + WFIFOL(fd, 22) = server[i].ip; + WFIFOW(fd, 26) = server[i].port; + WFIFOSET(fd, 28); + + if (auth_fifo_pos >= AUTH_FIFO_SIZE) { + auth_fifo_pos = 0; + } +// printf("auth_fifo set (auth_fifo_pos:%d) - account_id:%d char_id:%d login_id1:%d\n", auth_fifo_pos, sd->account_id, char_dat[0].char_id, sd->login_id1); + auth_fifo[auth_fifo_pos].account_id = sd->account_id; + auth_fifo[auth_fifo_pos].char_id = char_dat[0].char_id; + auth_fifo[auth_fifo_pos].login_id1 = sd->login_id1; + auth_fifo[auth_fifo_pos].login_id2 = sd->login_id2; + auth_fifo[auth_fifo_pos].delflag = 0; + //auth_fifo[auth_fifo_pos].char_pos = sd->found_char[ch]; + auth_fifo[auth_fifo_pos].char_pos = 0; + auth_fifo[auth_fifo_pos].sex = sd->sex; + auth_fifo[auth_fifo_pos].connect_until_time = sd->connect_until_time; + auth_fifo[auth_fifo_pos].ip = session[fd]->client_addr.sin_addr.s_addr; + auth_fifo_pos++; +// printf("0x66> end\n"); + RFIFOSKIP(fd, 3); + break; + + case 0x67: // make new +// printf("0x67>request make new char\n"); + if (RFIFOREST(fd) < 37) + return 0; + i = make_new_char_sql(fd, RFIFOP(fd, 2)); + if (i < 0) { + WFIFOW(fd, 0) = 0x6e; + WFIFOB(fd, 2) = 0x00; + WFIFOSET(fd, 3); + RFIFOSKIP(fd, 37); + break; + } + + WFIFOW(fd, 0) = 0x6d; + memset(WFIFOP(fd, 2), 0x00, 106); + + mmo_char_fromsql(i, char_dat, 0); + i = 0; + WFIFOL(fd, 2) = char_dat[i].char_id; + WFIFOL(fd,2+4) = char_dat[i].base_exp; + WFIFOL(fd,2+8) = char_dat[i].zeny; + WFIFOL(fd,2+12) = char_dat[i].job_exp; + WFIFOL(fd,2+16) = char_dat[i].job_level; + + WFIFOL(fd,2+28) = char_dat[i].karma; + WFIFOL(fd,2+32) = char_dat[i].manner; + + WFIFOW(fd,2+40) = 0x30; + WFIFOW(fd,2+42) = (char_dat[i].hp > 0x7fff) ? 0x7fff : char_dat[i].hp; + WFIFOW(fd,2+44) = (char_dat[i].max_hp > 0x7fff) ? 0x7fff : char_dat[i].max_hp; + WFIFOW(fd,2+46) = (char_dat[i].sp > 0x7fff) ? 0x7fff : char_dat[i].sp; + WFIFOW(fd,2+48) = (char_dat[i].max_sp > 0x7fff) ? 0x7fff : char_dat[i].max_sp; + WFIFOW(fd,2+50) = DEFAULT_WALK_SPEED; // char_dat[i].speed; + WFIFOW(fd,2+52) = char_dat[i].class; + WFIFOW(fd,2+54) = char_dat[i].hair; + + WFIFOW(fd,2+58) = char_dat[i].base_level; + WFIFOW(fd,2+60) = char_dat[i].skill_point; + + WFIFOW(fd,2+64) = char_dat[i].shield; + WFIFOW(fd,2+66) = char_dat[i].head_top; + WFIFOW(fd,2+68) = char_dat[i].head_mid; + WFIFOW(fd,2+70) = char_dat[i].hair_color; + + memcpy(WFIFOP(fd,2+74), char_dat[i].name, 24); + + WFIFOB(fd,2+98) = char_dat[i].str; + WFIFOB(fd,2+99) = char_dat[i].agi; + WFIFOB(fd,2+100) = char_dat[i].vit; + WFIFOB(fd,2+101) = char_dat[i].int_; + WFIFOB(fd,2+102) = char_dat[i].dex; + WFIFOB(fd,2+103) = char_dat[i].luk; + WFIFOB(fd,2+104) = char_dat[i].char_num; + + WFIFOSET(fd, 108); + RFIFOSKIP(fd, 37); + //to do + for(ch = 0; ch < 9; ch++) { + if (sd->found_char[ch] == -1) { + sd->found_char[ch] = char_dat[i].char_id; + break; + } + } + + case 0x68: // delete + if (RFIFOREST(fd) < 46) + return 0; + printf("\033[1;31m Request Char Del:\033[0m \033[1;32m%d\033[0m(\033[1;32m%d\033[0m)\n", sd->account_id, RFIFOL(fd, 2)); + memcpy(email, RFIFOP(fd,6), 40); + sprintf(tmp_sql, "SELECT `email` FROM `%s` WHERE `%s`='%d'",login_db, login_db_account_id, sd->account_id); + if (mysql_query(&lmysql_handle, tmp_sql)) { + printf("\033[1;31m DB server Error Delete Char data - %s \033[0m \n", mysql_error(&lmysql_handle)); + } + sql_res = mysql_store_result(&lmysql_handle); + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); + if (strcmp(email,sql_row[0]) == 0) { + mysql_free_result(sql_res); + } else { + WFIFOW(fd, 0) = 0x70; + WFIFOB(fd, 2) = 0; + WFIFOSET(fd, 3); + RFIFOSKIP(fd, 46); + mysql_free_result(sql_res); + break; + } + } else { + WFIFOW(fd, 0) = 0x70; + WFIFOB(fd, 2) = 0; + WFIFOSET(fd, 3); + RFIFOSKIP(fd, 46); + mysql_free_result(sql_res); + break; + } + sprintf(tmp_sql, "SELECT `name`,`partner_id` FROM `%s` WHERE `char_id`='%d'",char_db, RFIFOL(fd,2)); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + if(sql_res) + sql_row = mysql_fetch_row(sql_res); + + if (sql_res && sql_row[0]) { + //delete char from SQL + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",pet_db, RFIFOL(fd, 2)); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",inventory_db, RFIFOL(fd, 2)); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",cart_db, RFIFOL(fd, 2)); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",memo_db, RFIFOL(fd, 2)); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",skill_db, RFIFOL(fd, 2)); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",char_db, RFIFOL(fd, 2)); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + //Divorce [Wizputer] + if (sql_row[1] != 0) { + char buf[64]; + sprintf(tmp_sql,"UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d'",char_db,atoi(sql_row[1])); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sprintf(tmp_sql,"DELETE FROM `%s` WHERE (`nameid`='%d' OR `nameid`='%d') AND `char_id`='%d'",inventory_db,WEDDING_RING_M,WEDDING_RING_F,atoi(sql_row[1])); + if(mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + WBUFW(buf,0) = 0x2b12; + WBUFL(buf,2) = atoi(sql_row[0]); + WBUFL(buf,6) = atoi(sql_row[1]); + mapif_sendall(buf,10); + } + // Also delete info from guildtables. + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",guild_member_db, RFIFOL(fd,2)); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql, "SELECT `guild_id` FROM `%s` WHERE `master` = '%s'", guild_db, sql_row[0]); + + if (mysql_query(&mysql_handle, tmp_sql) == 0) { + sql_res = mysql_store_result(&mysql_handle); + + if (sql_res != NULL) { + if (mysql_num_rows(sql_res) != 0) { + sql_row = mysql_fetch_row(sql_res); + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_db, atoi(sql_row[0])); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_member_db, atoi(sql_row[0])); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_castle_db, atoi(sql_row[0])); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_storage_db, atoi(sql_row[0])); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d' OR `alliance_id` = '%d'", guild_alliance_db, atoi(sql_row[0]), atoi(sql_row[0])); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_position_db, atoi(sql_row[0])); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_skill_db, atoi(sql_row[0])); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '%d'", guild_expulsion_db, atoi(sql_row[0])); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + mysql_free_result(sql_res); + } + } else { + if (mysql_errno(&mysql_handle) != 0) { + printf("Database server error: %s\n", mysql_error(&mysql_handle)); + } + } + } else { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + } + + for(i = 0; i < 9; i++) { + printf("char comp: %d - %d (%d)\n", sd->found_char[i], RFIFOL(fd, 2), sd->account_id); + if (sd->found_char[i] == RFIFOL(fd, 2)) { + for(ch = i; ch < 9-1; ch++) + sd->found_char[ch] = sd->found_char[ch+1]; + sd->found_char[8] = -1; + break; + } + } + if (i == 9) { // reject + WFIFOW(fd, 0) = 0x70; + WFIFOB(fd, 2) = 0; + WFIFOSET(fd, 3); + } else { // deleted! + WFIFOW(fd, 0) = 0x6f; + WFIFOSET(fd, 2); + } + RFIFOSKIP(fd, 46); + break; + + case 0x2af8: // login as map-server + if (RFIFOREST(fd) < 60) + return 0; + WFIFOW(fd, 0) = 0x2af9; + for(i = 0; i < MAX_MAP_SERVERS; i++) { + if (server_fd[i] < 0) + break; + } + if (i == MAX_MAP_SERVERS || strcmp(RFIFOP(fd,2), userid) || strcmp(RFIFOP(fd,26), passwd)) { + WFIFOB(fd,2) = 3; + WFIFOSET(fd, 3); + } else { +// int len; + WFIFOB(fd,2) = 0; + WFIFOSET(fd, 3); + session[fd]->func_parse = parse_frommap; + server_fd[i] = fd; + if(anti_freeze_enable) + server_freezeflag[i] = 5; // Map anti-freeze system. Counter. 5 ok, 4...0 freezed + server[i].ip = RFIFOL(fd, 54); + server[i].port = RFIFOW(fd, 58); + server[i].users = 0; + memset(server[i].map, 0, sizeof(server[i].map)); + realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK); + char_mapif_init(fd); + // send gm acccounts level to map-servers +/* removed by CLOWNISIUS due to isGM + len = 4; + WFIFOW(fd,0) = 0x2b15; + for(i = 0; i < GM_num; i++) { + WFIFOL(fd,len) = gm_account[i].account_id; + WFIFOB(fd,len+4) = (unsigned char)gm_account[i].level; + len += 5; + } + WFIFOW(fd,2) = len; + WFIFOSET(fd,len);*/ + } + RFIFOSKIP(fd,60); + break; + + case 0x187: // Alive? + if (RFIFOREST(fd) < 6) { + return 0; + } + RFIFOSKIP(fd, 6); + break; + + case 0x7530: // Athena info get + WFIFOW(fd, 0) = 0x7531; + WFIFOB(fd, 2) = ATHENA_MAJOR_VERSION; + WFIFOB(fd, 3) = ATHENA_MINOR_VERSION; + WFIFOB(fd, 4) = ATHENA_REVISION; + WFIFOB(fd, 5) = ATHENA_RELEASE_FLAG; + WFIFOB(fd, 6) = ATHENA_OFFICIAL_FLAG; + WFIFOB(fd, 7) = ATHENA_SERVER_INTER | ATHENA_SERVER_CHAR; + WFIFOW(fd, 8) = ATHENA_MOD_VERSION; + WFIFOSET(fd, 10); + RFIFOSKIP(fd, 2); + return 0; + + case 0x7532: // disconnect(default also disconnect) + default: + session[fd]->eof = 1; + return 0; + } + } + RFIFOFLUSH(fd); + + return 0; +} + +// MAP send all +int mapif_sendall(unsigned char *buf, unsigned int len) { + int i, c; + int fd; + + c = 0; + for(i = 0; i < MAX_MAP_SERVERS; i++) { + if ((fd = server_fd[i]) >= 0) { + memcpy(WFIFOP(fd,0), buf, len); + WFIFOSET(fd,len); + c++; + } + } + + return c; +} + +int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len) { + int i, c; + int fd; + + c = 0; + for(i=0, c=0;i= 0 && fd != sfd) { + memcpy(WFIFOP(fd,0), buf, len); + WFIFOSET(fd, len); + c++; + } + } + + return c; +} + +int mapif_send(int fd, unsigned char *buf, unsigned int len) { + int i; + + if (fd >= 0) { + for(i = 0; i < MAX_MAP_SERVERS; i++) { + if (fd == server_fd[i]) { + memcpy(WFIFOP(fd,0), buf, len); + WFIFOSET(fd,len); + return 1; + } + } + } + return 0; +} + +int send_users_tologin(int tid, unsigned int tick, int id, int data) { + int users = count_users(); + char buf[16]; + + if (login_fd > 0 && session[login_fd]) { + // send number of user to login server + WFIFOW(login_fd,0) = 0x2714; + WFIFOL(login_fd,2) = users; + WFIFOSET(login_fd,6); + } + // send number of players to all map-servers + WBUFW(buf,0) = 0x2b00; + WBUFL(buf,2) = users; + mapif_sendall(buf, 6); + + return 0; +} + +int check_connect_login_server(int tid, unsigned int tick, int id, int data) { + if (login_fd <= 0 || session[login_fd] == NULL) { + printf("Attempt to connect to login-server...\n"); + login_fd = make_connection(login_ip, login_port); + session[login_fd]->func_parse = parse_tologin; + realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK); + WFIFOW(login_fd,0) = 0x2710; + memset(WFIFOP(login_fd,2), 0, 24); + memcpy(WFIFOP(login_fd,2), userid, strlen(userid) < 24 ? strlen(userid) : 24); + memset(WFIFOP(login_fd,26), 0, 24); + memcpy(WFIFOP(login_fd,26), passwd, strlen(passwd) < 24 ? strlen(passwd) : 24); + WFIFOL(login_fd,50) = 0; + WFIFOL(login_fd,54) = char_ip; + WFIFOL(login_fd,58) = char_port; + memset(WFIFOP(login_fd,60), 0, 20); + memcpy(WFIFOP(login_fd,60), server_name, strlen(server_name) < 20 ? strlen(server_name) : 20); + WFIFOW(login_fd,80) = 0; + WFIFOW(login_fd,82) = char_maintenance; + WFIFOW(login_fd,84) = char_new; + WFIFOSET(login_fd,86); + } + return 0; +} + +//---------------------------------------------------------- +// Return numerical value of a switch configuration by [Yor] +// on/off, english, fran軋is, deutsch, espaol +//---------------------------------------------------------- +int config_switch(const char *str) { + if (strcmpi(str, "on") == 0 || strcmpi(str, "yes") == 0 || strcmpi(str, "oui") == 0 || strcmpi(str, "ja") == 0 || strcmpi(str, "si") == 0) + return 1; + if (strcmpi(str, "off") == 0 || strcmpi(str, "no") == 0 || strcmpi(str, "non") == 0 || strcmpi(str, "nein") == 0) + return 0; + + return atoi(str); +} + +// Lan Support conf reading added by Kashy +int char_lan_config_read(const char *lancfgName){ + char subnetmask[128]; + char line[1024], w1[1024], w2[1024]; + FILE *fp; + struct hostent * h = NULL; + + if ((fp = fopen(lancfgName, "r")) == NULL) { + printf("file not found: %s\n", lancfgName); + return 1; + } + + printf("Start reading of Lan Support configuration file\n"); + + while(fgets(line, sizeof(line)-1, fp)){ + if (line[0] == '/' && line[1] == '/') + continue; + + if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2) + continue; + + else if (strcmpi(w1, "lan_map_ip") == 0) { + h = gethostbyname(w2); + if (h != NULL) { + sprintf(lan_map_ip, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } else { + strncpy(lan_map_ip, w2, sizeof(lan_map_ip)); + lan_map_ip[sizeof(lan_map_ip)-1] = 0; + } + printf("set Lan_map_IP : %s\n", lan_map_ip); + } + + else if (strcmpi(w1, "subnetmask") == 0) { + unsigned int k0, k1, k2, k3; + strcpy(subnetmask, w2); + sscanf(subnetmask, "%d.%d.%d.%d", &k0, &k1, &k2, &k3); + subnetmaski[0] = k0; subnetmaski[1] = k1; subnetmaski[2] = k2; subnetmaski[3] = k3; + printf("set subnetmask : %s\n", w2); + } + } + fclose(fp); + + printf("End reading of Lan Support configuration file\n"); + return 0; +} + +void do_final(void) { + printf("Doing final stage...\n"); + //mmo_char_sync(); + //inter_save(); + do_final_itemdb(); + //check SQL save progress. + //wait until save char complete + printf("waiting until char saving complete...\n"); + do { + sleep (0); + }while (save_flag != 0); + + sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `online`='1'", char_db); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (insert `char`)- %s\n", mysql_error(&mysql_handle)); + } + + sprintf(tmp_sql,"DELETE FROM `ragsrvinfo"); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error (insert `char`)- %s\n", mysql_error(&mysql_handle)); + } + + if (gm_account != NULL) + free(gm_account); + + free(char_dat); + free(gm_account); + delete_session(login_fd); + delete_session(char_fd); + + mysql_close(&mysql_handle); + mysql_close(&lmysql_handle); + + printf("ok! all done...\n"); +} + +void sql_config_read(const char *cfgName){ /* Kalaspuff, to get login_db */ + char line[1024], w1[1024], w2[1024]; + FILE *fp; + + printf("reading configure: %s\n", cfgName); + + if ((fp = fopen(cfgName, "r")) == NULL) { + printf("file not found: %s\n", cfgName); + exit(1); + } + + while(fgets(line, sizeof(line)-1, fp)){ + if(line[0] == '/' && line[1] == '/') + continue; + + if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2) + continue; + + if(strcmpi(w1, "login_db") == 0) { + strcpy(login_db, w2); + }else if(strcmpi(w1,"char_db")==0){ + strcpy(char_db,w2); + }else if(strcmpi(w1,"cart_db")==0){ + strcpy(cart_db,w2); + }else if(strcmpi(w1,"inventory_db")==0){ + strcpy(inventory_db,w2); + }else if(strcmpi(w1,"charlog_db")==0){ + strcpy(charlog_db,w2); + }else if(strcmpi(w1,"storage_db")==0){ + strcpy(storage_db,w2); + }else if(strcmpi(w1,"reg_db")==0){ + strcpy(reg_db,w2); + }else if(strcmpi(w1,"skill_db")==0){ + strcpy(skill_db,w2); + }else if(strcmpi(w1,"interlog_db")==0){ + strcpy(interlog_db,w2); + }else if(strcmpi(w1,"memo_db")==0){ + strcpy(memo_db,w2); + }else if(strcmpi(w1,"guild_db")==0){ + strcpy(guild_db,w2); + }else if(strcmpi(w1,"guild_alliance_db")==0){ + strcpy(guild_alliance_db,w2); + }else if(strcmpi(w1,"guild_castle_db")==0){ + strcpy(guild_castle_db,w2); + }else if(strcmpi(w1,"guild_expulsion_db")==0){ + strcpy(guild_expulsion_db,w2); + }else if(strcmpi(w1,"guild_member_db")==0){ + strcpy(guild_member_db,w2); + }else if(strcmpi(w1,"guild_skill_db")==0){ + strcpy(guild_skill_db,w2); + }else if(strcmpi(w1,"guild_position_db")==0){ + strcpy(guild_position_db,w2); + }else if(strcmpi(w1,"guild_storage_db")==0){ + strcpy(guild_storage_db,w2); + }else if(strcmpi(w1,"party_db")==0){ + strcpy(party_db,w2); + }else if(strcmpi(w1,"pet_db")==0){ + strcpy(pet_db,w2); + //Map server option to use SQL db or not + }else if(strcmpi(w1,"use_sql_db")==0){ // added for sql item_db read for char server [Valaris] + if (strcmpi(w2, "yes")) { + db_use_sqldbs = 1; + } else if (strcmpi(w2, "no")) { + db_use_sqldbs = 0; + } + printf("Using SQL dbs: %s\n",w2); + //custom columns for login database + }else if(strcmpi(w1,"login_db_level")==0){ + strcpy(login_db_level,w2); + }else if(strcmpi(w1,"login_db_account_id")==0){ + strcpy(login_db_account_id,w2); + }else if(strcmpi(w1,"lowest_gm_level")==0){ + lowest_gm_level = atoi(w2); + printf("set lowest_gm_level : %s\n",w2); + } + } + fclose(fp); + printf("reading configure done.....\n"); +} + +int char_config_read(const char *cfgName) { + struct hostent *h = NULL; + char line[1024], w1[1024], w2[1024]; + FILE *fp; + + if ((fp = fopen(cfgName, "r")) == NULL) { + printf("Configuration file not found: %s.\n", cfgName); + exit(1); + } + + while(fgets(line, sizeof(line)-1, fp)) { + if (line[0] == '/' && line[1] == '/') + continue; + + line[sizeof(line)-1] = '\0'; + if (sscanf(line,"%[^:]: %[^\r\n]", w1, w2) != 2) + continue; + + remove_control_chars(w1); + remove_control_chars(w2); + if (strcmpi(w1, "userid") == 0) { + memcpy(userid, w2, 24); + } else if (strcmpi(w1, "passwd") == 0) { + memcpy(passwd, w2, 24); + } else if (strcmpi(w1, "server_name") == 0) { + memcpy(server_name, w2, 16); + printf("%s server has been intialized\n", w2); + } else if (strcmpi(w1, "wisp_server_name") == 0) { + if (strlen(w2) >= 4) { + memcpy(wisp_server_name, w2, sizeof(wisp_server_name)); + wisp_server_name[sizeof(wisp_server_name) - 1] = '\0'; + } + } else if (strcmpi(w1, "login_ip") == 0) { + h = gethostbyname (w2); + if (h != NULL) { + printf("Login server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + sprintf(login_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } else + memcpy(login_ip_str,w2,16); + } else if (strcmpi(w1, "login_port") == 0) { + login_port=atoi(w2); + } else if (strcmpi(w1, "char_ip") == 0) { + h = gethostbyname (w2); + if(h != NULL) { + printf("Character server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + sprintf(char_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } else + memcpy(char_ip_str, w2, 16); + } else if (strcmpi(w1, "char_port") == 0) { + char_port = atoi(w2); + } else if (strcmpi(w1, "char_maintenance") == 0) { + char_maintenance = atoi(w2); + } else if (strcmpi(w1, "char_new")==0){ + char_new = atoi(w2); + } else if (strcmpi(w1, "max_connect_user") == 0) { + max_connect_user = atoi(w2); + if (max_connect_user < 0) + max_connect_user = 0; // unlimited online players + } else if (strcmpi(w1, "check_ip_flag") == 0) { + check_ip_flag = config_switch(w2); + } else if (strcmpi(w1, "autosave_time") == 0) { + autosave_interval = atoi(w2)*1000; + if (autosave_interval <= 0) + autosave_interval = DEFAULT_AUTOSAVE_INTERVAL; + } else if (strcmpi(w1, "start_point") == 0) { + char map[32]; + int x, y; + if (sscanf(w2,"%[^,],%d,%d", map, &x, &y) < 3) + continue; + if (strstr(map, ".gat") != NULL) { // Verify at least if '.gat' is in the map name + memcpy(start_point.map, map, 16); + start_point.x = x; + start_point.y = y; + } + } else if (strcmpi(w1, "start_zeny") == 0) { + start_zeny = atoi(w2); + if (start_zeny < 0) + start_zeny = 0; + } else if (strcmpi(w1, "start_weapon") == 0) { + start_zeny = atoi(w2); + if (start_weapon < 0) + start_weapon = 0; + } else if (strcmpi(w1, "start_armor") == 0) { + start_zeny = atoi(w2); + if (start_armor < 0) + start_armor = 0; + } else if (strcmpi(w1, "unknown_char_name") == 0) { + strcpy(unknown_char_name, w2); + unknown_char_name[24] = 0; + } else if (strcmpi(w1, "name_ignoring_case") == 0) { + name_ignoring_case = config_switch(w2); + } else if (strcmpi(w1, "char_name_option") == 0) { + char_name_option = atoi(w2); + } else if (strcmpi(w1, "char_name_letters") == 0) { + strcpy(char_name_letters, w2); + } else if (strcmpi(w1, "check_ip_flag") == 0) { + check_ip_flag = config_switch(w2); + // anti-freeze options [Valaris] + } else if(strcmpi(w1,"anti_freeze_enable")==0){ + anti_freeze_enable = config_switch(w2); + } else if (strcmpi(w1, "anti_freeze_interval") == 0) { + ANTI_FREEZE_INTERVAL = atoi(w2); + if (ANTI_FREEZE_INTERVAL < 5) + ANTI_FREEZE_INTERVAL = 5; // minimum 5 seconds + } else if (strcmpi(w1, "import") == 0) { + char_config_read(w2); + } + } + fclose(fp); + +//Read ItemDB + do_init_itemdb(); + + return 0; +} + +int do_init(int argc, char **argv){ + int i; + + for(i = 0; i < MAX_MAP_SERVERS; i++) { + memset(&server[i], 0, sizeof(struct mmo_map_server)); + server_fd[i] = -1; + } + + char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]); + char_lan_config_read((argc > 1) ? argv[1] : LAN_CONF_NAME); + sql_config_read(SQL_CONF_NAME); + + printf("charserver configuration reading done.....\n"); + + inter_init((argc > 2) ? argv[2] : inter_cfgName); // inter server テハア篳ュ + printf("interserver configuration reading done.....\n"); + + printf("start char server initializing.....\n"); + mmo_char_sql_init(); + printf("char server initializing done.....\n"); + + printf("set parser -> parse_char().....\n"); + set_defaultparse(parse_char); + + printf("set terminate function -> do_final().....\n"); + set_termfunc(do_final); + + printf("open port %d.....\n",char_port); + char_fd = make_listen_port(char_port); + + login_ip = inet_addr(login_ip_str); + char_ip = inet_addr(char_ip_str); + + // send ALIVE PING to login server. + printf("add interval tic (check_connect_login_server)....\n"); + i = add_timer_interval(gettick() + 10, check_connect_login_server, 0, 0, 10 * 1000); + + // send USER COUNT PING to login server. + printf("add interval tic (send_users_tologin)....\n"); + i = add_timer_interval(gettick() + 10, send_users_tologin, 0, 0, 5 * 1000); + + //no need to set sync timer on SQL version. + //printf("add interval tic (mmo_char_sync_timer)....\n"); + //i = add_timer_interval(gettick() + 10, mmo_char_sync_timer, 0, 0, autosave_interval); + + if(anti_freeze_enable > 0) { + add_timer_func_list(map_anti_freeze_system, "map_anti_freeze_system"); + i = add_timer_interval(gettick() + 1000, map_anti_freeze_system, 0, 0, ANTI_FREEZE_INTERVAL * 1000); // checks every X seconds user specifies + } + + read_gm_account(); + + printf("char server init func end (now unlimited loop start!)....\n"); + printf("The char-server is \033[1;32mready\033[0m (Server is listening on the port %d).\n\n", char_port); + return 0; +} + + diff --git a/misc/src/char_sql/char.h b/misc/src/char_sql/char.h new file mode 100644 index 0000000..116a301 --- /dev/null +++ b/misc/src/char_sql/char.h @@ -0,0 +1,82 @@ +#include "../common/core.h" +#include "../common/socket.h" +#include "../common/timer.h" +#include "../common/mmo.h" +#include "../common/version.h" +#include "../common/db.h" + +#ifndef _CHAR_H_ +#define _CHAR_H_ + +#define MAX_MAP_SERVERS 30 + +#define LAN_CONF_NAME "conf/lan_support.conf" + +#define DEFAULT_AUTOSAVE_INTERVAL 300*1000 + +struct mmo_map_server{ + long ip; + short port; + int users; + char map[MAX_MAP_PER_SERVER][16]; +}; +struct itemtmp { + int flag;//checked = 1 else 0 + int id; + short nameid; + short amount; + unsigned short equip; + char identify; + char refine; + char attribute; + short card[4]; + short broken; +}; +enum { + TABLE_INVENTORY, + TABLE_CART, + TABLE_STORAGE, + TABLE_GUILD_STORAGE, +}; +struct itemtemp{ + struct itemtmp equip[MAX_GUILD_STORAGE],notequip[MAX_GUILD_STORAGE]; +}; +int memitemdata_to_sql(struct itemtemp mapitem, int eqcount, int noteqcount, int char_id,int tableswitch); +int mapif_sendall(unsigned char *buf,unsigned int len); +int mapif_sendallwos(int fd,unsigned char *buf,unsigned int len); +int mapif_send(int fd,unsigned char *buf,unsigned int len); + +extern int autosave_interval; +extern char char_db[256]; +extern char cart_db[256]; +extern char inventory_db[256]; +extern char charlog_db[256]; +extern char storage_db[256]; +extern char interlog_db[256]; +extern char reg_db[256]; +extern char skill_db[256]; +extern char memo_db[256]; +extern char guild_db[256]; +extern char guild_alliance_db[256]; +extern char guild_castle_db[256]; +extern char guild_expulsion_db[256]; +extern char guild_member_db[256]; +extern char guild_position_db[256]; +extern char guild_skill_db[256]; +extern char guild_storage_db[256]; +extern char party_db[256]; +extern char pet_db[256]; + +int db_use_sqldbs; // added for sql item_db read for char server [Valaris] +extern char login_db_level[32]; +extern char login_db_account_id[32]; + +extern int lowest_gm_level; + +#endif + +#include "inter.h" +#include "int_pet.h" +#include "int_guild.h" +#include "int_party.h" +#include "int_storage.h" diff --git a/misc/src/char_sql/int_guild.c b/misc/src/char_sql/int_guild.c new file mode 100644 index 0000000..1983896 --- /dev/null +++ b/misc/src/char_sql/int_guild.c @@ -0,0 +1,1604 @@ +// +// original code from athena +// SQL conversion by hack +// + +#include "char.h" +#include "strlib.h" +#include "int_storage.h" +#include "inter.h" +#include "int_guild.h" +#include "int_storage.h" +#include "mmo.h" +#include "socket.h" + +#include +#include +#include + +static struct guild *guild_pt; +static struct guild *guild_pt2; +static struct guild_castle * guildcastle_pt; +static int guild_newid=10000; + +static int guild_exp[100]; + +int mapif_parse_GuildLeave(int fd,int guild_id,int account_id,int char_id,int flag,const char *mes); +int mapif_guild_broken(int guild_id,int flag); +int guild_check_empty(struct guild *g); +int guild_calcinfo(struct guild *g); +int mapif_guild_basicinfochanged(int guild_id,int type,const void *data,int len); +int mapif_guild_info(int fd,struct guild *g); +int guild_break_sub(void *key,void *data,va_list ap); + + +// Save guild into sql +int inter_guild_tosql(struct guild *g,int flag) +{ + // 1 `guild` (`guild_id`, `name`,`master`,`guild_lv`,`connect_member`,`max_member`,`average_lv`,`exp`,`next_exp`,`skill_point`,`castle_id`,`mes1`,`mes2`,`emblem_len`,`emblem_id`,`emblem_data`) + // 2 `guild_member` (`guild_id`,`account_id`,`char_id`,`hair`,`hair_color`,`gender`,`class`,`lv`,`exp`,`exp_payper`,`online`,`position`,`rsv1`,`rsv2`,`name`) + // 4 `guild_position` (`guild_id`,`position`,`name`,`mode`,`exp_mode`) + // 8 `guild_alliance` (`guild_id`,`opposition`,`alliance_id`,`name`) + // 16 `guild_expulsion` (`guild_id`,`name`,`mes`,`acc`,`account_id`,`rsv1`,`rsv2`,`rsv3`) + // 32 `guild_skill` (`guild_id`,`id`,`lv`) + + char t_name[100],t_master[24],t_mes1[60],t_mes2[120],t_member[24],t_position[24],t_alliance[24]; // temporay storage for str convertion; + char t_ename[24],t_emes[40]; + char emblem_data[4096]; + int i=0; + int guild_exist=0,guild_member=0,guild_online_member=0; + + if (g->guild_id<=0) return -1; + + printf("(\033[1;35m%d\033[0m) Request save guild - ",g->guild_id); + + jstrescapecpy(t_name, g->name); + + //printf("- Check if guild %d exists\n",g->guild_id); + sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `guild_id`='%d'",guild_db,g->guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild`)- %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + sql_row = mysql_fetch_row(sql_res); + guild_exist = atoi (sql_row[0]); + //printf("- Check if guild %d exists : %s\n",g->guild_id,((guild_exist==0)?"No":"Yes")); + } + mysql_free_result(sql_res) ; //resource free + + if (guild_exist >0){ + // Check members in party + sprintf(tmp_sql,"SELECT count(*) FROM `%s` WHERE `guild_id`='%d'",guild_member_db, g->guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + return -1; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + sql_row = mysql_fetch_row(sql_res); + + guild_member = atoi (sql_row[0]); + // printf("- Check members in guild %d : %d \n",g->guild_id,guild_member); + + } + mysql_free_result(sql_res) ; //resource free + + // Delete old guild from sql + if (flag&1||guild_member==0){ + // printf("- Delete guild %d from guild\n",g->guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_db, g->guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild`)- %s\n", mysql_error(&mysql_handle) ); + } + } + if (flag&2||guild_member==0){ + // printf("- Delete guild %d from guild_member\n",g->guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_member_db, g->guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_member`)- %s\n", mysql_error(&mysql_handle) ); + } + sprintf(tmp_sql, "UPDATE `%s` SET `guild_id`='0' WHERE `guild_id`='%d'",char_db, g->guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); + } + } + if (flag&32||guild_member==0){ + // printf("- Delete guild %d from guild_skill\n",g->guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_skill_db, g->guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_skill`)- %s\n", mysql_error(&mysql_handle) ); + } + } + if (flag&4||guild_member==0){ + // printf("- Delete guild %d from guild_position\n",g->guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_position_db, g->guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_position`)- %s\n", mysql_error(&mysql_handle) ); + } + } + if (flag&16||guild_member==0){ + // printf("- Delete guild %d from guild_expulsion\n",g->guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_expulsion_db, g->guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_expulsion`)- %s\n", mysql_error(&mysql_handle) ); + } + } + if (flag&8||guild_member==0){ + // printf("- Delete guild %d from guild_alliance\n",g->guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d' OR `alliance_id`='%d'",guild_alliance_db, g->guild_id,g->guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_alliance`)- %s\n", mysql_error(&mysql_handle) ); + } + } + if (flag&2||guild_member==0){ + // printf("- Delete guild %d from char\n",g->guild_id); + sprintf(tmp_sql, "UPDATE `%s` SET `guild_id`='0' WHERE `guild_id`='%d'",char_db, g->guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_alliance`)- %s\n", mysql_error(&mysql_handle) ); + } + } + if (guild_member==0){ + // printf("- Delete guild %d from guild_castle\n",g->guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_castle_db, g->guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_castle`)- %s\n", mysql_error(&mysql_handle) ); + } + } + } + + guild_online_member = 0; + i=0; + while (imax_member) { + if (g->member[i].account_id>0) guild_online_member++; + i++; + } + + // No member in guild , no need to create it in sql + if (guild_member <= 0 && guild_online_member <=0) { + inter_guild_storage_delete(g->guild_id); + printf("No member in guild %d , break it! \n",g->guild_id); + return -2; + } + + // Insert new guild to sqlserver + if (flag&1||guild_member==0){ + int len=0; + //printf("- Insert guild %d to guild\n",g->guild_id); + for(i=0;iemblem_len;i++){ + len+=sprintf(emblem_data+len,"%02x",(unsigned char)(g->emblem_data[i])); + //printf("%02x",(unsigned char)(g->emblem_data[i])); + } + emblem_data[len] = '\0'; + //printf("- emblem_len = %d \n",g->emblem_len); + sprintf(tmp_sql,"INSERT INTO `%s` " + "(`guild_id`, `name`,`master`,`guild_lv`,`connect_member`,`max_member`,`average_lv`,`exp`,`next_exp`,`skill_point`,`castle_id`,`mes1`,`mes2`,`emblem_len`,`emblem_id`,`emblem_data`) " + "VALUES ('%d', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%d', '%d', '%s')", + guild_db, g->guild_id,t_name,jstrescapecpy(t_master,g->master), + g->guild_lv,g->connect_member,g->max_member,g->average_lv,g->exp,g->next_exp,g->skill_point,g->castle_id, + jstrescapecpy(t_mes1,g->mes1),jstrescapecpy(t_mes2,g->mes2),g->emblem_len,g->emblem_id,emblem_data); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (insert `guild`)- %s\n", mysql_error(&mysql_handle) ); + } + } + + if (flag&2||guild_member==0){ + //printf("- Insert guild %d to guild_member\n",g->guild_id); + for(i=0;imax_member;i++){ + if (g->member[i].account_id>0){ + struct guild_member *m = &g->member[i]; + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",guild_member_db, m->char_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_member`)- %s\n", mysql_error(&mysql_handle) ); + } + sprintf(tmp_sql,"INSERT INTO `%s` " + "(`guild_id`,`account_id`,`char_id`,`hair`,`hair_color`,`gender`,`class`,`lv`,`exp`,`exp_payper`,`online`,`position`,`rsv1`,`rsv2`,`name`) " + "VALUES ('%d','%d','%d','%d','%d', '%d','%d','%d','%d','%d','%d','%d','%d','%d','%s')", + guild_member_db, g->guild_id, + m->account_id,m->char_id, + m->hair,m->hair_color,m->gender, + m->class,m->lv,m->exp,m->exp_payper,m->online,m->position, + 0,0, + jstrescapecpy(t_member,m->name)); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (insert `guild_member`)- %s\n", mysql_error(&mysql_handle) ); + } + sprintf(tmp_sql, "UPDATE `%s` SET `guild_id`='%d' WHERE `account_id`='%d' AND `char_id`='%d'",char_db, g->guild_id,m->account_id,m->char_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); + } + } + } + } + + if (flag&4||guild_member==0){ + //printf("- Insert guild %d to guild_position\n",g->guild_id); + for(i=0;iposition[i]; + sprintf(tmp_sql,"INSERT INTO `%s` (`guild_id`,`position`,`name`,`mode`,`exp_mode`) VALUES ('%d','%d', '%s','%d','%d')", + guild_position_db, g->guild_id, i, jstrescapecpy(t_position,p->name),p->mode,p->exp_mode); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (insert `guild_position`)- %s\n", mysql_error(&mysql_handle) ); + } + } + } + + if (flag&8||guild_member==0){ + //printf("- Insert guild %d to guild_alliance\n",g->guild_id); + for(i=0;ialliance[i]; + if(a->guild_id>0){ + sprintf(tmp_sql,"INSERT INTO `%s` (`guild_id`,`opposition`,`alliance_id`,`name`) " + "VALUES ('%d','%d','%d','%s')", + guild_alliance_db, g->guild_id,a->opposition,a->guild_id,jstrescapecpy(t_alliance,a->name)); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (insert `guild_alliance`)- %s\n", mysql_error(&mysql_handle) ); + } + sprintf(tmp_sql,"INSERT INTO `%s` (`guild_id`,`opposition`,`alliance_id`,`name`) " + "VALUES ('%d','%d','%d','%s')", + guild_alliance_db, a->guild_id,a->opposition,g->guild_id,t_name); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (insert `guild_alliance`)- %s\n", mysql_error(&mysql_handle) ); + } + } + } + } + + if (flag&16||guild_member==0){ + //printf("- Insert guild %d to guild_expulsion\n",g->guild_id); + for(i=0;iexplusion[i]; + if(e->account_id>0){ + sprintf(tmp_sql,"INSERT INTO `%s` (`guild_id`,`name`,`mes`,`acc`,`account_id`,`rsv1`,`rsv2`,`rsv3`) " + "VALUES ('%d','%s','%s','%s','%d','%d','%d','%d')", + guild_expulsion_db, g->guild_id, + jstrescapecpy(t_ename,e->name),jstrescapecpy(t_emes,e->mes),e->acc,e->account_id,e->rsv1,e->rsv2,e->rsv3 ); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (insert `guild_expulsion`)- %s\n", mysql_error(&mysql_handle) ); + } + } + } + } + + if (flag&32||guild_member==0){ + //printf("- Insert guild %d to guild_skill\n",g->guild_id); + for(i=0;iskill[i].id>0){ + sprintf(tmp_sql,"INSERT INTO `%s` (`guild_id`,`id`,`lv`) VALUES ('%d','%d','%d')", + guild_skill_db, g->guild_id,g->skill[i].id,g->skill[i].lv); + //printf("%s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (insert `guild_skill`)- %s\n", mysql_error(&mysql_handle) ); + } + } + } + } + + printf("Save guild done\n"); + return 0; +} + +// Read guild from sql +int inter_guild_fromsql(int guild_id,struct guild *g) +{ + int i; + char emblem_data[4096]; + char *pstr; + + if (g==NULL) return 0; + memset(g,0,sizeof(struct guild)); + if (guild_id==0) return 0; + +// printf("Retrieve guild information from sql ......\n"); +// printf("- Read guild %d from sql \n",guild_id); + + sprintf(tmp_sql,"SELECT `guild_id`, `name`,`master`,`guild_lv`,`connect_member`,`max_member`,`average_lv`,`exp`,`next_exp`,`skill_point`,`castle_id`,`mes1`,`mes2`,`emblem_len`,`emblem_id`,`emblem_data` " + "FROM `%s` WHERE `guild_id`='%d'",guild_db, guild_id); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `guild`)- %s\n", mysql_error(&mysql_handle) ); + return 0; + } + + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + sql_row = mysql_fetch_row(sql_res); + if (sql_row==NULL) { + mysql_free_result(sql_res); + return 0; + } + + g->guild_id=atoi(sql_row[0]); + strncpy(g->name,sql_row[1],24); + strncpy(g->master,sql_row[2],24); + g->guild_lv=atoi(sql_row[3]); + g->connect_member=atoi(sql_row[4]); + g->max_member=atoi(sql_row[5]); + g->average_lv=atoi(sql_row[6]); + g->exp=atoi(sql_row[7]); + g->next_exp=atoi(sql_row[8]); + g->skill_point=atoi(sql_row[9]); + g->castle_id=atoi(sql_row[10]); + strncpy(g->mes1,sql_row[11],60); + strncpy(g->mes2,sql_row[12],120); + g->emblem_len=atoi(sql_row[13]); + g->emblem_id=atoi(sql_row[14]); + strncpy(emblem_data,sql_row[15],4096); + for(i=0,pstr=emblem_data;iemblem_len;i++,pstr+=2){ + int c1=pstr[0],c2=pstr[1],x1=0,x2=0; + if(c1>='0' && c1<='9')x1=c1-'0'; + if(c1>='a' && c1<='f')x1=c1-'a'+10; + if(c1>='A' && c1<='F')x1=c1-'A'+10; + if(c2>='0' && c2<='9')x2=c2-'0'; + if(c2>='a' && c2<='f')x2=c2-'a'+10; + if(c2>='A' && c2<='F')x2=c2-'A'+10; + g->emblem_data[i]=(x1<<4)|x2; + } + } + mysql_free_result(sql_res); + + //printf("- Read guild_member %d from sql \n",guild_id); + sprintf(tmp_sql,"SELECT `guild_id`,`account_id`,`char_id`,`hair`,`hair_color`,`gender`,`class`,`lv`,`exp`,`exp_payper`,`online`,`position`,`rsv1`,`rsv2`,`name` " + "FROM `%s` WHERE `guild_id`='%d' ORDER BY `position`", guild_member_db, guild_id); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `guild_member`)- %s\n", mysql_error(&mysql_handle) ); + return 0; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + int i; + for(i=0;((sql_row = mysql_fetch_row(sql_res))&&imax_member);i++){ + struct guild_member *m = &g->member[i]; + m->account_id=atoi(sql_row[1]); + m->char_id=atoi(sql_row[2]); + m->hair=atoi(sql_row[3]); + m->hair_color=atoi(sql_row[4]); + m->gender=atoi(sql_row[5]); + m->class=atoi(sql_row[6]); + m->lv=atoi(sql_row[7]); + m->exp=atoi(sql_row[8]); + m->exp_payper=atoi(sql_row[9]); + m->online=atoi(sql_row[10]); + m->position=atoi(sql_row[11]); + strncpy(m->name,sql_row[14],24); + } + } + mysql_free_result(sql_res); + + //printf("- Read guild_position %d from sql \n",guild_id); + sprintf(tmp_sql,"SELECT `guild_id`,`position`,`name`,`mode`,`exp_mode` FROM `%s` WHERE `guild_id`='%d'",guild_position_db, guild_id); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `guild_position`)- %s\n", mysql_error(&mysql_handle) ); + return 0; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + int i; + for(i=0;((sql_row = mysql_fetch_row(sql_res))&&iposition[position]; + strncpy(p->name,sql_row[2],24); + p->mode=atoi(sql_row[3]); + p->exp_mode=atoi(sql_row[4]); + } + } + mysql_free_result(sql_res); + + //printf("- Read guild_alliance %d from sql \n",guild_id); + sprintf(tmp_sql,"SELECT `guild_id`,`opposition`,`alliance_id`,`name` FROM `%s` WHERE `guild_id`='%d'",guild_alliance_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `guild_alliance`)- %s\n", mysql_error(&mysql_handle) ); + return 0; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + int i; + for(i=0;((sql_row = mysql_fetch_row(sql_res))&&ialliance[i]; + a->opposition=atoi(sql_row[1]); + a->guild_id=atoi(sql_row[2]); + strncpy(a->name,sql_row[3],24); + } + } + mysql_free_result(sql_res); + + //printf("- Read guild_expulsion %d from sql \n",guild_id); + sprintf(tmp_sql,"SELECT `guild_id`,`name`,`mes`,`acc`,`account_id`,`rsv1`,`rsv2`,`rsv3` FROM `%s` WHERE `guild_id`='%d'",guild_expulsion_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `guild_expulsion`)- %s\n", mysql_error(&mysql_handle) ); + return 0; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + int i; + for(i=0;((sql_row = mysql_fetch_row(sql_res))&&iexplusion[i]; + + strncpy(e->name,sql_row[1],24); + strncpy(e->mes,sql_row[2],40); + strncpy(e->acc,sql_row[3],24); + e->account_id=atoi(sql_row[4]); + e->rsv1=atoi(sql_row[5]); + e->rsv2=atoi(sql_row[6]); + e->rsv3=atoi(sql_row[7]); + + } + } + mysql_free_result(sql_res); + + //printf("- Read guild_skill %d from sql \n",guild_id); + sprintf(tmp_sql,"SELECT `guild_id`,`id`,`lv` FROM `%s` WHERE `guild_id`='%d' ORDER BY `id`",guild_skill_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `guild_skill`)- %s\n", mysql_error(&mysql_handle) ); + return 0; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + int i; + for(i=0;((sql_row = mysql_fetch_row(sql_res))&&iskill[i].id=atoi(sql_row[1]); + g->skill[i].lv=atoi(sql_row[2]); + } + } + mysql_free_result(sql_res); + +// printf("Successfully retrieve guild information from sql!\n"); + + return 0; + +} + +// Save guild_castle to sql +int inter_guildcastle_tosql(struct guild_castle *gc) +{ + // `guild_castle` (`castle_id`, `guild_id`, `economy`, `defense`, `triggerE`, `triggerD`, `nextTime`, `payTime`, `createTime`, `visibleC`, `visibleG0`, `visibleG1`, `visibleG2`, `visibleG3`, `visibleG4`, `visibleG5`, `visibleG6`, `visibleG7`) + + if (gc==NULL) return 0; + //printf("Save to guild_castle\n"); + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `castle_id`='%d'",guild_castle_db, gc->castle_id); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + return 0; + } + + sprintf(tmp_sql,"INSERT INTO `%s` " + "(`castle_id`, `guild_id`, `economy`, `defense`, `triggerE`, `triggerD`, `nextTime`, `payTime`, `createTime`," + "`visibleC`, `visibleG0`, `visibleG1`, `visibleG2`, `visibleG3`, `visibleG4`, `visibleG5`, `visibleG6`, `visibleG7`," + "`Ghp0`, `Ghp1`, `Ghp2`, `Ghp3`, `Ghp4`, `Ghp5`, `Ghp6`, `Ghp7`)" + "VALUES ('%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d')", + guild_castle_db, gc->castle_id, gc->guild_id, gc->economy, gc->defense, gc->triggerE, gc->triggerD, gc->nextTime, gc->payTime, + gc->createTime, gc->visibleC, gc->visibleG0, gc->visibleG1, gc->visibleG2, gc->visibleG3, gc->visibleG4, gc->visibleG5, + gc->visibleG6, gc->visibleG7, gc->Ghp0, gc->Ghp1, gc->Ghp2, gc->Ghp3, gc->Ghp4, gc->Ghp5, gc->Ghp6, gc->Ghp7); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + return 0; + } + + sprintf(tmp_sql,"UPDATE `%s` SET `castle_id`='-1' WHERE `castle_id`='%d'",guild_db, gc->castle_id); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + return 0; + } + + sprintf(tmp_sql,"UPDATE `%s` SET `castle_id`='%d' WHERE `guild_id`='%d'",guild_db, gc->castle_id,gc->guild_id); + //printf(" %s\n",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + return 0; + } + + return 0; +} +// Read guild_castle from sql +int inter_guildcastle_fromsql(int castle_id,struct guild_castle *gc) +{ + + if (gc==NULL) return 0; + //printf("Read from guild_castle\n"); + memset(gc,0,sizeof(struct guild_castle)); + gc->castle_id=castle_id; + if (castle_id==-1) return 0; + sprintf(tmp_sql,"SELECT `castle_id`, `guild_id`, `economy`, `defense`, `triggerE`, `triggerD`, `nextTime`, `payTime`, `createTime`, " + "`visibleC`, `visibleG0`, `visibleG1`, `visibleG2`, `visibleG3`, `visibleG4`, `visibleG5`, `visibleG6`, `visibleG7`," + "`Ghp0`, `Ghp1`, `Ghp2`, `Ghp3`, `Ghp4`, `Ghp5`, `Ghp6`, `Ghp7`" + " FROM `%s` WHERE `castle_id`='%d'",guild_castle_db, castle_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + return 0; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + sql_row = mysql_fetch_row(sql_res); + if (sql_row==NULL){ + mysql_free_result(sql_res); + return 0; + } + + gc->guild_id = atoi (sql_row[1]); + gc->economy = atoi (sql_row[2]); + gc->defense = atoi (sql_row[3]); + gc->triggerE = atoi (sql_row[4]); + gc->triggerD = atoi (sql_row[5]); + gc->nextTime = atoi (sql_row[6]); + gc->payTime = atoi (sql_row[7]); + gc->createTime = atoi (sql_row[8]); + gc->visibleC = atoi (sql_row[9]); + gc->visibleG0 = atoi (sql_row[10]); + gc->visibleG1 = atoi (sql_row[11]); + gc->visibleG2 = atoi (sql_row[12]); + gc->visibleG3 = atoi (sql_row[13]); + gc->visibleG4 = atoi (sql_row[14]); + gc->visibleG5 = atoi (sql_row[15]); + gc->visibleG6 = atoi (sql_row[16]); + gc->visibleG7 = atoi (sql_row[17]); + gc->Ghp0 = atoi (sql_row[18]); + gc->Ghp1 = atoi (sql_row[19]); + gc->Ghp2 = atoi (sql_row[20]); + gc->Ghp3 = atoi (sql_row[21]); + gc->Ghp4 = atoi (sql_row[22]); + gc->Ghp5 = atoi (sql_row[23]); + gc->Ghp6 = atoi (sql_row[24]); + gc->Ghp7 = atoi (sql_row[25]); + + //printf("Read Castle %d of guild %d from sql \n",castle_id,gc->guild_id); + + } + mysql_free_result(sql_res) ; //resource free + return 0; +} + +// Read exp_guild.txt +int inter_guild_readdb() +{ + int i; + FILE *fp; + char line[1024]; + for (i=0;i<100;i++) guild_exp[i]=0; + + fp=fopen("db/exp_guild.txt","r"); + if(fp==NULL){ + printf("can't read db/exp_guild.txt\n"); + return 1; + } + i=0; + while(fgets(line,256,fp) && i<100){ + if(line[0]=='/' && line[1]=='/') + continue; + guild_exp[i]=atoi(line); + i++; + } + fclose(fp); + + return 0; +} + + +// Initialize guild sql +int inter_guild_sql_init() +{ + int i; + + printf("interserver guild memory initialize.... (%d byte)\n",sizeof(struct guild)); + guild_pt = calloc(sizeof(struct guild), 1); + guild_pt2= calloc(sizeof(struct guild), 1); + guildcastle_pt=calloc(sizeof(struct guild_castle), 1); + + inter_guild_readdb(); // Read exp + + sprintf(tmp_sql,"UPDATE `%s` SET `online`='0'",guild_member_db); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); + exit(0); + } + + sprintf (tmp_sql , "SELECT count(*) FROM `%s`",guild_db); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + exit(0); + } + sql_res = mysql_store_result(&mysql_handle) ; + sql_row = mysql_fetch_row(sql_res); + printf("total guild data -> '%s'.......\n",sql_row[0]); + i = atoi (sql_row[0]); + mysql_free_result(sql_res); + + if (i > 0) { + //set party_newid + sprintf (tmp_sql , "SELECT max(`guild_id`) FROM `%s`",guild_db); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + exit(0); + } + + sql_res = mysql_store_result(&mysql_handle) ; + sql_row = mysql_fetch_row(sql_res); + guild_newid = atoi(sql_row[0])+1; + mysql_free_result(sql_res); + } + + printf("set guild_newid: %d.......\n",guild_newid); + + return 0; +} + + +// Get guild by its name +struct guild* search_guildname(char *str) +{ + struct guild *g=guild_pt; + char t_name[24]; + int guild_id=0; + printf("search_guildname\n"); + sprintf (tmp_sql , "SELECT `guild_id` FROM `%s` WHERE `name`='%s'",guild_db, jstrescapecpy(t_name,str)); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + sql_row = mysql_fetch_row(sql_res); + guild_id = atoi (sql_row[0]); + } + mysql_free_result(sql_res); + inter_guild_fromsql(guild_id,g); + return g; +} + +// Check if guild is empty +int guild_check_empty(struct guild *g) +{ + int i; + for(i=0;imax_member;i++){ + if(g->member[i].account_id>0){ + return 0; + } + } + + // 誰もいないので解散 + mapif_guild_broken(g->guild_id,0); + inter_guild_storage_delete(g->guild_id); + inter_guild_tosql(g,255); + memset(g,0,sizeof(struct guild)); + return 1; +} + +int guild_nextexp(int level) +{ + if(level < 100 && level >0) // Change by hack + return guild_exp[level-1]; + + return 0; +} + +// ギルドスキルがあるか確認 +int guild_checkskill(struct guild *g,int id){ return g->skill[id-10000].lv; } + +// ギルドの情報の再計算 +int guild_calcinfo(struct guild *g) +{ + int i,c,nextexp; + struct guild before=*g; + + // スキルIDの設定 + for(i=0;i<5;i++) + g->skill[i].id=i+10000; + + // ギルドレベル + if(g->guild_lv<=0) g->guild_lv=1; + nextexp = guild_nextexp(g->guild_lv); + if(nextexp > 0) { + while(g->exp >= nextexp && nextexp>0){ // Change by hack + g->exp-=nextexp; + g->guild_lv++; + g->skill_point++; + nextexp = guild_nextexp(g->guild_lv); + } + } + + // ギルドの次の経験値 + g->next_exp = guild_nextexp(g->guild_lv); + + // メンバ上限(ギルド拡張適用) + g->max_member=16+guild_checkskill(g,10004)*2; // Updated max_members [PoW] + + // 平均レベルとオンライン人数 + g->average_lv=0; + g->connect_member=0; + for(i=c=0;imax_member;i++){ + if(g->member[i].account_id>0){ + g->average_lv+=g->member[i].lv; + c++; + + if(g->member[i].online>0) + g->connect_member++; + } + } + g->average_lv/=c; + + // 全データを送る必要がありそう + if( g->max_member!=before.max_member || + g->guild_lv!=before.guild_lv || + g->skill_point!=before.skill_point ){ + mapif_guild_info(-1,g); + return 1; + } + + return 0; +} + +//------------------------------------------------------------------- +// map serverへの通信 + +// ギルド作成可否 +int mapif_guild_created(int fd,int account_id,struct guild *g) +{ + WFIFOW(fd,0)=0x3830; + WFIFOL(fd,2)=account_id; + if(g!=NULL){ + WFIFOL(fd,6)=g->guild_id; + printf("int_guild: created! %d %s\n",g->guild_id,g->name); + }else{ + WFIFOL(fd,6)=0; + } + WFIFOSET(fd,10); + return 0; +} +// ギルド情報見つからず +int mapif_guild_noinfo(int fd,int guild_id) +{ + WFIFOW(fd,0)=0x3831; + WFIFOW(fd,2)=8; + WFIFOL(fd,4)=guild_id; + WFIFOSET(fd,8); + printf("int_guild: info not found %d\n",guild_id); + return 0; +} +// ギルド情報まとめ送り +int mapif_guild_info(int fd,struct guild *g) +{ + unsigned char buf[16384]; + WBUFW(buf,0)=0x3831; + memcpy(buf+4,g,sizeof(struct guild)); + WBUFW(buf,2)=4+sizeof(struct guild); +// printf("int_guild: sizeof(guild)=%d\n",sizeof(struct guild)); + if(fd<0) + mapif_sendall(buf,WBUFW(buf,2)); + else + mapif_send(fd,buf,WBUFW(buf,2)); +// printf("int_guild: info %d %s\n",p->guild_id,p->name); + return 0; +} + +// メンバ追加可否 +int mapif_guild_memberadded(int fd,int guild_id,int account_id,int char_id,int flag) +{ + WFIFOW(fd,0)=0x3832; + WFIFOL(fd,2)=guild_id; + WFIFOL(fd,6)=account_id; + WFIFOL(fd,10)=char_id; + WFIFOB(fd,14)=flag; + WFIFOSET(fd,15); + return 0; +} +// 脱退/追放通知 +int mapif_guild_leaved(int guild_id,int account_id,int char_id,int flag, + const char *name,const char *mes) +{ + unsigned char buf[128]; + WBUFW(buf, 0)=0x3834; + WBUFL(buf, 2)=guild_id; + WBUFL(buf, 6)=account_id; + WBUFL(buf,10)=char_id; + WBUFB(buf,14)=flag; + memcpy(WBUFP(buf,15),mes,40); + memcpy(WBUFP(buf,55),name,24); + mapif_sendall(buf,79); + printf("int_guild: guild leaved %d %d %s %s\n",guild_id,account_id,name,mes); + return 0; +} + +// オンライン状態とLv更新通知 +int mapif_guild_memberinfoshort(struct guild *g,int idx) +{ + unsigned char buf[32]; + WBUFW(buf, 0)=0x3835; + WBUFL(buf, 2)=g->guild_id; + WBUFL(buf, 6)=g->member[idx].account_id; + WBUFL(buf,10)=g->member[idx].char_id; + WBUFB(buf,14)=g->member[idx].online; + WBUFW(buf,15)=g->member[idx].lv; + WBUFW(buf,17)=g->member[idx].class; + mapif_sendall(buf,19); + return 0; +} + +// 解散通知 +int mapif_guild_broken(int guild_id,int flag) +{ + unsigned char buf[16]; + WBUFW(buf,0)=0x3836; + WBUFL(buf,2)=guild_id; + WBUFB(buf,6)=flag; + mapif_sendall(buf,7); + printf("int_guild: broken %d\n",guild_id); + return 0; +} + +// ギルド内発言 +int mapif_guild_message(int guild_id,int account_id,char *mes,int len) +{ + unsigned char buf[512]; + WBUFW(buf,0)=0x3837; + WBUFW(buf,2)=len+12; + WBUFL(buf,4)=guild_id; + WBUFL(buf,8)=account_id; + memcpy(WBUFP(buf,12),mes,len); + mapif_sendall(buf,len+12); + return 0; +} + +// ギルド基本情報変更通知 +int mapif_guild_basicinfochanged(int guild_id,int type,const void *data,int len) +{ + unsigned char buf[2048]; + WBUFW(buf, 0)=0x3839; + WBUFW(buf, 2)=len+10; + WBUFL(buf, 4)=guild_id; + WBUFW(buf, 8)=type; + memcpy(WBUFP(buf,10),data,len); + mapif_sendall(buf,len+10); + return 0; +} +// ギルドメンバ情報変更通知 +int mapif_guild_memberinfochanged(int guild_id,int account_id,int char_id, + int type,const void *data,int len) +{ + unsigned char buf[2048]; + WBUFW(buf, 0)=0x383a; + WBUFW(buf, 2)=len+18; + WBUFL(buf, 4)=guild_id; + WBUFL(buf, 8)=account_id; + WBUFL(buf,12)=char_id; + WBUFW(buf,16)=type; + memcpy(WBUFP(buf,18),data,len); + mapif_sendall(buf,len+18); + return 0; +} +// ギルドスキルアップ通知 +int mapif_guild_skillupack(int guild_id,int skill_num,int account_id) +{ + unsigned char buf[16]; + WBUFW(buf, 0)=0x383c; + WBUFL(buf, 2)=guild_id; + WBUFL(buf, 6)=skill_num; + WBUFL(buf,10)=account_id; + mapif_sendall(buf,14); + return 0; +} +// ギルド同盟/敵対通知 +int mapif_guild_alliance(int guild_id1,int guild_id2,int account_id1,int account_id2, + int flag,const char *name1,const char *name2) +{ + unsigned char buf[128]; + WBUFW(buf, 0)=0x383d; + WBUFL(buf, 2)=guild_id1; + WBUFL(buf, 6)=guild_id2; + WBUFL(buf,10)=account_id1; + WBUFL(buf,14)=account_id2; + WBUFB(buf,18)=flag; + memcpy(WBUFP(buf,19),name1,24); + memcpy(WBUFP(buf,43),name2,24); + mapif_sendall(buf,67); + return 0; +} + +// ギルド役職変更通知 +int mapif_guild_position(struct guild *g,int idx) +{ + unsigned char buf[128]; + WBUFW(buf,0)=0x383b; + WBUFW(buf,2)=sizeof(struct guild_position)+12; + WBUFL(buf,4)=g->guild_id; + WBUFL(buf,8)=idx; + memcpy(WBUFP(buf,12),&g->position[idx],sizeof(struct guild_position)); + mapif_sendall(buf,WBUFW(buf,2)); + return 0; +} + +// ギルド告知変更通知 +int mapif_guild_notice(struct guild *g) +{ + unsigned char buf[256]; + WBUFW(buf,0)=0x383e; + WBUFL(buf,2)=g->guild_id; + memcpy(WBUFP(buf,6),g->mes1,60); + memcpy(WBUFP(buf,66),g->mes2,120); + mapif_sendall(buf,186); + return 0; +} +// ギルドエンブレム変更通知 +int mapif_guild_emblem(struct guild *g) +{ + unsigned char buf[2048]; + WBUFW(buf,0)=0x383f; + WBUFW(buf,2)=g->emblem_len+12; + WBUFL(buf,4)=g->guild_id; + WBUFL(buf,8)=g->emblem_id; + memcpy(WBUFP(buf,12),g->emblem_data,g->emblem_len); + mapif_sendall(buf,WBUFW(buf,2)); + return 0; +} + +int mapif_guild_castle_dataload(int castle_id,int index,int value) // +{ + unsigned char buf[16]; + WBUFW(buf, 0)=0x3840; + WBUFW(buf, 2)=castle_id; + WBUFB(buf, 4)=index; + WBUFL(buf, 5)=value; + mapif_sendall(buf,9); + return 0; +} + +int mapif_guild_castle_datasave(int castle_id,int index,int value) // +{ + unsigned char buf[16]; + WBUFW(buf, 0)=0x3841; + WBUFW(buf, 2)=castle_id; + WBUFB(buf, 4)=index; + WBUFL(buf, 5)=value; + mapif_sendall(buf,9); + return 0; +} + +int mapif_guild_castle_alldataload(int fd) { + struct guild_castle* gc = guildcastle_pt; + int i, len = 4; + + WFIFOW(fd,0) = 0x3842; + sprintf(tmp_sql, "SELECT * FROM `%s` ORDER BY `castle_id`", guild_castle_db); + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle); + if (sql_res != NULL && mysql_num_rows(sql_res) > 0) { + for(i = 0; ((sql_row = mysql_fetch_row(sql_res)) && i < MAX_GUILDCASTLE); i++) { + memset(gc, 0, sizeof(struct guild_castle)); + gc->castle_id = atoi(sql_row[0]); + gc->guild_id = atoi(sql_row[1]); + gc->economy = atoi(sql_row[2]); + gc->defense = atoi(sql_row[3]); + gc->triggerE = atoi(sql_row[4]); + gc->triggerD = atoi(sql_row[5]); + gc->nextTime = atoi(sql_row[6]); + gc->payTime = atoi(sql_row[7]); + gc->createTime = atoi(sql_row[8]); + gc->visibleC = atoi(sql_row[9]); + gc->visibleG0 = atoi(sql_row[10]); + gc->visibleG1 = atoi(sql_row[11]); + gc->visibleG2 = atoi(sql_row[12]); + gc->visibleG3 = atoi(sql_row[13]); + gc->visibleG4 = atoi(sql_row[14]); + gc->visibleG5 = atoi(sql_row[15]); + gc->visibleG6 = atoi(sql_row[16]); + gc->visibleG7 = atoi(sql_row[17]); + gc->Ghp0 = atoi(sql_row[18]); + gc->Ghp1 = atoi(sql_row[19]); + gc->Ghp2 = atoi(sql_row[20]); + gc->Ghp3 = atoi(sql_row[21]); + gc->Ghp4 = atoi(sql_row[22]); + gc->Ghp5 = atoi(sql_row[23]); + gc->Ghp6 = atoi(sql_row[24]); + gc->Ghp7 = atoi(sql_row[25]); + memcpy(WFIFOP(fd,len), gc, sizeof(struct guild_castle)); + len += sizeof(struct guild_castle); + } + } + mysql_free_result(sql_res); + WFIFOW(fd,2) = len; + WFIFOSET(fd,len); + + return 0; +} + + +//------------------------------------------------------------------- +// map serverからの通信 + + +// ギルド作成要求 +int mapif_parse_CreateGuild(int fd,int account_id,char *name,struct guild_member *master) +{ + struct guild *g; + int i; + + printf("CreateGuild\n"); + g=search_guildname(name); + if(g!=NULL&&g->guild_id>0){ + printf("int_guild: same name guild exists [%s]\n",name); + mapif_guild_created(fd,account_id,NULL); + return 0; + } + g=guild_pt; + memset(g,0,sizeof(struct guild)); + g->guild_id=guild_newid++; + memcpy(g->name,name,24); + memcpy(g->master,master->name,24); + memcpy(&g->member[0],master,sizeof(struct guild_member)); + + g->position[0].mode=0x11; + strcpy(g->position[ 0].name,"GuildMaster"); + strcpy(g->position[MAX_GUILDPOSITION-1].name,"Newbie"); + for(i=1;iposition[i].name,"Position %d",i+1); + + // Initialize guild property + g->max_member=16; + g->average_lv=master->lv; + g->castle_id=-1; + for(i=0;i<5;i++) + g->skill[i].id=i+10000; + + // Save to sql + printf("Create initialize OK!\n"); + i=inter_guild_tosql(g,255); + + if (i<0) { + mapif_guild_created(fd,account_id,NULL); + return 0; + } + + // Report to client + mapif_guild_created(fd,account_id,g); + mapif_guild_info(fd,g); + + inter_log("guild %s (id=%d) created by master %s (id=%d)" RETCODE, + name, g->guild_id, master->name, master->account_id ); + + + return 0; +} +// Return guild info to client +int mapif_parse_GuildInfo(int fd,int guild_id) +{ + struct guild *g; + g=guild_pt; + inter_guild_fromsql(guild_id,g); + if(g!=NULL&&g->guild_id>0){ + guild_calcinfo(g); + mapif_guild_info(fd,g); + //inter_guild_tosql(g,1); // Change guild + }else + mapif_guild_noinfo(fd,guild_id); + return 0; +} +// Add member to guild +int mapif_parse_GuildAddMember(int fd,int guild_id,struct guild_member *m) +{ + struct guild *g; + int i; + g=guild_pt; + inter_guild_fromsql(guild_id,g); + + if(g==NULL||g->guild_id<=0){ + mapif_guild_memberadded(fd,guild_id,m->account_id,m->char_id,1); + return 0; + } + + for(i=0;imax_member;i++){ + if(g->member[i].account_id==0){ + + memcpy(&g->member[i],m,sizeof(struct guild_member)); + mapif_guild_memberadded(fd,guild_id,m->account_id,m->char_id,0); + guild_calcinfo(g); + mapif_guild_info(-1,g); + inter_guild_tosql(g,3); // Change guild & guild_member + return 0; + } + } + mapif_guild_memberadded(fd,guild_id,m->account_id,m->char_id,1); + //inter_guild_tosql(g,3); // Change guild & guild_member + return 0; +} +// Delete member from guild +int mapif_parse_GuildLeave(int fd,int guild_id,int account_id,int char_id,int flag,const char *mes) +{ + struct guild *g=NULL; + g=guild_pt; + inter_guild_fromsql(guild_id,g); + + if(g!=NULL&&g->guild_id>0){ + int i; + for(i=0;imax_member;i++){ + if( g->member[i].account_id==account_id && + g->member[i].char_id==char_id){ + printf("%d %d\n",i, (int)(&g->member[i])); + printf("%d %s\n",i, g->member[i].name); + + if(flag){ // 追放の場合追放リストに入れる + int j; + for(j=0;jexplusion[j].account_id==0) + break; + } + if(j==MAX_GUILDEXPLUSION){ // 一杯なので古いのを消す + for(j=0;jexplusion[j]=g->explusion[j+1]; + j=MAX_GUILDEXPLUSION-1; + } + g->explusion[j].account_id=account_id; + memcpy(g->explusion[j].acc,"dummy",24); + memcpy(g->explusion[j].name,g->member[i].name,24); + memcpy(g->explusion[j].mes,mes,40); + } + + mapif_guild_leaved(guild_id,account_id,char_id,flag,g->member[i].name,mes); + printf("%d %d\n",i, (int)(&g->member[i])); + printf("%d %s\n",i, (&g->member[i])->name); + memset(&g->member[i],0,sizeof(struct guild_member)); + + if( guild_check_empty(g)==0 ) + mapif_guild_info(-1,g);// まだ人がいるのでデータ送信 + /* + else + inter_guild_save(); // 解散したので一応セーブ + return 0;*/ + } + } + guild_calcinfo(g); + inter_guild_tosql(g,19); // Change guild & guild_member & guild_expulsion + }else{ + sprintf(tmp_sql, "UPDATE `%s` SET `guild_id`='0' WHERE `account_id`='%d' AND `char_id`='%d'",char_db, account_id,char_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); + } + /* mapif_guild_leaved(guild_id,account_id,char_id,flag,g->member[i].name,mes); */ + } + + + return 0; +} +// Change member info +int mapif_parse_GuildChangeMemberInfoShort(int fd,int guild_id, + int account_id,int char_id,int online,int lv,int class) +{ + // Could speed up by manipulating only guild_member + struct guild *g; + int i,alv,c; + g=guild_pt; + inter_guild_fromsql(guild_id,g); + + if(g==NULL||g->guild_id<=0){ + return 0; + } + + g->connect_member=0; + + for(i=0,alv=0,c=0;imax_member;i++){ + if( g->member[i].account_id==account_id && + g->member[i].char_id==char_id){ + + g->member[i].online=online; + g->member[i].lv=lv; + g->member[i].class=class; + mapif_guild_memberinfoshort(g,i); + } + if( g->member[i].account_id>0 ){ + alv+=g->member[i].lv; + c++; + } + if( g->member[i].online ) + g->connect_member++; + } + // 平均レベル + g->average_lv=alv/c; + + inter_guild_tosql(g,3); // Change guild & guild_member + + return 0; +} + +// BreakGuild +int mapif_parse_BreakGuild(int fd,int guild_id) +{ + struct guild *g; + g=guild_pt; + inter_guild_fromsql(guild_id,g); + if(g==NULL){ + return 0; + } + + // Delete guild from sql + //printf("- Delete guild %d from guild\n",guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild`)- %s\n", mysql_error(&mysql_handle) ); + } + //printf("- Delete guild %d from guild_member\n",guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_member_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_member`)- %s\n", mysql_error(&mysql_handle) ); + } + //printf("- Delete guild %d from guild_skill\n",guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_skill_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_skill`)- %s\n", mysql_error(&mysql_handle) ); + } + //printf("- Delete guild %d from guild_position\n",guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_position_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_position`)- %s\n", mysql_error(&mysql_handle) ); + } + //printf("- Delete guild %d from guild_expulsion\n",guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_expulsion_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_expulsion`)- %s\n", mysql_error(&mysql_handle) ); + } + //printf("- Delete guild %d from guild_alliance\n",guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d' OR `alliance_id`='%d'",guild_alliance_db, guild_id,guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_position`)- %s\n", mysql_error(&mysql_handle) ); + } + + //printf("- Delete guild %d from guild_castle\n",guild_id); + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_castle_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_position`)- %s\n", mysql_error(&mysql_handle) ); + } + + //printf("- Update guild %d of char\n",guild_id); + sprintf(tmp_sql, "UPDATE `%s` SET `guild_id`='0' WHERE `guild_id`='%d'",char_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_position`)- %s\n", mysql_error(&mysql_handle) ); + } + + inter_guild_storage_delete(guild_id); + mapif_guild_broken(guild_id,0); + + inter_log("guild %s (id=%d) broken" RETCODE,g->name,guild_id); + + return 0; +} + +// ギルドメッセージ送信 +int mapif_parse_GuildMessage(int fd,int guild_id,int account_id,char *mes,int len) +{ + return mapif_guild_message(guild_id,account_id,mes,len); +} +// ギルド基本データ変更要求 +int mapif_parse_GuildBasicInfoChange(int fd,int guild_id, + int type,const char *data,int len) +{ + struct guild *g; +// int dd=*((int *)data); + short dw=*((short *)data); + g=guild_pt; + inter_guild_fromsql(guild_id,g); + + if(g==NULL||g->guild_id<=0){ + return 0; + } + switch(type){ + case GBI_GUILDLV: { + printf("GBI_GUILDLV\n"); + if(dw>0 && g->guild_lv+dw<=50){ + g->guild_lv+=dw; + g->skill_point+=dw; + }else if(dw<0 && g->guild_lv+dw>=1) + g->guild_lv+=dw; + mapif_guild_info(-1,g); + inter_guild_tosql(g,1); + } return 0; + default: + printf("int_guild: GuildBasicInfoChange: Unknown type %d\n",type); + break; + } + mapif_guild_basicinfochanged(guild_id,type,data,len); + //inter_guild_tosql(g,1); // Change guild + return 0; +} + +// ギルドメンバデータ変更要求 +int mapif_parse_GuildMemberInfoChange(int fd,int guild_id,int account_id,int char_id, + int type,const char *data,int len) +{ + // Could make some improvement in speed, because only change guild_member + int i; + struct guild *g; + g=guild_pt; + inter_guild_fromsql(guild_id,g); + //printf("GuildMemberInfoChange %s \n",(type==GMI_EXP)?"GMI_EXP":"OTHER"); + + if(g==NULL){ + return 0; + } + for(i=0;imax_member;i++) + if( g->member[i].account_id==account_id && + g->member[i].char_id==char_id ) + break; + if(i==g->max_member){ + printf("int_guild: GuildMemberChange: Not found %d,%d in %d[%s]\n", + account_id,char_id,guild_id,g->name); + return 0; + } + switch(type){ + case GMI_POSITION: // 役職 + g->member[i].position=*((int *)data); + break; + case GMI_EXP: { // EXP + int exp,oldexp=g->member[i].exp; + exp=g->member[i].exp=*((unsigned int *)data); + g->exp+=(exp-oldexp); + guild_calcinfo(g); // Lvアップ判断 + mapif_guild_basicinfochanged(guild_id,GBI_EXP,&g->exp,4); + }break; + default: + printf("int_guild: GuildMemberInfoChange: Unknown type %d\n",type); + break; + } + mapif_guild_memberinfochanged(guild_id,account_id,char_id,type,data,len); + inter_guild_tosql(g,3); // Change guild & guild_member + return 0; +} + +// ギルド役職名変更要求 +int mapif_parse_GuildPosition(int fd,int guild_id,int idx,struct guild_position *p) +{ + // Could make some improvement in speed, because only change guild_position + struct guild *g; + g=guild_pt; + inter_guild_fromsql(guild_id,g); + + if(g==NULL || idx<0 || idx>=MAX_GUILDPOSITION){ + return 0; + } + memcpy(&g->position[idx],p,sizeof(struct guild_position)); + mapif_guild_position(g,idx); + printf("int_guild: position changed %d\n",idx); + inter_guild_tosql(g,4); // Change guild_position + return 0; +} +// ギルドスキルアップ要求 +int mapif_parse_GuildSkillUp(int fd,int guild_id,int skill_num,int account_id) +{ + // Could make some improvement in speed, because only change guild_position + struct guild *g; + g=guild_pt; + inter_guild_fromsql(guild_id,g); + int idx=skill_num-10000; + if(g==NULL || skill_num<10000) + return 0; + //printf("GuildSkillUp\n"); + + if( g->skill_point>0 && g->skill[idx].id>0 && + g->skill[idx].lv<10 ){ + g->skill[idx].lv++; + g->skill_point--; + if(guild_calcinfo(g)==0) + mapif_guild_info(-1,g); + mapif_guild_skillupack(guild_id,skill_num,account_id); + printf("int_guild: skill %d up\n",skill_num); + inter_guild_tosql(g,33); // Change guild & guild_skill + } + + return 0; +} +// ギルド同盟要求 +int mapif_parse_GuildAlliance(int fd,int guild_id1,int guild_id2, + int account_id1,int account_id2,int flag) +{ + // Could speed up + struct guild *g[2]; + int j,i; + g[0]=guild_pt; + g[1]=guild_pt2; + inter_guild_fromsql(guild_id1,g[0]); + inter_guild_fromsql(guild_id2,g[1]); + + if(g[0]==NULL || g[1]==NULL || g[0]->guild_id ==0 || g[1]->guild_id==0) + return 0; + + if(!(flag&0x8)){ + for(i=0;i<2-(flag&1);i++){ + for(j=0;jalliance[j].guild_id==0){ + g[i]->alliance[j].guild_id=g[1-i]->guild_id; + memcpy(g[i]->alliance[j].name,g[1-i]->name,24); + g[i]->alliance[j].opposition=flag&1; + break; + } + } + }else{ // 関係解消 + for(i=0;i<2-(flag&1);i++){ + for(j=0;jalliance[j].guild_id==g[1-i]->guild_id && + g[i]->alliance[j].opposition==(flag&1)){ + g[i]->alliance[j].guild_id=0; + break; + } + } + } + mapif_guild_alliance(guild_id1,guild_id2,account_id1,account_id2,flag, + g[0]->name,g[1]->name); + inter_guild_tosql(g[0],8); // Change guild_alliance + inter_guild_tosql(g[1],8); // Change guild_alliance + return 0; +} +// ギルド告知変更要求 +int mapif_parse_GuildNotice(int fd,int guild_id,const char *mes1,const char *mes2) +{ + struct guild *g; + g=guild_pt; + inter_guild_fromsql(guild_id,g); + + if(g==NULL||g->guild_id<=0) + return 0; + memcpy(g->mes1,mes1,60); + memcpy(g->mes2,mes2,120); + inter_guild_tosql(g,1); // Change mes of guild + return mapif_guild_notice(g); +} +// ギルドエンブレム変更要求 +int mapif_parse_GuildEmblem(int fd,int len,int guild_id,int dummy,const char *data) +{ + struct guild *g; + g=guild_pt; + inter_guild_fromsql(guild_id,g); + + if(g==NULL||g->guild_id<=0) + return 0; + memcpy(g->emblem_data,data,len); + g->emblem_len=len; + g->emblem_id++; + inter_guild_tosql(g,1); // Change guild + return mapif_guild_emblem(g); +} + +int mapif_parse_GuildCastleDataLoad(int fd,int castle_id,int index) // +{ + struct guild_castle *gc=guildcastle_pt; + inter_guildcastle_fromsql(castle_id, gc); + if(gc==NULL||gc->castle_id==-1){ + return mapif_guild_castle_dataload(castle_id,0,0); + } + switch(index){ + case 1: return mapif_guild_castle_dataload(gc->castle_id,index,gc->guild_id); break; + case 2: return mapif_guild_castle_dataload(gc->castle_id,index,gc->economy); break; + case 3: return mapif_guild_castle_dataload(gc->castle_id,index,gc->defense); break; + case 4: return mapif_guild_castle_dataload(gc->castle_id,index,gc->triggerE); break; + case 5: return mapif_guild_castle_dataload(gc->castle_id,index,gc->triggerD); break; + case 6: return mapif_guild_castle_dataload(gc->castle_id,index,gc->nextTime); break; + case 7: return mapif_guild_castle_dataload(gc->castle_id,index,gc->payTime); break; + case 8: return mapif_guild_castle_dataload(gc->castle_id,index,gc->createTime); break; + case 9: return mapif_guild_castle_dataload(gc->castle_id,index,gc->visibleC); break; + case 10: return mapif_guild_castle_dataload(gc->castle_id,index,gc->visibleG0); break; + case 11: return mapif_guild_castle_dataload(gc->castle_id,index,gc->visibleG1); break; + case 12: return mapif_guild_castle_dataload(gc->castle_id,index,gc->visibleG2); break; + case 13: return mapif_guild_castle_dataload(gc->castle_id,index,gc->visibleG3); break; + case 14: return mapif_guild_castle_dataload(gc->castle_id,index,gc->visibleG4); break; + case 15: return mapif_guild_castle_dataload(gc->castle_id,index,gc->visibleG5); break; + case 16: return mapif_guild_castle_dataload(gc->castle_id,index,gc->visibleG6); break; + case 17: return mapif_guild_castle_dataload(gc->castle_id,index,gc->visibleG7); break; + case 18: return mapif_guild_castle_dataload(gc->castle_id,index,gc->Ghp0); break; // guardian HP [Valaris] + case 19: return mapif_guild_castle_dataload(gc->castle_id,index,gc->Ghp1); break; + case 20: return mapif_guild_castle_dataload(gc->castle_id,index,gc->Ghp2); break; + case 21: return mapif_guild_castle_dataload(gc->castle_id,index,gc->Ghp3); break; + case 22: return mapif_guild_castle_dataload(gc->castle_id,index,gc->Ghp4); break; + case 23: return mapif_guild_castle_dataload(gc->castle_id,index,gc->Ghp5); break; + case 24: return mapif_guild_castle_dataload(gc->castle_id,index,gc->Ghp6); break; + case 25: return mapif_guild_castle_dataload(gc->castle_id,index,gc->Ghp7); break; // end additions [Valaris] + default: + printf("mapif_parse_GuildCastleDataLoad ERROR!! (Not found index=%d)\n", index); + return 0; + } +} + +int mapif_parse_GuildCastleDataSave(int fd,int castle_id,int index,int value) // +{ + struct guild_castle *gc=guildcastle_pt; + inter_guildcastle_fromsql(castle_id, gc); + if(gc==NULL||gc->castle_id==-1){ + return mapif_guild_castle_datasave(castle_id,index,value); + } + switch(index){ + case 1: + if( gc->guild_id!=value ){ + int gid=(value)?value:gc->guild_id; + struct guild *g=guild_pt; + inter_guild_fromsql(gid, g); + inter_log("guild %s (id=%d) %s castle id=%d" RETCODE, + (g)?g->name:"??" ,gid, (value)?"occupy":"abandon", index); + } + gc->guild_id = value; + break; + case 2: gc->economy = value; break; + case 3: gc->defense = value; break; + case 4: gc->triggerE = value; break; + case 5: gc->triggerD = value; break; + case 6: gc->nextTime = value; break; + case 7: gc->payTime = value; break; + case 8: gc->createTime = value; break; + case 9: gc->visibleC = value; break; + case 10: gc->visibleG0 = value; break; + case 11: gc->visibleG1 = value; break; + case 12: gc->visibleG2 = value; break; + case 13: gc->visibleG3 = value; break; + case 14: gc->visibleG4 = value; break; + case 15: gc->visibleG5 = value; break; + case 16: gc->visibleG6 = value; break; + case 17: gc->visibleG7 = value; break; + case 18: gc->Ghp0 = value; break; // guardian HP [Valaris] + case 19: gc->Ghp1 = value; break; + case 20: gc->Ghp2 = value; break; + case 21: gc->Ghp3 = value; break; + case 22: gc->Ghp4 = value; break; + case 23: gc->Ghp5 = value; break; + case 24: gc->Ghp6 = value; break; + case 25: gc->Ghp7 = value; break; // end additions [Valaris] + default: + printf("mapif_parse_GuildCastleDataSave ERROR!! (Not found index=%d)\n", index); + return 0; + } + inter_guildcastle_tosql(gc); + return mapif_guild_castle_datasave(gc->castle_id,index,value); +} + +// ギルドチェック要求 +int mapif_parse_GuildCheck(int fd,int guild_id,int account_id,int char_id) +{ + // What does this mean? Check if belong to another guild? + return 0; +} + +// map server からの通信 +// ・1パケットのみ解析すること +// ・パケット長データはinter.cにセットしておくこと +// ・パケット長チェックや、RFIFOSKIPは呼び出し元で行われるので行ってはならない +// ・エラーなら0(false)、そうでないなら1(true)をかえさなければならない +int inter_guild_parse_frommap(int fd) +{ + switch(RFIFOW(fd,0)){ + case 0x3030: mapif_parse_CreateGuild(fd,RFIFOL(fd,4),RFIFOP(fd,8),(struct guild_member *)RFIFOP(fd,32)); break; + case 0x3031: mapif_parse_GuildInfo(fd,RFIFOL(fd,2)); break; + case 0x3032: mapif_parse_GuildAddMember(fd,RFIFOL(fd,4),(struct guild_member *)RFIFOP(fd,8)); break; + case 0x3034: mapif_parse_GuildLeave(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14),RFIFOP(fd,15)); break; + case 0x3035: mapif_parse_GuildChangeMemberInfoShort(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14),RFIFOW(fd,15),RFIFOW(fd,17)); break; + case 0x3036: mapif_parse_BreakGuild(fd,RFIFOL(fd,2)); break; + case 0x3037: mapif_parse_GuildMessage(fd,RFIFOL(fd,4),RFIFOL(fd,8),RFIFOP(fd,12),RFIFOW(fd,2)-12); break; + case 0x3038: mapif_parse_GuildCheck(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10)); break; + case 0x3039: mapif_parse_GuildBasicInfoChange(fd,RFIFOL(fd,4),RFIFOW(fd,8),RFIFOP(fd,10),RFIFOW(fd,2)-10); break; + case 0x303A: mapif_parse_GuildMemberInfoChange(fd,RFIFOL(fd,4),RFIFOL(fd,8),RFIFOL(fd,12),RFIFOW(fd,16),RFIFOP(fd,18),RFIFOW(fd,2)-18); break; + case 0x303B: mapif_parse_GuildPosition(fd,RFIFOL(fd,4),RFIFOL(fd,8),(struct guild_position *)RFIFOP(fd,12)); break; + case 0x303C: mapif_parse_GuildSkillUp(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10)); break; + case 0x303D: mapif_parse_GuildAlliance(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOL(fd,14),RFIFOB(fd,18)); break; + case 0x303E: mapif_parse_GuildNotice(fd,RFIFOL(fd,2),RFIFOP(fd,6),RFIFOP(fd,66)); break; + case 0x303F: mapif_parse_GuildEmblem(fd,RFIFOW(fd,2)-12,RFIFOL(fd,4),RFIFOL(fd,8),RFIFOP(fd,12)); break; + case 0x3040: mapif_parse_GuildCastleDataLoad(fd,RFIFOW(fd,2),RFIFOB(fd,4)); break; + case 0x3041: mapif_parse_GuildCastleDataSave(fd,RFIFOW(fd,2),RFIFOB(fd,4),RFIFOL(fd,5)); break; + + default: + return 0; + } + return 1; +} + +int inter_guild_mapif_init(int fd) +{ + return mapif_guild_castle_alldataload(fd); +} + +// サーバーから脱退要求(キャラ削除用) +int inter_guild_leave(int guild_id,int account_id,int char_id) +{ + return mapif_parse_GuildLeave(-1,guild_id,account_id,char_id,0,"**サーバー命令**"); +} diff --git a/misc/src/char_sql/int_guild.h b/misc/src/char_sql/int_guild.h new file mode 100644 index 0000000..8f4203d --- /dev/null +++ b/misc/src/char_sql/int_guild.h @@ -0,0 +1,10 @@ +#ifndef _INT_GUILD_H_ +#define _INT_GUILD_H_ + +int inter_guild_parse_frommap(int fd); +int inter_guild_sql_init(); +int inter_guild_mapif_init(int fd); + +int inter_guild_leave(int guild_id,int account_id,int char_id); + +#endif diff --git a/misc/src/char_sql/int_party.c b/misc/src/char_sql/int_party.c new file mode 100644 index 0000000..84de078 --- /dev/null +++ b/misc/src/char_sql/int_party.c @@ -0,0 +1,755 @@ +// +// original code from athena +// SQL conversion by hack +// + +#include "char.h" +#include "strlib.h" +#include "socket.h" +#include +#include +#include + +static struct party *party_pt; +static int party_newid=100; + +int mapif_party_broken(int party_id,int flag); +int party_check_empty(struct party *p); +int mapif_parse_PartyLeave(int fd,int party_id,int account_id); + +// Save party to mysql +int inter_party_tosql(int party_id,struct party *p) +{ + // 'party' ('party_id','name','exp','item','leader') + + char t_name[100]; + char t_member[24]; + int party_member = 0, party_online_member = 0; + int party_exist = 0; + int leader_id = 0; + int i = 0; + + printf("(\033[1;64m%d\033[0m) Request save party - ",party_id); + + jstrescapecpy(t_name, p->name); + + if (p==NULL || party_id==0 || p->party_id ==0 || party_id!=p->party_id) { + printf("- Party pointer or party_id error \n"); + return 0; + } + + // Check if party exists + sprintf(tmp_sql,"SELECT count(*) FROM `%s` WHERE `party_id`='%d'",party_db, party_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + return 0; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + sql_row = mysql_fetch_row(sql_res); + party_exist = atoi (sql_row[0]); + //printf("- Check if party %d exists : %s\n",party_id,party_exist==0?"No":"Yes"); + } + mysql_free_result(sql_res) ; //resource free + + if (party_exist >0){ + // Check members in party + sprintf(tmp_sql,"SELECT count(*) FROM `%s` WHERE `party_id`='%d'",char_db, party_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + return 0; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + sql_row = mysql_fetch_row(sql_res); + + party_member = atoi (sql_row[0]); + // printf("- Check members in party %d : %d \n",party_id,party_member); + + } + mysql_free_result(sql_res) ; //resource free + + party_online_member = 0; + i=0; + while (imember[i].account_id>0) party_online_member++; + i++; + } + + //if (party_online_member==0) printf("- No member online \n"); else printf("- Some member %d online \n", party_online_member); + + if (party_member <= 0 && party_online_member == 0) { + + // Delete the party, if has no member. + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `party_id`='%d'",party_db, party_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } + // printf("No member in party %d, break it \n",party_id); + memset(p, 0, sizeof(struct party)); + return 0; + } else { + // Update party information, if exists + + int i=0; + + for (i=0;imember[i].account_id>0){ + sprintf(tmp_sql,"UPDATE `%s` SET `party_id`='%d', `online`='%d' WHERE `account_id`='%d' AND `name`='%s'", + char_db, party_id, p->member[i].online, p->member[i].account_id,jstrescapecpy(t_member,p->member[i].name)); + //printf("%s",tmp_sql); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); + } + } + } + + + sprintf(tmp_sql,"UPDATE `%s` SET `name`='%s', `exp`='%d', `item`='%d', `leader_id`=`leader_id` WHERE `party_id`='%d'", + party_db, t_name,p->exp,p->item,party_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (inset/update `party`)- %s\n", mysql_error(&mysql_handle) ); + } + + + // printf("- Update party %d information \n",party_id); + } + } else { + // Add new party, if not exist + int i = 0; + while (imember[i].account_id>0&&p->member[i].leader==0)||(p->member[i].account_id<0))) i++; + if (imember[i].account_id; + sprintf(tmp_sql,"INSERT INTO `%s` (`party_id`, `name`, `exp`, `item`, `leader_id`) VALUES ('%d', '%s', '%d', '%d', '%d')", + party_db, party_id, t_name, p->exp, p->item,leader_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (inset/update `party`)- %s\n", mysql_error(&mysql_handle) ); + return 0; + } + + sprintf(tmp_sql,"UPDATE `%s` SET `party_id`='%d', `online`='1' WHERE `account_id`='%d' AND `name`='%s'", + char_db, party_id,leader_id, jstrescapecpy(t_member,p->member[i].name)); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (inset/update `party`)- %s\n", mysql_error(&mysql_handle) ); + } + + //printf("- Insert new party %d \n",party_id); + } + + printf("Party save success\n"); + return 0; + +} + +// Read party from mysql +int inter_party_fromsql(int party_id,struct party *p) +{ + int leader_id=0; + printf("(\033[1;64m%d\033[0m) Request load party - ",party_id); + + memset(p, 0, sizeof(struct party)); + + sprintf(tmp_sql,"SELECT `party_id`, `name`,`exp`,`item`, `leader_id` FROM `%s` WHERE `party_id`='%d'",party_db, party_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `party`)- %s\n", mysql_error(&mysql_handle) ); + return 0; + } + + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + sql_row = mysql_fetch_row(sql_res); + // printf("- Read party %d from MySQL\n",party_id); + p->party_id = party_id; + strcpy(p->name, sql_row[1]); + p->exp = atoi(sql_row[2]); + p->item = atoi(sql_row[3]); + leader_id = atoi(sql_row[4]); + } else { + mysql_free_result(sql_res); + // printf("- Cannot find party %d \n",party_id); + return 0; + } + + mysql_free_result(sql_res); + + // Load members + sprintf(tmp_sql,"SELECT `account_id`, `name`,`base_level`,`last_map`,`online` FROM `%s` WHERE `party_id`='%d'",char_db, party_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `party`)- %s\n", mysql_error(&mysql_handle) ); + return 0; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + int i; + for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){ + struct party_member *m = &p->member[i]; + m->account_id = atoi(sql_row[0]); + if (m->account_id == leader_id) m->leader = 1; else m->leader = 0; + strncpy(m->name,sql_row[1],sizeof(m->name)); + m->lv = atoi(sql_row[2]); + strncpy(m->map,sql_row[3],sizeof(m->map)); + m->online = atoi(sql_row[4]); + } + // printf("- %d members found in party %d \n",i,party_id); + } + mysql_free_result(sql_res); + + + printf("Party load success\n"); + return 0; + +} + +int inter_party_sql_init(){ + int i; + + //memory alloc + printf("interserver party memory initialize.... (%d byte)\n",sizeof(struct party)); + party_pt = calloc(sizeof(struct party), 1); + + sprintf(tmp_sql,"UPDATE `%s` SET `online`='0'", char_db); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); + } + + sprintf (tmp_sql , "SELECT count(*) FROM `%s`",party_db); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle) ; + sql_row = mysql_fetch_row(sql_res); + printf("total party data -> '%s'.......\n",sql_row[0]); + i = atoi (sql_row[0]); + mysql_free_result(sql_res); + + if (i > 0) { + //set party_newid + sprintf (tmp_sql , "SELECT max(`party_id`) FROM `%s`", party_db); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } + + sql_res = mysql_store_result(&mysql_handle) ; + + sql_row = mysql_fetch_row(sql_res); + party_newid = atoi (sql_row[0])+1; + mysql_free_result(sql_res); + } + + printf("set party_newid: %d.......\n",party_newid); + + return 0; +} + + +// Search for the party according to its name +struct party* search_partyname(char *str) +{ + struct party *p=NULL; + int leader_id = 0; + char t_name[24]; + + sprintf(tmp_sql,"SELECT `party_id`, `name`,`exp`,`item`,`leader_id` FROM `%s` WHERE `name`='%s'",party_db, jstrescapecpy(t_name,str)); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `party`)- %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res==NULL || mysql_num_rows(sql_res)<=0) { mysql_free_result(sql_res); return p; } + sql_row = mysql_fetch_row(sql_res); + p = party_pt; + p->party_id = atoi(sql_row[0]); + strcpy(p->name, sql_row[1]); + p->exp = atoi(sql_row[2]); + p->item = atoi(sql_row[3]); + leader_id = atoi(sql_row[4]); + mysql_free_result(sql_res); + + // Load members + sprintf(tmp_sql,"SELECT `account_id`, `name`,`base_level`,`last_map`,`online` FROM `%s` WHERE `party_id`='%d'",char_db, p->party_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `party`)- %s\n", mysql_error(&mysql_handle) ); + return 0; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + int i; + for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){ + struct party_member *m = &p->member[i]; + m->account_id = atoi(sql_row[0]); + if (m->account_id == leader_id) m->leader = 1; else m->leader = 0; + strncpy(m->name,sql_row[1],sizeof(m->name)); + m->lv = atoi(sql_row[2]); + strncpy(m->map,sql_row[3],sizeof(m->map)); + m->online = atoi(sql_row[4]); + } + printf("- %d members found in party %d \n",i,p->party_id); + } + mysql_free_result(sql_res); + + return p; +} + +// EXP公平分配できるかチェック +int party_check_exp_share(struct party *p) +{ + int i; + int maxlv=0,minlv=0x7fffffff; + for(i=0;imember[i].lv; + if( p->member[i].online ){ + if( lv < minlv ) minlv=lv; + if( maxlv < lv ) maxlv=lv; + } + } + return (maxlv==0 || maxlv-minlv<=party_share_level); +} +// Is there any member in the party? +int party_check_empty(struct party *p) +{ + int i; + if (p==NULL||p->party_id==0) return 1; +// printf("party check empty %08X\n",(int)p); + for(i=0;imember[i].account_id); + if(p->member[i].account_id>0){ + return 0; + } + } + // If there is no member, then break the party + mapif_party_broken(p->party_id,0); + inter_party_tosql(p->party_id,p); + return 1; +} + + +// Check if a member is in two party, not necessary :) +int party_check_conflict(int party_id,int account_id,char *nick) +{ + return 0; +} + +//------------------------------------------------------------------- +// map serverへの通信 + +// パーティ作成可否 +int mapif_party_created(int fd,int account_id,struct party *p) +{ + WFIFOW(fd,0)=0x3820; + WFIFOL(fd,2)=account_id; + if(p!=NULL){ + WFIFOB(fd,6)=0; + WFIFOL(fd,7)=p->party_id; + memcpy(WFIFOP(fd,11),p->name,24); + printf("int_party: created! %d %s\n",p->party_id,p->name); + }else{ + WFIFOB(fd,6)=1; + WFIFOL(fd,7)=0; + memcpy(WFIFOP(fd,11),"error",24); + } + WFIFOSET(fd,35); + return 0; +} + +// パーティ情報見つからず +int mapif_party_noinfo(int fd,int party_id) +{ + WFIFOW(fd,0)=0x3821; + WFIFOW(fd,2)=8; + WFIFOL(fd,4)=party_id; + WFIFOSET(fd,8); + printf("int_party: info not found %d\n",party_id); + return 0; +} +// パーティ情報まとめ送り +int mapif_party_info(int fd,struct party *p) +{ + unsigned char buf[1024]; + WBUFW(buf,0)=0x3821; + memcpy(buf+4,p,sizeof(struct party)); + WBUFW(buf,2)=4+sizeof(struct party); + if(fd<0) + mapif_sendall(buf,WBUFW(buf,2)); + else + mapif_send(fd,buf,WBUFW(buf,2)); +// printf("int_party: info %d %s\n",p->party_id,p->name); + return 0; +} +// パーティメンバ追加可否 +int mapif_party_memberadded(int fd,int party_id,int account_id,int flag) +{ + WFIFOW(fd,0)=0x3822; + WFIFOL(fd,2)=party_id; + WFIFOL(fd,6)=account_id; + WFIFOB(fd,10)=flag; + WFIFOSET(fd,11); + return 0; +} +// パーティ設定変更通知 +int mapif_party_optionchanged(int fd,struct party *p,int account_id,int flag) +{ + unsigned char buf[16]; + WBUFW(buf,0)=0x3823; + WBUFL(buf,2)=p->party_id; + WBUFL(buf,6)=account_id; + WBUFW(buf,10)=p->exp; + WBUFW(buf,12)=p->item; + WBUFB(buf,14)=flag; + if(flag==0) + mapif_sendall(buf,15); + else + mapif_send(fd,buf,15); + //printf("int_party: option changed %d %d %d %d %d\n",p->party_id,account_id,p->exp,p->item,flag); + return 0; +} +// パーティ脱退通知 +int mapif_party_leaved(int party_id,int account_id,char *name) +{ + unsigned char buf[64]; + WBUFW(buf,0)=0x3824; + WBUFL(buf,2)=party_id; + WBUFL(buf,6)=account_id; + memcpy(WBUFP(buf,10),name,24); + mapif_sendall(buf,34); + //printf("int_party: party leaved %d %d %s\n",party_id,account_id,name); + return 0; +} +// パーティマップ更新通知 +int mapif_party_membermoved(struct party *p,int idx) +{ + unsigned char buf[32]; + WBUFW(buf,0)=0x3825; + WBUFL(buf,2)=p->party_id; + WBUFL(buf,6)=p->member[idx].account_id; + memcpy(WBUFP(buf,10),p->member[idx].map,16); + WBUFB(buf,26)=p->member[idx].online; + WBUFW(buf,27)=p->member[idx].lv; + mapif_sendall(buf,29); + return 0; +} +// パーティ解散通知 +int mapif_party_broken(int party_id,int flag) +{ + unsigned char buf[16]; + WBUFW(buf,0)=0x3826; + WBUFL(buf,2)=party_id; + WBUFB(buf,6)=flag; + mapif_sendall(buf,7); + //printf("int_party: broken %d\n",party_id); + return 0; +} +// パーティ内発言 +int mapif_party_message(int party_id,int account_id,char *mes,int len) +{ + unsigned char buf[512]; + WBUFW(buf,0)=0x3827; + WBUFW(buf,2)=len+12; + WBUFL(buf,4)=party_id; + WBUFL(buf,8)=account_id; + memcpy(WBUFP(buf,12),mes,len); + mapif_sendall(buf,len+12); + return 0; +} + +//------------------------------------------------------------------- +// map serverからの通信 + + +// Create Party +int mapif_parse_CreateParty(int fd,int account_id,char *name,char *nick,char *map,int lv) +{ + struct party *p; + if( (p=search_partyname(name))!=NULL){ +// printf("int_party: same name party exists [%s]\n",name); + mapif_party_created(fd,account_id,NULL); + return 0; + } + p=party_pt; + if(p==NULL){ + printf("int_party: out of memory !\n"); + mapif_party_created(fd,account_id,NULL); + return 0; + } + memset(p,0,sizeof(struct party)); + p->party_id=party_newid++; + memcpy(p->name,name,24); + p->exp=0; + p->item=0; + p->member[0].account_id=account_id; + memcpy(p->member[0].name,nick,24); + memcpy(p->member[0].map,map,16); + p->member[0].leader=1; + p->member[0].online=1; + p->member[0].lv=lv; + + inter_party_tosql(p->party_id,p); + + mapif_party_created(fd,account_id,p); + mapif_party_info(fd,p); + + return 0; +} +// パーティ情報要求 +int mapif_parse_PartyInfo(int fd,int party_id) +{ + struct party *p = party_pt; + if(p==NULL){ + printf("int_party: out of memory !\n"); + return 0; + } + inter_party_fromsql(party_id, p); + + if(p->party_id >= 0) + mapif_party_info(fd,p); + else + mapif_party_noinfo(fd,party_id); + return 0; +} +// パーティ追加要求 +int mapif_parse_PartyAddMember(int fd,int party_id,int account_id,char *nick,char *map,int lv) +{ + struct party *p; + int i; + + p = party_pt; + if(p==NULL){ + printf("int_party: out of memory !\n"); + return 0; + } + inter_party_fromsql(party_id, p); + + if(p->party_id <= 0){ + mapif_party_memberadded(fd,party_id,account_id,1); + return 0; + } + + for(i=0;imember[i].account_id==0){ + int flag=0; + + p->member[i].account_id=account_id; + memcpy(p->member[i].name,nick,24); + memcpy(p->member[i].map,map,16); + p->member[i].leader=0; + p->member[i].online=1; + p->member[i].lv=lv; + mapif_party_memberadded(fd,party_id,account_id,0); + mapif_party_info(-1,p); + + if( p->exp>0 && !party_check_exp_share(p) ){ + p->exp=0; + flag=0x01; + } + if(flag) + mapif_party_optionchanged(fd,p,0,0); + + inter_party_tosql(party_id, p); + return 0; + } + } + mapif_party_memberadded(fd,party_id,account_id,1); + //inter_party_tosql(party_id, p); + return 0; +} +// パーティー設定変更要求 +int mapif_parse_PartyChangeOption(int fd,int party_id,int account_id,int exp,int item) +{ + struct party *p; + int flag=0; + + p = party_pt; + if(p==NULL){ + printf("int_party: out of memory !\n"); + return 0; + } + + inter_party_fromsql(party_id, p); + + if(p->party_id <= 0){ + return 0; + } + + p->exp=exp; + if( exp>0 && !party_check_exp_share(p) ){ + flag|=0x01; + p->exp=0; + } + + p->item=item; + + mapif_party_optionchanged(fd,p,account_id,flag); + inter_party_tosql(party_id, p); + return 0; +} +// パーティ脱退要求 +int mapif_parse_PartyLeave(int fd,int party_id,int account_id) +{ + char t_member[24]; + struct party *p = party_pt; + if(p==NULL){ + printf("int_party: out of memory !\n"); + return 0; + } + + inter_party_fromsql(party_id, p); + + if(p->party_id >= 0){ + int i,j; + for(i=0;imember[i].account_id==account_id){ + //printf("p->member[i].account_id = %d , account_id = %d \n",p->member[i].account_id,account_id); + mapif_party_leaved(party_id,account_id,p->member[i].name); + + + + // Update char information, does the name need encoding? + sprintf(tmp_sql,"UPDATE `%s` SET `party_id`='0', `online`='1' WHERE `party_id`='%d' AND `name`='%s'", + char_db, party_id, jstrescapecpy(t_member,p->member[i].name)); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); + } +// printf("Delete member %s from MySQL \n", p->member[i].name); + + if (p->member[i].leader==1){ + for(j=0;jmember[j].account_id = %d , p->member[j].account_id = %d \n",j,p->member[j].account_id,p->member[j].account_id); + if(p->member[j].account_id>0&&j!=i){ + mapif_party_leaved(party_id,p->member[j].account_id,p->member[j].name); + // Update char information, does the name need encoding? + sprintf(tmp_sql,"UPDATE `%s` SET `party_id`='0', `online`='1' WHERE `party_id`='%d' AND `name`='%s'", + char_db, party_id, jstrescapecpy(t_member,p->member[i].name)); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); + } +// printf("Delete member %s from MySQL \n", p->member[j].name); + } + } + // Delete the party, if has no member. + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `party_id`='%d'",party_db, party_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } +// printf("Leader breaks party %d \n",party_id); + memset(p, 0, sizeof(struct party)); + }else memset(&p->member[i],0,sizeof(struct party_member)); + + break; + + } + } + if( party_check_empty(p)==0 ) + mapif_party_info(-1,p);// まだ人がいるのでデータ送信 + /* + else + inter_party_tosql(party_id,p); // Break the party if no member + */ + }else{ + sprintf(tmp_sql,"UPDATE `%s` SET `party_id`='0' WHERE `party_id`='%d' AND `account_id`='%d' AND `online`='1'", + char_db, party_id, account_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (update `char`)- %s\n", mysql_error(&mysql_handle) ); + } + } + return 0; +} +// When member goes to other map +int mapif_parse_PartyChangeMap(int fd,int party_id,int account_id,char *map,int online,int lv) +{ + struct party *p; + int i; + + p = party_pt; + if(p==NULL){ + printf("int_party: out of memory !\n"); + return 0; + } + inter_party_fromsql(party_id, p); + + if(p->party_id <= 0){ + return 0; + } + for(i=0;imember[i].account_id==account_id){ + int flag=0; + + memcpy(p->member[i].map,map,16); + p->member[i].online=online; + p->member[i].lv=lv; + mapif_party_membermoved(p,i); + + if( p->exp>0 && !party_check_exp_share(p) ){ + p->exp=0; + flag=1; + } + if(flag) + mapif_party_optionchanged(fd,p,0,0); + break; + } + } + inter_party_tosql(party_id, p); + return 0; +} +// パーティ解散要求 +int mapif_parse_BreakParty(int fd,int party_id) +{ + struct party *p; + + p = party_pt; + if(p==NULL){ + printf("int_party: out of memory !\n"); + return 0; + } + + inter_party_fromsql(party_id, p); + + if(p->party_id <= 0){ + return 0; + } + inter_party_tosql(party_id,p); + + mapif_party_broken(fd,party_id); + return 0; +} +// パーティメッセージ送信 +int mapif_parse_PartyMessage(int fd,int party_id,int account_id,char *mes,int len) +{ + return mapif_party_message(party_id,account_id,mes,len); +} +// パーティチェック要求 +int mapif_parse_PartyCheck(int fd,int party_id,int account_id,char *nick) +{ + return party_check_conflict(party_id,account_id,nick); +} + +// map server からの通信 +// ・1パケットのみ解析すること +// ・パケット長データはinter.cにセットしておくこと +// ・パケット長チェックや、RFIFOSKIPは呼び出し元で行われるので行ってはならない +// ・エラーなら0(false)、そうでないなら1(true)をかえさなければならない +int inter_party_parse_frommap(int fd) +{ + switch(RFIFOW(fd,0)){ + case 0x3020: mapif_parse_CreateParty(fd,RFIFOL(fd,2),RFIFOP(fd,6),RFIFOP(fd,30),RFIFOP(fd,54),RFIFOW(fd,70)); break; + case 0x3021: mapif_parse_PartyInfo(fd,RFIFOL(fd,2)); break; + case 0x3022: mapif_parse_PartyAddMember(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOP(fd,10),RFIFOP(fd,34),RFIFOW(fd,50)); break; + case 0x3023: mapif_parse_PartyChangeOption(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOW(fd,10),RFIFOW(fd,12)); break; + case 0x3024: mapif_parse_PartyLeave(fd,RFIFOL(fd,2),RFIFOL(fd,6)); break; + case 0x3025: mapif_parse_PartyChangeMap(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOP(fd,10),RFIFOB(fd,26),RFIFOW(fd,27)); break; + case 0x3026: mapif_parse_BreakParty(fd,RFIFOL(fd,2)); break; + case 0x3027: mapif_parse_PartyMessage(fd,RFIFOL(fd,4),RFIFOL(fd,8),RFIFOP(fd,12),RFIFOW(fd,2)-12); break; + case 0x3028: mapif_parse_PartyCheck(fd,RFIFOL(fd,2),RFIFOL(fd,6),RFIFOP(fd,10)); break; + default: + return 0; + } + return 1; +} + +// サーバーから脱退要求(キャラ削除用) +int inter_party_leave(int party_id,int account_id) +{ + return mapif_parse_PartyLeave(-1,party_id,account_id); +} + + + diff --git a/misc/src/char_sql/int_party.h b/misc/src/char_sql/int_party.h new file mode 100644 index 0000000..04f71c8 --- /dev/null +++ b/misc/src/char_sql/int_party.h @@ -0,0 +1,8 @@ +#ifndef _INT_PARTY_H_ +#define _INT_PARTY_H_ + +int inter_party_parse_frommap(int fd); +int inter_party_sql_init(); +int inter_party_leave(int party_id,int account_id); + +#endif diff --git a/misc/src/char_sql/int_pet.c b/misc/src/char_sql/int_pet.c new file mode 100644 index 0000000..9b5566d --- /dev/null +++ b/misc/src/char_sql/int_pet.c @@ -0,0 +1,324 @@ +// +// original code from athena +// SQL conversion by Jioh L. Jung +// +#include "char.h" +#include "strlib.h" + +#include +#include +#include + +struct s_pet *pet_pt; +static int pet_newid = 100; + + +//--------------------------------------------------------- +int inter_pet_tosql(int pet_id, struct s_pet *p) { + //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate`) + char t_name[100]; + + printf("request save pet: %d.......\n",pet_id); + + jstrescapecpy(t_name, p->name); + + if(p->hungry < 0) + p->hungry = 0; + else if(p->hungry > 100) + p->hungry = 100; + if(p->intimate < 0) + p->intimate = 0; + else if(p->intimate > 1000) + p->intimate = 1000; + sprintf(tmp_sql,"SELECT * FROM `%s` WHERE `pet_id`='%d'",pet_db, pet_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) + //row reside -> updating + sprintf(tmp_sql, "UPDATE `%s` SET `class`='%d',`name`='%s',`account_id`='%d',`char_id`='%d',`level`='%d',`egg_id`='%d',`equip`='%d',`intimate`='%d',`hungry`='%d',`rename_flag`='%d',`incuvate`='%d' WHERE `pet_id`='%d'", + pet_db, p->class, t_name, p->account_id, p->char_id, p->level, p->egg_id, + p->equip, p->intimate, p->hungry, p->rename_flag, p->incuvate, p->pet_id); + else //no row -> insert + sprintf(tmp_sql,"INSERT INTO `%s` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate`) VALUES ('%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')", + pet_db, pet_id, p->class, t_name, p->account_id, p->char_id, p->level, p->egg_id, + p->equip, p->intimate, p->hungry, p->rename_flag, p->incuvate); + mysql_free_result(sql_res) ; //resource free + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (inset/update `pet`)- %s\n", mysql_error(&mysql_handle) ); + } + + printf("pet save success.......\n"); + return 0; +} + +int inter_pet_fromsql(int pet_id, struct s_pet *p){ + + printf("request load pet: %d.......\n",pet_id); + + memset(p, 0, sizeof(struct s_pet)); + + //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate`) + + sprintf(tmp_sql,"SELECT `pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incuvate` FROM `%s` WHERE `pet_id`='%d'",pet_db, pet_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `pet`)- %s\n", mysql_error(&mysql_handle) ); + return 0; + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + sql_row = mysql_fetch_row(sql_res); + + p->pet_id = pet_id; + p->class = atoi(sql_row[1]); + memcpy(p->name, sql_row[2],24); + p->account_id = atoi(sql_row[3]); + p->char_id = atoi(sql_row[4]); + p->level = atoi(sql_row[5]); + p->egg_id = atoi(sql_row[6]); + p->equip = atoi(sql_row[7]); + p->intimate = atoi(sql_row[8]); + p->hungry = atoi(sql_row[9]); + p->rename_flag = atoi(sql_row[10]); + p->incuvate = atoi(sql_row[11]); + } + if(p->hungry < 0) + p->hungry = 0; + else if(p->hungry > 100) + p->hungry = 100; + if(p->intimate < 0) + p->intimate = 0; + else if(p->intimate > 1000) + p->intimate = 1000; + + mysql_free_result(sql_res); + + printf("pet load success.......\n"); + return 0; +} +//---------------------------------------------- + +int inter_pet_sql_init(){ + int i; + + //memory alloc + printf("interserver pet memory initialize.... (%d byte)\n",sizeof(struct s_pet)); + pet_pt = calloc(sizeof(struct s_pet), 1); + + sprintf (tmp_sql , "SELECT count(*) FROM `%s`", pet_db); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + exit(0); + } + sql_res = mysql_store_result(&mysql_handle) ; + sql_row = mysql_fetch_row(sql_res); + printf("total pet data -> '%s'.......\n",sql_row[0]); + i = atoi (sql_row[0]); + mysql_free_result(sql_res); + + if (i > 0) { + //set pet_newid + sprintf (tmp_sql , "SELECT max(`pet_id`) FROM `%s`",pet_db ); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } + + sql_res = mysql_store_result(&mysql_handle) ; + + sql_row = mysql_fetch_row(sql_res); + pet_newid = atoi (sql_row[0]); + mysql_free_result(sql_res); + } + + printf("set pet_newid: %d.......\n",pet_newid); + + return 0; +} +//---------------------------------- +int inter_pet_delete(int pet_id){ + printf("request delete pet: %d.......\n",pet_id); + + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `pet_id`='%d'",pet_db, pet_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } + return 0; +} +//------------------------------------------------------ +int mapif_pet_created(int fd, int account_id, struct s_pet *p) +{ + WFIFOW(fd, 0) =0x3880; + WFIFOL(fd, 2) =account_id; + if(p!=NULL){ + WFIFOB(fd, 6)=0; + WFIFOL(fd, 7) =p->pet_id; + printf("int_pet: created! %d %s\n", p->pet_id, p->name); + }else{ + WFIFOB(fd, 6)=1; + WFIFOL(fd, 7)=0; + } + WFIFOSET(fd, 11); + + return 0; +} + +int mapif_pet_info(int fd, int account_id, struct s_pet *p){ + WFIFOW(fd, 0) =0x3881; + WFIFOW(fd, 2) =sizeof(struct s_pet) + 9; + WFIFOL(fd, 4) =account_id; + WFIFOB(fd, 8)=0; + memcpy(WFIFOP(fd, 9), p, sizeof(struct s_pet)); + WFIFOSET(fd, WFIFOW(fd, 2)); + + return 0; +} + +int mapif_pet_noinfo(int fd, int account_id){ + WFIFOW(fd, 0) =0x3881; + WFIFOW(fd, 2) =sizeof(struct s_pet) + 9; + WFIFOL(fd, 4) =account_id; + WFIFOB(fd, 8)=1; + memset(WFIFOP(fd, 9), 0, sizeof(struct s_pet)); + WFIFOSET(fd, WFIFOW(fd, 2)); + + return 0; +} + +int mapif_save_pet_ack(int fd, int account_id, int flag){ + WFIFOW(fd, 0) =0x3882; + WFIFOL(fd, 2) =account_id; + WFIFOB(fd, 6) =flag; + WFIFOSET(fd, 7); + + return 0; +} + +int mapif_delete_pet_ack(int fd, int flag){ + WFIFOW(fd, 0) =0x3883; + WFIFOB(fd, 2) =flag; + WFIFOSET(fd, 3); + + return 0; +} + +int mapif_create_pet(int fd, int account_id, int char_id, short pet_class, short pet_lv, short pet_egg_id, + short pet_equip, short intimate, short hungry, char rename_flag, char incuvate, char *pet_name){ + + memset(pet_pt, 0, sizeof(struct s_pet)); + pet_pt->pet_id = pet_newid++; + memcpy(pet_pt->name, pet_name, 24); + if(incuvate == 1) + pet_pt->account_id = pet_pt->char_id = 0; + else { + pet_pt->account_id = account_id; + pet_pt->char_id = char_id; + } + pet_pt->class = pet_class; + pet_pt->level = pet_lv; + pet_pt->egg_id = pet_egg_id; + pet_pt->equip = pet_equip; + pet_pt->intimate = intimate; + pet_pt->hungry = hungry; + pet_pt->rename_flag = rename_flag; + pet_pt->incuvate = incuvate; + + if(pet_pt->hungry < 0) + pet_pt->hungry = 0; + else if(pet_pt->hungry > 100) + pet_pt->hungry = 100; + if(pet_pt->intimate < 0) + pet_pt->intimate = 0; + else if(pet_pt->intimate > 1000) + pet_pt->intimate = 1000; + + inter_pet_tosql(pet_pt->pet_id,pet_pt); + + mapif_pet_created(fd, account_id, pet_pt); + + return 0; +} + +int mapif_load_pet(int fd, int account_id, int char_id, int pet_id){ + memset(pet_pt, 0, sizeof(struct s_pet)); + + inter_pet_fromsql(pet_id, pet_pt); + + if(pet_pt!=NULL) { + if(pet_pt->incuvate == 1) { + pet_pt->account_id = pet_pt->char_id = 0; + mapif_pet_info(fd, account_id, pet_pt); + } + else if(account_id == pet_pt->account_id && char_id == pet_pt->char_id) + mapif_pet_info(fd, account_id, pet_pt); + else + mapif_pet_noinfo(fd, account_id); + } + else + mapif_pet_noinfo(fd, account_id); + + return 0; +} + +int mapif_save_pet(int fd, int account_id, struct s_pet *data) { + //here process pet save request. + int len=RFIFOW(fd, 2); + if(sizeof(struct s_pet)!=len-8) { + printf("inter pet: data size error %d %d\n", sizeof(struct s_pet), len-8); + } + + else{ + if(data->hungry < 0) + data->hungry = 0; + else if(data->hungry > 100) + data->hungry = 100; + if(data->intimate < 0) + data->intimate = 0; + else if(data->intimate > 1000) + data->intimate = 1000; + inter_pet_tosql(data->pet_id,data); + mapif_save_pet_ack(fd, account_id, 0); + } + + return 0; +} + +int mapif_delete_pet(int fd, int pet_id){ + mapif_delete_pet_ack(fd, inter_pet_delete(pet_id)); + + return 0; +} + +int mapif_parse_CreatePet(int fd){ + mapif_create_pet(fd, RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOW(fd, 10), RFIFOW(fd, 12), RFIFOW(fd, 14), RFIFOW(fd, 16), RFIFOL(fd, 18), + RFIFOL(fd, 20), RFIFOB(fd, 22), RFIFOB(fd, 23), RFIFOP(fd, 24)); + return 0; +} + +int mapif_parse_LoadPet(int fd){ + mapif_load_pet(fd, RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOL(fd, 10)); + return 0; +} + +int mapif_parse_SavePet(int fd){ + mapif_save_pet(fd, RFIFOL(fd, 4), (struct s_pet *) RFIFOP(fd, 8)); + return 0; +} + +int mapif_parse_DeletePet(int fd){ + mapif_delete_pet(fd, RFIFOL(fd, 2)); + return 0; +} + +int inter_pet_parse_frommap(int fd){ + switch(RFIFOW(fd, 0)){ + case 0x3080: mapif_parse_CreatePet(fd); break; + case 0x3081: mapif_parse_LoadPet(fd); break; + case 0x3082: mapif_parse_SavePet(fd); break; + case 0x3083: mapif_parse_DeletePet(fd); break; + default: + return 0; + } + return 1; +} + diff --git a/misc/src/char_sql/int_pet.h b/misc/src/char_sql/int_pet.h new file mode 100644 index 0000000..b6e3f1b --- /dev/null +++ b/misc/src/char_sql/int_pet.h @@ -0,0 +1,12 @@ +#ifndef _INT_PET_H_ +#define _INT_PET_H_ + +int inter_pet_init(); +int inter_pet_save(); +int inter_pet_delete(int pet_id); + +int inter_pet_parse_frommap(int fd); +int inter_pet_sql_init(); +//extern char pet_txt[256]; + +#endif diff --git a/misc/src/char_sql/int_storage.c b/misc/src/char_sql/int_storage.c new file mode 100644 index 0000000..fdf85ae --- /dev/null +++ b/misc/src/char_sql/int_storage.c @@ -0,0 +1,366 @@ +// +// original code from athena +// SQL conversion by Jioh L. Jung +// +#include "char.h" +#include "itemdb.h" +#include +#include + +#define STORAGE_MEMINC 16 + +// reset by inter_config_read() +struct storage *storage_pt=NULL; +struct guild_storage *guild_storage_pt=NULL; + + +// storage data -> DB conversion +int storage_tosql(int account_id,struct storage *p){ + int i; + int eqcount=1; + int noteqcount=1; + struct itemtemp mapitem; + for(i=0;istorage[i].nameid>0){ + if(itemdb_isequip(p->storage[i].nameid)==1){ + mapitem.equip[eqcount].flag=0; + mapitem.equip[eqcount].id = p->storage[i].id; + mapitem.equip[eqcount].nameid=p->storage[i].nameid; + mapitem.equip[eqcount].amount = p->storage[i].amount; + mapitem.equip[eqcount].equip = p->storage[i].equip; + mapitem.equip[eqcount].identify = p->storage[i].identify; + mapitem.equip[eqcount].refine = p->storage[i].refine; + mapitem.equip[eqcount].attribute = p->storage[i].attribute; + mapitem.equip[eqcount].card[0] = p->storage[i].card[0]; + mapitem.equip[eqcount].card[1] = p->storage[i].card[1]; + mapitem.equip[eqcount].card[2] = p->storage[i].card[2]; + mapitem.equip[eqcount].card[3] = p->storage[i].card[3]; + mapitem.equip[eqcount].broken = p->storage[i].broken; + eqcount++; + } + else if(itemdb_isequip(p->storage[i].nameid)==0){ + mapitem.notequip[noteqcount].flag=0; + mapitem.notequip[noteqcount].id = p->storage[i].id; + mapitem.notequip[noteqcount].nameid=p->storage[i].nameid; + mapitem.notequip[noteqcount].amount = p->storage[i].amount; + mapitem.notequip[noteqcount].equip = p->storage[i].equip; + mapitem.notequip[noteqcount].identify = p->storage[i].identify; + mapitem.notequip[noteqcount].refine = p->storage[i].refine; + mapitem.notequip[noteqcount].attribute = p->storage[i].attribute; + mapitem.notequip[noteqcount].card[0] = p->storage[i].card[0]; + mapitem.notequip[noteqcount].card[1] = p->storage[i].card[1]; + mapitem.notequip[noteqcount].card[2] = p->storage[i].card[2]; + mapitem.notequip[noteqcount].card[3] = p->storage[i].card[3]; + mapitem.notequip[noteqcount].broken = p->storage[i].broken; + noteqcount++; + } + } + } + + memitemdata_to_sql(mapitem, eqcount, noteqcount, account_id,TABLE_STORAGE); + + //printf ("storage dump to DB - id: %d (total: %d)\n", account_id, j); + return 0; +} + +// DB -> storage data conversion +int storage_fromsql(int account_id, struct storage *p){ + int i=0; + + memset(p,0,sizeof(struct storage)); //clean up memory + p->storage_amount = 0; + p->account_id = account_id; + + // storage {`account_id`/`id`/`nameid`/`amount`/`equip`/`identify`/`refine`/`attribute`/`card0`/`card1`/`card2`/`card3`} + sprintf(tmp_sql,"SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`,`card0`,`card1`,`card2`,`card3`,`broken` FROM `%s` WHERE `account_id`='%d'",storage_db, account_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle) ; + + if (sql_res) { + while((sql_row = mysql_fetch_row(sql_res))) { //start to fetch + p->storage[i].id= atoi(sql_row[0]); + p->storage[i].nameid= atoi(sql_row[1]); + p->storage[i].amount= atoi(sql_row[2]); + p->storage[i].equip= atoi(sql_row[3]); + p->storage[i].identify= atoi(sql_row[4]); + p->storage[i].refine= atoi(sql_row[5]); + p->storage[i].attribute= atoi(sql_row[6]); + p->storage[i].card[0]= atoi(sql_row[7]); + p->storage[i].card[1]= atoi(sql_row[8]); + p->storage[i].card[2]= atoi(sql_row[9]); + p->storage[i].card[3]= atoi(sql_row[10]); + p->storage[i].broken = atoi(sql_row[11]); + p->storage_amount = ++i; + } + mysql_free_result(sql_res); + } + + printf ("storage load complete from DB - id: %d (total: %d)\n", account_id, p->storage_amount); + return 1; +} + +// Save guild_storage data to sql +int guild_storage_tosql(int guild_id, struct guild_storage *p){ + int i; + int eqcount=1; + int noteqcount=1; + struct itemtemp mapitem; + for(i=0;istorage[i].nameid>0){ + if(itemdb_isequip(p->storage[i].nameid)==1){ + mapitem.equip[eqcount].flag=0; + mapitem.equip[eqcount].id = p->storage[i].id; + mapitem.equip[eqcount].nameid=p->storage[i].nameid; + mapitem.equip[eqcount].amount = p->storage[i].amount; + mapitem.equip[eqcount].equip = p->storage[i].equip; + mapitem.equip[eqcount].identify = p->storage[i].identify; + mapitem.equip[eqcount].refine = p->storage[i].refine; + mapitem.equip[eqcount].attribute = p->storage[i].attribute; + mapitem.equip[eqcount].card[0] = p->storage[i].card[0]; + mapitem.equip[eqcount].card[1] = p->storage[i].card[1]; + mapitem.equip[eqcount].card[2] = p->storage[i].card[2]; + mapitem.equip[eqcount].card[3] = p->storage[i].card[3]; + mapitem.equip[eqcount].broken = p->storage[i].broken; + eqcount++; + } + else if(itemdb_isequip(p->storage[i].nameid)==0){ + mapitem.notequip[noteqcount].flag=0; + mapitem.notequip[noteqcount].id = p->storage[i].id; + mapitem.notequip[noteqcount].nameid=p->storage[i].nameid; + mapitem.notequip[noteqcount].amount = p->storage[i].amount; + mapitem.notequip[noteqcount].equip = p->storage[i].equip; + mapitem.notequip[noteqcount].identify = p->storage[i].identify; + mapitem.notequip[noteqcount].refine = p->storage[i].refine; + mapitem.notequip[noteqcount].attribute = p->storage[i].attribute; + mapitem.notequip[noteqcount].card[0] = p->storage[i].card[0]; + mapitem.notequip[noteqcount].card[1] = p->storage[i].card[1]; + mapitem.notequip[noteqcount].card[2] = p->storage[i].card[2]; + mapitem.notequip[noteqcount].card[3] = p->storage[i].card[3]; + mapitem.notequip[noteqcount].broken = p->storage[i].broken; + noteqcount++; + } + } + } + + memitemdata_to_sql(mapitem, eqcount, noteqcount, guild_id,TABLE_GUILD_STORAGE); + + printf ("guild storage save to DB - id: %d (total: %d)\n", guild_id,i); + return 0; +} + +// Load guild_storage data to mem +int guild_storage_fromsql(int guild_id, struct guild_storage *p){ + int i=0; + struct guild_storage *gs=guild_storage_pt; + p=gs; + + memset(p,0,sizeof(struct guild_storage)); //clean up memory + p->storage_amount = 0; + p->guild_id = guild_id; + + // storage {`guild_id`/`id`/`nameid`/`amount`/`equip`/`identify`/`refine`/`attribute`/`card0`/`card1`/`card2`/`card3`} + sprintf(tmp_sql,"SELECT `id`,`nameid`,`amount`,`equip`,`identify`,`refine`,`attribute`,`card0`,`card1`,`card2`,`card3`,`broken` FROM `%s` WHERE `guild_id`='%d'",guild_storage_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle) ; + + if (sql_res) { + while((sql_row = mysql_fetch_row(sql_res))) { //start to fetch + p->storage[i].id= atoi(sql_row[0]); + p->storage[i].nameid= atoi(sql_row[1]); + p->storage[i].amount= atoi(sql_row[2]); + p->storage[i].equip= atoi(sql_row[3]); + p->storage[i].identify= atoi(sql_row[4]); + p->storage[i].refine= atoi(sql_row[5]); + p->storage[i].attribute= atoi(sql_row[6]); + p->storage[i].card[0]= atoi(sql_row[7]); + p->storage[i].card[1]= atoi(sql_row[8]); + p->storage[i].card[2]= atoi(sql_row[9]); + p->storage[i].card[3]= atoi(sql_row[10]); + p->storage[i].broken = atoi(sql_row[11]); + p->storage_amount = ++i; + } + mysql_free_result(sql_res); + } + printf ("guild storage load complete from DB - id: %d (total: %d)\n", guild_id, p->storage_amount); + return 0; +} + +//--------------------------------------------------------- +// storage data initialize +int inter_storage_sql_init(){ + + //memory alloc + printf("interserver storage memory initialize....(%d byte)\n",sizeof(struct storage)); + storage_pt=calloc(sizeof(struct storage), 1); + guild_storage_pt=calloc(sizeof(struct guild_storage), 1); + memset(storage_pt,0,sizeof(struct storage)); + memset(guild_storage_pt,0,sizeof(struct guild_storage)); + + return 1; +} +// 倉庫データ削除 +int inter_storage_delete(int account_id) +{ + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id`='%d'",storage_db, account_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `storage`)- %s\n", mysql_error(&mysql_handle) ); + } + return 0; +} +int inter_guild_storage_delete(int guild_id) +{ + sprintf(tmp_sql, "DELETE FROM `%s` WHERE `guild_id`='%d'",guild_storage_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild_storage`)- %s\n", mysql_error(&mysql_handle) ); + } + return 0; +} + +//--------------------------------------------------------- +// packet from map server + +// recive packet about storage data +int mapif_load_storage(int fd,int account_id){ + //load from DB + storage_fromsql(account_id, storage_pt); + WFIFOW(fd,0)=0x3810; + WFIFOW(fd,2)=sizeof(struct storage)+8; + WFIFOL(fd,4)=account_id; + memcpy(WFIFOP(fd,8),storage_pt,sizeof(struct storage)); + WFIFOSET(fd,WFIFOW(fd,2)); + return 0; +} +// send ack to map server which is "storage data save ok." +int mapif_save_storage_ack(int fd,int account_id){ + WFIFOW(fd,0)=0x3811; + WFIFOL(fd,2)=account_id; + WFIFOB(fd,6)=0; + WFIFOSET(fd,7); + return 0; +} + +int mapif_load_guild_storage(int fd,int account_id,int guild_id) +{ + int guild_exist=0; + WFIFOW(fd,0)=0x3818; + + // Check if guild exists, I may write a function for this later, coz I use it several times. + //printf("- Check if guild %d exists\n",g->guild_id); + sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `guild_id`='%d'",guild_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild`)- %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + sql_row = mysql_fetch_row(sql_res); + guild_exist = atoi (sql_row[0]); + //printf("- Check if guild %d exists : %s\n",g->guild_id,((guild_exist==0)?"No":"Yes")); + } + mysql_free_result(sql_res) ; //resource free + + if(guild_exist==1) { + guild_storage_fromsql(guild_id,guild_storage_pt); + WFIFOW(fd,2)=sizeof(struct guild_storage)+12; + WFIFOL(fd,4)=account_id; + WFIFOL(fd,8)=guild_id; + memcpy(WFIFOP(fd,12),guild_storage_pt,sizeof(struct guild_storage)); + } + else { + WFIFOW(fd,2)=12; + WFIFOL(fd,4)=account_id; + WFIFOL(fd,8)=0; + } + WFIFOSET(fd,WFIFOW(fd,2)); + + return 0; +} +int mapif_save_guild_storage_ack(int fd,int account_id,int guild_id,int fail) +{ + WFIFOW(fd,0)=0x3819; + WFIFOL(fd,2)=account_id; + WFIFOL(fd,6)=guild_id; + WFIFOB(fd,10)=fail; + WFIFOSET(fd,11); + return 0; +} + +//--------------------------------------------------------- +// packet from map server + +// recive request about storage data +int mapif_parse_LoadStorage(int fd){ + mapif_load_storage(fd,RFIFOL(fd,2)); + return 0; +} +// storage data recive and save +int mapif_parse_SaveStorage(int fd){ + int account_id=RFIFOL(fd,4); + int len=RFIFOW(fd,2); + + if(sizeof(struct storage)!=len-8){ + printf("inter storage: data size error %d %d\n",sizeof(struct storage),len-8); + }else{ + memcpy(&storage_pt[0],RFIFOP(fd,8),sizeof(struct storage)); + storage_tosql(account_id, storage_pt); + mapif_save_storage_ack(fd,account_id); + } + return 0; +} + +int mapif_parse_LoadGuildStorage(int fd) +{ + mapif_load_guild_storage(fd,RFIFOL(fd,2),RFIFOL(fd,6)); + return 0; +} + +int mapif_parse_SaveGuildStorage(int fd) +{ + int guild_exist=0; + int guild_id=RFIFOL(fd,8); + int len=RFIFOW(fd,2); + if(sizeof(struct guild_storage)!=len-12){ + printf("inter storage: data size error %d %d\n",sizeof(struct guild_storage),len-12); + } + else { + // Check if guild exists, I may write a function for this later, coz I use it several times. + //printf("- Check if guild %d exists\n",g->guild_id); + sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `guild_id`='%d'",guild_db, guild_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `guild`)- %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res!=NULL && mysql_num_rows(sql_res)>0) { + sql_row = mysql_fetch_row(sql_res); + guild_exist = atoi (sql_row[0]); + //printf("- Check if guild %d exists : %s\n",g->guild_id,((guild_exist==0)?"No":"Yes")); + } + mysql_free_result(sql_res) ; //resource free + + if(guild_exist==1) { + memcpy(guild_storage_pt,RFIFOP(fd,12),sizeof(struct guild_storage)); + guild_storage_tosql(guild_id,guild_storage_pt); + mapif_save_guild_storage_ack(fd,RFIFOL(fd,4),guild_id,0); + } + else + mapif_save_guild_storage_ack(fd,RFIFOL(fd,4),guild_id,1); + } + return 0; +} + + +int inter_storage_parse_frommap(int fd){ + switch(RFIFOW(fd,0)){ + case 0x3010: mapif_parse_LoadStorage(fd); break; + case 0x3011: mapif_parse_SaveStorage(fd); break; + case 0x3018: mapif_parse_LoadGuildStorage(fd); break; + case 0x3019: mapif_parse_SaveGuildStorage(fd); break; + default: + return 0; + } + return 1; +} + diff --git a/misc/src/char_sql/int_storage.h b/misc/src/char_sql/int_storage.h new file mode 100644 index 0000000..f9f37db --- /dev/null +++ b/misc/src/char_sql/int_storage.h @@ -0,0 +1,13 @@ +#ifndef _INT_STORAGE_H_ +#define _INT_STORAGE_H_ + +int inter_storage_sql_init(); +int inter_storage_delete(int account_id); +int inter_guild_storage_delete(int guild_id); + +int inter_storage_parse_frommap(int fd); + + +//extern char storage_txt[256]; + +#endif diff --git a/misc/src/char_sql/inter.c b/misc/src/char_sql/inter.c new file mode 100644 index 0000000..c8fa9b4 --- /dev/null +++ b/misc/src/char_sql/inter.c @@ -0,0 +1,573 @@ +// +// original code from athena +// SQL conversion by Jioh L. Jung +// + +#include +#include + +#include "char.h" +#include "strlib.h" +#include "inter.h" +#include "int_party.h" +#include "int_guild.h" +#include "int_storage.h" +#include "int_pet.h" +#include "lock.h" + +#define WISDATA_TTL (60*1000) // Wisデータの生存時間(60秒) +#define WISDELLIST_MAX 256 // Wisデータ削除リストの要素数 + + +struct accreg { + int account_id,reg_num; + struct global_reg reg[ACCOUNT_REG_NUM]; +}; + +static struct accreg *accreg_pt; + + +int party_share_level = 10; +MYSQL mysql_handle; +MYSQL_RES* sql_res ; +MYSQL_ROW sql_row ; +int sql_fields, sql_cnt; +char tmp_sql[65535]; + +MYSQL lmysql_handle; +char tmp_lsql[65535]; +MYSQL_RES* lsql_res ; +MYSQL_ROW lsql_row ; + +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"; + +int login_server_port = 3306; +char login_server_ip[32] = "127.0.0.1"; +char login_server_id[32] = "ragnarok"; +char login_server_pw[32] = "ragnarok"; +char login_server_db[32] = "ragnarok"; + +// sending packet list +int inter_send_packet_length[]={ + -1,-1,27, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + -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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; +// recv. packet list +int inter_recv_packet_length[]={ + -1,-1, 7, 0, -1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 6,-1, 0, 0, 0, 0, 0, 0, 10,-1, 0, 0, 0, 0, 0, 0, + 72, 6,52,14, 10,29, 6,-1, 34, 0, 0, 0, 0, 0, 0, 0, + -1, 6,-1, 0, 55,19, 6,-1, 14,-1,-1,-1, 14,19,186,-1, + 5, 9, 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, 0, + 48,14,-1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; + +struct WisData { + int id,fd,count,len; + unsigned long tick; + unsigned char src[24],dst[24],msg[512]; +}; +static struct dbt * wis_db = NULL; +static int wis_dellist[WISDELLIST_MAX], wis_delnum; + +//-------------------------------------------------------- +// Save account_reg to sql (type=2) +int inter_accreg_tosql(int account_id,struct accreg *reg){ + + int j; + char temp_str[32]; + if (account_id<=0) return 0; + reg->account_id=account_id; + + //`global_reg_value` (`type`, `account_id`, `char_id`, `str`, `value`) + sprintf(tmp_sql,"DELETE FROM `%s` WHERE `type`=2 AND `account_id`='%d'",reg_db, account_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (delete `global_reg_value`)- %s\n", mysql_error(&mysql_handle) ); + } + + if (reg->reg_num<=0) return 0; + + for(j=0;jreg_num;j++){ + if(reg->reg[j].str != NULL){ + sprintf(tmp_sql,"INSERT INTO `%s` (`type`, `account_id`, `str`, `value`) VALUES (2,'%d', '%s','%d')", + reg_db, reg->account_id, jstrescapecpy(temp_str,reg->reg[j].str), reg->reg[j].value); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (insert `global_reg_value`)- %s\n", mysql_error(&mysql_handle) ); + } + } + } + return 0; +} + +// Load account_reg from sql (type=2) +int inter_accreg_fromsql(int account_id,struct accreg *reg) +{ + int j=0; + if (reg==NULL) return 0; + memset(reg, 0, sizeof(struct accreg)); + reg->account_id=account_id; + + //`global_reg_value` (`type`, `account_id`, `char_id`, `str`, `value`) + sprintf (tmp_sql, "SELECT `str`, `value` FROM `%s` WHERE `type`=2 AND `account_id`='%d'",reg_db, reg->account_id); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (select `global_reg_value`)- %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle); + + if (sql_res) { + for(j=0;(sql_row = mysql_fetch_row(sql_res));j++){ + memcpy(reg->reg[j].str, sql_row[0],32); + reg->reg[j].value = atoi(sql_row[1]); + } + mysql_free_result(sql_res); + } + reg->reg_num=j; + return 0; +} + +// Initialize +int inter_accreg_sql_init() +{ + CREATE(accreg_pt, struct accreg, 1); + return 0; + +} + +/*========================================== + * read config file + *------------------------------------------ + */ +int inter_config_read(const char *cfgName) { + printf ("start reading interserver configuration: %s\n",cfgName); + int i; + char line[1024], w1[1024], w2[1024]; + FILE *fp; + + fp=fopen(cfgName,"r"); + if(fp==NULL){ + printf("file not found: %s\n", cfgName); + return 1; + } + while(fgets(line, 1020, fp)){ + i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2); + if(i!=2) + continue; + + if(strcmpi(w1,"char_server_ip")==0){ + strcpy(char_server_ip, w2); + printf ("set char_server_ip : %s\n",w2); + } + else if(strcmpi(w1,"char_server_port")==0){ + char_server_port=atoi(w2); + printf ("set char_server_port : %s\n",w2); + } + else if(strcmpi(w1,"char_server_id")==0){ + strcpy(char_server_id, w2); + printf ("set char_server_id : %s\n",w2); + } + else if(strcmpi(w1,"char_server_pw")==0){ + strcpy(char_server_pw, w2); + printf ("set char_server_pw : %s\n",w2); + } + else if(strcmpi(w1,"char_server_db")==0){ + strcpy(char_server_db, w2); + printf ("set char_server_db : %s\n",w2); + } + //Logins information to be read from the inter_athena.conf + //for character deletion (checks email in the loginDB) + + else if(strcmpi(w1,"login_server_ip")==0){ + strcpy(login_server_ip, w2); + printf ("set login_server_ip : %s\n",w2); + } + else if(strcmpi(w1,"login_server_port")==0){ + login_server_port=atoi(w2); + printf ("set login_server_port : %s\n",w2); + } + else if(strcmpi(w1,"login_server_id")==0){ + strcpy(login_server_id, w2); + printf ("set login_server_id : %s\n",w2); + } + else if(strcmpi(w1,"login_server_pw")==0){ + strcpy(login_server_pw, w2); + printf ("set login_server_pw : %s\n",w2); + } + else if(strcmpi(w1,"login_server_db")==0){ + strcpy(login_server_db, w2); + printf ("set login_server_db : %s\n",w2); + } + else if(strcmpi(w1,"party_share_level")==0){ + party_share_level=atoi(w2); + if(party_share_level < 0) party_share_level = 0; + }else if(strcmpi(w1,"import")==0){ + inter_config_read(w2); + } + else if(strcmpi(w1,"login_server_db")==0){ + strcpy(login_server_db, w2); + printf ("set login_server_db : %s\n",w2); + } + } + fclose(fp); + + printf ("success reading interserver configuration\n"); + + return 0; +} + +// Save interlog into sql +int inter_log(char *fmt,...) +{ + char str[255]; + char temp_str[255]; + va_list ap; + va_start(ap,fmt); + + vsprintf(str,fmt,ap); + sprintf(tmp_sql,"INSERT INTO `%s` (`time`, `log`) VALUES (NOW(), '%s')",interlog_db, jstrescapecpy(temp_str,str)); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error (insert `interlog`)- %s\n", mysql_error(&mysql_handle) ); + } + + va_end(ap); + return 0; +} + + +// initialize +int inter_init(const char *file) +{ + //int i; + + printf ("interserver initialize...\n"); + inter_config_read(file); + + //DB connection initialized + mysql_init(&mysql_handle); + printf("Connect Character DB server.... (Character Server)\n"); + if(!mysql_real_connect(&mysql_handle, char_server_ip, char_server_id, char_server_pw, + char_server_db ,char_server_port, (char *)NULL, 0)) { + //pointer check + printf("%s\n",mysql_error(&mysql_handle)); + exit(1); + } + else { + printf ("Connect Success! (Character Server)\n"); + } + + mysql_init(&lmysql_handle); + printf("Connect Character DB server.... (login server)\n"); + if(!mysql_real_connect(&lmysql_handle, login_server_ip, login_server_id, login_server_pw, + login_server_db ,login_server_port, (char *)NULL, 0)) { + //pointer check + printf("%s\n",mysql_error(&lmysql_handle)); + exit(1); + }else { + printf ("Connect Success! (Login Server)"); + } + wis_db = numdb_init(); + inter_guild_sql_init(); + inter_storage_sql_init(); + inter_party_sql_init(); + + inter_pet_sql_init(); + inter_accreg_sql_init(); + + //printf ("interserver timer initializing : %d sec...\n",autosave_interval); + //i=add_timer_interval(gettick()+autosave_interval,inter_save_timer,0,0,autosave_interval); + + return 0; +} + +int inter_mapif_init(int fd) { + inter_guild_mapif_init(fd); + + return 0; +} + + +//-------------------------------------------------------- + +// GM message sending +int mapif_GMmessage(unsigned char *mes, int len) { + unsigned char buf[len]; + + WBUFW(buf, 0) = 0x3800; + WBUFW(buf, 2) = len; + memcpy(WBUFP(buf, 4), mes, len-4); + mapif_sendall(buf, len); + printf("\033[1;34m inter server: GM[len:%d] - '%s' \033[0m\n", len, mes); + return 0; +} + +// Wis sending +int mapif_wis_message(struct WisData *wd) { + unsigned char buf[56 + wd->len]; + + WBUFW(buf, 0) = 0x3801; + WBUFW(buf, 2) = 56 +wd->len; + WBUFL(buf, 4) = wd->id; + memcpy(WBUFP(buf, 8), wd->src, 24); + memcpy(WBUFP(buf,32), wd->dst, 24); + memcpy(WBUFP(buf,56), wd->msg, wd->len); + wd->count = mapif_sendall(buf,WBUFW(buf,2)); + + return 0; +} +// 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; + mapif_send(wd->fd,buf,27); +// printf("inter server wis_end %d\n",flag); + return 0; +} + +int mapif_account_reg(int fd,unsigned char *src) +{ + unsigned char buf[WBUFW(src,2)]; + memcpy(WBUFP(buf,0),src,WBUFW(src,2)); + WBUFW(buf, 0)=0x3804; + mapif_sendallwos(fd,buf,WBUFW(buf,2)); + return 0; +} + +// Send the requested account_reg +int mapif_account_reg_reply(int fd,int account_id) +{ + struct accreg *reg=accreg_pt; + inter_accreg_fromsql(account_id,reg); + + WFIFOW(fd,0)=0x3804; + WFIFOL(fd,4)=account_id; + if(reg->reg_num==0){ + WFIFOW(fd,2)=8; + }else{ + int j,p; + for(j=0,p=8;jreg_num;j++,p+=36){ + memcpy(WFIFOP(fd,p),reg->reg[j].str,32); + WFIFOL(fd,p+32)=reg->reg[j].value; + } + WFIFOW(fd,2)=p; + } + WFIFOSET(fd,WFIFOW(fd,2)); + return 0; +} + +//-------------------------------------------------------- + +// Existence check of WISP data +int check_ttl_wisdata_sub(void *key, void *data, va_list ap) { + unsigned long tick; + struct WisData *wd = (struct WisData *)data; + tick = va_arg(ap, unsigned long); + + if (DIFF_TICK(tick, wd->tick) > WISDATA_TTL && wis_delnum < WISDELLIST_MAX) + wis_dellist[wis_delnum++] = wd->id; + + return 0; +} + +int check_ttl_wisdata() { + unsigned long tick = gettick(); + int i; + + do { + wis_delnum = 0; + numdb_foreach(wis_db, check_ttl_wisdata_sub, tick); + for(i = 0; i < wis_delnum; i++) { + struct WisData *wd = numdb_search(wis_db, wis_dellist[i]); + printf("inter: wis data id=%d time out : from %s to %s\n", wd->id, wd->src, wd->dst); + // removed. not send information after a timeout. Just no answer for the player + //mapif_wis_end(wd, 1); // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target + numdb_erase(wis_db, wd->id); + free(wd); + } + } while(wis_delnum >= WISDELLIST_MAX); + + return 0; +} + +//-------------------------------------------------------- + +// GM message sending +int mapif_parse_GMmessage(int fd) +{ + mapif_GMmessage(RFIFOP(fd, 4), RFIFOW(fd, 2)); + return 0; +} + + +// Wisp/page request to send +int mapif_parse_WisRequest(int fd) { + struct WisData* wd; + static int wisid = 0; + + if (RFIFOW(fd,2)-52 >= sizeof(wd->msg)) { + printf("inter: Wis message size too long.\n"); + return 0; + } else if (RFIFOW(fd,2)-52 <= 0) { // normaly, impossible, but who knows... + printf("inter: Wis message doesn't exist.\n"); + return 0; + } + sprintf (tmp_sql, "SELECT `name` FROM `%s` WHERE `char_id`='%d'",char_db, (int) RFIFOP(fd,28)); + if(mysql_query(&mysql_handle, tmp_sql) ) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle) ); + } + sql_res = mysql_store_result(&mysql_handle); + + // search if character exists before to ask all map-servers + if (!(sql_row = mysql_fetch_row(sql_res))) { + unsigned char buf[27]; + WBUFW(buf, 0) = 0x3802; + memcpy(WBUFP(buf, 2), RFIFOP(fd, 4), 24); + 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); + // Character exists. So, ask all map-servers + } else { + // to be sure of the correct name, rewrite it + memset(RFIFOP(fd,28), 0, 24); + strncpy(RFIFOP(fd,28), sql_row[0], 24); + // if source is destination, don't ask other servers. + if (strcmp(RFIFOP(fd,4),RFIFOP(fd,28)) == 0) { + unsigned char buf[27]; + WBUFW(buf, 0) = 0x3802; + memcpy(WBUFP(buf, 2), RFIFOP(fd, 4), 24); + 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), 24); + memcpy(wd->dst, RFIFOP(fd,28), 24); + memcpy(wd->msg, RFIFOP(fd,52), wd->len); + wd->tick = gettick(); + numdb_insert(wis_db, wd->id, wd); + mapif_wis_message(wd); + } + } + + return 0; +} + + +// Wisp/page transmission result +int mapif_parse_WisReply(int fd) { + int id = RFIFOL(fd,2), flag = RFIFOB(fd,6); + struct WisData *wd = numdb_search(wis_db, id); + + if (wd == NULL) + 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 + numdb_erase(wis_db, id); + free(wd); + } + + return 0; +} + + +// Save account_reg into sql (type=2) +int mapif_parse_AccReg(int fd) +{ + int j,p; + struct accreg *reg=accreg_pt; + int account_id = RFIFOL(fd,4); + memset(accreg_pt,0,sizeof(struct accreg)); + + for(j=0,p=8;jreg[j].str,RFIFOP(fd,p),32); + reg->reg[j].value=RFIFOL(fd,p+32); + } + reg->reg_num=j; + + inter_accreg_tosql(account_id,reg); + mapif_account_reg(fd,RFIFOP(fd,0)); // Send confirm message to map + return 0; +} + +// Request the value of account_reg +int mapif_parse_AccRegRequest(int fd) +{ +// printf("mapif: accreg request\n"); + return mapif_account_reg_reply(fd,RFIFOL(fd,2)); +} + + + +//-------------------------------------------------------- +int inter_parse_frommap(int fd) +{ + int cmd=RFIFOW(fd,0); + int len=0; + + // inter鯖管轄かを調べる + if(cmd<0x3000 || cmd>=0x3000+( sizeof(inter_recv_packet_length)/ + sizeof(inter_recv_packet_length[0]) ) ) + return 0; + + // パケット長を調べる + if( (len=inter_check_length(fd,inter_recv_packet_length[cmd-0x3000]))==0 ) + return 2; + + switch(cmd){ + case 0x3000: mapif_parse_GMmessage(fd); break; + case 0x3001: mapif_parse_WisRequest(fd); break; + case 0x3002: mapif_parse_WisReply(fd); break; + case 0x3004: mapif_parse_AccReg(fd); break; + case 0x3005: mapif_parse_AccRegRequest(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; + return 0; + } + RFIFOSKIP(fd, len ); + return 1; +} + +// RFIFO check +int inter_check_length(int fd, int length) +{ + if(length==-1){ // v-len packet + if(RFIFOREST(fd)<4) // packet not yet + return 0; + length = RFIFOW(fd, 2); + } + + if(RFIFOREST(fd) + +extern MYSQL mysql_handle; +extern char tmp_sql[65535]; +extern MYSQL_RES* sql_res ; +extern MYSQL_ROW sql_row ; +extern int sql_cnt; + +extern MYSQL lmysql_handle; +extern char tmp_lsql[65535]; +extern MYSQL_RES* lsql_res ; +extern MYSQL_ROW lsql_row ; + +extern int char_server_port; +extern char char_server_ip[32]; +extern char char_server_id[32]; +extern char char_server_pw[32]; +extern char char_server_db[32]; + +extern int login_db_server_port; +extern char login_db_server_ip[32]; +extern char login_db_server_id[32]; +extern char login_db_server_pw[32]; +extern char login_db_server_db[32]; + +#endif diff --git a/misc/src/char_sql/itemdb.c b/misc/src/char_sql/itemdb.c new file mode 100644 index 0000000..0bed07c --- /dev/null +++ b/misc/src/char_sql/itemdb.c @@ -0,0 +1,247 @@ +// $Id: itemdb.c,v 1.1.1.1 2004/09/10 17:44:48 MagicalTux Exp $ +#include +#include +#include + +#include "itemdb.h" +#include "db.h" +#include "inter.h" +#include "char.h" +#include "utils.h" + +#ifdef MEMWATCH +#include "memwatch.h" +#endif + +#define MAX_RANDITEM 2000 + +// ** ITEMDB_OVERRIDE_NAME_VERBOSE ** +// 定義すると、itemdb.txtとgrfで名前が異なる場合、表示します. +//#define ITEMDB_OVERRIDE_NAME_VERBOSE 1 + +char item_db_db[256]="item_db"; // added to specify item_db sql table [Valaris] + +static struct dbt* item_db; + +/*========================================== + * DBの検索 + *------------------------------------------ + */ +struct item_data* itemdb_search(int nameid) +{ + struct item_data *id; + + id=numdb_search(item_db,nameid); + if(id) return id; + + CREATE(id, struct item_data, 1); + + numdb_insert(item_db,nameid,id); + + + if(nameid>500 && nameid<600) + id->type=0; //heal item + else if(nameid>600 && nameid<700) + id->type=2; //use item + else if((nameid>700 && nameid<1100) || + (nameid>7000 && nameid<8000)) + id->type=3; //correction + else if(nameid>=1750 && nameid<1771) + id->type=10; //arrow + else if(nameid>1100 && nameid<2000) + id->type=4; //weapon + else if((nameid>2100 && nameid<3000) || + (nameid>5000 && nameid<6000)) + id->type=5; //armor + else if(nameid>4000 && nameid<5000) + id->type=6; //card + else if(nameid>9000 && nameid<10000) + id->type=7; //egg + else if(nameid>10000) + id->type=8; //petequip + + return id; +} + +/*========================================== + * + *------------------------------------------ + */ +int itemdb_isequip(int nameid) +{ + int type=itemdb_type(nameid); + if(type==0 || type==2 || type==3 || type==6 || type==10) + return 0; + return 1; +} +/*========================================== + * + *------------------------------------------ + */ +int itemdb_isequip2(struct item_data *data) +{ + if(data) { + int type=data->type; + if(type==0 || type==2 || type==3 || type==6 || type==10) + return 0; + else + return 1; + } + return 0; +} + + + +/*========================================== + * アイテムデータベースの読み込み + *------------------------------------------ + */ +static int itemdb_readdb(void) +{ + FILE *fp; + char line[1024]; + int ln=0; + int nameid,j; + char *str[32],*p,*np; + struct item_data *id; + + fp=fopen("db/item_db.txt","r"); + if(fp==NULL){ + printf("can't read db/item_db.txt\n"); + exit(1); + } + while(fgets(line,1020,fp)){ + if(line[0]=='/' && line[1]=='/') + continue; + memset(str,0,sizeof(str)); + for(j=0,np=p=line;j<17 && p;j++){ + str[j]=p; + p=strchr(p,','); + if(p){ *p++=0; np=p; } + } + if(str[0]==NULL) + continue; + + nameid=atoi(str[0]); + if(nameid<=0 || nameid>=20000) + continue; + ln++; + + //ID,Name,Jname,Type,Price,Sell,Weight,ATK,DEF,Range,Slot,Job,Gender,Loc,wLV,eLV,View + id=itemdb_search(nameid); + memcpy(id->name,str[1],24); + memcpy(id->jname,str[2],24); + id->type=atoi(str[3]); + + } + fclose(fp); + printf("read db/item_db.txt done (count=%d)\n",ln); + return 0; +} + +static int itemdb_read_sqldb(void) // sql item_db read, shortened version of map-server item_db read [Valaris] +{ + unsigned int nameid; // Type should be "unsigned short int", but currently isn't for compatibility with numdb_insert() + struct item_data *id; + + // ---------- + + // Output query to retrieve all rows from the item database table + sprintf(tmp_sql, "SELECT * FROM `%s`", item_db_db); + + // Execute the query; if the query execution fails, output an error + if (mysql_query(&mysql_handle, tmp_sql)) { + printf("Database server error (executing query for %s): %s\n", item_db_db, mysql_error(&mysql_handle)); + } + + // Store the query result + sql_res = mysql_store_result(&mysql_handle); + + // If the storage of the query result succeeded + if (sql_res) { + // Parse each row in the query result into sql_row + while ((sql_row = mysql_fetch_row(sql_res))) { + nameid = atoi(sql_row[0]); + + // If the identifier is not within the valid range, process the next row + if (nameid == 0 || nameid >= 20000) { // Should ">= 20000" be "> 20000"? + continue; + } + + // ---------- + + // Insert a new row into the item database +/* + id = calloc(sizeof(struct item_data), 1); + + if (id == NULL) { + printf("out of memory : itemdb_read_sqldb\n"); + exit(1); + } + + memset(id, 0, sizeof(struct item_data)); + numdb_insert(item_db, nameid, id); + + // ---------- +*/ + id=itemdb_search(nameid); + + memcpy(id->name, sql_row[1], 24); + memcpy(id->jname, sql_row[2], 24); + + id->type = atoi(sql_row[3]); + } + + // If the retrieval failed, output an error + if (mysql_errno(&mysql_handle)) { + printf("Database server error (retrieving rows from %s): %s\n", item_db_db, mysql_error(&mysql_handle)); + } + + printf("read %s done (count = %lu)\n", item_db_db, (unsigned long) mysql_num_rows(sql_res)); + + // Free the query result + mysql_free_result(sql_res); + } else { + printf("MySQL error (storing query result for %s): %s\n", item_db_db, mysql_error(&mysql_handle)); + } + + return 0; +} + +static int itemdb_final(void *key,void *data,va_list ap) +{ + struct item_data *id; + + id=data; + if(id->use_script) + free(id->use_script); + if(id->equip_script) + free(id->equip_script); + free(id); + + return 0; +} + + +/*========================================== + * + *------------------------------------------ + */ +void do_final_itemdb(void) +{ + if(item_db){ + numdb_final(item_db,itemdb_final); + item_db=NULL; + } +} +int do_init_itemdb(void) +{ + item_db = numdb_init(); + + if (db_use_sqldbs) // it db_use_sqldbs in inter config are yes, will read from item_db for char server display [Valaris] + itemdb_read_sqldb(); + else + itemdb_readdb(); + return 0; +} + diff --git a/misc/src/char_sql/itemdb.h b/misc/src/char_sql/itemdb.h new file mode 100644 index 0000000..dea835e --- /dev/null +++ b/misc/src/char_sql/itemdb.h @@ -0,0 +1,34 @@ +#ifndef _ITEMDB_H_ +#define _ITEMDB_H_ + +struct item_data { + int nameid; + char name[24],jname[24]; + int value_buy,value_sell,value_notdc,value_notoc; + int type; + int class; + int sex; + int equip; + int weight; + int atk; + int def; + int range; + int slot; + int look; + int elv; + int wlv; + char *use_script; // 回復とかも全部この中でやろうかなと + char *equip_script; // 攻撃,防御の属性設定もこの中で可能かな? + char available; +}; + +struct item_data* itemdb_search(int nameid); +#define itemdb_type(n) itemdb_search(n)->type + +int itemdb_isequip(int); +int itemdb_isequip2(struct item_data *); + +void do_final_itemdb(void); +int do_init_itemdb(void); + +#endif diff --git a/misc/src/char_sql/make.sh b/misc/src/char_sql/make.sh new file mode 100644 index 0000000..a4ca8b5 --- /dev/null +++ b/misc/src/char_sql/make.sh @@ -0,0 +1,11 @@ +#!/bin/sh + rsqlt=`rm -rf *.o` + gcc -c char.c -I/usr/local/include/mysql/ + gcc -c int_guild.c -I/usr/local/include/mysql/ + gcc -c int_party.c -I/usr/local/include/mysql/ + gcc -c int_pet.c -I/usr/local/include/mysql/ + gcc -c int_storage.c -I/usr/local/include/mysql/ + gcc -c inter.c -I/usr/local/include/mysql/ + gcc -c strlib.c + gcc -c itemdb.c -I../common/ + gcc -o ../char-server inter.o char.o int_pet.o int_storage.o int_guild.o int_party.o strlib.o itemdb.o ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o -L/usr/local/lib/mysql -lmysqlclient -lz diff --git a/misc/src/char_sql/readme.txt b/misc/src/char_sql/readme.txt new file mode 100644 index 0000000..41b1144 --- /dev/null +++ b/misc/src/char_sql/readme.txt @@ -0,0 +1,250 @@ +サソ//Encoded with UTF-8 (UNICODE) +//--------------------------------------------- +// V.018 - Aarlex +1. ADD Makefile & GNUmakefile +2. fix guild_leave. +3. fix char select windows HP & SP value error. + +// V.017 - Aarlex +1. fix guild member view job update.(For mod-0728) + inter.c + int_guild.c + +// V.016 - by Aarlex +1. Add e-mail check when you Delete char data. +2. modify storage save func like 014. +2. remove Lan_support func. + +// v.014 - by Aarlex +I rewrite save function. +besause myfriend find that Mysql will use more than 40% CPU. +And log file is too big (4 days 22G ..= =+) +(maybe he sets autosave_time less then 1 min.) +but. i still rewrite save func. +char server will delete all of user item(inventory & Cart) data then insert them again before. +so i use two struct to save item data from map & database. +then compare two struct to get different . +AND add some debug message.but message maybe too much XD. + + +1. ADDED itemdb.c itemdb.h +2. modify mmo_char_tosql(). +3. ADDED memitemdata_to_sql(). +4. ADDED some debug message in memitemdata_to_sql(). +5. modify make.sh + +// v.013 - by Aarlex +1. some SQL sentance fix in old version Mysql . + +2. in_guild.c mapif_guild_leaved() + unsigned char buf[64] -> unsigned char buf[128] + +3. in_pet.c inter_pet_tosql() + if (sql_res) - > if (mysql_num_rows(sql_res)!=0) + +4. in_char.c mmo_char_send006b() + + WFIFOW(fd, offset+(i*106)+42) = char_dat[0].hp -> WFIFOW(fd,offset+(i*106)+42) = (char_dat[j].hp > 0x7fff)? 0x7fff:char_dat[j].hp; + WFIFOW(fd, offset+(i*106)+44) = char_dat[0].max_hp -> WFIFOW(fd,offset+(i*106)+44) = (char_dat[j].max_hp > 0x7fff)? 0x7fff:char_dat[j].max_hp; + WFIFOW(fd, offset+(i*106)+46) = char_dat[0].sp -> WFIFOW(fd,offset+(i*106)+46) = (char_dat[j].sp > 0x7fff)? 0x7fff:char_dat[j].sp + WFIFOW(fd, offset+(i*106)+48) = char_dat[0].max_sp -> WFIFOW(fd,offset+(i*106)+48) = (char_dat[j].max_sp > 0x7fff)? 0x7fff:char_dat[j].max_sp; + + in_char.c parse_char() + + WFIFOW(fd, offset+(i*106)+42) = char_dat[0].hp -> WFIFOW(fd,offset+(i*106)+42) = (char_dat[j].hp > 0x7fff)? 0x7fff:char_dat[j].hp; + WFIFOW(fd, offset+(i*106)+44) = char_dat[0].max_hp -> WFIFOW(fd,offset+(i*106)+44) = (char_dat[j].max_hp > 0x7fff)? 0x7fff:char_dat[j].max_hp; + WFIFOW(fd, offset+(i*106)+46) = char_dat[0].sp -> WFIFOW(fd,offset+(i*106)+46) = (char_dat[j].sp > 0x7fff)? 0x7fff:char_dat[j].sp + WFIFOW(fd, offset+(i*106)+48) = char_dat[0].max_sp -> WFIFOW(fd,offset+(i*106)+48) = (char_dat[j].max_sp > 0x7fff)? 0x7fff:char_dat[j].max_sp; + +// v.012 - by Jazz +1. 0627 official version縺ォ蜷医o縺帙※菫ョ豁」縺励∪縺励◆. +2. no more binary files are supplied. + +//--------------------------------------------- +// v.011 - by Mark +1. Fixed a couple bugs which would cause segfaults under linux :) + +//--------------------------------------------- +// v.010 - by Jazz +1. added some debug info - for reporting. + +//--------------------------------------------- +// v.009 - by Jazz +1. code added for debug. +2. some SQL sentance fix. + +//--------------------------------------------- +// v.009 - by Jazz +1. fix crash bug. - pet db. + +//--------------------------------------------- +// v.008 - by Jazz +1. DB table fix. - char.fix-from.007.to.008.sql + +譌「蟄倥ョ table 讒矩縺ォ縺ッ遏「縺瑚」逹隗」髯、縺輔l繧句エ蜷医′縺ゅj縺セ縺. + +item.equip縺ッ 'unsigned short' 蠖「蠑上〒縺. +縺薙ョ縺溘a縺ォ SQL table繧剃ソョ豁」縺励↑縺代l縺ー縺ェ繧翫∪縺帙s. + +菫ョ豁」繧ウ繝シ繝峨ッ char.fix-from.007.to.008.sql 縺ァ縺. +MySQL縺ァ荳蠎ヲ陦後▲縺ヲ縺上l繧後ー驕ゥ逕ィ縺輔l縺セ縺. 譌「蟄倥ョ繝繝シ繧ソ縺ッ螳牙ィ縺ァ縺. + +//--------------------------------------------- +// v.007 - by Jazz +1. domain 隗」驥医↓蟇セ縺吶k蝠城。後r菫ョ豁」縺励∪縺励◆. + +//--------------------------------------------- +// v.006 - by Jazz +1. crash bug fix. - when your pet DB is empty + +//--------------------------------------------- +// v.005 - by Jazz +1. 0590 official version縺ォ蜷医o縺帙※菫ョ豁」縺励∪縺励◆. + +//--------------------------------------------- +// v.004 - by Jazz +1. 0586 official version縺ォ蜷医o縺帙※菫ョ豁」縺励∪縺励◆. + +//--------------------------------------------- +// v.003 - by Jazz +1. official縺ョ guild.c 縺ィ party.c 繝輔ぃ繧、繝ォ縺ァ縺セ縺溷堺ソョ豁」縺励∪縺励◆. + +//--------------------------------------------- +// v.002 - by Jazz +1. aphostropy 蝠城。後r隗」豎コ縺励∪縺励◆. 縺薙l縺ッ菫晏ョ峨→騾」髢「縺後≠繧句撫鬘後〒縺. +2. SQL縺ョ讒矩繧剃ソョ豁」縺励∪縺励◆. + +//--------------------------------------------- +// v.001 - by Jazz +1. 荳逡ェ逶ョ螳牙ョ version縺ァ縺. alpha version縺ォ蝠城。檎せ繧貞、壽焚菫ョ豁」縺励∪縺励◆. + +//------------------------------------------------------------------------ +//For JAPANESE USER +Athena Char-Server for MySQL. 005 + +荳蠢 guild縺ィ party縺ッ繧医¥蜻シ縺ウ蜃コ縺励&繧後k驛ィ蛻縺ェ縺ョ縺ァ譌「蟄倥ョ athena char-server縺御スソ縺」縺ヲ縺繧 file 蝓コ逶、縺ョ讒矩繧呈戟縺」縺ヲ陦後″縺セ縺. +縺薙l縺ッ, 荳蠎ヲ縺ォ繝。繝「繝ェ繝シ縺ァ逧隱ュ繧薙〒, 繧ゅ≧荳蠎ヲ縺ォ逧 file縺ァ菴ソ縺譁ケ縺碁Κ荳九′蟆代↑縺上°縺九k縺ィ諤昴>縺セ縺. +縺昴@縺ヲ char 繝繝シ繧ソ縺ォ豈斐∋縺ヲ, lost縺ォ縺ェ縺」縺ヲ繧ょ撫鬘後′繧ゅ▲縺ィ蟆代↑縺縺ィ蛻、譁ュ縺励※縺昴≧縺励∪縺励◆. + +MySQL繝舌シ繧ク繝ァ繝ウ縺ョ compile縺ッ MySQL Clients Library縺悟ソ隕√〒縺. windows(cygwin) 繝舌シ繧ク繝ァ繝ウ縺ォ繧ウ繝ウ繝代う繝ォ縺輔l縺 binary繧呈キサ莉倥@縺セ縺励◆. + +險ュ鄂ョ: +縺セ縺 text->DB縺ョ converter縺ッ縺セ縺ィ繧ゅ↓謾ッ謠エ縺励↑縺縺ァ縺. 蜀驛ィ逧縺ォ縺。繧縺」縺ィ繝舌げ縺檎匱隕九&繧後※繝舌げ繧剃ソョ豁」荳ュ縺ァ縺. +縺ァ縺阪k縺縺第掠縺 upload繧偵@縺セ縺. + +1. char.sql繧 MySQL縺ォ dump縺励∪縺. + +2. inter_athena.conf縺ォ谺。繧定ソス蜉縺励∪縺. 縺昴@縺ヲ閾ェ蛻縺ョ DB繧オ繝シ繝舌シ縺ョ諠蝣ア縺ォ蜷医o縺帙※縺上l縺セ縺. +縺薙%縺ァ windows(cygwin)縺ョ蝣エ蜷医↓縺ッ localhost繧剃スソ縺」縺ヲ縺ッ縺縺代↑縺縺ァ縺. ip縺ァ譖ク縺縺ヲ縺上l縺ェ縺代l縺ー縺ェ繧翫∪縺帙s. +localhost縺ァ菴ソ縺蝣エ蜷 UNIX domain socket縺御ス懷虚縺吶k縺九i騾」邨舌′荳榊庄閭ス縺ォ縺ェ繧翫∪縺. + +//3306 is default +db_server_port: 3306 +//DB ip +db_server_ip: 127.0.0.1 +//DB id +db_server_id: ragnarok +//DB pass +db_server_pw: ragnarok +//DB name +db_server_logindb: ragnarok + +3. MySQL 繝舌シ繧ク繝ァ繝ウ縺ッ 2蛟九ョ MySQL connect session繧呈戟縺。縺セ縺. +荳縺、縺ッ繧ュ繝」繝ゥ繧ッ繧ソ繝シ繝繝シ繧ソ繧定ェュ繧薙〒譖ク縺上ョ縺ォ菴ソ繧上l縺ヲ, 莠檎分逶ョ縺ッ inter server縺ョ縺溘a縺ォ騾」邨舌@縺セ縺. + +髢狗匱縺ィ繝繧ケ繝育腸蠅縺ッ P4 2.4a/1024MB/WinXP pro(MediaCenter Edition 2K4 KR)/Cygwin GCC 縺ァ縺. +譛ャ莠コ縺碁沒蝗ス莠コ縺縺九i髻灘嵜隱樣幕逋コ閠縺ョ騾」邨。繧よュ楢ソ弱@縺セ縺. +髻灘嵜隱槭→譌・譛ャ隱槭ョ荳頑焔縺ェ譁ケ縺ッ騾」邨。繧偵¥縺縺輔>. 譌・譛ャ隱槭ョ菴ソ逕ィ縺ォ髮」縺励&繧呈─縺倥※縺縺セ縺. + +迴セ蝨ィ DarkWeiss縺 login server縺ォ MySQL繧呈髪謠エ縺怜ァ九a縺セ縺励◆. +縺励°縺 athena縺ョ縺昴l縺ッ DarkWeiss 繧医j繧ゅ▲縺ィ繧医¥菴懷虚縺吶k縺ィ諤昴>縺セ縺. + +contact : jazz@creper.com + +//------------------------------------------------------------------------ +//For KOREAN USER +Athena Char-Server for MySQL. 005 + +攵卿 guild凰 party株 梵」シ 从カ罹据株 カカ擽攵 クー。エ攪 athena char-serverー ぎ圸葺ウ 梭株 file クーー們攪 オャ。ー・シ ーァウ ー瀧笈共. +擽 イ捩, 復イ溢乱 ゥ罷ェィヲャ。 ェィ草 攷ウ, 共亨 復イ溢乱 ェィ草 file。 堂株 ェス擽 カ葺ー イ アクヲー共ウ 晝ー鮒笈共. +キクヲャウ char 魂擽┣乱 ケ紛, lostー 据鵠攵巡 ャク懋ー 鵠 共ウ 倹卿紛 キクイ 葺慣笈共. + +MySQLイ攪 compile捩 MySQL Clients Libraryー 符囈鮒笈共. windows(cygwin) イ愍。 サエ血攵頗 binary・シ イィカ葺慣笈共. + +└ケ: +符ァ text->DB攪 converter株 罹劇。 ァ寳葺ァ 賦慣笈共. ざカ愍。 平ー イキクー ー懋イャ据牟 イキク・シ 們菩、卓桿笈共. +据巡。 ケィヲャ upload・シ 葺イ慣笈共. + +1. char.sql揆 MySQL乱 dump鮒笈共. + +2. inter_athena.conf乱 共搆揆 カ緋ー 鮒笈共. キクヲャウ 梵侠攪 DB罹イ攪 簿ウエ乱 ァ樌カ肥牟 、鷺笈共. +流クー乱 windows(cygwin)攪 イス垈乱株 localhost・シ 堂ゥエ 譜姓笈共. ip。 牟」シ牟幣 鮒笈共. +localhost。 ぎ圸葺株 イス垈 UNIX domain socket擽 梠徐 葺クー 阜ャク乱 硫イー擽 カ一ー冠紛 ァ瀧笈共. + +//3306 is default +db_server_port: 3306 +//DB ip +db_server_ip: 127.0.0.1 +//DB id +db_server_id: ragnarok +//DB pass +db_server_pw: ragnarok +//DB name +db_server_logindb: ragnarok + +3. MySQL イ捩 2ー懍攪 MySQL connect session揆 ーァ瀧笈共. +葺x株 コ尖ヲュ┣ 魂擽┣・シ 攷ウ 堂株魂 ぎ圸据ゥー, 草イ溢ァク株 inter server・シ 怱紛 硫イー鮒笈共. + +ー罹ー懋ウシ 護侃敢 劍イス捩 P4 2.4a/1024MB/WinXP pro(MediaCenter Edition 2K4 KR)/Cygwin GCC 桿笈共. +ウク攤擽 復オュ攤擽クー 阜ャク乱 復オュ牟 ー罹ー懍梵攪 硫攷巡 劍鮒笈共. +復オュ牟凰 攵ウク牟ー 冠呰復 カ捩 硫攷揆 」シ┷囈. 攵ウク牟攪 ぎ圸乱 牟、它揆 叶⊂ウ 梭慣笈共. + +椪 DarkWeissー login server乱 MySQL揆 ァ寳葺クー 亨梠毎慣笈共. +葺ァァ athena攪 キクイ捩 DarkWeiss ウエ共 鵠 椈 梠徐復共ウ 晝ー鮒笈共. + +contact : jazz@creper.com + +//------------------------------------------------------------------------ +//For ENGLISH USER +Athena Char-Server for MySQL. alpha 005 + += hehe. My English is poor. and I have no time to write. :) + +anyway this version use guild and party data on text file base system. +It accesses many times, so memory dumping is useful for less cpu consume. + +MySQL version need MySQL Clients Library to compile. This include Win32-binary compiled by cygwin-gcc. + +Install: +not yet text->DB converter. I found some bug on it, so It's under debug progress. + +1. dump char.sql to MySQL. + +2. add below on inter_athena.conf. and set your own information. +do not use 'localhost' as domain on cygwin. you must set as ip. +if you use localhost on cygwin, cygwin tries to connect MySQL as UNIX domain socket. +but, MySQL does not support UNIX domain socket on windows. + +//3306 is default +db_server_port: 3306 +//DB ip +db_server_ip: 127.0.0.1 +//DB id +db_server_id: ragnarok +//DB pass +db_server_pw: ragnarok +//DB name +db_server_logindb: ragnarok + +3. MySQL version has 2 MySQL connect session. +one is for char-server and the other one is for inter-server. + +developement enviroment) + P4 2.4a/1024MB/WinXP pro(MediaCenter Edition 2K4 KR)/Cygwin GCC + +I'm korean, so contect if U're Korean developer. +Please contact me, If U can use Korean & Japanese well. + +DarkWeiss starts to support MySQL version of login server, but Athena's one works better, I thnik. + +contact : jazz@creper.com diff --git a/misc/src/char_sql/strlib.c b/misc/src/char_sql/strlib.c new file mode 100644 index 0000000..b113d96 --- /dev/null +++ b/misc/src/char_sql/strlib.c @@ -0,0 +1,79 @@ +#include +#include +#include + +#include "strlib.h" +#include "utils.h" + +//----------------------------------------------- +// string lib. +unsigned char* jstrescape (unsigned char* pt) { + //copy from here + unsigned char * ptr; + int i =0, j=0; + + //copy string to temporary + CREATE(ptr, char, J_MAX_MALLOC_SIZE); + strcpy (ptr,pt); + + while (ptr[i] != '\0') { + switch (ptr[i]) { + case '\'': + pt[j++] = '\\'; + pt[j++] = ptr[i++]; + break; + case '\\': + pt[j++] = '\\'; + pt[j++] = ptr[i++]; + break; + default: + pt[j++] = ptr[i++]; + } + } + pt[j++] = '\0'; + free (ptr); + return (unsigned char*) &pt[0]; +} + +unsigned char* jstrescapecpy (unsigned char* pt,unsigned char* spt) { + //copy from here + int i =0, j=0; + + while (spt[i] != '\0') { + switch (spt[i]) { + case '\'': + pt[j++] = '\\'; + pt[j++] = spt[i++]; + break; + case '\\': + pt[j++] = '\\'; + pt[j++] = spt[i++]; + break; + default: + pt[j++] = spt[i++]; + } + } + pt[j++] = '\0'; + return (unsigned char*) &pt[0]; +} +int jmemescapecpy (unsigned char* pt,unsigned char* spt, int size) { + //copy from here + int i =0, j=0; + + while (i < size) { + switch (spt[i]) { + case '\'': + pt[j++] = '\\'; + pt[j++] = spt[i++]; + break; + case '\\': + pt[j++] = '\\'; + pt[j++] = spt[i++]; + break; + default: + pt[j++] = spt[i++]; + } + } + // copy size is 0 ~ (j-1) + return j; +} diff --git a/misc/src/char_sql/strlib.h b/misc/src/char_sql/strlib.h new file mode 100644 index 0000000..6b61690 --- /dev/null +++ b/misc/src/char_sql/strlib.h @@ -0,0 +1,10 @@ +#ifndef _J_STR_LIB_H_ +#define _J_STR_LIB_H_ +#define J_MAX_MALLOC_SIZE 65535 +// String function library. +// code by Jioh L. Jung (ziozzang@4wish.net) +// This code is under license "BSD" +unsigned char* jstrescape (unsigned char* pt); +unsigned char* jstrescapecpy (unsigned char* pt,unsigned char* spt); +int jmemescapecpy (unsigned char* pt,unsigned char* spt, int size); +#endif diff --git a/misc/src/common/GNUmakefile b/misc/src/common/GNUmakefile new file mode 100644 index 0000000..689ac3b --- /dev/null +++ b/misc/src/common/GNUmakefile @@ -0,0 +1,13 @@ +txt sql all: core.o socket.o timer.o grfio.o db.o lock.o nullpo.o malloc.o + +core.o: core.c core.h +socket.o: socket.c socket.h mmo.h +timer.o: timer.c timer.h +grfio.o: grfio.c grfio.h +db.o: db.c db.h +lock.o: lock.h +nullpo.o: nullpo.c nullpo.h +malloc.o: malloc.c malloc.h + +clean: + rm -f *.o diff --git a/misc/src/common/Makefile b/misc/src/common/Makefile new file mode 100644 index 0000000..689ac3b --- /dev/null +++ b/misc/src/common/Makefile @@ -0,0 +1,13 @@ +txt sql all: core.o socket.o timer.o grfio.o db.o lock.o nullpo.o malloc.o + +core.o: core.c core.h +socket.o: socket.c socket.h mmo.h +timer.o: timer.c timer.h +grfio.o: grfio.c grfio.h +db.o: db.c db.h +lock.o: lock.h +nullpo.o: nullpo.c nullpo.h +malloc.o: malloc.c malloc.h + +clean: + rm -f *.o diff --git a/misc/src/common/core.c b/misc/src/common/core.c new file mode 100644 index 0000000..62af254 --- /dev/null +++ b/misc/src/common/core.c @@ -0,0 +1,152 @@ +// $Id: core.c,v 1.1.1.1 2004/09/10 17:44:49 MagicalTux Exp $ +// original : core.c 2003/02/26 18:03:12 Rev 1.7 + +#include +#include +#ifndef LCCWIN32 +#include +#endif +#include + +#include "core.h" +#include "socket.h" +#include "timer.h" +#include "version.h" + +#ifdef MEMWATCH +#include "memwatch.h" +#endif + +static void (*term_func)(void)=NULL; + +/*====================================== + * CORE : Set function + *-------------------------------------- + */ +void set_termfunc(void (*termfunc)(void)) +{ + term_func = termfunc; +} + +/*====================================== + * CORE : Signal Sub Function + *-------------------------------------- + */ + +static void sig_proc(int sn) +{ + int i; + switch(sn){ + case SIGINT: + case SIGTERM: + if(term_func) + term_func(); + for(i=0;i +#include +#include +#include "db.h" +#include "utils.h" + +#ifdef MEMWATCH +#include "memwatch.h" +#endif + +#define ROOT_SIZE 4096 +#ifdef MALLOC_DBN +static struct dbn *dbn_root[512], *dbn_free; +static int dbn_root_rest=0,dbn_root_num=0; + +static void * malloc_dbn(void) +{ + struct dbn* ret; + + if(dbn_free==NULL){ + if(dbn_root_rest<=0){ + CREATE(dbn_root[dbn_root_num], struct dbn, ROOT_SIZE); + + dbn_root_rest=ROOT_SIZE; + dbn_root_num++; + } + return &(dbn_root[dbn_root_num-1][--dbn_root_rest]); + } + ret=dbn_free; + dbn_free = dbn_free->parent; + return ret; +} + +static void free_dbn(struct dbn* add_dbn) +{ + add_dbn->parent = dbn_free; + dbn_free = add_dbn; +} +#endif + +static int strdb_cmp(struct dbt* table,void* a,void* b) +{ + if(table->maxlen) + return strncmp(a,b,table->maxlen); + return strcmp(a,b); +} + +static unsigned int strdb_hash(struct dbt* table,void* a) +{ + int i; + unsigned int h; + unsigned char *p=a; + + i=table->maxlen; + if(i==0) i=0x7fffffff; + for(h=0;*p && --i>=0;){ + h=(h*33 + *p++) ^ (h>>24); + } + return h; +} + +struct dbt* strdb_init(int maxlen) +{ + int i; + struct dbt* table; + + CREATE(table, struct dbt, 1); + + table->cmp=strdb_cmp; + table->hash=strdb_hash; + table->maxlen=maxlen; + for(i=0;iht[i]=NULL; + return table; +} + +static int numdb_cmp(struct dbt* table,void* a,void* b) +{ + int ia,ib; + + ia=(int)a; + ib=(int)b; + + if((ia^ib) & 0x80000000) + return ia<0 ? -1 : 1; + + return ia-ib; +} + +static unsigned int numdb_hash(struct dbt* table,void* a) +{ + return (unsigned int)a; +} + +struct dbt* numdb_init(void) +{ + int i; + struct dbt* table; + + CREATE(table, struct dbt, 1); + + table->cmp=numdb_cmp; + table->hash=numdb_hash; + table->maxlen=sizeof(int); + for(i=0;iht[i]=NULL; + return table; +} + +void* db_search(struct dbt *table,void* key) +{ + struct dbn *p; + + for(p=table->ht[table->hash(table,key) % HASH_SIZE];p;){ + int c=table->cmp(table,key,p->key); + if(c==0) + return p->data; + if(c<0) + p=p->left; + else + p=p->right; + } + return NULL; +} + +void * db_search2(struct dbt *table, const char *key) +{ + int i,sp; + struct dbn *p,*pn,*stack[64]; + int slen = strlen(key); + + for(i=0;iht[i])==NULL) + continue; + sp=0; + while(1){ + if (strncasecmp(key, p->key, slen) == 0) + return p->data; + if((pn=p->left)!=NULL){ + if(p->right){ + stack[sp++]=p->right; + } + p=pn; + } else { + if(p->right){ + p=p->right; + } else { + if(sp==0) + break; + p=stack[--sp]; + } + } + } + } + return 0; +} + +static void db_rotate_left(struct dbn *p,struct dbn **root) +{ + struct dbn * y = p->right; + p->right = y->left; + if (y->left !=0) + y->left->parent = p; + y->parent = p->parent; + + if (p == *root) + *root = y; + else if (p == p->parent->left) + p->parent->left = y; + else + p->parent->right = y; + y->left = p; + p->parent = y; +} + +static void db_rotate_right(struct dbn *p,struct dbn **root) +{ + struct dbn * y = p->left; + p->left = y->right; + if (y->right != 0) + y->right->parent = p; + y->parent = p->parent; + + if (p == *root) + *root = y; + else if (p == p->parent->right) + p->parent->right = y; + else + p->parent->left = y; + y->right = p; + p->parent = y; +} + +static void db_rebalance(struct dbn *p,struct dbn **root) +{ + p->color = RED; + while(p!=*root && p->parent->color==RED){ // rootは必ず黒で親は赤いので親の親は必ず存在する + if (p->parent == p->parent->parent->left) { + struct dbn *y = p->parent->parent->right; + if (y && y->color == RED) { + p->parent->color = BLACK; + y->color = BLACK; + p->parent->parent->color = RED; + p = p->parent->parent; + } else { + if (p == p->parent->right) { + p = p->parent; + db_rotate_left(p, root); + } + p->parent->color = BLACK; + p->parent->parent->color = RED; + db_rotate_right(p->parent->parent, root); + } + } else { + struct dbn* y = p->parent->parent->left; + if (y && y->color == RED) { + p->parent->color = BLACK; + y->color = BLACK; + p->parent->parent->color = RED; + p = p->parent->parent; + } else { + if (p == p->parent->left) { + p = p->parent; + db_rotate_right(p, root); + } + p->parent->color = BLACK; + p->parent->parent->color = RED; + db_rotate_left(p->parent->parent, root); + } + } + } + (*root)->color=BLACK; +} + +static void db_rebalance_erase(struct dbn *z,struct dbn **root) +{ + struct dbn *y = z, *x = NULL, *x_parent = NULL; + + if (y->left == NULL) + x = y->right; + else if (y->right == NULL) + x = y->left; + else { + y = y->right; + while (y->left != NULL) + y = y->left; + x = y->right; + } + if (y != z) { // 左右が両方埋まっていた時 yをzの位置に持ってきてzを浮かせる + z->left->parent = y; + y->left = z->left; + if (y != z->right) { + x_parent = y->parent; + if (x) x->parent = y->parent; + y->parent->left = x; + y->right = z->right; + z->right->parent = y; + } else + x_parent = y; + if (*root == z) + *root = y; + else if (z->parent->left == z) + z->parent->left = y; + else + z->parent->right = y; + y->parent = z->parent; + { int tmp=y->color; y->color=z->color; z->color=tmp; } + y = z; + } else { // どちらか空いていた場合 xをzの位置に持ってきてzを浮かせる + x_parent = y->parent; + if (x) x->parent = y->parent; + if (*root == z) + *root = x; + else if (z->parent->left == z) + z->parent->left = x; + else + z->parent->right = x; + } + // ここまで色の移動の除いて通常の2分木と同じ + if (y->color != RED) { // 赤が消える分には影響無し + while (x != *root && (x == NULL || x->color == BLACK)) + if (x == x_parent->left) { + struct dbn* w = x_parent->right; + if (w->color == RED) { + w->color = BLACK; + x_parent->color = RED; + db_rotate_left(x_parent, root); + w = x_parent->right; + } + if ((w->left == NULL || + w->left->color == BLACK) && + (w->right == NULL || + w->right->color == BLACK)) { + w->color = RED; + x = x_parent; + x_parent = x_parent->parent; + } else { + if (w->right == NULL || + w->right->color == BLACK) { + if (w->left) w->left->color = BLACK; + w->color = RED; + db_rotate_right(w, root); + w = x_parent->right; + } + w->color = x_parent->color; + x_parent->color = BLACK; + if (w->right) w->right->color = BLACK; + db_rotate_left(x_parent, root); + break; + } + } else { // same as above, with right <-> left. + struct dbn* w = x_parent->left; + if (w->color == RED) { + w->color = BLACK; + x_parent->color = RED; + db_rotate_right(x_parent, root); + w = x_parent->left; + } + if ((w->right == NULL || + w->right->color == BLACK) && + (w->left == NULL || + w->left->color == BLACK)) { + w->color = RED; + x = x_parent; + x_parent = x_parent->parent; + } else { + if (w->left == NULL || + w->left->color == BLACK) { + if (w->right) w->right->color = BLACK; + w->color = RED; + db_rotate_left(w, root); + w = x_parent->left; + } + w->color = x_parent->color; + x_parent->color = BLACK; + if (w->left) w->left->color = BLACK; + db_rotate_right(x_parent, root); + break; + } + } + if (x) x->color = BLACK; + } +} + +struct dbn* db_insert(struct dbt *table,void* key,void* data) +{ + struct dbn *p,*priv; + int c,hash; + + hash = table->hash(table,key) % HASH_SIZE; + for(c=0,priv=NULL ,p = table->ht[hash];p;){ + c=table->cmp(table,key,p->key); + if(c==0){ // replace + if (table->release) + table->release(p, 3); + p->data=data; + p->key=key; + return p; + } + priv=p; + if(c<0){ + p=p->left; + } else { + p=p->right; + } + } +#ifdef MALLOC_DBN + p=malloc_dbn(); +#else + CREATE(p, struct dbn, 1); +#endif + if(p==NULL){ + printf("out of memory : db_insert\n"); + return NULL; + } + p->parent= NULL; + p->left = NULL; + p->right = NULL; + p->key = key; + p->data = data; + p->color = RED; + if(c==0){ // hash entry is empty + table->ht[hash] = p; + p->color = BLACK; + } else { + if(c<0){ // left node + priv->left = p; + p->parent=priv; + } else { // right node + priv->right = p; + p->parent=priv; + } + if(priv->color==RED){ // must rebalance + db_rebalance(p,&table->ht[hash]); + } + } + return p; +} + +void* db_erase(struct dbt *table,void* key) +{ + void *data; + struct dbn *p; + int c,hash; + + hash = table->hash(table,key) % HASH_SIZE; + for(c=0,p = table->ht[hash];p;){ + c=table->cmp(table,key,p->key); + if(c==0) + break; + if(c<0) + p=p->left; + else + p=p->right; + } + if(!p) + return NULL; + data=p->data; + db_rebalance_erase(p,&table->ht[hash]); +#ifdef MALLOC_DBN + free_dbn(p); +#else + free(p); +#endif + return data; +} + +void db_foreach(struct dbt *table,int (*func)(void*,void*,va_list),...) +{ + int i,sp; + // red-black treeなので64個stackがあれば2^32個ノードまで大丈夫 + struct dbn *p,*pn,*stack[64]; + va_list ap; + + va_start(ap,func); + for(i=0;iht[i])==NULL) + continue; + sp=0; + while(1){ + func(p->key,p->data,ap); + if((pn=p->left)!=NULL){ + if(p->right){ + stack[sp++]=p->right; + } + p=pn; + } else { + if(p->right){ + p=p->right; + } else { + if(sp==0) + break; + p=stack[--sp]; + } + } + } + } + va_end(ap); +} + +void db_final(struct dbt *table,int (*func)(void*,void*,va_list),...) +{ + int i,sp; + struct dbn *p,*pn,*stack[64]; + va_list ap; + + va_start(ap,func); + for(i=0;iht[i])==NULL) + continue; + sp=0; + while(1){ + if(func) + func(p->key,p->data,ap); + if((pn=p->left)!=NULL){ + if(p->right){ + stack[sp++]=p->right; + } + } else { + if(p->right){ + pn=p->right; + } else { + if(sp==0) + break; + pn=stack[--sp]; + } + } +#ifdef MALLOC_DBN + free_dbn(p); +#else + free(p); +#endif + p=pn; + } + } + free(table); + va_end(ap); +} diff --git a/misc/src/common/db.h b/misc/src/common/db.h new file mode 100644 index 0000000..ea9acea --- /dev/null +++ b/misc/src/common/db.h @@ -0,0 +1,47 @@ +#ifndef _DB_H_ +#define _DB_H_ + +#include + +#define HASH_SIZE (256+27) + +#define RED 0 +#define BLACK 1 + +struct dbn { + struct dbn *parent,*left,*right; + int color; + void *key; + void *data; +}; + +struct dbt { + int (*cmp)(struct dbt*,void*,void*); + unsigned int (*hash)(struct dbt*,void*); + // which 1 - key, 2 - data, 3 - both + void (*release)(struct dbn*,int which); + int maxlen; + struct dbn *ht[HASH_SIZE]; +}; + +#define strdb_search(t,k) db_search((t),(void*)(k)) +#define strdb_insert(t,k,d) db_insert((t),(void*)(k),(void*)(d)) +#define strdb_erase(t,k) db_erase ((t),(void*)(k)) +#define strdb_foreach db_foreach +#define strdb_final db_final +#define numdb_search(t,k) db_search((t),(void*)(k)) +#define numdb_insert(t,k,d) db_insert((t),(void*)(k),(void*)(d)) +#define numdb_erase(t,k) db_erase ((t),(void*)(k)) +#define numdb_foreach db_foreach +#define numdb_final db_final + +struct dbt* strdb_init(int maxlen); +struct dbt* numdb_init(void); +void* db_search(struct dbt *table,void* key); +void* db_search2(struct dbt *table, const char *key); // [MouseJstr] +struct dbn* db_insert(struct dbt *table,void* key,void* data); +void* db_erase(struct dbt *table,void* key); +void db_foreach(struct dbt*,int(*)(void*,void*,va_list),...); +void db_final(struct dbt*,int(*)(void*,void*,va_list),...); + +#endif diff --git a/misc/src/common/grfio.c b/misc/src/common/grfio.c new file mode 100644 index 0000000..08a8b2a --- /dev/null +++ b/misc/src/common/grfio.c @@ -0,0 +1,948 @@ +/********************************************************************* + * + * Ragnarok Online Emulator : grfio.c -- grf file I/O Module + *-------------------------------------------------------------------- + * special need library : zlib + ********************************************************************* + * $Id: grfio.c,v 1.2 2004/09/29 17:31:49 kalaspuff Exp $ + * + * 2002/12/18... the original edition + * 2003/01/23 ... Code correction + * 2003/02/01 ... An addition and decryption processing are improved for LocalFile and two or more GRF(s) check processing. + * 2003/02/02 ... Even if there is no grf it does not stop -- as -- correction + * 2003/02/02... grf reading specification can be added later -- as -- correction (grfio_add function addition) + * 2003/02 / 03... at the time of grfio_resourcecheck processing the entry addition processing method -- correction + * 2003/02/05... change of the processing in grfio_init + * 2003/02/23... a local file check -- GRFIO_LOCAL -- switch (Defoe -- Function Off) + * 2003/10/21 ... The data of alpha client was read. + * 2003/11/10 ... Ready new grf format. + * 2003/11/11 ... version check fix & bug fix + */ + +#include +#include +#include +#include +#include + +#include + +#include "utils.h" +#include "grfio.h" +#include "mmo.h" + +#ifdef MEMWATCH +#include "memwatch.h" +#endif + +typedef unsigned char BYTE; +typedef unsigned short WORD; +typedef unsigned long DWORD; + +static char data_file[1024] = ""; // "data.grf"; +static char sdata_file[1024] = ""; // "sdata.grf"; +static char adata_file[1024] = ""; // "adata.grf"; +static char data_dir[1024] = ""; // "../"; + +// accessor to data_file,adata_file,sdata_file +char *grfio_setdatafile(const char *str){ strcpy(data_file,str); return data_file; } +char *grfio_setadatafile(const char *str){ strcpy(adata_file,str); return adata_file; } +char *grfio_setsdatafile(const char *str){ strcpy(sdata_file,str); return sdata_file; } + +//---------------------------- +// file entry table struct +//---------------------------- +typedef struct { + int srclen; // compressed size + int srclen_aligned; // + int declen; // original size + int srcpos; + short next; + char cycle; + char type; + char fn[128-4*5]; // file name + char gentry; // read grf file select +} FILELIST; +//gentry ... 0 : It acquires from a local file. +// It acquires from the resource file of 1>=:gentry_table[gentry-1]. +// 1<=: Check a local file. +// If it is, after re-setting to 0, it acquires from a local file. +// If there is nothing, mark reversal will be carried out, and it will re-set, and will acquire from a resource file as well as 1>=. + +//Since char defines *FILELIST.gentry, the maximum which can be added by grfio_add becomes by 127 pieces. + +#define GENTRY_LIMIT 127 +#define FILELIST_LIMIT 32768 // temporary maximum, and a theory top maximum are 2G. + +static FILELIST *filelist; +static int filelist_entrys; +static int filelist_maxentry; + +static char **gentry_table; +static int gentry_entrys; +static int gentry_maxentry; + +//---------------------------- +// file list hash table +//---------------------------- +static int filelist_hash[256]; + +//---------------------------- +// grf decode data table +//---------------------------- +static unsigned char BitMaskTable[8] = { + 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 +}; + +static char BitSwapTable1[64] = { + 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, + 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, + 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, + 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 +}; +static char BitSwapTable2[64] = { + 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, + 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, + 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, + 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 +}; +static char BitSwapTable3[32] = { + 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, + 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 +}; + +static unsigned char NibbleData[4][64]={ + { + 0xef, 0x03, 0x41, 0xfd, 0xd8, 0x74, 0x1e, 0x47, 0x26, 0xef, 0xfb, 0x22, 0xb3, 0xd8, 0x84, 0x1e, + 0x39, 0xac, 0xa7, 0x60, 0x62, 0xc1, 0xcd, 0xba, 0x5c, 0x96, 0x90, 0x59, 0x05, 0x3b, 0x7a, 0x85, + 0x40, 0xfd, 0x1e, 0xc8, 0xe7, 0x8a, 0x8b, 0x21, 0xda, 0x43, 0x64, 0x9f, 0x2d, 0x14, 0xb1, 0x72, + 0xf5, 0x5b, 0xc8, 0xb6, 0x9c, 0x37, 0x76, 0xec, 0x39, 0xa0, 0xa3, 0x05, 0x52, 0x6e, 0x0f, 0xd9, + }, { + 0xa7, 0xdd, 0x0d, 0x78, 0x9e, 0x0b, 0xe3, 0x95, 0x60, 0x36, 0x36, 0x4f, 0xf9, 0x60, 0x5a, 0xa3, + 0x11, 0x24, 0xd2, 0x87, 0xc8, 0x52, 0x75, 0xec, 0xbb, 0xc1, 0x4c, 0xba, 0x24, 0xfe, 0x8f, 0x19, + 0xda, 0x13, 0x66, 0xaf, 0x49, 0xd0, 0x90, 0x06, 0x8c, 0x6a, 0xfb, 0x91, 0x37, 0x8d, 0x0d, 0x78, + 0xbf, 0x49, 0x11, 0xf4, 0x23, 0xe5, 0xce, 0x3b, 0x55, 0xbc, 0xa2, 0x57, 0xe8, 0x22, 0x74, 0xce, + }, { + 0x2c, 0xea, 0xc1, 0xbf, 0x4a, 0x24, 0x1f, 0xc2, 0x79, 0x47, 0xa2, 0x7c, 0xb6, 0xd9, 0x68, 0x15, + 0x80, 0x56, 0x5d, 0x01, 0x33, 0xfd, 0xf4, 0xae, 0xde, 0x30, 0x07, 0x9b, 0xe5, 0x83, 0x9b, 0x68, + 0x49, 0xb4, 0x2e, 0x83, 0x1f, 0xc2, 0xb5, 0x7c, 0xa2, 0x19, 0xd8, 0xe5, 0x7c, 0x2f, 0x83, 0xda, + 0xf7, 0x6b, 0x90, 0xfe, 0xc4, 0x01, 0x5a, 0x97, 0x61, 0xa6, 0x3d, 0x40, 0x0b, 0x58, 0xe6, 0x3d, + }, { + 0x4d, 0xd1, 0xb2, 0x0f, 0x28, 0xbd, 0xe4, 0x78, 0xf6, 0x4a, 0x0f, 0x93, 0x8b, 0x17, 0xd1, 0xa4, + 0x3a, 0xec, 0xc9, 0x35, 0x93, 0x56, 0x7e, 0xcb, 0x55, 0x20, 0xa0, 0xfe, 0x6c, 0x89, 0x17, 0x62, + 0x17, 0x62, 0x4b, 0xb1, 0xb4, 0xde, 0xd1, 0x87, 0xc9, 0x14, 0x3c, 0x4a, 0x7e, 0xa8, 0xe2, 0x7d, + 0xa0, 0x9f, 0xf6, 0x5c, 0x6a, 0x09, 0x8d, 0xf0, 0x0f, 0xe3, 0x53, 0x25, 0x95, 0x36, 0x28, 0xcb, + } +}; +/*----------------- + * long data get + */ +static unsigned int getlong(unsigned char *p) +{ + return *p+p[1]*256+(p[2]+p[3]*256)*65536; +} + +/*========================================== + * Grf data decode : Subs + *------------------------------------------ + */ +static void NibbleSwap(BYTE *Src, int len) +{ + for(;0>4) | (*Src<<4); + } +} + +static void BitConvert(BYTE *Src,char *BitSwapTable) +{ + int lop,prm; + BYTE tmp[8]; + *(DWORD*)tmp=*(DWORD*)(tmp+4)=0; + for(lop=0;lop!=64;lop++) { + prm = BitSwapTable[lop]-1; + if (Src[(prm >> 3) & 7] & BitMaskTable[prm & 7]) { + tmp[(lop >> 3) & 7] |= BitMaskTable[lop & 7]; + } + } + *(DWORD*)Src = *(DWORD*)tmp; + *(DWORD*)(Src+4) = *(DWORD*)(tmp+4); +} + +static void BitConvert4(BYTE *Src) +{ + int lop,prm; + BYTE tmp[8]; + tmp[0] = ((Src[7]<<5) | (Src[4]>>3)) & 0x3f; // ..0 vutsr + tmp[1] = ((Src[4]<<1) | (Src[5]>>7)) & 0x3f; // ..srqpo n + tmp[2] = ((Src[4]<<5) | (Src[5]>>3)) & 0x3f; // ..o nmlkj + tmp[3] = ((Src[5]<<1) | (Src[6]>>7)) & 0x3f; // ..kjihg f + tmp[4] = ((Src[5]<<5) | (Src[6]>>3)) & 0x3f; // ..g fedcb + tmp[5] = ((Src[6]<<1) | (Src[7]>>7)) & 0x3f; // ..cba98 7 + tmp[6] = ((Src[6]<<5) | (Src[7]>>3)) & 0x3f; // ..8 76543 + tmp[7] = ((Src[7]<<1) | (Src[4]>>7)) & 0x3f; // ..43210 v + + for(lop=0;lop!=4;lop++) { + tmp[lop] = (NibbleData[lop][tmp[lop*2]] & 0xf0) + | (NibbleData[lop][tmp[lop*2+1]] & 0x0f); + } + + *(DWORD*)(tmp+4)=0; + for(lop=0;lop!=32;lop++) { + prm = BitSwapTable3[lop]-1; + if (tmp[prm >> 3] & BitMaskTable[prm & 7]) { + tmp[(lop >> 3) + 4] |= BitMaskTable[lop & 7]; + } + } + *(DWORD*)Src ^= *(DWORD*)(tmp+4); +} + +static void decode_des_etc(BYTE *buf,int len,int type,int cycle) +{ + int lop,cnt=0; + if(cycle<3) cycle=3; + else if(cycle<5) cycle++; + else if(cycle<7) cycle+=9; + else cycle+=15; + + for(lop=0;lop*8 64K on 16-bit machine: */ + if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; + + stream.next_out = dest; + stream.avail_out = (uInt)*destLen; + if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; + + stream.zalloc = (alloc_func)0; + stream.zfree = (free_func)0; + + err = inflateInit(&stream); + if (err != Z_OK) return err; + + err = inflate(&stream, Z_FINISH); + if (err != Z_STREAM_END) { + inflateEnd(&stream); + return err == Z_OK ? Z_BUF_ERROR : err; + } + *destLen = stream.total_out; + + err = inflateEnd(&stream); + return err; +} +/*********************************************************** + *** File List Sobroutines *** + ***********************************************************/ + +/*========================================== + * File List : Hash make + *------------------------------------------ + */ +static int filehash(unsigned char *fname) +{ + unsigned int hash=0; + while(*fname) { + hash = ((hash<<1)+(hash>>7)*9+tolower(*fname)); + fname++; + } + return hash & 255; +} + +/*========================================== + * File List : Hash initalize + *------------------------------------------ + */ +static void hashinit(void) +{ + int lop; + for(lop=0;lop<256;lop++) + filelist_hash[lop]=-1; +} + +/*========================================== + * File List : File find + *------------------------------------------ + */ +FILELIST *filelist_find(char *fname) +{ + int hash; + + for(hash=filelist_hash[filehash(fname)];hash>=0;hash=filelist[hash].next) { + if(strcasecmp(filelist[hash].fn,fname)==0) + break; + } + + return (hash>=0)? &filelist[hash] : NULL; +} + +/*========================================== + * File List : Filelist add + *------------------------------------------ + */ +#define FILELIST_ADDS 1024 // number increment of file lists ` + +static FILELIST* filelist_add(FILELIST *entry) +{ + int hash; + + if (filelist_entrys>=FILELIST_LIMIT) { + printf("filelist limit : filelist_add\n"); + exit(1); + } + + if (filelist_entrys>=filelist_maxentry) { + FILELIST *new_filelist = (FILELIST*)realloc( + (void*)filelist, (filelist_maxentry+FILELIST_ADDS)*sizeof(FILELIST) ); + if (new_filelist != NULL) { + filelist = new_filelist; + memset(filelist + filelist_maxentry, '\0', + FILELIST_ADDS * sizeof(FILELIST)); + filelist_maxentry += FILELIST_ADDS; + } else { + printf("out of memory : filelist_add\n"); + exit(1); + } + } + + memcpy( &filelist[filelist_entrys], entry, sizeof(FILELIST) ); + + hash = filehash(entry->fn); + filelist[filelist_entrys].next = filelist_hash[hash]; + filelist_hash[hash] = filelist_entrys; + + filelist_entrys++; + + return &filelist[filelist_entrys-1]; +} + +static FILELIST* filelist_modify(FILELIST *entry) +{ + FILELIST *fentry; + if ((fentry=filelist_find(entry->fn))!=NULL) { + int tmp = fentry->next; + memcpy( fentry, entry, sizeof(FILELIST) ); + fentry->next = tmp; + } else { + fentry = filelist_add(entry); + } + return fentry; +} + +/*========================================== + * File List : filelist size adjust + *------------------------------------------ + */ +static void filelist_adjust(void) +{ + if (filelist!=NULL) { + if (filelist_maxentry>filelist_entrys) { + FILELIST *new_filelist = (FILELIST*)realloc( + (void*)filelist,filelist_entrys*sizeof(FILELIST) ); + if (new_filelist != NULL) { + filelist = new_filelist; + filelist_maxentry = filelist_entrys; + } else { + printf("out of memory : filelist\n"); + exit(1); + } + } + } +} + +/*********************************************************** + *** Grfio Sobroutines *** + ***********************************************************/ +/*========================================== + * Grfio : Resnametable replace + *------------------------------------------ + */ +char* grfio_resnametable(char* fname, char *lfname) +{ + FILE *fp; + char *p; + char w1[256],w2[256],restable[256],line[512]; + + sprintf(restable,"%sdata\\resnametable.txt",data_dir); + + for(p=&restable[0];*p!=0;p++) if (*p=='\\') *p = '/'; + + fp = fopen(restable,"rb"); + if(fp==NULL) { + printf("%s not found\n",restable); + exit(1); // 1:not found error + } + + while(fgets(line,508,fp)){ + if((sscanf(line,"%[^#]#%[^#]#",w1,w2)==2) && (sscanf(fname,"%*5s%s",lfname)==1) && (!strcmpi(w1,lfname))){ + sprintf(lfname,"data\\%s",w2); + fclose(fp); + return lfname; + } + } + fclose(fp); + return fname; + +} + +/*========================================== + * Grfio : Resource file size get + *------------------------------------------ + */ +int grfio_size(char *fname) +{ + FILELIST *entry; + + entry = filelist_find(fname); + + if (entry==NULL || entry->gentry<0) { // LocalFileCheck + char lfname[256],rname[256],*p; + FILELIST lentry; + struct stat st; + + //printf("%s\t",fname); + sprintf(rname,"%s",grfio_resnametable(fname,lfname)); + //printf("%s\n",rname); + sprintf(lfname,"%s%s",data_dir,rname); + //printf("%s\n",lfname); + + for(p=&lfname[0];*p!=0;p++) if (*p=='\\') *p = '/'; // * At the time of Unix + + if (stat(lfname,&st)==0) { + strncpy(lentry.fn, fname, sizeof(lentry.fn)-1 ); + lentry.declen = st.st_size; + lentry.gentry = 0; // 0:LocalFile + entry = filelist_modify(&lentry); + } else if (entry==NULL) { + printf("%s not found\n", fname); + //exit(1); + return -1; + } + } + return entry->declen; +} + +/*========================================== + * Grfio : Resource file read & size get + *------------------------------------------ + */ +void* grfio_reads(char *fname, int *size) +{ + FILE *in = NULL; + unsigned char *buf=NULL,*buf2=NULL; + char *gfname; + FILELIST *entry; + + entry = filelist_find(fname); + + if (entry==NULL || entry->gentry<=0) { // LocalFileCheck + char lfname[256],rname[256],*p; + FILELIST lentry; + + strncpy(lfname,fname,255); + sprintf(rname,"%s",grfio_resnametable(fname,lfname)); + sprintf(lfname,"%s%s",data_dir,rname); + //printf("%s\n",lfname); + + for(p=&lfname[0];*p!=0;p++) if (*p=='\\') *p = '/'; // * At the time of Unix + + in = fopen(lfname,"rb"); + if(in!=NULL) { + if (entry!=NULL && entry->gentry==0) { + lentry.declen=entry->declen; + } else { + fseek(in,0,2); // SEEK_END + lentry.declen = ftell(in); + } + fseek(in,0,0); // SEEK_SET + buf2 = calloc(lentry.declen+1024, 1); + if (buf2==NULL) { + printf("file read memory allocate error : declen\n"); + goto errret; + } + fread(buf2,1,lentry.declen,in); + fclose(in); in = NULL; + strncpy( lentry.fn, fname, sizeof(lentry.fn)-1 ); + lentry.gentry = 0; // 0:LocalFile + entry = filelist_modify(&lentry); + } else { + if (entry!=NULL && entry->gentry<0) { + entry->gentry = -entry->gentry; // local file checked + } else { + printf("%s not found\n", fname); + //goto errret; + free(buf2); + return NULL; + } + } + } + if (entry!=NULL && entry->gentry>0) { // Archive[GRF] File Read + buf = calloc(entry->srclen_aligned+1024, 1); + if (buf==NULL) { + printf("file read memory allocate error : srclen_aligned\n"); + goto errret; + } + gfname = gentry_table[entry->gentry-1]; + in = fopen(gfname,"rb"); + if(in==NULL) { + printf("%s not found\n",gfname); + //goto errret; + free(buf); + return NULL; + } + fseek(in,entry->srcpos,0); + fread(buf,1,entry->srclen_aligned,in); + fclose(in); + buf2=calloc(entry->declen+1024, 1); + if (buf2==NULL) { + printf("file decode memory allocate error\n"); + goto errret; + } + if(entry->type==1 || entry->type==3 || entry->type==5) { + uLongf len; + if (entry->cycle>=0) { + decode_des_etc(buf,entry->srclen_aligned,entry->cycle==0,entry->cycle); + } + len=entry->declen; + decode_zip(buf2,&len,buf,entry->srclen); + if(len!=entry->declen) { + printf("decode_zip size miss match err: %d != %d\n",(int)len,entry->declen); + goto errret; + } + } else { + memcpy(buf2,buf,entry->declen); + } + free(buf); + } + if (size!=NULL && entry!=NULL) + *size = entry->declen; + return buf2; +errret: + if (buf!=NULL) free(buf); + if (buf2!=NULL) free(buf2); + if (in!=NULL) fclose(in); + exit(1); //return NULL; +} + +/*========================================== + * Grfio : Resource file read + *------------------------------------------ + */ +void* grfio_read(char *fname) +{ + return grfio_reads(fname,NULL); +} + +/*========================================== + * Resource filename decode + *------------------------------------------ + */ +static unsigned char * decode_filename(unsigned char *buf,int len) +{ + int lop; + for(lop=0;lop> 8; + + if (grf_version==0x01) { //****** Grf version 01xx ****** + list_size = grf_size-ftell(fp); + grf_filelist = calloc(list_size, 1); + if(grf_filelist==NULL){ + fclose(fp); + printf("out of memory : grf_filelist\n"); + return 3; // 3:memory alloc error + } + fread(grf_filelist,1,list_size,fp); + fclose(fp); + + entrys = getlong(grf_header+0x26) - getlong(grf_header+0x22) - 7; + + // Get an entry + for(entry=0,ofs=0;entrysizeof(aentry.fn)-1){ + printf("file name too long : %s\n",fname); + free(grf_filelist); + exit(1); + } + srclen=0; + if((period_ptr=rindex(fname,'.'))!=NULL){ + for(lop=0;lop<4;lop++) { + if(strcasecmp(period_ptr,".gnd\0.gat\0.act\0.str"+lop*5)==0) + break; + } + srclen=getlong(grf_filelist+ofs2)-getlong(grf_filelist+ofs2+8)-715; + if(lop==4) { + for(lop=10,srccount=1;srclen>=lop;lop=lop*10,srccount++); + } else { + srccount=0; + } + } else { + srccount=0; + } + + aentry.srclen = srclen; + aentry.srclen_aligned = getlong(grf_filelist+ofs2+4)-37579; + aentry.declen = getlong(grf_filelist+ofs2+8); + aentry.srcpos = getlong(grf_filelist+ofs2+13)+0x2e; + aentry.cycle = srccount; + aentry.type = type; + strncpy(aentry.fn,fname,sizeof(aentry.fn)-1); +#ifdef GRFIO_LOCAL + aentry.gentry = -(gentry+1); // As Flag for making it a negative number carrying out the first time LocalFileCheck +#else + aentry.gentry = gentry+1; // With no first time LocalFileCheck +#endif + filelist_modify(&aentry); + } + ofs = ofs2 + 17; + } + free(grf_filelist); + + } else if (grf_version==0x02) { //****** Grf version 02xx ****** + unsigned char eheader[8]; + unsigned char *rBuf; + uLongf rSize,eSize; + + fread(eheader,1,8,fp); + rSize = getlong(eheader); // Read Size + eSize = getlong(eheader+4); // Extend Size + + if (rSize > grf_size-ftell(fp)) { + fclose(fp); + printf("Illegal data format : grf compress entry size\n"); + return 4; + } + + rBuf = calloc( rSize , 1); // Get a Read Size + if (rBuf==NULL) { + fclose(fp); + printf("out of memory : grf compress entry table buffer\n"); + return 3; + } + grf_filelist = calloc( eSize , 1); // Get a Extend Size + if (grf_filelist==NULL) { + free(rBuf); + fclose(fp); + printf("out of memory : grf extract entry table buffer\n"); + return 3; + } + fread(rBuf,1,rSize,fp); + fclose(fp); + decode_zip(grf_filelist,&eSize,rBuf,rSize); // Decode function + list_size = eSize; + free(rBuf); + + entrys = getlong(grf_header+0x26) - 7; + + // Get an entry + for(entry=0,ofs=0;entrysizeof(aentry.fn)-1) { + printf("grf : file name too long : %s\n",fname); + free(grf_filelist); + exit(1); + } + ofs2 = ofs+strlen(grf_filelist+ofs)+1; + type = grf_filelist[ofs2+12]; + if(type==1 || type==3 || type==5) { + srclen=getlong(grf_filelist+ofs2); + if (grf_filelist[ofs2+12]==3) { + for(lop=10,srccount=1;srclen>=lop;lop=lop*10,srccount++); + } else if (grf_filelist[ofs2+12]==5) { + srccount = 0; + } else { // if (grf_filelist[ofs2+12]==1) { + srccount = -1; + } + + aentry.srclen = srclen; + aentry.srclen_aligned = getlong(grf_filelist+ofs2+4); + aentry.declen = getlong(grf_filelist+ofs2+8); + aentry.srcpos = getlong(grf_filelist+ofs2+13)+0x2e; + aentry.cycle = srccount; + aentry.type = type; + strncpy(aentry.fn,fname,sizeof(aentry.fn)-1); +#ifdef GRFIO_LOCAL + aentry.gentry = -(gentry+1); // As Flag for making it a negative number carrying out the first time LocalFileCheck +#else + aentry.gentry = gentry+1; // With no first time LocalFileCheck +#endif + filelist_modify(&aentry); + } + ofs = ofs2 + 17; + } + free(grf_filelist); + + } else { //****** Grf Other version ****** + fclose(fp); + printf("not support grf versions : %04x\n",getlong(grf_header+0x2a)); + return 4; + } + + filelist_adjust(); // Unnecessary area release of filelist + + return 0; // 0:no error +} + +/*========================================== + * Grfio : Resource file check + *------------------------------------------ + */ +static void grfio_resourcecheck() +{ + int size; + unsigned char *buf,*ptr; + char w1[256],w2[256],src[256],dst[256]; + FILELIST *entry; + + buf=grfio_reads("data\\resnametable.txt",&size); + buf[size] = 0; + + for(ptr=buf;ptr-buf=GENTRY_LIMIT) { + printf("gentrys limit : grfio_add\n"); + exit(1); + } + + printf("%s file reading...\n",fname); + + if (gentry_entrys>=gentry_maxentry) { + char **new_gentry = (char**)realloc( + (void*)gentry_table,(gentry_maxentry+GENTRY_ADDS)*sizeof(char*) ); + if (new_gentry!=NULL) { + int lop; + gentry_table = new_gentry; + gentry_maxentry += GENTRY_ADDS; + for(lop=gentry_entrys;lop +#include "lock.h" + +// 書き込みファイルの保護処理 +// (書き込みが終わるまで、旧ファイルを保管しておく) + +// 新しいファイルの書き込み開始 +FILE* lock_fopen(const char* filename,int *info) { + char newfile[512]; + FILE *fp; + int no = 0; + + // 安全なファイル名を得る(手抜き) + do { + sprintf(newfile,"%s_%04d.tmp",filename,++no); + } while((fp = fopen(newfile,"r")) && (fclose(fp), no<9999) ); + *info = no; + return fopen(newfile,"w"); +} + +// 旧ファイルを削除&新ファイルをリネーム +int lock_fclose(FILE *fp,const char* filename,int *info) { + int ret = 0; + char newfile[512]; + if(fp != NULL) { + ret = fclose(fp); + sprintf(newfile,"%s_%04d.tmp",filename,*info); + remove(filename); + // このタイミングで落ちると最悪。 + rename(newfile,filename); + return ret; + } else { + return 1; + } +} + diff --git a/misc/src/common/lock.h b/misc/src/common/lock.h new file mode 100644 index 0000000..795bf88 --- /dev/null +++ b/misc/src/common/lock.h @@ -0,0 +1,8 @@ +#ifndef _LOCK_H_ +#define _LOCK_H_ + +FILE* lock_fopen(const char* filename,int *info); +int lock_fclose(FILE *fp,const char* filename,int *info); + +#endif + diff --git a/misc/src/common/malloc.c b/misc/src/common/malloc.c new file mode 100644 index 0000000..eda9bc2 --- /dev/null +++ b/misc/src/common/malloc.c @@ -0,0 +1,44 @@ +#include +#include +#include "malloc.h" + +void* aMalloc_( size_t size, const char *file, int line, const char *func ) +{ + void *ret; + +// printf("%s:%d: in func %s: malloc %d\n",file,line,func,size); + ret=malloc(size); + if(ret==NULL){ + printf("%s:%d: in func %s: malloc error out of memory!\n",file,line,func); + exit(1); + + } + return ret; +} +void* aCalloc_( size_t num, size_t size, const char *file, int line, const char *func ) +{ + void *ret; + +// printf("%s:%d: in func %s: calloc %d %d\n",file,line,func,num,size); + ret=calloc(num,size); + if(ret==NULL){ + printf("%s:%d: in func %s: calloc error out of memory!\n",file,line,func); + exit(1); + + } + return ret; +} + +void* aRealloc_( void *p, size_t size, const char *file, int line, const char *func ) +{ + void *ret; + +// printf("%s:%d: in func %s: realloc %p %d\n",file,line,func,p,size); + ret=realloc(p,size); + if(ret==NULL){ + printf("%s:%d: in func %s: realloc error out of memory!\n",file,line,func); + exit(1); + + } + return ret; +} diff --git a/misc/src/common/malloc.h b/misc/src/common/malloc.h new file mode 100644 index 0000000..3733a5e --- /dev/null +++ b/misc/src/common/malloc.h @@ -0,0 +1,25 @@ +#ifndef _MALLOC_H_ +#define _MALLOC_H_ + +#include + +#if __STDC_VERSION__ < 199901L +# if __GNUC__ >= 2 +# define __func__ __FUNCTION__ +# else +# define __func__ "" +# endif +#endif + +#define ALC_MARK __FILE__, __LINE__, __func__ + +void* aMalloc_( size_t size, const char *file, int line, const char *func ); +void* aCalloc_( size_t num, size_t size, const char *file, int line, const char *func ); +void* aRealloc_( void *p, size_t size, const char *file, int line, const char *func ); + +#define aMalloc(n) aMalloc_(n,ALC_MARK) +#define aCalloc(m,n) aCalloc_(m,n,ALC_MARK) +#define aRealloc(p,n) aRealloc_(p,n,ALC_MARK) + + +#endif diff --git a/misc/src/common/mmo.h b/misc/src/common/mmo.h new file mode 100644 index 0000000..4105135 --- /dev/null +++ b/misc/src/common/mmo.h @@ -0,0 +1,304 @@ +// $Id: mmo.h,v 1.3 2004/09/25 20:12:25 PoW Exp $ +// Original : mmo.h 2003/03/14 12:07:02 Rev.1.7 + +#ifndef _MMO_H_ +#define _MMO_H_ + +#include +#include "utils.h" // LCCWIN32 + +#ifdef CYGWIN +// txtやlogなどの書き出すファイルの改行コード +#define RETCODE "\r\n" // (CR/LF:Windows系) +#else +#define RETCODE "\n" // (LF:Unix系) +#endif + +#define FIFOSIZE_SERVERLINK 128*1024 + +// set to 0 to not check IP of player between each server. +// set to another value if you want to check (1) +#define CMP_AUTHFIFO_IP 1 + +#define CMP_AUTHFIFO_LOGIN2 1 + +#define MAX_MAP_PER_SERVER 512 +#define MAX_INVENTORY 100 +#define MAX_AMOUNT 30000 +#define MAX_ZENY 1000000000 // 1G zeny +#define MAX_CART 100 +#define MAX_SKILL 450 +#define GLOBAL_REG_NUM 96 +#define ACCOUNT_REG_NUM 16 +#define ACCOUNT_REG2_NUM 16 +#define DEFAULT_WALK_SPEED 150 +#define MIN_WALK_SPEED 0 +#define MAX_WALK_SPEED 1000 +#define MAX_STORAGE 300 +#define MAX_GUILD_STORAGE 1000 +#define MAX_PARTY 12 +#define MAX_GUILD 36 // increased max guild members to accomodate for +2 increase for extension levels [Valaris] (removed) [PoW] +#define MAX_GUILDPOSITION 20 // increased max guild positions to accomodate for all members [Valaris] (removed) [PoW] +#define MAX_GUILDEXPLUSION 32 +#define MAX_GUILDALLIANCE 16 +#define MAX_GUILDSKILL 8 +#define MAX_GUILDCASTLE 24 // increased to include novice castles [Valaris] +#define MAX_GUILDLEVEL 50 + +#define MIN_HAIR_STYLE battle_config.min_hair_style +#define MAX_HAIR_STYLE battle_config.max_hair_style +#define MIN_HAIR_COLOR battle_config.min_hair_color +#define MAX_HAIR_COLOR battle_config.max_hair_color +#define MIN_CLOTH_COLOR battle_config.min_cloth_color +#define MAX_CLOTH_COLOR battle_config.max_cloth_color + +// for produce +#define MIN_ATTRIBUTE 0 +#define MAX_ATTRIBUTE 4 +#define ATTRIBUTE_NORMAL 0 +#define MIN_STAR 0 +#define MAX_STAR 3 + +#define MIN_PORTAL_MEMO 0 +#define MAX_PORTAL_MEMO 2 + +#define MAX_STATUS_TYPE 5 + +#define WEDDING_RING_M 2634 +#define WEDDING_RING_F 2635 + +#define CHAR_CONF_NAME "conf/char_athena.conf" + +struct item { + int id; + short nameid; + short amount; + unsigned short equip; + char identify; + char refine; + char attribute; + short card[4]; + short broken; +}; + +struct point{ + char map[24]; + short x,y; +}; + +struct skill { + unsigned short id,lv,flag; +}; + +struct global_reg { + char str[32]; + int value; +}; + +struct s_pet { + int account_id; + int char_id; + int pet_id; + short class; + short level; + short egg_id;//pet egg id + short equip;//pet equip name_id + short intimate;//pet friendly + short hungry;//pet hungry + char name[24]; + char rename_flag; + char incuvate; +}; + +struct mmo_charstatus { + int char_id; + int account_id; + int partner_id; + + int base_exp,job_exp,zeny; + + short class; + short status_point,skill_point; + int hp,max_hp,sp,max_sp; + short option,karma,manner; + short hair,hair_color,clothes_color; + int party_id,guild_id,pet_id; + + short weapon,shield; + short head_top,head_mid,head_bottom; + + char name[24]; + unsigned char base_level,job_level; + short str,agi,vit,int_,dex,luk; + unsigned char char_num,sex; + + unsigned long mapip; + unsigned int mapport; + + struct point last_point,save_point,memo_point[10]; + struct item inventory[MAX_INVENTORY],cart[MAX_CART]; + struct skill skill[MAX_SKILL]; + int global_reg_num; + struct global_reg global_reg[GLOBAL_REG_NUM]; + int account_reg_num; + struct global_reg account_reg[ACCOUNT_REG_NUM]; + int account_reg2_num; + struct global_reg account_reg2[ACCOUNT_REG2_NUM]; +}; + +struct storage { + int account_id; + short storage_status; + short storage_amount; + struct item storage[MAX_STORAGE]; +}; + +struct guild_storage { + int guild_id; + short storage_status; + short storage_amount; + struct item storage[MAX_GUILD_STORAGE]; +}; + +struct map_session_data; + +struct gm_account { + int account_id; + int level; +}; + +struct party_member { + int account_id; + char name[24],map[24]; + int leader,online,lv; + struct map_session_data *sd; +}; + +struct party { + int party_id; + char name[24]; + int exp; + int item; + struct party_member member[MAX_PARTY]; +}; + +struct guild_member { + int account_id, char_id; + short hair,hair_color,gender,class,lv; + int exp,exp_payper; + short online,position; + int rsv1,rsv2; + char name[24]; + struct map_session_data *sd; +}; + +struct guild_position { + char name[24]; + int mode; + int exp_mode; +}; + +struct guild_alliance { + int opposition; + int guild_id; + char name[24]; +}; + +struct guild_explusion { + char name[24]; + char mes[40]; + char acc[40]; + int account_id; + int rsv1,rsv2,rsv3; +}; + +struct guild_skill { + int id,lv; +}; + +struct guild { + int guild_id; + short guild_lv, connect_member, max_member, average_lv; + int exp,next_exp,skill_point,castle_id; + char name[24],master[24]; + struct guild_member member[MAX_GUILD]; + struct guild_position position[MAX_GUILDPOSITION]; + char mes1[60],mes2[120]; + int emblem_len,emblem_id; + char emblem_data[2048]; + struct guild_alliance alliance[MAX_GUILDALLIANCE]; + struct guild_explusion explusion[MAX_GUILDEXPLUSION]; + struct guild_skill skill[MAX_GUILDSKILL]; +}; + +struct guild_castle { + int castle_id; + char map_name[24]; + char castle_name[24]; + char castle_event[24]; + int guild_id; + int economy; + int defense; + int triggerE; + int triggerD; + int nextTime; + int payTime; + int createTime; + int visibleC; + int visibleG0; + int visibleG1; + int visibleG2; + int visibleG3; + int visibleG4; + int visibleG5; + int visibleG6; + int visibleG7; + int Ghp0; // added Guardian HP [Valaris] + int Ghp1; + int Ghp2; + int Ghp3; + int Ghp4; + int Ghp5; + int Ghp6; + int Ghp7; + int GID0; + int GID1; + int GID2; + int GID3; + int GID4; + int GID5; + int GID6; + int GID7; // end addition [Valaris] +}; +struct square { + int val1[5]; + int val2[5]; +}; + +enum { + GBI_EXP =1, // ギルドのEXP + GBI_GUILDLV =2, // ギルドのLv + GBI_SKILLPOINT =3, // ギルドのスキルポイント + GBI_SKILLLV =4, // ギルドスキルLv + + GMI_POSITION =0, // メンバーの役職変更 + GMI_EXP =1, // メンバーのEXP + +}; + +#ifndef LCCWIN32 +#ifndef strcmpi +#define strcmpi strcasecmp +#endif +#ifndef stricmp +#define stricmp strcasecmp +#endif +#ifndef strncmpi +#define strncmpi strncasecmp +#endif +#ifndef strnicmp +#define strnicmp strncasecmp +#endif +#endif + +#endif // _MMO_H_ diff --git a/misc/src/common/nullpo.c b/misc/src/common/nullpo.c new file mode 100644 index 0000000..5fbf5fc --- /dev/null +++ b/misc/src/common/nullpo.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include "nullpo.h" +// #include "logs.h" // 布石してみる + +static void nullpo_info_core(const char *file, int line, const char *func, + const char *fmt, va_list ap); + +/*====================================== + * Nullチェック 及び 情報出力 + *-------------------------------------- + */ +int nullpo_chk_f(const char *file, int line, const char *func, const void *target, + const char *fmt, ...) +{ + va_list ap; + + if (target != NULL) + return 0; + + va_start(ap, fmt); + nullpo_info_core(file, line, func, fmt, ap); + va_end(ap); + return 1; +} + +int nullpo_chk(const char *file, int line, const char *func, const void *target) +{ + if (target != NULL) + return 0; + + nullpo_info_core(file, line, func, NULL, NULL); + return 1; +} + + +/*====================================== + * nullpo情報出力(外部呼出し向けラッパ) + *-------------------------------------- + */ +void nullpo_info_f(const char *file, int line, const char *func, + const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + nullpo_info_core(file, line, func, fmt, ap); + va_end(ap); +} + +void nullpo_info(const char *file, int line, const char *func) +{ + nullpo_info_core(file, line, func, NULL, NULL); +} + + +/*====================================== + * nullpo情報出力(Main) + *-------------------------------------- + */ +static void nullpo_info_core(const char *file, int line, const char *func, + const char *fmt, va_list ap) +{ + if (file == NULL) + file = "??"; + + func = + func == NULL ? "unknown": + func[0] == '\0' ? "unknown": + func; + + printf("--- nullpo info --------------------------------------------\n"); + printf("%s:%d: in func `%s'\n", file, line, func); + if (fmt != NULL) + { + if (fmt[0] != '\0') + { + vprintf(fmt, ap); + + // 最後に改行したか確認 + if (fmt[strlen(fmt)-1] != '\n') + printf("\n"); + } + } + printf("--- end nullpo info ----------------------------------------\n"); + + // ここらでnullpoログをファイルに書き出せたら + // まとめて提出できるなと思っていたり。 +} diff --git a/misc/src/common/nullpo.h b/misc/src/common/nullpo.h new file mode 100644 index 0000000..2d33500 --- /dev/null +++ b/misc/src/common/nullpo.h @@ -0,0 +1,222 @@ +#ifndef _NULLPO_H_ +#define _NULLPO_H_ + + +#define NULLPO_CHECK 1 + // 全体のスイッチを宣言しているヘッダがあれば + // そこに移動していただけると + + +#if __STDC_VERSION__ < 199901L +# if __GNUC__ >= 2 +# define __func__ __FUNCTION__ +# else +# define __func__ "" +# endif +#endif + +#ifdef LCCWIN32 +#define __attribute__(x) /* nothing */ +#endif + + +#define NLP_MARK __FILE__, __LINE__, __func__ + +/*---------------------------------------------------------------------------- + * Macros + *---------------------------------------------------------------------------- + */ +/*====================================== + * Nullチェック 及び 情報出力後 return + *・展開するとifとかreturn等が出るので + * 一行単体で使ってください。 + *・nullpo_ret(x = func()); + * のような使用法も想定しています。 + *-------------------------------------- + * nullpo_ret(t) + * 戻り値 0固定 + * [引数] + * t チェック対象 + *-------------------------------------- + * nullpo_retv(t) + * 戻り値 なし + * [引数] + * t チェック対象 + *-------------------------------------- + * nullpo_retr(ret, t) + * 戻り値 指定 + * [引数] + * ret return(ret); + * t チェック対象 + *-------------------------------------- + * nullpo_ret_f(t, fmt, ...) + * 詳細情報出力用 + * 戻り値 0 + * [引数] + * t チェック対象 + * fmt ... vprintfに渡される + * 備考や関係変数の書き出しなどに + *-------------------------------------- + * nullpo_retv_f(t, fmt, ...) + * 詳細情報出力用 + * 戻り値 なし + * [引数] + * t チェック対象 + * fmt ... vprintfに渡される + * 備考や関係変数の書き出しなどに + *-------------------------------------- + * nullpo_retr_f(ret, t, fmt, ...) + * 詳細情報出力用 + * 戻り値 指定 + * [引数] + * ret return(ret); + * t チェック対象 + * fmt ... vprintfに渡される + * 備考や関係変数の書き出しなどに + *-------------------------------------- + */ + +#if NULLPO_CHECK + +#define nullpo_ret(t) \ + if (nullpo_chk(NLP_MARK, (void *)(t))) {return(0);} + +#define nullpo_retv(t) \ + if (nullpo_chk(NLP_MARK, (void *)(t))) {return;} + +#define nullpo_retr(ret, t) \ + if (nullpo_chk(NLP_MARK, (void *)(t))) {return(ret);} + + +// 可変引数マクロに関する条件コンパイル +#if __STDC_VERSION__ >= 199901L +/* C99に対応 */ +#define nullpo_ret_f(t, fmt, ...) \ + if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return(0);} + +#define nullpo_retv_f(t, fmt, ...) \ + if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return;} + +#define nullpo_retr_f(ret, t, fmt, ...) \ + if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), __VA_ARGS__)) {return(ret);} + +#elif __GNUC__ >= 2 +/* GCC用 */ +#define nullpo_ret_f(t, fmt, args...) \ + if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return(0);} + +#define nullpo_retv_f(t, fmt, args...) \ + if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return;} + +#define nullpo_retr_f(ret, t, fmt, args...) \ + if (nullpo_chk_f(NLP_MARK, (void *)(t), (fmt), ## args)) {return(ret);} + +#else + +/* その他の場合・・・ orz */ + +#endif + +#else /* NULLPO_CHECK */ +/* No Nullpo check */ + +// if((t)){;} +// 良い方法が思いつかなかったので・・・苦肉の策です。 +// 一応ワーニングは出ないはず + +#define nullpo_ret(t) if((t)){;} +#define nullpo_retv(t) if((t)){;} +#define nullpo_retr(ret, t) if((t)){;} + +// 可変引数マクロに関する条件コンパイル +#if __STDC_VERSION__ >= 199901L +/* C99に対応 */ +#define nullpo_ret_f(t, fmt, ...) if((t)){;} +#define nullpo_retv_f(t, fmt, ...) if((t)){;} +#define nullpo_retr_f(ret, t, fmt, ...) if((t)){;} + +#elif __GNUC__ >= 2 +/* GCC用 */ +#define nullpo_ret_f(t, fmt, args...) if((t)){;} +#define nullpo_retv_f(t, fmt, args...) if((t)){;} +#define nullpo_retr_f(ret, t, fmt, args...) if((t)){;} + +#else +/* その他の場合・・・ orz */ +#endif + +#endif /* NULLPO_CHECK */ + +/*---------------------------------------------------------------------------- + * Functions + *---------------------------------------------------------------------------- + */ +/*====================================== + * nullpo_chk + * Nullチェック 及び 情報出力 + * [引数] + * file __FILE__ + * line __LINE__ + * func __func__ (関数名) + * これらには NLP_MARK を使うとよい + * target チェック対象 + * [返り値] + * 0 OK + * 1 NULL + *-------------------------------------- + */ +int nullpo_chk(const char *file, int line, const char *func, const void *target); + + +/*====================================== + * nullpo_chk_f + * Nullチェック 及び 詳細な情報出力 + * [引数] + * file __FILE__ + * line __LINE__ + * func __func__ (関数名) + * これらには NLP_MARK を使うとよい + * target チェック対象 + * fmt ... vprintfに渡される + * 備考や関係変数の書き出しなどに + * [返り値] + * 0 OK + * 1 NULL + *-------------------------------------- + */ +int nullpo_chk_f(const char *file, int line, const char *func, const void *target, + const char *fmt, ...) + __attribute__((format(printf,5,6))); + + +/*====================================== + * nullpo_info + * nullpo情報出力 + * [引数] + * file __FILE__ + * line __LINE__ + * func __func__ (関数名) + * これらには NLP_MARK を使うとよい + *-------------------------------------- + */ +void nullpo_info(const char *file, int line, const char *func); + + +/*====================================== + * nullpo_info_f + * nullpo詳細情報出力 + * [引数] + * file __FILE__ + * line __LINE__ + * func __func__ (関数名) + * これらには NLP_MARK を使うとよい + * fmt ... vprintfに渡される + * 備考や関係変数の書き出しなどに + *-------------------------------------- + */ +void nullpo_info_f(const char *file, int line, const char *func, + const char *fmt, ...) + __attribute__((format(printf,4,5))); + + +#endif diff --git a/misc/src/common/socket.c b/misc/src/common/socket.c new file mode 100644 index 0000000..1711286 --- /dev/null +++ b/misc/src/common/socket.c @@ -0,0 +1,439 @@ +// $Id: socket.c,v 1.1.1.1 2004/09/10 17:44:49 MagicalTux Exp $ +// original : core.c 2003/02/26 18:03:12 Rev 1.7 + +#include +#include +#include + +#ifdef LCCWIN32 +#define WIN32_LEAN_AND_MEAN +#include +#include +#else +#include +#include +#include +#include +#include +#endif + +#include +#include + +#include "mmo.h" // [Valaris] thanks to fov +#include "socket.h" +#include "utils.h" + +#ifdef MEMWATCH +#include "memwatch.h" +#endif + +fd_set readfds; +int fd_max; + +int rfifo_size = 65536; +int wfifo_size = 65536; + +struct socket_data *session[FD_SETSIZE]; + +static int null_parse(int fd); +static int (*default_func_parse)(int) = null_parse; + +/*====================================== + * CORE : Set function + *-------------------------------------- + */ +void set_defaultparse(int (*defaultparse)(int)) +{ + default_func_parse = defaultparse; +} + +/*====================================== + * CORE : Socket Sub Function + *-------------------------------------- + */ + +static int recv_to_fifo(int fd) +{ + int len; + + //printf("recv_to_fifo : %d %d\n",fd,session[fd]->eof); + if(session[fd]->eof) + return -1; + + +#ifdef LCCWIN32 + len = recv(fd,session[fd]->rdata+session[fd]->rdata_size, RFIFOSPACE(fd), 0); +#else + len=read(fd,session[fd]->rdata+session[fd]->rdata_size,RFIFOSPACE(fd)); +#endif + +// printf (":::RECEIVE:::\n"); +// dump(session[fd]->rdata, len); printf ("\n"); + + //{ int i; printf("recv %d : ",fd); for(i=0;irdata_size+i)); } printf("\n");} + if(len>0){ + session[fd]->rdata_size+=len; + } else if(len<=0){ + // value of connection is not necessary the same +// if (fd == 4) // Removed [Yor] +// printf("Char-Server Has Disconnected.\n"); +// else if (fd == 5) // Removed [Yor] +// printf("Attempt To Log In Successful.\n"); +// else if (fd == 7) // Removed [Yor] +// printf("Char-Server Has Disconnected.\n"); +// else if (fd == 8) // Removed [Valaris] +// printf("%s has logged off your server.\n",RFIFOP(fd,6)); // Removed [Valaris] + +// else if (fd != 8) // [Valaris] + printf("set eof : connection #%d\n", fd); + session[fd]->eof=1; + } + return 0; +} + +static int send_from_fifo(int fd) +{ + int len; + + //printf("send_from_fifo : %d\n",fd); + if(session[fd]->eof) + return -1; + +#ifdef LCCWIN32 + len = send(fd, session[fd]->wdata,session[fd]->wdata_size, 0); +#else + len=write(fd,session[fd]->wdata,session[fd]->wdata_size); +#endif + +// printf (":::SEND:::\n"); +// dump(session[fd]->wdata, len); printf ("\n"); + + //{ int i; printf("send %d : ",fd); for(i=0;iwdata[i]); } printf("\n");} + if(len>0){ + if(lenwdata_size){ + memmove(session[fd]->wdata,session[fd]->wdata+len,session[fd]->wdata_size-len); + session[fd]->wdata_size-=len; + } else { + session[fd]->wdata_size=0; + } + } else { + printf("set eof :%d\n",fd); + session[fd]->eof=1; + } + return 0; +} + +static int null_parse(int fd) +{ + printf("null_parse : %d\n",fd); + RFIFOSKIP(fd,RFIFOREST(fd)); + return 0; +} + +/*====================================== + * CORE : Socket Function + *-------------------------------------- + */ + +static int connect_client(int listen_fd) +{ + int fd; + struct sockaddr_in client_address; + int len; + int result; + int yes = 1; // reuse fix + + //printf("connect_client : %d\n",listen_fd); + + len=sizeof(client_address); + + fd=accept(listen_fd,(struct sockaddr*)&client_address,&len); + if(fd_max<=fd) fd_max=fd+1; + +// setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,NULL,0); + setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char *)&yes,sizeof yes); // reuse fix +#ifdef SO_REUSEPORT +// setsockopt(fd,SOL_SOCKET,SO_REUSEPORT,NULL,0); + setsockopt(fd,SOL_SOCKET,SO_REUSEPORT,(char *)&yes,sizeof yes); //reuse fix +#endif +// setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,NULL,0); + setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,(char *)&yes,sizeof yes); // reuse fix + + if(fd==-1){ + perror("accept"); + } else { + FD_SET(fd,&readfds); + } + +#ifdef LCCWIN32 + { + unsigned long val = 1; + ioctlsocket(fd, FIONBIO, &val); + } +#else + result = fcntl(fd, F_SETFL, O_NONBLOCK); +#endif + + CREATE(session[fd], struct socket_data, 1); + CREATE(session[fd]->rdata, char, rfifo_size); + CREATE(session[fd]->wdata, char, wfifo_size); + + session[fd]->max_rdata = rfifo_size; + session[fd]->max_wdata = wfifo_size; + session[fd]->func_recv = recv_to_fifo; + session[fd]->func_send = send_from_fifo; + session[fd]->func_parse = default_func_parse; + session[fd]->client_addr = client_address; + + //printf("new_session : %d %d\n",fd,session[fd]->eof); + return fd; +} + +int make_listen_port(int port) +{ + struct sockaddr_in server_address; + int fd; + int result; + int yes = 1; // reuse fix + + fd = socket( AF_INET, SOCK_STREAM, 0 ); + if(fd_max<=fd) fd_max=fd+1; + +#ifdef LCCWIN32 + { + unsigned long val = 1; + ioctlsocket(fd, FIONBIO, &val); + } +#else + result = fcntl(fd, F_SETFL, O_NONBLOCK); +#endif + +// setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,NULL,0); + setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char *)&yes,sizeof yes); // reuse fix +#ifdef SO_REUSEPORT +// setsockopt(fd,SOL_SOCKET,SO_REUSEPORT,NULL,0); + setsockopt(fd,SOL_SOCKET,SO_REUSEPORT,(char *)&yes,sizeof yes); //reuse fix +#endif +// setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,NULL,0); + setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,(char *)&yes,sizeof yes); // reuse fix + + server_address.sin_family = AF_INET; + server_address.sin_addr.s_addr = htonl( INADDR_ANY ); + server_address.sin_port = htons(port); + + result = bind(fd, (struct sockaddr*)&server_address, sizeof(server_address)); + if( result == -1 ) { + perror("bind"); + exit(1); + } + result = listen( fd, 5 ); + if( result == -1 ) { /* error */ + perror("listen"); + exit(1); + } + + FD_SET(fd, &readfds ); + + CREATE(session[fd], struct socket_data, 1); + + if(session[fd]==NULL){ + printf("out of memory : make_listen_port\n"); + exit(1); + } + memset(session[fd],0,sizeof(*session[fd])); + session[fd]->func_recv = connect_client; + + return fd; +} + +int make_connection(long ip,int port) +{ + struct sockaddr_in server_address; + int fd; + int result; + int yes = 1; // reuse fix + + fd = socket( AF_INET, SOCK_STREAM, 0 ); + if(fd_max<=fd) fd_max=fd+1; +// setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,NULL,0); + setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char *)&yes,sizeof yes); // reuse fix +#ifdef SO_REUSEPORT +// setsockopt(fd,SOL_SOCKET,SO_REUSEPORT,NULL,0); + setsockopt(fd,SOL_SOCKET,SO_REUSEPORT,(char *)&yes,sizeof yes); //reuse fix +#endif +// setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,NULL,0); + setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,(char *)&yes,sizeof yes); // reuse fix + + server_address.sin_family = AF_INET; + server_address.sin_addr.s_addr = ip; + server_address.sin_port = htons(port); + +#ifdef LCCWIN32 + { + unsigned long val = 1; + ioctlsocket(fd, FIONBIO, &val); + } +#else + result = fcntl(fd, F_SETFL, O_NONBLOCK); +#endif + + result = connect(fd, (struct sockaddr *)(&server_address),sizeof(struct sockaddr_in)); + + FD_SET(fd,&readfds); + + CREATE(session[fd], struct socket_data, 1); + CREATE(session[fd]->rdata, char, rfifo_size); + CREATE(session[fd]->wdata, char, wfifo_size); + + session[fd]->max_rdata = rfifo_size; + session[fd]->max_wdata = wfifo_size; + session[fd]->func_recv = recv_to_fifo; + session[fd]->func_send = send_from_fifo; + session[fd]->func_parse = default_func_parse; + + return fd; +} + +int delete_session(int fd) +{ + if(fd<0 || fd>=FD_SETSIZE) + return -1; + FD_CLR(fd,&readfds); + if(session[fd]){ + if(session[fd]->rdata) + free(session[fd]->rdata); + if(session[fd]->wdata) + free(session[fd]->wdata); + if(session[fd]->session_data) + free(session[fd]->session_data); + free(session[fd]); + } + session[fd]=NULL; + //printf("delete_session:%d\n",fd); + return 0; +} + +int realloc_fifo(int fd,int rfifo_size,int wfifo_size) +{ + struct socket_data *s=session[fd]; + if( s->max_rdata != rfifo_size && s->rdata_size < rfifo_size){ + RECREATE(s->rdata, char, rfifo_size); + s->max_rdata = rfifo_size; + } + if( s->max_wdata != wfifo_size && s->wdata_size < wfifo_size){ + RECREATE(s->wdata, char, wfifo_size); + s->max_wdata = wfifo_size; + } + return 0; +} + +int WFIFOSET(int fd,int len) +{ + struct socket_data *s=session[fd]; + if( s->wdata_size+len+16384 > s->max_wdata ){ + realloc_fifo(fd,s->max_rdata, s->max_wdata <<1 ); + printf("socket: %d wdata expanded to %d bytes.\n",fd, s->max_wdata); + } + s->wdata_size=(s->wdata_size+(len)+2048 < s->max_wdata) ? + s->wdata_size+len : (printf("socket: %d wdata lost !!\n",fd),s->wdata_size); + return 0; +} + +int do_sendrecv(int next) +{ + fd_set rfd,wfd; + struct timeval timeout; + int ret,i; + + rfd=readfds; + FD_ZERO(&wfd); + for(i=0;iwdata_size) + FD_SET(i,&wfd); + } + timeout.tv_sec = next/1000; + timeout.tv_usec = next%1000*1000; + ret = select(fd_max,&rfd,&wfd,NULL,&timeout); + if(ret<=0) + return 0; + for(i=0;ifunc_send) + //send_from_fifo(i); + session[i]->func_send(i); + } + if(FD_ISSET(i,&rfd)){ + //printf("read:%d\n",i); + if(session[i]->func_recv) + //recv_to_fifo(i); + session[i]->func_recv(i); + } + } + return 0; +} + +int do_parsepacket(void) +{ + int i; + for(i=0;irdata_size==0 && session[i]->eof==0) + continue; + if(session[i]->func_parse){ + session[i]->func_parse(i); + if(!session[i]) + continue; + } + RFIFOFLUSH(i); + } + return 0; +} + +void do_socket(void) +{ + FD_ZERO(&readfds); +} + +int RFIFOSKIP(int fd,int len) +{ + struct socket_data *s=session[fd]; + + if (s->rdata_size-s->rdata_pos-len<0) { + fprintf(stderr,"too many skip\n"); + exit(1); + } + + s->rdata_pos = s->rdata_pos+len; + + return 0; +} + + +int Net_Init(void) +{ + #ifdef LCCWIN32 + /* Start up the windows networking */ + WORD version_wanted = MAKEWORD(1,1); + WSADATA wsaData; + + if ( WSAStartup(version_wanted, &wsaData) != 0 ) { + printf("SYSERR: WinSock not available!\n"); + exit(1); + } + #endif + + return(0); +} + diff --git a/misc/src/common/socket.h b/misc/src/common/socket.h new file mode 100644 index 0000000..fe06e40 --- /dev/null +++ b/misc/src/common/socket.h @@ -0,0 +1,96 @@ +// original : core.h 2003/03/14 11:55:25 Rev 1.4 + +#ifndef _SOCKET_H_ +#define _SOCKET_H_ + +#include + +#ifdef LCCWIN32 +#include +#else +#include +#include +#include +#endif + +// define declaration + +#define RFIFOP(fd,pos) (session[fd]->rdata+session[fd]->rdata_pos+(pos)) +#define RFIFOB(fd,pos) (*(unsigned char*)(session[fd]->rdata+session[fd]->rdata_pos+(pos))) +#define RFIFOW(fd,pos) (*(unsigned short*)(session[fd]->rdata+session[fd]->rdata_pos+(pos))) +#define RFIFOL(fd,pos) (*(unsigned int*)(session[fd]->rdata+session[fd]->rdata_pos+(pos))) +//#define RFIFOSKIP(fd,len) ((session[fd]->rdata_size-session[fd]->rdata_pos-(len)<0) ? (fprintf(stderr,"too many skip\n"),exit(1)) : (session[fd]->rdata_pos+=(len))) +#define RFIFOREST(fd) (session[fd]->rdata_size-session[fd]->rdata_pos) +#define RFIFOFLUSH(fd) (memmove(session[fd]->rdata,RFIFOP(fd,0),RFIFOREST(fd)),session[fd]->rdata_size=RFIFOREST(fd),session[fd]->rdata_pos=0) +#define RFIFOSPACE(fd) (session[fd]->max_rdata-session[fd]->rdata_size) +#define RBUFP(p,pos) (((unsigned char*)(p))+(pos)) +#define RBUFB(p,pos) (*(unsigned char*)RBUFP((p),(pos))) +#define RBUFW(p,pos) (*(unsigned short*)RBUFP((p),(pos))) +#define RBUFL(p,pos) (*(unsigned int*)RBUFP((p),(pos))) + +#define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size) +#define WFIFOP(fd,pos) (session[fd]->wdata+session[fd]->wdata_size+(pos)) +#define WFIFOB(fd,pos) (*(unsigned char*)(session[fd]->wdata+session[fd]->wdata_size+(pos))) +#define WFIFOW(fd,pos) (*(unsigned short*)(session[fd]->wdata+session[fd]->wdata_size+(pos))) +#define WFIFOL(fd,pos) (*(unsigned int*)(session[fd]->wdata+session[fd]->wdata_size+(pos))) +// use function instead of macro. +//#define WFIFOSET(fd,len) (session[fd]->wdata_size = (session[fd]->wdata_size+(len)+2048 < session[fd]->max_wdata) ? session[fd]->wdata_size+len : session[fd]->wdata_size) +#define WBUFP(p,pos) (((unsigned char*)(p))+(pos)) +#define WBUFB(p,pos) (*(unsigned char*)WBUFP((p),(pos))) +#define WBUFW(p,pos) (*(unsigned short*)WBUFP((p),(pos))) +#define WBUFL(p,pos) (*(unsigned int*)WBUFP((p),(pos))) + +#ifdef __INTERIX +#define FD_SETSIZE 4096 +#endif // __INTERIX + + +/* Removed Cygwin FD_SETSIZE declarations, now are directly passed on to the compiler through Makefile [Valaris] */ + +// Struct declaration + +struct socket_data{ + int eof; + unsigned char *rdata,*wdata; + int max_rdata,max_wdata; + int rdata_size,wdata_size; + int rdata_pos; + struct sockaddr_in client_addr; + int (*func_recv)(int); + int (*func_send)(int); + int (*func_parse)(int); + void* session_data; +}; + +// Data prototype declaration + +#ifdef LCCWIN32 + + #undef FD_SETSIZE + #define FD_SETSIZE 4096 + +#endif + +extern struct socket_data *session[FD_SETSIZE]; + +extern int rfifo_size,wfifo_size; +extern int fd_max; + +// Function prototype declaration + +int make_listen_port(int); +int make_connection(long,int); +int delete_session(int); +int realloc_fifo(int fd,int rfifo_size,int wfifo_size); +int WFIFOSET(int fd,int len); +int RFIFOSKIP(int fd,int len); + +int do_sendrecv(int next); +int do_parsepacket(void); +void do_socket(void); + +void set_defaultparse(int (*defaultparse)(int)); + +int Net_Init(void); + +#endif // _SOCKET_H_ diff --git a/misc/src/common/timer.c b/misc/src/common/timer.c new file mode 100644 index 0000000..8193ff9 --- /dev/null +++ b/misc/src/common/timer.c @@ -0,0 +1,312 @@ +// $Id: timer.c,v 1.1.1.1 2004/09/10 17:44:49 MagicalTux Exp $ +// original : core.c 2003/02/26 18:03:12 Rev 1.7 + +#include +#include +#include +#include + +#ifdef LCCWIN32 +#include +#else +#include +#include +#endif + +#include "timer.h" +#include "utils.h" + +#ifdef MEMWATCH +#include "memwatch.h" +#endif + +static struct TimerData* timer_data; +static int timer_data_max,timer_data_num; +static int* free_timer_list; +static int free_timer_list_max, free_timer_list_pos; + +static int timer_heap_max; +static int* timer_heap = NULL; + +// for debug +struct timer_func_list { + int (*func)(int,unsigned int,int,int); + struct timer_func_list* next; + char* name; +}; +static struct timer_func_list* tfl_root; + +#if defined(LCCWIN32) +void gettimeofday(struct timeval *t, struct timezone *dummy) +{ + DWORD millisec = GetTickCount(); + + t->tv_sec = (int) (millisec / 1000); + t->tv_usec = (millisec % 1000) * 1000; +} + +#endif + + +// +int add_timer_func_list(int (*func)(int,unsigned int,int,int),char* name) +{ + struct timer_func_list* tfl; + + CREATE(tfl, struct timer_func_list, 1); + CREATE(tfl->name, char, strlen(name) + 1); + + tfl->next = tfl_root; + tfl->func = func; + strcpy(tfl->name,name); + tfl_root = tfl; + + return 0; +} + +char* search_timer_func_list(int (*func)(int,unsigned int,int,int)) +{ + struct timer_func_list* tfl; + for(tfl = tfl_root;tfl;tfl = tfl->next) { + if (func == tfl->func) + return tfl->name; + } + return "???"; +} + +/*---------------------------- + * Get tick time + *----------------------------*/ +static unsigned int gettick_cache; +static int gettick_count; +unsigned int gettick_nocache(void) +{ + struct timeval tval; + gettimeofday(&tval,NULL); + gettick_count = 256; + return gettick_cache = tval.tv_sec * 1000 + tval.tv_usec/1000; +} + +unsigned int gettick(void) +{ + gettick_count--; + if (gettick_count<0) + return gettick_nocache(); + return gettick_cache; +} + +/*====================================== + * CORE : Timer Heap + *-------------------------------------- + */ +static void push_timer_heap(int index) +{ + int i, h; + + if (timer_heap == NULL || timer_heap[0] + 1 >= timer_heap_max) { + int first = timer_heap == NULL; + + timer_heap_max += 256; + RECREATE(timer_heap, int, timer_heap_max); + memset(timer_heap + (timer_heap_max - 256), 0, sizeof(int) * 256); + if (first) + timer_heap[0] = 0; + } + + timer_heap[0]++; + + for (h = timer_heap[0]-1, i = (h - 1) / 2; + h > 0 && DIFF_TICK(timer_data[index].tick, + timer_data[timer_heap[i + 1]].tick) < 0; + i = (h - 1) / 2) { + timer_heap[h + 1] = timer_heap[i + 1]; + h = i; + } + timer_heap[h + 1] = index; +} + +static int top_timer_heap() +{ + if (timer_heap == NULL || timer_heap[0] <= 0) + return -1; + + return timer_heap[1]; +} + +static int pop_timer_heap() +{ + int i,h,k; + int ret,last; + + if (timer_heap == NULL || timer_heap[0] <= 0) + return -1; + ret = timer_heap[1]; + last = timer_heap[timer_heap[0]]; + timer_heap[0]--; + + for(h = 0,k = 2;k0) + k--; + timer_heap[h + 1] = timer_heap[k + 1], h = k; + } + if (k == timer_heap[0]) + timer_heap[h + 1] = timer_heap[k], h = k-1; + + for(i = (h-1)/2; + h>0 && DIFF_TICK(timer_data[timer_heap[i + 1]].tick , timer_data[last].tick)>0; + i = (h-1)/2) { + timer_heap[h + 1] = timer_heap[i + 1],h = i; + } + timer_heap[h + 1] = last; + + return ret; +} + +int add_timer(unsigned int tick,int (*func)(int,unsigned int,int,int),int id,int data) +{ + struct TimerData* td; + int i; + + if (free_timer_list_pos) { + do { + i = free_timer_list[--free_timer_list_pos]; + } while(i >= timer_data_num && free_timer_list_pos > 0); + } else + i = timer_data_num; + if (i >= timer_data_num) + for (i = timer_data_num;i= timer_data_num && i >= timer_data_max) { + int j; + if (timer_data_max == 0) { + timer_data_max = 256; + CREATE(timer_data, struct TimerData, timer_data_max); + } else { + timer_data_max += 256; + RECREATE(timer_data, struct TimerData, timer_data_max); + if (timer_data == NULL) { + printf("out of memory : add_timer timer_data\n"); + exit(1); + } + memset(timer_data + (timer_data_max - 256), 0, + sizeof(struct TimerData) * 256); + } + for(j = timer_data_max-256;jtick = tick; + td->func = func; + td->id = id; + td->data = data; + td->type = TIMER_ONCE_AUTODEL; + td->interval = 1000; + push_timer_heap(i); + if (i >= timer_data_num) + timer_data_num = i + 1; + return i; +} + +int add_timer_interval(unsigned int tick,int (*func)(int,unsigned int,int,int),int id,int data,int interval) +{ + int tid; + tid = add_timer(tick,func,id,data); + timer_data[tid].type = TIMER_INTERVAL; + timer_data[tid].interval = interval; + return tid; +} + +int delete_timer(int id,int (*func)(int,unsigned int,int,int)) +{ + if (id <= 0 || id >= timer_data_num) { + printf("delete_timer error : no such timer %d\n", id); + return -1; + } + if (timer_data[id].func != func) { + printf("delete_timer error : function dismatch %08x(%s) != %08x(%s)\n", + (int)timer_data[id].func, + search_timer_func_list(timer_data[id].func), + (int)func, + search_timer_func_list(func)); + return -2; + } + // そのうち消えるにまかせる + timer_data[id].func = NULL; + timer_data[id].type = TIMER_ONCE_AUTODEL; + timer_data[id].tick -= 60 * 60 * 1000; + return 0; +} + +int addtick_timer(int tid,unsigned int tick) +{ + return timer_data[tid].tick += tick; +} +struct TimerData* get_timer(int tid) +{ + return &timer_data[tid]; +} + + +int do_timer(unsigned int tick) +{ + int i,nextmin = 1000; + +#if 0 + static int disp_tick = 0; + if (DIFF_TICK(disp_tick,tick)<-5000 || DIFF_TICK(disp_tick,tick)>5000) { + printf("timer %d(%d + %d)\n",timer_data_num,timer_heap[0],free_timer_list_pos); + disp_tick = tick; + } +#endif + + while((i = top_timer_heap()) >= 0) { + if (DIFF_TICK(timer_data[i].tick , tick)>0) { + nextmin = DIFF_TICK(timer_data[i].tick , tick); + break; + } + pop_timer_heap(); + timer_data[i].type |= TIMER_REMOVE_HEAP; + if (timer_data[i].func) { + if (DIFF_TICK(timer_data[i].tick , tick) < -1000) { + // 1秒以上の大幅な遅延が発生しているので、 + // timer処理タイミングを現在値とする事で + // 呼び出し時タイミング(引数のtick)相対で処理してる + // timer関数の次回処理タイミングを遅らせる + timer_data[i].func(i,tick,timer_data[i].id,timer_data[i].data); + } else { + timer_data[i].func(i,timer_data[i].tick,timer_data[i].id,timer_data[i].data); + } + } + if (timer_data[i].type&TIMER_REMOVE_HEAP) { + switch(timer_data[i].type & ~TIMER_REMOVE_HEAP) { + case TIMER_ONCE_AUTODEL: + timer_data[i].type = 0; + if (free_timer_list_pos >= free_timer_list_max) { + free_timer_list_max += 256; + RECREATE(free_timer_list, int, free_timer_list_max); + memset(free_timer_list + (free_timer_list_max - 256), 0, + 256 * sizeof(free_timer_list[0])); + } + free_timer_list[free_timer_list_pos++] = i; + break; + case TIMER_INTERVAL: + if (DIFF_TICK(timer_data[i].tick , tick) < -1000) { + timer_data[i].tick = tick + timer_data[i].interval; + } else { + timer_data[i].tick += timer_data[i].interval; + } + timer_data[i].type &= ~TIMER_REMOVE_HEAP; + push_timer_heap(i); + break; + } + } + } + + if (nextmin<10) + nextmin = 10; + return nextmin; +} + +void timer_final() +{ + free(timer_data); +} diff --git a/misc/src/common/timer.h b/misc/src/common/timer.h new file mode 100644 index 0000000..f6fc5c8 --- /dev/null +++ b/misc/src/common/timer.h @@ -0,0 +1,45 @@ +// original : core.h 2003/03/14 11:55:25 Rev 1.4 + +#ifndef _TIMER_H_ +#define _TIMER_H_ + +#define BASE_TICK 5 + +#define TIMER_ONCE_AUTODEL 1 +#define TIMER_INTERVAL 2 +#define TIMER_REMOVE_HEAP 16 + +#define DIFF_TICK(a,b) ((int)((a)-(b))) + +// Struct declaration + +struct TimerData { + unsigned int tick; + int (*func)(int,unsigned int,int,int); + int id; + int data; + int type; + int interval; + int heap_pos; +}; + +// Function prototype declaration + +unsigned int gettick_nocache(void); +unsigned int gettick(void); + +int add_timer(unsigned int,int (*)(int,unsigned int,int,int),int,int); +int add_timer_interval(unsigned int,int (*)(int,unsigned int,int,int),int,int,int); +int delete_timer(int,int (*)(int,unsigned int,int,int)); + +int addtick_timer(int tid,unsigned int tick); +struct TimerData *get_timer(int tid); + +int do_timer(unsigned int tick); + +int add_timer_func_list(int (*)(int,unsigned int,int,int),char*); +char* search_timer_func_list(int (*)(int,unsigned int,int,int)); + +extern void timer_final(); + +#endif // _TIMER_H_ diff --git a/misc/src/common/utils.c b/misc/src/common/utils.c new file mode 100644 index 0000000..b0ecd26 --- /dev/null +++ b/misc/src/common/utils.c @@ -0,0 +1,108 @@ +#include +#include "utils.h" +#include + +void dump(unsigned char *buffer, int num) +{ + int icnt,jcnt; + + printf(" Hex ASCII\n"); + printf(" ----------------------------------------------- ----------------"); + + for (icnt=0;icnt 31 && buffer[jcnt] < 127) + printf("%c",buffer[jcnt]); + else + printf("."); + } else + printf(" "); + } + } + printf("\n"); +} + + +#ifdef LCCWIN32 +char *rindex(char *str, char c) +{ + char *sptr; + + sptr = str; + while(*sptr) + ++sptr; + if (c == '\0') + return(sptr); + while(str != sptr) + if (*sptr-- == c) + return(++sptr); + return(NULL); +} + +int strcasecmp(const char *arg1, const char *arg2) +{ + int chk, i; + + if (arg1 == NULL || arg2 == NULL) { + printf("SYSERR: str_cmp() passed a NULL pointer, %p or %p.\n", arg1, arg2); + return (0); + } + + for (i = 0; arg1[i] || arg2[i]; i++) + if ((chk = LOWER(arg1[i]) - LOWER(arg2[i])) != 0) + return (chk); /* not equal */ + + return (0); +} + +int strncasecmp(const char *arg1, const char *arg2, int n) +{ + int chk, i; + + if (arg1 == NULL || arg2 == NULL) { + printf("SYSERR: strn_cmp() passed a NULL pointer, %p or %p.\n", arg1, arg2); + return (0); + } + + for (i = 0; (arg1[i] || arg2[i]) && (n > 0); i++, n--) + if ((chk = LOWER(arg1[i]) - LOWER(arg2[i])) != 0) + return (chk); /* not equal */ + + return (0); +} + +void str_upper(char *name) +{ + + int len = strlen(name); + while (len--) { + if (*name >= 'a' && *name <= 'z') + *name -= ('a' - 'A'); + name++; + } +} + +void str_lower(char *name) +{ + int len = strlen(name); + + while (len--) { + if (*name >= 'A' && *name <= 'Z') + *name += ('a' - 'A'); + name++; + } +} + +#endif + diff --git a/misc/src/common/utils.h b/misc/src/common/utils.h new file mode 100644 index 0000000..29463cf --- /dev/null +++ b/misc/src/common/utils.h @@ -0,0 +1,33 @@ + +#ifndef NULL +#define NULL (void *)0 +#endif + +#define LOWER(c) (((c)>='A' && (c) <= 'Z') ? ((c)+('a'-'A')) : (c)) +#define UPPER(c) (((c)>='a' && (c) <= 'z') ? ((c)+('A'-'a')) : (c) ) + +/* strcasecmp -> stricmp -> str_cmp */ + + +#ifdef LCCWIN32 + int strcasecmp(const char *arg1, const char *arg2); + int strncasecmp(const char *arg1, const char *arg2, int n); + void str_upper(char *name); + void str_lower(char *name); + char *rindex(char *str, char c); +#endif + + + void dump(unsigned char *buffer, int num); + + +#define CREATE(result, type, number) do {\ + if ((number) * sizeof(type) <= 0) \ + printf("SYSERR: Zero bytes or less requested at %s:%d.\n", __FILE__, __LINE__); \ + if (!((result) = (type *) calloc ((number), sizeof(type)))) \ + { perror("SYSERR: malloc failure"); abort(); } } while(0) + +#define RECREATE(result,type,number) do {\ + if (!((result) = (type *) realloc ((result), sizeof(type) * (number))))\ + { printf("SYSERR: realloc failure"); abort(); } } while(0) + diff --git a/misc/src/common/version.h b/misc/src/common/version.h new file mode 100644 index 0000000..e33e2b3 --- /dev/null +++ b/misc/src/common/version.h @@ -0,0 +1,27 @@ +// $Id: version.h,v 1.2 2004/09/22 09:49:06 PoW Exp $ +#ifndef _VERSION_H_ +#define _VERSION_H_ + +#define ATHENA_MAJOR_VERSION 1 // Major Version +#define ATHENA_MINOR_VERSION 0 // Minor Version +#define ATHENA_REVISION 0 // Revision + +#define ATHENA_RELEASE_FLAG 1 // 1=Develop,0=Stable +#define ATHENA_OFFICIAL_FLAG 1 // 1=Mod,0=Official + +#define ATHENA_SERVER_LOGIN 1 // login server +#define ATHENA_SERVER_CHAR 2 // char server +#define ATHENA_SERVER_INTER 4 // inter server +#define ATHENA_SERVER_MAP 8 // map server + +// ATHENA_MOD_VERSIONはパッチ番号です。 +// これは無理に変えなくても気が向いたら変える程度の扱いで。 +// (毎回アップロードの度に変更するのも面倒と思われるし、そもそも +//  この項目を参照する人がいるかどうかで疑問だから。) +// その程度の扱いなので、サーバーに問い合わせる側も、あくまで目安程度の扱いで +// あんまり信用しないこと。 +// 鯖snapshotの時や、大きな変更があった場合は設定してほしいです。 +// C言語の仕様上、最初に0を付けると8進数になるので間違えないで下さい。 +#define ATHENA_MOD_VERSION 1052 // mod version (patch No.) + +#endif diff --git a/misc/src/ladmin/GNUmakefile b/misc/src/ladmin/GNUmakefile new file mode 100644 index 0000000..ce19d9d --- /dev/null +++ b/misc/src/ladmin/GNUmakefile @@ -0,0 +1,14 @@ +all: ladmin +txt: ladmin +sql: ladmin + +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o ../common/malloc.o +COMMON_H = ../common/core.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/version.h ../common/db.h ../common/malloc.h + +ladmin: ladmin.o md5calc.o $(COMMON_OBJ) + $(CC) -o ../../$@ ladmin.o md5calc.o $(COMMON_OBJ) +ladmin.o: ladmin.c ladmin.h md5calc.h $(COMMON_H) +md5calc.o: md5calc.c md5calc.h + +clean: + rm -f *.o ../../ladmin diff --git a/misc/src/ladmin/Makefile b/misc/src/ladmin/Makefile new file mode 100644 index 0000000..ce19d9d --- /dev/null +++ b/misc/src/ladmin/Makefile @@ -0,0 +1,14 @@ +all: ladmin +txt: ladmin +sql: ladmin + +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o ../common/malloc.o +COMMON_H = ../common/core.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/version.h ../common/db.h ../common/malloc.h + +ladmin: ladmin.o md5calc.o $(COMMON_OBJ) + $(CC) -o ../../$@ ladmin.o md5calc.o $(COMMON_OBJ) +ladmin.o: ladmin.c ladmin.h md5calc.h $(COMMON_H) +md5calc.o: md5calc.c md5calc.h + +clean: + rm -f *.o ../../ladmin diff --git a/misc/src/ladmin/ladmin.c b/misc/src/ladmin/ladmin.c new file mode 100644 index 0000000..497f3bd --- /dev/null +++ b/misc/src/ladmin/ladmin.c @@ -0,0 +1,4385 @@ +// $Id: ladmin.c,v 1.1.1.1 2004/09/10 17:26:52 MagicalTux Exp $ +/////////////////////////////////////////////////////////////////////////// +// EAthena login-server remote administration tool +// Ladamin in C by [Yor] +// if you modify this software, modify ladmin in tool too. +/////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include +#include // gettimeofday +#include +#include +#include // close +#include +#include +#include // str* +#include // inet_addr +#include // gethostbyname +#include // valist +#include // tolower + +#include "core.h" +#include "socket.h" +#include "ladmin.h" +#include "version.h" +#include "mmo.h" + +#ifdef PASSWORDENC +#include "md5calc.h" +#endif + +#ifdef MEMWATCH +#include "memwatch.h" +#endif + +//-------------------------------INSTRUCTIONS------------------------------ +// Set the variables below: +// IP of the login server. +// Port where the login-server listens incoming packets. +// Password of administration (same of config_athena.conf). +// Displayed language of the sofware (if not correct, english is used). +// IMPORTANT: +// Be sure that you authorize remote administration in login-server +// (see login_athena.conf, 'admin_state' parameter) +//------------------------------------------------------------------------- +char loginserverip[16] = "127.0.0.1"; // IP of login-server +int loginserverport = 6900; // Port of login-server +char loginserveradminpassword[24] = "admin"; // Administration password +#ifdef PASSWORDENC +int passenc = 2; // Encoding type of the password +#else +int passenc = 0; // Encoding type of the password +#endif +char defaultlanguage = 'E'; // Default language (F: Fran軋is/E: English) + // (if it's not 'F', default is English) +char ladmin_log_filename[1024] = "log/ladmin.log"; +char date_format[32] = "%Y-%m-%d %H:%M:%S"; +//------------------------------------------------------------------------- +// LIST of COMMANDs that you can type at the prompt: +// To use these commands you can only type only the first letters. +// You must type a minimum of letters (you can not type 'a', +// because ladmin doesn't know if it's for 'aide' or for 'add') +// q <= quit, li <= list, pass <= passwd, etc. +// +// Note: every time you must give a account_name, you can use "" or '' (spaces can be included) +// +// aide/help/? +// Display the description of the commands +// aide/help/? [command] +// Display the description of the specified command +// +// add +// Create an account with the default email (a@a.com). +// Concerning the sex, only the first letter is used (F or M). +// The e-mail is set to a@a.com (default e-mail). It's like to have no e-mail. +// When the password is omitted, the input is done without displaying of the pressed keys. +// add testname Male testpass +// +// ban/banish yyyy/mm/dd hh:mm:ss +// Changes the final date of a banishment of an account. +// Like banset, but is at end. +// +// banadd +// Adds or substracts time from the final date of a banishment of an account. +// Modifier is done as follows: +// Adjustment value (-1, 1, +1, etc...) +// Modified element: +// a or y: year +// m: month +// j or d: day +// h: hour +// mn: minute +// s: second +// banadd testname +1m-2mn1s-6y +// this example adds 1 month and 1 second, and substracts 2 minutes and 6 years at the same time. +// NOTE: If you modify the final date of a non-banished account, +// you fix the final date to (actual time +- adjustments) +// +// banset yyyy/mm/dd [hh:mm:ss] +// Changes the final date of a banishment of an account. +// Default time [hh:mm:ss]: 23:59:59. +// banset 0 +// Set a non-banished account (0 = unbanished). +// +// block +// Set state 5 (You have been blocked by the GM Team) to an account. +// Like state 5. +// +// check +// Check the validity of a password for an account +// NOTE: Server will never sends back a password. +// It's the only method you have to know if a password is correct. +// The other method is to have a ('physical') access to the accounts file. +// +// create +// Like the 'add' command, but with e-mail moreover. +// create testname Male my@mail.com testpass +// +// del +// Remove an account. +// This order requires confirmation. After confirmation, the account is deleted. +// +// email +// Modify the e-mail of an account. +// +// getcount +// Give the number of players online on all char-servers. +// +// gm [GM_level] +// Modify the GM level of an account. +// Default value remove GM level (GM level = 0). +// gm testname 80 +// +// id +// Give the id of an account. +// +// info +// Display complete information of an account. +// +// kami +// Sends a broadcast message on all map-server (in yellow). +// kamib +// Sends a broadcast message on all map-server (in blue). +// +// language +// Change the language of displaying. +// +// list/ls [start_id [end_id]] +// Display a list of accounts. +// 'start_id', 'end_id': indicate end and start identifiers. +// Research by name is not possible with this command. +// list 10 9999999 +// +// listBan/lsBan [start_id [end_id]] +// Like list/ls, but only for accounts with state or banished +// +// listGM/lsGM [start_id [end_id]] +// Like list/ls, but only for GM accounts +// +// listOK/lsOK [start_id [end_id]] +// Like list/ls, but only for accounts without state and not banished +// +// memo +// Modify the memo of an account. +// 'memo': it can have until 253 characters (with spaces or not). +// +// name +// Give the name of an account. +// +// passwd +// Change the password of an account. +// When new password is omitted, the input is done without displaying of the pressed keys. +// +// quit/end/exit +// End of the program of administration +// +// reloadGM +// Reload GM configuration file +// +// search +// Seek accounts. +// Displays the accounts whose names correspond. +// search -r/-e/--expr/--regex +// Seek accounts by regular expression. +// Displays the accounts whose names correspond. +// +// sex +// Modify the sex of an account. +// sex testname Male +// +// state +// Change the state of an account. +// 'new_state': state is the state of the packet 0x006a + 1. The possibilities are: +// 0 = Account ok 6 = Your Game's EXE file is not the latest version +// 1 = Unregistered ID 7 = You are Prohibited to log in until %s +// 2 = Incorrect Password 8 = Server is jammed due to over populated +// 3 = This ID is expired 9 = No MSG +// 4 = Rejected from Server 100 = This ID has been totally erased +// 5 = You have been blocked by the GM Team +// all other values are 'No MSG', then use state 9 please. +// 'error_message_#7': message of the code error 6 = Your are Prohibited to log in until %s (packet 0x006a) +// +// timeadd +// Adds or substracts time from the validity limit of an account. +// Modifier is done as follows: +// Adjustment value (-1, 1, +1, etc...) +// Modified element: +// a or y: year +// m: month +// j or d: day +// h: hour +// mn: minute +// s: second +// timeadd testname +1m-2mn1s-6y +// this example adds 1 month and 1 second, and substracts 2 minutes and 6 years at the same time. +// NOTE: You can not modify a unlimited validity limit. +// If you want modify it, you want probably create a limited validity limit. +// So, at first, you must set the validity limit to a date/time. +// +// timeset yyyy/mm/dd [hh:mm:ss] +// Changes the validity limit of an account. +// Default time [hh:mm:ss]: 23:59:59. +// timeset 0 +// Gives an unlimited validity limit (0 = unlimited). +// +// unban/unbanish +// Unban an account. +// Like banset 0. +// +// unblock +// Set state 0 (Account ok) to an account. +// Like state 0. +// +// version +// Display the version of the login-server. +// +// who +// Displays complete information of an account. +// +//------------------------------------------------------------------------- +int login_fd; +int login_ip; +int bytes_to_read = 0; // flag to know if we waiting bytes from login-server +char command[1024]; +char parameters[1024]; +int list_first, list_last, list_type, list_count; // parameter to display a list of accounts +int already_exit_function = 0; // sometimes, the exit function is called twice... so, don't log twice the message + +//------------------------------ +// Writing function of logs file +//------------------------------ +int ladmin_log(char *fmt, ...) { + FILE *logfp; + va_list ap; + struct timeval tv; + char tmpstr[2048]; + + va_start(ap, fmt); + + logfp = fopen(ladmin_log_filename, "a"); + if (logfp) { + if (fmt[0] == '\0') // jump a line if no message + fprintf(logfp, RETCODE); + else { + gettimeofday(&tv, NULL); + strftime(tmpstr, 24, date_format, localtime(&(tv.tv_sec))); + sprintf(tmpstr + strlen(tmpstr), ".%03d: %s", (int)tv.tv_usec / 1000, fmt); + vfprintf(logfp, tmpstr, ap); + } + fclose(logfp); + } + + va_end(ap); + return 0; +} + +//----------------------------------------------------- +// Function to suppress control characters in a string. +//----------------------------------------------------- +int remove_control_chars(unsigned char *str) { + int i; + int change = 0; + + for(i = 0; str[i]; i++) { + if (str[i] < 32) { + str[i] = '_'; + change = 1; + } + } + + return change; +} + +//--------------------------------------------- +// Function to return ordonal text of a number. +//--------------------------------------------- +char* makeordinal(int number) { + if (defaultlanguage == 'F') { + if (number == 0) + return ""; + else if (number == 1) + return "er"; + else + return "鑪e"; + } else { + if ((number % 10) < 4 && (number % 10) != 0 && (number < 10 || number > 20)) { + if ((number % 10) == 1) + return "st"; + else if ((number % 10) == 2) + return "nd"; + else + return "rd"; + } else { + return "th"; + } + } + return ""; +} + +//----------------------------------------------------------------------------------------- +// Function to test of the validity of an account name (return 0 if incorrect, and 1 if ok) +//----------------------------------------------------------------------------------------- +int verify_accountname(char* account_name) { + int i; + + for(i = 0; account_name[i]; i++) { + if (account_name[i] < 32) { + if (defaultlanguage == 'F') { + printf("Caract鑽e interdit trouv dans le nom du compte (%d%s caract鑽e).\n", i+1, makeordinal(i+1)); + ladmin_log("Caract鑽e interdit trouv dans le nom du compte (%d%s caract鑽e)." RETCODE, i+1, makeordinal(i+1)); + } else { + printf("Illegal character found in the account name (%d%s character).\n", i+1, makeordinal(i+1)); + ladmin_log("Illegal character found in the account name (%d%s character)." RETCODE, i+1, makeordinal(i+1)); + } + return 0; + } + } + + if (strlen(account_name) < 4) { + if (defaultlanguage == 'F') { + printf("Nom du compte trop court. Entrez un nom de compte de 4-23 caract鑽es.\n"); + ladmin_log("Nom du compte trop court. Entrez un nom de compte de 4-23 caract鑽es." RETCODE); + } else { + printf("Account name is too short. Please input an account name of 4-23 bytes.\n"); + ladmin_log("Account name is too short. Please input an account name of 4-23 bytes." RETCODE); + } + return 0; + } + + if (strlen(account_name) > 23) { + if (defaultlanguage == 'F') { + printf("Nom du compte trop long. Entrez un nom de compte de 4-23 caract鑽es.\n"); + ladmin_log("Nom du compte trop long. Entrez un nom de compte de 4-23 caract鑽es." RETCODE); + } else { + printf("Account name is too long. Please input an account name of 4-23 bytes.\n"); + ladmin_log("Account name is too long. Please input an account name of 4-23 bytes." RETCODE); + } + return 0; + } + + return 1; +} + +//--------------------------------------------------- +// E-mail check: return 0 (not correct) or 1 (valid). +//--------------------------------------------------- +int e_mail_check(unsigned char *email) { + char ch; + unsigned char* last_arobas; + + // athena limits + if (strlen(email) < 3 || strlen(email) > 39) + return 0; + + // part of RFC limits (official reference of e-mail description) + if (strchr(email, '@') == NULL || email[strlen(email)-1] == '@') + return 0; + + if (email[strlen(email)-1] == '.') + return 0; + + last_arobas = strrchr(email, '@'); + + if (strstr(last_arobas, "@.") != NULL || + strstr(last_arobas, "..") != NULL) + return 0; + + for(ch = 1; ch < 32; ch++) { + if (strchr(last_arobas, ch) != NULL) { + return 0; + break; + } + } + + if (strchr(last_arobas, ' ') != NULL || + strchr(last_arobas, ';') != NULL) + return 0; + + // all correct + return 1; +} + +//---------------------------------- +// Sub-function: Input of a password +//---------------------------------- +int typepasswd(char * password) { + char password1[1023], password2[1023]; + int letter; + int i; + + if (defaultlanguage == 'F') { + ladmin_log("Aucun mot de passe n'a 騁 donn. Demande d'un mot de passe." RETCODE); + } else { + ladmin_log("No password was given. Request to obtain a password." RETCODE); + } + + memset(password1, '\0', sizeof(password1)); + memset(password2, '\0', sizeof(password2)); + if (defaultlanguage == 'F') + printf("\033[1;36m Entrez le mot de passe > \033[0;32;42m"); + else + printf("\033[1;36m Type the password > \033[0;32;42m"); + i = 0; + while ((letter = getchar()) != '\n') + password1[i++] = letter; + if (defaultlanguage == 'F') + printf("\033[0m\033[1;36m R-entrez le mot de passe > \033[0;32;42m"); + else + printf("\033[0m\033[1;36m Verify the password > \033[0;32;42m"); + i = 0; + while ((letter = getchar()) != '\n') + password2[i++] = letter; + + printf("\033[0m"); + fflush(stdout); + fflush(stdin); + + if (strcmp(password1, password2) != 0) { + if (defaultlanguage == 'F') { + printf("Erreur de v駻ification du mot de passe: Saisissez le m麥e mot de passe svp.\n"); + ladmin_log("Erreur de v駻ification du mot de passe: Saisissez le m麥e mot de passe svp." RETCODE); + ladmin_log(" Premier mot de passe: %s, second mot de passe: %s." RETCODE, password1, password2); + } else { + printf("Password verification failed. Please input same password.\n"); + ladmin_log("Password verification failed. Please input same password." RETCODE); + ladmin_log(" First password: %s, second password: %s." RETCODE, password1, password2); + } + return 0; + } + if (defaultlanguage == 'F') { + ladmin_log("Mot de passe saisi: %s." RETCODE, password1); + } else { + ladmin_log("Typed password: %s." RETCODE, password1); + } + strcpy(password, password1); + return 1; +} + +//------------------------------------------------------------------------------------ +// Sub-function: Test of the validity of password (return 0 if incorrect, and 1 if ok) +//------------------------------------------------------------------------------------ +int verify_password(char * password) { + int i; + + for(i = 0; password[i]; i++) { + if (password[i] < 32) { + if (defaultlanguage == 'F') { + printf("Caract鑽e interdit trouv dans le mot de passe (%d%s caract鑽e).\n", i+1, makeordinal(i+1)); + ladmin_log("Caract鑽e interdit trouv dans le nom du compte (%d%s caract鑽e)." RETCODE, i+1, makeordinal(i+1)); + } else { + printf("Illegal character found in the password (%d%s character).\n", i+1, makeordinal(i+1)); + ladmin_log("Illegal character found in the password (%d%s character)." RETCODE, i+1, makeordinal(i+1)); + } + return 0; + } + } + + if (strlen(password) < 4) { + if (defaultlanguage == 'F') { + printf("Nom du compte trop court. Entrez un nom de compte de 4-23 caract鑽es.\n"); + ladmin_log("Nom du compte trop court. Entrez un nom de compte de 4-23 caract鑽es." RETCODE); + } else { + printf("Account name is too short. Please input an account name of 4-23 bytes.\n"); + ladmin_log("Account name is too short. Please input an account name of 4-23 bytes." RETCODE); + } + return 0; + } + + if (strlen(password) > 23) { + if (defaultlanguage == 'F') { + printf("Mot de passe trop long. Entrez un mot de passe de 4-23 caract鑽es.\n"); + ladmin_log("Mot de passe trop long. Entrez un mot de passe de 4-23 caract鑽es." RETCODE); + } else { + printf("Password is too long. Please input a password of 4-23 bytes.\n"); + ladmin_log("Password is too long. Please input a password of 4-23 bytes." RETCODE); + } + return 0; + } + + return 1; +} + +//------------------------------------------------------------------ +// Sub-function: Check the name of a command (return complete name) +//----------------------------------------------------------------- +int check_command(char * command) { +// help + if (strncmp(command, "aide", 2) == 0 && strncmp(command, "aide", strlen(command)) == 0) // not 1 letter command: 'aide' or 'add'? + strcpy(command, "aide"); + else if (strncmp(command, "help", 1) == 0 && strncmp(command, "help", strlen(command)) == 0) + strcpy(command, "help"); +// general commands + else if (strncmp(command, "add", 2) == 0 && strncmp(command, "add", strlen(command)) == 0) // not 1 letter command: 'aide' or 'add'? + strcpy(command, "add"); + else if ((strncmp(command, "ban", 3) == 0 && strncmp(command, "ban", strlen(command)) == 0) || + (strncmp(command, "banish", 4) == 0 && strncmp(command, "banish", strlen(command)) == 0)) + strcpy(command, "ban"); + else if ((strncmp(command, "banadd", 4) == 0 && strncmp(command, "banadd", strlen(command)) == 0) || // not 1 letter command: 'ba' or 'bs'? 'banadd' or 'banset' ? + strcmp(command, "ba") == 0) + strcpy(command, "banadd"); + else if ((strncmp(command, "banset", 4) == 0 && strncmp(command, "banset", strlen(command)) == 0) || // not 1 letter command: 'ba' or 'bs'? 'banadd' or 'banset' ? + strcmp(command, "bs") == 0) + strcpy(command, "banset"); + else if (strncmp(command, "block", 2) == 0 && strncmp(command, "block", strlen(command)) == 0) + strcpy(command, "block"); + else if (strncmp(command, "check", 2) == 0 && strncmp(command, "check", strlen(command)) == 0) // not 1 letter command: 'check' or 'create'? + strcpy(command, "check"); + else if (strncmp(command, "create", 2) == 0 && strncmp(command, "create", strlen(command)) == 0) // not 1 letter command: 'check' or 'create'? + strcpy(command, "create"); + else if (strncmp(command, "delete", 1) == 0 && strncmp(command, "delete", strlen(command)) == 0) + strcpy(command, "delete"); + else if ((strncmp(command, "email", 2) == 0 && strncmp(command, "email", strlen(command)) == 0) || // not 1 letter command: 'email', 'end' or 'exit'? + (strncmp(command, "e-mail", 2) == 0 && strncmp(command, "e-mail", strlen(command)) == 0)) + strcpy(command, "email"); + else if (strncmp(command, "getcount", 2) == 0 && strncmp(command, "getcount", strlen(command)) == 0) // not 1 letter command: 'getcount' or 'gm'? + strcpy(command, "getcount"); +// else if (strncmp(command, "gm", 2) == 0 && strncmp(command, "gm", strlen(command)) == 0) // not 1 letter command: 'getcount' or 'gm'? +// strcpy(command, "gm"); +// else if (strncmp(command, "id", 2) == 0 && strncmp(command, "id", strlen(command)) == 0) // not 1 letter command: 'id' or 'info'? +// strcpy(command, "id"); + else if (strncmp(command, "info", 2) == 0 && strncmp(command, "info", strlen(command)) == 0) // not 1 letter command: 'id' or 'info'? + strcpy(command, "info"); +// else if (strncmp(command, "kami", 4) == 0 && strncmp(command, "kami", strlen(command)) == 0) // only all letters command: 'kami' or 'kamib'? +// strcpy(command, "kami"); +// else if (strncmp(command, "kamib", 5) == 0 && strncmp(command, "kamib", strlen(command)) == 0) // only all letters command: 'kami' or 'kamib'? +// strcpy(command, "kamib"); + else if ((strncmp(command, "language", 2) == 0 && strncmp(command, "language", strlen(command)) == 0)) // not 1 letter command: 'language' or 'list'? + strcpy(command, "language"); + else if ((strncmp(command, "list", 2) == 0 && strncmp(command, "list", strlen(command)) == 0) || // 'list' is default list command // not 1 letter command: 'language' or 'list'? + strcmp(command, "ls") == 0) + strcpy(command, "list"); + else if ((strncmp(command, "listban", 5) == 0 && strncmp(command, "listban", strlen(command)) == 0) || + (strncmp(command, "lsban", 3) == 0 && strncmp(command, "lsban", strlen(command)) == 0) || + strcmp(command, "lb") == 0) + strcpy(command, "listban"); + else if ((strncmp(command, "listgm", 5) == 0 && strncmp(command, "listgm", strlen(command)) == 0) || + (strncmp(command, "lsgm", 3) == 0 && strncmp(command, "lsgm", strlen(command)) == 0) || + strcmp(command, "lg") == 0) + strcpy(command, "listgm"); + else if ((strncmp(command, "listok", 5) == 0 && strncmp(command, "listok", strlen(command)) == 0) || + (strncmp(command, "lsok", 3) == 0 && strncmp(command, "lsok", strlen(command)) == 0) || + strcmp(command, "lo") == 0) + strcpy(command, "listok"); + else if (strncmp(command, "memo", 1) == 0 && strncmp(command, "memo", strlen(command)) == 0) + strcpy(command, "memo"); + else if (strncmp(command, "name", 1) == 0 && strncmp(command, "name", strlen(command)) == 0) + strcpy(command, "name"); + else if ((strncmp(command, "password", 1) == 0 && strncmp(command, "password", strlen(command)) == 0) || + strcmp(command, "passwd") == 0) + strcpy(command, "password"); + else if (strncmp(command, "reloadgm", 1) == 0 && strncmp(command, "reloadgm", strlen(command)) == 0) + strcpy(command, "reloadgm"); + else if (strncmp(command, "search", 3) == 0 && strncmp(command, "search", strlen(command)) == 0) // not 1 letter command: 'search', 'state' or 'sex'? + strcpy(command, "search"); // not 2 letters command: 'search' or 'sex'? +// else if (strncmp(command, "sex", 3) == 0 && strncmp(command, "sex", strlen(command)) == 0) // not 1 letter command: 'search', 'state' or 'sex'? +// strcpy(command, "sex"); // not 2 letters command: 'search' or 'sex'? + else if (strncmp(command, "state", 2) == 0 && strncmp(command, "state", strlen(command)) == 0) // not 1 letter command: 'search', 'state' or 'sex'? + strcpy(command, "state"); + else if ((strncmp(command, "timeadd", 5) == 0 && strncmp(command, "timeadd", strlen(command)) == 0) || // not 1 letter command: 'ta' or 'ts'? 'timeadd' or 'timeset'? + strcmp(command, "ta") == 0) + strcpy(command, "timeadd"); + else if ((strncmp(command, "timeset", 5) == 0 && strncmp(command, "timeset", strlen(command)) == 0) || // not 1 letter command: 'ta' or 'ts'? 'timeadd' or 'timeset'? + strcmp(command, "ts") == 0) + strcpy(command, "timeset"); + else if ((strncmp(command, "unban", 5) == 0 && strncmp(command, "unban", strlen(command)) == 0) || + (strncmp(command, "unbanish", 4) == 0 && strncmp(command, "unbanish", strlen(command)) == 0)) + strcpy(command, "unban"); + else if (strncmp(command, "unblock", 4) == 0 && strncmp(command, "unblock", strlen(command)) == 0) + strcpy(command, "unblock"); + else if (strncmp(command, "version", 1) == 0 && strncmp(command, "version", strlen(command)) == 0) + strcpy(command, "version"); + else if (strncmp(command, "who", 1) == 0 && strncmp(command, "who", strlen(command)) == 0) + strcpy(command, "who"); +// quit + else if (strncmp(command, "quit", 1) == 0 && strncmp(command, "quit", strlen(command)) == 0) + strcpy(command, "quit"); + else if (strncmp(command, "exit", 2) == 0 && strncmp(command, "exit", strlen(command)) == 0) // not 1 letter command: 'email', 'end' or 'exit'? + strcpy(command, "exit"); + else if (strncmp(command, "end", 2) == 0 && strncmp(command, "end", strlen(command)) == 0) // not 1 letter command: 'email', 'end' or 'exit'? + strcpy(command, "end"); + + return 0; +} + +//----------------------------------------- +// Sub-function: Display commands of ladmin +//----------------------------------------- +void display_help(char* param, int language) { + char command[1023]; + int i; + + memset(command, '\0', sizeof(command)); + + if (sscanf(param, "%s ", command) < 1 || strlen(command) == 0) + strcpy(command, ""); // any value that is not a command + + if (command[0] == '?') { + if (defaultlanguage == 'F') + strcpy(command, "aide"); + else + strcpy(command, "help"); + } + + // lowercase for command + for (i = 0; command[i]; i++) + command[i] = tolower(command[i]); + + // Analyse of the command + check_command(command); // give complete name to the command + + if (defaultlanguage == 'F') { + ladmin_log("Affichage des commandes ou d'une commande." RETCODE); + } else { + ladmin_log("Displaying of the commands or a command." RETCODE); + } + + if (language == 1) { + if (strcmp(command, "aide") == 0) { + printf("aide/help/?\n"); + printf(" Affiche la description des commandes\n"); + printf("aide/help/? [commande]\n"); + printf(" Affiche la description de la commande specifi馥\n"); + } else if (strcmp(command, "help") == 0 ) { + printf("aide/help/?\n"); + printf(" Display the description of the commands\n"); + printf("aide/help/? [command]\n"); + printf(" Display the description of the specified command\n"); +// general commands + } else if (strcmp(command, "add") == 0) { + printf("add \n"); + printf(" Cr馥 un compte avec l'email par d馭aut (a@a.com).\n"); + printf(" Concernant le sexe, seule la premi鑽e lettre compte (F ou M).\n"); + printf(" L'e-mail est a@a.com (e-mail par d馭aut). C'est comme n'avoir aucun e-mail.\n"); + printf(" Lorsque motdepasse est omis, la saisie se fait sans que la frappe se voit.\n"); + printf(" add testname Male testpass\n"); + } else if (strcmp(command, "ban") == 0) { + printf("ban/banish aaaa/mm/jj hh:mm:ss \n"); + printf(" Change la date de fin de bannissement d'un compte.\n"); + printf(" Comme banset, mais est la fin.\n"); + } else if (strcmp(command, "banadd") == 0) { + printf("banadd \n"); + printf(" Ajoute ou soustrait du temps la date de banissement d'un compte.\n"); + printf(" Les modificateurs sont construits comme suit:\n"); + printf(" Valeur d'ajustement (-1, 1, +1, etc...)\n"); + printf(" El駑ent modifi:\n"); + printf(" a ou y: ann馥\n"); + printf(" m: mois\n"); + printf(" j ou d: jour\n"); + printf(" h: heure\n"); + printf(" mn: minute\n"); + printf(" s: seconde\n"); + printf(" banadd testname +1m-2mn1s-6a\n"); + printf(" Cette exemple ajoute 1 mois et une seconde, et soustrait 2 minutes\n"); + printf(" et 6 ans dans le m麥e temps.\n"); + printf("NOTE: Si vous modifez la date de banissement d'un compte non bani,\n"); + printf(" vous indiquez comme date (le moment actuel +- les ajustements)\n"); + } else if (strcmp(command, "banset") == 0) { + printf("banset aaaa/mm/jj [hh:mm:ss]\n"); + printf(" Change la date de fin de bannissement d'un compte.\n"); + printf(" Heure par d馭aut [hh:mm:ss]: 23:59:59.\n"); + printf("banset 0\n"); + printf(" D饕anni un compte (0 = de-banni).\n"); + } else if (strcmp(command, "block") == 0) { + printf("block \n"); + printf(" Place le status d'un compte 5 (You have been blocked by the GM Team).\n"); + printf(" La commande est l'駲uivalent de state 5.\n"); + } else if (strcmp(command, "check") == 0) { + printf("check \n"); + printf(" V駻ifie la validit d'un mot de passe pour un compte\n"); + printf(" NOTE: Le serveur n'enverra jamais un mot de passe.\n"); + printf(" C'est la seule m騁hode que vous poss馘ez pour savoir\n"); + printf(" si un mot de passe est le bon. L'autre m騁hode est\n"); + printf(" d'avoir un acc鑚 ('physique') au fichier des comptes.\n"); + } else if (strcmp(command, "create") == 0) { + printf("create \n"); + printf(" Comme la commande add, mais avec l'e-mail en plus.\n"); + printf(" create testname Male mon@mail.com testpass\n"); + } else if (strcmp(command, "delete") == 0) { + printf("del \n"); + printf(" Supprime un compte.\n"); + printf(" La commande demande confirmation. Apr鑚 confirmation, le compte est d騁ruit.\n"); + } else if (strcmp(command, "email") == 0) { + printf("email \n"); + printf(" Modifie l'e-mail d'un compte.\n"); + } else if (strcmp(command, "getcount") == 0) { + printf("getcount\n"); + printf(" Donne le nombre de joueurs en ligne par serveur de char.\n"); + } else if (strcmp(command, "gm") == 0) { + printf("gm [Niveau_GM]\n"); + printf(" Modifie le niveau de GM d'un compte.\n"); + printf(" Valeur par d馭aut: 0 (suppression du niveau de GM).\n"); + printf(" gm nomtest 80\n"); + } else if (strcmp(command, "id") == 0) { + printf("id \n"); + printf(" Donne l'id d'un compte.\n"); + } else if (strcmp(command, "info") == 0) { + printf("info \n"); + printf(" Affiche les informations sur un compte.\n"); + } else if (strcmp(command, "kami") == 0) { + printf("kami \n"); + printf(" Envoi un message g駭駻al sur tous les serveurs de map (en jaune).\n"); + } else if (strcmp(command, "kamib") == 0) { + printf("kamib \n"); + printf(" Envoi un message g駭駻al sur tous les serveurs de map (en bleu).\n"); + } else if (strcmp(command, "language") == 0) { + printf("language \n"); + printf(" Change la langue d'affichage.\n"); + printf(" Langues possibles: 'Fran軋is' ou 'English'.\n"); + } else if (strcmp(command, "list") == 0) { + printf("list/ls [Premier_id [Dernier_id]]\n"); + printf(" Affiche une liste de comptes.\n"); + printf(" 'Premier_id', 'Dernier_id': indique les identifiants de d駱art et de fin.\n"); + printf(" La recherche par nom n'est pas possible avec cette commande.\n"); + printf(" list 10 9999999\n"); + } else if (strcmp(command, "listban") == 0) { + printf("listBan/lsBan [Premier_id [Dernier_id]]\n"); + printf(" Comme list/ls, mais seulement pour les comptes avec statut ou bannis.\n"); + } else if (strcmp(command, "listgm") == 0) { + printf("listGM/lsGM [Premier_id [Dernier_id]]\n"); + printf(" Comme list/ls, mais seulement pour les comptes GM.\n"); + } else if (strcmp(command, "listok") == 0) { + printf("listOK/lsOK [Premier_id [Dernier_id]]\n"); + printf(" Comme list/ls, mais seulement pour les comptes sans statut et non bannis.\n"); + } else if (strcmp(command, "memo") == 0) { + printf("memo \n"); + printf(" Modifie le m駑o d'un compte.\n"); + printf(" 'memo': Il peut avoir jusqu' 253 caract鑽es (avec des espaces ou non).\n"); + } else if (strcmp(command, "name") == 0) { + printf("name \n"); + printf(" Donne le nom d'un compte.\n"); + } else if (strcmp(command, "password") == 0) { + printf("passwd \n"); + printf(" Change le mot de passe d'un compte.\n"); + printf(" Lorsque nouveaumotdepasse est omis,\n"); + printf(" la saisie se fait sans que la frappe ne se voit.\n"); + } else if (strcmp(command, "reloadgm") == 0) { + printf("reloadGM\n"); + printf(" Reload GM configuration file\n"); + } else if (strcmp(command, "search") == 0) { + printf("search \n"); + printf(" Cherche des comptes.\n"); + printf(" Affiche les comptes dont les noms correspondent.\n"); +// printf("search -r/-e/--expr/--regex \n"); +// printf(" Cherche des comptes par expression reguli鑽e.\n"); +// printf(" Affiche les comptes dont les noms correspondent.\n"); + } else if (strcmp(command, "sex") == 0) { + printf("sex \n"); + printf(" Modifie le sexe d'un compte.\n"); + printf(" sex testname Male\n"); + } else if (strcmp(command, "state") == 0) { + printf("state \n"); + printf(" Change le statut d'un compte.\n"); + printf(" 'nouveaustatut': Le statut est le m麥e que celui du packet 0x006a + 1.\n"); + printf(" les possibilit駸 sont:\n"); + printf(" 0 = Compte ok\n"); + printf(" 1 = Unregistered ID\n"); + printf(" 2 = Incorrect Password\n"); + printf(" 3 = This ID is expired\n"); + printf(" 4 = Rejected from Server\n"); + printf(" 5 = You have been blocked by the GM Team\n"); + printf(" 6 = Your Game's EXE file is not the latest version\n"); + printf(" 7 = You are Prohibited to log in until...\n"); + printf(" 8 = Server is jammed due to over populated\n"); + printf(" 9 = No MSG\n"); + printf(" 100 = This ID has been totally erased\n"); + printf(" all other values are 'No MSG', then use state 9 please.\n"); + printf(" 'message_erreur_7': message du code erreur 6 =\n"); + printf(" = Your are Prohibited to log in until... (packet 0x006a)\n"); + } else if (strcmp(command, "timeadd") == 0) { + printf("timeadd \n"); + printf(" Ajoute/soustrait du temps la limite de validit d'un compte.\n"); + printf(" Le modificateur est compos comme suit:\n"); + printf(" Valeur modificatrice (-1, 1, +1, etc...)\n"); + printf(" El駑ent modifi:\n"); + printf(" a ou y: ann馥\n"); + printf(" m: mois\n"); + printf(" j ou d: jour\n"); + printf(" h: heure\n"); + printf(" mn: minute\n"); + printf(" s: seconde\n"); + printf(" timeadd testname +1m-2mn1s-6a\n"); + printf(" Cette exemple ajoute 1 mois et une seconde, et soustrait 2 minutes\n"); + printf(" et 6 ans dans le m麥e temps.\n"); + printf("NOTE: Vous ne pouvez pas modifier une limite de validit illimit馥. Si vous\n"); + printf(" d駸irez le faire, c'est que vous voulez probablement cr馥r un limite de\n"); + printf(" validit limit馥. Donc, en premier, fix une limite de valitid.\n"); + } else if (strcmp(command, "timeadd") == 0) { + printf("timeset aaaa/mm/jj [hh:mm:ss]\n"); + printf(" Change la limite de validit d'un compte.\n"); + printf(" Heure par d馭aut [hh:mm:ss]: 23:59:59.\n"); + printf("timeset 0\n"); + printf(" Donne une limite de validit illimit馥 (0 = illimit馥).\n"); + } else if (strcmp(command, "unban") == 0) { + printf("unban/unbanish \n"); + printf(" Ote le banissement d'un compte.\n"); + printf(" La commande est l'駲uivalent de banset 0.\n"); + } else if (strcmp(command, "unblock") == 0) { + printf("unblock \n"); + printf(" Place le status d'un compte 0 (Compte ok).\n"); + printf(" La commande est l'駲uivalent de state 0.\n"); + } else if (strcmp(command, "version") == 0) { + printf("version\n"); + printf(" Affiche la version du login-serveur.\n"); + } else if (strcmp(command, "who") == 0) { + printf("who \n"); + printf(" Affiche les informations sur un compte.\n"); +// quit + } else if (strcmp(command, "quit") == 0 || + strcmp(command, "exit") == 0 || + strcmp(command, "end") == 0) { + printf("quit/end/exit\n"); + printf(" Fin du programme d'administration.\n"); +// unknown command + } else { + if (strlen(command) > 0) + printf("Commande inconnue [%s] pour l'aide. Affichage de toutes les commandes.\n", command); + printf(" aide/help/? -- Affiche cet aide\n"); + printf(" aide/help/? [commande] -- Affiche l'aide de la commande\n"); + printf(" add -- Cr馥 un compte (sans email)\n"); + printf(" ban/banish aaaa/mm/jj hh:mm:ss -- Fixe la date finale de banismnt\n"); + printf(" banadd/ba -- Ajout/soustrait du temps la\n"); + printf(" exemple: ba moncompte +1m-2mn1s-2y date finale de banissement\n"); + printf(" banset/bs aaaa/mm/jj [hh:mm:ss] -- Change la date fin de banisemnt\n"); + printf(" banset/bs 0 -- D-banis un compte.\n"); + printf(" block -- Mets le status d'un compte 5 (blocked by the GM Team)\n"); + printf(" check -- V駻ifie un mot de passe d'un compte\n"); + printf(" create -- Cr馥 un compte (avec email)\n"); + printf(" del -- Supprime un compte\n"); + printf(" email -- Modifie l'e-mail d'un compte\n"); + printf(" getcount -- Donne le nb de joueurs en ligne\n"); + printf(" gm [Niveau_GM] -- Modifie le niveau de GM d'un compte\n"); + printf(" id -- Donne l'id d'un compte\n"); + printf(" info -- Affiche les infos sur un compte\n"); + printf(" kami -- Envoi un message g駭駻al (en jaune)\n"); + printf(" kamib -- Envoi un message g駭駻al (en bleu)\n"); + printf(" language -- Change la langue d'affichage.\n"); + printf(" list/ls [Premier_id [Dernier_id] ] -- Affiche une liste de comptes\n"); + printf(" listBan/lsBan [Premier_id [Dernier_id] ] -- Affiche une liste de comptes\n"); + printf(" avec un statut ou bannis\n"); + printf(" listGM/lsGM [Premier_id [Dernier_id] ] -- Affiche une liste de comptes GM\n"); + printf(" listOK/lsOK [Premier_id [Dernier_id] ] -- Affiche une liste de comptes\n"); + printf(" sans status et non bannis\n"); + printf(" memo -- Modifie le memo d'un compte\n"); + printf(" name -- Donne le nom d'un compte\n"); + printf(" passwd -- Change le mot de passe d'un compte\n"); + printf(" quit/end/exit -- Fin du programme d'administation\n"); + printf(" reloadGM -- Recharger le fichier de config des GM\n"); + printf(" search -- Cherche des comptes\n"); +// printf(" search -e/-r/--expr/--regex -- Cherche des comptes par REGEX\n"); + printf(" sex -- Modifie le sexe d'un compte\n"); + printf(" state -- Change le statut d'1 compte\n"); + printf(" timeadd/ta -- Ajout/soustrait du temps la\n"); + printf(" exemple: ta moncompte +1m-2mn1s-2y limite de validit饅n"); + printf(" timeset/ts aaaa/mm/jj [hh:mm:ss] -- Change la limite de validit饅n"); + printf(" timeset/ts 0 -- limite de validit = illimit馥\n"); + printf(" unban/unbanish -- Ote le banissement d'un compte\n"); + printf(" unblock -- Mets le status d'un compte 0 (Compte ok)\n"); + printf(" version -- Donne la version du login-serveur\n"); + printf(" who -- Affiche les infos sur un compte\n"); + printf(" Note: Pour les noms de compte avec des espaces, tapez \"\" (ou ').\n"); + } + } else { + if (strcmp(command, "aide") == 0) { + printf("aide/help/?\n"); + printf(" Display the description of the commands\n"); + printf("aide/help/? [command]\n"); + printf(" Display the description of the specified command\n"); + } else if (strcmp(command, "help") == 0 ) { + printf("aide/help/?\n"); + printf(" Display the description of the commands\n"); + printf("aide/help/? [command]\n"); + printf(" Display the description of the specified command\n"); +// general commands + } else if (strcmp(command, "add") == 0) { + printf("add \n"); + printf(" Create an account with the default email (a@a.com).\n"); + printf(" Concerning the sex, only the first letter is used (F or M).\n"); + printf(" The e-mail is set to a@a.com (default e-mail). It's like to have no e-mail.\n"); + printf(" When the password is omitted,\n"); + printf(" the input is done without displaying of the pressed keys.\n"); + printf(" add testname Male testpass\n"); + } else if (strcmp(command, "ban") == 0) { + printf("ban/banish yyyy/mm/dd hh:mm:ss \n"); + printf(" Changes the final date of a banishment of an account.\n"); + printf(" Like banset, but is at end.\n"); + } else if (strcmp(command, "banadd") == 0) { + printf("banadd \n"); + printf(" Adds or substracts time from the final date of a banishment of an account.\n"); + printf(" Modifier is done as follows:\n"); + printf(" Adjustment value (-1, 1, +1, etc...)\n"); + printf(" Modified element:\n"); + printf(" a or y: year\n"); + printf(" m: month\n"); + printf(" j or d: day\n"); + printf(" h: hour\n"); + printf(" mn: minute\n"); + printf(" s: second\n"); + printf(" banadd testname +1m-2mn1s-6y\n"); + printf(" this example adds 1 month and 1 second, and substracts 2 minutes\n"); + printf(" and 6 years at the same time.\n"); + printf("NOTE: If you modify the final date of a non-banished account,\n"); + printf(" you fix the final date to (actual time +- adjustments)\n"); + } else if (strcmp(command, "banset") == 0) { + printf("banset yyyy/mm/dd [hh:mm:ss]\n"); + printf(" Changes the final date of a banishment of an account.\n"); + printf(" Default time [hh:mm:ss]: 23:59:59.\n"); + printf("banset 0\n"); + printf(" Set a non-banished account (0 = unbanished).\n"); + } else if (strcmp(command, "block") == 0) { + printf("block \n"); + printf(" Set state 5 (You have been blocked by the GM Team) to an account.\n"); + printf(" This command works like state 5.\n"); + } else if (strcmp(command, "check") == 0) { + printf("check \n"); + printf(" Check the validity of a password for an account.\n"); + printf(" NOTE: Server will never sends back a password.\n"); + printf(" It's the only method you have to know if a password is correct.\n"); + printf(" The other method is to have a ('physical') access to the accounts file.\n"); + } else if (strcmp(command, "create") == 0) { + printf("create \n"); + printf(" Like the 'add' command, but with e-mail moreover.\n"); + printf(" create testname Male my@mail.com testpass\n"); + } else if (strcmp(command, "delete") == 0) { + printf("del \n"); + printf(" Remove an account.\n"); + printf(" This order requires confirmation. After confirmation, the account is deleted.\n"); + } else if (strcmp(command, "email") == 0) { + printf("email \n"); + printf(" Modify the e-mail of an account.\n"); + } else if (strcmp(command, "getcount") == 0) { + printf("getcount\n"); + printf(" Give the number of players online on all char-servers.\n"); + } else if (strcmp(command, "gm") == 0) { + printf("gm [GM_level]\n"); + printf(" Modify the GM level of an account.\n"); + printf(" Default value remove GM level (GM level = 0).\n"); + printf(" gm testname 80\n"); + } else if (strcmp(command, "id") == 0) { + printf("id \n"); + printf(" Give the id of an account.\n"); + } else if (strcmp(command, "info") == 0) { + printf("info \n"); + printf(" Display complete information of an account.\n"); + } else if (strcmp(command, "kami") == 0) { + printf("kami \n"); + printf(" Sends a broadcast message on all map-server (in yellow).\n"); + } else if (strcmp(command, "kamib") == 0) { + printf("kamib \n"); + printf(" Sends a broadcast message on all map-server (in blue).\n"); + } else if (strcmp(command, "language") == 0) { + printf("language \n"); + printf(" Change the language of displaying.\n"); + printf(" Possible languages: Fran軋is or English.\n"); + } else if (strcmp(command, "list") == 0) { + printf("list/ls [start_id [end_id]]\n"); + printf(" Display a list of accounts.\n"); + printf(" 'start_id', 'end_id': indicate end and start identifiers.\n"); + printf(" Research by name is not possible with this command.\n"); + printf(" list 10 9999999\n"); + } else if (strcmp(command, "listban") == 0) { + printf("listBan/lsBan [start_id [end_id]]\n"); + printf(" Like list/ls, but only for accounts with state or banished.\n"); + } else if (strcmp(command, "listgm") == 0) { + printf("listGM/lsGM [start_id [end_id]]\n"); + printf(" Like list/ls, but only for GM accounts.\n"); + } else if (strcmp(command, "listok") == 0) { + printf("listOK/lsOK [start_id [end_id]]\n"); + printf(" Like list/ls, but only for accounts without state and not banished.\n"); + } else if (strcmp(command, "memo") == 0) { + printf("memo \n"); + printf(" Modify the memo of an account.\n"); + printf(" 'memo': it can have until 253 characters (with spaces or not).\n"); + } else if (strcmp(command, "name") == 0) { + printf("name \n"); + printf(" Give the name of an account.\n"); + } else if (strcmp(command, "password") == 0) { + printf("passwd \n"); + printf(" Change the password of an account.\n"); + printf(" When new password is omitted,\n"); + printf(" the input is done without displaying of the pressed keys.\n"); + } else if (strcmp(command, "reloadgm") == 0) { + printf("reloadGM\n"); + printf(" Reload GM configuration file\n"); + } else if (strcmp(command, "search") == 0) { + printf("search \n"); + printf(" Seek accounts.\n"); + printf(" Displays the accounts whose names correspond.\n"); +// printf("search -r/-e/--expr/--regex \n"); +// printf(" Seek accounts by regular expression.\n"); +// printf(" Displays the accounts whose names correspond.\n"); + } else if (strcmp(command, "sex") == 0) { + printf("sex \n"); + printf(" Modify the sex of an account.\n"); + printf(" sex testname Male\n"); + } else if (strcmp(command, "state") == 0) { + printf("state \n"); + printf(" Change the state of an account.\n"); + printf(" 'new_state': state is the state of the packet 0x006a + 1.\n"); + printf(" The possibilities are:\n"); + printf(" 0 = Account ok\n"); + printf(" 1 = Unregistered ID\n"); + printf(" 2 = Incorrect Password\n"); + printf(" 3 = This ID is expired\n"); + printf(" 4 = Rejected from Server\n"); + printf(" 5 = You have been blocked by the GM Team\n"); + printf(" 6 = Your Game's EXE file is not the latest version\n"); + printf(" 7 = You are Prohibited to log in until...\n"); + printf(" 8 = Server is jammed due to over populated\n"); + printf(" 9 = No MSG\n"); + printf(" 100 = This ID has been totally erased\n"); + printf(" all other values are 'No MSG', then use state 9 please.\n"); + printf(" 'error_message_#7': message of the code error 6\n"); + printf(" = Your are Prohibited to log in until... (packet 0x006a)\n"); + } else if (strcmp(command, "timeadd") == 0) { + printf("timeadd \n"); + printf(" Adds or substracts time from the validity limit of an account.\n"); + printf(" Modifier is done as follows:\n"); + printf(" Adjustment value (-1, 1, +1, etc...)\n"); + printf(" Modified element:\n"); + printf(" a or y: year\n"); + printf(" m: month\n"); + printf(" j or d: day\n"); + printf(" h: hour\n"); + printf(" mn: minute\n"); + printf(" s: second\n"); + printf(" timeadd testname +1m-2mn1s-6y\n"); + printf(" this example adds 1 month and 1 second, and substracts 2 minutes\n"); + printf(" and 6 years at the same time.\n"); + printf("NOTE: You can not modify a unlimited validity limit.\n"); + printf(" If you want modify it, you want probably create a limited validity limit.\n"); + printf(" So, at first, you must set the validity limit to a date/time.\n"); + } else if (strcmp(command, "timeadd") == 0) { + printf("timeset yyyy/mm/dd [hh:mm:ss]\n"); + printf(" Changes the validity limit of an account.\n"); + printf(" Default time [hh:mm:ss]: 23:59:59.\n"); + printf("timeset 0\n"); + printf(" Gives an unlimited validity limit (0 = unlimited).\n"); + } else if (strcmp(command, "unban") == 0) { + printf("unban/unbanish \n"); + printf(" Remove the banishment of an account.\n"); + printf(" This command works like banset 0.\n"); + } else if (strcmp(command, "unblock") == 0) { + printf("unblock \n"); + printf(" Set state 0 (Account ok) to an account.\n"); + printf(" This command works like state 0.\n"); + } else if (strcmp(command, "version") == 0) { + printf("version\n"); + printf(" Display the version of the login-server.\n"); + } else if (strcmp(command, "who") == 0) { + printf("who \n"); + printf(" Displays complete information of an account.\n"); +// quit + } else if (strcmp(command, "quit") == 0 || + strcmp(command, "exit") == 0 || + strcmp(command, "end") == 0) { + printf("quit/end/exit\n"); + printf(" End of the program of administration.\n"); +// unknown command + } else { + if (strlen(command) > 0) + printf("Unknown command [%s] for help. Displaying of all commands.\n", command); + printf(" aide/help/? -- Display this help\n"); + printf(" aide/help/? [command] -- Display the help of the command\n"); + printf(" add -- Create an account with default email\n"); + printf(" ban/banish yyyy/mm/dd hh:mm:ss -- Change final date of a ban\n"); + printf(" banadd/ba -- Add or substract time from the final\n"); + printf(" example: ba apple +1m-2mn1s-2y date of a banishment of an account\n"); + printf(" banset/bs yyyy/mm/dd [hh:mm:ss] -- Change final date of a ban\n"); + printf(" banset/bs 0 -- Un-banish an account\n"); + printf(" block -- Set state 5 (blocked by the GM Team) to an account\n"); + printf(" check -- Check the validity of a password\n"); + printf(" create -- Create an account with email\n"); + printf(" del -- Remove an account\n"); + printf(" email -- Modify an email of an account\n"); + printf(" getcount -- Give the number of players online\n"); + printf(" gm [GM_level] -- Modify the GM level of an account\n"); + printf(" id -- Give the id of an account\n"); + printf(" info -- Display all information of an account\n"); + printf(" kami -- Sends a broadcast message (in yellow)\n"); + printf(" kamib -- Sends a broadcast message (in blue)\n"); + printf(" language -- Change the language of displaying.\n"); + printf(" list/ls [First_id [Last_id]] -- Display a list of accounts\n"); + printf(" listBan/lsBan [First_id [Last_id] ] -- Display a list of accounts\n"); + printf(" with state or banished\n"); + printf(" listGM/lsGM [First_id [Last_id]] -- Display a list of GM accounts\n"); + printf(" listOK/lsOK [First_id [Last_id] ] -- Display a list of accounts\n"); + printf(" without state and not banished\n"); + printf(" memo -- Modify the memo of an account\n"); + printf(" name -- Give the name of an account\n"); + printf(" passwd -- Change the password of an account\n"); + printf(" quit/end/exit -- End of the program of administation\n"); + printf(" reloadGM -- Reload GM configuration file\n"); + printf(" search -- Seek accounts\n"); +// printf(" search -e/-r/--expr/--regex -- Seek accounts by regular-expression\n"); + printf(" sex -- Modify the sex of an account\n"); + printf(" state -- Change the state\n"); + printf(" timeadd/ta -- Add or substract time from the\n"); + printf(" example: ta apple +1m-2mn1s-2y validity limit of an account\n"); + printf(" timeset/ts yyyy/mm/dd [hh:mm:ss] -- Change the validify limit\n"); + printf(" timeset/ts 0 -- Give a unlimited validity limit\n"); + printf(" unban/unbanish -- Remove the banishment of an account\n"); + printf(" unblock -- Set state 0 (Account ok) to an account\n"); + printf(" version -- Gives the version of the login-server\n"); + printf(" who -- Display all information of an account\n"); + printf(" who -- Display all information of an account\n"); + printf(" Note: To use spaces in an account name, type \"\" (or ').\n"); + } + } +} + +//----------------------------- +// Sub-function: add an account +//----------------------------- +int addaccount(char* param, int emailflag) { + char name[1023], sex[1023], email[1023], password[1023]; +// int i; + + memset(name, '\0', sizeof(name)); + memset(sex, '\0', sizeof(sex)); + memset(email, '\0', sizeof(email)); + memset(password, '\0', sizeof(password)); + + if (emailflag == 0) { // add command + if (sscanf(param, "\"%[^\"]\" %s %[^\r\n]", name, sex, password) < 2 && // password can be void + sscanf(param, "'%[^']' %s %[^\r\n]", name, sex, password) < 2 && // password can be void + sscanf(param, "%s %s %[^\r\n]", name, sex, password) < 2) { // password can be void + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte, un sexe et un mot de passe svp.\n"); + printf(" add nomtest Male motdepassetest\n"); + ladmin_log("Nombre incorrect de param鑼res pour cr馥r un compte (commande 'add')." RETCODE); + } else { + printf("Please input an account name, a sex and a password.\n"); + printf(" add testname Male testpass\n"); + ladmin_log("Incomplete parameters to create an account ('add' command)." RETCODE); + } + return 136; + } + strcpy(email, "a@a.com"); // default email + } else { // 1: create command + if (sscanf(param, "\"%[^\"]\" %s %s %[^\r\n]", name, sex, email, password) < 3 && // password can be void + sscanf(param, "'%[^']' %s %s %[^\r\n]", name, sex, email, password) < 3 && // password can be void + sscanf(param, "%s %s %s %[^\r\n]", name, sex, email, password) < 3) { // password can be void + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte, un sexe et un mot de passe svp.\n"); + printf(" create nomtest Male mo@mail.com motdepassetest\n"); + ladmin_log("Nombre incorrect de param鑼res pour cr馥r un compte (commande 'create')." RETCODE); + } else { + printf("Please input an account name, a sex and a password.\n"); + printf(" create testname Male my@mail.com testpass\n"); + ladmin_log("Incomplete parameters to create an account ('create' command)." RETCODE); + } + return 136; + } + } + if (verify_accountname(name) == 0) { + return 102; + } + +/* for(i = 0; name[i]; i++) { + if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_", name[i]) == NULL) { + if (defaultlanguage == 'F') { + printf("Caract鑽e interdit (%c) trouv dans le nom du compte (%d%s caract鑽e).\n", name[i], i+1, makeordinal(i+1)); + ladmin_log("Caract鑽e interdit (%c) trouv dans le nom du compte (%d%s caract鑽e)." RETCODE, name[i], i+1, makeordinal(i+1)); + } else { + printf("Illegal character (%c) found in the account name (%d%s character).\n", name[i], i+1, makeordinal(i+1)); + ladmin_log("Illegal character (%c) found in the account name (%d%s character)." RETCODE, name[i], i+1, makeordinal(i+1)); + } + return 101; + } + }*/ + + sex[0] = toupper(sex[0]); + if (strchr("MF", sex[0]) == NULL) { + if (defaultlanguage == 'F') { + printf("Sexe incorrect [%s]. Entrez M ou F svp.\n", sex); + ladmin_log("Sexe incorrect [%s]. Entrez M ou F svp." RETCODE, sex); + } else { + printf("Illegal gender [%s]. Please input M or F.\n", sex); + ladmin_log("Illegal gender [%s]. Please input M or F." RETCODE, sex); + } + return 103; + } + + if (strlen(email) < 3) { + if (defaultlanguage == 'F') { + printf("Email trop courte [%s]. Entrez une e-mail valide svp.\n", email); + ladmin_log("Email trop courte [%s]. Entrez une e-mail valide svp." RETCODE, email); + } else { + printf("Email is too short [%s]. Please input a valid e-mail.\n", email); + ladmin_log("Email is too short [%s]. Please input a valid e-mail." RETCODE, email); + } + return 109; + } + if (strlen(email) > 39) { + if (defaultlanguage == 'F') { + printf("Email trop longue [%s]. Entrez une e-mail de 39 caract鑽es maximum svp.\n", email); + ladmin_log("Email trop longue [%s]. Entrez une e-mail de 39 caract鑽es maximum svp." RETCODE, email); + } else { + printf("Email is too long [%s]. Please input an e-mail with 39 bytes at the most.\n", email); + ladmin_log("Email is too long [%s]. Please input an e-mail with 39 bytes at the most." RETCODE, email); + } + return 109; + } + if (e_mail_check(email) == 0) { + if (defaultlanguage == 'F') { + printf("Email incorrecte [%s]. Entrez une e-mail valide svp.\n", email); + ladmin_log("Email incorrecte [%s]. Entrez une e-mail valide svp." RETCODE, email); + } else { + printf("Invalid email [%s]. Please input a valid e-mail.\n", email); + ladmin_log("Invalid email [%s]. Please input a valid e-mail." RETCODE, email); + } + return 109; + } + + if (strlen(password) == 0) { + if (typepasswd(password) == 0) + return 108; + } + if (verify_password(password) == 0) + return 104; + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour cr馥r un compte." RETCODE); + } else { + ladmin_log("Request to login-server to create an account." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7930; + memcpy(WFIFOP(login_fd,2), name, 24); + memcpy(WFIFOP(login_fd,26), password, 24); + WFIFOB(login_fd,50) = sex[0]; + memcpy(WFIFOP(login_fd,51), email, 40); + WFIFOSET(login_fd,91); + bytes_to_read = 1; + + return 0; +} + +//--------------------------------------------------------------------------------- +// Sub-function: Add/substract time to the final date of a banishment of an account +//--------------------------------------------------------------------------------- +int banaddaccount(char* param) { + char name[1023], modif[1023]; + int year, month, day, hour, minute, second; + char * p_modif; + int value, i; + + memset(name, '\0', sizeof(name)); + memset(modif, '\0', sizeof(modif)); + year = month = day = hour = minute = second = 0; + + if (sscanf(param, "\"%[^\"]\" %[^\r\n]", name, modif) < 2 && + sscanf(param, "'%[^']' %[^\r\n]", name, modif) < 2 && + sscanf(param, "%s %[^\r\n]", name, modif) < 2) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte et un modificateur svp.\n"); + printf(" banadd nomtest +1m-2mn1s-6y\n"); + printf(" Cette exemple ajoute 1 mois et 1 seconde, et soustrait 2 minutes\n"); + printf(" et 6 ans dans le m麥e temps.\n"); + ladmin_log("Nombre incorrect de param鑼res pour modifier la fin de ban d'un compte (commande 'banadd')." RETCODE); + } else { + printf("Please input an account name and a modifier.\n"); + printf(" : banadd testname +1m-2mn1s-6y\n"); + printf(" this example adds 1 month and 1 second, and substracts 2 minutes\n"); + printf(" and 6 years at the same time.\n"); + ladmin_log("Incomplete parameters to modify the ban date/time of an account ('banadd' command)." RETCODE); + } + return 136; + } + if (verify_accountname(name) == 0) { + return 102; + } + + // lowercase for modif + for (i = 0; modif[i]; i++) + modif[i] = tolower(modif[i]); + p_modif = modif; + while (strlen(p_modif) > 0) { + value = atoi(p_modif); + if (value == 0) { + p_modif++; + } else { + if (p_modif[0] == '-' || p_modif[0] == '+') + p_modif++; + while (strlen(p_modif) > 0 && p_modif[0] >= '0' && p_modif[0] <= '9') { + p_modif++; + } + if (p_modif[0] == 's') { + second = value; + p_modif++; + } else if (p_modif[0] == 'm' && p_modif[1] == 'n') { + minute = value; + p_modif += 2; + } else if (p_modif[0] == 'h') { + hour = value; + p_modif++; + } else if (p_modif[0] == 'd' || p_modif[0] == 'j') { + day = value; + p_modif += 2; + } else if (p_modif[0] == 'm') { + month = value; + p_modif++; + } else if (p_modif[0] == 'y' || p_modif[0] == 'a') { + year = value; + p_modif++; + } else { + p_modif++; + } + } + } + + if (defaultlanguage == 'F') { + printf(" ann馥: %d\n", year); + printf(" mois: %d\n", month); + printf(" jour: %d\n", day); + printf(" heure: %d\n", hour); + printf(" minute: %d\n", minute); + printf(" seconde: %d\n", second); + } else { + printf(" year: %d\n", year); + printf(" month: %d\n", month); + printf(" day: %d\n", day); + printf(" hour: %d\n", hour); + printf(" minute: %d\n", minute); + printf(" second: %d\n", second); + } + + if (year == 0 && month == 0 && day == 0 && hour == 0 && minute == 0 && second == 0) { + if (defaultlanguage == 'F') { + printf("Vous devez entrer un ajustement avec cette commande, svp:\n"); + printf(" Valeur d'ajustement (-1, 1, +1, etc...)\n"); + printf(" Element modifi:\n"); + printf(" a ou y: ann馥\n"); + printf(" m: mois\n"); + printf(" j ou d: jour\n"); + printf(" h: heure\n"); + printf(" mn: minute\n"); + printf(" s: seconde\n"); + printf(" banadd nomtest +1m-2mn1s-6y\n"); + printf(" Cette exemple ajoute 1 mois et 1 seconde, et soustrait 2 minutes\n"); + printf(" et 6 ans dans le m麥e temps.\n"); + ladmin_log("Aucun ajustement n'est pas un ajustement (commande 'banadd')." RETCODE); + } else { + printf("Please give an adjustment with this command:\n"); + printf(" Adjustment value (-1, 1, +1, etc...)\n"); + printf(" Modified element:\n"); + printf(" a or y: year\n"); + printf(" m: month\n"); + printf(" j or d: day\n"); + printf(" h: hour\n"); + printf(" mn: minute\n"); + printf(" s: second\n"); + printf(" banadd testname +1m-2mn1s-6y\n"); + printf(" this example adds 1 month and 1 second, and substracts 2 minutes\n"); + printf(" and 6 years at the same time.\n"); + ladmin_log("No adjustment isn't an adjustment ('banadd' command)." RETCODE); + } + return 137; + } + if (year > 127 || year < -127) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement d'ann馥s correct (de -127 127), svp.\n"); + ladmin_log("Ajustement de l'ann馥 hors norme (commande 'banadd')." RETCODE); + } else { + printf("Please give a correct adjustment for the years (from -127 to 127).\n"); + ladmin_log("Abnormal adjustement for the year ('banadd' command)." RETCODE); + } + return 137; + } + if (month > 255 || month < -255) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement de mois correct (de -255 255), svp.\n"); + ladmin_log("Ajustement du mois hors norme (commande 'banadd')." RETCODE); + } else { + printf("Please give a correct adjustment for the months (from -255 to 255).\n"); + ladmin_log("Abnormal adjustement for the month ('banadd' command)." RETCODE); + } + return 137; + } + if (day > 32767 || day < -32767) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement de jours correct (de -32767 32767), svp.\n"); + ladmin_log("Ajustement des jours hors norme (commande 'banadd')." RETCODE); + } else { + printf("Please give a correct adjustment for the days (from -32767 to 32767).\n"); + ladmin_log("Abnormal adjustement for the days ('banadd' command)." RETCODE); + } + return 137; + } + if (hour > 32767 || hour < -32767) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement d'heures correct (de -32767 32767), svp.\n"); + ladmin_log("Ajustement des heures hors norme (commande 'banadd')." RETCODE); + } else { + printf("Please give a correct adjustment for the hours (from -32767 to 32767).\n"); + ladmin_log("Abnormal adjustement for the hours ('banadd' command)." RETCODE); + } + return 137; + } + if (minute > 32767 || minute < -32767) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement de minutes correct (de -32767 32767), svp.\n"); + ladmin_log("Ajustement des minutes hors norme (commande 'banadd')." RETCODE); + } else { + printf("Please give a correct adjustment for the minutes (from -32767 to 32767).\n"); + ladmin_log("Abnormal adjustement for the minutes ('banadd' command)." RETCODE); + } + return 137; + } + if (second > 32767 || second < -32767) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement de secondes correct (de -32767 32767), svp.\n"); + ladmin_log("Ajustement des secondes hors norme (commande 'banadd')." RETCODE); + } else { + printf("Please give a correct adjustment for the seconds (from -32767 to 32767).\n"); + ladmin_log("Abnormal adjustement for the seconds ('banadd' command)." RETCODE); + } + return 137; + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour modifier la date d'un bannissement." RETCODE); + } else { + ladmin_log("Request to login-server to modify a ban date/time." RETCODE); + } + + WFIFOW(login_fd,0) = 0x794c; + memcpy(WFIFOP(login_fd,2), name, 24); + WFIFOW(login_fd,26) = (short)year; + WFIFOW(login_fd,28) = (short)month; + WFIFOW(login_fd,30) = (short)day; + WFIFOW(login_fd,32) = (short)hour; + WFIFOW(login_fd,34) = (short)minute; + WFIFOW(login_fd,36) = (short)second; + WFIFOSET(login_fd,38); + bytes_to_read = 1; + + return 0; +} + +//----------------------------------------------------------------------- +// Sub-function of sub-function banaccount, unbanaccount or bansetaccount +// Set the final date of a banishment of an account +//----------------------------------------------------------------------- +int bansetaccountsub(char* name, char* date, char* time) { + int year, month, day, hour, minute, second; + time_t ban_until_time; // # of seconds 1/1/1970 (timestamp): ban time limit of the account (0 = no ban) + struct tm *tmtime; + + year = month = day = hour = minute = second = 0; + ban_until_time = 0; + tmtime = localtime(&ban_until_time); // initialize + + if (verify_accountname(name) == 0) { + return 102; + } + + if (atoi(date) != 0 && + ((sscanf(date, "%d/%d/%d", &year, &month, &day) < 3 && + sscanf(date, "%d-%d-%d", &year, &month, &day) < 3 && + sscanf(date, "%d.%d.%d", &year, &month, &day) < 3) || + sscanf(time, "%d:%d:%d", &hour, &minute, &second) < 3)) { + if (defaultlanguage == 'F') { + printf("Entrez une date et une heure svp (format: aaaa/mm/jj hh:mm:ss).\n"); + printf("Vous pouvez aussi mettre 0 la place si vous utilisez la commande 'banset'.\n"); + ladmin_log("Format incorrect pour la date/heure (commande'banset' ou 'ban')." RETCODE); + } else { + printf("Please input a date and a time (format: yyyy/mm/dd hh:mm:ss).\n"); + printf("You can imput 0 instead of if you use 'banset' command.\n"); + ladmin_log("Invalid format for the date/time ('banset' or 'ban' command)." RETCODE); + } + return 102; + } + + if (atoi(date) == 0) { + ban_until_time = 0; + } else { + if (year < 70) { + year = year + 100; + } + if (year >= 1900) { + year = year - 1900; + } + if (month < 1 || month > 12) { + if (defaultlanguage == 'F') { + printf("Entrez un mois correct svp (entre 1 et 12).\n"); + ladmin_log("Mois incorrect pour la date (command 'banset' ou 'ban')." RETCODE); + } else { + printf("Please give a correct value for the month (from 1 to 12).\n"); + ladmin_log("Invalid month for the date ('banset' or 'ban' command)." RETCODE); + } + return 102; + } + month = month - 1; + if (day < 1 || day > 31) { + if (defaultlanguage == 'F') { + printf("Entrez un jour correct svp (entre 1 et 31).\n"); + ladmin_log("Jour incorrect pour la date (command 'banset' ou 'ban')." RETCODE); + } else { + printf("Please give a correct value for the day (from 1 to 31).\n"); + ladmin_log("Invalid day for the date ('banset' or 'ban' command)." RETCODE); + } + return 102; + } + if (((month == 3 || month == 5 || month == 8 || month == 10) && day > 30) || + (month == 1 && day > 29)) { + if (defaultlanguage == 'F') { + printf("Entrez un jour correct en fonction du mois (%d) svp.\n", month); + ladmin_log("Jour incorrect pour ce mois correspondant (command 'banset' ou 'ban')." RETCODE); + } else { + printf("Please give a correct value for a day of this month (%d).\n", month); + ladmin_log("Invalid day for this month ('banset' or 'ban' command)." RETCODE); + } + return 102; + } + if (hour < 0 || hour > 23) { + if (defaultlanguage == 'F') { + printf("Entrez une heure correcte svp (entre 0 et 23).\n"); + ladmin_log("Heure incorrecte pour l'heure (command 'banset' ou 'ban')." RETCODE); + } else { + printf("Please give a correct value for the hour (from 0 to 23).\n"); + ladmin_log("Invalid hour for the time ('banset' or 'ban' command)." RETCODE); + } + return 102; + } + if (minute < 0 || minute > 59) { + if (defaultlanguage == 'F') { + printf("Entrez des minutes correctes svp (entre 0 et 59).\n"); + ladmin_log("Minute incorrecte pour l'heure (command 'banset' ou 'ban')." RETCODE); + } else { + printf("Please give a correct value for the minutes (from 0 to 59).\n"); + ladmin_log("Invalid minute for the time ('banset' or 'ban' command)." RETCODE); + } + return 102; + } + if (second < 0 || second > 59) { + if (defaultlanguage == 'F') { + printf("Entrez des secondes correctes svp (entre 0 et 59).\n"); + ladmin_log("Seconde incorrecte pour l'heure (command 'banset' ou 'ban')." RETCODE); + } else { + printf("Please give a correct value for the seconds (from 0 to 59).\n"); + ladmin_log("Invalid second for the time ('banset' or 'ban' command)." RETCODE); + } + return 102; + } + tmtime->tm_year = year; + tmtime->tm_mon = month; + tmtime->tm_mday = day; + tmtime->tm_hour = hour; + tmtime->tm_min = minute; + tmtime->tm_sec = second; + tmtime->tm_isdst = -1; // -1: no winter/summer time modification + ban_until_time = mktime(tmtime); + if (ban_until_time == -1) { + if (defaultlanguage == 'F') { + printf("Date incorrecte.\n"); + printf("Entrez une date et une heure svp (format: aaaa/mm/jj hh:mm:ss).\n"); + printf("Vous pouvez aussi mettre 0 la place si vous utilisez la commande 'banset'.\n"); + ladmin_log("Date incorrecte. (command 'banset' ou 'ban')." RETCODE); + } else { + printf("Invalid date.\n"); + printf("Please input a date and a time (format: yyyy/mm/dd hh:mm:ss).\n"); + printf("You can imput 0 instead of if you use 'banset' command.\n"); + ladmin_log("Invalid date. ('banset' or 'ban' command)." RETCODE); + } + return 102; + } + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour fixer un ban." RETCODE); + } else { + ladmin_log("Request to login-server to set a ban." RETCODE); + } + + WFIFOW(login_fd,0) = 0x794a; + memcpy(WFIFOP(login_fd,2), name, 24); + WFIFOL(login_fd,26) = (int)ban_until_time; + WFIFOSET(login_fd,30); + bytes_to_read = 1; + + return 0; +} + +//--------------------------------------------------------------------- +// Sub-function: Set the final date of a banishment of an account (ban) +//--------------------------------------------------------------------- +int banaccount(char* param) { + char name[1023], date[1023], time[1023]; + + memset(name, '\0', sizeof(name)); + memset(date, '\0', sizeof(date)); + memset(time, '\0', sizeof(time)); + + if (sscanf(param, "%s %s \"%[^\"]\"", date, time, name) < 3 && + sscanf(param, "%s %s '%[^']'", date, time, name) < 3 && + sscanf(param, "%s %s %[^\r\n]", date, time, name) < 3) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte, une date et une heure svp.\n"); + printf(": banset aaaa/mm/jj [hh:mm:ss]\n"); + printf(" banset 0 (0 = d-bani)\n"); + printf(" ban/banish aaaa/mm/jj hh:mm:ss \n"); + printf(" unban/unbanish \n"); + printf(" Heure par d馭aut [hh:mm:ss]: 23:59:59.\n"); + ladmin_log("Nombre incorrect de param鑼res pour fixer un ban (commande 'banset' ou 'ban')." RETCODE); + } else { + printf("Please input an account name, a date and a hour.\n"); + printf(": banset yyyy/mm/dd [hh:mm:ss]\n"); + printf(" banset 0 (0 = un-banished)\n"); + printf(" ban/banish yyyy/mm/dd hh:mm:ss \n"); + printf(" unban/unbanish \n"); + printf(" Default time [hh:mm:ss]: 23:59:59.\n"); + ladmin_log("Incomplete parameters to set a ban ('banset' or 'ban' command)." RETCODE); + } + return 136; + } + + return bansetaccountsub(name, date, time); +} + +//------------------------------------------------------------------------ +// Sub-function: Set the final date of a banishment of an account (banset) +//------------------------------------------------------------------------ +int bansetaccount(char* param) { + char name[1023], date[1023], time[1023]; + + memset(name, '\0', sizeof(name)); + memset(date, '\0', sizeof(date)); + memset(time, '\0', sizeof(time)); + + if (sscanf(param, "\"%[^\"]\" %s %[^\r\n]", name, date, time) < 2 && // if date = 0, time can be void + sscanf(param, "'%[^']' %s %[^\r\n]", name, date, time) < 2 && // if date = 0, time can be void + sscanf(param, "%s %s %[^\r\n]", name, date, time) < 2) { // if date = 0, time can be void + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte, une date et une heure svp.\n"); + printf(": banset aaaa/mm/jj [hh:mm:ss]\n"); + printf(" banset 0 (0 = d-bani)\n"); + printf(" ban/banish aaaa/mm/jj hh:mm:ss \n"); + printf(" unban/unbanish \n"); + printf(" Heure par d馭aut [hh:mm:ss]: 23:59:59.\n"); + ladmin_log("Nombre incorrect de param鑼res pour fixer un ban (commande 'banset' ou 'ban')." RETCODE); + } else { + printf("Please input an account name, a date and a hour.\n"); + printf(": banset yyyy/mm/dd [hh:mm:ss]\n"); + printf(" banset 0 (0 = un-banished)\n"); + printf(" ban/banish yyyy/mm/dd hh:mm:ss \n"); + printf(" unban/unbanish \n"); + printf(" Default time [hh:mm:ss]: 23:59:59.\n"); + ladmin_log("Incomplete parameters to set a ban ('banset' or 'ban' command)." RETCODE); + } + return 136; + } + + if (time[0] == '\0') + strcpy(time, "23:59:59"); + + return bansetaccountsub(name, date, time); +} + +//------------------------------------------------- +// Sub-function: unbanishment of an account (unban) +//------------------------------------------------- +int unbanaccount(char* param) { + char name[1023]; + + memset(name, '\0', sizeof(name)); + + if (strlen(param) == 0 || + (sscanf(param, "\"%[^\"]\"", name) < 1 && + sscanf(param, "'%[^']'", name) < 1 && + sscanf(param, "%[^\r\n]", name) < 1) || + strlen(name) == 0) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte svp.\n"); + printf(": banset aaaa/mm/jj [hh:mm:ss]\n"); + printf(" banset 0 (0 = d-bani)\n"); + printf(" ban/banish aaaa/mm/jj hh:mm:ss \n"); + printf(" unban/unbanish \n"); + printf(" Heure par d馭aut [hh:mm:ss]: 23:59:59.\n"); + ladmin_log("Nombre incorrect de param鑼res pour fixer un ban (commande 'unban')." RETCODE); + } else { + printf("Please input an account name.\n"); + printf(": banset yyyy/mm/dd [hh:mm:ss]\n"); + printf(" banset 0 (0 = un-banished)\n"); + printf(" ban/banish yyyy/mm/dd hh:mm:ss \n"); + printf(" unban/unbanish \n"); + printf(" Default time [hh:mm:ss]: 23:59:59.\n"); + ladmin_log("Incomplete parameters to set a ban ('unban' command)." RETCODE); + } + return 136; + } + + return bansetaccountsub(name, "0", ""); +} + +//--------------------------------------------------------- +// Sub-function: Asking to check the validity of a password +// (Note: never send back a password with login-server!! security of passwords) +//--------------------------------------------------------- +int checkaccount(char* param) { + char name[1023], password[1023]; + + memset(name, '\0', sizeof(name)); + memset(password, '\0', sizeof(password)); + + if (sscanf(param, "\"%[^\"]\" %[^\r\n]", name, password) < 1 && // password can be void + sscanf(param, "'%[^']' %[^\r\n]", name, password) < 1 && // password can be void + sscanf(param, "%s %[^\r\n]", name, password) < 1) { // password can be void + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte svp.\n"); + printf(" check testname motdepasse\n"); + ladmin_log("Nombre incorrect de param鑼res pour tester le mot d'un passe d'un compte (commande 'check')." RETCODE); + } else { + printf("Please input an account name.\n"); + printf(" check testname password\n"); + ladmin_log("Incomplete parameters to check the password of an account ('check' command)." RETCODE); + } + return 136; + } + + if (verify_accountname(name) == 0) { + return 102; + } + + if (strlen(password) == 0) { + if (typepasswd(password) == 0) + return 134; + } + if (verify_password(password) == 0) + return 131; + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour test un mot de passe." RETCODE); + } else { + ladmin_log("Request to login-server to check a password." RETCODE); + } + + WFIFOW(login_fd,0) = 0x793a; + memcpy(WFIFOP(login_fd,2), name, 24); + memcpy(WFIFOP(login_fd,26), password, 24); + WFIFOSET(login_fd,50); + bytes_to_read = 1; + + return 0; +} + +//------------------------------------------------ +// Sub-function: Asking for deletion of an account +//------------------------------------------------ +int delaccount(char* param) { + char name[1023]; + char letter; + char confirm[1023]; + int i; + + memset(name, '\0', sizeof(name)); + + if (strlen(param) == 0 || + (sscanf(param, "\"%[^\"]\"", name) < 1 && + sscanf(param, "'%[^']'", name) < 1 && + sscanf(param, "%[^\r\n]", name) < 1) || + strlen(name) == 0) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte svp.\n"); + printf(" del nomtestasupprimer\n"); + ladmin_log("Aucun nom donn pour supprimer un compte (commande 'delete')." RETCODE); + } else { + printf("Please input an account name.\n"); + printf(" del testnametodelete\n"); + ladmin_log("No name given to delete an account ('delete' command)." RETCODE); + } + return 136; + } + + if (verify_accountname(name) == 0) { + return 102; + } + + memset(confirm, '\0', sizeof(confirm)); + while ((confirm[0] != 'o' || defaultlanguage != 'F') && confirm[0] != 'n' && (confirm[0] != 'y' || defaultlanguage == 'F')) { + if (defaultlanguage == 'F') + printf("\033[1;36m ** Etes-vous vraiment sr de vouloir SUPPRIMER le compte [$userid]? (o/n) > \033[0m"); + else + printf("\033[1;36m ** Are you really sure to DELETE account [$userid]? (y/n) > \033[0m"); + fflush(stdout); + memset(confirm, '\0', sizeof(confirm)); + i = 0; + while ((letter = getchar()) != '\n') + confirm[i++] = letter; + } + + if (confirm[0] == 'n') { + if (defaultlanguage == 'F') { + printf("Suppression annul馥.\n"); + ladmin_log("Suppression annul馥 par l'utilisateur (commande 'delete')." RETCODE); + } else { + printf("Deletion canceled.\n"); + ladmin_log("Deletion canceled by user ('delete' command)." RETCODE); + } + return 121; + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour d騁ruire un compte." RETCODE); + } else { + ladmin_log("Request to login-server to delete an acount." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7932; + memcpy(WFIFOP(login_fd,2), name, 24); + WFIFOSET(login_fd,26); + bytes_to_read = 1; + + return 0; +} + +//---------------------------------------------------------- +// Sub-function: Asking to modification of an account e-mail +//---------------------------------------------------------- +int changeemail(char* param) { + char name[1023], email[1023]; + + memset(name, '\0', sizeof(name)); + memset(email, '\0', sizeof(email)); + + if (sscanf(param, "\"%[^\"]\" %[^\r\n]", name, email) < 2 && + sscanf(param, "'%[^']' %[^\r\n]", name, email) < 2 && + sscanf(param, "%s %[^\r\n]", name, email) < 2) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte et une email svp.\n"); + printf(" email testname nouveauemail\n"); + ladmin_log("Nombre incorrect de param鑼res pour changer l'email d'un compte (commande 'email')." RETCODE); + } else { + printf("Please input an account name and an email.\n"); + printf(" email testname newemail\n"); + ladmin_log("Incomplete parameters to change the email of an account ('email' command)." RETCODE); + } + return 136; + } + + if (verify_accountname(name) == 0) { + return 102; + } + + if (strlen(email) < 3) { + if (defaultlanguage == 'F') { + printf("Email trop courte [%s]. Entrez une e-mail valide svp.\n", email); + ladmin_log("Email trop courte [%s]. Entrez une e-mail valide svp." RETCODE, email); + } else { + printf("Email is too short [%s]. Please input a valid e-mail.\n", email); + ladmin_log("Email is too short [%s]. Please input a valid e-mail." RETCODE, email); + } + return 109; + } + if (strlen(email) > 39) { + if (defaultlanguage == 'F') { + printf("Email trop longue [%s]. Entrez une e-mail de 39 caract鑽es maximum svp.\n", email); + ladmin_log("Email trop longue [%s]. Entrez une e-mail de 39 caract鑽es maximum svp." RETCODE, email); + } else { + printf("Email is too long [%s]. Please input an e-mail with 39 bytes at the most.\n", email); + ladmin_log("Email is too long [%s]. Please input an e-mail with 39 bytes at the most." RETCODE, email); + } + return 109; + } + if (e_mail_check(email) == 0) { + if (defaultlanguage == 'F') { + printf("Email incorrecte [%s]. Entrez une e-mail valide svp.\n", email); + ladmin_log("Email incorrecte [%s]. Entrez une e-mail valide svp." RETCODE, email); + } else { + printf("Invalid email [%s]. Please input a valid e-mail.\n", email); + ladmin_log("Invalid email [%s]. Please input a valid e-mail." RETCODE, email); + } + return 109; + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour changer une email." RETCODE); + } else { + ladmin_log("Request to login-server to change an email." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7940; + memcpy(WFIFOP(login_fd,2), name, 24); + memcpy(WFIFOP(login_fd,26), email, 40); + WFIFOSET(login_fd,66); + bytes_to_read = 1; + + return 0; +} + +//----------------------------------------------------- +// Sub-function: Asking of the number of online players +//----------------------------------------------------- +int getlogincount() { + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour obtenir le nombre de joueurs en jeu." RETCODE); + } else { + ladmin_log("Request to login-server to obtain the # of online players." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7938; + WFIFOSET(login_fd,2); + bytes_to_read = 1; + + return 0; +} + +//---------------------------------------------------------- +// Sub-function: Asking to modify the GM level of an account +//---------------------------------------------------------- +int changegmlevel(char* param) { + char name[1023]; + int GM_level; + + memset(name, '\0', sizeof(name)); + GM_level = 0; + + if (sscanf(param, "\"%[^\"]\" %d", name, &GM_level) < 1 && + sscanf(param, "'%[^']' %d", name, &GM_level) < 1 && + sscanf(param, "%s %d", name, &GM_level) < 1) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte et un niveau de GM svp.\n"); + printf(" gm nomtest 80\n"); + ladmin_log("Nombre incorrect de param鑼res pour changer le Niveau de GM d'un compte (commande 'gm')." RETCODE); + } else { + printf("Please input an account name and a GM level.\n"); + printf(" gm testname 80\n"); + ladmin_log("Incomplete parameters to change the GM level of an account ('gm' command)." RETCODE); + } + return 136; + } + + if (verify_accountname(name) == 0) { + return 102; + } + + if (GM_level < 0 || GM_level > 99) { + if (defaultlanguage == 'F') { + printf("Niveau de GM incorrect [%d]. Entrez une valeur de 0 99 svp.\n", GM_level); + ladmin_log("Niveau de GM incorrect [%d]. La valeur peut 黎re de 0 99." RETCODE, GM_level); + } else { + printf("Illegal GM level [%d]. Please input a value from 0 to 99.\n", GM_level); + ladmin_log("Illegal GM level [%d]. The value can be from 0 to 99." RETCODE, GM_level); + } + return 103; + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour changer un niveau de GM." RETCODE); + } else { + ladmin_log("Request to login-server to change a GM level." RETCODE); + } + + WFIFOW(login_fd,0) = 0x793e; + memcpy(WFIFOP(login_fd,2), name, 24); + WFIFOB(login_fd,26) = GM_level; + WFIFOSET(login_fd,27); + bytes_to_read = 1; + + return 0; +} + +//--------------------------------------------- +// Sub-function: Asking to obtain an account id +//--------------------------------------------- +int idaccount(char* param) { + char name[1023]; + + memset(name, '\0', sizeof(name)); + + if (strlen(param) == 0 || + (sscanf(param, "\"%[^\"]\"", name) < 1 && + sscanf(param, "'%[^']'", name) < 1 && + sscanf(param, "%[^\r\n]", name) < 1) || + strlen(name) == 0) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte svp.\n"); + printf(" id nomtest\n"); + ladmin_log("Aucun nom donn pour rechecher l'id d'un compte (commande 'id')." RETCODE); + } else { + printf("Please input an account name.\n"); + printf(" id testname\n"); + ladmin_log("No name given to search an account id ('id' command)." RETCODE); + } + return 136; + } + + if (verify_accountname(name) == 0) { + return 102; + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour connatre l'id d'un compte." RETCODE); + } else { + ladmin_log("Request to login-server to know an account id." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7944; + memcpy(WFIFOP(login_fd,2), name, 24); + WFIFOSET(login_fd,26); + bytes_to_read = 1; + + return 0; +} + +//---------------------------------------------------------------------------- +// Sub-function: Asking to displaying information about an account (by its id) +//---------------------------------------------------------------------------- +int infoaccount(int account_id) { + if (account_id < 0) { + if (defaultlanguage == 'F') { + printf("Entrez un id ayant une valeur positive svp.\n"); + ladmin_log("Une valeur n馮ative a 騁 donn pour trouver le compte." RETCODE); + } else { + printf("Please input a positive value for the id.\n"); + ladmin_log("Negative value was given to found the account." RETCODE); + } + return 136; + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour obtenir le information d'un compte (par l'id)." RETCODE); + } else { + ladmin_log("Request to login-server to obtain information about an account (by its id)." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7954; + WFIFOL(login_fd,2) = account_id; + WFIFOSET(login_fd,6); + bytes_to_read = 1; + + return 0; +} + +//--------------------------------------- +// Sub-function: Send a broadcast message +//--------------------------------------- +int sendbroadcast(short type, char* message) { + if (strlen(message) == 0) { + if (defaultlanguage == 'F') { + printf("Entrez un message svp.\n"); + if (type == 0) { + printf(" kami un message\n"); + } else { + printf(" kamib un message\n"); + } + ladmin_log("Le message est vide (commande 'kami(b)')." RETCODE); + } else { + printf("Please input a message.\n"); + if (type == 0) { + printf(" kami a message\n"); + } else { + printf(" kamib a message\n"); + } + ladmin_log("The message is void ('kami(b)' command)." RETCODE); + } + return 136; + } + + WFIFOW(login_fd,0) = 0x794e; + WFIFOW(login_fd,2) = type; + WFIFOL(login_fd,4) = strlen(message)+1; + memcpy(WFIFOP(login_fd,8), message, strlen(message)+1); + WFIFOSET(login_fd,8+strlen(message)+1); + bytes_to_read = 1; + + return 0; +} + +//-------------------------------------------- +// Sub-function: Change language of displaying +//-------------------------------------------- +int changelanguage(char* language) { + if (strlen(language) == 0) { + if (defaultlanguage == 'F') { + printf("Entrez une langue svp.\n"); + printf(" language english\n"); + printf(" language fran軋is\n"); + ladmin_log("La langue est vide (commande 'language')." RETCODE); + } else { + printf("Please input a language.\n"); + printf(" language english\n"); + printf(" language fran軋is\n"); + ladmin_log("The language is void ('language' command)." RETCODE); + } + return 136; + } + + language[0] = toupper(language[0]); + if (language[0] == 'F' || language[0] == 'E') { + defaultlanguage = language[0]; + if (defaultlanguage == 'F') { + printf("Changement de la langue d'affichage en Fran軋is.\n"); + ladmin_log("Changement de la langue d'affichage en Fran軋is." RETCODE); + } else { + printf("Displaying language changed to English.\n"); + ladmin_log("Displaying language changed to English." RETCODE); + } + } else { + if (defaultlanguage == 'F') { + printf("Langue non param騁r馥 (langues possibles: 'Fran軋is' ou 'English').\n"); + ladmin_log("Langue non param騁r馥 (Fran軋is ou English n馗essaire)." RETCODE); + } else { + printf("Undefined language (possible languages: Fran軋is or English).\n"); + ladmin_log("Undefined language (must be Fran軋is or English)." RETCODE); + } + } + + return 0; +} + +//-------------------------------------------------------- +// Sub-function: Asking to Displaying of the accounts list +//-------------------------------------------------------- +int listaccount(char* param, int type) { +//int list_first, list_last, list_type; // parameter to display a list of accounts + int i; + + list_type = type; + + // set default values + list_first = 0; + list_last = 0; + + if (list_type == 1) { // if listgm + // get all accounts = use default + } else if (list_type == 2) { // if search + for (i = 0; param[i]; i++) + param[i] = tolower(param[i]); + // get all accounts = use default + } else if (list_type == 3) { // if listban + // get all accounts = use default + } else if (list_type == 4) { // if listok + // get all accounts = use default + } else { // if list (list_type == 0) + switch(sscanf(param, "%d %d", &list_first, &list_last)) { + case 0: + // get all accounts = use default + break; + case 1: + list_last = 0; + // use tests of the following value + default: + if (list_first < 0) + list_first = 0; + if (list_last < list_first || list_last < 0) + list_last = 0; + break; + } + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour obtenir la liste des comptes de %d %d." RETCODE, list_first, list_last); + } else { + ladmin_log("Request to login-server to obtain the list of accounts from %d to %d." RETCODE, list_first, list_last); + } + + WFIFOW(login_fd,0) = 0x7920; + WFIFOL(login_fd,2) = list_first; + WFIFOL(login_fd,6) = list_last; + WFIFOSET(login_fd,10); + bytes_to_read = 1; + + // 0123456789 01 01234567890123456789012301234 012345 0123456789012345678901234567 + if (defaultlanguage == 'F') { + printf(" id_compte GM nom_utilisateur sexe count statut\n"); + } else { + printf("account_id GM user_name sex count state\n"); + } + printf("-------------------------------------------------------------------------------\n"); + list_count = 0; + + return 0; +} + +//-------------------------------------------- +// Sub-function: Asking to modify a memo field +//-------------------------------------------- +int changememo(char* param) { + char name[1023], memo[1023]; + + memset(name, '\0', sizeof(name)); + memset(memo, '\0', sizeof(memo)); + + if (sscanf(param, "\"%[^\"]\" %[^\r\n]", name, memo) < 1 && // memo can be void + sscanf(param, "'%[^']' %[^\r\n]", name, memo) < 1 && // memo can be void + sscanf(param, "%s %[^\r\n]", name, memo) < 1) { // memo can be void + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte et un m駑o svp.\n"); + printf(" memo nomtest nouveau memo\n"); + ladmin_log("Nombre incorrect de param鑼res pour changer le m駑o d'un compte (commande 'email')." RETCODE); + } else { + printf("Please input an account name and a memo.\n"); + printf(" memo testname new memo\n"); + ladmin_log("Incomplete parameters to change the memo of an account ('email' command)." RETCODE); + } + return 136; + } + + if (verify_accountname(name) == 0) { + return 102; + } + + if (strlen(memo) > 254) { + if (defaultlanguage == 'F') { + printf("M駑o trop long (%d caract鑽es).\n", strlen(memo)); + printf("Entrez un m駑o de 254 caract鑽es maximum svp.\n"); + ladmin_log("M駑o trop long (%d caract鑽es). Entrez un m駑o de 254 caract鑽es maximum svp." RETCODE, strlen(memo)); + } else { + printf("Memo is too long (%d characters).\n", strlen(memo)); + printf("Please input a memo of 254 bytes at the maximum.\n"); + ladmin_log("Email is too long (%d characters). Please input a memo of 254 bytes at the maximum." RETCODE, strlen(memo)); + } + return 102; + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour changer un m駑o." RETCODE); + } else { + ladmin_log("Request to login-server to change a memo." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7942; + memcpy(WFIFOP(login_fd,2), name, 24); + WFIFOW(login_fd,26) = strlen(memo); + if (strlen(memo) > 0) + memcpy(WFIFOP(login_fd,28), memo, strlen(memo)); + WFIFOSET(login_fd,28+strlen(memo)); + bytes_to_read = 1; + + return 0; +} + +//----------------------------------------------- +// Sub-function: Asking to obtain an account name +//----------------------------------------------- +int nameaccount(int id) { + if (id < 0) { + if (defaultlanguage == 'F') { + printf("Entrez un id ayant une valeur positive svp.\n"); + ladmin_log("Id n馮atif donn pour rechecher le nom d'un compte (commande 'name')." RETCODE); + } else { + printf("Please input a positive value for the id.\n"); + ladmin_log("Negativ id given to search an account name ('name' command)." RETCODE); + } + return 136; + } + + if (defaultlanguage == 'F') + ladmin_log("Envoi d'un requ黎e au serveur de logins pour connatre le nom d'un compte." RETCODE); + else + ladmin_log("Request to login-server to know an account name." RETCODE); + + WFIFOW(login_fd,0) = 0x7946; + WFIFOL(login_fd,2) = id; + WFIFOSET(login_fd,6); + bytes_to_read = 1; + + return 0; +} + +//------------------------------------------ +// Sub-function: Asking to modify a password +// (Note: never send back a password with login-server!! security of passwords) +//------------------------------------------ +int changepasswd(char* param) { + char name[1023], password[1023]; + + memset(name, '\0', sizeof(name)); + memset(password, '\0', sizeof(password)); + + if (sscanf(param, "\"%[^\"]\" %[^\r\n]", name, password) < 1 && + sscanf(param, "'%[^']' %[^\r\n]", name, password) < 1 && + sscanf(param, "%s %[^\r\n]", name, password) < 1) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte svp.\n"); + printf(" passwd nomtest nouveaumotdepasse\n"); + ladmin_log("Nombre incorrect de param鑼res pour changer le mot d'un passe d'un compte (commande 'password')." RETCODE); + } else { + printf("Please input an account name.\n"); + printf(" passwd testname newpassword\n"); + ladmin_log("Incomplete parameters to change the password of an account ('password' command)." RETCODE); + } + return 136; + } + + if (verify_accountname(name) == 0) { + return 102; + } + + if (strlen(password) == 0) { + if (typepasswd(password) == 0) + return 134; + } + if (verify_password(password) == 0) + return 131; + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour changer un mot de passe." RETCODE); + } else { + ladmin_log("Request to login-server to change a password." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7934; + memcpy(WFIFOP(login_fd,2), name, 24); + memcpy(WFIFOP(login_fd,26), password, 24); + WFIFOSET(login_fd,50); + bytes_to_read = 1; + + return 0; +} + +//---------------------------------------------------------------------- +// Sub-function: Request to login-server to reload GM configuration file +// this function have no answer +//---------------------------------------------------------------------- +int reloadGM() { + WFIFOW(login_fd,0) = 0x7955; + WFIFOSET(login_fd,2); + bytes_to_read = 0; + + if (defaultlanguage == 'F') { + ladmin_log("Demande de recharger le fichier de configuration des GM envoy馥." RETCODE); + printf("Demande de recharger le fichier de configuration des GM envoy馥.\n"); + printf("V駻ifiez les comptes GM actuels (apr鑚 rechargement):\n"); + } else { + ladmin_log("Request to reload the GM configuration file sended." RETCODE); + printf("Request to reload the GM configuration file sended.\n"); + printf("Check the actual GM accounts (after reloading):\n"); + } + listaccount(parameters, 1); // 1: to list only GM + + return 180; +} + +//----------------------------------------------------- +// Sub-function: Asking to modify the sex of an account +//----------------------------------------------------- +int changesex(char* param) { + char name[1023], sex[1023]; + + memset(name, '\0', sizeof(name)); + memset(sex, '\0', sizeof(sex)); + + if (sscanf(param, "\"%[^\"]\" %[^\r\n]", name, sex) < 2 && + sscanf(param, "'%[^']' %[^\r\n]", name, sex) < 2 && + sscanf(param, "%s %[^\r\n]", name, sex) < 2) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte et un sexe svp.\n"); + printf(" sex nomtest Male\n"); + ladmin_log("Nombre incorrect de param鑼res pour changer le sexe d'un compte (commande 'sex')." RETCODE); + } else { + printf("Please input an account name and a sex.\n"); + printf(" sex testname Male\n"); + ladmin_log("Incomplete parameters to change the sex of an account ('sex' command)." RETCODE); + } + return 136; + } + + if (verify_accountname(name) == 0) { + return 102; + } + + sex[0] = toupper(sex[0]); + if (strchr("MF", sex[0]) == NULL) { + if (defaultlanguage == 'F') { + printf("Sexe incorrect [%s]. Entrez M ou F svp.\n", sex); + ladmin_log("Sexe incorrect [%s]. Entrez M ou F svp." RETCODE, sex); + } else { + printf("Illegal gender [%s]. Please input M or F.\n", sex); + ladmin_log("Illegal gender [%s]. Please input M or F." RETCODE, sex); + } + return 103; + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour changer un sexe." RETCODE); + } else { + ladmin_log("Request to login-server to change a sex." RETCODE); + } + + WFIFOW(login_fd,0) = 0x793c; + memcpy(WFIFOP(login_fd,2), name, 24); + WFIFOB(login_fd,26) = sex[0]; + WFIFOSET(login_fd,27); + bytes_to_read = 1; + + return 0; +} + +//------------------------------------------------------------------------- +// Sub-function of sub-function changestate, blockaccount or unblockaccount +// Asking to modify the state of an account +//------------------------------------------------------------------------- +int changestatesub(char* name, int state, char* error_message7) { + char error_message[1023]; // need to use, because we can modify error_message7 + + memset(error_message, '\0', sizeof(error_message)); + strncpy(error_message, error_message7, sizeof(error_message)-1); + + if ((state < 0 || state > 9) && state != 100) { // Valid values: 0: ok, or value of the 0x006a packet + 1 + if (defaultlanguage == 'F') { + printf("Entrez une des statuts suivantes svp:\n"); + printf(" 0 = Compte ok 6 = Your Game's EXE file is not the latest version\n"); + } else { + printf("Please input one of these states:\n"); + printf(" 0 = Account ok 6 = Your Game's EXE file is not the latest version\n"); + } + printf(" 1 = Unregistered ID 7 = You are Prohibited to log in until + message\n"); + printf(" 2 = Incorrect Password 8 = Server is jammed due to over populated\n"); + printf(" 3 = This ID is expired 9 = No MSG\n"); + printf(" 4 = Rejected from Server 100 = This ID has been totally erased\n"); + printf(" 5 = You have been blocked by the GM Team\n"); + if (defaultlanguage == 'F') { + printf(" state nomtest 5\n"); + printf(" state nomtest 7 fin de votre ban\n"); + printf(" block \n"); + printf(" unblock \n"); + ladmin_log("Valeur incorrecte pour le statut d'un compte (commande 'state', 'block' ou 'unblock')." RETCODE); + } else { + printf(" state testname 5\n"); + printf(" state testname 7 end of your ban\n"); + printf(" block \n"); + printf(" unblock \n"); + ladmin_log("Invalid value for the state of an account ('state', 'block' or 'unblock' command)." RETCODE); + } + return 151; + } + + if (verify_accountname(name) == 0) { + return 102; + } + + if (state != 7) { + strcpy(error_message, "-"); + } else { + if (strlen(error_message) < 1) { + if (defaultlanguage == 'F') { + printf("Message d'erreur trop court. Entrez un message de 1-19 caract鑽es.\n"); + ladmin_log("Message d'erreur trop court. Entrez un message de 1-19 caract鑽es." RETCODE); + } else { + printf("Error message is too short. Please input a message of 1-19 bytes.\n"); + ladmin_log("Error message is too short. Please input a message of 1-19 bytes." RETCODE); + } + return 102; + } + if (strlen(error_message) > 19) { + if (defaultlanguage == 'F') { + printf("Message d'erreur trop long. Entrez un message de 1-19 caract鑽es.\n"); + ladmin_log("Message d'erreur trop long. Entrez un message de 1-19 caract鑽es." RETCODE); + } else { + printf("Error message is too long. Please input a message of 1-19 bytes.\n"); + ladmin_log("Error message is too long. Please input a message of 1-19 bytes." RETCODE); + } + return 102; + } + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour changer un statut." RETCODE); + } else { + ladmin_log("Request to login-server to change a state." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7936; + memcpy(WFIFOP(login_fd,2), name, 24); + WFIFOL(login_fd,26) = state; + memcpy(WFIFOP(login_fd,30), error_message, 20); + WFIFOSET(login_fd,50); + bytes_to_read = 1; + + return 0; +} + +//------------------------------------------------------- +// Sub-function: Asking to modify the state of an account +//------------------------------------------------------- +int changestate(char* param) { + char name[1023], error_message[1023]; + int state; + + memset(name, '\0', sizeof(name)); + memset(error_message, '\0', sizeof(error_message)); + + if (sscanf(param, "\"%[^\"]\" %d %[^\r\n]", name, &state, error_message) < 2 && + sscanf(param, "'%[^']' %d %[^\r\n]", name, &state, error_message) < 2 && + sscanf(param, "%s %d %[^\r\n]", name, &state, error_message) < 2) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte et un statut svp.\n"); + printf(" state nomtest 5\n"); + printf(" state nomtest 7 fin de votre ban\n"); + printf(" block \n"); + printf(" unblock \n"); + ladmin_log("Nombre incorrect de param鑼res pour changer le statut d'un compte (commande 'state')." RETCODE); + } else { + printf("Please input an account name and a state.\n"); + printf(" state testname 5\n"); + printf(" state testname 7 end of your ban\n"); + printf(" block \n"); + printf(" unblock \n"); + ladmin_log("Incomplete parameters to change the state of an account ('state' command)." RETCODE); + } + return 136; + } + + return changestatesub(name, state, error_message); +} + +//------------------------------------------- +// Sub-function: Asking to unblock an account +//------------------------------------------- +int unblockaccount(char* param) { + char name[1023]; + + memset(name, '\0', sizeof(name)); + + if (strlen(param) == 0 || + (sscanf(param, "\"%[^\"]\"", name) < 1 && + sscanf(param, "'%[^']'", name) < 1 && + sscanf(param, "%[^\r\n]", name) < 1) || + strlen(name) == 0) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte svp.\n"); + printf(" state nomtest 5\n"); + printf(" state nomtest 7 fin de votre ban\n"); + printf(" block \n"); + printf(" unblock \n"); + ladmin_log("Nombre incorrect de param鑼res pour changer le statut d'un compte (commande 'unblock')." RETCODE); + } else { + printf("Please input an account name.\n"); + printf(" state testname 5\n"); + printf(" state testname 7 end of your ban\n"); + printf(" block \n"); + printf(" unblock \n"); + ladmin_log("Incomplete parameters to change the state of an account ('unblock' command)." RETCODE); + } + return 136; + } + + return changestatesub(name, 0, "-"); // state 0, no error message +} + +//------------------------------------------- +// Sub-function: Asking to unblock an account +//------------------------------------------- +int blockaccount(char* param) { + char name[1023]; + + memset(name, '\0', sizeof(name)); + + if (strlen(param) == 0 || + (sscanf(param, "\"%[^\"]\"", name) < 1 && + sscanf(param, "'%[^']'", name) < 1 && + sscanf(param, "%[^\r\n]", name) < 1) || + strlen(name) == 0) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte svp.\n"); + printf(" state nomtest 5\n"); + printf(" state nomtest 7 fin de votre ban\n"); + printf(" block \n"); + printf(" unblock \n"); + ladmin_log("Nombre incorrect de param鑼res pour changer le statut d'un compte (commande 'block')." RETCODE); + } else { + printf("Please input an account name.\n"); + printf(" state testname 5\n"); + printf(" state testname 7 end of your ban\n"); + printf(" block \n"); + printf(" unblock \n"); + ladmin_log("Incomplete parameters to change the state of an account ('block' command)." RETCODE); + } + return 136; + } + + return changestatesub(name, 5, "-"); // state 5, no error message +} + +//--------------------------------------------------------------------- +// Sub-function: Add/substract time to the validity limit of an account +//--------------------------------------------------------------------- +int timeaddaccount(char* param) { + char name[1023], modif[1023]; + int year, month, day, hour, minute, second; + char * p_modif; + int value, i; + + memset(name, '\0', sizeof(name)); + memset(modif, '\0', sizeof(modif)); + year = month = day = hour = minute = second = 0; + + if (sscanf(param, "\"%[^\"]\" %[^\r\n]", name, modif) < 2 && + sscanf(param, "'%[^']' %[^\r\n]", name, modif) < 2 && + sscanf(param, "%s %[^\r\n]", name, modif) < 2) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte et un modificateur svp.\n"); + printf(" timeadd nomtest +1m-2mn1s-6y\n"); + printf(" Cette exemple ajoute 1 mois et 1 seconde, et soustrait 2 minutes\n"); + printf(" et 6 ans dans le m麥e temps.\n"); + ladmin_log("Nombre incorrect de param鑼res pour modifier une date limite d'utilisation (commande 'timeadd')." RETCODE); + } else { + printf("Please input an account name and a modifier.\n"); + printf(" : timeadd testname +1m-2mn1s-6y\n"); + printf(" this example adds 1 month and 1 second, and substracts 2 minutes\n"); + printf(" and 6 years at the same time.\n"); + ladmin_log("Incomplete parameters to modify a limit time ('timeadd' command)." RETCODE); + } + return 136; + } + if (verify_accountname(name) == 0) { + return 102; + } + + // lowercase for modif + for (i = 0; modif[i]; i++) + modif[i] = tolower(modif[i]); + p_modif = modif; + while (strlen(p_modif) > 0) { + value = atoi(p_modif); + if (value == 0) { + p_modif++; + } else { + if (p_modif[0] == '-' || p_modif[0] == '+') + p_modif++; + while (strlen(p_modif) > 0 && p_modif[0] >= '0' && p_modif[0] <= '9') { + p_modif++; + } + if (p_modif[0] == 's') { + second = value; + p_modif++; + } else if (p_modif[0] == 'm' && p_modif[1] == 'n') { + minute = value; + p_modif += 2; + } else if (p_modif[0] == 'h') { + hour = value; + p_modif++; + } else if (p_modif[0] == 'd' || p_modif[0] == 'j') { + day = value; + p_modif += 2; + } else if (p_modif[0] == 'm') { + month = value; + p_modif++; + } else if (p_modif[0] == 'y' || p_modif[0] == 'a') { + year = value; + p_modif++; + } else { + p_modif++; + } + } + } + + if (defaultlanguage == 'F') { + printf(" ann馥: %d\n", year); + printf(" mois: %d\n", month); + printf(" jour: %d\n", day); + printf(" heure: %d\n", hour); + printf(" minute: %d\n", minute); + printf(" seconde: %d\n", second); + } else { + printf(" year: %d\n", year); + printf(" month: %d\n", month); + printf(" day: %d\n", day); + printf(" hour: %d\n", hour); + printf(" minute: %d\n", minute); + printf(" second: %d\n", second); + } + + if (year == 0 && month == 0 && day == 0 && hour == 0 && minute == 0 && second == 0) { + if (defaultlanguage == 'F') { + printf("Vous devez entrer un ajustement avec cette commande, svp:\n"); + printf(" Valeur d'ajustement (-1, 1, +1, etc...)\n"); + printf(" El駑ent modifi:\n"); + printf(" a ou y: ann馥\n"); + printf(" m: mois\n"); + printf(" j ou d: jour\n"); + printf(" h: heure\n"); + printf(" mn: minute\n"); + printf(" s: seconde\n"); + printf(" timeadd nomtest +1m-2mn1s-6y\n"); + printf(" Cette exemple ajoute 1 mois et 1 seconde, et soustrait 2 minutes\n"); + printf(" et 6 ans dans le m麥e temps.\n"); + ladmin_log("Aucun ajustement n'est pas un ajustement (commande 'timeadd')." RETCODE); + } else { + printf("Please give an adjustment with this command:\n"); + printf(" Adjustment value (-1, 1, +1, etc...)\n"); + printf(" Modified element:\n"); + printf(" a or y: year\n"); + printf(" m: month\n"); + printf(" j or d: day\n"); + printf(" h: hour\n"); + printf(" mn: minute\n"); + printf(" s: second\n"); + printf(" timeadd testname +1m-2mn1s-6y\n"); + printf(" this example adds 1 month and 1 second, and substracts 2 minutes\n"); + printf(" and 6 years at the same time.\n"); + ladmin_log("No adjustment isn't an adjustment ('timeadd' command)." RETCODE); + } + return 137; + } + if (year > 127 || year < -127) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement d'ann馥s correct (de -127 127), svp.\n"); + ladmin_log("Ajustement de l'ann馥 hors norme ('timeadd' command)." RETCODE); + } else { + printf("Please give a correct adjustment for the years (from -127 to 127).\n"); + ladmin_log("Abnormal adjustement for the year ('timeadd' command)." RETCODE); + } + return 137; + } + if (month > 255 || month < -255) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement de mois correct (de -255 255), svp.\n"); + ladmin_log("Ajustement du mois hors norme ('timeadd' command)." RETCODE); + } else { + printf("Please give a correct adjustment for the months (from -255 to 255).\n"); + ladmin_log("Abnormal adjustement for the month ('timeadd' command)." RETCODE); + } + return 137; + } + if (day > 32767 || day < -32767) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement de jours correct (de -32767 32767), svp.\n"); + ladmin_log("Ajustement des jours hors norme ('timeadd' command)." RETCODE); + } else { + printf("Please give a correct adjustment for the days (from -32767 to 32767).\n"); + ladmin_log("Abnormal adjustement for the days ('timeadd' command)." RETCODE); + } + return 137; + } + if (hour > 32767 || hour < -32767) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement d'heures correct (de -32767 32767), svp.\n"); + ladmin_log("Ajustement des heures hors norme ('timeadd' command)." RETCODE); + } else { + printf("Please give a correct adjustment for the hours (from -32767 to 32767).\n"); + ladmin_log("Abnormal adjustement for the hours ('timeadd' command)." RETCODE); + } + return 137; + } + if (minute > 32767 || minute < -32767) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement de minutes correct (de -32767 32767), svp.\n"); + ladmin_log("Ajustement des minutes hors norme ('timeadd' command)." RETCODE); + } else { + printf("Please give a correct adjustment for the minutes (from -32767 to 32767).\n"); + ladmin_log("Abnormal adjustement for the minutes ('timeadd' command)." RETCODE); + } + return 137; + } + if (second > 32767 || second < -32767) { + if (defaultlanguage == 'F') { + printf("Entrez un ajustement de secondes correct (de -32767 32767), svp.\n"); + ladmin_log("Ajustement des secondes hors norme ('timeadd' command)." RETCODE); + } else { + printf("Please give a correct adjustment for the seconds (from -32767 to 32767).\n"); + ladmin_log("Abnormal adjustement for the seconds ('timeadd' command)." RETCODE); + } + return 137; + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour modifier une date limite d'utilisation." RETCODE); + } else { + ladmin_log("Request to login-server to modify a time limit." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7950; + memcpy(WFIFOP(login_fd,2), name, 24); + WFIFOW(login_fd,26) = (short)year; + WFIFOW(login_fd,28) = (short)month; + WFIFOW(login_fd,30) = (short)day; + WFIFOW(login_fd,32) = (short)hour; + WFIFOW(login_fd,34) = (short)minute; + WFIFOW(login_fd,36) = (short)second; + WFIFOSET(login_fd,38); + bytes_to_read = 1; + + return 0; +} + +//------------------------------------------------- +// Sub-function: Set a validity limit of an account +//------------------------------------------------- +int timesetaccount(char* param) { + char name[1023], date[1023], time[1023]; + int year, month, day, hour, minute, second; + time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited) + struct tm *tmtime; + + memset(name, '\0', sizeof(name)); + memset(date, '\0', sizeof(date)); + memset(time, '\0', sizeof(time)); + year = month = day = hour = minute = second = 0; + connect_until_time = 0; + tmtime = localtime(&connect_until_time); // initialize + + if (sscanf(param, "\"%[^\"]\" %s %[^\r\n]", name, date, time) < 2 && // if date = 0, time can be void + sscanf(param, "'%[^']' %s %[^\r\n]", name, date, time) < 2 && // if date = 0, time can be void + sscanf(param, "%s %s %[^\r\n]", name, date, time) < 2) { // if date = 0, time can be void + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte, une date et une heure svp.\n"); + printf(": timeset aaaa/mm/jj [hh:mm:ss]\n"); + printf(" timeset 0 (0 = illimit)\n"); + printf(" Heure par d馭aut [hh:mm:ss]: 23:59:59.\n"); + ladmin_log("Nombre incorrect de param鑼res pour fixer une date limite d'utilisation (commande 'timeset')." RETCODE); + } else { + printf("Please input an account name, a date and a hour.\n"); + printf(": timeset yyyy/mm/dd [hh:mm:ss]\n"); + printf(" timeset 0 (0 = unlimited)\n"); + printf(" Default time [hh:mm:ss]: 23:59:59.\n"); + ladmin_log("Incomplete parameters to set a limit time ('timeset' command)." RETCODE); + } + return 136; + } + if (verify_accountname(name) == 0) { + return 102; + } + + if (time[0] == '\0') + strcpy(time, "23:59:59"); + + if (atoi(date) != 0 && + ((sscanf(date, "%d/%d/%d", &year, &month, &day) < 3 && + sscanf(date, "%d-%d-%d", &year, &month, &day) < 3 && + sscanf(date, "%d.%d.%d", &year, &month, &day) < 3 && + sscanf(date, "%d'%d'%d", &year, &month, &day) < 3) || + sscanf(time, "%d:%d:%d", &hour, &minute, &second) < 3)) { + if (defaultlanguage == 'F') { + printf("Entrez 0 ou une date et une heure svp (format: 0 ou aaaa/mm/jj hh:mm:ss).\n"); + ladmin_log("Format incorrect pour la date/heure ('timeset' command)." RETCODE); + } else { + printf("Please input 0 or a date and a time (format: 0 or yyyy/mm/dd hh:mm:ss).\n"); + ladmin_log("Invalid format for the date/time ('timeset' command)." RETCODE); + } + return 102; + } + + if (atoi(date) == 0) { + connect_until_time = 0; + } else { + if (year < 70) { + year = year + 100; + } + if (year >= 1900) { + year = year - 1900; + } + if (month < 1 || month > 12) { + if (defaultlanguage == 'F') { + printf("Entrez un mois correct svp (entre 1 et 12).\n"); + ladmin_log("Mois incorrect pour la date ('timeset' command)." RETCODE); + } else { + printf("Please give a correct value for the month (from 1 to 12).\n"); + ladmin_log("Invalid month for the date ('timeset' command)." RETCODE); + } + return 102; + } + month = month - 1; + if (day < 1 || day > 31) { + if (defaultlanguage == 'F') { + printf("Entrez un jour correct svp (entre 1 et 31).\n"); + ladmin_log("Jour incorrect pour la date ('timeset' command)." RETCODE); + } else { + printf("Please give a correct value for the day (from 1 to 31).\n"); + ladmin_log("Invalid day for the date ('timeset' command)." RETCODE); + } + return 102; + } + if (((month == 3 || month == 5 || month == 8 || month == 10) && day > 30) || + (month == 1 && day > 29)) { + if (defaultlanguage == 'F') { + printf("Entrez un jour correct en fonction du mois (%d) svp.\n", month); + ladmin_log("Jour incorrect pour ce mois correspondant ('timeset' command)." RETCODE); + } else { + printf("Please give a correct value for a day of this month (%d).\n", month); + ladmin_log("Invalid day for this month ('timeset' command)." RETCODE); + } + return 102; + } + if (hour < 0 || hour > 23) { + if (defaultlanguage == 'F') { + printf("Entrez une heure correcte svp (entre 0 et 23).\n"); + ladmin_log("Heure incorrecte pour l'heure ('timeset' command)." RETCODE); + } else { + printf("Please give a correct value for the hour (from 0 to 23).\n"); + ladmin_log("Invalid hour for the time ('timeset' command)." RETCODE); + } + return 102; + } + if (minute < 0 || minute > 59) { + if (defaultlanguage == 'F') { + printf("Entrez des minutes correctes svp (entre 0 et 59).\n"); + ladmin_log("Minute incorrecte pour l'heure ('timeset' command)." RETCODE); + } else { + printf("Please give a correct value for the minutes (from 0 to 59).\n"); + ladmin_log("Invalid minute for the time ('timeset' command)." RETCODE); + } + return 102; + } + if (second < 0 || second > 59) { + if (defaultlanguage == 'F') { + printf("Entrez des secondes correctes svp (entre 0 et 59).\n"); + ladmin_log("Seconde incorrecte pour l'heure ('timeset' command)." RETCODE); + } else { + printf("Please give a correct value for the seconds (from 0 to 59).\n"); + ladmin_log("Invalid second for the time ('timeset' command)." RETCODE); + } + return 102; + } + tmtime->tm_year = year; + tmtime->tm_mon = month; + tmtime->tm_mday = day; + tmtime->tm_hour = hour; + tmtime->tm_min = minute; + tmtime->tm_sec = second; + tmtime->tm_isdst = -1; // -1: no winter/summer time modification + connect_until_time = mktime(tmtime); + if (connect_until_time == -1) { + if (defaultlanguage == 'F') { + printf("Date incorrecte.\n"); + printf("Ajoutez 0 ou une date et une heure svp (format: 0 ou aaaa/mm/jj hh:mm:ss).\n"); + ladmin_log("Date incorrecte. ('timeset' command)." RETCODE); + } else { + printf("Invalid date.\n"); + printf("Please add 0 or a date and a time (format: 0 or yyyy/mm/dd hh:mm:ss).\n"); + ladmin_log("Invalid date. ('timeset' command)." RETCODE); + } + return 102; + } + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour fixer une date limite d'utilisation." RETCODE); + } else { + ladmin_log("Request to login-server to set a time limit." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7948; + memcpy(WFIFOP(login_fd,2), name, 24); + WFIFOL(login_fd,26) = (int)connect_until_time; + WFIFOSET(login_fd,30); + bytes_to_read = 1; + + return 0; +} + +//------------------------------------------------------------------------------ +// Sub-function: Asking to displaying information about an account (by its name) +//------------------------------------------------------------------------------ +int whoaccount(char* param) { + char name[1023]; + + memset(name, '\0', sizeof(name)); + + if (strlen(param) == 0 || + (sscanf(param, "\"%[^\"]\"", name) < 1 && + sscanf(param, "'%[^']'", name) < 1 && + sscanf(param, "%[^\r\n]", name) < 1) || + strlen(name) == 0) { + if (defaultlanguage == 'F') { + printf("Entrez un nom de compte svp.\n"); + printf(" who nomtest\n"); + ladmin_log("Aucun nom n'a 騁 donn pour trouver le compte." RETCODE); + } else { + printf("Please input an account name.\n"); + printf(" who testname\n"); + ladmin_log("No name was given to found the account." RETCODE); + } + return 136; + } + if (verify_accountname(name) == 0) { + return 102; + } + + if (defaultlanguage == 'F') { + ladmin_log("Envoi d'un requ黎e au serveur de logins pour obtenir le information d'un compte (par le nom)." RETCODE); + } else { + ladmin_log("Request to login-server to obtain information about an account (by its name)." RETCODE); + } + + WFIFOW(login_fd,0) = 0x7952; + memcpy(WFIFOP(login_fd,2), name, 24); + WFIFOSET(login_fd,26); + bytes_to_read = 1; + + return 0; +} + +//-------------------------------------------------------- +// Sub-function: Asking of the version of the login-server +//-------------------------------------------------------- +int checkloginversion() { + if (defaultlanguage == 'F') + ladmin_log("Envoi d'un requ黎e au serveur de logins pour obtenir sa version." RETCODE); + else + ladmin_log("Request to login-server to obtain its version." RETCODE); + + WFIFOW(login_fd,0) = 0x7530; + WFIFOSET(login_fd,2); + bytes_to_read = 1; + + return 0; +} + +//--------------------------------------------- +// Prompt function +// this function wait until user type a command +// and analyse the command. +//--------------------------------------------- +int prompt() { + int i, j; + char buf[1024]; + char *p; + + // while we don't wait new packets + while (bytes_to_read == 0) { + // for help with the console colors look here: + // http://www.edoceo.com/liberum/?doc=printf-with-color + // some code explanation (used here): + // \033[2J : clear screen and go up/left (0, 0 position) + // \033[K : clear line from actual position to end of the line + // \033[0m : reset color parameter + // \033[1m : use bold for font + printf("\n"); + if (defaultlanguage == 'F') + printf("\033[32mPour afficher les commandes, tapez 'Entr馥'.\033[0m\n"); + else + printf("\033[32mTo list the commands, type 'enter'.\033[0m\n"); + printf("\033[0;36mLadmin-> \033[0m"); + printf("\033[1m"); + fflush(stdout); + + // get command and parameter + memset(buf, '\0', sizeof(buf)); + fflush(stdin); + fgets(buf, 1023, stdin); + buf[1023] = '\0'; + + printf("\033[0m"); + fflush(stdout); + + // remove final \n + if((p = strrchr(buf, '\n')) != NULL) + p[0] = '\0'; + // remove all control char + for (i = 0; buf[i]; i++) + if (buf[i] < 32) { + // remove cursor control. + if (buf[i] == 27 && buf[i+1] == '[' && + (buf[i+2] == 'H' || // home position (cursor) + buf[i+2] == 'J' || // clear screen + buf[i+2] == 'A' || // up 1 line + buf[i+2] == 'B' || // down 1 line + buf[i+2] == 'C' || // right 1 position + buf[i+2] == 'D' || // left 1 position + buf[i+2] == 'G')) { // center cursor (windows) + for (j = i; buf[j]; j++) + buf[j] = buf[j+3]; + } else if (buf[i] == 27 && buf[i+1] == '[' && buf[i+2] == '2' && buf[i+3] == 'J') { // clear screen + for (j = i; buf[j]; j++) + buf[j] = buf[j+4]; + } else if (buf[i] == 27 && buf[i+1] == '[' && buf[i+3] == '~' && + (buf[i+2] == '1' || // home (windows) + buf[i+2] == '2' || // insert (windows) + buf[i+2] == '3' || // del (windows) + buf[i+2] == '4' || // end (windows) + buf[i+2] == '5' || // pgup (windows) + buf[i+2] == '6')) { // pgdown (windows) + for (j = i; buf[j]; j++) + buf[j] = buf[j+4]; + } else { + // remove other control char. + for (j = i; buf[j]; j++) + buf[j] = buf[j+1]; + } + i--; + } + + // extract command name and parameters + memset(command, '\0', sizeof(command)); + memset(parameters, '\0', sizeof(parameters)); + sscanf(buf, "%1023s %[^\n]", command, parameters); + command[1023] = '\0'; + parameters[1023] = '\0'; + + // lowercase for command line + for (i = 0; command[i]; i++) + command[i] = tolower(command[i]); + + if (command[0] == '?' || strlen(command) == 0) { + if (defaultlanguage == 'F') { + strcpy(buf, "aide"); + strcpy(command, "aide"); + } else { + strcpy(buf, "help"); + strcpy(command, "help"); + } + } + + // Analyse of the command + check_command(command); // give complete name to the command + + if (strlen(parameters) == 0) { + if (defaultlanguage == 'F') { + ladmin_log("Commande: '%s' (sans param鑼re)" RETCODE, command, parameters); + } else { + ladmin_log("Command: '%s' (without parameters)" RETCODE, command, parameters); + } + } else { + if (defaultlanguage == 'F') { + ladmin_log("Commande: '%s', param鑼res: '%s'" RETCODE, command, parameters); + } else { + ladmin_log("Command: '%s', parameters: '%s'" RETCODE, command, parameters); + } + } + + // Analyse of the command +// help + if (strcmp(command, "aide") == 0) { + display_help(parameters, 1); // 1: french + } else if (strcmp(command, "help") == 0 ) { + display_help(parameters, 0); // 0: english +// general commands + } else if (strcmp(command, "add") == 0) { + addaccount(parameters, 0); // 0: no email + } else if (strcmp(command, "ban") == 0) { + banaccount(parameters); + } else if (strcmp(command, "banadd") == 0) { + banaddaccount(parameters); + } else if (strcmp(command, "banset") == 0) { + bansetaccount(parameters); + } else if (strcmp(command, "block") == 0) { + blockaccount(parameters); + } else if (strcmp(command, "check") == 0) { + checkaccount(parameters); + } else if (strcmp(command, "create") == 0) { + addaccount(parameters, 1); // 1: with email + } else if (strcmp(command, "delete") == 0) { + delaccount(parameters); + } else if (strcmp(command, "email") == 0) { + changeemail(parameters); + } else if (strcmp(command, "getcount") == 0) { + getlogincount(); + } else if (strcmp(command, "gm") == 0) { + changegmlevel(parameters); + } else if (strcmp(command, "id") == 0) { + idaccount(parameters); + } else if (strcmp(command, "info") == 0) { + infoaccount(atoi(parameters)); + } else if (strcmp(command, "kami") == 0) { + sendbroadcast(0, parameters); // flag for normal + } else if (strcmp(command, "kamib") == 0) { + sendbroadcast(0x10, parameters); // flag for blue + } else if (strcmp(command, "language") == 0) { + changelanguage(parameters); + } else if (strcmp(command, "list") == 0) { + listaccount(parameters, 0); // 0: to list all + } else if (strcmp(command, "listban") == 0) { + listaccount(parameters, 3); // 3: to list only accounts with state or bannished + } else if (strcmp(command, "listgm") == 0) { + listaccount(parameters, 1); // 1: to list only GM + } else if (strcmp(command, "listok") == 0) { + listaccount(parameters, 4); // 4: to list only accounts without state and not bannished + } else if (strcmp(command, "memo") == 0) { + changememo(parameters); + } else if (strcmp(command, "name") == 0) { + nameaccount(atoi(parameters)); + } else if (strcmp(command, "password") == 0) { + changepasswd(parameters); + } else if (strcmp(command, "reloadgm") == 0) { + reloadGM(); + } else if (strcmp(command, "search") == 0) { // no regex in C version + listaccount(parameters, 2); // 2: to list with pattern + } else if (strcmp(command, "sex") == 0) { + changesex(parameters); + } else if (strcmp(command, "state") == 0) { + changestate(parameters); + } else if (strcmp(command, "timeadd") == 0) { + timeaddaccount(parameters); + } else if (strcmp(command, "timeset") == 0) { + timesetaccount(parameters); + } else if (strcmp(command, "unban") == 0) { + unbanaccount(parameters); + } else if (strcmp(command, "unblock") == 0) { + unblockaccount(parameters); + } else if (strcmp(command, "version") == 0) { + checkloginversion(); + } else if (strcmp(command, "who") == 0) { + whoaccount(parameters); +// quit + } else if (strcmp(command, "quit") == 0 || + strcmp(command, "exit") == 0 || + strcmp(command, "end") == 0) { + if (defaultlanguage == 'F') { + printf("Au revoir.\n"); + } else { + printf("Bye.\n"); + } + exit(0); +// unknown command + } else { + if (defaultlanguage == 'F') { + printf("Commande inconnue [%s].\n", buf); + ladmin_log("Commande inconnue [%s]." RETCODE, buf); + } else { + printf("Unknown command [%s].\n", buf); + ladmin_log("Unknown command [%s]." RETCODE, buf); + } + } + } + + return 0; +} + +//------------------------------------------------------------- +// Function: Parse receiving informations from the login-server +//------------------------------------------------------------- +int parse_fromlogin(int fd) { + struct char_session_data *sd; + + if (session[fd]->eof) { + if (defaultlanguage == 'F') { + printf("Impossible de se connecter au serveur de login [%s:%d] !\n", loginserverip, loginserverport); + ladmin_log("Impossible de se connecter au serveur de login [%s:%d] !" RETCODE, loginserverip, loginserverport); + } else { + printf("Impossible to have a connection with the login-server [%s:%d] !\n", loginserverip, loginserverport); + ladmin_log("Impossible to have a connection with the login-server [%s:%d] !" RETCODE, loginserverip, loginserverport); + } + close(fd); + delete_session(fd); + exit (0); + } + +// printf("parse_fromlogin : %d %d %d\n", fd, RFIFOREST(fd), RFIFOW(fd,0)); + sd = session[fd]->session_data; + + while(RFIFOREST(fd) >= 2) { + switch(RFIFOW(fd,0)) { + case 0x7919: // answer of a connection request + if (RFIFOREST(fd) < 3) + return 0; + if (RFIFOB(fd,2) != 0) { + if (defaultlanguage == 'F') { + printf("Erreur de login:\n"); + printf(" - mot de passe incorrect,\n"); + printf(" - syst鑪e d'administration non activ, ou\n"); + printf(" - IP non autoris馥.\n"); + ladmin_log("Erreur de login: mot de passe incorrect, syst鑪e d'administration non activ, ou IP non autoris馥." RETCODE); + } else { + printf("Error at login:\n"); + printf(" - incorrect password,\n"); + printf(" - administration system not activated, or\n"); + printf(" - unauthorised IP.\n"); + ladmin_log("Error at login: incorrect password, administration system not activated, or unauthorised IP." RETCODE); + } + session[fd]->eof = 1; + //bytes_to_read = 1; // not stop at prompt + } else { + if (defaultlanguage == 'F') { + printf("Connexion 騁ablie.\n"); + ladmin_log("Connexion 騁ablie." RETCODE); + printf("Lecture de la version du serveur de login...\n"); + ladmin_log("Lecture de la version du serveur de login..." RETCODE); + } else { + printf("Established connection.\n"); + ladmin_log("Established connection." RETCODE); + printf("Reading of the version of the login-server...\n"); + ladmin_log("Reading of the version of the login-server..." RETCODE); + } + //bytes_to_read = 1; // unchanged + checkloginversion(); + } + RFIFOSKIP(fd,3); + break; + +#ifdef PASSWORDENC + case 0x01dc: // answer of a coding key request + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + { + char md5str[64] = "", md5bin[32], md5key[RFIFOW(fd,2) - 4 + 1]; + memcpy(md5key, RFIFOP(fd,4), RFIFOW(fd,2) - 4); + md5key[sizeof(md5key)-1] = '0'; + if (passenc == 1) { + strncpy(md5str, RFIFOP(fd,4), RFIFOW(fd,2) - 4); + strcat(md5str, loginserveradminpassword); + } else if (passenc == 2) { + strncpy(md5str, loginserveradminpassword, sizeof(loginserveradminpassword)); + strcat(md5str, RFIFOP(fd,4)); + } + MD5_String2binary(md5str, md5bin); + WFIFOW(login_fd,0) = 0x7918; // Request for administation login (encrypted password) + WFIFOW(login_fd,2) = passenc; // Encrypted type + memcpy(WFIFOP(login_fd,4), md5bin, 16); + WFIFOSET(login_fd,20); + if (defaultlanguage == 'F') { + printf("R馗eption de la clef MD5.\n"); + ladmin_log("R馗eption de la clef MD5." RETCODE); + printf("Envoi du mot de passe crypt...\n"); + ladmin_log("Envoi du mot de passe crypt..." RETCODE); + } else { + printf("Receiving of the MD5 key.\n"); + ladmin_log("Receiving of the MD5 key." RETCODE); + printf("Sending of the encrypted password...\n"); + ladmin_log("Sending of the encrypted password..." RETCODE); + } + } + bytes_to_read = 1; + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; +#endif + + case 0x7531: // Displaying of the version of the login-server + if (RFIFOREST(fd) < 10) + return 0; + printf(" Login-Server [%s:%d]\n", loginserverip, loginserverport); + if (((int)RFIFOB(login_fd,5)) == 0) { + printf(" eAthena version stable-%d.%d", (int)RFIFOB(login_fd,2), (int)RFIFOB(login_fd,3)); + } else { + printf(" eAthena version dev-%d.%d", (int)RFIFOB(login_fd,2), (int)RFIFOB(login_fd,3)); + } + if (((int)RFIFOB(login_fd,4)) == 0) + printf(" revision %d", (int)RFIFOB(login_fd,4)); + if (((int)RFIFOB(login_fd,6)) == 0) + printf("%d.\n", RFIFOW(login_fd,8)); + else + printf("-mod%d.\n", RFIFOW(login_fd,8)); + bytes_to_read = 0; + RFIFOSKIP(fd,10); + break; + + case 0x7921: // Displaying of the list of accounts + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + if (RFIFOW(fd,2) < 5) { + if (defaultlanguage == 'F') { + ladmin_log(" R馗eption d'une liste des comptes vide." RETCODE); + if (list_count == 0) + printf("Aucun compte trouv.\n"); + else if (list_count == 1) + printf("1 compte trouv.\n"); + else + printf("%d comptes trouv駸.\n", list_count); + } else { + ladmin_log(" Receiving of a void accounts list." RETCODE); + if (list_count == 0) + printf("No account found.\n"); + else if (list_count == 1) + printf("1 account found.\n"); + else + printf("%d accounts found.\n", list_count); + } + bytes_to_read = 0; + } else { + int i; + if (defaultlanguage == 'F') + ladmin_log(" R馗eption d'une liste des comptes." RETCODE); + else + ladmin_log(" Receiving of a accounts list." RETCODE); + for(i = 4; i < RFIFOW(fd,2); i += 38) { + int j; + char userid[24]; + char lower_userid[24]; + memcpy(userid, RFIFOP(fd,i + 5), sizeof(userid)); + userid[sizeof(userid)-1] = '\0'; + memset(lower_userid, '\0', sizeof(lower_userid)); + for (j = 0; userid[j]; j++) + lower_userid[j] = tolower(userid[j]); + list_first = RFIFOL(fd,i) + 1; + // here are checks... + if (list_type == 0 || + (list_type == 1 && RFIFOB(fd,i+4) > 0) || + (list_type == 2 && strstr(lower_userid, parameters) != NULL) || + (list_type == 3 && RFIFOL(fd,i+34) != 0) || + (list_type == 4 && RFIFOL(fd,i+34) == 0)) { + printf("%10d ", RFIFOL(fd,i)); + if (RFIFOB(fd,i+4) == 0) + printf(" "); + else + printf("%2d ", (int)RFIFOB(fd,i+4)); + printf("%-24s", userid); + if (defaultlanguage == 'F') { + if (RFIFOB(fd,i+29) == 0) + printf("%-5s ", "Femme"); + else if (RFIFOB(fd,i+29) == 1) + printf("%-5s ", "Male"); + else + printf("%-5s ", "Servr"); + } else { + if (RFIFOB(fd,i+29) == 0) + printf("%-5s ", "Femal"); + else if (RFIFOB(fd,i+29) == 1) + printf("%-5s ", "Male"); + else + printf("%-5s ", "Servr"); + } + printf("%6d ", RFIFOL(fd,i+30)); + switch(RFIFOL(fd,i+34)) { + case 0: + if (defaultlanguage == 'F') + printf("%-27s\n", "Compte Ok"); + else + printf("%-27s\n", "Account OK"); + break; + case 1: + printf("%-27s\n", "Unregistered ID"); + break; + case 2: + printf("%-27s\n", "Incorrect Password"); + break; + case 3: + printf("%-27s\n", "This ID is expired"); + break; + case 4: + printf("%-27s\n", "Rejected from Server"); + break; + case 5: + printf("%-27s\n", "Blocked by the GM Team"); // You have been blocked by the GM Team + break; + case 6: + printf("%-27s\n", "Your EXE file is too old"); // Your Game's EXE file is not the latest version + break; + case 7: + printf("%-27s\n", "Banishement or"); + printf(" Prohibited to login until...\n"); // You are Prohibited to log in until %s + break; + case 8: + printf("%-27s\n", "Server is over populated"); + break; + case 9: + printf("%-27s\n", "No MSG"); + break; + default: // 100 + printf("%-27s\n", "This ID is totally erased"); // This ID has been totally erased + break; + } + list_count++; + } + } + // asking of the following acounts + if (defaultlanguage == 'F') + ladmin_log("Envoi d'un requ黎e au serveur de logins pour obtenir la liste des comptes de %d %d (compl駑ent)." RETCODE, list_first, list_last); + else + ladmin_log("Request to login-server to obtain the list of accounts from %d to %d (complement)." RETCODE, list_first, list_last); + WFIFOW(login_fd,0) = 0x7920; + WFIFOL(login_fd,2) = list_first; + WFIFOL(login_fd,6) = list_last; + WFIFOSET(login_fd,10); + bytes_to_read = 1; + } + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + + case 0x7931: // Answer of login-server about an account creation + if (RFIFOREST(fd) < 30) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec la cr饌tion du compte [%s]. Un compte identique existe d駛.\n", RFIFOP(fd,6)); + ladmin_log("Echec la cr饌tion du compte [%s]. Un compte identique existe d駛." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] creation failed. Same account already exists.\n", RFIFOP(fd,6)); + ladmin_log("Account [%s] creation failed. Same account already exists." RETCODE, RFIFOP(fd,6)); + } + } else { + if (defaultlanguage == 'F') { + printf("Compte [%s] cr鳬 avec succ鑚 [id: %d].\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Compte [%s] cr鳬 avec succ鑚 [id: %d]." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("Account [%s] is successfully created [id: %d].\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Account [%s] is successfully created [id: %d]." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,30); + break; + + case 0x7933: // Answer of login-server about an account deletion + if (RFIFOREST(fd) < 30) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec de la suppression du compte [%s]. Le compte n'existe pas.\n", RFIFOP(fd,6)); + ladmin_log("Echec de la suppression du compte [%s]. Le compte n'existe pas." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] deletion failed. Account doesn't exist.\n", RFIFOP(fd,6)); + ladmin_log("Account [%s] deletion failed. Account doesn't exist." RETCODE, RFIFOP(fd,6)); + } + } else { + if (defaultlanguage == 'F') { + printf("Compte [%s][id: %d] SUPPRIME avec succ鑚.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Compte [%s][id: %d] SUPPRIME avec succ鑚." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("Account [%s][id: %d] is successfully DELETED.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Account [%s][id: %d] is successfully DELETED." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,30); + break; + + case 0x7935: // answer of the change of an account password + if (RFIFOREST(fd) < 30) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec de la modification du mot de passe du compte [%s].\n", RFIFOP(fd,6)); + printf("Le compte [%s] n'existe pas.\n", RFIFOP(fd,6)); + ladmin_log("Echec de la modification du mot de passe du compte. Le compte [%s] n'existe pas." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] password changing failed.\n", RFIFOP(fd,6)); + printf("Account [%s] doesn't exist.\n", RFIFOP(fd,6)); + ladmin_log("Account password changing failed. The compte [%s] doesn't exist." RETCODE, RFIFOP(fd,6)); + } + } else { + if (defaultlanguage == 'F') { + printf("Modification du mot de passe du compte [%s][id: %d] r騏ssie.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Modification du mot de passe du compte [%s][id: %d] r騏ssie." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("Account [%s][id: %d] password successfully changed.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Account [%s][id: %d] password successfully changed." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,30); + break; + + case 0x7937: // answer of the change of an account state + if (RFIFOREST(fd) < 34) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec du changement du statut du compte [%s]. Le compte n'existe pas.\n", RFIFOP(fd,6)); + ladmin_log("Echec du changement du statut du compte [%s]. Le compte n'existe pas." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] state changing failed. Account doesn't exist.\n", RFIFOP(fd,6)); + ladmin_log("Account [%s] state changing failed. Account doesn't exist." RETCODE, RFIFOP(fd,6)); + } + } else { + char tmpstr[256]; + if (defaultlanguage == 'F') { + sprintf(tmpstr, "Statut du compte [%s] chang avec succ鑚 en [", RFIFOP(fd,6)); + } else { + sprintf(tmpstr, "Account [%s] state successfully changed in [", RFIFOP(fd,6)); + } + switch(RFIFOL(fd,30)) { + case 0: + if (defaultlanguage == 'F') + strcat(tmpstr, "0: Compte Ok"); + else + strcat(tmpstr, "0: Account OK"); + break; + case 1: + strcat(tmpstr, "1: Unregistered ID"); + break; + case 2: + strcat(tmpstr, "2: Incorrect Password"); + break; + case 3: + strcat(tmpstr, "3: This ID is expired"); + break; + case 4: + strcat(tmpstr, "4: Rejected from Server"); + break; + case 5: + strcat(tmpstr, "5: You have been blocked by the GM Team"); + break; + case 6: + strcat(tmpstr, "6: [Your Game's EXE file is not the latest version"); + break; + case 7: + strcat(tmpstr, "7: You are Prohibited to log in until..."); + break; + case 8: + strcat(tmpstr, "8: Server is jammed due to over populated"); + break; + case 9: + strcat(tmpstr, "9: No MSG"); + break; + default: // 100 + strcat(tmpstr, "100: This ID is totally erased"); + break; + } + strcat(tmpstr, "]"); + printf("%s\n", tmpstr); + ladmin_log("%s%s", tmpstr, RETCODE); + } + bytes_to_read = 0; + RFIFOSKIP(fd,34); + break; + + case 0x7939: // answer of the number of online players + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + { + // Get length of the received packet + int i; + char name[20]; + if (defaultlanguage == 'F') { + ladmin_log(" R馗eption du nombre de joueurs en ligne." RETCODE); + } else { + ladmin_log(" Receiving of the number of online players." RETCODE); + } + // Read information of the servers + if (RFIFOW(fd,2) < 5) { + if (defaultlanguage == 'F') { + printf(" Aucun serveur n'est connect au login serveur.\n"); + } else { + printf(" No server is connected to the login-server.\n"); + } + } else { + if (defaultlanguage == 'F') { + printf(" Nombre de joueurs en ligne (serveur: nb):\n"); + } else { + printf(" Number of online players (server: number).\n"); + } + // Displaying of result + for(i = 4; i < RFIFOW(fd,2); i += 32) { + memcpy(name, RFIFOP(fd,i+6), sizeof(name)); + name[sizeof(name) - 1] = '\0'; + printf(" %-20s : %5d\n", name, RFIFOW(fd,i+26)); + } + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + + case 0x793b: // answer of the check of a password + if (RFIFOREST(fd) < 30) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Le compte [%s] n'existe pas ou le mot de passe est incorrect.\n", RFIFOP(fd,6)); + ladmin_log("Le compte [%s] n'existe pas ou le mot de passe est incorrect." RETCODE, RFIFOP(fd,6)); + } else { + printf("The account [%s] doesn't exist or the password is incorrect.\n", RFIFOP(fd,6)); + ladmin_log("The account [%s] doesn't exist or the password is incorrect." RETCODE, RFIFOP(fd,6)); + } + } else { + if (defaultlanguage == 'F') { + printf("Le mot de passe donn correspond bien au compte [%s][id: %d].\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Le mot de passe donn correspond bien au compte [%s][id: %d]." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("The proposed password is correct for the account [%s][id: %d].\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("The proposed password is correct for the account [%s][id: %d]." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,30); + break; + + case 0x793d: // answer of the change of an account sex + if (RFIFOREST(fd) < 30) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec de la modification du sexe du compte [%s].\n", RFIFOP(fd,6)); + printf("Le compte [%s] n'existe pas ou le sexe est d駛 celui demand.\n", RFIFOP(fd,6)); + ladmin_log("Echec de la modification du sexe du compte. Le compte [%s] n'existe pas ou le sexe est d駛 celui demand." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] sex changing failed.\n", RFIFOP(fd,6)); + printf("Account [%s] doesn't exist or the sex is already the good sex.\n", RFIFOP(fd,6)); + ladmin_log("Account sex changing failed. The compte [%s] doesn't exist or the sex is already the good sex." RETCODE, RFIFOP(fd,6)); + } + } else { + if (defaultlanguage == 'F') { + printf("Sexe du compte [%s][id: %d] chang avec succ鑚.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Sexe du compte [%s][id: %d] chang avec succ鑚." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("Account [%s][id: %d] sex successfully changed.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Account [%s][id: %d] sex successfully changed." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,30); + break; + + case 0x793f: // answer of the change of an account GM level + if (RFIFOREST(fd) < 30) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec de la modification du niveau de GM du compte [%s].\n", RFIFOP(fd,6)); + printf("Le compte [%s] n'existe pas, le niveau de GM est d駛 celui demand饅n", RFIFOP(fd,6)); + printf("ou il est impossible de modifier le fichier des comptes GM.\n"); + ladmin_log("Echec de la modification du niveau de GM du compte. Le compte [%s] n'existe pas, le niveau de GM est d駛 celui demand ou il est impossible de modifier le fichier des comptes GM." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] GM level changing failed.\n", RFIFOP(fd,6)); + printf("Account [%s] doesn't exist, the GM level is already the good GM level\n", RFIFOP(fd,6)); + printf("or it's impossible to modify the GM accounts file.\n"); + ladmin_log("Account GM level changing failed. The compte [%s] doesn't exist, the GM level is already the good sex or it's impossible to modify the GM accounts file." RETCODE, RFIFOP(fd,6)); + } + } else { + if (defaultlanguage == 'F') { + printf("Niveau de GM du compte [%s][id: %d] chang avec succ鑚.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Niveau de GM du compte [%s][id: %d] chang avec succ鑚." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("Account [%s][id: %d] GM level successfully changed.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Account [%s][id: %d] GM level successfully changed." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,30); + break; + + case 0x7941: // answer of the change of an account email + if (RFIFOREST(fd) < 30) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec de la modification de l'e-mail du compte [%s].\n", RFIFOP(fd,6)); + printf("Le compte [%s] n'existe pas.\n", RFIFOP(fd,6)); + ladmin_log("Echec de la modification de l'e-mail du compte. Le compte [%s] n'existe pas." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] e-mail changing failed.\n", RFIFOP(fd,6)); + printf("Account [%s] doesn't exist.\n", RFIFOP(fd,6)); + ladmin_log("Account e-mail changing failed. The compte [%s] doesn't exist." RETCODE, RFIFOP(fd,6)); + } + } else { + if (defaultlanguage == 'F') { + printf("Modification de l'e-mail du compte [%s][id: %d] r騏ssie.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Modification de l'e-mail du compte [%s][id: %d] r騏ssie." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("Account [%s][id: %d] e-mail successfully changed.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Account [%s][id: %d] e-mail successfully changed." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,30); + break; + + case 0x7943: // answer of the change of an account memo + if (RFIFOREST(fd) < 30) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec du changement du m駑o du compte [%s]. Le compte n'existe pas.\n", RFIFOP(fd,6)); + ladmin_log("Echec du changement du m駑o du compte [%s]. Le compte n'existe pas." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] memo changing failed. Account doesn't exist.\n", RFIFOP(fd,6)); + ladmin_log("Account [%s] memo changing failed. Account doesn't exist." RETCODE, RFIFOP(fd,6)); + } + } else { + if (defaultlanguage == 'F') { + printf("M駑o du compte [%s][id: %d] chang avec succ鑚.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("M駑o du compte [%s][id: %d] chang avec succ鑚." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("Account [%s][id: %d] memo successfully changed.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Account [%s][id: %d] memo successfully changed." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,30); + break; + + case 0x7945: // answer of an account id search + if (RFIFOREST(fd) < 30) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Impossible de trouver l'id du compte [%s]. Le compte n'existe pas.\n", RFIFOP(fd,6)); + ladmin_log("Impossible de trouver l'id du compte [%s]. Le compte n'existe pas." RETCODE, RFIFOP(fd,6)); + } else { + printf("Unable to find the account [%s] id. Account doesn't exist.\n", RFIFOP(fd,6)); + ladmin_log("Unable to find the account [%s] id. Account doesn't exist." RETCODE, RFIFOP(fd,6)); + } + } else { + if (defaultlanguage == 'F') { + printf("Le compte [%s] a pour id: %d.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Le compte [%s] a pour id: %d." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("The account [%s] have the id: %d.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("The account [%s] have the id: %d." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,30); + break; + + case 0x7947: // answer of an account name search + if (RFIFOREST(fd) < 30) + return 0; + if (strcmp(RFIFOP(fd,6), "") == 0) { + if (defaultlanguage == 'F') { + printf("Impossible de trouver le nom du compte [%d]. Le compte n'existe pas.\n", RFIFOL(fd,2)); + ladmin_log("Impossible de trouver le nom du compte [%d]. Le compte n'existe pas." RETCODE, RFIFOL(fd,2)); + } else { + printf("Unable to find the account [%d] name. Account doesn't exist.\n", RFIFOL(fd,2)); + ladmin_log("Unable to find the account [%d] name. Account doesn't exist." RETCODE, RFIFOL(fd,2)); + } + } else { + if (defaultlanguage == 'F') { + printf("Le compte [id: %d] a pour nom: %s.\n", RFIFOL(fd,2), RFIFOP(fd,6)); + ladmin_log("Le compte [id: %d] a pour nom: %s." RETCODE, RFIFOL(fd,2), RFIFOP(fd,6)); + } else { + printf("The account [id: %d] have the name: %s.\n", RFIFOL(fd,2), RFIFOP(fd,6)); + ladmin_log("The account [id: %d] have the name: %s." RETCODE, RFIFOL(fd,2), RFIFOP(fd,6)); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,30); + break; + + case 0x7949: // answer of an account validity limit set + if (RFIFOREST(fd) < 34) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec du changement de la validit du compte [%s]. Le compte n'existe pas.\n", RFIFOP(fd,6)); + ladmin_log("Echec du changement de la validit du compte [%s]. Le compte n'existe pas." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] validity limit changing failed. Account doesn't exist.\n", RFIFOP(fd,6)); + ladmin_log("Account [%s] validity limit changing failed. Account doesn't exist." RETCODE, RFIFOP(fd,6)); + } + } else { + time_t timestamp = RFIFOL(fd,30); + if (timestamp == 0) { + if (defaultlanguage == 'F') { + printf("Limite de validit du compte [%s][id: %d] chang馥 avec succ鑚 en [illimit饐.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Limite de validit du compte [%s][id: %d] chang馥 avec succ鑚 en [illimit饐." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("Validity Limit of the account [%s][id: %d] successfully changed to [unlimited].\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Validity Limit of the account [%s][id: %d] successfully changed to [unlimited]." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } else { + char tmpstr[128]; + strftime(tmpstr, 24, date_format, localtime(×tamp)); + if (defaultlanguage == 'F') { + printf("Limite de validit du compte [%s][id: %d] chang馥 avec succ鑚 pour 黎re jusqu'au %s.\n", RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + ladmin_log("Limite de validit du compte [%s][id: %d] chang馥 avec succ鑚 pour 黎re jusqu'au %s." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + } else { + printf("Validity Limit of the account [%s][id: %d] successfully changed to be until %s.\n", RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + ladmin_log("Validity Limit of the account [%s][id: %d] successfully changed to be until %s." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + } + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,34); + break; + + case 0x794b: // answer of an account ban set + if (RFIFOREST(fd) < 34) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec du changement de la date finale de banissement du compte [%s]. Le compte n'existe pas.\n", RFIFOP(fd,6)); + ladmin_log("Echec du changement de la date finale de banissement du compte [%s]. Le compte n'existe pas." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] final date of banishment changing failed. Account doesn't exist.\n", RFIFOP(fd,6)); + ladmin_log("Account [%s] final date of banishment changing failed. Account doesn't exist." RETCODE, RFIFOP(fd,6)); + } + } else { + time_t timestamp = RFIFOL(fd,30); + if (timestamp == 0) { + if (defaultlanguage == 'F') { + printf("Date finale de banissement du compte [%s][id: %d] chang馥 avec succ鑚 en [d-bannie].\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Date finale de banissement du compte [%s][id: %d] chang馥 avec succ鑚 en [d-bannie]." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("Final date of banishment of the account [%s][id: %d] successfully changed to [unbanished].\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Final date of banishment of the account [%s][id: %d] successfully changed to [unbanished]." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } else { + char tmpstr[128]; + strftime(tmpstr, 24, date_format, localtime(×tamp)); + if (defaultlanguage == 'F') { + printf("Date finale de banissement du compte [%s][id: %d] chang馥 avec succ鑚 pour 黎re jusqu'au %s.\n", RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + ladmin_log("Date finale de banissement du compte [%s][id: %d] chang馥 avec succ鑚 pour 黎re jusqu'au %s." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + } else { + printf("Final date of banishment of the account [%s][id: %d] successfully changed to be until %s.\n", RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + ladmin_log("Final date of banishment of the account [%s][id: %d] successfully changed to be until %s." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + } + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,34); + break; + + case 0x794d: // answer of an account ban date/time changing + if (RFIFOREST(fd) < 34) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec du changement de la date finale de banissement du compte [%s]. Le compte n'existe pas.\n", RFIFOP(fd,6)); + ladmin_log("Echec du changement de la date finale de banissement du compte [%s]. Le compte n'existe pas." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] final date of banishment changing failed. Account doesn't exist.\n", RFIFOP(fd,6)); + ladmin_log("Account [%s] final date of banishment changing failed. Account doesn't exist." RETCODE, RFIFOP(fd,6)); + } + } else { + time_t timestamp = RFIFOL(fd,30); + if (timestamp == 0) { + if (defaultlanguage == 'F') { + printf("Date finale de banissement du compte [%s][id: %d] chang馥 avec succ鑚 en [d-bannie].\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Date finale de banissement du compte [%s][id: %d] chang馥 avec succ鑚 en [d-bannie]." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("Final date of banishment of the account [%s][id: %d] successfully changed to [unbanished].\n", RFIFOP(fd,6), RFIFOL(fd,2)); + ladmin_log("Final date of banishment of the account [%s][id: %d] successfully changed to [unbanished]." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } else { + char tmpstr[128]; + strftime(tmpstr, 24, date_format, localtime(×tamp)); + if (defaultlanguage == 'F') { + printf("Date finale de banissement du compte [%s][id: %d] chang馥 avec succ鑚 pour 黎re jusqu'au %s.\n", RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + ladmin_log("Date finale de banissement du compte [%s][id: %d] chang馥 avec succ鑚 pour 黎re jusqu'au %s." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + } else { + printf("Final date of banishment of the account [%s][id: %d] successfully changed to be until %s.\n", RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + ladmin_log("Final date of banishment of the account [%s][id: %d] successfully changed to be until %s." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + } + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,34); + break; + + case 0x794f: // answer of a broadcast + if (RFIFOREST(fd) < 4) + return 0; + if (RFIFOW(fd,2) == (unsigned short)-1) { + if (defaultlanguage == 'F') { + printf("Echec de l'envoi du message. Aucun server de char en ligne.\n"); + ladmin_log("Echec de l'envoi du message. Aucun server de char en ligne." RETCODE); + } else { + printf("Message sending failed. No online char-server.\n"); + ladmin_log("Message sending failed. No online char-server." RETCODE); + } + } else { + if (defaultlanguage == 'F') { + printf("Message transmis au server de logins avec succ鑚.\n"); + ladmin_log("Message transmis au server de logins avec succ鑚." RETCODE); + } else { + printf("Message successfully sended to login-server.\n"); + ladmin_log("Message successfully sended to login-server." RETCODE); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,4); + break; + + case 0x7951: // answer of an account validity limit changing + if (RFIFOREST(fd) < 34) + return 0; + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Echec du changement de la validit du compte [%s]. Le compte n'existe pas.\n", RFIFOP(fd,6)); + ladmin_log("Echec du changement de la validit du compte [%s]. Le compte n'existe pas." RETCODE, RFIFOP(fd,6)); + } else { + printf("Account [%s] validity limit changing failed. Account doesn't exist.\n", RFIFOP(fd,6)); + ladmin_log("Account [%s] validity limit changing failed. Account doesn't exist." RETCODE, RFIFOP(fd,6)); + } + } else { + time_t timestamp = RFIFOL(fd,30); + if (timestamp == 0) { + if (defaultlanguage == 'F') { + printf("Limite de validit du compte [%s][id: %d] inchang馥.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + printf("Le compte a une validit illimit馥 ou\n"); + printf("la modification est impossible avec les ajustements demand駸.\n"); + ladmin_log("Limite de validit du compte [%s][id: %d] inchang馥. Le compte a une validit illimit馥 ou la modification est impossible avec les ajustements demand駸." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } else { + printf("Validity limit of the account [%s][id: %d] unchanged.\n", RFIFOP(fd,6), RFIFOL(fd,2)); + printf("The account have an unlimited validity limit or\n"); + printf("the changing is impossible with the proposed adjustments.\n"); + ladmin_log("Validity limit of the account [%s][id: %d] unchanged. The account have an unlimited validity limit or the changing is impossible with the proposed adjustments." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2)); + } + } else { + char tmpstr[128]; + strftime(tmpstr, 24, date_format, localtime(×tamp)); + if (defaultlanguage == 'F') { + printf("Limite de validit du compte [%s][id: %d] chang馥 avec succ鑚 pour 黎re jusqu'au %s.\n", RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + ladmin_log("Limite de validit du compte [%s][id: %d] chang馥 avec succ鑚 pour 黎re jusqu'au %s." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + } else { + printf("Validity limit of the account [%s][id: %d] successfully changed to be until %s.\n", RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + ladmin_log("Validity limit of the account [%s][id: %d] successfully changed to be until %s." RETCODE, RFIFOP(fd,6), RFIFOL(fd,2), tmpstr); + } + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,34); + break; + + case 0x7953: // answer of a request about informations of an account (by account name/id) + if (RFIFOREST(fd) < 150 || RFIFOREST(fd) < (150 + RFIFOW(fd,148))) + return 0; + { + char userid[24], error_message[20], lastlogin[24], last_ip[16], email[40], memo[255]; + time_t ban_until_time; // # of seconds 1/1/1970 (timestamp): ban time limit of the account (0 = no ban) + time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited) + memcpy(userid, RFIFOP(fd,7), sizeof(userid)); + userid[sizeof(userid)-1] = '\0'; + memcpy(error_message, RFIFOP(fd,40), sizeof(error_message)); + error_message[sizeof(error_message)-1] = '\0'; + memcpy(lastlogin, RFIFOP(fd,60), sizeof(lastlogin)); + lastlogin[sizeof(lastlogin)-1] = '\0'; + memcpy(last_ip, RFIFOP(fd,84), sizeof(last_ip)); + last_ip[sizeof(last_ip)-1] = '\0'; + memcpy(email, RFIFOP(fd,100), sizeof(email)); + email[sizeof(email)-1] = '\0'; + connect_until_time = (time_t)RFIFOL(fd,140); + ban_until_time = (time_t)RFIFOL(fd,144); + memset(memo, '\0', sizeof(memo)); + strncpy(memo, RFIFOP(fd,150), RFIFOW(fd,148)); + if (RFIFOL(fd,2) == -1) { + if (defaultlanguage == 'F') { + printf("Impossible de trouver le compte [%s]. Le compte n'existe pas.\n", parameters); + ladmin_log("Impossible de trouver le compte [%s]. Le compte n'existe pas." RETCODE, parameters); + } else { + printf("Unabled to find the account [%s]. Account doesn't exist.\n", parameters); + ladmin_log("Unabled to find the account [%s]. Account doesn't exist." RETCODE, parameters); + } + } else if (strlen(userid) == 0) { + if (defaultlanguage == 'F') { + printf("Impossible de trouver le compte [id: %s]. Le compte n'existe pas.\n", parameters); + ladmin_log("Impossible de trouver le compte [id: %s]. Le compte n'existe pas." RETCODE, parameters); + } else { + printf("Unabled to find the account [id: %s]. Account doesn't exist.\n", parameters); + ladmin_log("Unabled to find the account [id: %s]. Account doesn't exist." RETCODE, parameters); + } + } else { + if (defaultlanguage == 'F') { + ladmin_log("R馗eption d'information concernant un compte." RETCODE); + printf("Le compte a les caract駻istiques suivantes:\n"); + } else { + ladmin_log("Receiving information about an account." RETCODE); + printf("The account is set with:\n"); + } + if (RFIFOB(fd,6) == 0) { + printf(" Id: %d (non-GM)\n", RFIFOL(fd,2)); + } else { + if (defaultlanguage == 'F') { + printf(" Id: %d (GM niveau %d)\n", RFIFOL(fd,2), (int)RFIFOB(fd,6)); + } else { + printf(" Id: %d (GM level %d)\n", RFIFOL(fd,2), (int)RFIFOB(fd,6)); + } + } + if (defaultlanguage == 'F') { + printf(" Nom: '%s'\n", userid); + if (RFIFOB(fd,31) == 0) + printf(" Sexe: Femme\n"); + else if (RFIFOB(fd,31) == 1) + printf(" Sexe: Male\n"); + else + printf(" Sexe: Serveur\n"); + } else { + printf(" Name: '%s'\n", userid); + if (RFIFOB(fd,31) == 0) + printf(" Sex: Female\n"); + else if (RFIFOB(fd,31) == 1) + printf(" Sex: Male\n"); + else + printf(" Sex: Server\n"); + } + printf(" E-mail: %s\n", email); + switch(RFIFOL(fd,36)) { + case 0: + if (defaultlanguage == 'F') + printf(" Statut: 0 [Compte Ok]\n"); + else + printf(" Statut: 0 [Account OK]\n"); + break; + case 1: + printf(" Statut: 1 [Unregistered ID]\n"); + break; + case 2: + printf(" Statut: 2 [Incorrect Password]\n"); + break; + case 3: + printf(" Statut: 3 [This ID is expired]\n"); + break; + case 4: + printf(" Statut: 4 [Rejected from Server]\n"); + break; + case 5: + printf(" Statut: 5 [You have been blocked by the GM Team]\n"); + break; + case 6: + printf(" Statut: 6 [Your Game's EXE file is not the latest version]\n"); + break; + case 7: + printf(" Statut: 7 [You are Prohibited to log in until %s]\n", error_message); + break; + case 8: + printf(" Statut: 8 [Server is jammed due to over populated]\n"); + break; + case 9: + printf(" Statut: 9 [No MSG]\n"); + break; + default: // 100 + printf(" Statut: %d [This ID is totally erased]\n", RFIFOL(fd,36)); + break; + } + if (defaultlanguage == 'F') { + if (ban_until_time == 0) { + printf(" Banissement: non banni.\n"); + } else { + char tmpstr[128]; + strftime(tmpstr, 24, date_format, localtime(&ban_until_time)); + printf(" Banissement: jusqu'au %s.\n", tmpstr); + } + if (RFIFOL(fd,32) > 1) + printf(" Compteur: %d connexions.\n", RFIFOL(fd,32)); + else + printf(" Compteur: %d connexion.\n", RFIFOL(fd,32)); + printf(" Derni鑽e connexion le: %s (ip: %s)\n", lastlogin, last_ip); + if (connect_until_time == 0) { + printf(" Limite de validit: illimit.\n"); + } else { + char tmpstr[128]; + strftime(tmpstr, 24, date_format, localtime(&connect_until_time)); + printf(" Limite de validit: jusqu'au %s.\n", tmpstr); + } + } else { + if (ban_until_time == 0) { + printf(" Banishment: not banished.\n"); + } else { + char tmpstr[128]; + strftime(tmpstr, 24, date_format, localtime(&ban_until_time)); + printf(" Banishment: until %s.\n", tmpstr); + } + if (RFIFOL(fd,32) > 1) + printf(" Count: %d connections.\n", RFIFOL(fd,32)); + else + printf(" Count: %d connection.\n", RFIFOL(fd,32)); + printf(" Last connection at: %s (ip: %s)\n", lastlogin, last_ip); + if (connect_until_time == 0) { + printf(" Validity limit: unlimited.\n"); + } else { + char tmpstr[128]; + strftime(tmpstr, 24, date_format, localtime(&connect_until_time)); + printf(" Validity limit: until %s.\n", tmpstr); + } + } + printf(" Memo: '%s'\n", memo); + } + } + bytes_to_read = 0; + RFIFOSKIP(fd,150 + RFIFOW(fd,148)); + break; + + default: + printf("Remote administration has been disconnected (unknown packet).\n"); + ladmin_log("'End of connection, unknown packet." RETCODE); + session[fd]->eof = 1; + return 0; + } + } + + // if we don't wait new packets, do the prompt + prompt(); + + return 0; +} + +//------------------------------------ +// Function to connect to login-server +//------------------------------------ +int Connect_login_server() { + if (defaultlanguage == 'F') { + printf("Essai de connection au server de logins...\n"); + ladmin_log("Essai de connection au server de logins..." RETCODE); + } else { + printf("Attempt to connect to login-server...\n"); + ladmin_log("Attempt to connect to login-server..." RETCODE); + } + + login_fd = make_connection(login_ip, loginserverport); + +#ifdef PASSWORDENC + if (passenc == 0) { +#endif + WFIFOW(login_fd,0) = 0x7918; // Request for administation login + WFIFOW(login_fd,2) = 0; // no encrypted + memcpy(WFIFOP(login_fd,4), loginserveradminpassword, 24); + WFIFOSET(login_fd,28); + bytes_to_read = 1; + if (defaultlanguage == 'F') { + printf("Envoi du mot de passe...\n"); + ladmin_log("Envoi du mot de passe..." RETCODE); + } else { + printf("Sending of the password...\n"); + ladmin_log("Sending of the password..." RETCODE); + } +#ifdef PASSWORDENC + } else { + WFIFOW(login_fd,0) = 0x791a; // Sending request about the coding key + WFIFOSET(login_fd,2); + bytes_to_read = 1; + if (defaultlanguage == 'F') { + printf("Demande de la clef MD5...\n"); + ladmin_log("Demande de la clef MD5..." RETCODE); + } else { + printf("Request about the MD5 key...\n"); + ladmin_log("Request about the MD5 key..." RETCODE); + } + } +#endif + + return 0; +} + +//------------------------------------------------- +// Return numerical value of a switch configuration +// on/off, english, fran軋is, deutsch, espaol +//------------------------------------------------- +int config_switch(const char *str) { + if (strcmpi(str, "on") == 0 || strcmpi(str, "yes") == 0 || strcmpi(str, "oui") == 0 || strcmpi(str, "ja") == 0 || strcmpi(str, "si") == 0) + return 1; + if (strcmpi(str, "off") == 0 || strcmpi(str, "no") == 0 || strcmpi(str, "non") == 0 || strcmpi(str, "nein") == 0) + return 0; + + return atoi(str); +} + +//----------------------------------- +// Reading general configuration file +//----------------------------------- +int ladmin_config_read(const char *cfgName) { + char line[1024], w1[1024], w2[1024]; + FILE *fp; + + fp = fopen(cfgName, "r"); + if (fp == NULL) { + if (defaultlanguage == 'F') { + printf("\033[0mFichier de configuration (%s) non trouv.\n", cfgName); + } else { + printf("\033[0mConfiguration file (%s) not found.\n", cfgName); + } + return 1; + } + + if (defaultlanguage == 'F') { + printf("\033[0m---D饕ut de lecture du fichier de configuration Ladmin (%s)\n", cfgName); + } else { + printf("\033[0m---Start reading of Ladmin configuration file (%s)\n", cfgName); + } + while(fgets(line, sizeof(line)-1, fp)) { + if (line[0] == '/' && line[1] == '/') + continue; + + line[sizeof(line)-1] = '\0'; + if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) == 2) { + remove_control_chars(w1); + remove_control_chars(w2); + + if(strcmpi(w1,"login_ip")==0){ + struct hostent *h = gethostbyname (w2); + if (h != NULL) { + if (defaultlanguage == 'F') { + printf("Adresse du serveur de logins: %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } else { + printf("Login server IP address: %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } + sprintf(loginserverip, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } else + memcpy(loginserverip, w2, 16); + } else if (strcmpi(w1, "login_port") == 0) { + loginserverport = atoi(w2); + } else if (strcmpi(w1, "admin_pass") == 0) { + strncpy(loginserveradminpassword, w2, sizeof(loginserveradminpassword)); + loginserveradminpassword[sizeof(loginserveradminpassword)-1] = '\0'; +#ifdef PASSWORDENC + } else if (strcmpi(w1, "passenc") == 0) { + passenc = atoi(w2); + if (passenc < 0 || passenc > 2) + passenc = 0; +#endif + } else if (strcmpi(w1, "defaultlanguage") == 0) { + if (w2[0] == 'F' || w2[0] == 'E') + defaultlanguage = w2[0]; + } else if (strcmpi(w1, "ladmin_log_filename") == 0) { + strncpy(ladmin_log_filename, w2, sizeof(ladmin_log_filename)); + ladmin_log_filename[sizeof(ladmin_log_filename)-1] = '\0'; + } else if (strcmpi(w1, "date_format") == 0) { // note: never have more than 19 char for the date! + switch (atoi(w2)) { + case 0: + strcpy(date_format, "%d-%m-%Y %H:%M:%S"); // 31-12-2004 23:59:59 + break; + case 1: + strcpy(date_format, "%m-%d-%Y %H:%M:%S"); // 12-31-2004 23:59:59 + break; + case 2: + strcpy(date_format, "%Y-%d-%m %H:%M:%S"); // 2004-31-12 23:59:59 + break; + case 3: + strcpy(date_format, "%Y-%m-%d %H:%M:%S"); // 2004-12-31 23:59:59 + break; + } + } else if (strcmpi(w1, "import") == 0) { + ladmin_config_read(w2); + } + } + } + fclose(fp); + + login_ip = inet_addr(loginserverip); + + if (defaultlanguage == 'F') { + printf("---Lecture du fichier de configuration Ladmin termin馥.\n"); + } else { + printf("---End reading of Ladmin configuration file.\n"); + } + + return 0; +} + +//-------------------------------------- +// Function called at exit of the server +//-------------------------------------- +void do_final(void) { + + if (already_exit_function == 0) { + delete_session(login_fd); + + if (defaultlanguage == 'F') { + printf("\033[0m----Fin de Ladmin (fin normale avec fermeture de tous les fichiers).\n"); + ladmin_log("----Fin de Ladmin (fin normale avec fermeture de tous les fichiers)." RETCODE); + } else { + printf("\033[0m----End of Ladmin (normal end with closing of all files).\n"); + ladmin_log("----End of Ladmin (normal end with closing of all files)." RETCODE); + } + + already_exit_function = 1; + } +} + +//------------------------ +// Main function of ladmin +//------------------------ +int do_init(int argc, char **argv) { + // read ladmin configuration + ladmin_config_read((argc > 1) ? argv[1] : LADMIN_CONF_NAME); + + ladmin_log(""); + if (defaultlanguage == 'F') { + ladmin_log("Fichier de configuration lu." RETCODE); + } else { + ladmin_log("Configuration file readed." RETCODE); + } + + srand(time(NULL)); + + set_termfunc(do_final); + set_defaultparse(parse_fromlogin); + + if (defaultlanguage == 'F') { + printf("Outil d'administration distance de eAthena.\n"); + printf("(pour eAthena version %d.%d.%d.)\n", ATHENA_MAJOR_VERSION, ATHENA_MINOR_VERSION, ATHENA_REVISION); + } else { + printf("EAthena login-server administration tool.\n"); + printf("(for eAthena version %d.%d.%d.)\n", ATHENA_MAJOR_VERSION, ATHENA_MINOR_VERSION, ATHENA_REVISION); + } + + if (defaultlanguage == 'F') { + ladmin_log("Ladmin est pr黎." RETCODE); + printf("Ladmin est \033[1;32mpr黎\033[0m.\n\n"); + } else { + ladmin_log("Ladmin is ready." RETCODE); + printf("Ladmin is \033[1;32mready\033[0m.\n\n"); + } + + Connect_login_server(); + + atexit(do_final); + + return 0; +} diff --git a/misc/src/ladmin/ladmin.h b/misc/src/ladmin/ladmin.h new file mode 100644 index 0000000..f76bfc2 --- /dev/null +++ b/misc/src/ladmin/ladmin.h @@ -0,0 +1,11 @@ +// $Id: ladmin.h,v 1.1.1.1 2004/09/10 17:26:52 MagicalTux Exp $ +#ifndef _LADMIN_H_ +#define _LADMIN_H_ + +#define LADMIN_CONF_NAME "conf/ladmin_athena.conf" +#define PASSWORDENC 3 // A definition is given when making an encryption password correspond. + // It is 1 at the time of passwordencrypt. + // It is made into 2 at the time of passwordencrypt2. + // When it is made 3, it corresponds to both. + +#endif diff --git a/misc/src/ladmin/md5calc.c b/misc/src/ladmin/md5calc.c new file mode 100644 index 0000000..7b9a9a2 --- /dev/null +++ b/misc/src/ladmin/md5calc.c @@ -0,0 +1,237 @@ +// $Id: md5calc.c,v 1.1.1.1 2004/09/10 17:26:53 MagicalTux Exp $ +/*********************************************************** + * md5 calculation algorithm + * + * The source code referred to the following URL. + * http://www.geocities.co.jp/SiliconValley-Oakland/8878/lab17/lab17.html + * + ***********************************************************/ + +#include "md5calc.h" +#include +#include + +#ifndef UINT_MAX +#define UINT_MAX 4294967295U +#endif + +// Global variable +static unsigned int *pX; + +// Stirng Table +static const unsigned int T[] = { + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, //0 + 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, //4 + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, //8 + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, //12 + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, //16 + 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, //20 + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, //24 + 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, //28 + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, //32 + 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, //36 + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, //40 + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, //44 + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, //48 + 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, //52 + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, //56 + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 //60 +}; + +// ROTATE_LEFT The left is made to rotate x [ n-bit ]. This is diverted as it is from RFC. +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +// The function used for other calculation +static unsigned int F(unsigned int X, unsigned int Y, unsigned int Z) +{ + return (X & Y) | (~X & Z); +} +static unsigned int G(unsigned int X, unsigned int Y, unsigned int Z) +{ + return (X & Z) | (Y & ~Z); +} +static unsigned int H(unsigned int X, unsigned int Y, unsigned int Z) +{ + return X ^ Y ^ Z; +} +static unsigned int I(unsigned int X, unsigned int Y, unsigned int Z) +{ + return Y ^ (X | ~Z); +} + +static unsigned int Round(unsigned int a, unsigned int b, unsigned int FGHI, + unsigned int k, unsigned int s, unsigned int i) +{ + return b + ROTATE_LEFT(a + FGHI + pX[k] + T[i], s); +} + +static void Round1(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, F(b,c,d), k, s, i); +} +static void Round2(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, G(b,c,d), k, s, i); +} +static void Round3(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, H(b,c,d), k, s, i); +} +static void Round4(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, I(b,c,d), k, s, i); +} + +static void MD5_Round_Calculate(const unsigned char *block, + unsigned int *A2, unsigned int *B2, unsigned int *C2, unsigned int *D2) +{ + //create X It is since it is required. + unsigned int X[16]; //512bit 64byte + int j,k; + + //Save A as AA, B as BB, C as CC, and and D as DD (saving of A, B, C, and D) + unsigned int A=*A2, B=*B2, C=*C2, D=*D2; + unsigned int AA = A,BB = B,CC = C,DD = D; + + //It is a large region variable reluctantly because of calculation of a round. . . for Round1...4 + pX = X; + + //Copy block(padding_message) i into X + for (j=0,k=0; j<64; j+=4,k++) + X[k] = ( (unsigned int )block[j] ) // 8byte*4 -> 32byte conversion + | ( ((unsigned int )block[j+1]) << 8 ) // A function called Decode as used in the field of RFC + | ( ((unsigned int )block[j+2]) << 16 ) + | ( ((unsigned int )block[j+3]) << 24 ); + + + //Round 1 + Round1(&A,B,C,D, 0, 7, 0); Round1(&D,A,B,C, 1, 12, 1); Round1(&C,D,A,B, 2, 17, 2); Round1(&B,C,D,A, 3, 22, 3); + Round1(&A,B,C,D, 4, 7, 4); Round1(&D,A,B,C, 5, 12, 5); Round1(&C,D,A,B, 6, 17, 6); Round1(&B,C,D,A, 7, 22, 7); + Round1(&A,B,C,D, 8, 7, 8); Round1(&D,A,B,C, 9, 12, 9); Round1(&C,D,A,B, 10, 17, 10); Round1(&B,C,D,A, 11, 22, 11); + Round1(&A,B,C,D, 12, 7, 12); Round1(&D,A,B,C, 13, 12, 13); Round1(&C,D,A,B, 14, 17, 14); Round1(&B,C,D,A, 15, 22, 15); + + //Round 2 + Round2(&A,B,C,D, 1, 5, 16); Round2(&D,A,B,C, 6, 9, 17); Round2(&C,D,A,B, 11, 14, 18); Round2(&B,C,D,A, 0, 20, 19); + Round2(&A,B,C,D, 5, 5, 20); Round2(&D,A,B,C, 10, 9, 21); Round2(&C,D,A,B, 15, 14, 22); Round2(&B,C,D,A, 4, 20, 23); + Round2(&A,B,C,D, 9, 5, 24); Round2(&D,A,B,C, 14, 9, 25); Round2(&C,D,A,B, 3, 14, 26); Round2(&B,C,D,A, 8, 20, 27); + Round2(&A,B,C,D, 13, 5, 28); Round2(&D,A,B,C, 2, 9, 29); Round2(&C,D,A,B, 7, 14, 30); Round2(&B,C,D,A, 12, 20, 31); + + //Round 3 + Round3(&A,B,C,D, 5, 4, 32); Round3(&D,A,B,C, 8, 11, 33); Round3(&C,D,A,B, 11, 16, 34); Round3(&B,C,D,A, 14, 23, 35); + Round3(&A,B,C,D, 1, 4, 36); Round3(&D,A,B,C, 4, 11, 37); Round3(&C,D,A,B, 7, 16, 38); Round3(&B,C,D,A, 10, 23, 39); + Round3(&A,B,C,D, 13, 4, 40); Round3(&D,A,B,C, 0, 11, 41); Round3(&C,D,A,B, 3, 16, 42); Round3(&B,C,D,A, 6, 23, 43); + Round3(&A,B,C,D, 9, 4, 44); Round3(&D,A,B,C, 12, 11, 45); Round3(&C,D,A,B, 15, 16, 46); Round3(&B,C,D,A, 2, 23, 47); + + //Round 4 + Round4(&A,B,C,D, 0, 6, 48); Round4(&D,A,B,C, 7, 10, 49); Round4(&C,D,A,B, 14, 15, 50); Round4(&B,C,D,A, 5, 21, 51); + Round4(&A,B,C,D, 12, 6, 52); Round4(&D,A,B,C, 3, 10, 53); Round4(&C,D,A,B, 10, 15, 54); Round4(&B,C,D,A, 1, 21, 55); + Round4(&A,B,C,D, 8, 6, 56); Round4(&D,A,B,C, 15, 10, 57); Round4(&C,D,A,B, 6, 15, 58); Round4(&B,C,D,A, 13, 21, 59); + Round4(&A,B,C,D, 4, 6, 60); Round4(&D,A,B,C, 11, 10, 61); Round4(&C,D,A,B, 2, 15, 62); Round4(&B,C,D,A, 9, 21, 63); + + // Then perform the following additions. (let's add) + *A2 = A + AA; + *B2 = B + BB; + *C2 = C + CC; + *D2 = D + DD; + + //The clearance of confidential information + memset(pX, 0, sizeof(X)); +} + +//------------------------------------------------------------------- +// The function for the exteriors + +/** output is the coded binary in the character sequence which wants to code string. */ +void MD5_String2binary(const char * string, char * output) +{ +//var + /*8bit*/ + unsigned char padding_message[64]; //Extended message 512bit 64byte + unsigned char *pstring; //The position of string in the present scanning notes is held. + +// unsigned char digest[16]; + /*32bit*/ + unsigned int string_byte_len, //The byte chief of string is held. + string_bit_len, //The bit length of string is held. + copy_len, //The number of bytes which is used by 1-3 and which remained + msg_digest[4]; //Message digest 128bit 4byte + unsigned int *A = &msg_digest[0], //The message digest in accordance with RFC (reference) + *B = &msg_digest[1], + *C = &msg_digest[2], + *D = &msg_digest[3]; + int i; + +//prog + //Step 3.Initialize MD Buffer (although it is the initialization; step 3 of A, B, C, and D -- unavoidable -- a head) + *A = 0x67452301; + *B = 0xefcdab89; + *C = 0x98badcfe; + *D = 0x10325476; + + //Step 1.Append Padding Bits (extension of a mark bit) + //1-1 + string_byte_len = strlen(string); //The byte chief of a character sequence is acquired. + pstring = (unsigned char *)string; //The position of the present character sequence is set. + + //1-2 Repeat calculation until length becomes less than 64 bytes. + for (i=string_byte_len; 64<=i; i-=64,pstring+=64) + MD5_Round_Calculate(pstring, A,B,C,D); + + //1-3 + copy_len = string_byte_len % 64; //The number of bytes which remained is computed. + strncpy((char *)padding_message, (char *)pstring, copy_len); //A message is copied to an extended bit sequence. + memset(padding_message+copy_len, 0, 64 - copy_len); //It buries by 0 until it becomes extended bit length. + padding_message[copy_len] |= 0x80; //The next of a message is 1. + + //1-4 + //If 56 bytes or more (less than 64 bytes) of remainder becomes, it will calculate by extending to 64 bytes. + if (56 <= copy_len) { + MD5_Round_Calculate(padding_message, A,B,C,D); + memset(padding_message, 0, 56); //56 bytes is newly fill uped with 0. + } + + + //Step 2.Append Length (the information on length is added) + string_bit_len = string_byte_len * 8; //From the byte chief to bit length (32 bytes of low rank) + memcpy(&padding_message[56], &string_bit_len, 4); //32 bytes of low rank is set. + + //When bit length cannot be expressed in 32 bytes of low rank, it is a beam raising to a higher rank. + if (UINT_MAX / 8 < string_byte_len) { + unsigned int high = (string_byte_len - UINT_MAX / 8) * 8; + memcpy(&padding_message[60], &high, 4); + } else + memset(&padding_message[60], 0, 4); //In this case, it is good for a higher rank at 0. + + //Step 4.Process Message in 16-Word Blocks (calculation of MD5) + MD5_Round_Calculate(padding_message, A,B,C,D); + + + //Step 5.Output (output) + memcpy(output,msg_digest,16); +// memcpy (digest, msg_digest, and 16); //8 byte*4 < - 32byte conversion A function called Encode as used in the field of RFC +/* sprintf(output, + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", + digest[ 0], digest[ 1], digest[ 2], digest[ 3], + digest[ 4], digest[ 5], digest[ 6], digest[ 7], + digest[ 8], digest[ 9], digest[10], digest[11], + digest[12], digest[13], digest[14], digest[15]);*/ +} + +/** output is the coded character sequence in the character sequence which wants to code string. */ +void MD5_String(const char * string, char * output) +{ + unsigned char digest[16]; + + MD5_String2binary(string,digest); + sprintf(output, + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", + digest[ 0], digest[ 1], digest[ 2], digest[ 3], + digest[ 4], digest[ 5], digest[ 6], digest[ 7], + digest[ 8], digest[ 9], digest[10], digest[11], + digest[12], digest[13], digest[14], digest[15]); +} + diff --git a/misc/src/ladmin/md5calc.h b/misc/src/ladmin/md5calc.h new file mode 100644 index 0000000..b4dd614 --- /dev/null +++ b/misc/src/ladmin/md5calc.h @@ -0,0 +1,8 @@ +// $Id: md5calc.h,v 1.1.1.1 2004/09/10 17:26:53 MagicalTux Exp $ +#ifndef _MD5CALC_H_ +#define _MD5CALC_H_ + +void MD5_String(const char * string, char * output); +void MD5_String2binary(const char * string, char * output); + +#endif diff --git a/misc/src/login/GNUmakefile b/misc/src/login/GNUmakefile new file mode 100644 index 0000000..df6cb21 --- /dev/null +++ b/misc/src/login/GNUmakefile @@ -0,0 +1,13 @@ +all: login-server +txt: login-server + +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o ../common/lock.o ../common/malloc.o +COMMON_H = ../common/core.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/version.h ../common/db.h ../common/lock.h ../common/malloc.h + +login-server: login.o md5calc.o $(COMMON_OBJ) + $(CC) -o ../../$@ login.o md5calc.o $(COMMON_OBJ) +login.o: login.c login.h md5calc.h $(COMMON_H) +md5calc.o: md5calc.c md5calc.h + +clean: + rm -f *.o ../../login-server diff --git a/misc/src/login/Makefile b/misc/src/login/Makefile new file mode 100644 index 0000000..df6cb21 --- /dev/null +++ b/misc/src/login/Makefile @@ -0,0 +1,13 @@ +all: login-server +txt: login-server + +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o ../common/lock.o ../common/malloc.o +COMMON_H = ../common/core.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/version.h ../common/db.h ../common/lock.h ../common/malloc.h + +login-server: login.o md5calc.o $(COMMON_OBJ) + $(CC) -o ../../$@ login.o md5calc.o $(COMMON_OBJ) +login.o: login.c login.h md5calc.h $(COMMON_H) +md5calc.o: md5calc.c md5calc.h + +clean: + rm -f *.o ../../login-server diff --git a/misc/src/login/login.c b/misc/src/login/login.c new file mode 100644 index 0000000..8d9818d --- /dev/null +++ b/misc/src/login/login.c @@ -0,0 +1,3698 @@ +// $Id: login.c,v 1.1.1.1 2004/09/10 17:26:53 MagicalTux Exp $ +// new version of the login-server by [Yor] + +#include +#include +#include +#include +#include +#include +#include +#include +#include // for stat/lstat/fstat +#include +#include +#include +#include +#include +#include + +#include "core.h" +#include "socket.h" +#include "timer.h" +#include "login.h" +#include "mmo.h" +#include "version.h" +#include "db.h" +#include "lock.h" + +#ifdef PASSWORDENC +#include "md5calc.h" +#endif + +#ifdef MEMWATCH +#include "memwatch.h" +#endif + +int account_id_count = START_ACCOUNT_NUM; +int server_num; +int new_account_flag = 0; +int login_port = 6900; +char lan_char_ip[16]; +int subneti[4]; +int subnetmaski[4]; + +char account_filename[1024] = "save/account.txt"; +char GM_account_filename[1024] = "conf/GM_account.txt"; +char login_log_filename[1024] = "log/login.log"; +char login_log_unknown_packets_filename[1024] = "log/login_unknown_packets.log"; +char date_format[32] = "%Y-%m-%d %H:%M:%S"; +int save_unknown_packets = 0; +long creation_time_GM_account_file; +int gm_account_filename_check_timer = 15; // Timer to check if GM_account file has been changed and reload GM account automaticaly (in seconds; default: 15) + +int display_parse_login = 0; // 0: no, 1: yes +int display_parse_admin = 0; // 0: no, 1: yes +int display_parse_fromchar = 0; // 0: no, 1: yes (without packet 0x2714), 2: all packets + +struct mmo_char_server server[MAX_SERVERS]; +int server_fd[MAX_SERVERS]; +int server_freezeflag[MAX_SERVERS]; // Char-server anti-freeze system. Counter. 5 ok, 4...0 freezed +int anti_freeze_enable = 0; +int ANTI_FREEZE_INTERVAL = 15; + +int login_fd; + +enum { + ACO_DENY_ALLOW = 0, + ACO_ALLOW_DENY, + ACO_MUTUAL_FAILTURE, + ACO_STRSIZE = 128, +}; + +int access_order = ACO_DENY_ALLOW; +int access_allownum = 0; +int access_denynum = 0; +char *access_allow = NULL; +char *access_deny = NULL; + +int access_ladmin_allownum = 0; +char *access_ladmin_allow = NULL; + +int min_level_to_connect = 0; // minimum level of player/GM (0: player, 1-99: gm) to connect on the server +int add_to_unlimited_account = 0; // Give possibility or not to adjust (ladmin command: timeadd) the time of an unlimited account. +int start_limited_time = -1; // Starting additional sec from now for the limited time at creation of accounts (-1: unlimited time, 0 or more: additional sec from now) +int check_ip_flag = 1; // It's to check IP of a player between login-server and char-server (part of anti-hacking system) + +struct login_session_data { + int md5keylen; + char md5key[20]; +}; + +#define AUTH_FIFO_SIZE 256 +struct { + int account_id, login_id1, login_id2; + int ip, sex, delflag; +} auth_fifo[AUTH_FIFO_SIZE]; +int auth_fifo_pos = 0; + +struct auth_dat { + int account_id, sex; + char userid[24], pass[24], lastlogin[24]; + int logincount; + int state; // packet 0x006a value + 1 (0: compte OK) + char email[40]; // e-mail (by default: a@a.com) + char error_message[20]; // Message of error code #6 = Your are Prohibited to log in until %s (packet 0x006a) + time_t ban_until_time; // # of seconds 1/1/1970 (timestamp): ban time limit of the account (0 = no ban) + time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited) + char last_ip[16]; // save of last IP of connection + char memo[255]; // a memo field + int account_reg2_num; + struct global_reg account_reg2[ACCOUNT_REG2_NUM]; +} *auth_dat; + +int auth_num = 0, auth_max = 0; + +// define the number of times that some players must authentify them before to save account file. +// it's just about normal authentification. If an account is created or modified, save is immediatly done. +// An authentification just change last connected IP and date. It already save in log file. +// set minimum auth change before save: +#define AUTH_BEFORE_SAVE_FILE 10 +// set divider of auth_num to found number of change before save +#define AUTH_SAVE_FILE_DIVIDER 50 +int auth_before_save_file = 0; // Counter. First save when 1st char-server do connection. + +int admin_state = 0; +char admin_pass[24] = ""; +char gm_pass[64] = ""; +int level_new_gm = 60; + +static struct dbt *gm_account_db; + +//------------------------------ +// Writing function of logs file +//------------------------------ +int login_log(char *fmt, ...) { + FILE *logfp; + va_list ap; + struct timeval tv; + char tmpstr[2048]; + + va_start(ap, fmt); + + logfp = fopen(login_log_filename, "a"); + if (logfp) { + if (fmt[0] == '\0') // jump a line if no message + fprintf(logfp, RETCODE); + else { + gettimeofday(&tv, NULL); + strftime(tmpstr, 24, date_format, localtime(&(tv.tv_sec))); + sprintf(tmpstr + strlen(tmpstr), ".%03d: %s", (int)tv.tv_usec / 1000, fmt); + vfprintf(logfp, tmpstr, ap); + } + fclose(logfp); + } + + va_end(ap); + return 0; +} + +//---------------------------------------------------------------------- +// Determine if an account (id) is a GM account +// and returns its level (or 0 if it isn't a GM account or if not found) +//---------------------------------------------------------------------- +int isGM(int account_id) { + struct gm_account *p; + + p = numdb_search(gm_account_db, account_id); + if (p == NULL) + return 0; + return p->level; +} + +//------------------------------------------------------- +// Reading function of GM accounts file (and their level) +//------------------------------------------------------- +int read_gm_account() { + char line[512]; + struct gm_account *p; + FILE *fp; + int c = 0; + int GM_level; + struct stat file_stat; + + free(gm_account_db); + gm_account_db = numdb_init(); + + // get last modify time/date + if (stat(GM_account_filename, &file_stat)) + creation_time_GM_account_file = 0; // error + else + creation_time_GM_account_file = file_stat.st_mtime; + + if ((fp = fopen(GM_account_filename, "r")) == NULL) { + printf("read_gm_account: GM accounts file [%s] not found.\n", GM_account_filename); + printf(" Actually, there is no GM accounts on the server.\n"); + login_log("read_gm_account: GM accounts file [%s] not found." RETCODE, GM_account_filename); + login_log(" Actually, there is no GM accounts on the server." RETCODE); + return 1; + } + // limited to 4000, because we send information to char-servers (more than 4000 GM accounts???) + // int (id) + int (level) = 8 bytes * 4000 = 32k (limit of packets in windows) + while(fgets(line, sizeof(line)-1, fp) && c < 4000) { + if ((line[0] == '/' && line[1] == '/') || line[0] == '\0' || line[0] == '\n' || line[0] == '\r') + continue; + p = calloc(sizeof(struct gm_account), 1); + if (p == NULL) { + printf("read_gm_account: memory allocation failure (malloc)!\n"); + exit(0); + } + if (sscanf(line, "%d %d", &p->account_id, &p->level) != 2 && sscanf(line, "%d: %d", &p->account_id, &p->level) != 2) + printf("read_gm_account: file [%s], invalid 'id_acount level' format.\n", GM_account_filename); + else if (p->level <= 0) + printf("read_gm_account: file [%s] %dth account (invalid level [0 or negative]: %d).\n", GM_account_filename, c+1, p->level); + else { + if (p->level > 99) { + printf("read_gm_account: file [%s] %dth account (invalid level, but corrected: %d->99).\n", GM_account_filename, c+1, p->level); + p->level = 99; + } + if ((GM_level = isGM(p->account_id)) > 0) { // if it's not a new account + if (GM_level == p->level) + printf("read_gm_account: GM account %d defined twice (same level: %d).\n", p->account_id, p->level); + else + printf("read_gm_account: GM account %d defined twice (levels: %d and %d).\n", p->account_id, GM_level, p->level); + } + if (GM_level != p->level) { // if new account or new level + numdb_insert(gm_account_db, p->account_id, p); + //printf("GM account:%d, level: %d->%d\n", p->account_id, GM_level, p->level); + if (GM_level == 0) { // if new account + c++; + if (c >= 4000) { + printf("***WARNING: 4000 GM accounts found. Next GM accounts are not readed.\n"); + login_log("***WARNING: 4000 GM accounts found. Next GM accounts are not readed." RETCODE); + } + } + } + } + } + fclose(fp); + + printf("read_gm_account: file '%s' readed (%d GM accounts found).\n", GM_account_filename, c); + login_log("read_gm_account: file '%s' readed (%d GM accounts found)." RETCODE, GM_account_filename, c); + + return 0; +} + +//-------------------------------------------------------------- +// Test of the IP mask +// (ip: IP to be tested, str: mask x.x.x.x/# or x.x.x.x/y.y.y.y) +//-------------------------------------------------------------- +int check_ipmask(unsigned int ip, const unsigned char *str) { + unsigned int mask = 0, i = 0, m, ip2, a0, a1, a2, a3; + unsigned char *p = (unsigned char *)&ip2, *p2 = (unsigned char *)&mask; + + if (sscanf(str, "%d.%d.%d.%d/%n", &a0, &a1, &a2, &a3, &i) != 4 || i == 0) + return 0; + p[0] = a0; p[1] = a1; p[2] = a2; p[3] = a3; + + if (sscanf(str+i, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) == 4) { + p2[0] = a0; p2[1] = a1; p2[2] = a2; p2[3] = a3; + mask = ntohl(mask); + } else if (sscanf(str+i, "%d", &m) == 1 && m >= 0 && m <= 32) { + for(i = 0; i < m && i < 32; i++) + mask = (mask >> 1) | 0x80000000; + } else { + printf("check_ipmask: invalid mask [%s].\n", str); + return 0; + } + +// printf("Tested IP: %08x, network: %08x, network mask: %08x\n", +// (unsigned int)ntohl(ip), (unsigned int)ntohl(ip2), (unsigned int)mask); + return ((ntohl(ip) & mask) == (ntohl(ip2) & mask)); +} + +//--------------------- +// Access control by IP +//--------------------- +int check_ip(unsigned int ip) { + int i; + unsigned char *p = (unsigned char *)&ip; + char buf[32]; + enum { ACF_DEF, ACF_ALLOW, ACF_DENY } flag = ACF_DEF; + + if (access_allownum == 0 && access_denynum == 0) + return 1; // When there is no restriction, all IP are authorised. + +// + 012.345.: front match form, or +// all: all IP are matched, or +// 012.345.678.901/24: network form (mask with # of bits), or +// 012.345.678.901/255.255.255.0: network form (mask with ip mask) +// + Note about the DNS resolution (like www.ne.jp, etc.): +// There is no guarantee to have an answer. +// If we have an answer, there is no guarantee to have a 100% correct value. +// And, the waiting time (to check) can be long (over 1 minute to a timeout). That can block the software. +// So, DNS notation isn't authorised for ip checking. + sprintf(buf, "%d.%d.%d.%d.", p[0], p[1], p[2], p[3]); + + for(i = 0; i < access_allownum; i++) { + const char *p = access_allow + i * ACO_STRSIZE; + if (memcmp(p, buf, strlen(p)) == 0 || check_ipmask(ip, p)) { + flag = ACF_ALLOW; + if (access_order == ACO_ALLOW_DENY) + return 1; // With 'allow, deny' (deny if not allow), allow has priority + break; + } + } + + for(i = 0; i < access_denynum; i++) { + const char *p = access_deny + i * ACO_STRSIZE; + if (memcmp(p, buf, strlen(p)) == 0 || check_ipmask(ip, p)) { + flag = ACF_DENY; + return 0; // At this point, if it's 'deny', we refuse connection. + break; + } + } + + return (flag == ACF_ALLOW || access_order == ACO_DENY_ALLOW) ? 1:0; + // With 'mutual-failture', only 'allow' and non 'deny' IP are authorised. + // A non 'allow' (even non 'deny') IP is not authorised. It's like: if allowed and not denied, it's authorised. + // So, it's disapproval if you have no description at the time of 'mutual-failture'. + // With 'deny,allow' (allow if not deny), because here it's not deny, we authorise. +} + +//-------------------------------- +// Access control by IP for ladmin +//-------------------------------- +int check_ladminip(unsigned int ip) { + int i; + unsigned char *p = (unsigned char *)&ip; + char buf[32]; + + if (access_ladmin_allownum == 0) + return 1; // When there is no restriction, all IP are authorised. + +// + 012.345.: front match form, or +// all: all IP are matched, or +// 012.345.678.901/24: network form (mask with # of bits), or +// 012.345.678.901/255.255.255.0: network form (mask with ip mask) +// + Note about the DNS resolution (like www.ne.jp, etc.): +// There is no guarantee to have an answer. +// If we have an answer, there is no guarantee to have a 100% correct value. +// And, the waiting time (to check) can be long (over 1 minute to a timeout). That can block the software. +// So, DNS notation isn't authorised for ip checking. + sprintf(buf, "%d.%d.%d.%d.", p[0], p[1], p[2], p[3]); + + for(i = 0; i < access_ladmin_allownum; i++) { + const char *p = access_ladmin_allow + i * ACO_STRSIZE; + if (memcmp(p, buf, strlen(p)) == 0 || check_ipmask(ip, p)) { + return 1; + } + } + + return 0; +} + +//----------------------------------------------------- +// Function to suppress control characters in a string. +//----------------------------------------------------- +int remove_control_chars(unsigned char *str) { + int i; + int change = 0; + + for(i = 0; str[i]; i++) { + if (str[i] < 32) { + str[i] = '_'; + change = 1; + } + } + + return change; +} + +//--------------------------------------------------- +// E-mail check: return 0 (not correct) or 1 (valid). +//--------------------------------------------------- +int e_mail_check(unsigned char *email) { + char ch; + unsigned char* last_arobas; + + // athena limits + if (strlen(email) < 3 || strlen(email) > 39) + return 0; + + // part of RFC limits (official reference of e-mail description) + if (strchr(email, '@') == NULL || email[strlen(email)-1] == '@') + return 0; + + if (email[strlen(email)-1] == '.') + return 0; + + last_arobas = strrchr(email, '@'); + + if (strstr(last_arobas, "@.") != NULL || + strstr(last_arobas, "..") != NULL) + return 0; + + for(ch = 1; ch < 32; ch++) { + if (strchr(last_arobas, ch) != NULL) { + return 0; + break; + } + } + + if (strchr(last_arobas, ' ') != NULL || + strchr(last_arobas, ';') != NULL) + return 0; + + // all correct + return 1; +} + +//----------------------------------------------- +// Search an account id +// (return account index or -1 (if not found)) +// If exact account name is not found, +// the function checks without case sensitive +// and returns index if only 1 account is found +// and similar to the searched name. +//----------------------------------------------- +int search_account_index(char* account_name) { + int i, quantity, index; + + quantity = 0; + index = -1; + for(i = 0; i < auth_num; i++) { + // Without case sensitive check (increase the number of similar account names found) + if (stricmp(auth_dat[i].userid, account_name) == 0) { + // Strict comparison (if found, we finish the function immediatly with correct value) + if (strcmp(auth_dat[i].userid, account_name) == 0) + return i; + quantity++; + index = i; + } + } + // Here, the exact account name is not found + // We return the found index of a similar account ONLY if there is 1 similar account + if (quantity == 1) + return index; + + // Exact account name is not found and 0 or more than 1 similar accounts have been found ==> we say not found + return -1; +} + +//-------------------------------------------------------- +// Create a string to save the account in the account file +//-------------------------------------------------------- +int mmo_auth_tostr(char *str, struct auth_dat *p) { + int i; + char *str_p = str; + + str_p += sprintf(str_p, "%d\t%s\t%s\t%s\t%c\t%d\t%d\t" + "%s\t%s\t%ld\t%s\t%s\t%ld\t", + p->account_id, p->userid, p->pass, p->lastlogin, + (p->sex == 2) ? 'S' : (p->sex ? 'M' : 'F'), + p->logincount, p->state, + p->email, p->error_message, + p->connect_until_time, p->last_ip, p->memo, p->ban_until_time); + + for(i = 0; i < p->account_reg2_num; i++) + if (p->account_reg2[i].str[0]) + str_p += sprintf(str_p, "%s,%d ", p->account_reg2[i].str, p->account_reg2[i].value); + + return 0; +} + +//--------------------------------- +// Reading of the accounts database +//--------------------------------- +int mmo_auth_init(void) { + FILE *fp; + int account_id, logincount, state, n, i, j, v; + char line[2048], *p, userid[2048], pass[2048], lastlogin[2048], sex, email[2048], error_message[2048], last_ip[2048], memo[2048]; + time_t ban_until_time; + time_t connect_until_time; + char str[2048]; + int GM_count = 0; + int server_count = 0; + + auth_dat = calloc(sizeof(struct auth_dat) * 256, 1); + auth_max = 256; + + fp = fopen(account_filename, "r"); + if (fp == NULL) { + // no account file -> no account -> no login, including char-server (ERROR) + printf("\033[1;31mmmo_auth_init: Accounts file [%s] not found.\033[0m\n", account_filename); + return 0; + } + + while(fgets(line, sizeof(line)-1, fp) != NULL) { + if (line[0] == '/' && line[1] == '/') + continue; + line[sizeof(line)-1] = '\0'; + p = line; + + // database version reading (v2) + if (((i = sscanf(line, "%d\t%[^\t]\t%[^\t]\t%[^\t]\t%c\t%d\t%d\t" + "%[^\t]\t%[^\t]\t%ld\t%[^\t]\t%[^\t]\t%ld%n", + &account_id, userid, pass, lastlogin, &sex, &logincount, &state, + email, error_message, &connect_until_time, last_ip, memo, &ban_until_time, &n)) == 13 && line[n] == '\t') || + ((i = sscanf(line, "%d\t%[^\t]\t%[^\t]\t%[^\t]\t%c\t%d\t%d\t" + "%[^\t]\t%[^\t]\t%ld\t%[^\t]\t%[^\t]%n", + &account_id, userid, pass, lastlogin, &sex, &logincount, &state, + email, error_message, &connect_until_time, last_ip, memo, &n)) == 12 && line[n] == '\t')) { + n = n + 1; + + // Some checks + if (account_id > END_ACCOUNT_NUM) { + printf("\033[1;31mmmo_auth_init: ******Error: an account has an id higher than %d\n", END_ACCOUNT_NUM); + printf(" account id #%d -> account not read (saved in log file).\033[0m\n", account_id); + login_log("mmmo_auth_init: ******Error: an account has an id higher than %d." RETCODE, END_ACCOUNT_NUM); + login_log(" account id #%d -> account not read (saved in next line):" RETCODE, account_id); + login_log("%s", line); + continue; + } + userid[23] = '\0'; + remove_control_chars(userid); + for(j = 0; j < auth_num; j++) { + if (auth_dat[j].account_id == account_id) { + printf("\033[1;31mmmo_auth_init: ******Error: an account has an identical id to another.\n"); + printf(" account id #%d -> new account not read (saved in log file).\033[0m\n", account_id); + login_log("mmmo_auth_init: ******Error: an account has an identical id to another." RETCODE); + login_log(" account id #%d -> new account not read (saved in next line):" RETCODE, account_id); + login_log("%s", line); + break; + } else if (strcmp(auth_dat[j].userid, userid) == 0) { + printf("\033[1;31mmmo_auth_init: ******Error: account name already exists.\n"); + printf(" account name '%s' -> new account not read.\n", userid); // 2 lines, account name can be long. + printf(" Account saved in log file.\033[0m\n"); + login_log("mmmo_auth_init: ******Error: an account has an identical id to another." RETCODE); + login_log(" account id #%d -> new account not read (saved in next line):" RETCODE, account_id); + login_log("%s", line); + break; + } + } + if (j != auth_num) + continue; + + if (auth_num >= auth_max) { + auth_max += 256; + auth_dat = realloc(auth_dat, sizeof(struct auth_dat) * auth_max); + } + + memset(&auth_dat[auth_num], '\0', sizeof(struct auth_dat)); + + auth_dat[auth_num].account_id = account_id; + + strncpy(auth_dat[auth_num].userid, userid, 24); + + pass[23] = '\0'; + remove_control_chars(pass); + strncpy(auth_dat[auth_num].pass, pass, 24); + + lastlogin[23] = '\0'; + remove_control_chars(lastlogin); + strncpy(auth_dat[auth_num].lastlogin, lastlogin, 24); + + auth_dat[auth_num].sex = (sex == 'S' || sex == 's') ? 2 : (sex == 'M' || sex == 'm'); + + if (logincount >= 0) + auth_dat[auth_num].logincount = logincount; + else + auth_dat[auth_num].logincount = 0; + + if (state > 255) + auth_dat[auth_num].state = 100; + else if (state < 0) + auth_dat[auth_num].state = 0; + else + auth_dat[auth_num].state = state; + + if (e_mail_check(email) == 0) { + printf("Account %s (%d): invalid e-mail (replaced par a@a.com).\n", auth_dat[auth_num].userid, auth_dat[auth_num].account_id); + strncpy(auth_dat[auth_num].email, "a@a.com", 40); + } else { + remove_control_chars(email); + strncpy(auth_dat[auth_num].email, email, 40); + } + + error_message[19] = '\0'; + remove_control_chars(error_message); + if (error_message[0] == '\0' || state != 7) { // 7, because state is packet 0x006a value + 1 + strncpy(auth_dat[auth_num].error_message, "-", 20); + } else { + strncpy(auth_dat[auth_num].error_message, error_message, 20); + } + + if (i == 13) + auth_dat[auth_num].ban_until_time = ban_until_time; + else + auth_dat[auth_num].ban_until_time = 0; + + auth_dat[auth_num].connect_until_time = connect_until_time; + + last_ip[15] = '\0'; + remove_control_chars(last_ip); + strncpy(auth_dat[auth_num].last_ip, last_ip, 16); + + memo[254] = '\0'; + remove_control_chars(memo); + strncpy(auth_dat[auth_num].memo, memo, 255); + + for(j = 0; j < ACCOUNT_REG2_NUM; j++) { + p += n; + if (sscanf(p, "%[^\t,],%d %n", str, &v, &n) != 2) { + // We must check if a str is void. If it's, we can continue to read other REG2. + // Account line will have something like: str2,9 ,9 str3,1 (here, ,9 is not good) + if (p[0] == ',' && sscanf(p, ",%d %n", &v, &n) == 1) { + j--; + continue; + } else + break; + } + str[31] = '\0'; + remove_control_chars(str); + strncpy(auth_dat[auth_num].account_reg2[j].str, str, 32); + auth_dat[auth_num].account_reg2[j].value = v; + } + auth_dat[auth_num].account_reg2_num = j; + + if (isGM(account_id) > 0) + GM_count++; + if (auth_dat[auth_num].sex == 2) + server_count++; + + auth_num++; + if (account_id >= account_id_count) + account_id_count = account_id + 1; + + // Old athena database version reading (v1) + } else if ((i = sscanf(line, "%d\t%[^\t]\t%[^\t]\t%[^\t]\t%c\t%d\t%d\t%n", + &account_id, userid, pass, lastlogin, &sex, &logincount, &state, &n)) >= 5) { + if (account_id > END_ACCOUNT_NUM) { + printf("\033[1;31mmmo_auth_init: ******Error: an account has an id higher than %d\n", END_ACCOUNT_NUM); + printf(" account id #%d -> account not read (saved in log file).\033[0m\n", account_id); + login_log("mmmo_auth_init: ******Error: an account has an id higher than %d." RETCODE, END_ACCOUNT_NUM); + login_log(" account id #%d -> account not read (saved in next line):" RETCODE, account_id); + login_log("%s", line); + continue; + } + userid[23] = '\0'; + remove_control_chars(userid); + for(j = 0; j < auth_num; j++) { + if (auth_dat[j].account_id == account_id) { + printf("\033[1;31mmmo_auth_init: ******Error: an account has an identical id to another.\n"); + printf(" account id #%d -> new account not read (saved in log file).\033[0m\n", account_id); + login_log("mmmo_auth_init: ******Error: an account has an identical id to another." RETCODE); + login_log(" account id #%d -> new account not read (saved in next line):" RETCODE, account_id); + login_log("%s", line); + break; + } else if (strcmp(auth_dat[j].userid, userid) == 0) { + printf("\033[1;31mmmo_auth_init: ******Error: account name already exists.\n"); + printf(" account name '%s' -> new account not read.\n", userid); // 2 lines, account name can be long. + printf(" Account saved in log file.\033[0m\n"); + login_log("mmmo_auth_init: ******Error: an account has an identical id to another." RETCODE); + login_log(" account id #%d -> new account not read (saved in next line):" RETCODE, account_id); + login_log("%s", line); + break; + } + } + if (j != auth_num) + continue; + + if (auth_num >= auth_max) { + auth_max += 256; + auth_dat = realloc(auth_dat, sizeof(struct auth_dat) * auth_max); + } + + memset(&auth_dat[auth_num], '\0', sizeof(struct auth_dat)); + + auth_dat[auth_num].account_id = account_id; + + strncpy(auth_dat[auth_num].userid, userid, 24); + + pass[23] = '\0'; + remove_control_chars(pass); + strncpy(auth_dat[auth_num].pass, pass, 24); + + lastlogin[23] = '\0'; + remove_control_chars(lastlogin); + strncpy(auth_dat[auth_num].lastlogin, lastlogin, 24); + + auth_dat[auth_num].sex = (sex == 'S' || sex == 's') ? 2 : (sex == 'M' || sex == 'm'); + + if (i >= 6) { + if (logincount >= 0) + auth_dat[auth_num].logincount = logincount; + else + auth_dat[auth_num].logincount = 0; + } else + auth_dat[auth_num].logincount = 0; + + if (i >= 7) { + if (state > 255) + auth_dat[auth_num].state = 100; + else if (state < 0) + auth_dat[auth_num].state = 0; + else + auth_dat[auth_num].state = state; + } else + auth_dat[auth_num].state = 0; + + // Initialization of new data + strncpy(auth_dat[auth_num].email, "a@a.com", 40); + strncpy(auth_dat[auth_num].error_message, "-", 20); + auth_dat[auth_num].ban_until_time = 0; + auth_dat[auth_num].connect_until_time = 0; + strncpy(auth_dat[auth_num].last_ip, "-", 16); + strncpy(auth_dat[auth_num].memo, "-", 255); + + for(j = 0; j < ACCOUNT_REG2_NUM; j++) { + p += n; + if (sscanf(p, "%[^\t,],%d %n", str, &v, &n) != 2) { + // We must check if a str is void. If it's, we can continue to read other REG2. + // Account line will have something like: str2,9 ,9 str3,1 (here, ,9 is not good) + if (p[0] == ',' && sscanf(p, ",%d %n", &v, &n) == 1) { + j--; + continue; + } else + break; + } + str[31] = '\0'; + remove_control_chars(str); + strncpy(auth_dat[auth_num].account_reg2[j].str, str, 32); + auth_dat[auth_num].account_reg2[j].value = v; + } + auth_dat[auth_num].account_reg2_num = j; + + if (isGM(account_id) > 0) + GM_count++; + if (auth_dat[auth_num].sex == 2) + server_count++; + + auth_num++; + if (account_id >= account_id_count) + account_id_count = account_id + 1; + + } else { + i = 0; + if (sscanf(line, "%d\t%%newid%%\n%n", &account_id, &i) == 1 && + i > 0 && account_id > account_id_count) + account_id_count = account_id; + } + } + fclose(fp); + + if (auth_num == 0) { + printf("mmo_auth_init: No account found in %s.\n", account_filename); + sprintf(line, "No account found in %s.", account_filename); + } else { + if (auth_num == 1) { + printf("mmo_auth_init: 1 account read in %s,\n", account_filename); + sprintf(line, "1 account read in %s,", account_filename); + } else { + printf("mmo_auth_init: %d accounts read in %s,\n", auth_num, account_filename); + sprintf(line, "%d accounts read in %s,", auth_num, account_filename); + } + if (GM_count == 0) { + printf(" of which is no GM account, and "); + sprintf(str, "%s of which is no GM account and", line); + } else if (GM_count == 1) { + printf(" of which is 1 GM account, and "); + sprintf(str, "%s of which is 1 GM account and", line); + } else { + printf(" of which is %d GM accounts, and ", GM_count); + sprintf(str, "%s of which is %d GM accounts and", line, GM_count); + } + if (server_count == 0) { + printf("no server account ('S').\n"); + sprintf(line, "%s no server account ('S').", str); + } else if (server_count == 1) { + printf("1 server account ('S').\n"); + sprintf(line, "%s 1 server account ('S').", str); + } else { + printf("%d server accounts ('S').\n", server_count); + sprintf(line, "%s %d server accounts ('S').", str, server_count); + } + } + login_log("%s" RETCODE, line); + + return 0; +} + +//------------------------------------------ +// Writing of the accounts database file +// (accounts are sorted by id before save) +//------------------------------------------ +void mmo_auth_sync(void) { + FILE *fp; + int i, j, k, lock; + int id[auth_num]; + char line[65536]; + + // Sorting before save + for(i = 0; i < auth_num; i++) { + id[i] = i; + for(j = 0; j < i; j++) { + if (auth_dat[i].account_id < auth_dat[id[j]].account_id) { + for(k = i; k > j; k--) + id[k] = id[k-1]; + id[j] = i; // id[i] + break; + } + } + } + + // Data save + fp = lock_fopen(account_filename, &lock); + if (fp == NULL) + return; + fprintf(fp, "// Accounts file: here are saved all information about the accounts.\n"); + fprintf(fp, "// Structure: ID, account name, password, last login time, sex, # of logins, state, email, error message for state 7, validity time, last (accepted) login ip, memo field, ban timestamp, repeated(register text, register value)\n"); + fprintf(fp, "// Some explanations:\n"); + fprintf(fp, "// account name : between 4 to 23 char for a normal account (standard client can't send less than 4 char).\n"); + fprintf(fp, "// account password: between 4 to 23 char\n"); + fprintf(fp, "// sex : M or F for normal accounts, S for server accounts\n"); + fprintf(fp, "// state : 0: account is ok, 1 to 256: error code of packet 0x006a + 1\n"); + fprintf(fp, "// email : between 3 to 39 char (a@a.com is like no email)\n"); + fprintf(fp, "// error message : text for the state 7: 'Your are Prohibited to login until '. Max 19 char\n"); + fprintf(fp, "// valitidy time : 0: unlimited account, : date calculated by addition of 1/1/1970 + value (number of seconds since the 1/1/1970)\n"); + fprintf(fp, "// memo field : max 254 char\n"); + fprintf(fp, "// ban time : 0: no ban, : banned until the date: date calculated by addition of 1/1/1970 + value (number of seconds since the 1/1/1970)\n"); + for(i = 0; i < auth_num; i++) { + k = id[i]; // use of sorted index + if (auth_dat[k].account_id < 0) + continue; + + mmo_auth_tostr(line, &auth_dat[k]); + fprintf(fp, "%s" RETCODE, line); + } + fprintf(fp, "%d\t%%newid%%\n", account_id_count); + + lock_fclose(fp, account_filename, &lock); + + // set new counter to minimum number of auth before save + auth_before_save_file = auth_num / AUTH_SAVE_FILE_DIVIDER; // Re-initialise counter. We have save. + if (auth_before_save_file < AUTH_BEFORE_SAVE_FILE) + auth_before_save_file = AUTH_BEFORE_SAVE_FILE; + + return; +} + +//----------------------------------------------------- +// Check if we must save accounts file or not +// every minute, we check if we must save because we +// have do some authentifications without arrive to +// the minimum of authentifications for the save. +// Note: all other modification of accounts (deletion, +// change of some informations excepted lastip/ +// lastlogintime, creation) are always save +// immediatly and set the minimum of +// authentifications to its initialization value. +//----------------------------------------------------- +int check_auth_sync(int tid, unsigned int tick, int id, int data) { + // we only save if necessary: + // we have do some authentifications without do saving + if (auth_before_save_file < AUTH_BEFORE_SAVE_FILE || + auth_before_save_file < (int)(auth_num / AUTH_SAVE_FILE_DIVIDER)) + mmo_auth_sync(); + + return 0; +} + +//-------------------------------------------------------------------- +// Packet send to all char-servers, except one (wos: without our self) +//-------------------------------------------------------------------- +int charif_sendallwos(int sfd, unsigned char *buf, unsigned int len) { + int i, c; + + for(i = 0, c = 0; i < MAX_SERVERS; i++) { + int fd; + if ((fd = server_fd[i]) >= 0 && fd != sfd) { + memcpy(WFIFOP(fd,0), buf, len); + WFIFOSET(fd, len); + c++; + } + } + return c; +} + +//----------------------------------------------------- +// Send GM accounts to all char-server +//----------------------------------------------------- +void send_GM_accounts() { + int i; + char buf[32000]; + int GM_value; + int len; + + len = 4; + WBUFW(buf,0) = 0x2732; + for(i = 0; i < auth_num; i++) + // send only existing accounts. We can not create a GM account when server is online. + if ((GM_value = isGM(auth_dat[i].account_id)) > 0) { + WBUFL(buf,len) = auth_dat[i].account_id; + WBUFB(buf,len+4) = (unsigned char)GM_value; + len += 5; + } + WBUFW(buf,2) = len; + charif_sendallwos(-1, buf, len); + + return; +} + +//----------------------------------------------------- +// Check if GM file account have been changed +//----------------------------------------------------- +int check_GM_file(int tid, unsigned int tick, int id, int data) { + struct stat file_stat; + long new_time; + + // if we would not check + if (gm_account_filename_check_timer < 1) + return 0; + + // get last modify time/date + if (stat(GM_account_filename, &file_stat)) + new_time = 0; // error + else + new_time = file_stat.st_mtime; + + if (new_time != creation_time_GM_account_file) { + read_gm_account(); + send_GM_accounts(); + } + + return 0; +} + +//------------------------------------- +// Account creation (with e-mail check) +//------------------------------------- +int mmo_auth_new(struct mmo_account* account, char sex, char* email) { + time_t timestamp, timestamp_temp; + struct tm *tmtime; + int i = auth_num; + + if (auth_num >= auth_max) { + auth_max += 256; + auth_dat = realloc(auth_dat, sizeof(struct auth_dat) * auth_max); + } + + memset(&auth_dat[i], '\0', sizeof(struct auth_dat)); + + while (isGM(account_id_count) > 0) + account_id_count++; + + auth_dat[i].account_id = account_id_count++; + + strncpy(auth_dat[i].userid, account->userid, 24); + auth_dat[i].userid[23] = '\0'; + + strncpy(auth_dat[i].pass, account->passwd, 24); + auth_dat[i].pass[23] = '\0'; + + memcpy(auth_dat[i].lastlogin, "-", 2); + + auth_dat[i].sex = (sex == 'M'); + + auth_dat[i].logincount = 0; + + auth_dat[i].state = 0; + + if (e_mail_check(email) == 0) + strncpy(auth_dat[i].email, "a@a.com", 40); + else + strncpy(auth_dat[i].email, email, 40); + + strncpy(auth_dat[i].error_message, "-", 20); + + auth_dat[i].ban_until_time = 0; + + if (start_limited_time < 0) + auth_dat[i].connect_until_time = 0; // unlimited + else { // limited time + timestamp = time(NULL) + start_limited_time; + // double conversion to be sure that it is possible + tmtime = localtime(×tamp); + timestamp_temp = mktime(tmtime); + if (timestamp_temp != -1 && (timestamp_temp + 3600) >= timestamp) // check possible value and overflow (and avoid summer/winter hour) + auth_dat[i].connect_until_time = timestamp_temp; + else + auth_dat[i].connect_until_time = 0; // unlimited + } + + strncpy(auth_dat[i].last_ip, "-", 16); + + strncpy(auth_dat[i].memo, "-", 255); + + auth_dat[i].account_reg2_num = 0; + + auth_num++; + + return (account_id_count - 1); +} + +//--------------------------------------- +// Check/authentification of a connection +//--------------------------------------- +int mmo_auth(struct mmo_account* account, int fd) { + int i; + struct timeval tv; + char tmpstr[256]; + int len, newaccount = 0; + char md5str[64], md5bin[32]; + char ip[16]; + unsigned char *sin_addr = (unsigned char *)&session[fd]->client_addr.sin_addr; + + sprintf(ip, "%d.%d.%d.%d", sin_addr[0], sin_addr[1], sin_addr[2], sin_addr[3]); + + len = strlen(account->userid) - 2; + // Account creation with _M/_F + if (account->passwdenc == 0 && account->userid[len] == '_' && + (account->userid[len+1] == 'F' || account->userid[len+1] == 'M') && new_account_flag == 1 && + account_id_count <= END_ACCOUNT_NUM && len >= 4 && strlen(account->passwd) >= 4) { + if (new_account_flag == 1) + newaccount = 1; + account->userid[len] = '\0'; + } + + // Strict account search + for(i = 0; i < auth_num; i++) { + if (strcmp(account->userid, auth_dat[i].userid) == 0) + break; + } + // if there is no creation request and strict account search fails, we do a no sensitive case research for index + if (newaccount == 0 && i == auth_num) { + i = search_account_index(account->userid); + if (i == -1) + i = auth_num; + else + memcpy(account->userid, auth_dat[i].userid, 24); // for the possible tests/checks afterwards (copy correcte sensitive case). + } + + if (i != auth_num) { + int encpasswdok = 0; + struct login_session_data *ld; + if (newaccount) { + login_log("Attempt of creation of an already existant account (account: %s_%c, pass: %s, received pass: %s, ip: %s)" RETCODE, + account->userid, account->userid[len+1], auth_dat[i].pass, account->passwd, ip); + return 9; // 9 = Account already exists + } + ld = session[fd]->session_data; +#ifdef PASSWORDENC + if (account->passwdenc > 0) { + int j = account->passwdenc; + if (!ld) { + login_log("Md5 key not created (account: %s, ip: %s)" RETCODE, account->userid, ip); + return 1; // 1 = Incorrect Password + } + if (j > 2) + j = 1; + do { + if (j == 1) { + strncpy(md5str, ld->md5key, sizeof(ld->md5key)); // 20 + strcat(md5str, auth_dat[i].pass); // 24 + } else if (j == 2) { + strncpy(md5str, auth_dat[i].pass, sizeof(auth_dat[i].pass)); // 24 + strcat(md5str, ld->md5key); // 20 + } else + md5str[0] = '\0'; + md5str[sizeof(md5str)-1] = '\0'; // 64 + MD5_String2binary(md5str, md5bin); + encpasswdok = (memcmp(account->passwd, md5bin, 16) == 0); + } while (j < 2 && !encpasswdok && (j++) != account->passwdenc); +// printf("key[%s] md5 [%s] ", md5key, md5); +// printf("client [%s] accountpass [%s]\n", account->passwd, auth_dat[i].pass); + } +#endif + if ((strcmp(account->passwd, auth_dat[i].pass) && !encpasswdok)) { + if (account->passwdenc == 0) + login_log("Invalid password (account: %s, pass: %s, received pass: %s, ip: %s)" RETCODE, account->userid, auth_dat[i].pass, account->passwd, ip); +#ifdef PASSWORDENC + else { + char logbuf[512], *p = logbuf; + int j; + p += sprintf(p, "Invalid password (account: %s, received md5[", account->userid); + for(j = 0; j < 16; j++) + p += sprintf(p, "%02x", ((unsigned char *)account->passwd)[j]); + p += sprintf(p,"] calculated md5["); + for(j = 0; j < 16; j++) + p += sprintf(p, "%02x", ((unsigned char *)md5bin)[j]); + p += sprintf(p, "] md5 key["); + for(j = 0; j < ld->md5keylen; j++) + p += sprintf(p, "%02x", ((unsigned char *)ld->md5key)[j]); + p += sprintf(p, "], ip: %s)" RETCODE, ip); + login_log(logbuf); + } +#endif + return 1; // 1 = Incorrect Password + } + + if (auth_dat[i].state) { + login_log("Connection refused (account: %s, pass: %s, state: %d, ip: %s)" RETCODE, + account->userid, account->passwd, auth_dat[i].state, ip); + switch(auth_dat[i].state) { // packet 0x006a value + 1 + case 1: // 0 = Unregistered ID + case 2: // 1 = Incorrect Password + case 3: // 2 = This ID is expired + case 4: // 3 = Rejected from Server + case 5: // 4 = You have been blocked by the GM Team + case 6: // 5 = Your Game's EXE file is not the latest version + case 7: // 6 = Your are Prohibited to log in until %s + case 8: // 7 = Server is jammed due to over populated + case 9: // 8 = No MSG (actually, all states after 9 except 99 are No MSG, use only this) + case 100: // 99 = This ID has been totally erased + return auth_dat[i].state - 1; + break; + default: + return 99; // 99 = ID has been totally erased + break; + } + } + + if (auth_dat[i].ban_until_time != 0) { // if account is banned + strftime(tmpstr, 20, date_format, localtime(&auth_dat[i].ban_until_time)); + tmpstr[19] = '\0'; + if (auth_dat[i].ban_until_time > time(NULL)) { // always banned + login_log("Connection refused (account: %s, pass: %s, banned until %s, ip: %s)" RETCODE, + account->userid, account->passwd, tmpstr, ip); + return 6; // 6 = Your are Prohibited to log in until %s + } else { // ban is finished + login_log("End of ban (account: %s, pass: %s, previously banned until %s -> not more banned, ip: %s)" RETCODE, + account->userid, account->passwd, tmpstr, ip); + auth_dat[i].ban_until_time = 0; // reset the ban time + } + } + + if (auth_dat[i].connect_until_time != 0 && auth_dat[i].connect_until_time < time(NULL)) { + login_log("Connection refused (account: %s, pass: %s, expired ID, ip: %s)" RETCODE, + account->userid, account->passwd, ip); + return 2; // 2 = This ID is expired + } + + login_log("Authentification accepted (account: %s (id: %d), ip: %s)" RETCODE, account->userid, auth_dat[i].account_id, ip); + } else { + if (newaccount == 0) { + login_log("Unknown account (account: %s, received pass: %s, ip: %s)" RETCODE, + account->userid, account->passwd, ip); + return 0; // 0 = Unregistered ID + } else { + int new_id = mmo_auth_new(account, account->userid[len+1], "a@a.com"); + login_log("Account creation and authentification accepted (account %s (id: %d), pass: %s, sex: %c, connection with _F/_M, ip: %s)" RETCODE, + account->userid, new_id, account->passwd, account->userid[len+1], ip); + auth_before_save_file = 0; // Creation of an account -> save accounts file immediatly + } + } + + gettimeofday(&tv, NULL); + strftime(tmpstr, 24, date_format, localtime(&(tv.tv_sec))); + sprintf(tmpstr + strlen(tmpstr), ".%03d", (int)tv.tv_usec / 1000); + + account->account_id = auth_dat[i].account_id; + account->login_id1 = rand(); + account->login_id2 = rand(); + memcpy(account->lastlogin, auth_dat[i].lastlogin, 24); + memcpy(auth_dat[i].lastlogin, tmpstr, 24); + account->sex = auth_dat[i].sex; + strncpy(auth_dat[i].last_ip, ip, 16); + auth_dat[i].logincount++; + + // Save until for change ip/time of auth is not very useful => limited save for that + // Save there informations isnot necessary, because they are saved in log file. + if (--auth_before_save_file <= 0) // Reduce counter. 0 or less, we save + mmo_auth_sync(); + + return -1; // account OK +} + +//------------------------------- +// Char-server anti-freeze system +//------------------------------- +int char_anti_freeze_system(int tid, unsigned int tick, int id, int data) { + int i; + + //printf("Entering in char_anti_freeze_system function to check freeze of servers.\n"); + for(i = 0; i < MAX_SERVERS; i++) { + if (server_fd[i] >= 0) {// if char-server is online + //printf("char_anti_freeze_system: server #%d '%s', flag: %d.\n", i, server[i].name, server_freezeflag[i]); + if (server_freezeflag[i]-- < 1) { // Char-server anti-freeze system. Counter. 5 ok, 4...0 freezed + printf("Char-server anti-freeze system: char-server #%d '%s' is freezed -> disconnection.\n", i, server[i].name); + login_log("Char-server anti-freeze system: char-server #%d '%s' is freezed -> disconnection." RETCODE, + i, server[i].name); + session[server_fd[i]]->eof = 1; + } + } + } + + return 0; +} + +//-------------------------------- +// Packet parsing for char-servers +//-------------------------------- +int parse_fromchar(int fd) { + int i, j, id; + unsigned char *p = (unsigned char *) &session[fd]->client_addr.sin_addr; + char ip[16]; + + sprintf(ip, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); + + for(id = 0; id < MAX_SERVERS; id++) + if (server_fd[id] == fd) + break; + if (id == MAX_SERVERS || session[fd]->eof) { + if (id < MAX_SERVERS) { + printf("Char-server '%s' has disconnected.\n", server[id].name); + login_log("Char-server '%s' has disconnected (ip: %s)." RETCODE, + server[id].name, ip); + server_fd[id] = -1; + memset(&server[id], 0, sizeof(struct mmo_char_server)); + } + close(fd); + delete_session(fd); + return 0; + } + + while (RFIFOREST(fd) >= 2) { + if (display_parse_fromchar == 2 || (display_parse_fromchar == 1 && RFIFOW(fd,0) != 0x2714)) // 0x2714 is done very often (number of players) + printf("parse_fromchar: connection #%d, packet: 0x%x (with being read: %d bytes).\n", fd, RFIFOW(fd,0), RFIFOREST(fd)); + + switch (RFIFOW(fd,0)) { + // request from map-server via char-server to reload GM accounts (by Yor). + case 0x2709: + login_log("Char-server '%s': Request to re-load GM configuration file (ip: %s)." RETCODE, server[id].name, ip); + read_gm_account(); + // send GM accounts to all char-servers + send_GM_accounts(); + RFIFOSKIP(fd,2); + break; + + case 0x2712: // request from char-server to authentify an account + if (RFIFOREST(fd) < 19) + return 0; + { + int acc; + acc = RFIFOL(fd,2); // speed up + for(i = 0; i < AUTH_FIFO_SIZE; i++) { + if (auth_fifo[i].account_id == acc && + auth_fifo[i].login_id1 == RFIFOL(fd,6) && +#if CMP_AUTHFIFO_LOGIN2 != 0 + auth_fifo[i].login_id2 == RFIFOL(fd,10) && // relate to the versions higher than 18 +#endif + auth_fifo[i].sex == RFIFOB(fd,14) && + (!check_ip_flag || auth_fifo[i].ip == RFIFOL(fd,15)) && + !auth_fifo[i].delflag) { + int p, k; + auth_fifo[i].delflag = 1; + login_log("Char-server '%s': authentification of the account %d accepted (ip: %s)." RETCODE, + server[id].name, acc, ip); +// printf("%d\n", i); + for(k = 0; k < auth_num; k++) { + if (auth_dat[k].account_id == acc) { + WFIFOW(fd,0) = 0x2729; // Sending of the account_reg2 + WFIFOL(fd,4) = acc; + for(p = 8, j = 0; j < auth_dat[k].account_reg2_num; p += 36, j++) { + memcpy(WFIFOP(fd,p), auth_dat[k].account_reg2[j].str, 32); + WFIFOL(fd,p+32) = auth_dat[k].account_reg2[j].value; + } + WFIFOW(fd,2) = p; + WFIFOSET(fd,p); +// printf("parse_fromchar: Sending of account_reg2: login->char (auth fifo)\n"); + WFIFOW(fd,0) = 0x2713; + WFIFOL(fd,2) = acc; + WFIFOB(fd,6) = 0; + memcpy(WFIFOP(fd, 7), auth_dat[k].email, 40); + WFIFOL(fd,47) = (unsigned long)auth_dat[k].connect_until_time; + WFIFOSET(fd,51); + break; + } + } + break; + } + } + // authentification not found + if (i == AUTH_FIFO_SIZE) { + login_log("Char-server '%s': authentification of the account %d REFUSED (ip: %s)." RETCODE, + server[id].name, acc, ip); + WFIFOW(fd,0) = 0x2713; + WFIFOL(fd,2) = acc; + WFIFOB(fd,6) = 1; + // It is unnecessary to send email + // It is unnecessary to send validity date of the account + WFIFOSET(fd,51); + } + } + RFIFOSKIP(fd,19); + break; + + case 0x2714: + if (RFIFOREST(fd) < 6) + return 0; + //printf("parse_fromchar: Receiving of the users number of the server '%s': %d\n", server[id].name, RFIFOL(fd,2)); + server[id].users = RFIFOL(fd,2); + if(anti_freeze_enable) + server_freezeflag[id] = 5; // Char anti-freeze system. Counter. 5 ok, 4...0 freezed + RFIFOSKIP(fd,6); + break; + + // we receive a e-mail creation of an account with a default e-mail (no answer) + case 0x2715: { + int acc; + char email[40]; + if (RFIFOREST(fd) < 46) + return 0; + acc = RFIFOL(fd,2); // speed up + memcpy(email, RFIFOP(fd,6), 40); + email[39] = '\0'; + remove_control_chars(email); + //printf("parse_fromchar: an e-mail creation of an account with a default e-mail: server '%s', account: %d, e-mail: '%s'.\n", server[id].name, acc, RFIFOP(fd,6)); + if (e_mail_check(email) == 0) + login_log("Char-server '%s': Attempt to create an e-mail on an account with a default e-mail REFUSED - e-mail is invalid (account: %d, ip: %s)" RETCODE, + server[id].name, acc, ip); + else { + for(i = 0; i < auth_num; i++) { + if (auth_dat[i].account_id == acc && (strcmp(auth_dat[i].email, "a@a.com") == 0 || auth_dat[i].email[0] == '\0')) { + memcpy(auth_dat[i].email, email, 40); + login_log("Char-server '%s': Create an e-mail on an account with a default e-mail (account: %d, new e-mail: %s, ip: %s)." RETCODE, + server[id].name, acc, email, ip); + // Save + mmo_auth_sync(); + break; + } + } + if (i == auth_num) + login_log("Char-server '%s': Attempt to create an e-mail on an account with a default e-mail REFUSED - account doesn't exist or e-mail of account isn't default e-mail (account: %d, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + RFIFOSKIP(fd,46); + break; + + // We receive an e-mail/limited time request, because a player comes back from a map-server to the char-server + } + case 0x2716: + if (RFIFOREST(fd) < 6) + return 0; + //printf("parse_fromchar: E-mail/limited time request from '%s' server (concerned account: %d)\n", server[id].name, RFIFOL(fd,2)); + for(i = 0; i < auth_num; i++) { + if (auth_dat[i].account_id == RFIFOL(fd,2)) { + login_log("Char-server '%s': e-mail of the account %d found (ip: %s)." RETCODE, + server[id].name, RFIFOL(fd,2), ip); + WFIFOW(fd,0) = 0x2717; + WFIFOL(fd,2) = RFIFOL(fd,2); + memcpy(WFIFOP(fd, 6), auth_dat[i].email, 40); + WFIFOL(fd,46) = (unsigned long)auth_dat[i].connect_until_time; + WFIFOSET(fd,50); + break; + } + } + if (i == auth_num) { + login_log("Char-server '%s': e-mail of the account %d NOT found (ip: %s)." RETCODE, + server[id].name, RFIFOL(fd,2), ip); + } + RFIFOSKIP(fd,6); + break; + + case 0x2720: // To become GM request + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + { + int acc; + unsigned char buf[10]; + FILE *fp; + acc = RFIFOL(fd,4); + //printf("parse_fromchar: Request to become a GM acount from %d account.\n", acc); + WBUFW(buf,0) = 0x2721; + WBUFL(buf,2) = acc; + WBUFL(buf,6) = 0; + if (strcmp(RFIFOP(fd,8), gm_pass) == 0) { + // only non-GM can become GM + if (isGM(acc) == 0) { + // if we autorise creation + if (level_new_gm > 0) { + // if we can open the file to add the new GM + if ((fp = fopen(GM_account_filename, "a")) != NULL) { + char tmpstr[24]; + struct timeval tv; + gettimeofday(&tv, NULL); + strftime(tmpstr, 23, date_format, localtime(&(tv.tv_sec))); + fprintf(fp, RETCODE "// %s: @GM command on account %d" RETCODE "%d %d" RETCODE, tmpstr, acc, acc, level_new_gm); + fclose(fp); + WBUFL(buf,6) = level_new_gm; + read_gm_account(); + send_GM_accounts(); + printf("GM Change of the account %d: level 0 -> %d.\n", acc, level_new_gm); + login_log("Char-server '%s': GM Change of the account %d: level 0 -> %d (ip: %s)." RETCODE, + server[id].name, acc, level_new_gm, ip); + } else { + printf("Error of GM change (suggested account: %d, correct password, unable to add a GM account in GM accounts file)\n", acc); + login_log("Char-server '%s': Error of GM change (suggested account: %d, correct password, unable to add a GM account in GM accounts file, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + } else { + printf("Error of GM change (suggested account: %d, correct password, but GM creation is disable (level_new_gm = 0))\n", acc); + login_log("Char-server '%s': Error of GM change (suggested account: %d, correct password, but GM creation is disable (level_new_gm = 0), ip: %s)." RETCODE, + server[id].name, acc, ip); + } + } else { + printf("Error of GM change (suggested account: %d (already GM), correct password).\n", acc); + login_log("Char-server '%s': Error of GM change (suggested account: %d (already GM), correct password, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + } else { + printf("Error of GM change (suggested account: %d, invalid password).\n", acc); + login_log("Char-server '%s': Error of GM change (suggested account: %d, invalid password, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + charif_sendallwos(-1, buf, 10); + } + RFIFOSKIP(fd, RFIFOW(fd,2)); + return 0; + + // Map server send information to change an email of an account via char-server + case 0x2722: // 0x2722 .L .40B .40B + if (RFIFOREST(fd) < 86) + return 0; + { + int acc; + char actual_email[40], new_email[40]; + acc = RFIFOL(fd,2); + memcpy(actual_email, RFIFOP(fd,6), 40); + actual_email[39] = '\0'; + remove_control_chars(actual_email); + memcpy(new_email, RFIFOP(fd,46), 40); + new_email[39] = '\0'; + remove_control_chars(new_email); + if (e_mail_check(actual_email) == 0) + login_log("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command), but actual email is invalid (account: %d, ip: %s)" RETCODE, + server[id].name, acc, ip); + else if (e_mail_check(new_email) == 0) + login_log("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command) with a invalid new e-mail (account: %d, ip: %s)" RETCODE, + server[id].name, acc, ip); + else if (strcmpi(new_email, "a@a.com") == 0) + login_log("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command) with a default e-mail (account: %d, ip: %s)" RETCODE, + server[id].name, acc, ip); + else { + for(i = 0; i < auth_num; i++) { + if (auth_dat[i].account_id == acc) { + if (strcmpi(auth_dat[i].email, actual_email) == 0) { + memcpy(auth_dat[i].email, new_email, 40); + login_log("Char-server '%s': Modify an e-mail on an account (@email GM command) (account: %d (%s), new e-mail: %s, ip: %s)." RETCODE, + server[id].name, acc, auth_dat[i].userid, new_email, ip); + // Save + mmo_auth_sync(); + } else + login_log("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command), but actual e-mail is incorrect (account: %d (%s), actual e-mail: %s, proposed e-mail: %s, ip: %s)." RETCODE, + server[id].name, acc, auth_dat[i].userid, auth_dat[i].email, actual_email, ip); + break; + } + } + if (i == auth_num) + login_log("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command), but account doesn't exist (account: %d, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + } + RFIFOSKIP(fd, 86); + break; + + // Receiving of map-server via char-server a status change resquest (by Yor) + case 0x2724: + if (RFIFOREST(fd) < 10) + return 0; + { + int acc, statut; + acc = RFIFOL(fd,2); + statut = RFIFOL(fd,6); + for(i = 0; i < auth_num; i++) { + if (auth_dat[i].account_id == acc) { + if (auth_dat[i].state != statut) { + login_log("Char-server '%s': Status change (account: %d, new status %d, ip: %s)." RETCODE, + server[id].name, acc, statut, ip); + if (statut != 0) { + unsigned char buf[16]; + WBUFW(buf,0) = 0x2731; + WBUFL(buf,2) = acc; + WBUFB(buf,6) = 0; // 0: change of statut, 1: ban + WBUFL(buf,7) = statut; // status or final date of a banishment + charif_sendallwos(-1, buf, 11); + for(j = 0; j < AUTH_FIFO_SIZE; j++) + if (auth_fifo[j].account_id == acc) + auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification) + } + auth_dat[i].state = statut; + // Save + mmo_auth_sync(); + } else + login_log("Char-server '%s': Error of Status change - actual status is already the good status (account: %d, status %d, ip: %s)." RETCODE, + server[id].name, acc, statut, ip); + break; + } + } + if (i == auth_num) { + login_log("Char-server '%s': Error of Status change (account: %d not found, suggested status %d, ip: %s)." RETCODE, + server[id].name, acc, statut, ip); + } + RFIFOSKIP(fd,10); + } + return 0; + + case 0x2725: // Receiving of map-server via char-server a ban resquest (by Yor) + if (RFIFOREST(fd) < 18) + return 0; + { + int acc; + acc = RFIFOL(fd,2); + for(i = 0; i < auth_num; i++) { + if (auth_dat[i].account_id == acc) { + time_t timestamp; + struct tm *tmtime; + if (auth_dat[i].ban_until_time == 0 || auth_dat[i].ban_until_time < time(NULL)) + timestamp = time(NULL); + else + timestamp = auth_dat[i].ban_until_time; + tmtime = localtime(×tamp); + tmtime->tm_year = tmtime->tm_year + (short)RFIFOW(fd,6); + tmtime->tm_mon = tmtime->tm_mon + (short)RFIFOW(fd,8); + tmtime->tm_mday = tmtime->tm_mday + (short)RFIFOW(fd,10); + tmtime->tm_hour = tmtime->tm_hour + (short)RFIFOW(fd,12); + tmtime->tm_min = tmtime->tm_min + (short)RFIFOW(fd,14); + tmtime->tm_sec = tmtime->tm_sec + (short)RFIFOW(fd,16); + timestamp = mktime(tmtime); + if (timestamp != -1) { + if (timestamp <= time(NULL)) + timestamp = 0; + if (auth_dat[i].ban_until_time != timestamp) { + if (timestamp != 0) { + unsigned char buf[16]; + char tmpstr[2048]; + strftime(tmpstr, 24, date_format, localtime(×tamp)); + login_log("Char-server '%s': Ban request (account: %d, new final date of banishment: %d (%s), ip: %s)." RETCODE, + server[id].name, acc, timestamp, (timestamp == 0 ? "no banishment" : tmpstr), ip); + WBUFW(buf,0) = 0x2731; + WBUFL(buf,2) = auth_dat[i].account_id; + WBUFB(buf,6) = 1; // 0: change of statut, 1: ban + WBUFL(buf,7) = timestamp; // status or final date of a banishment + charif_sendallwos(-1, buf, 11); + for(j = 0; j < AUTH_FIFO_SIZE; j++) + if (auth_fifo[j].account_id == acc) + auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification) + } else { + login_log("Char-server '%s': Error of ban request (account: %d, new date unbans the account, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + auth_dat[i].ban_until_time = timestamp; + // Save + mmo_auth_sync(); + } else { + login_log("Char-server '%s': Error of ban request (account: %d, no change for ban date, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + } else { + login_log("Char-server '%s': Error of ban request (account: %d, invalid date, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + break; + } + } + if (i == auth_num) { + login_log("Char-server '%s': Error of ban request (account: %d not found, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + RFIFOSKIP(fd,18); + } + return 0; + + case 0x2727: // Change of sex (sex is reversed) + if (RFIFOREST(fd) < 6) + return 0; + { + int acc, sex; + acc = RFIFOL(fd,2); + for(i = 0; i < auth_num; i++) { +// printf("%d,", auth_dat[i].account_id); + if (auth_dat[i].account_id == acc) { + if (auth_dat[i].sex == 2) + login_log("Char-server '%s': Error of sex change - Server account (suggested account: %d, actual sex %d (Server), ip: %s)." RETCODE, + server[id].name, acc, auth_dat[i].sex, ip); + else { + unsigned char buf[16]; + if (auth_dat[i].sex == 0) + sex = 1; + else + sex = 0; + login_log("Char-server '%s': Sex change (account: %d, new sex %c, ip: %s)." RETCODE, + server[id].name, acc, (sex == 2) ? 'S' : (sex ? 'M' : 'F'), ip); + for(j = 0; j < AUTH_FIFO_SIZE; j++) + if (auth_fifo[j].account_id == acc) + auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification) + auth_dat[i].sex = sex; + WBUFW(buf,0) = 0x2723; + WBUFL(buf,2) = acc; + WBUFB(buf,6) = sex; + charif_sendallwos(-1, buf, 7); + // Save + mmo_auth_sync(); + } + break; + } + } + if (i == auth_num) { + login_log("Char-server '%s': Error of sex change (account: %d not found, sex would be reversed, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + RFIFOSKIP(fd,6); + } + return 0; + + case 0x2728: // We receive account_reg2 from a char-server, and we send them to other char-servers. + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + { + int acc, p; + acc = RFIFOL(fd,4); + for(i = 0; i < auth_num; i++) { + if (auth_dat[i].account_id == acc) { + unsigned char buf[RFIFOW(fd,2)+1]; + login_log("Char-server '%s': receiving (from the char-server) of account_reg2 (account: %d, ip: %s)." RETCODE, + server[id].name, acc, ip); + for(p = 8, j = 0; p < RFIFOW(fd,2) && j < ACCOUNT_REG2_NUM; p += 36, j++) { + memcpy(auth_dat[i].account_reg2[j].str, RFIFOP(fd,p), 32); + auth_dat[i].account_reg2[j].str[31] = '\0'; + remove_control_chars(auth_dat[i].account_reg2[j].str); + auth_dat[i].account_reg2[j].value = RFIFOL(fd,p+32); + } + auth_dat[i].account_reg2_num = j; + // Sending information towards the other char-servers. + memcpy(WBUFP(buf,0), RFIFOP(fd,0), RFIFOW(fd,2)); + WBUFW(buf,0) = 0x2729; + charif_sendallwos(fd, buf, WBUFW(buf,2)); + // Save + mmo_auth_sync(); +// printf("parse_fromchar: receiving (from the char-server) of account_reg2 (account id: %d).\n", acc); + break; + } + } + if (i == auth_num) { +// printf("parse_fromchar: receiving (from the char-server) of account_reg2 (unknwon account id: %d).\n", acc); + login_log("Char-server '%s': receiving (from the char-server) of account_reg2 (account: %d not found, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + } + RFIFOSKIP(fd,RFIFOW(fd,2)); + break; + + case 0x272a: // Receiving of map-server via char-server a unban resquest (by Yor) + if (RFIFOREST(fd) < 6) + return 0; + { + int acc; + acc = RFIFOL(fd,2); + for(i = 0; i < auth_num; i++) { + if (auth_dat[i].account_id == acc) { + if (auth_dat[i].ban_until_time != 0) { + auth_dat[i].ban_until_time = 0; + login_log("Char-server '%s': UnBan request (account: %d, ip: %s)." RETCODE, + server[id].name, acc, ip); + } else { + login_log("Char-server '%s': Error of UnBan request (account: %d, no change for unban date, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + break; + } + } + if (i == auth_num) { + login_log("Char-server '%s': Error of UnBan request (account: %d not found, ip: %s)." RETCODE, + server[id].name, acc, ip); + } + RFIFOSKIP(fd,6); + } + return 0; + + default: + { + FILE *logfp; + char tmpstr[24]; + struct timeval tv; + logfp = fopen(login_log_unknown_packets_filename, "a"); + if (logfp) { + gettimeofday(&tv, NULL); + strftime(tmpstr, 23, date_format, localtime(&(tv.tv_sec))); + fprintf(logfp, "%s.%03d: receiving of an unknown packet -> disconnection" RETCODE, tmpstr, (int)tv.tv_usec / 1000); + fprintf(logfp, "parse_fromchar: connection #%d (ip: %s), packet: 0x%x (with being read: %d)." RETCODE, fd, ip, RFIFOW(fd,0), RFIFOREST(fd)); + fprintf(logfp, "Detail (in hex):" RETCODE); + fprintf(logfp, "---- 00-01-02-03-04-05-06-07 08-09-0A-0B-0C-0D-0E-0F" RETCODE); + memset(tmpstr, '\0', sizeof(tmpstr)); + for(i = 0; i < RFIFOREST(fd); i++) { + if ((i & 15) == 0) + fprintf(logfp, "%04X ",i); + fprintf(logfp, "%02x ", RFIFOB(fd,i)); + if (RFIFOB(fd,i) > 0x1f) + tmpstr[i % 16] = RFIFOB(fd,i); + else + tmpstr[i % 16] = '.'; + if ((i - 7) % 16 == 0) // -8 + 1 + fprintf(logfp, " "); + else if ((i + 1) % 16 == 0) { + fprintf(logfp, " %s" RETCODE, tmpstr); + memset(tmpstr, '\0', sizeof(tmpstr)); + } + } + if (i % 16 != 0) { + for(j = i; j % 16 != 0; j++) { + fprintf(logfp, " "); + if ((j - 7) % 16 == 0) // -8 + 1 + fprintf(logfp, " "); + } + fprintf(logfp, " %s" RETCODE, tmpstr); + } + fprintf(logfp, RETCODE); + fclose(logfp); + } + } + printf("parse_fromchar: Unknown packet 0x%x (from a char-server)! -> disconnection.\n", RFIFOW(fd,0)); + session[fd]->eof = 1; + printf("Char-server has been disconnected (unknown packet).\n"); + return 0; + } + } + return 0; +} + +//--------------------------------------- +// Packet parsing for administation login +//--------------------------------------- +int parse_admin(int fd) { + int i, j; + unsigned char *p = (unsigned char *) &session[fd]->client_addr.sin_addr; + char* account_name; + char ip[16]; + + sprintf(ip, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); + + if (session[fd]->eof) { + close(fd); + delete_session(fd); + printf("Remote administration has disconnected (session #%d).\n", fd); + return 0; + } + + while(RFIFOREST(fd) >= 2) { + if (display_parse_admin == 1) + printf("parse_admin: connection #%d, packet: 0x%x (with being read: %d).\n", fd, RFIFOW(fd,0), RFIFOREST(fd)); + + switch(RFIFOW(fd,0)) { + case 0x7530: // Request of the server version + login_log("'ladmin': Sending of the server version (ip: %s)" RETCODE, ip); + WFIFOW(fd,0) = 0x7531; + WFIFOB(fd,2) = ATHENA_MAJOR_VERSION; + WFIFOB(fd,3) = ATHENA_MINOR_VERSION; + WFIFOB(fd,4) = ATHENA_REVISION; + WFIFOB(fd,5) = ATHENA_RELEASE_FLAG; + WFIFOB(fd,6) = ATHENA_OFFICIAL_FLAG; + WFIFOB(fd,7) = ATHENA_SERVER_LOGIN; + WFIFOW(fd,8) = ATHENA_MOD_VERSION; + WFIFOSET(fd,10); + RFIFOSKIP(fd,2); + break; + + case 0x7532: // Request of end of connection + login_log("'ladmin': End of connection (ip: %s)" RETCODE, ip); + RFIFOSKIP(fd,2); + session[fd]->eof = 1; + break; + + case 0x7920: // Request of an accounts list + if (RFIFOREST(fd) < 10) + return 0; + { + int st, ed, len; + int id[auth_num]; + st = RFIFOL(fd,2); + ed = RFIFOL(fd,6); + RFIFOSKIP(fd,10); + WFIFOW(fd,0) = 0x7921; + if (st < 0) + st = 0; + if (ed > END_ACCOUNT_NUM || ed < st || ed <= 0) + ed = END_ACCOUNT_NUM; + login_log("'ladmin': Sending an accounts list (ask: from %d to %d, ip: %s)" RETCODE, st, ed, ip); + // Sort before send + for(i = 0; i < auth_num; i++) { + int k; + id[i] = i; + for(j = 0; j < i; j++) { + if (auth_dat[id[i]].account_id < auth_dat[id[j]].account_id) { + for(k = i; k > j; k--) { + id[k] = id[k-1]; + } + id[j] = i; // id[i] + break; + } + } + } + // Sending accounts information + len = 4; + for(i = 0; i < auth_num && len < 30000; i++) { + int account_id = auth_dat[id[i]].account_id; // use sorted index + if (account_id >= st && account_id <= ed) { + j = id[i]; + WFIFOL(fd,len) = account_id; + WFIFOB(fd,len+4) = (unsigned char)isGM(account_id); + memcpy(WFIFOP(fd,len+5), auth_dat[j].userid, 24); + WFIFOB(fd,len+29) = auth_dat[j].sex; + WFIFOL(fd,len+30) = auth_dat[j].logincount; + if (auth_dat[j].state == 0 && auth_dat[j].ban_until_time != 0) // if no state and banished + WFIFOL(fd,len+34) = 7; // 6 = Your are Prohibited to log in until %s + else + WFIFOL(fd,len+34) = auth_dat[j].state; + len += 38; + } + } + WFIFOW(fd,2) = len; + WFIFOSET(fd,len); + } + break; + + case 0x7930: // Request for an account creation + if (RFIFOREST(fd) < 91) + return 0; + { + struct mmo_account ma; + ma.userid = RFIFOP(fd, 2); + ma.passwd = RFIFOP(fd, 26); + memcpy(ma.lastlogin, "-", 2); + ma.sex = RFIFOB(fd,50); + WFIFOW(fd,0) = 0x7931; + WFIFOL(fd,2) = -1; + memcpy(WFIFOP(fd,6), RFIFOP(fd,2), 24); + if (strlen(ma.userid) > 23 || strlen(ma.passwd) > 23) { + login_log("'ladmin': Attempt to create an invalid account (account or pass is too long, ip: %s)" RETCODE, + ip); + } else if (strlen(ma.userid) < 4 || strlen(ma.passwd) < 4) { + login_log("'ladmin': Attempt to create an invalid account (account or pass is too short, ip: %s)" RETCODE, + ip); + } else if (ma.sex != 'F' && ma.sex != 'M') { + login_log("'ladmin': Attempt to create an invalid account (account: %s, received pass: %s, invalid sex, ip: %s)" RETCODE, + ma.userid, ma.passwd, ip); + } else if (account_id_count > END_ACCOUNT_NUM) { + login_log("'ladmin': Attempt to create an account, but there is no more available id number (account: %s, pass: %s, sex: %c, ip: %s)" RETCODE, + ma.userid, ma.passwd, ma.sex, ip); + } else { + remove_control_chars(ma.userid); + remove_control_chars(ma.passwd); + for(i = 0; i < auth_num; i++) { + if (strncmp(auth_dat[i].userid, ma.userid, 24) == 0) { + login_log("'ladmin': Attempt to create an already existing account (account: %s, pass: %s, received pass: %s, ip: %s)" RETCODE, + auth_dat[i].userid, auth_dat[i].pass, ma.passwd, ip); + break; + } + } + if (i == auth_num) { + int new_id; + char email[40]; + memcpy(email, RFIFOP(fd,51), 40); + email[39] = '\0'; + remove_control_chars(email); + new_id = mmo_auth_new(&ma, ma.sex, email); + login_log("'ladmin': Account creation (account: %s (id: %d), pass: %s, sex: %c, email: %s, ip: %s)" RETCODE, + ma.userid, new_id, ma.passwd, ma.sex, auth_dat[i].email, ip); + WFIFOL(fd,2) = new_id; + mmo_auth_sync(); + } + } + WFIFOSET(fd,30); + RFIFOSKIP(fd,91); + } + break; + + case 0x7932: // Request for an account deletion + if (RFIFOREST(fd) < 26) + return 0; + WFIFOW(fd,0) = 0x7933; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + i = search_account_index(account_name); + if (i != -1) { + // Char-server is notified of deletion (for characters deletion). + unsigned char buf[65535]; + WBUFW(buf,0) = 0x2730; + WBUFL(buf,2) = auth_dat[i].account_id; + charif_sendallwos(-1, buf, 6); + // send answer + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + WFIFOL(fd,2) = auth_dat[i].account_id; + // save deleted account in log file + login_log("'ladmin': Account deletion (account: %s, id: %d, ip: %s) - saved in next line:" RETCODE, + auth_dat[i].userid, auth_dat[i].account_id, ip); + mmo_auth_tostr(buf, &auth_dat[i]); + login_log("%s" RETCODE, buf); + // delete account + memset(auth_dat[i].userid, '\0', sizeof(auth_dat[i].userid)); + auth_dat[i].account_id = -1; + mmo_auth_sync(); + } else { + memcpy(WFIFOP(fd,6), account_name, 24); + login_log("'ladmin': Attempt to delete an unknown account (account: %s, ip: %s)" RETCODE, + account_name, ip); + } + WFIFOSET(fd,30); + RFIFOSKIP(fd,26); + break; + + case 0x7934: // Request to change a password + if (RFIFOREST(fd) < 50) + return 0; + WFIFOW(fd,0) = 0x7935; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + i = search_account_index(account_name); + if (i != -1) { + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + memcpy(auth_dat[i].pass, RFIFOP(fd,26), 24); + auth_dat[i].pass[23] = '\0'; + remove_control_chars(auth_dat[i].pass); + WFIFOL(fd,2) = auth_dat[i].account_id; + login_log("'ladmin': Modification of a password (account: %s, new password: %s, ip: %s)" RETCODE, + auth_dat[i].userid, auth_dat[i].pass, ip); + mmo_auth_sync(); + } else { + memcpy(WFIFOP(fd,6), account_name, 24); + login_log("'ladmin': Attempt to modify the password of an unknown account (account: %s, ip: %s)" RETCODE, + account_name, ip); + } + WFIFOSET(fd,30); + RFIFOSKIP(fd,50); + break; + + case 0x7936: // Request to modify a state + if (RFIFOREST(fd) < 50) + return 0; + { + char error_message[20]; + int statut; + WFIFOW(fd,0) = 0x7937; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + statut = RFIFOL(fd,26); + memcpy(error_message, RFIFOP(fd,30), 20); + error_message[19] = '\0'; + remove_control_chars(error_message); + if (statut != 7 || error_message[0] == '\0') { // 7: // 6 = Your are Prohibited to log in until %s + strcpy(error_message, "-"); + } + i = search_account_index(account_name); + if (i != -1) { + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + WFIFOL(fd,2) = auth_dat[i].account_id; + if (auth_dat[i].state == statut && strcmp(auth_dat[i].error_message, error_message) == 0) + login_log("'ladmin': Modification of a state, but the state of the account is already the good state (account: %s, received state: %d, ip: %s)" RETCODE, + account_name, statut, ip); + else { + if (statut == 7) + login_log("'ladmin': Modification of a state (account: %s, new state: %d - prohibited to login until '%s', ip: %s)" RETCODE, + auth_dat[i].userid, statut, error_message, ip); + else + login_log("'ladmin': Modification of a state (account: %s, new state: %d, ip: %s)" RETCODE, + auth_dat[i].userid, statut, ip); + if (auth_dat[i].state == 0) { + unsigned char buf[16]; + WBUFW(buf,0) = 0x2731; + WBUFL(buf,2) = auth_dat[i].account_id; + WBUFB(buf,6) = 0; // 0: change of statut, 1: ban + WBUFL(buf,7) = statut; // status or final date of a banishment + charif_sendallwos(-1, buf, 11); + for(j = 0; j < AUTH_FIFO_SIZE; j++) + if (auth_fifo[j].account_id == auth_dat[i].account_id) + auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification) + } + auth_dat[i].state = statut; + memcpy(auth_dat[i].error_message, error_message, 20); + mmo_auth_sync(); + } + } else { + memcpy(WFIFOP(fd,6), account_name, 24); + login_log("'ladmin': Attempt to modify the state of an unknown account (account: %s, received state: %d, ip: %s)" RETCODE, + account_name, statut, ip); + } + WFIFOL(fd,30) = statut; + } + WFIFOSET(fd,34); + RFIFOSKIP(fd,50); + break; + + case 0x7938: // Request for servers list and # of online players + login_log("'ladmin': Sending of servers list (ip: %s)" RETCODE, ip); + server_num = 0; + for(i = 0; i < MAX_SERVERS; i++) { + if (server_fd[i] >= 0) { + WFIFOL(fd,4+server_num*32) = server[i].ip; + WFIFOW(fd,4+server_num*32+4) = server[i].port; + memcpy(WFIFOP(fd,4+server_num*32+6), server[i].name, 20); + WFIFOW(fd,4+server_num*32+26) = server[i].users; + WFIFOW(fd,4+server_num*32+28) = server[i].maintenance; + WFIFOW(fd,4+server_num*32+30) = server[i].new; + server_num++; + } + } + WFIFOW(fd,0) = 0x7939; + WFIFOW(fd,2) = 4 + 32 * server_num; + WFIFOSET(fd,4+32*server_num); + RFIFOSKIP(fd,2); + break; + + case 0x793a: // Request to password check + if (RFIFOREST(fd) < 50) + return 0; + WFIFOW(fd,0) = 0x793b; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + i = search_account_index(account_name); + if (i != -1) { + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + if (strcmp(auth_dat[i].pass, RFIFOP(fd,26)) == 0) { + WFIFOL(fd,2) = auth_dat[i].account_id; + login_log("'ladmin': Check of password OK (account: %s, password: %s, ip: %s)" RETCODE, + auth_dat[i].userid, auth_dat[i].pass, ip); + } else { + char pass[24]; + memcpy(pass, RFIFOP(fd,26), 24); + pass[23] = '\0'; + remove_control_chars(pass); + login_log("'ladmin': Failure of password check (account: %s, proposed pass: %s, ip: %s)" RETCODE, + auth_dat[i].userid, pass, ip); + } + } else { + memcpy(WFIFOP(fd,6), account_name, 24); + login_log("'ladmin': Attempt to check the password of an unknown account (account: %s, ip: %s)" RETCODE, + account_name, ip); + } + WFIFOSET(fd,30); + RFIFOSKIP(fd,50); + break; + + case 0x793c: // Request to modify sex + if (RFIFOREST(fd) < 27) + return 0; + WFIFOW(fd,0) = 0x793d; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + memcpy(WFIFOP(fd,6), account_name, 24); + { + char sex; + sex = RFIFOB(fd,26); + if (sex != 'F' && sex != 'M') { + if (sex > 31) + login_log("'ladmin': Attempt to give an invalid sex (account: %s, received sex: %c, ip: %s)" RETCODE, + account_name, sex, ip); + else + login_log("'ladmin': Attempt to give an invalid sex (account: %s, received sex: 'control char', ip: %s)" RETCODE, + account_name, ip); + } else { + i = search_account_index(account_name); + if (i != -1) { + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + if (auth_dat[i].sex != ((sex == 'S' || sex == 's') ? 2 : (sex == 'M' || sex == 'm'))) { + unsigned char buf[16]; + WFIFOL(fd,2) = auth_dat[i].account_id; + for(j = 0; j < AUTH_FIFO_SIZE; j++) + if (auth_fifo[j].account_id == auth_dat[i].account_id) + auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification) + auth_dat[i].sex = (sex == 'S' || sex == 's') ? 2 : (sex == 'M' || sex == 'm'); + login_log("'ladmin': Modification of a sex (account: %s, new sex: %c, ip: %s)" RETCODE, + auth_dat[i].userid, sex, ip); + mmo_auth_sync(); + // send to all char-server the change + WBUFW(buf,0) = 0x2723; + WBUFL(buf,2) = auth_dat[i].account_id; + WBUFB(buf,6) = auth_dat[i].sex; + charif_sendallwos(-1, buf, 7); + } else { + login_log("'ladmin': Modification of a sex, but the sex is already the good sex (account: %s, sex: %c, ip: %s)" RETCODE, + auth_dat[i].userid, sex, ip); + } + } else { + login_log("'ladmin': Attempt to modify the sex of an unknown account (account: %s, received sex: %c, ip: %s)" RETCODE, + account_name, sex, ip); + } + } + } + WFIFOSET(fd,30); + RFIFOSKIP(fd,27); + break; + + case 0x793e: // Request to modify GM level + if (RFIFOREST(fd) < 27) + return 0; + WFIFOW(fd,0) = 0x793f; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + memcpy(WFIFOP(fd,6), account_name, 24); + { + char new_gm_level; + new_gm_level = RFIFOB(fd,26); + if (new_gm_level < 0 || new_gm_level > 99) { + login_log("'ladmin': Attempt to give an invalid GM level (account: %s, received GM level: %d, ip: %s)" RETCODE, + account_name, (int)new_gm_level, ip); + } else { + i = search_account_index(account_name); + if (i != -1) { + int acc = auth_dat[i].account_id; + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + if (isGM(acc) != new_gm_level) { + // modification of the file + FILE *fp, *fp2; + int lock; + char line[512]; + int GM_account, GM_level; + int modify_flag; + char tmpstr[24]; + struct timeval tv; + if ((fp2 = lock_fopen(GM_account_filename, &lock)) != NULL) { + if ((fp = fopen(GM_account_filename, "r")) != NULL) { + gettimeofday(&tv, NULL); + strftime(tmpstr, 23, date_format, localtime(&(tv.tv_sec))); + modify_flag = 0; + // read/write GM file + while(fgets(line, sizeof(line)-1, fp)) { + while(line[0] != '\0' && (line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r')) + line[strlen(line)-1] = '\0'; + if ((line[0] == '/' && line[1] == '/') || line[0] == '\0') + fprintf(fp2, "%s" RETCODE, line); + else { + if (sscanf(line, "%d %d", &GM_account, &GM_level) != 2 && sscanf(line, "%d: %d", &GM_account, &GM_level) != 2) + fprintf(fp2, "%s" RETCODE, line); + else if (GM_account != acc) + fprintf(fp2, "%s" RETCODE, line); + else if (new_gm_level < 1) { + fprintf(fp2, "// %s: 'ladmin' GM level removed on account %d '%s' (previous level: %d)" RETCODE "//%d %d" RETCODE, tmpstr, acc, auth_dat[i].userid, GM_level, acc, new_gm_level); + modify_flag = 1; + } else { + fprintf(fp2, "// %s: 'ladmin' GM level on account %d '%s' (previous level: %d)" RETCODE "%d %d" RETCODE, tmpstr, acc, auth_dat[i].userid, GM_level, acc, new_gm_level); + modify_flag = 1; + } + } + } + if (modify_flag == 0) + fprintf(fp2, "// %s: 'ladmin' GM level on account %d '%s' (previous level: 0)" RETCODE "%d %d" RETCODE, tmpstr, acc, auth_dat[i].userid, acc, new_gm_level); + fclose(fp); + } else { + login_log("'ladmin': Attempt to modify of a GM level - impossible to read GM accounts file (account: %s (%d), received GM level: %d, ip: %s)" RETCODE, + auth_dat[i].userid, acc, (int)new_gm_level, ip); + } + if (lock_fclose(fp2, GM_account_filename, &lock) == 0) { + WFIFOL(fd,2) = acc; + login_log("'ladmin': Modification of a GM level (account: %s (%d), new GM level: %d, ip: %s)" RETCODE, + auth_dat[i].userid, acc, (int)new_gm_level, ip); + // read and send new GM informations + read_gm_account(); + send_GM_accounts(); + } else { + login_log("'ladmin': Attempt to modify of a GM level - impossible to write GM accounts file (account: %s (%d), received GM level: %d, ip: %s)" RETCODE, + auth_dat[i].userid, acc, (int)new_gm_level, ip); + } + } else { + login_log("'ladmin': Attempt to modify of a GM level - impossible to write GM accounts file (account: %s (%d), received GM level: %d, ip: %s)" RETCODE, + auth_dat[i].userid, acc, (int)new_gm_level, ip); + } + } else { + login_log("'ladmin': Attempt to modify of a GM level, but the GM level is already the good GM level (account: %s (%d), GM level: %d, ip: %s)" RETCODE, + auth_dat[i].userid, acc, (int)new_gm_level, ip); + } + } else { + login_log("'ladmin': Attempt to modify the GM level of an unknown account (account: %s, received GM level: %d, ip: %s)" RETCODE, + account_name, (int)new_gm_level, ip); + } + } + } + WFIFOSET(fd,30); + RFIFOSKIP(fd,27); + break; + + case 0x7940: // Request to modify e-mail + if (RFIFOREST(fd) < 66) + return 0; + WFIFOW(fd,0) = 0x7941; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + memcpy(WFIFOP(fd,6), account_name, 24); + { + char email[40]; + memcpy(email, RFIFOP(fd,26), 40); + if (e_mail_check(email) == 0) { + login_log("'ladmin': Attempt to give an invalid e-mail (account: %s, ip: %s)" RETCODE, + account_name, ip); + } else { + remove_control_chars(email); + i = search_account_index(account_name); + if (i != -1) { + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + memcpy(auth_dat[i].email, email, 40); + WFIFOL(fd,2) = auth_dat[i].account_id; + login_log("'ladmin': Modification of an email (account: %s, new e-mail: %s, ip: %s)" RETCODE, + auth_dat[i].userid, email, ip); + mmo_auth_sync(); + } else { + login_log("'ladmin': Attempt to modify the e-mail of an unknown account (account: %s, received e-mail: %s, ip: %s)" RETCODE, + account_name, email, ip); + } + } + } + WFIFOSET(fd,30); + RFIFOSKIP(fd,66); + break; + + case 0x7942: // Request to modify memo field + if (RFIFOREST(fd) < 28 || RFIFOREST(fd) < (28 + RFIFOW(fd,26))) + return 0; + WFIFOW(fd,0) = 0x7943; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + i = search_account_index(account_name); + if (i != -1) { + int size_of_memo = sizeof(auth_dat[i].memo); + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + memset(auth_dat[i].memo, '\0', size_of_memo); + if (RFIFOW(fd,26) == 0) { + strncpy(auth_dat[i].memo, "-", size_of_memo); + } else if (RFIFOW(fd,26) > size_of_memo - 1) { + memcpy(auth_dat[i].memo, RFIFOP(fd,28), size_of_memo - 1); + } else { + memcpy(auth_dat[i].memo, RFIFOP(fd,28), RFIFOW(fd,26)); + } + auth_dat[i].memo[size_of_memo - 1] = '\0'; + remove_control_chars(auth_dat[i].memo); + WFIFOL(fd,2) = auth_dat[i].account_id; + login_log("'ladmin': Modification of a memo field (account: %s, new memo: %s, ip: %s)" RETCODE, + auth_dat[i].userid, auth_dat[i].memo, ip); + mmo_auth_sync(); + } else { + memcpy(WFIFOP(fd,6), account_name, 24); + login_log("'ladmin': Attempt to modify the memo field of an unknown account (account: %s, ip: %s)" RETCODE, + account_name, ip); + } + WFIFOSET(fd,30); + RFIFOSKIP(fd,28 + RFIFOW(fd,26)); + break; + + case 0x7944: // Request to found an account id + if (RFIFOREST(fd) < 26) + return 0; + WFIFOW(fd,0) = 0x7945; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + i = search_account_index(account_name); + if (i != -1) { + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + WFIFOL(fd,2) = auth_dat[i].account_id; + login_log("'ladmin': Request (by the name) of an account id (account: %s, id: %d, ip: %s)" RETCODE, + auth_dat[i].userid, auth_dat[i].account_id, ip); + } else { + memcpy(WFIFOP(fd,6), account_name, 24); + login_log("'ladmin': ID request (by the name) of an unknown account (account: %s, ip: %s)" RETCODE, + account_name, ip); + } + WFIFOSET(fd,30); + RFIFOSKIP(fd,26); + break; + + case 0x7946: // Request to found an account name + if (RFIFOREST(fd) < 6) + return 0; + WFIFOW(fd,0) = 0x7947; + WFIFOL(fd,2) = RFIFOL(fd,2); + memset(WFIFOP(fd,6), '\0', 24); + for(i = 0; i < auth_num; i++) { + if (auth_dat[i].account_id == RFIFOL(fd,2)) { + strncpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + login_log("'ladmin': Request (by id) of an account name (account: %s, id: %d, ip: %s)" RETCODE, + auth_dat[i].userid, RFIFOL(fd,2), ip); + break; + } + } + if (i == auth_num) { + login_log("'ladmin': Name request (by id) of an unknown account (id: %d, ip: %s)" RETCODE, + RFIFOL(fd,2), ip); + strncpy(WFIFOP(fd,6), "", 24); + } + WFIFOSET(fd,30); + RFIFOSKIP(fd,6); + break; + + case 0x7948: // Request to change the validity limit (timestamp) (absolute value) + if (RFIFOREST(fd) < 30) + return 0; + { + time_t timestamp; + char tmpstr[2048]; + WFIFOW(fd,0) = 0x7949; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + timestamp = (time_t)RFIFOL(fd,26); + strftime(tmpstr, 24, date_format, localtime(×tamp)); + i = search_account_index(account_name); + if (i != -1) { + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + login_log("'ladmin': Change of a validity limit (account: %s, new validity: %d (%s), ip: %s)" RETCODE, + auth_dat[i].userid, timestamp, (timestamp == 0 ? "unlimited" : tmpstr), ip); + auth_dat[i].connect_until_time = timestamp; + WFIFOL(fd,2) = auth_dat[i].account_id; + mmo_auth_sync(); + } else { + memcpy(WFIFOP(fd,6), account_name, 24); + login_log("'ladmin': Attempt to change the validity limit of an unknown account (account: %s, received validity: %d (%s), ip: %s)" RETCODE, + account_name, timestamp, (timestamp == 0 ? "unlimited" : tmpstr), ip); + } + WFIFOL(fd,30) = timestamp; + } + WFIFOSET(fd,34); + RFIFOSKIP(fd,30); + break; + + case 0x794a: // Request to change the final date of a banishment (timestamp) (absolute value) + if (RFIFOREST(fd) < 30) + return 0; + { + time_t timestamp; + char tmpstr[2048]; + WFIFOW(fd,0) = 0x794b; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + timestamp = (time_t)RFIFOL(fd,26); + if (timestamp <= time(NULL)) + timestamp = 0; + strftime(tmpstr, 24, date_format, localtime(×tamp)); + i = search_account_index(account_name); + if (i != -1) { + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + WFIFOL(fd,2) = auth_dat[i].account_id; + login_log("'ladmin': Change of the final date of a banishment (account: %s, new final date of banishment: %d (%s), ip: %s)" RETCODE, + auth_dat[i].userid, timestamp, (timestamp == 0 ? "no banishment" : tmpstr), ip); + if (auth_dat[i].ban_until_time != timestamp) { + if (timestamp != 0) { + unsigned char buf[16]; + WBUFW(buf,0) = 0x2731; + WBUFL(buf,2) = auth_dat[i].account_id; + WBUFB(buf,6) = 1; // 0: change of statut, 1: ban + WBUFL(buf,7) = timestamp; // status or final date of a banishment + charif_sendallwos(-1, buf, 11); + for(j = 0; j < AUTH_FIFO_SIZE; j++) + if (auth_fifo[j].account_id == auth_dat[i].account_id) + auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification) + } + auth_dat[i].ban_until_time = timestamp; + mmo_auth_sync(); + } + } else { + memcpy(WFIFOP(fd,6), account_name, 24); + login_log("'ladmin': Attempt to change the final date of a banishment of an unknown account (account: %s, received final date of banishment: %d (%s), ip: %s)" RETCODE, + account_name, timestamp, (timestamp == 0 ? "no banishment" : tmpstr), ip); + } + WFIFOL(fd,30) = timestamp; + } + WFIFOSET(fd,34); + RFIFOSKIP(fd,30); + break; + + case 0x794c: // Request to change the final date of a banishment (timestamp) (relative change) + if (RFIFOREST(fd) < 38) + return 0; + { + time_t timestamp; + struct tm *tmtime; + char tmpstr[2048]; + WFIFOW(fd,0) = 0x794d; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + i = search_account_index(account_name); + if (i != -1) { + WFIFOL(fd,2) = auth_dat[i].account_id; + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + if (auth_dat[i].ban_until_time == 0 || auth_dat[i].ban_until_time < time(NULL)) + timestamp = time(NULL); + else + timestamp = auth_dat[i].ban_until_time; + tmtime = localtime(×tamp); + tmtime->tm_year = tmtime->tm_year + (short)RFIFOW(fd,26); + tmtime->tm_mon = tmtime->tm_mon + (short)RFIFOW(fd,28); + tmtime->tm_mday = tmtime->tm_mday + (short)RFIFOW(fd,30); + tmtime->tm_hour = tmtime->tm_hour + (short)RFIFOW(fd,32); + tmtime->tm_min = tmtime->tm_min + (short)RFIFOW(fd,34); + tmtime->tm_sec = tmtime->tm_sec + (short)RFIFOW(fd,36); + timestamp = mktime(tmtime); + if (timestamp != -1) { + if (timestamp <= time(NULL)) + timestamp = 0; + strftime(tmpstr, 24, date_format, localtime(×tamp)); + login_log("'ladmin': Adjustment of a final date of a banishment (account: %s, (%+d y %+d m %+d d %+d h %+d mn %+d s) -> new validity: %d (%s), ip: %s)" RETCODE, + auth_dat[i].userid, (short)RFIFOW(fd,26), (short)RFIFOW(fd,28), (short)RFIFOW(fd,30), (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), timestamp, (timestamp == 0 ? "no banishment" : tmpstr), ip); + if (auth_dat[i].ban_until_time != timestamp) { + if (timestamp != 0) { + unsigned char buf[16]; + WBUFW(buf,0) = 0x2731; + WBUFL(buf,2) = auth_dat[i].account_id; + WBUFB(buf,6) = 1; // 0: change of statut, 1: ban + WBUFL(buf,7) = timestamp; // status or final date of a banishment + charif_sendallwos(-1, buf, 11); + for(j = 0; j < AUTH_FIFO_SIZE; j++) + if (auth_fifo[j].account_id == auth_dat[i].account_id) + auth_fifo[j].login_id1++; // to avoid reconnection error when come back from map-server (char-server will ask again the authentification) + } + auth_dat[i].ban_until_time = timestamp; + mmo_auth_sync(); + } + } else { + strftime(tmpstr, 24, date_format, localtime(&auth_dat[i].ban_until_time)); + login_log("'ladmin': Impossible to adjust the final date of a banishment (account: %s, %d (%s) + (%+d y %+d m %+d d %+d h %+d mn %+d s) -> ???, ip: %s)" RETCODE, + auth_dat[i].userid, auth_dat[i].ban_until_time, (auth_dat[i].ban_until_time == 0 ? "no banishment" : tmpstr), (short)RFIFOW(fd,26), (short)RFIFOW(fd,28), (short)RFIFOW(fd,30), (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), ip); + } + WFIFOL(fd,30) = (unsigned long)auth_dat[i].ban_until_time; + } else { + memcpy(WFIFOP(fd,6), account_name, 24); + login_log("'ladmin': Attempt to adjust the final date of a banishment of an unknown account (account: %s, ip: %s)" RETCODE, + account_name, ip); + WFIFOL(fd,30) = 0; + } + } + WFIFOSET(fd,34); + RFIFOSKIP(fd,38); + break; + + case 0x794e: // Request to send a broadcast message + if (RFIFOREST(fd) < 8 || RFIFOREST(fd) < (8 + RFIFOL(fd,4))) + return 0; + WFIFOW(fd,0) = 0x794f; + WFIFOW(fd,2) = -1; + if (RFIFOL(fd,4) < 1) { + login_log("'ladmin': Receiving a message for broadcast, but message is void (ip: %s)" RETCODE, + ip); + } else { + // at least 1 char-server + for(i = 0; i < MAX_SERVERS; i++) + if (server_fd[i] >= 0) + break; + if (i == MAX_SERVERS) { + login_log("'ladmin': Receiving a message for broadcast, but no char-server is online (ip: %s)" RETCODE, + ip); + } else { + char buf[32000]; + char message[32000]; + WFIFOW(fd,2) = 0; + memset(message, '\0', sizeof(message)); + memcpy(message, RFIFOP(fd,8), RFIFOL(fd,4)); + message[sizeof(message)-1] = '\0'; + remove_control_chars(message); + if (RFIFOW(fd,2) == 0) + login_log("'ladmin': Receiving a message for broadcast (message (in yellow): %s, ip: %s)" RETCODE, + message, ip); + else + login_log("'ladmin': Receiving a message for broadcast (message (in blue): %s, ip: %s)" RETCODE, + message, ip); + // send same message to all char-servers (no answer) + memcpy(WBUFP(buf,0), RFIFOP(fd,0), 8 + RFIFOL(fd,4)); + WBUFW(buf,0) = 0x2726; + charif_sendallwos(-1, buf, 8 + RFIFOL(fd,4)); + } + } + WFIFOSET(fd,4); + RFIFOSKIP(fd,8 + RFIFOL(fd,4)); + break; + + case 0x7950: // Request to change the validity limite (timestamp) (relative change) + if (RFIFOREST(fd) < 38) + return 0; + { + time_t timestamp; + struct tm *tmtime; + char tmpstr[2048]; + char tmpstr2[2048]; + WFIFOW(fd,0) = 0x7951; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + i = search_account_index(account_name); + if (i != -1) { + WFIFOL(fd,2) = auth_dat[i].account_id; + memcpy(WFIFOP(fd,6), auth_dat[i].userid, 24); + timestamp = auth_dat[i].connect_until_time; + if (add_to_unlimited_account == 0 && timestamp == 0) { + login_log("'ladmin': Attempt to adjust the validity limit of an unlimited account (account: %s, ip: %s)" RETCODE, + auth_dat[i].userid, ip); + WFIFOL(fd,30) = 0; + } else { + if (timestamp == 0 || timestamp < time(NULL)) + timestamp = time(NULL); + tmtime = localtime(×tamp); + tmtime->tm_year = tmtime->tm_year + (short)RFIFOW(fd,26); + tmtime->tm_mon = tmtime->tm_mon + (short)RFIFOW(fd,28); + tmtime->tm_mday = tmtime->tm_mday + (short)RFIFOW(fd,30); + tmtime->tm_hour = tmtime->tm_hour + (short)RFIFOW(fd,32); + tmtime->tm_min = tmtime->tm_min + (short)RFIFOW(fd,34); + tmtime->tm_sec = tmtime->tm_sec + (short)RFIFOW(fd,36); + timestamp = mktime(tmtime); + if (timestamp != -1) { + strftime(tmpstr, 24, date_format, localtime(&auth_dat[i].connect_until_time)); + strftime(tmpstr2, 24, date_format, localtime(×tamp)); + login_log("'ladmin': Adjustment of a validity limit (account: %s, %d (%s) + (%+d y %+d m %+d d %+d h %+d mn %+d s) -> new validity: %d (%s), ip: %s)" RETCODE, + auth_dat[i].userid, auth_dat[i].connect_until_time, (auth_dat[i].connect_until_time == 0 ? "unlimited" : tmpstr), (short)RFIFOW(fd,26), (short)RFIFOW(fd,28), (short)RFIFOW(fd,30), (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), timestamp, (timestamp == 0 ? "unlimited" : tmpstr2), ip); + auth_dat[i].connect_until_time = timestamp; + mmo_auth_sync(); + WFIFOL(fd,30) = (unsigned long)auth_dat[i].connect_until_time; + } else { + strftime(tmpstr, 24, date_format, localtime(&auth_dat[i].connect_until_time)); + login_log("'ladmin': Impossible to adjust a validity limit (account: %s, %d (%s) + (%+d y %+d m %+d d %+d h %+d mn %+d s) -> ???, ip: %s)" RETCODE, + auth_dat[i].userid, auth_dat[i].connect_until_time, (auth_dat[i].connect_until_time == 0 ? "unlimited" : tmpstr), (short)RFIFOW(fd,26), (short)RFIFOW(fd,28), (short)RFIFOW(fd,30), (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), ip); + WFIFOL(fd,30) = 0; + } + } + } else { + memcpy(WFIFOP(fd,6), account_name, 24); + login_log("'ladmin': Attempt to adjust the validity limit of an unknown account (account: %s, ip: %s)" RETCODE, + account_name, ip); + WFIFOL(fd,30) = 0; + } + } + WFIFOSET(fd,34); + RFIFOSKIP(fd,38); + break; + + case 0x7952: // Request about informations of an account (by account name) + if (RFIFOREST(fd) < 26) + return 0; + WFIFOW(fd,0) = 0x7953; + WFIFOL(fd,2) = -1; + account_name = RFIFOP(fd,2); + account_name[23] = '\0'; + remove_control_chars(account_name); + i = search_account_index(account_name); + if (i != -1) { + WFIFOL(fd,2) = auth_dat[i].account_id; + WFIFOB(fd,6) = (unsigned char)isGM(auth_dat[i].account_id); + memcpy(WFIFOP(fd,7), auth_dat[i].userid, 24); + WFIFOB(fd,31) = auth_dat[i].sex; + WFIFOL(fd,32) = auth_dat[i].logincount; + WFIFOL(fd,36) = auth_dat[i].state; + memcpy(WFIFOP(fd,40), auth_dat[i].error_message, 20); + memcpy(WFIFOP(fd,60), auth_dat[i].lastlogin, 24); + memcpy(WFIFOP(fd,84), auth_dat[i].last_ip, 16); + memcpy(WFIFOP(fd,100), auth_dat[i].email, 40); + WFIFOL(fd,140) = (unsigned long)auth_dat[i].connect_until_time; + WFIFOL(fd,144) = (unsigned long)auth_dat[i].ban_until_time; + WFIFOW(fd,148) = strlen(auth_dat[i].memo); + if (auth_dat[i].memo[0]) { + memcpy(WFIFOP(fd,150), auth_dat[i].memo, strlen(auth_dat[i].memo)); + } + login_log("'ladmin': Sending information of an account (request by the name; account: %s, id: %d, ip: %s)" RETCODE, + auth_dat[i].userid, auth_dat[i].account_id, ip); + WFIFOSET(fd,150+strlen(auth_dat[i].memo)); + } else { + memcpy(WFIFOP(fd,7), account_name, 24); + WFIFOW(fd,148) = 0; + login_log("'ladmin': Attempt to obtain information (by the name) of an unknown account (account: %s, ip: %s)" RETCODE, + account_name, ip); + WFIFOSET(fd,150); + } + RFIFOSKIP(fd,26); + break; + + case 0x7954: // Request about information of an account (by account id) + if (RFIFOREST(fd) < 6) + return 0; + WFIFOW(fd,0) = 0x7953; + WFIFOL(fd,2) = RFIFOL(fd,2); + memset(WFIFOP(fd,7), '\0', 24); + for(i = 0; i < auth_num; i++) { + if (auth_dat[i].account_id == RFIFOL(fd,2)) { + login_log("'ladmin': Sending information of an account (request by the id; account: %s, id: %d, ip: %s)" RETCODE, + auth_dat[i].userid, RFIFOL(fd,2), ip); + WFIFOB(fd,6) = (unsigned char)isGM(auth_dat[i].account_id); + memcpy(WFIFOP(fd,7), auth_dat[i].userid, 24); + WFIFOB(fd,31) = auth_dat[i].sex; + WFIFOL(fd,32) = auth_dat[i].logincount; + WFIFOL(fd,36) = auth_dat[i].state; + memcpy(WFIFOP(fd,40), auth_dat[i].error_message, 20); + memcpy(WFIFOP(fd,60), auth_dat[i].lastlogin, 24); + memcpy(WFIFOP(fd,84), auth_dat[i].last_ip, 16); + memcpy(WFIFOP(fd,100), auth_dat[i].email, 40); + WFIFOL(fd,140) = (unsigned long)auth_dat[i].connect_until_time; + WFIFOL(fd,144) = (unsigned long)auth_dat[i].ban_until_time; + WFIFOW(fd,148) = strlen(auth_dat[i].memo); + if (auth_dat[i].memo[0]) { + memcpy(WFIFOP(fd,150), auth_dat[i].memo, strlen(auth_dat[i].memo)); + } + WFIFOSET(fd,150+strlen(auth_dat[i].memo)); + break; + } + } + if (i == auth_num) { + login_log("'ladmin': Attempt to obtain information (by the id) of an unknown account (id: %d, ip: %s)" RETCODE, + RFIFOL(fd,2), ip); + strncpy(WFIFOP(fd,7), "", 24); + WFIFOW(fd,148) = 0; + WFIFOSET(fd,150); + } + RFIFOSKIP(fd,6); + break; + + case 0x7955: // Request to reload GM file (no answer) + login_log("'ladmin': Request to re-load GM configuration file (ip: %s)." RETCODE, ip); + read_gm_account(); + // send GM accounts to all char-servers + send_GM_accounts(); + RFIFOSKIP(fd,2); + break; + + default: + { + FILE *logfp; + char tmpstr[24]; + struct timeval tv; + logfp = fopen(login_log_unknown_packets_filename, "a"); + if (logfp) { + gettimeofday(&tv, NULL); + strftime(tmpstr, 23, date_format, localtime(&(tv.tv_sec))); + fprintf(logfp, "%s.%03d: receiving of an unknown packet -> disconnection" RETCODE, tmpstr, (int)tv.tv_usec / 1000); + fprintf(logfp, "parse_admin: connection #%d (ip: %s), packet: 0x%x (with being read: %d)." RETCODE, fd, ip, RFIFOW(fd,0), RFIFOREST(fd)); + fprintf(logfp, "Detail (in hex):" RETCODE); + fprintf(logfp, "---- 00-01-02-03-04-05-06-07 08-09-0A-0B-0C-0D-0E-0F" RETCODE); + memset(tmpstr, '\0', sizeof(tmpstr)); + for(i = 0; i < RFIFOREST(fd); i++) { + if ((i & 15) == 0) + fprintf(logfp, "%04X ",i); + fprintf(logfp, "%02x ", RFIFOB(fd,i)); + if (RFIFOB(fd,i) > 0x1f) + tmpstr[i % 16] = RFIFOB(fd,i); + else + tmpstr[i % 16] = '.'; + if ((i - 7) % 16 == 0) // -8 + 1 + fprintf(logfp, " "); + else if ((i + 1) % 16 == 0) { + fprintf(logfp, " %s" RETCODE, tmpstr); + memset(tmpstr, '\0', sizeof(tmpstr)); + } + } + if (i % 16 != 0) { + for(j = i; j % 16 != 0; j++) { + fprintf(logfp, " "); + if ((j - 7) % 16 == 0) // -8 + 1 + fprintf(logfp, " "); + } + fprintf(logfp, " %s" RETCODE, tmpstr); + } + fprintf(logfp, RETCODE); + fclose(logfp); + } + } + login_log("'ladmin': End of connection, unknown packet (ip: %s)" RETCODE, ip); + session[fd]->eof = 1; + printf("Remote administration has been disconnected (unknown packet).\n"); + return 0; + } + //WFIFOW(fd,0) = 0x791f; + //WFIFOSET(fd,2); + } + return 0; +} + +//-------------------------------------------- +// Test to know if an IP come from LAN or WAN. +//-------------------------------------------- +int lan_ip_check(unsigned char *p) { + int i; + int lancheck = 1; + +// printf("lan_ip_check: to compare: %d.%d.%d.%d, network: %d.%d.%d.%d/%d.%d.%d.%d\n", +// p[0], p[1], p[2], p[3], +// subneti[0], subneti[1], subneti[2], subneti[3], +// subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]); + for(i = 0; i < 4; i++) { + if ((subneti[i] & subnetmaski[i]) != (p[i] & subnetmaski[i])) { + lancheck = 0; + break; + } + } + printf("LAN test (result): %s source\033[0m.\n", (lancheck) ? "\033[1;36mLAN" : "\033[1;32mWAN"); + return lancheck; +} + +//---------------------------------------------------------------------------------------- +// Default packet parsing (normal players or administation/char-server connexion requests) +//---------------------------------------------------------------------------------------- +int parse_login(int fd) { + struct mmo_account account; + int result, i, j; + unsigned char *p = (unsigned char *) &session[fd]->client_addr.sin_addr; + char ip[16]; + + sprintf(ip, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); + + if (session[fd]->eof) { + close(fd); + delete_session(fd); + return 0; + } + + while(RFIFOREST(fd) >= 2) { + if (display_parse_login == 1) { + if (RFIFOW(fd,0) == 0x64 || RFIFOW(fd,0) == 0x01dd) { + if (RFIFOREST(fd) >= ((RFIFOW(fd,0) == 0x64) ? 55 : 47)) + printf("parse_login: connection #%d, packet: 0x%x (with being read: %d), account: %s.\n", fd, RFIFOW(fd,0), RFIFOREST(fd), RFIFOP(fd,6)); + } else if (RFIFOW(fd,0) == 0x2710) { + if (RFIFOREST(fd) >= 86) + printf("parse_login: connection #%d, packet: 0x%x (with being read: %d), server: %s.\n", fd, RFIFOW(fd,0), RFIFOREST(fd), RFIFOP(fd,60)); + } else + printf("parse_login: connection #%d, packet: 0x%x (with being read: %d).\n", fd, RFIFOW(fd,0), RFIFOREST(fd)); + } + + switch(RFIFOW(fd,0)) { + case 0x200: // New alive packet: structure: 0x200 .24B. used to verify if client is always alive. + if (RFIFOREST(fd) < 26) + return 0; + RFIFOSKIP(fd,26); + break; + + case 0x204: // New alive packet: structure: 0x204 .16B. (new ragexe from 22 june 2004) + if (RFIFOREST(fd) < 18) + return 0; + RFIFOSKIP(fd,18); + break; + + case 0x64: // Ask connection of a client + case 0x01dd: // Ask connection of a client (encryption mode) + if (RFIFOREST(fd) < ((RFIFOW(fd,0) == 0x64) ? 55 : 47)) + return 0; + + account.userid = RFIFOP(fd,6); + account.userid[23] = '\0'; + remove_control_chars(account.userid); + account.passwd = RFIFOP(fd,30); + if (RFIFOW(fd,0) == 0x64) { + account.passwd[23] = '\0'; + remove_control_chars(account.passwd); + } +#ifdef PASSWORDENC + account.passwdenc = (RFIFOW(fd,0) == 0x64) ? 0 : PASSWORDENC; +#else + account.passwdenc = 0; +#endif + + if (RFIFOW(fd,0) == 0x64) { + login_log("Request for connection (non encryption mode) of %s (ip: %s)." RETCODE, account.userid, ip); + } else { + login_log("Request for connection (encryption mode) of %s (ip: %s)." RETCODE, account.userid, ip); + } + + if (!check_ip(session[fd]->client_addr.sin_addr.s_addr)) { + login_log("Connection refused: IP isn't authorised (deny/allow, ip: %s)." RETCODE, ip); + WFIFOW(fd,0) = 0x6a; + WFIFOB(fd,2) = 0x03; + WFIFOSET(fd,3); + RFIFOSKIP(fd,(RFIFOW(fd,0) == 0x64) ? 55 : 47); + break; + } + + result = mmo_auth(&account, fd); + if (result == -1) { + int gm_level = isGM(account.account_id); + if (min_level_to_connect > gm_level) { + login_log("Connection refused: the minimum GM level for connection is %d (account: %s, GM level: %d, ip: %s)." RETCODE, + min_level_to_connect, account.userid, gm_level, ip); + WFIFOW(fd,0) = 0x81; + WFIFOL(fd,2) = 1; // 01 = Server closed + WFIFOSET(fd,3); + } else { + if (gm_level) + printf("Connection of the GM (level:%d) account '%s' accepted.\n", gm_level, account.userid); + else + printf("Connection of the account '%s' accepted.\n", account.userid); + server_num = 0; + for(i = 0; i < MAX_SERVERS; i++) { + if (server_fd[i] >= 0) { + if (lan_ip_check(p)) + WFIFOL(fd,47+server_num*32) = inet_addr(lan_char_ip); + else + WFIFOL(fd,47+server_num*32) = server[i].ip; + WFIFOW(fd,47+server_num*32+4) = server[i].port; + memcpy(WFIFOP(fd,47+server_num*32+6), server[i].name, 20); + WFIFOW(fd,47+server_num*32+26) = server[i].users; + WFIFOW(fd,47+server_num*32+28) = server[i].maintenance; + WFIFOW(fd,47+server_num*32+30) = server[i].new; + server_num++; + } + } + // if at least 1 char-server + if (server_num > 0) { + WFIFOW(fd,0) = 0x69; + WFIFOW(fd,2) = 47+32*server_num; + WFIFOL(fd,4) = account.login_id1; + WFIFOL(fd,8) = account.account_id; + WFIFOL(fd,12) = account.login_id2; + WFIFOL(fd,16) = 0; // in old version, that was for ip (not more used) + memcpy(WFIFOP(fd,20), account.lastlogin, 24); // in old version, that was for name (not more used) + WFIFOB(fd,46) = account.sex; + WFIFOSET(fd,47+32*server_num); + if (auth_fifo_pos >= AUTH_FIFO_SIZE) + auth_fifo_pos = 0; + auth_fifo[auth_fifo_pos].account_id = account.account_id; + auth_fifo[auth_fifo_pos].login_id1 = account.login_id1; + auth_fifo[auth_fifo_pos].login_id2 = account.login_id2; + auth_fifo[auth_fifo_pos].sex = account.sex; + auth_fifo[auth_fifo_pos].delflag = 0; + auth_fifo[auth_fifo_pos].ip = session[fd]->client_addr.sin_addr.s_addr; + auth_fifo_pos++; + // if no char-server, don't send void list of servers, just disconnect the player with proper message + } else { + login_log("Connection refused: there is no char-server online (account: %s, ip: %s)." RETCODE, + account.userid, ip); + WFIFOW(fd,0) = 0x81; + WFIFOL(fd,2) = 1; // 01 = Server closed + WFIFOSET(fd,3); + } + } + } else { + memset(WFIFOP(fd,0), '\0', 23); + WFIFOW(fd,0) = 0x6a; + WFIFOB(fd,2) = result; + if (result == 6) { // 6 = Your are Prohibited to log in until %s + i = search_account_index(account.userid); + if (i != -1) { + if (auth_dat[i].ban_until_time != 0) { // if account is banned, we send ban timestamp + char tmpstr[256]; + strftime(tmpstr, 20, date_format, localtime(&auth_dat[i].ban_until_time)); + tmpstr[19] = '\0'; + memcpy(WFIFOP(fd,3), tmpstr, 20); + } else { // we send error message + memcpy(WFIFOP(fd,3), auth_dat[i].error_message, 20); + } + } + } + WFIFOSET(fd,23); + } + RFIFOSKIP(fd,(RFIFOW(fd,0) == 0x64) ? 55 : 47); + break; + + case 0x01db: // Sending request of the coding key + case 0x791a: // Sending request of the coding key (administration packet) + { + struct login_session_data *ld; + if (session[fd]->session_data) { + printf("login: abnormal request of MD5 key (already opened session).\n"); + session[fd]->eof = 1; + return 0; + } + ld = session[fd]->session_data = calloc(sizeof(*ld), 1); + if (!ld) { + printf("login: Request for md5 key: memory allocation failure (malloc)!\n"); + session[fd]->eof = 1; + return 0; + } + if (RFIFOW(fd,0) == 0x01db) { + login_log("Sending request of the coding key (ip: %s)" RETCODE, ip); + } else { + login_log("'ladmin': Sending request of the coding key (ip: %s)" RETCODE, ip); + } + // Creation of the coding key + memset(ld->md5key, '\0', sizeof(ld->md5key)); + ld->md5keylen = rand() % 4 + 12; + for(i = 0; i < ld->md5keylen; i++) + ld->md5key[i] = rand() % 255 + 1; + + RFIFOSKIP(fd,2); + WFIFOW(fd,0) = 0x01dc; + WFIFOW(fd,2) = 4 + ld->md5keylen; + memcpy(WFIFOP(fd,4), ld->md5key, ld->md5keylen); + WFIFOSET(fd,WFIFOW(fd,2)); + } + break; + + case 0x2710: // Connection request of a char-server + if (RFIFOREST(fd) < 86) + return 0; + { + int GM_value, len; + unsigned char* server_name; + account.userid = RFIFOP(fd,2); + account.userid[23] = '\0'; + remove_control_chars(account.userid); + account.passwd = RFIFOP(fd,26); + account.passwd[23] = '\0'; + remove_control_chars(account.passwd); + account.passwdenc = 0; + server_name = RFIFOP(fd,60); + server_name[19] = '\0'; + remove_control_chars(server_name); + login_log("Connection request of the char-server '%s' @ %d.%d.%d.%d:%d (ip: %s)" RETCODE, + server_name, RFIFOB(fd,54), RFIFOB(fd,55), RFIFOB(fd,56), RFIFOB(fd,57), RFIFOW(fd,58), ip); + result = mmo_auth(&account, fd); + if (result == -1 && account.sex == 2 && account.account_id < MAX_SERVERS && server_fd[account.account_id] == -1) { + login_log("Connection of the char-server '%s' accepted (account: %s, pass: %s, ip: %s)" RETCODE, + server_name, account.userid, account.passwd, ip); + printf("Connection of the char-server '%s' accepted.\n", server_name); + memset(&server[account.account_id], 0, sizeof(struct mmo_char_server)); + server[account.account_id].ip = RFIFOL(fd,54); + server[account.account_id].port = RFIFOW(fd,58); + memcpy(server[account.account_id].name, server_name, 20); + server[account.account_id].users = 0; + server[account.account_id].maintenance = RFIFOW(fd,82); + server[account.account_id].new = RFIFOW(fd,84); + server_fd[account.account_id] = fd; + if(anti_freeze_enable) + server_freezeflag[account.account_id] = 5; // Char-server anti-freeze system. Counter. 5 ok, 4...0 freezed + WFIFOW(fd,0) = 0x2711; + WFIFOB(fd,2) = 0; + WFIFOSET(fd,3); + session[fd]->func_parse = parse_fromchar; + realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK); + // send GM account to char-server + len = 4; + WFIFOW(fd,0) = 0x2732; + for(i = 0; i < auth_num; i++) + // send only existing accounts. We can not create a GM account when server is online. + if ((GM_value = isGM(auth_dat[i].account_id)) > 0) { + WFIFOL(fd,len) = auth_dat[i].account_id; + WFIFOB(fd,len+4) = (unsigned char)GM_value; + len += 5; + } + WFIFOW(fd,2) = len; + WFIFOSET(fd,len); + } else { + login_log("Connexion of the char-server '%s' REFUSED (account: %s, pass: %s, ip: %s)" RETCODE, + server_name, account.userid, account.passwd, ip); + WFIFOW(fd,0) = 0x2711; + WFIFOB(fd,2) = 3; + WFIFOSET(fd,3); + } + } + RFIFOSKIP(fd,86); + return 0; + + case 0x7530: // Request of the server version + login_log("Sending of the server version (ip: %s)" RETCODE, ip); + WFIFOW(fd,0) = 0x7531; + WFIFOB(fd,2) = ATHENA_MAJOR_VERSION; + WFIFOB(fd,3) = ATHENA_MINOR_VERSION; + WFIFOB(fd,4) = ATHENA_REVISION; + WFIFOB(fd,5) = ATHENA_RELEASE_FLAG; + WFIFOB(fd,6) = ATHENA_OFFICIAL_FLAG; + WFIFOB(fd,7) = ATHENA_SERVER_LOGIN; + WFIFOW(fd,8) = ATHENA_MOD_VERSION; + WFIFOSET(fd,10); + RFIFOSKIP(fd,2); + break; + + case 0x7532: // Request to end connection + login_log("End of connection (ip: %s)" RETCODE, ip); + session[fd]->eof = 1; + return 0; + + case 0x7918: // Request for administation login + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < ((RFIFOW(fd,2) == 0) ? 28 : 20)) + return 0; + WFIFOW(fd,0) = 0x7919; + WFIFOB(fd,2) = 1; + if (!check_ladminip(session[fd]->client_addr.sin_addr.s_addr)) { + login_log("'ladmin'-login: Connection in administration mode refused: IP isn't authorised (ladmin_allow, ip: %s)." RETCODE, ip); + } else { + struct login_session_data *ld = session[fd]->session_data; + if (RFIFOW(fd,2) == 0) { // non encrypted password + unsigned char* password; + password = RFIFOP(fd,4); + password[23] = '\0'; + remove_control_chars(password); + // If remote administration is enabled and password sent by client matches password read from login server configuration file + if ((admin_state == 1) && (strcmp(password, admin_pass) == 0)) { + login_log("'ladmin'-login: Connection in administration mode accepted (non encrypted password: %s, ip: %s)" RETCODE, password, ip); + printf("Connection of a remote administration accepted (non encrypted password).\n"); + WFIFOB(fd,2) = 0; + session[fd]->func_parse = parse_admin; + } else if (admin_state != 1) + login_log("'ladmin'-login: Connection in administration mode REFUSED - remote administration is disabled (non encrypted password: %s, ip: %s)" RETCODE, password, ip); + else + login_log("'ladmin'-login: Connection in administration mode REFUSED - invalid password (non encrypted password: %s, ip: %s)" RETCODE, password, ip); + } else { // encrypted password + if (!ld) + printf("'ladmin'-login: error! MD5 key not created/requested for an administration login.\n"); + else { + char md5str[64] = "", md5bin[32]; + if (RFIFOW(fd,2) == 1) { + strncpy(md5str, ld->md5key, sizeof(ld->md5key)); // 20 + strcat(md5str, admin_pass); // 24 + } else if (RFIFOW(fd,2) == 2) { + strncpy(md5str, admin_pass, sizeof(admin_pass)); // 24 + strcat(md5str, ld->md5key); // 20 + } + MD5_String2binary(md5str, md5bin); + // If remote administration is enabled and password hash sent by client matches hash of password read from login server configuration file + if ((admin_state == 1) && (memcmp(md5bin, RFIFOP(fd,4), 16) == 0)) { + login_log("'ladmin'-login: Connection in administration mode accepted (encrypted password, ip: %s)" RETCODE, ip); + printf("Connection of a remote administration accepted (encrypted password).\n"); + WFIFOB(fd,2) = 0; + session[fd]->func_parse = parse_admin; + } else if (admin_state != 1) + login_log("'ladmin'-login: Connection in administration mode REFUSED - remote administration is disabled (encrypted password, ip: %s)" RETCODE, ip); + else + login_log("'ladmin'-login: Connection in administration mode REFUSED - invalid password (encrypted password, ip: %s)" RETCODE, ip); + } + } + } + WFIFOSET(fd,3); + RFIFOSKIP(fd, (RFIFOW(fd,2) == 0) ? 28 : 20); + break; + + default: + if (save_unknown_packets) { + FILE *logfp; + char tmpstr[24]; + struct timeval tv; + logfp = fopen(login_log_unknown_packets_filename, "a"); + if (logfp) { + gettimeofday(&tv, NULL); + strftime(tmpstr, 23, date_format, localtime(&(tv.tv_sec))); + fprintf(logfp, "%s.%03d: receiving of an unknown packet -> disconnection" RETCODE, tmpstr, (int)tv.tv_usec / 1000); + fprintf(logfp, "parse_login: connection #%d (ip: %s), packet: 0x%x (with being read: %d)." RETCODE, fd, ip, RFIFOW(fd,0), RFIFOREST(fd)); + fprintf(logfp, "Detail (in hex):" RETCODE); + fprintf(logfp, "---- 00-01-02-03-04-05-06-07 08-09-0A-0B-0C-0D-0E-0F" RETCODE); + memset(tmpstr, '\0', sizeof(tmpstr)); + for(i = 0; i < RFIFOREST(fd); i++) { + if ((i & 15) == 0) + fprintf(logfp, "%04X ",i); + fprintf(logfp, "%02x ", RFIFOB(fd,i)); + if (RFIFOB(fd,i) > 0x1f) + tmpstr[i % 16] = RFIFOB(fd,i); + else + tmpstr[i % 16] = '.'; + if ((i - 7) % 16 == 0) // -8 + 1 + fprintf(logfp, " "); + else if ((i + 1) % 16 == 0) { + fprintf(logfp, " %s" RETCODE, tmpstr); + memset(tmpstr, '\0', sizeof(tmpstr)); + } + } + if (i % 16 != 0) { + for(j = i; j % 16 != 0; j++) { + fprintf(logfp, " "); + if ((j - 7) % 16 == 0) // -8 + 1 + fprintf(logfp, " "); + } + fprintf(logfp, " %s" RETCODE, tmpstr); + } + fprintf(logfp, RETCODE); + fclose(logfp); + } + } + login_log("End of connection, unknown packet (ip: %s)" RETCODE, ip); + session[fd]->eof = 1; + return 0; + } + } + return 0; +} + + +//------------------------------------------------- +// Return numerical value of a switch configuration +// on/off, english, fran軋is, deutsch, espaol +//------------------------------------------------- +int config_switch(const char *str) { + if (strcmpi(str, "on") == 0 || strcmpi(str, "yes") == 0 || strcmpi(str, "oui") == 0 || strcmpi(str, "ja") == 0 || strcmpi(str, "si") == 0) + return 1; + if (strcmpi(str, "off") == 0 || strcmpi(str, "no") == 0 || strcmpi(str, "non") == 0 || strcmpi(str, "nein") == 0) + return 0; + + return atoi(str); +} + +//---------------------------------- +// Reading Lan Support configuration +//---------------------------------- +int login_lan_config_read(const char *lancfgName) { + int j; + struct hostent * h = NULL; + char line[1024], w1[1024], w2[1024]; + FILE *fp; + + // set default configuration + strncpy(lan_char_ip, "127.0.0.1", sizeof(lan_char_ip)); + subneti[0] = 127; + subneti[1] = 0; + subneti[2] = 0; + subneti[3] = 1; + for(j = 0; j < 4; j++) + subnetmaski[j] = 255; + + fp = fopen(lancfgName, "r"); + + if (fp == NULL) { + printf("***WARNING: LAN Support configuration file is not found: %s\n", lancfgName); + return 1; + } + + printf("---Start reading Lan Support configuration file\n"); + + while(fgets(line, sizeof(line)-1, fp)) { + if (line[0] == '/' && line[1] == '/') + continue; + + line[sizeof(line)-1] = '\0'; + if (sscanf(line,"%[^:]: %[^\r\n]", w1, w2) != 2) + continue; + + remove_control_chars(w1); + remove_control_chars(w2); + if (strcmpi(w1, "lan_char_ip") == 0) { // Read Char-Server Lan IP Address + h = gethostbyname(w2); + if (h != NULL) { + sprintf(lan_char_ip, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]); + } else { + strncpy(lan_char_ip, w2, sizeof(lan_char_ip)); + lan_char_ip[sizeof(lan_char_ip)-1] = '\0'; + } + printf("LAN IP of char-server: %s.\n", lan_char_ip); + } else if (strcmpi(w1, "subnet") == 0) { // Read Subnetwork + for(j = 0; j < 4; j++) + subneti[j] = 0; + h = gethostbyname(w2); + if (h != NULL) { + for(j = 0; j < 4; j++) + subneti[j] = (unsigned char)h->h_addr[j]; + } else { + sscanf(w2, "%d.%d.%d.%d", &subneti[0], &subneti[1], &subneti[2], &subneti[3]); + } + printf("Sub-network of the char-server: %d.%d.%d.%d.\n", subneti[0], subneti[1], subneti[2], subneti[3]); + } else if (strcmpi(w1, "subnetmask") == 0) { // Read Subnetwork Mask + for(j = 0; j < 4; j++) + subnetmaski[j] = 255; + h = gethostbyname(w2); + if (h != NULL) { + for(j = 0; j < 4; j++) + subnetmaski[j] = (unsigned char)h->h_addr[j]; + } else { + sscanf(w2, "%d.%d.%d.%d", &subnetmaski[0], &subnetmaski[1], &subnetmaski[2], &subnetmaski[3]); + } + printf("Sub-network mask of the char-server: %d.%d.%d.%d.\n", subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]); + } + } + fclose(fp); + + // log the LAN configuration + login_log("The LAN configuration of the server is set:" RETCODE); + login_log("- with LAN IP of char-server: %s." RETCODE, lan_char_ip); + login_log("- with the sub-network of the char-server: %d.%d.%d.%d/%d.%d.%d.%d." RETCODE, + subneti[0], subneti[1], subneti[2], subneti[3], subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]); + + // sub-network check of the char-server + { + unsigned int a0, a1, a2, a3; + unsigned char p[4]; + sscanf(lan_char_ip, "%d.%d.%d.%d", &a0, &a1, &a2, &a3); + p[0] = a0; p[1] = a1; p[2] = a2; p[3] = a3; + printf("LAN test of LAN IP of the char-server: "); + if (lan_ip_check(p) == 0) { + printf("\033[1;31m***ERROR: LAN IP of the char-server doesn't belong to the specified Sub-network\033[0m\n"); + login_log("***ERROR: LAN IP of the char-server doesn't belong to the specified Sub-network." RETCODE); + } + } + + printf("---End reading of Lan Support configuration file\n"); + + return 0; +} + +//----------------------------------- +// Reading general configuration file +//----------------------------------- +int login_config_read(const char *cfgName) { + char line[1024], w1[1024], w2[1024]; + FILE *fp; + + fp = fopen(cfgName, "r"); + if (fp == NULL) { + printf("Configuration file (%s) not found.\n", cfgName); + return 1; + } + + printf("---Start reading of Login Server configuration file (%s)\n", cfgName); + while(fgets(line, sizeof(line)-1, fp)) { + if (line[0] == '/' && line[1] == '/') + continue; + + line[sizeof(line)-1] = '\0'; + if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) == 2) { + remove_control_chars(w1); + remove_control_chars(w2); + + if (strcmpi(w1, "admin_state") == 0) { + admin_state = config_switch(w2); + } else if (strcmpi(w1, "admin_pass") == 0) { + strncpy(admin_pass, w2, sizeof(admin_pass)); + admin_pass[sizeof(admin_pass)-1] = '\0'; + } else if (strcmpi(w1, "ladminallowip") == 0) { + if (strcmpi(w2, "clear") == 0) { + if (access_ladmin_allow) + free(access_ladmin_allow); + access_ladmin_allow = NULL; + access_ladmin_allownum = 0; + } else { + if (strcmpi(w2, "all") == 0) { + // reset all previous values + if (access_ladmin_allow) + free(access_ladmin_allow); + // set to all + access_ladmin_allow = calloc(ACO_STRSIZE, 1); + access_ladmin_allownum = 1; + access_ladmin_allow[0] = '\0'; + } else if (w2[0] && !(access_ladmin_allownum == 1 && access_ladmin_allow[0] == '\0')) { // don't add IP if already 'all' + if (access_ladmin_allow) + access_ladmin_allow = realloc(access_ladmin_allow, (access_ladmin_allownum+1) * ACO_STRSIZE); + else + access_ladmin_allow = calloc(ACO_STRSIZE, 1); + strncpy(access_ladmin_allow + (access_ladmin_allownum++) * ACO_STRSIZE, w2, ACO_STRSIZE); + access_ladmin_allow[access_ladmin_allownum * ACO_STRSIZE - 1] = '\0'; + } + } + } else if (strcmpi(w1, "gm_pass") == 0) { + strncpy(gm_pass, w2, sizeof(gm_pass)); + gm_pass[sizeof(gm_pass)-1] = '\0'; + } else if (strcmpi(w1, "level_new_gm") == 0) { + level_new_gm = atoi(w2); + } else if (strcmpi(w1, "new_account") == 0) { + new_account_flag = config_switch(w2); + } else if (strcmpi(w1, "login_port") == 0) { + login_port = atoi(w2); + } else if (strcmpi(w1, "account_filename") == 0) { + strncpy(account_filename, w2, sizeof(account_filename)); + account_filename[sizeof(account_filename)-1] = '\0'; + } else if (strcmpi(w1, "gm_account_filename") == 0) { + strncpy(GM_account_filename, w2, sizeof(GM_account_filename)); + GM_account_filename[sizeof(GM_account_filename)-1] = '\0'; + } else if (strcmpi(w1, "gm_account_filename_check_timer") == 0) { + gm_account_filename_check_timer = atoi(w2); + } else if (strcmpi(w1, "login_log_filename") == 0) { + strncpy(login_log_filename, w2, sizeof(login_log_filename)); + login_log_filename[sizeof(login_log_filename)-1] = '\0'; + } else if (strcmpi(w1, "login_log_unknown_packets_filename") == 0) { + strncpy(login_log_unknown_packets_filename, w2, sizeof(login_log_unknown_packets_filename)); + login_log_unknown_packets_filename[sizeof(login_log_unknown_packets_filename)-1] = '\0'; + } else if (strcmpi(w1, "save_unknown_packets") == 0) { + save_unknown_packets = config_switch(w2); + } else if (strcmpi(w1, "display_parse_login") == 0) { + display_parse_login = config_switch(w2); // 0: no, 1: yes + } else if (strcmpi(w1, "display_parse_admin") == 0) { + display_parse_admin = config_switch(w2); // 0: no, 1: yes + } else if (strcmpi(w1, "display_parse_fromchar") == 0) { + display_parse_fromchar = config_switch(w2); // 0: no, 1: yes (without packet 0x2714), 2: all packets + } else if (strcmpi(w1, "date_format") == 0) { // note: never have more than 19 char for the date! + switch (atoi(w2)) { + case 0: + strcpy(date_format, "%d-%m-%Y %H:%M:%S"); // 31-12-2004 23:59:59 + break; + case 1: + strcpy(date_format, "%m-%d-%Y %H:%M:%S"); // 12-31-2004 23:59:59 + break; + case 2: + strcpy(date_format, "%Y-%d-%m %H:%M:%S"); // 2004-31-12 23:59:59 + break; + case 3: + strcpy(date_format, "%Y-%m-%d %H:%M:%S"); // 2004-12-31 23:59:59 + break; + } + } else if (strcmpi(w1, "min_level_to_connect") == 0) { + min_level_to_connect = atoi(w2); + } else if (strcmpi(w1, "add_to_unlimited_account") == 0) { + add_to_unlimited_account = config_switch(w2); + } else if (strcmpi(w1, "start_limited_time") == 0) { + start_limited_time = atoi(w2); + } else if (strcmpi(w1, "check_ip_flag") == 0) { + check_ip_flag = config_switch(w2); + } else if (strcmpi(w1, "order") == 0) { + access_order = atoi(w2); + if (strcmpi(w2, "deny,allow") == 0 || + strcmpi(w2, "deny, allow") == 0) access_order = ACO_DENY_ALLOW; + if (strcmpi(w2, "allow,deny") == 0 || + strcmpi(w2, "allow, deny") == 0) access_order = ACO_ALLOW_DENY; + if (strcmpi(w2, "mutual-failture") == 0 || + strcmpi(w2, "mutual-failure") == 0) access_order = ACO_MUTUAL_FAILTURE; + } else if (strcmpi(w1, "allow") == 0) { + if (strcmpi(w2, "clear") == 0) { + if (access_allow) + free(access_allow); + access_allow = NULL; + access_allownum = 0; + } else { + if (strcmpi(w2, "all") == 0) { + // reset all previous values + if (access_allow) + free(access_allow); + // set to all + access_allow = calloc(ACO_STRSIZE, 1); + access_allownum = 1; + access_allow[0] = '\0'; + } else if (w2[0] && !(access_allownum == 1 && access_allow[0] == '\0')) { // don't add IP if already 'all' + if (access_allow) + access_allow = realloc(access_allow, (access_allownum+1) * ACO_STRSIZE); + else + access_allow = calloc(ACO_STRSIZE, 1); + strncpy(access_allow + (access_allownum++) * ACO_STRSIZE, w2, ACO_STRSIZE); + access_allow[access_allownum * ACO_STRSIZE - 1] = '\0'; + } + } + } else if (strcmpi(w1, "deny") == 0) { + if (strcmpi(w2, "clear") == 0) { + if (access_deny) + free(access_deny); + access_deny = NULL; + access_denynum = 0; + } else { + if (strcmpi(w2, "all") == 0) { + // reset all previous values + if (access_deny) + free(access_deny); + // set to all + access_deny = calloc(ACO_STRSIZE, 1); + access_denynum = 1; + access_deny[0] = '\0'; + } else if (w2[0] && !(access_denynum == 1 && access_deny[0] == '\0')) { // don't add IP if already 'all' + if (access_deny) + access_deny = realloc(access_deny, (access_denynum+1) * ACO_STRSIZE); + else + access_deny = calloc(ACO_STRSIZE, 1); + strncpy(access_deny + (access_denynum++) * ACO_STRSIZE, w2, ACO_STRSIZE); + access_deny[access_denynum * ACO_STRSIZE - 1] = '\0'; + } + } + } else if(strcmpi(w1,"anti_freeze_enable")==0){ + anti_freeze_enable = config_switch(w2); + } else if (strcmpi(w1, "anti_freeze_interval") == 0) { + ANTI_FREEZE_INTERVAL = atoi(w2); + if (ANTI_FREEZE_INTERVAL < 5) + ANTI_FREEZE_INTERVAL = 5; // minimum 5 seconds + } else if (strcmpi(w1, "import") == 0) { + login_config_read(w2); + } + } + } + fclose(fp); + + printf("---End reading of Login Server configuration file.\n"); + + return 0; +} + +//------------------------------------- +// Displaying of configuration warnings +//------------------------------------- +void display_conf_warnings(void) { + if (admin_state != 0 && admin_state != 1) { + printf("***WARNING: Invalid value for admin_state parameter -> set to 0 (no remote admin).\n"); + admin_state = 0; + } + + if (admin_state == 1) { + if (admin_pass[0] == '\0') { + printf("***WARNING: Administrator password is void (admin_pass).\n"); + } else if (strcmp(admin_pass, "admin") == 0) { + printf("***WARNING: You are using the default administrator password (admin_pass).\n"); + printf(" We highly recommend that you change it.\n"); + } + } + + if (gm_pass[0] == '\0') { + printf("***WARNING: 'To GM become' password is void (gm_pass).\n"); + printf(" We highly recommend that you set one password.\n"); + } else if (strcmp(gm_pass, "gm") == 0) { + printf("***WARNING: You are using the default GM password (gm_pass).\n"); + printf(" We highly recommend that you change it.\n"); + } + + if (level_new_gm < 0 || level_new_gm > 99) { + printf("***WARNING: Invalid value for level_new_gm parameter -> set to 60 (default).\n"); + level_new_gm = 60; + } + + if (new_account_flag != 0 && new_account_flag != 1) { + printf("***WARNING: Invalid value for new_account parameter -> set to 0 (no new account).\n"); + new_account_flag = 0; + } + + if (login_port < 1024 || login_port > 65535) { + printf("***WARNING: Invalid value for login_port parameter -> set to 6900 (default).\n"); + login_port = 6900; + } + + if (gm_account_filename_check_timer < 0) { + printf("***WARNING: Invalid value for gm_account_filename_check_timer parameter.\n"); + printf(" -> set to 15 sec (default).\n"); + gm_account_filename_check_timer = 15; + } else if (gm_account_filename_check_timer == 1) { + printf("***WARNING: Invalid value for gm_account_filename_check_timer parameter.\n"); + printf(" -> set to 2 sec (minimum value).\n"); + gm_account_filename_check_timer = 2; + } + + if (save_unknown_packets != 0 && save_unknown_packets != 1) { + printf("WARNING: Invalid value for save_unknown_packets parameter -> set to 0-no save.\n"); + save_unknown_packets = 0; + } + + if (display_parse_login != 0 && display_parse_login != 1) { // 0: no, 1: yes + printf("***WARNING: Invalid value for display_parse_login parameter\n"); + printf(" -> set to 0 (no display).\n"); + display_parse_login = 0; + } + + if (display_parse_admin != 0 && display_parse_admin != 1) { // 0: no, 1: yes + printf("***WARNING: Invalid value for display_parse_admin parameter\n"); + printf(" -> set to 0 (no display).\n"); + display_parse_admin = 0; + } + + if (display_parse_fromchar < 0 || display_parse_fromchar > 2) { // 0: no, 1: yes (without packet 0x2714), 2: all packets + printf("***WARNING: Invalid value for display_parse_fromchar parameter\n"); + printf(" -> set to 0 (no display).\n"); + display_parse_fromchar = 0; + } + + if (min_level_to_connect < 0) { // 0: all players, 1-99 at least gm level x + printf("***WARNING: Invalid value for min_level_to_connect (%d) parameter\n", min_level_to_connect); + printf(" -> set to 0 (any player).\n"); + min_level_to_connect = 0; + } else if (min_level_to_connect > 99) { // 0: all players, 1-99 at least gm level x + printf("***WARNING: Invalid value for min_level_to_connect (%d) parameter\n", min_level_to_connect); + printf(" -> set to 99 (only GM level 99).\n"); + min_level_to_connect = 99; + } + + if (add_to_unlimited_account != 0 && add_to_unlimited_account != 1) { // 0: no, 1: yes + printf("***WARNING: Invalid value for add_to_unlimited_account parameter\n"); + printf(" -> set to 0 (impossible to add a time to an unlimited account).\n"); + add_to_unlimited_account = 0; + } + + if (start_limited_time < -1) { // -1: create unlimited account, 0 or more: additionnal sec from now to create limited time + printf("***WARNING: Invalid value for start_limited_time parameter\n"); + printf(" -> set to -1 (new accounts are created with unlimited time).\n"); + start_limited_time = -1; + } + + if (check_ip_flag != 0 && check_ip_flag != 1) { // 0: no, 1: yes + printf("***WARNING: Invalid value for check_ip_flag parameter\n"); + printf(" -> set to 1 (check players ip between login-server & char-server).\n"); + check_ip_flag = 1; + } + + if (access_order == ACO_DENY_ALLOW) { + if (access_denynum == 1 && access_deny[0] == '\0') { + printf("***WARNING: The IP security order is 'deny,allow' (allow if not deny).\n"); + printf(" And you refuse ALL IP.\n"); + } + } else if (access_order == ACO_ALLOW_DENY) { + if (access_allownum == 0) { + printf("***WARNING: The IP security order is 'allow,deny' (deny if not allow).\n"); + printf(" But, NO IP IS AUTHORISED!\n"); + } + } else { // ACO_MUTUAL_FAILTURE + if (access_allownum == 0) { + printf("***WARNING: The IP security order is 'mutual-failture'\n"); + printf(" (allow if in the allow list and not in the deny list).\n"); + printf(" But, NO IP IS AUTHORISED!\n"); + } else if (access_denynum == 1 && access_deny[0] == '\0') { + printf("***WARNING: The IP security order is mutual-failture\n"); + printf(" (allow if in the allow list and not in the deny list).\n"); + printf(" But, you refuse ALL IP!\n"); + } + } + + return; +} + +//------------------------------- +// Save configuration in log file +//------------------------------- +void save_config_in_log(void) { + int i; + + // a newline in the log... + login_log(""); + login_log("The login-server starting..." RETCODE); + + // save configuration in log file + login_log("The configuration of the server is set:" RETCODE); + + if (admin_state != 1) + login_log("- with no remote administration." RETCODE); + else if (admin_pass[0] == '\0') + login_log("- with a remote administration with a VOID password." RETCODE); + else if (strcmp(admin_pass, "admin") == 0) + login_log("- with a remote administration with the DEFAULT password." RETCODE); + else + login_log("- with a remote administration with the password of %d character(s)." RETCODE, strlen(admin_pass)); + if (access_ladmin_allownum == 0 || (access_ladmin_allownum == 1 && access_ladmin_allow[0] == '\0')) { + login_log("- to accept any IP for remote administration" RETCODE); + } else { + login_log("- to accept following IP for remote administration:" RETCODE); + for(i = 0; i < access_ladmin_allownum; i++) + login_log(" %s" RETCODE, (char *)(access_ladmin_allow + i * ACO_STRSIZE)); + } + + if (gm_pass[0] == '\0') + login_log("- with a VOID 'To GM become' password (gm_pass)." RETCODE); + else if (strcmp(gm_pass, "gm") == 0) + login_log("- with the DEFAULT 'To GM become' password (gm_pass)." RETCODE); + else + login_log("- with a 'To GM become' password (gm_pass) of %d character(s)." RETCODE, strlen(gm_pass)); + if (level_new_gm == 0) + login_log("- to refuse any creation of GM with @gm." RETCODE); + else + login_log("- to create GM with level '%d' when @gm is used." RETCODE, level_new_gm); + + if (new_account_flag == 1) + login_log("- to ALLOW new users (with _F/_M)." RETCODE); + else + login_log("- to NOT ALLOW new users (with _F/_M)." RETCODE); + login_log("- with port: %d." RETCODE, login_port); + login_log("- with the accounts file name: '%s'." RETCODE, account_filename); + login_log("- with the GM accounts file name: '%s'." RETCODE, GM_account_filename); + if (gm_account_filename_check_timer == 0) + login_log("- to NOT check GM accounts file modifications." RETCODE); + else + login_log("- to check GM accounts file modifications every %d seconds." RETCODE, gm_account_filename_check_timer); + + // not necessary to log the 'login_log_filename', we are inside :) + + login_log("- with the unknown packets file name: '%s'." RETCODE, login_log_unknown_packets_filename); + if (save_unknown_packets) + login_log("- to SAVE all unkown packets." RETCODE); + else + login_log("- to SAVE only unkown packets sending by a char-server or a remote administration." RETCODE); + if (display_parse_login) + login_log("- to display normal parse packets on console." RETCODE); + else + login_log("- to NOT display normal parse packets on console." RETCODE); + if (display_parse_admin) + login_log("- to display administration parse packets on console." RETCODE); + else + login_log("- to NOT display administration parse packets on console." RETCODE); + if (display_parse_fromchar) + login_log("- to display char-server parse packets on console." RETCODE); + else + login_log("- to NOT display char-server parse packets on console." RETCODE); + + if (min_level_to_connect == 0) // 0: all players, 1-99 at least gm level x + login_log("- with no minimum level for connection." RETCODE); + else if (min_level_to_connect == 99) + login_log("- to accept only GM with level 99." RETCODE); + else + login_log("- to accept only GM with level %d or more." RETCODE, min_level_to_connect); + + if (add_to_unlimited_account) + login_log("- to authorize adjustment (with timeadd ladmin) on an unlimited account." RETCODE); + else + login_log("- to refuse adjustment (with timeadd ladmin) on an unlimited account. You must use timeset (ladmin command) before." RETCODE); + + if (start_limited_time < 0) + login_log("- to create new accounts with an unlimited time." RETCODE); + else if (start_limited_time == 0) + login_log("- to create new accounts with a limited time: time of creation." RETCODE); + else + login_log("- to create new accounts with a limited time: time of creation + %d second(s)." RETCODE, start_limited_time); + + if (check_ip_flag) + login_log("- with control of players IP between login-server and char-server." RETCODE); + else + login_log("- to not check players IP between login-server and char-server." RETCODE); + + if (access_order == ACO_DENY_ALLOW) { + if (access_denynum == 0) { + login_log("- with the IP security order: 'deny,allow' (allow if not deny). You refuse no IP." RETCODE); + } else if (access_denynum == 1 && access_deny[0] == '\0') { + login_log("- with the IP security order: 'deny,allow' (allow if not deny). You refuse ALL IP." RETCODE); + } else { + login_log("- with the IP security order: 'deny,allow' (allow if not deny). Refused IP are:" RETCODE); + for(i = 0; i < access_denynum; i++) + login_log(" %s" RETCODE, (char *)(access_deny + i * ACO_STRSIZE)); + } + } else if (access_order == ACO_ALLOW_DENY) { + if (access_allownum == 0) { + login_log("- with the IP security order: 'allow,deny' (deny if not allow). But, NO IP IS AUTHORISED!" RETCODE); + } else if (access_allownum == 1 && access_allow[0] == '\0') { + login_log("- with the IP security order: 'allow,deny' (deny if not allow). You authorise ALL IP." RETCODE); + } else { + login_log("- with the IP security order: 'allow,deny' (deny if not allow). Authorised IP are:" RETCODE); + for(i = 0; i < access_allownum; i++) + login_log(" %s" RETCODE, (char *)(access_allow + i * ACO_STRSIZE)); + } + } else { // ACO_MUTUAL_FAILTURE + login_log("- with the IP security order: 'mutual-failture' (allow if in the allow list and not in the deny list)." RETCODE); + if (access_allownum == 0) { + login_log(" But, NO IP IS AUTHORISED!" RETCODE); + } else if (access_denynum == 1 && access_deny[0] == '\0') { + login_log(" But, you refuse ALL IP!" RETCODE); + } else { + if (access_allownum == 1 && access_allow[0] == '\0') { + login_log(" You authorise ALL IP." RETCODE); + } else { + login_log(" Authorised IP are:" RETCODE); + for(i = 0; i < access_allownum; i++) + login_log(" %s" RETCODE, (char *)(access_allow + i * ACO_STRSIZE)); + } + login_log(" Refused IP are:" RETCODE); + for(i = 0; i < access_denynum; i++) + login_log(" %s" RETCODE, (char *)(access_deny + i * ACO_STRSIZE)); + } + } +} + +//-------------------------------------- +// Function called at exit of the server +//-------------------------------------- +void do_final(void) { + int i, fd; + + mmo_auth_sync(); + + free(auth_dat); + free(gm_account_db); + for (i = 0; i < MAX_SERVERS; i++) { + if ((fd = server_fd[i]) >= 0) + delete_session(fd); + } + delete_session(login_fd); + + login_log("----End of login-server (normal end with closing of all files)." RETCODE); +} + +//------------------------------ +// Main function of login-server +//------------------------------ +int do_init(int argc, char **argv) { + int i, j; + + // read login-server configuration + login_config_read((argc > 1) ? argv[1] : LOGIN_CONF_NAME); + display_conf_warnings(); // not in login_config_read, because we can use 'import' option, and display same message twice or more + save_config_in_log(); // not before, because log file name can be changed + login_lan_config_read((argc > 1) ? argv[1] : LAN_CONF_NAME); + + srand(time(NULL)); + + for(i = 0; i< AUTH_FIFO_SIZE; i++) + auth_fifo[i].delflag = 1; + for(i = 0; i < MAX_SERVERS; i++) + server_fd[i] = -1; + + gm_account_db = numdb_init(); + + read_gm_account(); + mmo_auth_init(); + set_termfunc(mmo_auth_sync); + set_defaultparse(parse_login); + login_fd = make_listen_port(login_port); + + add_timer_func_list(check_auth_sync, "check_auth_sync"); + + i = add_timer_interval(gettick() + 60000, check_auth_sync, 0, 0, 60000); // every 60 sec we check if we must save accounts file (only if necessary to save) + + if(anti_freeze_enable > 0) { + add_timer_func_list(char_anti_freeze_system, "char_anti_freeze_system"); + i = add_timer_interval(gettick() + 1000, char_anti_freeze_system, 0, 0, ANTI_FREEZE_INTERVAL * 1000); + } + + // add timer to check GM accounts file modification + j = gm_account_filename_check_timer; + if (j == 0) // if we would not to check, we check every 60 sec, just to have timer (if we change timer, is was not necessary to check if timer already exists) + j = 60; + add_timer_func_list(check_GM_file, "check_GM_file"); + i = add_timer_interval(gettick() + j * 1000, check_GM_file, 0, 0, j * 1000); // every x sec we check if gm file has been changed + + login_log("The login-server is ready (Server is listening on the port %d)." RETCODE, login_port); + + printf("The login-server is \033[1;32mready\033[0m (Server is listening on the port %d).\n\n", login_port); + + atexit(do_final); + return 0; +} + + diff --git a/misc/src/login/login.h b/misc/src/login/login.h new file mode 100644 index 0000000..3a8a6d5 --- /dev/null +++ b/misc/src/login/login.h @@ -0,0 +1,38 @@ +// $Id: login.h,v 1.1.1.1 2004/09/10 17:26:53 MagicalTux Exp $ +#ifndef _LOGIN_H_ +#define _LOGIN_H_ + +#define MAX_SERVERS 30 + +#define LOGIN_CONF_NAME "conf/login_athena.conf" +#define LAN_CONF_NAME "conf/lan_support.conf" +#define PASSWORDENC 3 // A definition is given when making an encryption password correspond. + // It is 1 at the time of passwordencrypt. + // It is made into 2 at the time of passwordencrypt2. + // When it is made 3, it corresponds to both. +#define START_ACCOUNT_NUM 2000000 +#define END_ACCOUNT_NUM 100000000 + +struct mmo_account { + char* userid; + char* passwd; + int passwdenc; + + long account_id; + long login_id1; + long login_id2; + long char_id; + char lastlogin[24]; + int sex; +}; + +struct mmo_char_server { + char name[20]; + long ip; + short port; + int users; + int maintenance; + int new; +}; + +#endif diff --git a/misc/src/login/md5calc.c b/misc/src/login/md5calc.c new file mode 100644 index 0000000..96bfc34 --- /dev/null +++ b/misc/src/login/md5calc.c @@ -0,0 +1,237 @@ +// $Id: md5calc.c,v 1.1.1.1 2004/09/10 17:26:54 MagicalTux Exp $ +/*********************************************************** + * md5 calculation algorithm + * + * The source code referred to the following URL. + * http://www.geocities.co.jp/SiliconValley-Oakland/8878/lab17/lab17.html + * + ***********************************************************/ + +#include "md5calc.h" +#include +#include + +#ifndef UINT_MAX +#define UINT_MAX 4294967295U +#endif + +// Global variable +static unsigned int *pX; + +// Stirng Table +static const unsigned int T[] = { + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, //0 + 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, //4 + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, //8 + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, //12 + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, //16 + 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, //20 + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, //24 + 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, //28 + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, //32 + 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, //36 + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, //40 + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, //44 + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, //48 + 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, //52 + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, //56 + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 //60 +}; + +// ROTATE_LEFT The left is made to rotate x [ n-bit ]. This is diverted as it is from RFC. +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +// The function used for other calculation +static unsigned int F(unsigned int X, unsigned int Y, unsigned int Z) +{ + return (X & Y) | (~X & Z); +} +static unsigned int G(unsigned int X, unsigned int Y, unsigned int Z) +{ + return (X & Z) | (Y & ~Z); +} +static unsigned int H(unsigned int X, unsigned int Y, unsigned int Z) +{ + return X ^ Y ^ Z; +} +static unsigned int I(unsigned int X, unsigned int Y, unsigned int Z) +{ + return Y ^ (X | ~Z); +} + +static unsigned int Round(unsigned int a, unsigned int b, unsigned int FGHI, + unsigned int k, unsigned int s, unsigned int i) +{ + return b + ROTATE_LEFT(a + FGHI + pX[k] + T[i], s); +} + +static void Round1(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, F(b,c,d), k, s, i); +} +static void Round2(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, G(b,c,d), k, s, i); +} +static void Round3(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, H(b,c,d), k, s, i); +} +static void Round4(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, I(b,c,d), k, s, i); +} + +static void MD5_Round_Calculate(const unsigned char *block, + unsigned int *A2, unsigned int *B2, unsigned int *C2, unsigned int *D2) +{ + //create X It is since it is required. + unsigned int X[16]; //512bit 64byte + int j,k; + + //Save A as AA, B as BB, C as CC, and and D as DD (saving of A, B, C, and D) + unsigned int A=*A2, B=*B2, C=*C2, D=*D2; + unsigned int AA = A,BB = B,CC = C,DD = D; + + //It is a large region variable reluctantly because of calculation of a round. . . for Round1...4 + pX = X; + + //Copy block(padding_message) i into X + for (j=0,k=0; j<64; j+=4,k++) + X[k] = ( (unsigned int )block[j] ) // 8byte*4 -> 32byte conversion + | ( ((unsigned int )block[j+1]) << 8 ) // A function called Decode as used in the field of RFC + | ( ((unsigned int )block[j+2]) << 16 ) + | ( ((unsigned int )block[j+3]) << 24 ); + + + //Round 1 + Round1(&A,B,C,D, 0, 7, 0); Round1(&D,A,B,C, 1, 12, 1); Round1(&C,D,A,B, 2, 17, 2); Round1(&B,C,D,A, 3, 22, 3); + Round1(&A,B,C,D, 4, 7, 4); Round1(&D,A,B,C, 5, 12, 5); Round1(&C,D,A,B, 6, 17, 6); Round1(&B,C,D,A, 7, 22, 7); + Round1(&A,B,C,D, 8, 7, 8); Round1(&D,A,B,C, 9, 12, 9); Round1(&C,D,A,B, 10, 17, 10); Round1(&B,C,D,A, 11, 22, 11); + Round1(&A,B,C,D, 12, 7, 12); Round1(&D,A,B,C, 13, 12, 13); Round1(&C,D,A,B, 14, 17, 14); Round1(&B,C,D,A, 15, 22, 15); + + //Round 2 + Round2(&A,B,C,D, 1, 5, 16); Round2(&D,A,B,C, 6, 9, 17); Round2(&C,D,A,B, 11, 14, 18); Round2(&B,C,D,A, 0, 20, 19); + Round2(&A,B,C,D, 5, 5, 20); Round2(&D,A,B,C, 10, 9, 21); Round2(&C,D,A,B, 15, 14, 22); Round2(&B,C,D,A, 4, 20, 23); + Round2(&A,B,C,D, 9, 5, 24); Round2(&D,A,B,C, 14, 9, 25); Round2(&C,D,A,B, 3, 14, 26); Round2(&B,C,D,A, 8, 20, 27); + Round2(&A,B,C,D, 13, 5, 28); Round2(&D,A,B,C, 2, 9, 29); Round2(&C,D,A,B, 7, 14, 30); Round2(&B,C,D,A, 12, 20, 31); + + //Round 3 + Round3(&A,B,C,D, 5, 4, 32); Round3(&D,A,B,C, 8, 11, 33); Round3(&C,D,A,B, 11, 16, 34); Round3(&B,C,D,A, 14, 23, 35); + Round3(&A,B,C,D, 1, 4, 36); Round3(&D,A,B,C, 4, 11, 37); Round3(&C,D,A,B, 7, 16, 38); Round3(&B,C,D,A, 10, 23, 39); + Round3(&A,B,C,D, 13, 4, 40); Round3(&D,A,B,C, 0, 11, 41); Round3(&C,D,A,B, 3, 16, 42); Round3(&B,C,D,A, 6, 23, 43); + Round3(&A,B,C,D, 9, 4, 44); Round3(&D,A,B,C, 12, 11, 45); Round3(&C,D,A,B, 15, 16, 46); Round3(&B,C,D,A, 2, 23, 47); + + //Round 4 + Round4(&A,B,C,D, 0, 6, 48); Round4(&D,A,B,C, 7, 10, 49); Round4(&C,D,A,B, 14, 15, 50); Round4(&B,C,D,A, 5, 21, 51); + Round4(&A,B,C,D, 12, 6, 52); Round4(&D,A,B,C, 3, 10, 53); Round4(&C,D,A,B, 10, 15, 54); Round4(&B,C,D,A, 1, 21, 55); + Round4(&A,B,C,D, 8, 6, 56); Round4(&D,A,B,C, 15, 10, 57); Round4(&C,D,A,B, 6, 15, 58); Round4(&B,C,D,A, 13, 21, 59); + Round4(&A,B,C,D, 4, 6, 60); Round4(&D,A,B,C, 11, 10, 61); Round4(&C,D,A,B, 2, 15, 62); Round4(&B,C,D,A, 9, 21, 63); + + // Then perform the following additions. (let's add) + *A2 = A + AA; + *B2 = B + BB; + *C2 = C + CC; + *D2 = D + DD; + + //The clearance of confidential information + memset(pX, 0, sizeof(X)); +} + +//------------------------------------------------------------------- +// The function for the exteriors + +/** output is the coded binary in the character sequence which wants to code string. */ +void MD5_String2binary(const char * string, char * output) +{ +//var + /*8bit*/ + unsigned char padding_message[64]; //Extended message 512bit 64byte + unsigned char *pstring; //The position of string in the present scanning notes is held. + +// unsigned char digest[16]; + /*32bit*/ + unsigned int string_byte_len, //The byte chief of string is held. + string_bit_len, //The bit length of string is held. + copy_len, //The number of bytes which is used by 1-3 and which remained + msg_digest[4]; //Message digest 128bit 4byte + unsigned int *A = &msg_digest[0], //The message digest in accordance with RFC (reference) + *B = &msg_digest[1], + *C = &msg_digest[2], + *D = &msg_digest[3]; + int i; + +//prog + //Step 3.Initialize MD Buffer (although it is the initialization; step 3 of A, B, C, and D -- unavoidable -- a head) + *A = 0x67452301; + *B = 0xefcdab89; + *C = 0x98badcfe; + *D = 0x10325476; + + //Step 1.Append Padding Bits (extension of a mark bit) + //1-1 + string_byte_len = strlen(string); //The byte chief of a character sequence is acquired. + pstring = (unsigned char *)string; //The position of the present character sequence is set. + + //1-2 Repeat calculation until length becomes less than 64 bytes. + for (i=string_byte_len; 64<=i; i-=64,pstring+=64) + MD5_Round_Calculate(pstring, A,B,C,D); + + //1-3 + copy_len = string_byte_len % 64; //The number of bytes which remained is computed. + strncpy((char *)padding_message, (char *)pstring, copy_len); //A message is copied to an extended bit sequence. + memset(padding_message+copy_len, 0, 64 - copy_len); //It buries by 0 until it becomes extended bit length. + padding_message[copy_len] |= 0x80; //The next of a message is 1. + + //1-4 + //If 56 bytes or more (less than 64 bytes) of remainder becomes, it will calculate by extending to 64 bytes. + if (56 <= copy_len) { + MD5_Round_Calculate(padding_message, A,B,C,D); + memset(padding_message, 0, 56); //56 bytes is newly fill uped with 0. + } + + + //Step 2.Append Length (the information on length is added) + string_bit_len = string_byte_len * 8; //From the byte chief to bit length (32 bytes of low rank) + memcpy(&padding_message[56], &string_bit_len, 4); //32 bytes of low rank is set. + + //When bit length cannot be expressed in 32 bytes of low rank, it is a beam raising to a higher rank. + if (UINT_MAX / 8 < string_byte_len) { + unsigned int high = (string_byte_len - UINT_MAX / 8) * 8; + memcpy(&padding_message[60], &high, 4); + } else + memset(&padding_message[60], 0, 4); //In this case, it is good for a higher rank at 0. + + //Step 4.Process Message in 16-Word Blocks (calculation of MD5) + MD5_Round_Calculate(padding_message, A,B,C,D); + + + //Step 5.Output (output) + memcpy(output,msg_digest,16); +// memcpy (digest, msg_digest, and 16); //8 byte*4 < - 32byte conversion A function called Encode as used in the field of RFC +/* sprintf(output, + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", + digest[ 0], digest[ 1], digest[ 2], digest[ 3], + digest[ 4], digest[ 5], digest[ 6], digest[ 7], + digest[ 8], digest[ 9], digest[10], digest[11], + digest[12], digest[13], digest[14], digest[15]);*/ +} + +/** output is the coded character sequence in the character sequence which wants to code string. */ +void MD5_String(const char * string, char * output) +{ + unsigned char digest[16]; + + MD5_String2binary(string,digest); + sprintf(output, + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", + digest[ 0], digest[ 1], digest[ 2], digest[ 3], + digest[ 4], digest[ 5], digest[ 6], digest[ 7], + digest[ 8], digest[ 9], digest[10], digest[11], + digest[12], digest[13], digest[14], digest[15]); +} + diff --git a/misc/src/login/md5calc.h b/misc/src/login/md5calc.h new file mode 100644 index 0000000..9137b5b --- /dev/null +++ b/misc/src/login/md5calc.h @@ -0,0 +1,8 @@ +// $Id: md5calc.h,v 1.1.1.1 2004/09/10 17:26:54 MagicalTux Exp $ +#ifndef _MD5CALC_H_ +#define _MD5CALC_H_ + +void MD5_String(const char * string, char * output); +void MD5_String2binary(const char * string, char * output); + +#endif diff --git a/misc/src/login_sql/GNUmakefile b/misc/src/login_sql/GNUmakefile new file mode 100644 index 0000000..c245b79 --- /dev/null +++ b/misc/src/login_sql/GNUmakefile @@ -0,0 +1,17 @@ +all: login-server_sql +sql: login-server_sql + +shared_libs=all +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o ../common/malloc.o +COMMON_H = ../common/core.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/version.h ../common/db.h ../common/malloc.h + +login-server_sql: login.o md5calc.o strlib.o $(COMMON_OBJ) + $(CC) -o ../../$@ $^ $(LIB_S) + +login.o: login.c login.h md5calc.h strlib.h $(COMMON_H) +md5calc.o: md5calc.c md5calc.h +strlib.o: strlib.c strlib.h + +clean: + rm -f *.o ../../login-server_sql + diff --git a/misc/src/login_sql/Makefile b/misc/src/login_sql/Makefile new file mode 100644 index 0000000..d1f2734 --- /dev/null +++ b/misc/src/login_sql/Makefile @@ -0,0 +1,17 @@ +all: login-server_sql +sql: login-server_sql + +shared_libs=all +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o ../common/malloc.o +COMMON_H = ../common/core.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/version.h ../common/db.h ../common/malloc.h + +login-server_sql: login.o md5calc.o strlib.o $(COMMON_OBJ) + $(CC) -o ../../$@ $^ $(LIB_S) + +login.o: login.c login.h md5calc.h strlib.h $(COMMON_H) +md5calc.o: md5calc.c md5calc.h +strlib.o: strlib.c strlib.h + +clean: + rm -f *.o ../../login-server_sql + diff --git a/misc/src/login_sql/login.c b/misc/src/login_sql/login.c new file mode 100644 index 0000000..2fab051 --- /dev/null +++ b/misc/src/login_sql/login.c @@ -0,0 +1,1682 @@ +// $Id: login.c,v 1.6 2004/09/19 21:12:07 Valaris Exp $ +// original : login2.c 2003/01/28 02:29:17 Rev.1.1.1.1 +// txt version 1.100 + +#include + +#ifdef LCCWIN32 +#include +#pragma lib +#else +#include +#include +#include +#include +#include +#endif + +#include +#include +#include +#include +#include +#include +#include // for stat/lstat/fstat +#include +#include +#include +#include +#include + +//add include for DBMS(mysql) +#include + +#include "strlib.h" +#include "timer.h" +/* +#include "timer.h" +#include "core.h" +#include "socket.h" +#include "login.h" +#include "mmo.h" +#include "version.h" +#include "db.h" +*/ + +#include "../common/core.h" +#include "../common/socket.h" +#include "login.h" +#include "../common/mmo.h" +#include "../common/version.h" +#include "../common/db.h" +#include "../common/timer.h" + +#ifdef PASSWORDENC +#include "md5calc.h" +#endif + +#ifdef MEMWATCH +#include "memwatch.h" +#endif + +#define J_MAX_MALLOC_SIZE 65535 + +//----------------------------------------------------- +// global variable +//----------------------------------------------------- +int account_id_count = START_ACCOUNT_NUM; +int server_num; +int new_account_flag = 0; +int login_port = 6900; +char lan_char_ip[128]; // Lan char ip added by kashy +int subnetmaski[4]; // Subnetmask added by kashy + +struct mmo_char_server server[MAX_SERVERS]; +int server_fd[MAX_SERVERS]; +int server_freezeflag[MAX_SERVERS]; // Char-server anti-freeze system. Counter. 5 ok, 4...0 freezed +int anti_freeze_enable = 0; +int ANTI_FREEZE_INTERVAL = 15; + +int login_fd; + +char date_format[32] = "%Y-%m-%d %H:%M:%S"; +int auth_num = 0, auth_max = 0; + +int min_level_to_connect = 0; // minimum level of player/GM (0: player, 1-99: gm) to connect on the server +int check_ip_flag = 1; // It's to check IP of a player between login-server and char-server (part of anti-hacking system) + +MYSQL mysql_handle; + +int ipban = 1; +int dynamic_account_ban = 1; +int dynamic_account_ban_class = 0; +int dynamic_pass_failure_ban = 1; +int dynamic_pass_failure_ban_time = 5; +int dynamic_pass_failure_ban_how_many = 3; +int dynamic_pass_failure_ban_how_long = 60; + +int login_server_port = 3306; +char login_server_ip[32] = "127.0.0.1"; +char login_server_id[32] = "ragnarok"; +char login_server_pw[32] = "ragnarok"; +char login_server_db[32] = "ragnarok"; +int use_md5_passwds = 0; +char login_db[256] = "login"; +char loginlog_db[256] = "loginlog"; + +// added to help out custom login tables, without having to recompile +// source so options are kept in the login_athena.conf or the inter_athena.conf +char login_db_account_id[256] = "account_id"; +char login_db_userid[256] = "userid"; +char login_db_user_pass[256] = "user_pass"; +char login_db_level[256] = "level"; + +char tmpsql[65535], tmp_sql[65535]; + +//----------------------------------------------------- + +#define AUTH_FIFO_SIZE 256 +struct { + int account_id,login_id1,login_id2; + int ip,sex,delflag; +} auth_fifo[AUTH_FIFO_SIZE]; + +int auth_fifo_pos = 0; + + +//----------------------------------------------------- + +static char md5key[20], md5keylen = 16; + +//----------------------------------------------------- +// check user level +//----------------------------------------------------- + +int isGM(int account_id) { + int level; + + MYSQL_RES* sql_res; + MYSQL_ROW sql_row; + level = 0; + sprintf(tmpsql,"SELECT `%s` FROM `%s` WHERE `%s`='%d'", login_db_level, login_db, login_db_account_id, account_id); + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error (select GM Level to Memory)- %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); + level = atoi(sql_row[0]); + if (level > 99) + level = 99; + } + + if (level == 0) { + return 0; + //not GM + } + + mysql_free_result(sql_res); + + return level; +} + +//----------------------------------------------------- +// Function to suppress control characters in a string. +//----------------------------------------------------- +int remove_control_chars(unsigned char *str) { + int i; + int change = 0; + + for(i = 0; str[i]; i++) { + if (str[i] < 32) { + str[i] = '_'; + change = 1; + } + } + + return change; +} + +//--------------------------------------------------- +// E-mail check: return 0 (not correct) or 1 (valid). +//--------------------------------------------------- +int e_mail_check(unsigned char *email) { + char ch; + unsigned char* last_arobas; + + // athena limits + if (strlen(email) < 3 || strlen(email) > 39) + return 0; + + // part of RFC limits (official reference of e-mail description) + if (strchr(email, '@') == NULL || email[strlen(email)-1] == '@') + return 0; + + if (email[strlen(email)-1] == '.') + return 0; + + last_arobas = strrchr(email, '@'); + + if (strstr(last_arobas, "@.") != NULL || + strstr(last_arobas, "..") != NULL) + return 0; + + for(ch = 1; ch < 32; ch++) { + if (strchr(last_arobas, ch) != NULL) { + return 0; + break; + } + } + + if (strchr(last_arobas, ' ') != NULL || + strchr(last_arobas, ';') != NULL) + return 0; + + // all correct + return 1; +} + +//----------------------------------------------------- +// Read Account database - mysql db +//----------------------------------------------------- +int mmo_auth_sqldb_init(void) { + + printf("Login server init....\n"); + + // memory initialize + printf("memory initialize....\n"); + + mysql_init(&mysql_handle); + + // DB connection start + printf("Connect Login Database Server....\n"); + if (!mysql_real_connect(&mysql_handle, login_server_ip, login_server_id, login_server_pw, + login_server_db, login_server_port, (char *)NULL, 0)) { + // pointer check + printf("%s\n", mysql_error(&mysql_handle)); + exit(1); + } else { + printf("connect success!\n"); + } + + sprintf(tmpsql, "INSERT INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '', 'lserver', '100','login server started')", loginlog_db); + + //query + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + return 0; +} + +//----------------------------------------------------- +// DB server connect check +//----------------------------------------------------- +void mmo_auth_sqldb_sync(void) { + // db connect check? or close? + // ping pong DB server -if losted? then connect try. else crash. +} + +//----------------------------------------------------- +// close DB +//----------------------------------------------------- +void mmo_db_close(void) { + + //set log. + sprintf(tmpsql,"INSERT INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '', 'lserver','100', 'login server shutdown')", loginlog_db); + + //query + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + //delete all server status + sprintf(tmpsql,"DELETE FROM `sstatus`"); + //query + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + mysql_close(&mysql_handle); + printf("close DB connect....\n"); + + int i, fd; + + for (i = 0; i < MAX_SERVERS; i++) { + if ((fd = server_fd[i]) >= 0) + delete_session(fd); + } + delete_session(login_fd); +} + +//----------------------------------------------------- +// Make new account +//----------------------------------------------------- +int mmo_auth_sqldb_new(struct mmo_account* account,const char *tmpstr, char sex) { + //no need on DB version + + printf("Request new account.... - not support on this version\n"); + + return 0; +} + +//----------------------------------------------------- +// Make new account +//----------------------------------------------------- +int mmo_auth_new(struct mmo_account* account, const char *tmpstr, char sex) { + + return 0; +} + +#ifdef LCCWIN32 +extern void gettimeofday(struct timeval *t, struct timezone *dummy); +#endif + +//----------------------------------------------------- +// Auth +//----------------------------------------------------- +int mmo_auth( struct mmo_account* account , int fd){ + struct timeval tv; + time_t ban_until_time; + char tmpstr[256]; + char t_uid[256], t_pass[256]; + char user_password[256]; + + MYSQL_RES* sql_res ; + MYSQL_ROW sql_row ; + //int sql_fields, sql_cnt; + char md5str[64], md5bin[32]; + + char ip[16]; + + unsigned char *sin_addr = (unsigned char *)&session[fd]->client_addr.sin_addr; + + printf ("auth start...\n"); + + sprintf(ip, "%d.%d.%d.%d", sin_addr[0], sin_addr[1], sin_addr[2], sin_addr[3]); + + // auth start : time seed + gettimeofday(&tv, NULL); + strftime(tmpstr, 24, "%Y-%m-%d %H:%M:%S",localtime(&(tv.tv_sec))); + sprintf(tmpstr+19, ".%03d", (int)tv.tv_usec/1000); + + jstrescapecpy(t_uid,account->userid); + jstrescapecpy(t_pass, account->passwd); + + // make query + sprintf(tmpsql, "SELECT `%s`,`%s`,`%s`,`lastlogin`,`logincount`,`sex`,`connect_until`,`last_ip`,`ban_until`,`state`,`%s`" + " FROM `%s` WHERE `%s`='%s'", login_db_account_id, login_db_userid, login_db_user_pass, login_db_level, login_db, login_db_userid, t_uid); + //login {0-account_id/1-userid/2-user_pass/3-lastlogin/4-logincount/5-sex/6-connect_untl/7-last_ip/8-ban_until/9-state} + + // query + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); //row fetching + if (!sql_row) { + //there's no id. + printf ("auth failed no account %s %s %s\n", tmpstr, account->userid, account->passwd); + mysql_free_result(sql_res); + return 0; + } + } else { + printf("mmo_auth DB result error ! \n"); + return 0; + } + // Documented by CLOWNISIUS || LLRO || Gunstar lead this one with me + // IF changed to diferent returns~ you get diferent responses from your msgstringtable.txt + //Ireturn 2 == line 9 + //Ireturn 5 == line 311 + //Ireturn 6 == line 450 + //Ireturn 7 == line 440 + //Ireturn 8 == line 682 + //Ireturn 9 == line 704 + //Ireturn 10 == line 705 + //Ireturn 11 == line 706 + //Ireturn 12 == line 707 + //Ireturn 13 == line 708 + //Ireturn 14 == line 709 + //Ireturn 15 == line 710 + //Ireturn -1 == line 010 + // Check status + { + if (atoi(sql_row[9]) == -3) { + //id is banned + mysql_free_result(sql_res); + return -3; + } else if (atoi(sql_row[9]) == -2) { //dynamic ban + //id is banned + mysql_free_result(sql_res); + //add IP list. + return -2; + } + + if (use_md5_passwds) { + MD5_String(account->passwd,user_password); + } else { + jstrescapecpy(user_password, account->passwd); + } + printf("account id ok encval:%d\n",account->passwdenc); + int encpasswdok = 0; +#ifdef PASSWORDENC + if (account->passwdenc > 0) { + printf ("start md5calc..\n"); + int j = account->passwdenc; + if (j > 2) + j = 1; + do { + if (j == 1) { + sprintf(md5str, "%s%s", md5key,sql_row[2]); + } else if (j == 2) { + sprintf(md5str, "%s%s", sql_row[2], md5key); + } else + md5str[0] = 0; + printf("j:%d mdstr:%s\n", j, md5str); + MD5_String2binary(md5str, md5bin); + encpasswdok = (memcmp(user_password, md5bin, 16) == 0); + } while (j < 2 && !encpasswdok && (j++) != account->passwdenc); + //printf("key[%s] md5 [%s] ", md5key, md5); + printf("client [%s] accountpass [%s]\n", user_password, sql_row[2]); + printf ("end md5calc..\n"); + } +#endif + if ((strcmp(user_password, sql_row[2]) && !encpasswdok)) { + if (account->passwdenc == 0) { + printf ("auth failed pass error %s %s %s" RETCODE, tmpstr, account->userid, user_password); +#ifdef PASSWORDENC + } else { + char logbuf[1024], *p = logbuf; + int j; + p += sprintf(p, "auth failed pass error %s %s recv-md5[", tmpstr, account->userid); + for(j = 0; j < 16; j++) + p += sprintf(p, "%02x", ((unsigned char *)user_password)[j]); + p += sprintf(p, "] calc-md5["); + for(j = 0; j < 16; j++) + p += sprintf(p, "%02x", ((unsigned char *)md5bin)[j]); + p += sprintf(p, "] md5key["); + for(j = 0; j < md5keylen; j++) + p += sprintf(p, "%02x", ((unsigned char *)md5key)[j]); + p += sprintf(p, "]" RETCODE); + printf("%s\n", p); +#endif + } + return 1; + } + printf("auth ok %s %s" RETCODE, tmpstr, account->userid); + } + + if (atoi(sql_row[9])) { + switch(atoi(sql_row[9])) { // packet 0x006a value + 1 + case 1: // 0 = Unregistered ID + case 2: // 1 = Incorrect Password + case 3: // 2 = This ID is expired + case 4: // 3 = Rejected from Server + case 5: // 4 = You have been blocked by the GM Team + case 6: // 5 = Your Game's EXE file is not the latest version + case 7: // 6 = Your are Prohibited to log in until %s + case 8: // 7 = Server is jammed due to over populated + case 9: // 8 = No MSG (actually, all states after 9 except 99 are No MSG, use only this) + case 100: // 99 = This ID has been totally erased + printf("Auth Error #%d\n", atoi(sql_row[9])); + return atoi(sql_row[9]) - 1; + break; + default: + return 99; // 99 = ID has been totally erased + break; + } + } + +/* +// do not remove this section. this is meant for future, and current forums usage +// as a login manager and CP for login server. [CLOWNISIUS] + if (atoi(sql_row[10]) == 1) { + return 4; + } + + if (atoi(sql_row[10]) >= 5) { + switch(atoi(sql_row[10])) { + case 5: + return 5; + break; + case 6: + return 7; + break; + case 7: + return 9; + break; + case 8: + return 10; + break; + case 9: + return 11; + break; + default: + return 10; + break; + } + } +*/ + ban_until_time = atol(sql_row[8]); + + //login {0-account_id/1-userid/2-user_pass/3-lastlogin/4-logincount/5-sex/6-connect_untl/7-last_ip/8-ban_until/9-state} + if (ban_until_time != 0) { // if account is banned + strftime(tmpstr, 20, date_format, localtime(&ban_until_time)); + tmpstr[19] = '\0'; + if (ban_until_time > time(NULL)) { // always banned + return 6; // 6 = Your are Prohibited to log in until %s + } else { // ban is finished + // reset the ban time + sprintf(tmpsql, "UPDATE `%s` SET `ban_until`='0' WHERE `%s`='%s'", login_db, login_db_userid, t_uid); + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + } + } + + if (atol(sql_row[6]) != 0 && atol(sql_row[6]) < time(NULL)) { + return 2; // 2 = This ID is expired + } + + account->account_id = atoi(sql_row[0]); + account->login_id1 = rand(); + account->login_id2 = rand(); + memcpy(tmpstr, sql_row[3], 19); + memcpy(account->lastlogin, tmpstr, 24); + account->sex = sql_row[5][0] == 'S' ? 2 : sql_row[5][0]=='M'; + + sprintf(tmpsql, "UPDATE `%s` SET `lastlogin` = NOW(), `logincount`=`logincount` +1, `last_ip`='%s' WHERE `%s` = '%s'", + login_db, ip, login_db_userid, sql_row[1]); + mysql_free_result(sql_res) ; //resource free + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + return -1; +} + +// Send to char +int charif_sendallwos(int sfd, unsigned char *buf, unsigned int len) { + int i, c; + int fd; + + c = 0; + for(i = 0; i < MAX_SERVERS; i++) { + if ((fd = server_fd[i]) > 0 && fd != sfd) { + memcpy(WFIFOP(fd,0), buf, len); + WFIFOSET(fd,len); + c++; + } + } + + return c; +} + +//-------------------------------- +// Char-server anti-freeze system +//-------------------------------- +int char_anti_freeze_system(int tid, unsigned int tick, int id, int data) { + int i; + + for(i = 0; i < MAX_SERVERS; i++) { + if (server_fd[i] >= 0) {// if char-server is online +// printf("char_anti_freeze_system: server #%d '%s', flag: %d.\n", i, server[i].name, server_freezeflag[i]); + if (server_freezeflag[i]-- < 1) {// Char-server anti-freeze system. Counter. 5 ok, 4...0 freezed + session[server_fd[i]]->eof = 1; + } + } + } + + return 0; +} + +//----------------------------------------------------- +// char-server packet parse +//----------------------------------------------------- +int parse_fromchar(int fd){ + int i, id; + MYSQL_RES* sql_res; + MYSQL_ROW sql_row = NULL; + + unsigned char *p = (unsigned char *) &session[fd]->client_addr.sin_addr; + char ip[16]; + + sprintf(ip, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); + + for(id = 0; id < MAX_SERVERS; id++) + if (server_fd[id] == fd) + break; + + if (id == MAX_SERVERS || session[fd]->eof) { + if (id < MAX_SERVERS) { + printf("Char-server '%s' has disconnected.\n", server[id].name); + server_fd[id] = -1; + memset(&server[id], 0, sizeof(struct mmo_char_server)); + // server delete + sprintf(tmpsql, "DELETE FROM `sstatus` WHERE `index`='%d'", id); + // query + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + } + close(fd); + delete_session(fd); + return 0; + } + + while(RFIFOREST(fd) >= 2) { +// printf("char_parse: %d %d packet case=%x\n", fd, RFIFOREST(fd), RFIFOW(fd, 0)); + + switch (RFIFOW(fd,0)) { + case 0x2712: + if (RFIFOREST(fd) < 19) + return 0; + { + int account_id; + account_id = RFIFOL(fd,2); // speed up + for(i=0;i %d\n", i); + break; + } + } + + if (i != AUTH_FIFO_SIZE) { // send account_reg + int p; + time_t connect_until_time = 0; + char email[40] = ""; + account_id=RFIFOL(fd,2); + sprintf(tmpsql, "SELECT `email`,`connect_until` FROM `%s` WHERE `%s`='%d'", login_db, login_db_account_id, account_id); + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); + connect_until_time = atol(sql_row[1]); + strcpy(email, sql_row[0]); + } + mysql_free_result(sql_res); + if (account_id > 0) { + sprintf(tmpsql, "SELECT `str`,`value` FROM `global_reg_value` WHERE `type`='1' AND `account_id`='%d'",account_id); + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res) { + WFIFOW(fd,0) = 0x2729; + WFIFOL(fd,4) = account_id; + for(p = 8; (sql_row = mysql_fetch_row(sql_res));p+=36){ + memcpy(WFIFOP(fd,p), sql_row[0], 32); + WFIFOL(fd,p+32) = atoi(sql_row[1]); + } + WFIFOW(fd,2) = p; + WFIFOSET(fd,p); + //printf("account_reg2 send : login->char (auth fifo)\n"); + WFIFOW(fd,0) = 0x2713; + WFIFOL(fd,2) = account_id; + WFIFOB(fd,6) = 0; + memcpy(WFIFOP(fd, 7), email, 40); + WFIFOL(fd,47) = (unsigned long) connect_until_time; + WFIFOSET(fd,51); + } + mysql_free_result(sql_res); + } + } else { + WFIFOW(fd,0) = 0x2713; + WFIFOL(fd,2) = account_id; + WFIFOB(fd,6) = 1; + WFIFOSET(fd,51); + } + } + RFIFOSKIP(fd,19); + break; + + case 0x2714: + if (RFIFOREST(fd) < 6) + return 0; + // how many users on world? (update) + if (server[id].users != RFIFOL(fd,2)) + printf("set users %s : %d\n", server[id].name, RFIFOL(fd,2)); + server[id].users = RFIFOL(fd,2); + if(anti_freeze_enable) + server_freezeflag[id] = 5; // Char anti-freeze system. Counter. 5 ok, 4...0 freezed + + sprintf(tmpsql,"UPDATE `sstatus` SET `user` = '%d' WHERE `index` = '%d'", server[id].users, id); + // query + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + RFIFOSKIP(fd,6); + break; + + // We receive an e-mail/limited time request, because a player comes back from a map-server to the char-server + case 0x2716: + if (RFIFOREST(fd) < 6) + return 0; + { + int account_id; + time_t connect_until_time = 0; + char email[40] = ""; + account_id=RFIFOL(fd,2); + sprintf(tmpsql,"SELECT `email`,`connect_until` FROM `%s` WHERE `%s`='%d'",login_db, login_db_account_id, account_id); + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); + connect_until_time = atol(sql_row[1]); + strcpy(email, sql_row[0]); + } + mysql_free_result(sql_res); + //printf("parse_fromchar: E-mail/limited time request from '%s' server (concerned account: %d)\n", server[id].name, RFIFOL(fd,2)); + WFIFOW(fd,0) = 0x2717; + WFIFOL(fd,2) = RFIFOL(fd,2); + memcpy(WFIFOP(fd, 6), email, 40); + WFIFOL(fd,46) = (unsigned long) connect_until_time; + WFIFOSET(fd,50); + } + RFIFOSKIP(fd,6); + break; + + case 0x2720: // GM + if (RFIFOREST(fd) < 4) + return 0; + if (RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + //oldacc = RFIFOL(fd,4); + printf("change GM isn't support in this login server version.\n"); + printf("change GM error 0 %s\n", RFIFOP(fd, 8)); + + RFIFOSKIP(fd, RFIFOW(fd, 2)); + WFIFOW(fd, 0) = 0x2721; + WFIFOL(fd, 2) = RFIFOL(fd,4); // oldacc; + WFIFOL(fd, 6) = 0; // newacc; + WFIFOSET(fd, 10); + return 0; + + // Map server send information to change an email of an account via char-server + case 0x2722: // 0x2722 .L .40B .40B + if (RFIFOREST(fd) < 86) + return 0; + { + int acc; + char actual_email[40], new_email[40]; + acc = RFIFOL(fd,2); + memcpy(actual_email, RFIFOP(fd,6), 40); + memcpy(new_email, RFIFOP(fd,46), 40); + if (e_mail_check(actual_email) == 0) + printf("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command), but actual email is invalid (account: %d, ip: %s)" RETCODE, + server[id].name, acc, ip); + else if (e_mail_check(new_email) == 0) + printf("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command) with a invalid new e-mail (account: %d, ip: %s)" RETCODE, + server[id].name, acc, ip); + else if (strcmpi(new_email, "a@a.com") == 0) + printf("Char-server '%s': Attempt to modify an e-mail on an account (@email GM command) with a default e-mail (account: %d, ip: %s)" RETCODE, + server[id].name, acc, ip); + else { + sprintf(tmpsql, "SELECT `%s`,`email` FROM `%s` WHERE `%s` = '%d'", login_db_userid, login_db, login_db_account_id, acc); + if (mysql_query(&mysql_handle, tmpsql)) + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); //row fetching + + if (strcmpi(sql_row[1], actual_email) == 0) { + sprintf(tmpsql, "UPDATE `%s` SET `email` = '%s' WHERE `%s` = '%d'", login_db, new_email, login_db_account_id, acc); + // query + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + printf("Char-server '%s': Modify an e-mail on an account (@email GM command) (account: %d (%s), new e-mail: %s, ip: %s)." RETCODE, + server[id].name, acc, sql_row[0], actual_email, ip); + } + } + + } + } + RFIFOSKIP(fd, 86); + break; + + case 0x2724: // Receiving of map-server via char-server a status change resquest (by Yor) + if (RFIFOREST(fd) < 10) + return 0; + { + int acc, statut; + acc = RFIFOL(fd,2); + statut = RFIFOL(fd,6); + sprintf(tmpsql, "SELECT `state` FROM `%s` WHERE `%s` = '%d'", login_db, login_db_account_id, acc); + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); // row fetching + } + if (atoi(sql_row[0]) != statut && statut != 0) { + unsigned char buf[16]; + WBUFW(buf,0) = 0x2731; + WBUFL(buf,2) = acc; + WBUFB(buf,6) = 0; // 0: change of statut, 1: ban + WBUFL(buf,7) = statut; // status or final date of a banishment + charif_sendallwos(-1, buf, 11); + } + sprintf(tmpsql,"UPDATE `%s` SET `state` = '%d' WHERE `%s` = '%d'", login_db, statut,login_db_account_id,acc); + //query + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + RFIFOSKIP(fd,10); + } + return 0; + + case 0x2725: // Receiving of map-server via char-server a ban resquest (by Yor) + if (RFIFOREST(fd) < 18) + return 0; + { + int acc; + struct tm *tmtime; + time_t timestamp, tmptime; + acc = RFIFOL(fd,2); + sprintf(tmpsql, "SELECT `ban_until` FROM `%s` WHERE `%s` = '%d'",login_db,login_db_account_id,acc); + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle); + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); // row fetching + } + tmptime = atol(sql_row[0]); + if (tmptime == 0 || tmptime < time(NULL)) + timestamp = time(NULL); + else + timestamp = tmptime; + tmtime = localtime(×tamp); + tmtime->tm_year = tmtime->tm_year + (short)RFIFOW(fd,6); + tmtime->tm_mon = tmtime->tm_mon + (short)RFIFOW(fd,8); + tmtime->tm_mday = tmtime->tm_mday + (short)RFIFOW(fd,10); + tmtime->tm_hour = tmtime->tm_hour + (short)RFIFOW(fd,12); + tmtime->tm_min = tmtime->tm_min + (short)RFIFOW(fd,14); + tmtime->tm_sec = tmtime->tm_sec + (short)RFIFOW(fd,16); + timestamp = mktime(tmtime); + if (timestamp != -1) { + if (timestamp <= time(NULL)) + timestamp = 0; + if (tmptime != timestamp) { + if (timestamp != 0) { + unsigned char buf[16]; + WBUFW(buf,0) = 0x2731; + WBUFL(buf,2) = acc; + WBUFB(buf,6) = 1; // 0: change of statut, 1: ban + WBUFL(buf,7) = timestamp; // status or final date of a banishment + charif_sendallwos(-1, buf, 11); + } + printf("Account: %d Banned until: %ld\n", acc, timestamp); + sprintf(tmpsql, "UPDATE `%s` SET `ban_until` = '%ld', `state`='7' WHERE `%s` = '%d'", login_db, timestamp, login_db_account_id, acc); + // query + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + } + } + RFIFOSKIP(fd,18); + break; + } + return 0; + + case 0x2727: + if (RFIFOREST(fd) < 6) + return 0; + { + int acc,sex; + unsigned char buf[16]; + acc=RFIFOL(fd,4); + sprintf(tmpsql,"SELECT `sex` FROM `%s` WHERE `%s` = '%d'",login_db,login_db_account_id,acc); + + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + return 0; + } + + sql_res = mysql_store_result(&mysql_handle) ; + + if (sql_res) { + if (mysql_num_rows(sql_res) == 0) { + mysql_free_result(sql_res); + return 0; + } + sql_row = mysql_fetch_row(sql_res); //row fetching + } + + if (strcmpi(sql_row[0], "M") == 0) + sex = 1; + else + sex = 0; + sprintf(tmpsql,"UPDATE `%s` SET `sex` = '%c' WHERE `%s` = '%d'", login_db, (sex==0?'M':'F'), login_db_account_id, acc); + //query + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + WBUFW(buf,0) = 0x2723; + WBUFL(buf,2) = acc; + WBUFB(buf,6) = sex; + charif_sendallwos(-1, buf, 7); + RFIFOSKIP(fd,6); + } + return 0; + + case 0x2728: // save account_reg + if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2)) + return 0; + { + int acc,p,j; + char str[32]; + char temp_str[32]; + int value; + acc=RFIFOL(fd,4); + + if (acc>0){ + unsigned char buf[RFIFOW(fd,2)+1]; + for(p=8,j=0;peof = 1; + return 0; + } + } + + return 0; +} + +//Lan ip check added by Kashy +int lan_ip_check(unsigned char *p) { + int y; + int lancheck = 1; + int lancharip[4]; + + unsigned int k0, k1, k2, k3; + sscanf(lan_char_ip, "%d.%d.%d.%d", &k0, &k1, &k2, &k3); + lancharip[0] = k0; lancharip[1] = k1; lancharip[2] = k2; lancharip[3] = k3; + + for(y = 0; y < 4; y++) { + if ((lancharip[y] & subnetmaski[y])!= (p[y])) + lancheck = 0; + break; } + + printf("LAN check: %s.\n", (lancheck) ? "\033[1;32mLAN\033[0m" : "\033[1;31mWAN\033[0m"); + return lancheck; +} + +//---------------------------------------------------------------------------------------- +// Default packet parsing (normal players or administation/char-server connection requests) +//---------------------------------------------------------------------------------------- +int parse_login(int fd) { + //int len; + + MYSQL_RES* sql_res ; + MYSQL_ROW sql_row = NULL; + + char t_uid[100]; + //int sql_fields, sql_cnt; + struct mmo_account account; + + int result, i; + unsigned char *p = (unsigned char *) &session[fd]->client_addr.sin_addr; + char ip[16]; + + sprintf(ip, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); + + if (ipban > 0) { + //ip ban + //p[0], p[1], p[2], p[3] + //request DB connection + //check + sprintf(tmpsql, "SELECT count(*) FROM `ipbanlist` WHERE `list` = '%d.*.*.*' OR `list` = '%d.%d.*.*' OR `list` = '%d.%d.%d.*' OR `list` = '%d.%d.%d.%d'", + p[0], p[0], p[1], p[0], p[1], p[2], p[0], p[1], p[2], p[3]); + if (mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + sql_res = mysql_store_result(&mysql_handle) ; + sql_row = mysql_fetch_row(sql_res); //row fetching + + if (atoi(sql_row[0]) >0) { + // ip ban ok. + printf ("packet from banned ip : %d.%d.%d.%d" RETCODE, p[0], p[1], p[2], p[3]); + sprintf(tmpsql,"INSERT INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%d.%d.%d.%d', 'unknown','-3', 'ip banned')", loginlog_db, p[0], p[1], p[2], p[3]); + + // query + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + printf ("close session connection...\n"); + + // close connection + session[fd]->eof = 1; + + } else { + printf ("packet from ip (ban check ok) : %d.%d.%d.%d" RETCODE, p[0], p[1], p[2], p[3]); + } + mysql_free_result(sql_res); + } + + if (session[fd]->eof) { + for(i = 0; i < MAX_SERVERS; i++) + if (server_fd[i] == fd) + server_fd[i] = -1; + close(fd); + delete_session(fd); + return 0; + } + + while(RFIFOREST(fd)>=2){ + printf("parse_login : %d %d packet case=%x\n", fd, RFIFOREST(fd), RFIFOW(fd,0)); + + switch(RFIFOW(fd,0)){ + case 0x200: // New alive packet: structure: 0x200 .24B. used to verify if client is always alive. + if (RFIFOREST(fd) < 26) + return 0; + RFIFOSKIP(fd,26); + break; + + case 0x204: // New alive packet: structure: 0x204 .16B. (new ragexe from 22 june 2004) + if (RFIFOREST(fd) < 18) + return 0; + RFIFOSKIP(fd,18); + break; + + case 0x64: // request client login + case 0x01dd: // request client login with encrypt + if(RFIFOREST(fd)< ((RFIFOW(fd, 0) ==0x64)?55:47)) + return 0; + + printf("client connection request %s from %d.%d.%d.%d\n", RFIFOP(fd, 6), p[0], p[1], p[2], p[3]); + + account.userid = RFIFOP(fd, 6); + account.passwd = RFIFOP(fd, 30); +#ifdef PASSWORDENC + account.passwdenc= (RFIFOW(fd,0)==0x64)?0:PASSWORDENC; +#else + account.passwdenc=0; +#endif + result=mmo_auth(&account, fd); + + jstrescapecpy(t_uid,RFIFOP(fd, 6)); + if(result==-1){ + int gm_level = isGM(account.account_id); + if (min_level_to_connect > gm_level) { + WFIFOW(fd,0) = 0x81; + WFIFOL(fd,2) = 1; // 01 = Server closed + WFIFOSET(fd,3); + } else { + if (p[0] != 127) { + sprintf(tmpsql,"INSERT INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%d.%d.%d.%d', '%s','100', 'login ok')", loginlog_db, p[0], p[1], p[2], p[3], t_uid); + //query + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + } + if (gm_level) + printf("Connection of the GM (level:%d) account '%s' accepted.\n", gm_level, account.userid); + else + printf("Connection of the account '%s' accepted.\n", account.userid); + server_num=0; + for(i = 0; i < MAX_SERVERS; i++) { + if (server_fd[i] >= 0) { + //Lan check added by Kashy + if (lan_ip_check(p)) + WFIFOL(fd,47+server_num*32) = inet_addr(lan_char_ip); + else + WFIFOL(fd,47+server_num*32) = server[i].ip; + WFIFOW(fd,47+server_num*32+4) = server[i].port; + memcpy(WFIFOP(fd,47+server_num*32+6), server[i].name, 20); + WFIFOW(fd,47+server_num*32+26) = server[i].users; + WFIFOW(fd,47+server_num*32+28) = server[i].maintenance; + WFIFOW(fd,47+server_num*32+30) = server[i].new; + server_num++; + } + } + // if at least 1 char-server + if (server_num > 0) { + WFIFOW(fd,0)=0x69; + WFIFOW(fd,2)=47+32*server_num; + WFIFOL(fd,4)=account.login_id1; + WFIFOL(fd,8)=account.account_id; + WFIFOL(fd,12)=account.login_id2; + WFIFOL(fd,16)=0; + memcpy(WFIFOP(fd,20),account.lastlogin,24); + WFIFOB(fd,46)=account.sex; + WFIFOSET(fd,47+32*server_num); + if(auth_fifo_pos>=AUTH_FIFO_SIZE) + auth_fifo_pos=0; + auth_fifo[auth_fifo_pos].account_id=account.account_id; + auth_fifo[auth_fifo_pos].login_id1=account.login_id1; + auth_fifo[auth_fifo_pos].login_id2=account.login_id2; + auth_fifo[auth_fifo_pos].sex=account.sex; + auth_fifo[auth_fifo_pos].delflag=0; + auth_fifo[auth_fifo_pos].ip = session[fd]->client_addr.sin_addr.s_addr; + auth_fifo_pos++; + } else { + WFIFOW(fd,0) = 0x81; + WFIFOL(fd,2) = 1; // 01 = Server closed + WFIFOSET(fd,3); + } + } + } else { + char tmp_sql[512]; + char error[64]; + sprintf(tmp_sql,"INSERT INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%d.%d.%d.%d', '%s', '%d','login failed : %%s')", loginlog_db, p[0], p[1], p[2], p[3], t_uid, result); + switch((result + 1)) { + case -2: //-3 = Account Banned + sprintf(tmpsql,tmp_sql,"Account banned."); + sprintf(error,"Account banned."); + break; + case -1: //-2 = Dynamic Ban + sprintf(tmpsql,tmp_sql,"dynamic ban (ip and account)."); + sprintf(error,"dynamic ban (ip and account)."); + break; + case 1: // 0 = Unregistered ID + sprintf(tmpsql,tmp_sql,"Unregisterd ID."); + sprintf(error,"Unregisterd ID."); + break; + case 2: // 1 = Incorrect Password + sprintf(tmpsql,tmp_sql,"Incorrect Password."); + sprintf(error,"Incorrect Password."); + break; + case 3: // 2 = This ID is expired + sprintf(tmpsql,tmp_sql,"Account Expired."); + sprintf(error,"Account Expired."); + break; + case 4: // 3 = Rejected from Server + sprintf(tmpsql,tmp_sql,"Rejected from server."); + sprintf(error,"Rejected from server."); + break; + case 5: // 4 = You have been blocked by the GM Team + sprintf(tmpsql,tmp_sql,"Blocked by GM."); + sprintf(error,"Blocked by GM."); + break; + case 6: // 5 = Your Game's EXE file is not the latest version + sprintf(tmpsql,tmp_sql,"Not latest game EXE."); + sprintf(error,"Not latest game EXE."); + break; + case 7: // 6 = Your are Prohibited to log in until %s + sprintf(tmpsql,tmp_sql,"Banned."); + sprintf(error,"Banned."); + break; + case 8: // 7 = Server is jammed due to over populated + sprintf(tmpsql,tmp_sql,"Server Over-population."); + sprintf(error,"Server Over-population."); + break; + case 9: // 8 = No MSG (actually, all states after 9 except 99 are No MSG, use only this) + sprintf(tmpsql,tmp_sql," "); + sprintf(error," "); + break; + case 100: // 99 = This ID has been totally erased + sprintf(tmpsql,tmp_sql,"Account gone."); + sprintf(error,"Account gone."); + break; + default: + sprintf(tmpsql,tmp_sql,"Uknown Error."); + sprintf(error,"Uknown Error."); + break; + } + //query + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + if ((result == 1) && (dynamic_pass_failure_ban != 0)){ // failed password + sprintf(tmpsql,"SELECT count(*) FROM `%s` WHERE `ip` = '%d.%d.%d.%d' AND `rcode` = '1' AND `time` > NOW() - INTERVAL %d MINUTE", + loginlog_db, p[0], p[1], p[2], p[3], dynamic_pass_failure_ban_time); //how many times filed account? in one ip. + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + //check query result + sql_res = mysql_store_result(&mysql_handle) ; + sql_row = mysql_fetch_row(sql_res); //row fetching + + if (atoi(sql_row[0]) >= dynamic_pass_failure_ban_how_many ) { + sprintf(tmpsql,"INSERT INTO `ipbanlist`(`list`,`btime`,`rtime`,`reason`) VALUES ('%d.%d.%d.*', NOW() , NOW() + INTERVAL %d MINUTE ,'Password error ban: %s')", p[0], p[1], p[2], dynamic_pass_failure_ban_how_long, t_uid); + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + } + mysql_free_result(sql_res); + } + else if (result == -2){ //dynamic banned - add ip to ban list. + sprintf(tmpsql,"INSERT INTO `ipbanlist`(`list`,`btime`,`rtime`,`reason`) VALUES ('%d.%d.%d.*', NOW() , NOW() + INTERVAL 1 MONTH ,'Dynamic banned user id : %s')", p[0], p[1], p[2], t_uid); + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + result = -3; + } + + sprintf(tmpsql,"SELECT `ban_until` FROM `%s` WHERE `%s` = '%s'",login_db,login_db_userid, t_uid); + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + sql_res = mysql_store_result(&mysql_handle) ; + if (sql_res) { + sql_row = mysql_fetch_row(sql_res); //row fetching + } + //cannot connect login failed + memset(WFIFOP(fd,0),'\0',23); + WFIFOW(fd,0)=0x6a; + WFIFOB(fd,2)=result; + if (result == 6) { // 6 = Your are Prohibited to log in until %s + if (atol(sql_row[0]) != 0) { // if account is banned, we send ban timestamp + char tmpstr[256]; + time_t ban_until_time; + ban_until_time = atol(sql_row[0]); + strftime(tmpstr, 20, date_format, localtime(&ban_until_time)); + tmpstr[19] = '\0'; + memcpy(WFIFOP(fd,3), tmpstr, 20); + } else { // we send error message + memcpy(WFIFOP(fd,3), error, 20); + } + } + WFIFOSET(fd,23); + } + RFIFOSKIP(fd,(RFIFOW(fd,0)==0x64)?55:47); + break; + + case 0x01db: // request password key + if (session[fd]->session_data) { + printf("login: abnormal request of MD5 key (already opened session).\n"); + session[fd]->eof = 1; + return 0; + } + printf("Request Password key -%s\n",md5key); + RFIFOSKIP(fd,2); + WFIFOW(fd,0)=0x01dc; + WFIFOW(fd,2)=4+md5keylen; + memcpy(WFIFOP(fd,4),md5key,md5keylen); + WFIFOSET(fd,WFIFOW(fd,2)); + break; + + case 0x2710: // request Char-server connection + if(RFIFOREST(fd)<86) + return 0; + { + sprintf(tmpsql,"INSERT INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%d.%d.%d.%d', '%s@%s','100', 'charserver - %s@%d.%d.%d.%d:%d')", loginlog_db, p[0], p[1], p[2], p[3], RFIFOP(fd, 2),RFIFOP(fd, 60),RFIFOP(fd, 60), RFIFOB(fd, 54), RFIFOB(fd, 55), RFIFOB(fd, 56), RFIFOB(fd, 57), RFIFOW(fd, 58)); + + //query + if(mysql_query(&mysql_handle, tmpsql)) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + printf("server connection request %s @ %d.%d.%d.%d:%d (%d.%d.%d.%d)\n", + RFIFOP(fd, 60), RFIFOB(fd, 54), RFIFOB(fd, 55), RFIFOB(fd, 56), RFIFOB(fd, 57), RFIFOW(fd, 58), + p[0], p[1], p[2], p[3]); + unsigned char* server_name; + account.userid = RFIFOP(fd, 2); + account.passwd = RFIFOP(fd, 26); + account.passwdenc = 0; + server_name = RFIFOP(fd,60); + result = mmo_auth(&account, fd); + //printf("Result: %d - Sex: %d - Account ID: %d\n",result,account.sex,(int) account.account_id); + + if(result == -1 && account.sex==2 && account.account_idfunc_parse=parse_fromchar; + realloc_fifo(fd,FIFOSIZE_SERVERLINK,FIFOSIZE_SERVERLINK); + } else { + WFIFOW(fd, 0) =0x2711; + WFIFOB(fd, 2)=3; + WFIFOSET(fd, 3); + } + } + RFIFOSKIP(fd, 86); + return 0; + + case 0x7530: // request Athena information + WFIFOW(fd,0)=0x7531; + WFIFOB(fd,2)=ATHENA_MAJOR_VERSION; + WFIFOB(fd,3)=ATHENA_MINOR_VERSION; + WFIFOB(fd,4)=ATHENA_REVISION; + WFIFOB(fd,5)=ATHENA_RELEASE_FLAG; + WFIFOB(fd,6)=ATHENA_OFFICIAL_FLAG; + WFIFOB(fd,7)=ATHENA_SERVER_LOGIN; + WFIFOW(fd,8)=ATHENA_MOD_VERSION; + WFIFOSET(fd,10); + RFIFOSKIP(fd,2); + printf ("Athena version check...\n"); + break; + + case 0x7532: + default: + printf ("End of connection (ip: %s)" RETCODE, ip); + session[fd]->eof = 1; + return 0; + } + } + + return 0; +} + +//------------------------------------------------- +// Return numerical value of a switch configuration +// on/off, english, fran軋is, deutsch, espaol +//------------------------------------------------- +int config_switch(const char *str) { + if (strcmpi(str, "on") == 0 || strcmpi(str, "yes") == 0 || strcmpi(str, "oui") == 0 || strcmpi(str, "ja") == 0 || strcmpi(str, "si") == 0) + return 1; + if (strcmpi(str, "off") == 0 || strcmpi(str, "no") == 0 || strcmpi(str, "non") == 0 || strcmpi(str, "nein") == 0) + return 0; + + return atoi(str); +} + + +//Lan Support conf reading added by Kashy +int login_lan_config_read(const char *lancfgName){ + int i; + char subnetmask[128]; + char line[1024], w1[1024], w2[1024]; + FILE *fp; + + fp=fopen(lancfgName, "r"); + + if (fp == NULL) { + printf("file not found: %s\n", lancfgName); + return 1; + } + printf("Start reading of Lan Support configuration file\n"); + while(fgets(line, sizeof(line)-1, fp)){ + if (line[0] == '/' && line[1] == '/') + continue; + + i = sscanf(line,"%[^:]: %[^\r\n]",w1,w2); + if(i!=2) + continue; + + else if(strcmpi(w1,"lan_char_ip")==0){ + strcpy(lan_char_ip, w2); + printf ("set Lan_Char_IP : %s\n",w2); + } + + else if(strcmpi(w1,"subnetmask")==0){ + strcpy(subnetmask, w2); + unsigned int k0, k1, k2, k3; + sscanf(subnetmask, "%d.%d.%d.%d", &k0, &k1, &k2, &k3); + subnetmaski[0] = k0; subnetmaski[1] = k1; subnetmaski[2] = k2; subnetmaski[3] = k3; + printf ("set subnetmask : %s\n",w2); + } + } + fclose(fp); + + { + unsigned int a0, a1, a2, a3; + unsigned char p[4]; + sscanf(lan_char_ip, "%d.%d.%d.%d", &a0, &a1, &a2, &a3); + p[0] = a0; p[1] = a1; p[2] = a2; p[3] = a3; + printf("LAN test of LAN IP of the char-server: "); + if (lan_ip_check(p) == 0) { + printf("\033[1;31m***ERROR: LAN IP of the char-server doesn't belong to the specified Sub-network\033[0m\n"); + } + } + + printf("End reading of Lan Support configuration file\n"); + + return 0; +} + +//----------------------------------------------------- +//BANNED IP CHECK. +//----------------------------------------------------- +int ip_ban_check(int tid, unsigned int tick, int id, int data){ + + //query + if(mysql_query(&mysql_handle, "DELETE FROM `ipbanlist` WHERE `rtime` <= NOW()")) { + printf("DB server Error - %s\n", mysql_error(&mysql_handle)); + } + + return 0; +} + +//----------------------------------------------------- +// reading configuration +//----------------------------------------------------- +int login_config_read(const char *cfgName){ + int i; + char line[1024], w1[1024], w2[1024]; + FILE *fp; + + fp=fopen(cfgName,"r"); + + if(fp==NULL){ + printf("Configuration file (%s) not found.\n", cfgName); + return 1; + } + printf ("start reading configuration...\n"); + while(fgets(line, sizeof(line)-1, fp)){ + if(line[0] == '/' && line[1] == '/') + continue; + + i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2); + if(i!=2) + continue; + + else if(strcmpi(w1,"login_port")==0){ + login_port=atoi(w2); + printf ("set login_port : %s\n",w2); + } + else if(strcmpi(w1,"ipban")==0){ + ipban=atoi(w2); + printf ("set ipban : %d\n",ipban); + } + //account ban -> ip ban + else if(strcmpi(w1,"dynamic_account_ban")==0){ + dynamic_account_ban=atoi(w2); + printf ("set dynamic_account_ban : %d\n",dynamic_account_ban); + } + else if(strcmpi(w1,"dynamic_account_ban_class")==0){ + dynamic_account_ban_class=atoi(w2); + printf ("set dynamic_account_ban_class : %d\n",dynamic_account_ban_class); + } + //dynamic password error ban + else if(strcmpi(w1,"dynamic_pass_failure_ban")==0){ + dynamic_pass_failure_ban=atoi(w2); + printf ("set dynamic_pass_failure_ban : %d\n",dynamic_pass_failure_ban); + } + else if(strcmpi(w1,"dynamic_pass_failure_ban_time")==0){ + dynamic_pass_failure_ban_time=atoi(w2); + printf ("set dynamic_pass_failure_ban_time : %d\n",dynamic_pass_failure_ban_time); + } + else if(strcmpi(w1,"dynamic_pass_failure_ban_how_many")==0){ + dynamic_pass_failure_ban_how_many=atoi(w2); + printf ("set dynamic_pass_failure_ban_how_many : %d\n",dynamic_pass_failure_ban_how_many); + } + else if(strcmpi(w1,"dynamic_pass_failure_ban_how_long")==0){ + dynamic_pass_failure_ban_how_long=atoi(w2); + printf ("set dynamic_pass_failure_ban_how_long : %d\n",dynamic_pass_failure_ban_how_long); + } + else if(strcmpi(w1,"anti_freeze_enable")==0){ + anti_freeze_enable = config_switch(w2); + } + else if (strcmpi(w1, "anti_freeze_interval") == 0) { + ANTI_FREEZE_INTERVAL = atoi(w2); + if (ANTI_FREEZE_INTERVAL < 5) + ANTI_FREEZE_INTERVAL = 5; // minimum 5 seconds + } + else if (strcmpi(w1, "import") == 0) { + login_config_read(w2); + } + else if(strcmpi(w1,"use_MD5_passwords")==0){ + if (!strcmpi(w2,"yes")) { + use_md5_passwds=1; + } else if (!strcmpi(w2,"no")){ + use_md5_passwds=0; + } + printf ("Using MD5 Passwords: %s \n",w2); + } + else if (strcmpi(w1, "date_format") == 0) { // note: never have more than 19 char for the date! + switch (atoi(w2)) { + case 0: + strcpy(date_format, "%d-%m-%Y %H:%M:%S"); // 31-12-2004 23:59:59 + break; + case 1: + strcpy(date_format, "%m-%d-%Y %H:%M:%S"); // 12-31-2004 23:59:59 + break; + case 2: + strcpy(date_format, "%Y-%d-%m %H:%M:%S"); // 2004-31-12 23:59:59 + break; + case 3: + strcpy(date_format, "%Y-%m-%d %H:%M:%S"); // 2004-12-31 23:59:59 + break; + } + } + else if (strcmpi(w1, "min_level_to_connect") == 0) { + min_level_to_connect = atoi(w2); + } + else if (strcmpi(w1, "check_ip_flag") == 0) { + check_ip_flag = config_switch(w2); + } + } + fclose(fp); + printf ("End reading configuration...\n"); + return 0; +} + +void sql_config_read(const char *cfgName){ /* Kalaspuff, to get login_db */ + int i; + char line[1024], w1[1024], w2[1024]; + printf("reading configure: %s\n", cfgName); + FILE *fp=fopen(cfgName,"r"); + if(fp==NULL){ + printf("file not found: %s\n",cfgName); + exit(1); + } + while(fgets(line, sizeof(line)-1, fp)){ + if(line[0] == '/' && line[1] == '/') + continue; + i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2); + if(i!=2) + continue; + if (strcmpi(w1, "login_db") == 0) { + strcpy(login_db, w2); + } + //add for DB connection + else if(strcmpi(w1,"login_server_ip")==0){ + strcpy(login_server_ip, w2); + printf ("set login_server_ip : %s\n",w2); + } + else if(strcmpi(w1,"login_server_port")==0){ + login_server_port=atoi(w2); + printf ("set login_server_port : %s\n",w2); + } + else if(strcmpi(w1,"login_server_id")==0){ + strcpy(login_server_id, w2); + printf ("set login_server_id : %s\n",w2); + } + else if(strcmpi(w1,"login_server_pw")==0){ + strcpy(login_server_pw, w2); + printf ("set login_server_pw : %s\n",w2); + } + else if(strcmpi(w1,"login_server_db")==0){ + strcpy(login_server_db, w2); + printf ("set login_server_db : %s\n",w2); + } + //added for custom column names for custom login table + else if(strcmpi(w1,"login_db_account_id")==0){ + strcpy(login_db_account_id, w2); + } + else if(strcmpi(w1,"login_db_userid")==0){ + strcpy(login_db_userid, w2); + } + else if(strcmpi(w1,"login_db_user_pass")==0){ + strcpy(login_db_user_pass, w2); + } + else if(strcmpi(w1,"login_db_level")==0){ + strcpy(login_db_level, w2); + } + //end of custom table config + else if (strcmpi(w1, "loginlog_db") == 0) { + strcpy(loginlog_db, w2); + } + } + fclose(fp); + printf("reading configure done.....\n"); +} + +int do_init(int argc,char **argv){ + //initialize login server + int i; + + //read login configue + login_config_read( (argc>1)?argv[1]:LOGIN_CONF_NAME ); + sql_config_read(SQL_CONF_NAME); + login_lan_config_read((argc > 1) ? argv[1] : LAN_CONF_NAME); + //Generate Passworded Key. + printf ("memset md5key \n"); + memset(md5key, 0, sizeof(md5key)); + printf ("memset md5key complete\n"); + printf ("memset keyleng\n"); + md5keylen=rand()%4+12; + for(i=0;i 0) { + add_timer_func_list(char_anti_freeze_system, "char_anti_freeze_system"); + i = add_timer_interval(gettick()+1000, char_anti_freeze_system, 0, 0, ANTI_FREEZE_INTERVAL * 1000); + } + + // ban deleter timer - 1 minute term + printf("add interval tic (ip_ban_check)....\n"); + i=add_timer_interval(gettick()+10, ip_ban_check,0,0,60*1000); + + printf("The login-server is \033[1;32mready\033[0m (Server is listening on the port %d).\n\n", login_port); + + return 0; +} + + diff --git a/misc/src/login_sql/login.h b/misc/src/login_sql/login.h new file mode 100644 index 0000000..6335168 --- /dev/null +++ b/misc/src/login_sql/login.h @@ -0,0 +1,41 @@ +#ifndef _LOGIN_H_ +#define _LOGIN_H_ + +#define MAX_SERVERS 30 + +#define LOGIN_CONF_NAME "conf/login_athena.conf" +#define SQL_CONF_NAME "conf/inter_athena.conf" +#define LAN_CONF_NAME "conf/lan_support.conf" + +#define PASSWORDENC 3 // A definition is given when making an encryption password correspond. + // It is 1 at the time of passwordencrypt. + // It is made into 2 at the time of passwordencrypt2. + // When it is made 3, it corresponds to both. + +#define START_ACCOUNT_NUM 2000000 +#define END_ACCOUNT_NUM 100000000 + +struct mmo_account { + char* userid; + char* passwd; + int passwdenc; + + long account_id; + long login_id1; + long login_id2; + long char_id; + char lastlogin[24]; + int sex; +}; + +struct mmo_char_server { + char name[20]; + long ip; + short port; + int users; + int maintenance; + int new; +}; + + +#endif diff --git a/misc/src/login_sql/make.sh b/misc/src/login_sql/make.sh new file mode 100644 index 0000000..198d33b --- /dev/null +++ b/misc/src/login_sql/make.sh @@ -0,0 +1,6 @@ +#!/bin/sh + rsqlt=`rm -rf *.o` + gcc -c login.c -I/usr/local/include/mysql/ + gcc -c md5calc.c -I/usr/local/include/mysql/ + gcc -c strlib.c + gcc -o login-server login.o strlib.o md5calc.o ../common/core.o ../common/socket.o ../common/timer.o ../common/db.o -L/usr/local/lib/mysql/ -lmysqlclient -lz diff --git a/misc/src/login_sql/md5calc.c b/misc/src/login_sql/md5calc.c new file mode 100644 index 0000000..58cea12 --- /dev/null +++ b/misc/src/login_sql/md5calc.c @@ -0,0 +1,236 @@ +/*********************************************************** + * md5 calculation algorithm + * + * The source code referred to the following URL. + * http://www.geocities.co.jp/SiliconValley-Oakland/8878/lab17/lab17.html + * + ***********************************************************/ + +#include "md5calc.h" +#include +#include + +#ifndef UINT_MAX +#define UINT_MAX 4294967295U +#endif + +// Global variable +static unsigned int *pX; + +// Stirng Table +static const unsigned int T[] = { + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, //0 + 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, //4 + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, //8 + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, //12 + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, //16 + 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, //20 + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, //24 + 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, //28 + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, //32 + 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, //36 + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, //40 + 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, //44 + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, //48 + 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, //52 + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, //56 + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 //60 +}; + +// ROTATE_LEFT The left is made to rotate x [ n-bit ]. This is diverted as it is from RFC. +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +// The function used for other calculation +static unsigned int F(unsigned int X, unsigned int Y, unsigned int Z) +{ + return (X & Y) | (~X & Z); +} +static unsigned int G(unsigned int X, unsigned int Y, unsigned int Z) +{ + return (X & Z) | (Y & ~Z); +} +static unsigned int H(unsigned int X, unsigned int Y, unsigned int Z) +{ + return X ^ Y ^ Z; +} +static unsigned int I(unsigned int X, unsigned int Y, unsigned int Z) +{ + return Y ^ (X | ~Z); +} + +static unsigned int Round(unsigned int a, unsigned int b, unsigned int FGHI, + unsigned int k, unsigned int s, unsigned int i) +{ + return b + ROTATE_LEFT(a + FGHI + pX[k] + T[i], s); +} + +static void Round1(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, F(b,c,d), k, s, i); +} +static void Round2(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, G(b,c,d), k, s, i); +} +static void Round3(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, H(b,c,d), k, s, i); +} +static void Round4(unsigned int *a, unsigned int b, unsigned int c, + unsigned int d,unsigned int k, unsigned int s, unsigned int i) +{ + *a = Round(*a, b, I(b,c,d), k, s, i); +} + +static void MD5_Round_Calculate(const unsigned char *block, + unsigned int *A2, unsigned int *B2, unsigned int *C2, unsigned int *D2) +{ + //create X It is since it is required. + unsigned int X[16]; //512bit 64byte + int j,k; + + //Save A as AA, B as BB, C as CC, and and D as DD (saving of A, B, C, and D) + unsigned int A=*A2, B=*B2, C=*C2, D=*D2; + unsigned int AA = A,BB = B,CC = C,DD = D; + + //It is a large region variable reluctantly because of calculation of a round. . . for Round1...4 + pX = X; + + //Copy block(padding_message) i into X + for (j=0,k=0; j<64; j+=4,k++) + X[k] = ( (unsigned int )block[j] ) // 8byte*4 -> 32byte conversion + | ( ((unsigned int )block[j+1]) << 8 ) // A function called Decode as used in the field of RFC + | ( ((unsigned int )block[j+2]) << 16 ) + | ( ((unsigned int )block[j+3]) << 24 ); + + + //Round 1 + Round1(&A,B,C,D, 0, 7, 0); Round1(&D,A,B,C, 1, 12, 1); Round1(&C,D,A,B, 2, 17, 2); Round1(&B,C,D,A, 3, 22, 3); + Round1(&A,B,C,D, 4, 7, 4); Round1(&D,A,B,C, 5, 12, 5); Round1(&C,D,A,B, 6, 17, 6); Round1(&B,C,D,A, 7, 22, 7); + Round1(&A,B,C,D, 8, 7, 8); Round1(&D,A,B,C, 9, 12, 9); Round1(&C,D,A,B, 10, 17, 10); Round1(&B,C,D,A, 11, 22, 11); + Round1(&A,B,C,D, 12, 7, 12); Round1(&D,A,B,C, 13, 12, 13); Round1(&C,D,A,B, 14, 17, 14); Round1(&B,C,D,A, 15, 22, 15); + + //Round 2 + Round2(&A,B,C,D, 1, 5, 16); Round2(&D,A,B,C, 6, 9, 17); Round2(&C,D,A,B, 11, 14, 18); Round2(&B,C,D,A, 0, 20, 19); + Round2(&A,B,C,D, 5, 5, 20); Round2(&D,A,B,C, 10, 9, 21); Round2(&C,D,A,B, 15, 14, 22); Round2(&B,C,D,A, 4, 20, 23); + Round2(&A,B,C,D, 9, 5, 24); Round2(&D,A,B,C, 14, 9, 25); Round2(&C,D,A,B, 3, 14, 26); Round2(&B,C,D,A, 8, 20, 27); + Round2(&A,B,C,D, 13, 5, 28); Round2(&D,A,B,C, 2, 9, 29); Round2(&C,D,A,B, 7, 14, 30); Round2(&B,C,D,A, 12, 20, 31); + + //Round 3 + Round3(&A,B,C,D, 5, 4, 32); Round3(&D,A,B,C, 8, 11, 33); Round3(&C,D,A,B, 11, 16, 34); Round3(&B,C,D,A, 14, 23, 35); + Round3(&A,B,C,D, 1, 4, 36); Round3(&D,A,B,C, 4, 11, 37); Round3(&C,D,A,B, 7, 16, 38); Round3(&B,C,D,A, 10, 23, 39); + Round3(&A,B,C,D, 13, 4, 40); Round3(&D,A,B,C, 0, 11, 41); Round3(&C,D,A,B, 3, 16, 42); Round3(&B,C,D,A, 6, 23, 43); + Round3(&A,B,C,D, 9, 4, 44); Round3(&D,A,B,C, 12, 11, 45); Round3(&C,D,A,B, 15, 16, 46); Round3(&B,C,D,A, 2, 23, 47); + + //Round 4 + Round4(&A,B,C,D, 0, 6, 48); Round4(&D,A,B,C, 7, 10, 49); Round4(&C,D,A,B, 14, 15, 50); Round4(&B,C,D,A, 5, 21, 51); + Round4(&A,B,C,D, 12, 6, 52); Round4(&D,A,B,C, 3, 10, 53); Round4(&C,D,A,B, 10, 15, 54); Round4(&B,C,D,A, 1, 21, 55); + Round4(&A,B,C,D, 8, 6, 56); Round4(&D,A,B,C, 15, 10, 57); Round4(&C,D,A,B, 6, 15, 58); Round4(&B,C,D,A, 13, 21, 59); + Round4(&A,B,C,D, 4, 6, 60); Round4(&D,A,B,C, 11, 10, 61); Round4(&C,D,A,B, 2, 15, 62); Round4(&B,C,D,A, 9, 21, 63); + + // Then perform the following additions. (let's add) + *A2 = A + AA; + *B2 = B + BB; + *C2 = C + CC; + *D2 = D + DD; + + //The clearance of confidential information + memset(pX, 0, sizeof(X)); +} + +//------------------------------------------------------------------- +// The function for the exteriors + +/** output is the coded binary in the character sequence which wants to code string. */ +void MD5_String2binary(const char * string, char * output) +{ +//var + /*8bit*/ + unsigned char padding_message[64]; //Extended message 512bit 64byte + unsigned char *pstring; //The position of string in the present scanning notes is held. + +// unsigned char digest[16]; + /*32bit*/ + unsigned int string_byte_len, //The byte chief of string is held. + string_bit_len, //The bit length of string is held. + copy_len, //The number of bytes which is used by 1-3 and which remained + msg_digest[4]; //Message digest 128bit 4byte + unsigned int *A = &msg_digest[0], //The message digest in accordance with RFC (reference) + *B = &msg_digest[1], + *C = &msg_digest[2], + *D = &msg_digest[3]; + int i; + +//prog + //Step 3.Initialize MD Buffer (although it is the initialization; step 3 of A, B, C, and D -- unavoidable -- a head) + *A = 0x67452301; + *B = 0xefcdab89; + *C = 0x98badcfe; + *D = 0x10325476; + + //Step 1.Append Padding Bits (extension of a mark bit) + //1-1 + string_byte_len = strlen(string); //The byte chief of a character sequence is acquired. + pstring = (unsigned char *)string; //The position of the present character sequence is set. + + //1-2 Repeat calculation until length becomes less than 64 bytes. + for (i=string_byte_len; 64<=i; i-=64,pstring+=64) + MD5_Round_Calculate(pstring, A,B,C,D); + + //1-3 + copy_len = string_byte_len % 64; //The number of bytes which remained is computed. + strncpy((char *)padding_message, (char *)pstring, copy_len); //A message is copied to an extended bit sequence. + memset(padding_message+copy_len, 0, 64 - copy_len); //It buries by 0 until it becomes extended bit length. + padding_message[copy_len] |= 0x80; //The next of a message is 1. + + //1-4 + //If 56 bytes or more (less than 64 bytes) of remainder becomes, it will calculate by extending to 64 bytes. + if (56 <= copy_len) { + MD5_Round_Calculate(padding_message, A,B,C,D); + memset(padding_message, 0, 56); //56 bytes is newly fill uped with 0. + } + + + //Step 2.Append Length (the information on length is added) + string_bit_len = string_byte_len * 8; //From the byte chief to bit length (32 bytes of low rank) + memcpy(&padding_message[56], &string_bit_len, 4); //32 bytes of low rank is set. + + //When bit length cannot be expressed in 32 bytes of low rank, it is a beam raising to a higher rank. + if (UINT_MAX / 8 < string_byte_len) { + unsigned int high = (string_byte_len - UINT_MAX / 8) * 8; + memcpy(&padding_message[60], &high, 4); + } else + memset(&padding_message[60], 0, 4); //In this case, it is good for a higher rank at 0. + + //Step 4.Process Message in 16-Word Blocks (calculation of MD5) + MD5_Round_Calculate(padding_message, A,B,C,D); + + + //Step 5.Output (output) + memcpy(output,msg_digest,16); +// memcpy (digest, msg_digest, and 16); //8 byte*4 < - 32byte conversion A function called Encode as used in the field of RFC +/* sprintf(output, + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", + digest[ 0], digest[ 1], digest[ 2], digest[ 3], + digest[ 4], digest[ 5], digest[ 6], digest[ 7], + digest[ 8], digest[ 9], digest[10], digest[11], + digest[12], digest[13], digest[14], digest[15]);*/ +} + +/** output is the coded character sequence in the character sequence which wants to code string. */ +void MD5_String(const char * string, char * output) +{ + unsigned char digest[16]; + + MD5_String2binary(string,digest); + sprintf(output, + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", + digest[ 0], digest[ 1], digest[ 2], digest[ 3], + digest[ 4], digest[ 5], digest[ 6], digest[ 7], + digest[ 8], digest[ 9], digest[10], digest[11], + digest[12], digest[13], digest[14], digest[15]); +} + diff --git a/misc/src/login_sql/md5calc.h b/misc/src/login_sql/md5calc.h new file mode 100644 index 0000000..9bc554f --- /dev/null +++ b/misc/src/login_sql/md5calc.h @@ -0,0 +1,7 @@ +#ifndef _MD5CALC_H_ +#define _MD5CALC_H_ + +void MD5_String(const char * string, char * output); +void MD5_String2binary(const char * string, char * output); + +#endif diff --git a/misc/src/login_sql/readme.txt b/misc/src/login_sql/readme.txt new file mode 100644 index 0000000..b945af7 --- /dev/null +++ b/misc/src/login_sql/readme.txt @@ -0,0 +1,120 @@ +サソ// Encoded UTF-8 +//--------------------------------------------- +// 007 - by Jazz +1. 0590 official version縺ォ蜷医o縺帙※菫ョ豁」縺励∪縺励◆. + +//--------------------------------------------- +// 006 - by Jazz +1. 繝代せ繝ッ繝シ繝画アコ縺セ縺」縺溷屓謨ー髢馴&縺譎りェ蜍 IP驕ョ譁ュ蜈キ迴セ. +2. login_athena.conf繧剃ソョ豁」 + +//--------------------------------------------- +// 005 - by Jazz +1. 險ア螳ケ縺励↑縺 ID縺梧磁霑代ョ譎りェ蜍輔〒 IP驕ョ譁ュ讖溯ス蜈キ迴セ. +2. 閾ェ蜍輔〒驕ョ譁ュ隗」髯、讖溯ス蜈キ迴セ. + +//--------------------------------------------- +// 004 - by Jazz +1. aphostropy 蝠城。後r隗」豎コ縺励∪縺励◆. 縺薙l縺ッ菫晏ョ峨→騾」髢「縺後≠繧句撫鬘後〒縺. +2. SQL縺ョ讒矩繧剃ソョ豁」縺励∪縺励◆. +3. server 迥カ諷九r SQL縺ァ蜃コ蜉. + +//--------------------------------------------- +// 003 - by Jazz +1. 謗・邯夊ィ倬鹸繧 DB縺ァ蜃コ蜉. +2. IP蝓コ逶、縺ョ謗・邯夐ョ譁ュ蜈キ迴セ. + +//--------------------------------------------- +// 002 - by Jazz +1. 縺縺上▽縺九ョ隧ウ邏ー蝠城。檎せ菫ョ豁」. + +//--------------------------------------------- +// 001 - by Jazz +1. 荳逡ェ逶ョ螳牙ョ version縺ァ縺. + + +// notice some.. +In this program, new parts are under BSD License. +and imported parts are under GPL as athena did. + +this program is not public license. but modification and editing are +unlimit permitted. but auther don't have any responsibility on that. + +this realase does not gurantee working perfectly. and, I would +answer neither that question nor technical one. + +I use... +P4 2.4Gh/512MB/WinXP/cygwin + +athena is under GPL license. +Ragnarok is Gravity's trademark. +login-db for athena is BSD license except code from original athena. +cygwin is Redhat's trademark. + +// About compile. +You need mysqlclient library to compile this. You can get this by +compiling mysql source. + +"-lmysqlclient" option needed when you did compile. + +some patch need on mysql when you did compile under cygwin. + +// ェーァ 簿ウエ乱 劇葺流 + +擽 売。懋キク棹乱 ヨ。 ァ誤豆牟ァ カカ捩 BSD 攵擽┥侃 符椈乱 ーー小 姓笈共. +キクヲャウ import 頗 カカ捩 寳椈 athena攪 GPL 攵擽┥侃 符椈乱 梭慣笈共. + +擽 売。懋キク棹捩 public licenseー 符漁笈共. 葺ァァ ウイス, ー懍。ー, 們菩捩 +ャエ懦復 来圸据ゥー キク乱 劇復 ア桷捩 梵ー ァァ 賦慣笈共. + +椪 擽 ヲエヲャヲ壱株 刋イス梭 徐梠物揆 ウエ棗葺ァ 賦慣笈共. キクヲャウ キク乱 劇復 +メ橋 戦復 壱劇 ー幗ァ 賦慣笈共. + +クー唖攤 カカ攪 メ橋捩 ー幗ァ 賦慣笈共. + +ー罹ー 劍イス +P4 2.4Gh/512MB/WinXP/cygwin + +athena株 GPL 攵擽┥侃・シ 、 鮒笈共. +Ragnarok株 Gravity攪 trademark桿笈共. +login-db for athena株 athena乱 ーク乖 カカ揆 懍匣葺ゥエ BSD 攵擽┥侃・シ 伐ヲ笈共. +cygwin捩 Redhat攪 trademark桿笈共. + +// サエ血攵乱 劇葺流. + +擽 売。懋キク棹揆 サエ血攵葺クー 怱葺流 mysqlclient乱 劇復 libraryー 符囈鮒笈共. +擽株 mysql攪 護侃・シ サエ血攵葺ゥエ 冥揆 梭慣笈共. + +サエ血攵亨乱 -lmysqlclient 亰們擽 符囈鮒笈共. + +cygwin攪 イス垈株 ェーァ 肩ケ俾ー 符囈鮒笈共. + +// 縺縺上▽縺九ョ諠蝣ア縺ォ蟇セ縺励※ + +縺薙ョ繝励Ο繧ー繝ゥ繝縺ァ譁ー縺溘↓菴懊i繧後◆驛ィ蛻縺ッ BSD 繝ゥ繧、繧サ繝ウ繧ケ縺ョ荳九↓驟榊ク縺ォ縺ェ繧翫∪縺. +縺昴@縺ヲ import 縺ォ縺ェ縺」縺滄Κ蛻縺ッ蜈縲 athena縺ョ GPL 繝ゥ繧、繧サ繝ウ繧ケ縺ョ荳九↓縺ゅj縺セ縺. + +縺薙ョ繝励Ο繧ー繝ゥ繝縺ッ public license縺ァ縺ッ縺ェ縺縺ァ縺. 縺励°縺怜、画峩, 謾ケ騾, 菫ョ豁」縺ッ +辟。蛻カ髯占ィア螳ケ縺輔l縺ヲ縺昴l縺ォ蟇セ縺吶k雋ャ莉サ縺ッ闡苓縺瑚イ縺代↑縺縺ァ縺. + +迴セ蝨ィ縺薙ョ繝ェ繝ェ繝シ繧ケ縺ッ螳悟」√↓蜍穂ス懊☆繧九%縺ィ繧剃ソ晞囿縺励↑縺縺ァ縺. 縺昴@縺ヲ蠖シ縺ォ螟ァ髻 +逶ク隲繧らオカ蟇セ蜿励¢縺ェ縺縺ァ縺. + +謚陦鍋噪縺ェ驛ィ蛻縺ョ逶ク隲縺ッ蜿励¢縺ェ縺縺ァ縺. + +髢狗匱迺ー蠅 +P4 2.4Gh/512MB/WinXP/cygwin + +athena縺ッ GPL 繝ゥ繧、繧サ繝ウ繧ケ繧貞ョ医j縺セ縺. +Ragnarok縺ッ Gravity縺ョ trademark縺ァ縺. +login-db for athena縺ッ athena縺九i謖√▲縺ヲ譚・縺滄Κ蛻繧帝勁縺代ー BSD 繝ゥ繧、繧サ繝ウ繧ケ縺ォ莉倥″縺セ縺. +cygwin縺ッ Redhat縺ョ trademark縺ァ縺. + +// 繧ウ繝ウ繝代う繝ォ縺ォ蟇セ縺励※. + +縺薙ョ繝励Ο繧ー繝ゥ繝繧偵さ繝ウ繝代う繝ォ縺吶k縺溘a縺ォ mysqlclient縺ォ蟇セ縺吶k library縺悟ソ隕√〒縺. +縺薙l縺ッ mysql縺ョ繧ス繝シ繧ケ繧偵さ繝ウ繝代う繝ォ縺吶l縺ー蠕励k縺薙→縺後〒縺阪∪縺. + +繧ウ繝ウ繝代う繝ォ縺ョ譎ゅ↓ -lmysqlclient 繧ェ繝励す繝ァ繝ウ縺悟ソ隕√〒縺. + +cygwin縺ョ蝣エ蜷医ッ縺縺上▽縺九ョ繝代ャ繝√′蠢隕√〒縺. \ No newline at end of file diff --git a/misc/src/login_sql/strlib.c b/misc/src/login_sql/strlib.c new file mode 100644 index 0000000..7f6e197 --- /dev/null +++ b/misc/src/login_sql/strlib.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#include "strlib.h" +#include "utils.h" + +//----------------------------------------------- +// string lib. +unsigned char* jstrescape (unsigned char* pt) { + //copy from here + unsigned char * ptr; + int i =0, j=0; + + //copy string to temporary + CREATE(ptr, char, J_MAX_MALLOC_SIZE); + strcpy (ptr,pt); + + while (ptr[i] != '\0') { + switch (ptr[i]) { + case '\'': + pt[j++] = '\\'; + pt[j++] = ptr[i++]; + break; + case '\\': + pt[j++] = '\\'; + pt[j++] = ptr[i++]; + break; + default: + pt[j++] = ptr[i++]; + } + } + pt[j++] = '\0'; + free (ptr); + return (unsigned char*) &pt[0]; +} + +unsigned char* jstrescapecpy (unsigned char* pt,unsigned char* spt) { + //copy from here + int i =0, j=0; + + while (spt[i] != '\0') { + switch (spt[i]) { + case '\'': + pt[j++] = '\\'; + pt[j++] = spt[i++]; + break; + case '\\': + pt[j++] = '\\'; + pt[j++] = spt[i++]; + break; + default: + pt[j++] = spt[i++]; + } + } + pt[j++] = '\0'; + return (unsigned char*) &pt[0]; +} diff --git a/misc/src/login_sql/strlib.h b/misc/src/login_sql/strlib.h new file mode 100644 index 0000000..ddf29ab --- /dev/null +++ b/misc/src/login_sql/strlib.h @@ -0,0 +1,9 @@ +#ifndef _J_STR_LIB_H_ +#define _J_STR_LIB_H_ +#define J_MAX_MALLOC_SIZE 65535 +// String function library. +// code by Jioh L. Jung (ziozzang@4wish.net) +// This code is under license "BSD" +unsigned char* jstrescape (unsigned char* pt); +unsigned char* jstrescapecpy (unsigned char* pt,unsigned char* spt); +#endif diff --git a/misc/src/login_sql/timer.h b/misc/src/login_sql/timer.h new file mode 100644 index 0000000..be9706a --- /dev/null +++ b/misc/src/login_sql/timer.h @@ -0,0 +1,43 @@ +// original : core.h 2003/03/14 11:55:25 Rev 1.4 + +#ifndef _TIMER_H_ +#define _TIMER_H_ + +#define BASE_TICK 5 + +#define TIMER_ONCE_AUTODEL 1 +#define TIMER_INTERVAL 2 +#define TIMER_REMOVE_HEAP 16 + +#define DIFF_TICK(a,b) ((int)((a)-(b))) + +// Struct declaration + +struct TimerData { + unsigned int tick; + int (*func)(int,unsigned int,int,int); + int id; + int data; + int type; + int interval; + int heap_pos; +}; + +// Function prototype declaration + +unsigned int gettick_nocache(void); +unsigned int gettick(void); + +int add_timer(unsigned int,int (*)(int,unsigned int,int,int),int,int); +int add_timer_interval(unsigned int,int (*)(int,unsigned int,int,int),int,int,int); +int delete_timer(int,int (*)(int,unsigned int,int,int)); + +int addtick_timer(int tid,unsigned int tick); +struct TimerData *get_timer(int tid); + +int do_timer(unsigned int tick); + +int add_timer_func_list(int (*)(int,unsigned int,int,int),char*); +char* search_timer_func_list(int (*)(int,unsigned int,int,int)); + +#endif // _TIMER_H_ diff --git a/misc/src/map/GNUmakefile b/misc/src/map/GNUmakefile new file mode 100644 index 0000000..852bbb9 --- /dev/null +++ b/misc/src/map/GNUmakefile @@ -0,0 +1,72 @@ +all: txt sql + +txt: txtobj map-server + +sql: sqlobj map-server_sql + +txtobj: + mkdir txtobj + +sqlobj: + mkdir sqlobj + +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/grfio.o ../common/db.o ../common/lock.o ../common/nullpo.o ../common/malloc.o +LIBS = -lz -lm + +map-server: txtobj/map.o txtobj/chrif.o txtobj/clif.o txtobj/pc.o txtobj/npc.o txtobj/chat.o txtobj/path.o txtobj/itemdb.o txtobj/mob.o txtobj/script.o txtobj/storage.o txtobj/skill.o txtobj/atcommand.o txtobj/battle.o txtobj/intif.o txtobj/trade.o txtobj/party.o txtobj/vending.o txtobj/guild.o txtobj/pet.o $(COMMON_OBJ) + $(CC) -o ../../$@ $^ $(LIBS) + +map-server_sql: sqlobj/map.o sqlobj/chrif.o sqlobj/clif.o sqlobj/pc.o sqlobj/npc.o sqlobj/chat.o sqlobj/path.o sqlobj/itemdb.o sqlobj/mob.o sqlobj/script.o sqlobj/storage.o sqlobj/skill.o sqlobj/atcommand.o sqlobj/battle.o sqlobj/intif.o sqlobj/trade.o sqlobj/party.o sqlobj/vending.o sqlobj/guild.o sqlobj/pet.o sqlobj/mail.o $(COMMON_OBJ) + $(CC) -o ../../$@ $^ $(LIB_S) + +txtobj/%.o: %.c + $(COMPILE.c) -DTXT_ONLY $(OUTPUT_OPTION) $< + +sqlobj/%.o: %.c + $(COMPILE.c) $(OUTPUT_OPTION) $< + +txtobj/map.o: map.c map.h chrif.h clif.h npc.h pc.h mob.h chat.h skill.h itemdb.h storage.h party.h pet.h atcommand.h ../common/core.h ../common/timer.h ../common/db.h ../common/grfio.h ../common/mmo.h +txtobj/chrif.o: chrif.c map.h battle.h chrif.h clif.h intif.h pc.h npc.h ../common/socket.h ../common/timer.h ../common/mmo.h +txtobj/clif.o: clif.c map.h chrif.h clif.h mob.h intif.h pc.h npc.h itemdb.h chat.h script.h storage.h party.h guild.h atcommand.h pet.h atcommand.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/version.h +txtobj/pc.o: pc.c map.h clif.h intif.h pc.h npc.h mob.h itemdb.h battle.h skill.h script.h party.h guild.h pet.h trade.h storage.h chat.h vending.h ../common/timer.h ../common/mmo.h ../common/db.h +txtobj/npc.o: npc.c map.h npc.h clif.h pc.h script.h mob.h itemdb.h battle.h ../common/db.h ../common/timer.h ../common/mmo.h +txtobj/chat.o: chat.c map.h clif.h pc.h chat.h ../common/db.h ../common/mmo.h +txtobj/path.o: path.c map.h battle.h ../common/mmo.h +txtobj/itemdb.o: itemdb.c map.h battle.h itemdb.h ../common/db.h ../common/grfio.h ../common/mmo.h +txtobj/mob.o: mob.c map.h clif.h intif.h pc.h mob.h skill.h battle.h npc.h itemdb.h ../common/timer.h ../common/socket.h ../common/mmo.h +txtobj/script.o: script.c itemdb.h map.h pc.h mob.h clif.h intif.h npc.h script.h storage.h skill.h pet.h battle.h ../common/timer.h ../common/socket.h ../common/db.h ../common/mmo.h ../common/lock.h +txtobj/storage.o: storage.c itemdb.h pc.h clif.h intif.h storage.h guild.h ../common/mmo.h ../common/db.h +txtobj/skill.o: skill.c skill.h map.h clif.h pc.h mob.h battle.h itemdb.h script.h ../common/timer.h ../common/mmo.h +txtobj/atcommand.o: atcommand.c atcommand.h itemdb.h pc.h map.h skill.h clif.h mob.h intif.h battle.h storage.h guild.h pet.h ../common/socket.h ../common/timer.h ../common/mmo.h +txtobj/battle.o: battle.c battle.h skill.h map.h mob.h pc.h pet.h guild.h ../common/timer.h ../common/mmo.h +txtobj/intif.o: intif.c intif.h chrif.h clif.h party.h guild.h storage.h map.h battle.h pet.h ../common/socket.h ../common/mmo.h +txtobj/trade.o: trade.c trade.h clif.h itemdb.h map.h pc.h npc.h ../common/mmo.h +txtobj/party.o: party.c party.h clif.h intif.h pc.h map.h battle.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h +txtobj/vending.o: vending.c vending.h clif.h itemdb.h map.h pc.h ../common/mmo.h +txtobj/guild.o: guild.c guild.h storage.h battle.h clif.h intif.h pc.h npc.h map.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h +txtobj/pet.o: pet.c pet.h map.h clif.h chrif.h intif.h pc.h itemdb.h battle.h mob.h npc.h script.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h + +sqlobj/map.o: map.c map.h chrif.h clif.h npc.h pc.h mob.h chat.h skill.h itemdb.h storage.h party.h pet.h atcommand.h ../common/core.h ../common/timer.h ../common/db.h ../common/grfio.h ../common/mmo.h +sqlobj/chrif.o: chrif.c map.h battle.h chrif.h clif.h intif.h pc.h npc.h ../common/socket.h ../common/timer.h ../common/mmo.h +sqlobj/clif.o: clif.c map.h chrif.h clif.h mob.h intif.h pc.h npc.h itemdb.h chat.h script.h storage.h party.h guild.h atcommand.h pet.h atcommand.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/version.h +sqlobj/pc.o: pc.c map.h clif.h intif.h pc.h npc.h mob.h itemdb.h battle.h skill.h script.h party.h guild.h pet.h trade.h storage.h chat.h vending.h ../common/timer.h ../common/mmo.h ../common/db.h +sqlobj/npc.o: npc.c map.h npc.h clif.h pc.h script.h mob.h itemdb.h battle.h ../common/db.h ../common/timer.h ../common/mmo.h +sqlobj/chat.o: chat.c map.h clif.h pc.h chat.h ../common/db.h ../common/mmo.h +sqlobj/path.o: path.c map.h battle.h ../common/mmo.h +sqlobj/itemdb.o: itemdb.c map.h battle.h itemdb.h ../common/db.h ../common/grfio.h ../common/mmo.h +sqlobj/mob.o: mob.c map.h clif.h intif.h pc.h mob.h skill.h battle.h npc.h itemdb.h ../common/timer.h ../common/socket.h ../common/mmo.h +sqlobj/script.o: script.c itemdb.h map.h pc.h mob.h clif.h intif.h npc.h script.h storage.h skill.h pet.h battle.h ../common/timer.h ../common/socket.h ../common/db.h ../common/mmo.h ../common/lock.h +sqlobj/storage.o: storage.c itemdb.h pc.h clif.h intif.h storage.h guild.h ../common/mmo.h ../common/db.h +sqlobj/skill.o: skill.c skill.h map.h clif.h pc.h mob.h battle.h itemdb.h script.h ../common/timer.h ../common/mmo.h +sqlobj/atcommand.o: atcommand.c atcommand.h itemdb.h pc.h map.h skill.h clif.h mob.h intif.h battle.h storage.h guild.h pet.h ../common/socket.h ../common/timer.h ../common/mmo.h +sqlobj/battle.o: battle.c battle.h skill.h map.h mob.h pc.h pet.h guild.h ../common/timer.h ../common/mmo.h +sqlobj/intif.o: intif.c intif.h chrif.h clif.h party.h guild.h storage.h map.h battle.h pet.h ../common/socket.h ../common/mmo.h +sqlobj/trade.o: trade.c trade.h clif.h itemdb.h map.h pc.h npc.h ../common/mmo.h +sqlobj/party.o: party.c party.h clif.h intif.h pc.h map.h battle.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h +sqlobj/vending.o: vending.c vending.h clif.h itemdb.h map.h pc.h ../common/mmo.h +sqlobj/guild.o: guild.c guild.h storage.h battle.h clif.h intif.h pc.h npc.h map.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h +sqlobj/pet.o: pet.c pet.h map.h clif.h chrif.h intif.h pc.h itemdb.h battle.h mob.h npc.h script.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h +sqlobj/mail.o: mail.c mail.h + +clean: + rm -rf *.o ../../map-server ../../map-server_sql sqlobj txtobj diff --git a/misc/src/map/Makefile b/misc/src/map/Makefile new file mode 100644 index 0000000..3f5396e --- /dev/null +++ b/misc/src/map/Makefile @@ -0,0 +1,72 @@ +all: txt sql + +txt: txtobj map-server + +sql: sqlobj map-server_sql + +txtobj: + mkdir txtobj + +sqlobj: + mkdir sqlobj + +COMMON_OBJ = ../common/core.o ../common/socket.o ../common/timer.o ../common/grfio.o ../common/db.o ../common/lock.o ../common/nullpo.o ../common/malloc.o +LIBS = -lz -lm + +map-server: txtobj/map.o txtobj/chrif.o txtobj/clif.o txtobj/pc.o txtobj/npc.o txtobj/chat.o txtobj/path.o txtobj/itemdb.o txtobj/mob.o txtobj/script.o txtobj/storage.o txtobj/skill.o txtobj/atcommand.o txtobj/battle.o txtobj/intif.o txtobj/trade.o txtobj/party.o txtobj/vending.o txtobj/guild.o txtobj/pet.o $(COMMON_OBJ) + $(CC) -o ../../$@ $> $(LIBS) + +map-server_sql: sqlobj/map.o sqlobj/chrif.o sqlobj/clif.o sqlobj/pc.o sqlobj/npc.o sqlobj/chat.o sqlobj/path.o sqlobj/itemdb.o sqlobj/mob.o sqlobj/script.o sqlobj/storage.o sqlobj/skill.o sqlobj/atcommand.o sqlobj/battle.o sqlobj/intif.o sqlobj/trade.o sqlobj/party.o sqlobj/vending.o sqlobj/guild.o sqlobj/pet.o sqlobj/mail.o $(COMMON_OBJ) + $(CC) -o ../../$@ $> $(LIB_S) + +txtobj/%.o: %.c + $(COMPILE.c) -DTXT_ONLY $(OUTPUT_OPTION) $< + +sqlobj/%.o: %.c + $(COMPILE.c) $(OUTPUT_OPTION) $< + +txtobj/map.o: map.c map.h chrif.h clif.h npc.h pc.h mob.h chat.h skill.h itemdb.h storage.h party.h pet.h atcommand.h ../common/core.h ../common/timer.h ../common/db.h ../common/grfio.h ../common/mmo.h +txtobj/chrif.o: chrif.c map.h battle.h chrif.h clif.h intif.h pc.h npc.h ../common/socket.h ../common/timer.h ../common/mmo.h +txtobj/clif.o: clif.c map.h chrif.h clif.h mob.h intif.h pc.h npc.h itemdb.h chat.h script.h storage.h party.h guild.h atcommand.h pet.h atcommand.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/version.h +txtobj/pc.o: pc.c map.h clif.h intif.h pc.h npc.h mob.h itemdb.h battle.h skill.h script.h party.h guild.h pet.h trade.h storage.h chat.h vending.h ../common/timer.h ../common/mmo.h ../common/db.h +txtobj/npc.o: npc.c map.h npc.h clif.h pc.h script.h mob.h itemdb.h battle.h ../common/db.h ../common/timer.h ../common/mmo.h +txtobj/chat.o: chat.c map.h clif.h pc.h chat.h ../common/db.h ../common/mmo.h +txtobj/path.o: path.c map.h battle.h ../common/mmo.h +txtobj/itemdb.o: itemdb.c map.h battle.h itemdb.h ../common/db.h ../common/grfio.h ../common/mmo.h +txtobj/mob.o: mob.c map.h clif.h intif.h pc.h mob.h skill.h battle.h npc.h itemdb.h ../common/timer.h ../common/socket.h ../common/mmo.h +txtobj/script.o: script.c itemdb.h map.h pc.h mob.h clif.h intif.h npc.h script.h storage.h skill.h pet.h battle.h ../common/timer.h ../common/socket.h ../common/db.h ../common/mmo.h ../common/lock.h +txtobj/storage.o: storage.c itemdb.h pc.h clif.h intif.h storage.h guild.h ../common/mmo.h ../common/db.h +txtobj/skill.o: skill.c skill.h map.h clif.h pc.h mob.h battle.h itemdb.h script.h ../common/timer.h ../common/mmo.h +txtobj/atcommand.o: atcommand.c atcommand.h itemdb.h pc.h map.h skill.h clif.h mob.h intif.h battle.h storage.h guild.h pet.h ../common/socket.h ../common/timer.h ../common/mmo.h +txtobj/battle.o: battle.c battle.h skill.h map.h mob.h pc.h pet.h guild.h ../common/timer.h ../common/mmo.h +txtobj/intif.o: intif.c intif.h chrif.h clif.h party.h guild.h storage.h map.h battle.h pet.h ../common/socket.h ../common/mmo.h +txtobj/trade.o: trade.c trade.h clif.h itemdb.h map.h pc.h npc.h ../common/mmo.h +txtobj/party.o: party.c party.h clif.h intif.h pc.h map.h battle.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h +txtobj/vending.o: vending.c vending.h clif.h itemdb.h map.h pc.h ../common/mmo.h +txtobj/guild.o: guild.c guild.h storage.h battle.h clif.h intif.h pc.h npc.h map.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h +txtobj/pet.o: pet.c pet.h map.h clif.h chrif.h intif.h pc.h itemdb.h battle.h mob.h npc.h script.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h + +sqlobj/map.o: map.c map.h chrif.h clif.h npc.h pc.h mob.h chat.h skill.h itemdb.h storage.h party.h pet.h atcommand.h ../common/core.h ../common/timer.h ../common/db.h ../common/grfio.h ../common/mmo.h +sqlobj/chrif.o: chrif.c map.h battle.h chrif.h clif.h intif.h pc.h npc.h ../common/socket.h ../common/timer.h ../common/mmo.h +sqlobj/clif.o: clif.c map.h chrif.h clif.h mob.h intif.h pc.h npc.h itemdb.h chat.h script.h storage.h party.h guild.h atcommand.h pet.h atcommand.h ../common/socket.h ../common/timer.h ../common/mmo.h ../common/version.h +sqlobj/pc.o: pc.c map.h clif.h intif.h pc.h npc.h mob.h itemdb.h battle.h skill.h script.h party.h guild.h pet.h trade.h storage.h chat.h vending.h ../common/timer.h ../common/mmo.h ../common/db.h +sqlobj/npc.o: npc.c map.h npc.h clif.h pc.h script.h mob.h itemdb.h battle.h ../common/db.h ../common/timer.h ../common/mmo.h +sqlobj/chat.o: chat.c map.h clif.h pc.h chat.h ../common/db.h ../common/mmo.h +sqlobj/path.o: path.c map.h battle.h ../common/mmo.h +sqlobj/itemdb.o: itemdb.c map.h battle.h itemdb.h ../common/db.h ../common/grfio.h ../common/mmo.h +sqlobj/mob.o: mob.c map.h clif.h intif.h pc.h mob.h skill.h battle.h npc.h itemdb.h ../common/timer.h ../common/socket.h ../common/mmo.h +sqlobj/script.o: script.c itemdb.h map.h pc.h mob.h clif.h intif.h npc.h script.h storage.h skill.h pet.h battle.h ../common/timer.h ../common/socket.h ../common/db.h ../common/mmo.h ../common/lock.h +sqlobj/storage.o: storage.c itemdb.h pc.h clif.h intif.h storage.h guild.h ../common/mmo.h ../common/db.h +sqlobj/skill.o: skill.c skill.h map.h clif.h pc.h mob.h battle.h itemdb.h script.h ../common/timer.h ../common/mmo.h +sqlobj/atcommand.o: atcommand.c atcommand.h itemdb.h pc.h map.h skill.h clif.h mob.h intif.h battle.h storage.h guild.h pet.h ../common/socket.h ../common/timer.h ../common/mmo.h +sqlobj/battle.o: battle.c battle.h skill.h map.h mob.h pc.h pet.h guild.h ../common/timer.h ../common/mmo.h +sqlobj/intif.o: intif.c intif.h chrif.h clif.h party.h guild.h storage.h map.h battle.h pet.h ../common/socket.h ../common/mmo.h +sqlobj/trade.o: trade.c trade.h clif.h itemdb.h map.h pc.h npc.h ../common/mmo.h +sqlobj/party.o: party.c party.h clif.h intif.h pc.h map.h battle.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h +sqlobj/vending.o: vending.c vending.h clif.h itemdb.h map.h pc.h ../common/mmo.h +sqlobj/guild.o: guild.c guild.h storage.h battle.h clif.h intif.h pc.h npc.h map.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h +sqlobj/pet.o: pet.c pet.h map.h clif.h chrif.h intif.h pc.h itemdb.h battle.h mob.h npc.h script.h ../common/db.h ../common/socket.h ../common/timer.h ../common/mmo.h +sqlobj/mail.o: mail.c mail.h + +clean: + rm -rf *.o ../../map-server ../../map-server_sql sqlobj txtobj diff --git a/misc/src/map/atcommand.c b/misc/src/map/atcommand.c new file mode 100644 index 0000000..8dc39ba --- /dev/null +++ b/misc/src/map/atcommand.c @@ -0,0 +1,7753 @@ +// $Id: atcommand.c 148 2004-09-30 14:05:37Z MouseJstr $ +#include +#include +#include +#include +#include + +#include "socket.h" +#include "timer.h" +#include "nullpo.h" + +#include "clif.h" +#include "chrif.h" +#include "intif.h" +#include "itemdb.h" +#include "map.h" +#include "pc.h" +#include "skill.h" +#include "mob.h" +#include "pet.h" +#include "battle.h" +#include "party.h" +#include "guild.h" +#include "atcommand.h" +#include "script.h" +#include "npc.h" +#include "trade.h" +#include "core.h" + +#ifndef TXT_ONLY +#include "mail.h" +#endif + +#define STATE_BLIND 0x10 + +static char command_symbol = '@'; // first char of the commands (by [Yor]) + +static char msg_table[1000][1024]; // Server messages (0-499 reserved for GM commands, 500-999 reserved for others) + +#define ATCOMMAND_FUNC(x) int atcommand_ ## x (const int fd, struct map_session_data* sd, const char* command, const char* message) +ATCOMMAND_FUNC(broadcast); +ATCOMMAND_FUNC(localbroadcast); +ATCOMMAND_FUNC(rurap); +ATCOMMAND_FUNC(rura); +ATCOMMAND_FUNC(where); +ATCOMMAND_FUNC(jumpto); +ATCOMMAND_FUNC(jump); +ATCOMMAND_FUNC(who); +ATCOMMAND_FUNC(who2); +ATCOMMAND_FUNC(who3); +ATCOMMAND_FUNC(whomap); +ATCOMMAND_FUNC(whomap2); +ATCOMMAND_FUNC(whomap3); +ATCOMMAND_FUNC(whogm); // by Yor +ATCOMMAND_FUNC(save); +ATCOMMAND_FUNC(load); +ATCOMMAND_FUNC(speed); +ATCOMMAND_FUNC(storage); +ATCOMMAND_FUNC(guildstorage); +ATCOMMAND_FUNC(option); +ATCOMMAND_FUNC(hide); +ATCOMMAND_FUNC(jobchange); +ATCOMMAND_FUNC(die); +ATCOMMAND_FUNC(kill); +ATCOMMAND_FUNC(alive); +ATCOMMAND_FUNC(kami); +ATCOMMAND_FUNC(heal); +ATCOMMAND_FUNC(item); +ATCOMMAND_FUNC(item2); +ATCOMMAND_FUNC(itemreset); +ATCOMMAND_FUNC(itemcheck); +ATCOMMAND_FUNC(baselevelup); +ATCOMMAND_FUNC(joblevelup); +ATCOMMAND_FUNC(help); +ATCOMMAND_FUNC(gm); +ATCOMMAND_FUNC(pvpoff); +ATCOMMAND_FUNC(pvpon); +ATCOMMAND_FUNC(gvgoff); +ATCOMMAND_FUNC(gvgon); +ATCOMMAND_FUNC(model); +ATCOMMAND_FUNC(go); +ATCOMMAND_FUNC(monster); +ATCOMMAND_FUNC(spawn); +ATCOMMAND_FUNC(killmonster); +ATCOMMAND_FUNC(killmonster2); +ATCOMMAND_FUNC(refine); +ATCOMMAND_FUNC(produce); +ATCOMMAND_FUNC(memo); +ATCOMMAND_FUNC(gat); +ATCOMMAND_FUNC(packet); +ATCOMMAND_FUNC(statuspoint); +ATCOMMAND_FUNC(skillpoint); +ATCOMMAND_FUNC(zeny); +ATCOMMAND_FUNC(param); +ATCOMMAND_FUNC(guildlevelup); +ATCOMMAND_FUNC(makeegg); +ATCOMMAND_FUNC(hatch); +ATCOMMAND_FUNC(petfriendly); +ATCOMMAND_FUNC(pethungry); +ATCOMMAND_FUNC(petrename); +ATCOMMAND_FUNC(charpetrename); // by Yor +ATCOMMAND_FUNC(recall); +ATCOMMAND_FUNC(recallall); +ATCOMMAND_FUNC(character_job); +ATCOMMAND_FUNC(revive); +ATCOMMAND_FUNC(character_stats); +ATCOMMAND_FUNC(character_stats_all); +ATCOMMAND_FUNC(character_option); +ATCOMMAND_FUNC(character_save); +ATCOMMAND_FUNC(night); +ATCOMMAND_FUNC(day); +ATCOMMAND_FUNC(doom); +ATCOMMAND_FUNC(doommap); +ATCOMMAND_FUNC(raise); +ATCOMMAND_FUNC(raisemap); +ATCOMMAND_FUNC(character_baselevel); +ATCOMMAND_FUNC(character_joblevel); +ATCOMMAND_FUNC(kick); +ATCOMMAND_FUNC(kickall); +ATCOMMAND_FUNC(allskill); +ATCOMMAND_FUNC(questskill); +ATCOMMAND_FUNC(charquestskill); +ATCOMMAND_FUNC(lostskill); +ATCOMMAND_FUNC(charlostskill); +ATCOMMAND_FUNC(spiritball); +ATCOMMAND_FUNC(party); +ATCOMMAND_FUNC(guild); +ATCOMMAND_FUNC(charskreset); +ATCOMMAND_FUNC(charstreset); +ATCOMMAND_FUNC(charreset); +ATCOMMAND_FUNC(charstpoint); +ATCOMMAND_FUNC(charmodel); +ATCOMMAND_FUNC(charskpoint); +ATCOMMAND_FUNC(charzeny); +ATCOMMAND_FUNC(agitstart); +ATCOMMAND_FUNC(agitend); +ATCOMMAND_FUNC(reloaditemdb); +ATCOMMAND_FUNC(reloadmobdb); +ATCOMMAND_FUNC(reloadskilldb); +#ifndef TXT_ONLY +ATCOMMAND_FUNC(rehash);// by Fr3DBr +#else /* TXT_ONLY */ +ATCOMMAND_FUNC(reloadscript); +#endif /* TXT_ONLY */ +ATCOMMAND_FUNC(reloadgmdb); // by Yor +ATCOMMAND_FUNC(mapexit); +ATCOMMAND_FUNC(idsearch); +ATCOMMAND_FUNC(mapinfo); +ATCOMMAND_FUNC(dye); //** by fritz +ATCOMMAND_FUNC(hair_style); //** by fritz +ATCOMMAND_FUNC(hair_color); //** by fritz +ATCOMMAND_FUNC(stat_all); //** by fritz +ATCOMMAND_FUNC(char_change_sex); // by Yor +ATCOMMAND_FUNC(char_block); // by Yor +ATCOMMAND_FUNC(char_ban); // by Yor +ATCOMMAND_FUNC(char_unblock); // by Yor +ATCOMMAND_FUNC(char_unban); // by Yor +ATCOMMAND_FUNC(mount_peco); // by Valaris +ATCOMMAND_FUNC(char_mount_peco); // by Yor +ATCOMMAND_FUNC(guildspy); // [Syrus22] +ATCOMMAND_FUNC(partyspy); // [Syrus22] +ATCOMMAND_FUNC(repairall); // [Valaris] +ATCOMMAND_FUNC(guildrecall); // by Yor +ATCOMMAND_FUNC(partyrecall); // by Yor +//ATCOMMAND_FUNC(nuke); // [Valaris] +ATCOMMAND_FUNC(enablenpc); +ATCOMMAND_FUNC(disablenpc); +ATCOMMAND_FUNC(servertime); // by Yor +ATCOMMAND_FUNC(chardelitem); // by Yor +ATCOMMAND_FUNC(jail); // by Yor +ATCOMMAND_FUNC(unjail); // by Yor +ATCOMMAND_FUNC(disguise); // [Valaris] +ATCOMMAND_FUNC(undisguise); // by Yor +ATCOMMAND_FUNC(ignorelist); // by Yor +ATCOMMAND_FUNC(charignorelist); // by Yor +ATCOMMAND_FUNC(inall); // by Yor +ATCOMMAND_FUNC(exall); // by Yor +ATCOMMAND_FUNC(chardisguise); // Kalaspuff +ATCOMMAND_FUNC(charundisguise); // Kalaspuff +ATCOMMAND_FUNC(email); // by Yor +ATCOMMAND_FUNC(effect);//by Apple +ATCOMMAND_FUNC(character_item_list); // by Yor +ATCOMMAND_FUNC(character_storage_list); // by Yor +ATCOMMAND_FUNC(character_cart_list); // by Yor +ATCOMMAND_FUNC(addwarp); // by MouseJstr +ATCOMMAND_FUNC(follow); // by MouseJstr +ATCOMMAND_FUNC(skillon); // by MouseJstr +ATCOMMAND_FUNC(skilloff); // by MouseJstr +ATCOMMAND_FUNC(killer); // by MouseJstr +ATCOMMAND_FUNC(npcmove); // by MouseJstr +ATCOMMAND_FUNC(killable); // by MouseJstr +ATCOMMAND_FUNC(charkillable); // by MouseJstr +ATCOMMAND_FUNC(chareffect); // by MouseJstr +ATCOMMAND_FUNC(chardye); // by MouseJstr +ATCOMMAND_FUNC(charhairstyle); // by MouseJstr +ATCOMMAND_FUNC(charhaircolor); // by MouseJstr +ATCOMMAND_FUNC(dropall); // by MouseJstr +ATCOMMAND_FUNC(chardropall); // by MouseJstr +ATCOMMAND_FUNC(storeall); // by MouseJstr +ATCOMMAND_FUNC(charstoreall); // by MouseJstr +ATCOMMAND_FUNC(skillid); // by MouseJstr +ATCOMMAND_FUNC(useskill); // by MouseJstr +ATCOMMAND_FUNC(summon); +ATCOMMAND_FUNC(rain); +ATCOMMAND_FUNC(snow); +ATCOMMAND_FUNC(sakura); +ATCOMMAND_FUNC(fog); +ATCOMMAND_FUNC(leaves); +ATCOMMAND_FUNC(adjgmlvl); // by MouseJstr +ATCOMMAND_FUNC(adjcmdlvl); // by MouseJstr +ATCOMMAND_FUNC(trade); // by MouseJstr +ATCOMMAND_FUNC(unmute); // [Valaris] + +#ifndef TXT_ONLY +ATCOMMAND_FUNC(checkmail); // [Valaris] +ATCOMMAND_FUNC(listmail); // [Valaris] +ATCOMMAND_FUNC(listnewmail); // [Valaris] +ATCOMMAND_FUNC(readmail); // [Valaris] +ATCOMMAND_FUNC(sendmail); // [Valaris] +ATCOMMAND_FUNC(sendprioritymail); // [Valaris] +ATCOMMAND_FUNC(deletemail); // [Valaris] +ATCOMMAND_FUNC(sound); // [Valaris] +ATCOMMAND_FUNC(refreshonline); // [Valaris] +#endif /* TXT_ONLY */ + +/*========================================== + *AtCommandInfo atcommand_info[]構造体の定義 + *------------------------------------------ + */ + +// First char of commands is configured in atcommand_athena.conf. Leave @ in this list for default value. +// to set default level, read atcommand_athena.conf first please. +static AtCommandInfo atcommand_info[] = { + { AtCommand_RuraP, "@rura+", 60, atcommand_rurap }, + { AtCommand_RuraP, "@charwarp", 60, atcommand_rurap }, + { AtCommand_Rura, "@rura", 40, atcommand_rura }, + { AtCommand_Warp, "@warp", 40, atcommand_rura }, + { AtCommand_Where, "@where", 1, atcommand_where }, + { AtCommand_JumpTo, "@jumpto", 20, atcommand_jumpto }, // + /shift + { AtCommand_JumpTo, "@warpto", 20, atcommand_jumpto }, + { AtCommand_JumpTo, "@goto", 20, atcommand_jumpto }, + { AtCommand_Jump, "@jump", 40, atcommand_jump }, + { AtCommand_Who, "@who", 20, atcommand_who }, + { AtCommand_Who, "@whois", 20, atcommand_who }, + { AtCommand_Who2, "@who2", 20, atcommand_who2 }, + { AtCommand_Who3, "@who3", 20, atcommand_who3 }, + { AtCommand_WhoMap, "@whomap", 20, atcommand_whomap }, + { AtCommand_WhoMap2, "@whomap2", 20, atcommand_whomap2 }, + { AtCommand_WhoMap3, "@whomap3", 20, atcommand_whomap3 }, + { AtCommand_WhoGM, "@whogm", 20, atcommand_whogm }, // by Yor + { AtCommand_Save, "@save", 40, atcommand_save }, + { AtCommand_Load, "@return", 40, atcommand_load }, + { AtCommand_Load, "@load", 40, atcommand_load }, + { AtCommand_Speed, "@speed", 40, atcommand_speed }, + { AtCommand_Storage, "@storage", 1, atcommand_storage }, + { AtCommand_GuildStorage, "@gstorage", 50, atcommand_guildstorage }, + { AtCommand_Option, "@option", 40, atcommand_option }, + { AtCommand_Hide, "@hide", 40, atcommand_hide }, // + /hide + { AtCommand_JobChange, "@jobchange", 40, atcommand_jobchange }, + { AtCommand_JobChange, "@job", 40, atcommand_jobchange }, + { AtCommand_Die, "@die", 1, atcommand_die }, + { AtCommand_Kill, "@kill", 60, atcommand_kill }, + { AtCommand_Alive, "@alive", 60, atcommand_alive }, + { AtCommand_Kami, "@kami", 40, atcommand_kami }, + { AtCommand_KamiB, "@kamib", 40, atcommand_kami }, + { AtCommand_Heal, "@heal", 40, atcommand_heal }, + { AtCommand_Item, "@item", 60, atcommand_item }, + { AtCommand_Item2, "@item2", 60, atcommand_item2 }, + { AtCommand_ItemReset, "@itemreset", 40, atcommand_itemreset }, + { AtCommand_ItemCheck, "@itemcheck", 60, atcommand_itemcheck }, + { AtCommand_BaseLevelUp, "@lvup", 60, atcommand_baselevelup }, + { AtCommand_BaseLevelUp, "@blevel", 60, atcommand_baselevelup }, + { AtCommand_BaseLevelUp, "@baselvlup", 60, atcommand_baselevelup }, + { AtCommand_JobLevelUp, "@jlevel", 60, atcommand_joblevelup }, + { AtCommand_JobLevelUp, "@joblvup", 60, atcommand_joblevelup }, + { AtCommand_JobLevelUp, "@joblvlup", 60, atcommand_joblevelup }, + { AtCommand_H, "@h", 20, atcommand_help }, + { AtCommand_Help, "@help", 20, atcommand_help }, + { AtCommand_GM, "@gm", 100, atcommand_gm }, + { AtCommand_PvPOff, "@pvpoff", 40, atcommand_pvpoff }, + { AtCommand_PvPOn, "@pvpon", 40, atcommand_pvpon }, + { AtCommand_GvGOff, "@gvgoff", 40, atcommand_gvgoff }, + { AtCommand_GvGOff, "@gpvpoff", 40, atcommand_gvgoff }, + { AtCommand_GvGOn, "@gvgon", 40, atcommand_gvgon }, + { AtCommand_GvGOn, "@gpvpon", 40, atcommand_gvgon }, + { AtCommand_Model, "@model", 20, atcommand_model }, + { AtCommand_Go, "@go", 10, atcommand_go }, + { AtCommand_Spawn, "@monster", 50, atcommand_spawn }, + { AtCommand_Spawn, "@spawn", 50, atcommand_spawn }, + //{ AtCommand_Spawn, "@summon", 50, atcommand_spawn }, + { AtCommand_Monster, "@monster2", 50, atcommand_monster }, + { AtCommand_KillMonster, "@killmonster", 60, atcommand_killmonster }, + { AtCommand_KillMonster2, "@killmonster2", 40, atcommand_killmonster2 }, + { AtCommand_Refine, "@refine", 60, atcommand_refine }, + { AtCommand_Produce, "@produce", 60, atcommand_produce }, + { AtCommand_Memo, "@memo", 40, atcommand_memo }, + { AtCommand_GAT, "@gat", 99, atcommand_gat }, // debug function + { AtCommand_Packet, "@packet", 99, atcommand_packet }, // debug function + { AtCommand_StatusPoint, "@stpoint", 60, atcommand_statuspoint }, + { AtCommand_SkillPoint, "@skpoint", 60, atcommand_skillpoint }, + { AtCommand_Zeny, "@zeny", 60, atcommand_zeny }, + { AtCommand_Strength, "@str", 60, atcommand_param }, + { AtCommand_Agility, "@agi", 60, atcommand_param }, + { AtCommand_Vitality, "@vit", 60, atcommand_param }, + { AtCommand_Intelligence, "@int", 60, atcommand_param }, + { AtCommand_Dexterity, "@dex", 60, atcommand_param }, + { AtCommand_Luck, "@luk", 60, atcommand_param }, + { AtCommand_GuildLevelUp, "@guildlvup", 60, atcommand_guildlevelup }, + { AtCommand_GuildLevelUp, "@guildlvlup", 60, atcommand_guildlevelup }, + { AtCommand_MakeEgg, "@makeegg", 60, atcommand_makeegg }, + { AtCommand_Hatch, "@hatch", 60, atcommand_hatch }, + { AtCommand_PetFriendly, "@petfriendly", 40, atcommand_petfriendly }, + { AtCommand_PetHungry, "@pethungry", 40, atcommand_pethungry }, + { AtCommand_PetRename, "@petrename", 1, atcommand_petrename }, + { AtCommand_CharPetRename, "@charpetrename", 50, atcommand_charpetrename }, // by Yor + { AtCommand_Recall, "@recall", 60, atcommand_recall }, // + /recall + { AtCommand_CharacterJob, "@charjob", 60, atcommand_character_job }, + { AtCommand_CharacterJob, "@charjobchange", 60, atcommand_character_job }, + { AtCommand_Revive, "@revive", 60, atcommand_revive }, + { AtCommand_CharacterStats, "@charstats", 40, atcommand_character_stats }, + { AtCommand_CharacterStatsAll, "@charstatsall", 40, atcommand_character_stats_all }, + { AtCommand_CharacterOption, "@charoption", 60, atcommand_character_option }, + { AtCommand_CharacterSave, "@charsave", 60, atcommand_character_save }, + { AtCommand_Night, "@night", 80, atcommand_night }, + { AtCommand_Day, "@day", 80, atcommand_day }, + { AtCommand_Doom, "@doom", 80, atcommand_doom }, + { AtCommand_DoomMap, "@doommap", 80, atcommand_doommap }, + { AtCommand_Raise, "@raise", 80, atcommand_raise }, + { AtCommand_RaiseMap, "@raisemap", 80, atcommand_raisemap }, + { AtCommand_CharacterBaseLevel, "@charbaselvl", 60, atcommand_character_baselevel }, + { AtCommand_CharacterJobLevel, "@charjlvl", 60, atcommand_character_joblevel }, + { AtCommand_Kick, "@kick", 20, atcommand_kick }, // + right click menu for GM "(name) force to quit" + { AtCommand_KickAll, "@kickall", 99, atcommand_kickall }, + { AtCommand_AllSkill, "@allskill", 60, atcommand_allskill }, + { AtCommand_AllSkill, "@allskills", 60, atcommand_allskill }, + { AtCommand_AllSkill, "@skillall", 60, atcommand_allskill }, + { AtCommand_AllSkill, "@skillsall", 60, atcommand_allskill }, + { AtCommand_QuestSkill, "@questskill", 40, atcommand_questskill }, + { AtCommand_CharQuestSkill, "@charquestskill", 60, atcommand_charquestskill }, + { AtCommand_LostSkill, "@lostskill", 40, atcommand_lostskill }, + { AtCommand_CharLostSkill, "@charlostskill", 60, atcommand_charlostskill }, + { AtCommand_SpiritBall, "@spiritball", 40, atcommand_spiritball }, + { AtCommand_Party, "@party", 1, atcommand_party }, + { AtCommand_Guild, "@guild", 50, atcommand_guild }, + { AtCommand_AgitStart, "@agitstart", 60, atcommand_agitstart }, + { AtCommand_AgitEnd, "@agitend", 60, atcommand_agitend }, + { AtCommand_MapExit, "@mapexit", 99, atcommand_mapexit }, + { AtCommand_IDSearch, "@idsearch", 60, atcommand_idsearch }, + { AtCommand_MapMove, "@mapmove", 40, atcommand_rura }, // /mm command + { AtCommand_Broadcast, "@broadcast", 40, atcommand_broadcast }, // /b and /nb command + { AtCommand_LocalBroadcast, "@localbroadcast", 40, atcommand_localbroadcast }, // /lb and /nlb command + { AtCommand_RecallAll, "@recallall", 80, atcommand_recallall }, + { AtCommand_CharSkReset, "@charskreset", 60, atcommand_charskreset }, + { AtCommand_CharStReset, "@charstreset", 60, atcommand_charstreset }, + { AtCommand_ReloadItemDB, "@reloaditemdb", 99, atcommand_reloaditemdb }, // admin command + { AtCommand_ReloadMobDB, "@reloadmobdb", 99, atcommand_reloadmobdb }, // admin command + { AtCommand_ReloadSkillDB, "@reloadskilldb", 99, atcommand_reloadskilldb }, // admin command +#ifndef TXT_ONLY + { AtCommand_Rehash, "@rehash", 99, atcommand_rehash }, // admin command +#else /* TXT_ONLY */ + { AtCommand_ReloadScript, "@reloadscript", 99, atcommand_reloadscript }, // admin command +#endif /* TXT_ONLY */ + { AtCommand_ReloadGMDB, "@reloadgmdb", 99, atcommand_reloadgmdb }, // admin command + { AtCommand_CharReset, "@charreset", 60, atcommand_charreset }, + { AtCommand_CharModel, "@charmodel", 50, atcommand_charmodel }, + { AtCommand_CharSKPoint, "@charskpoint", 60, atcommand_charskpoint }, + { AtCommand_CharSTPoint, "@charstpoint", 60, atcommand_charstpoint }, + { AtCommand_CharZeny, "@charzeny", 60, atcommand_charzeny }, + { AtCommand_MapInfo, "@mapinfo", 99, atcommand_mapinfo }, + { AtCommand_Dye, "@dye", 40, atcommand_dye }, // by fritz + { AtCommand_Dye, "@ccolor", 40, atcommand_dye }, // by fritz + { AtCommand_Hstyle, "@hairstyle", 40, atcommand_hair_style }, // by fritz + { AtCommand_Hstyle, "@hstyle", 40, atcommand_hair_style }, // by fritz + { AtCommand_Hcolor, "@haircolor", 40, atcommand_hair_color }, // by fritz + { AtCommand_Hcolor, "@hcolor", 40, atcommand_hair_color }, // by fritz + { AtCommand_StatAll, "@statall", 60, atcommand_stat_all }, // by fritz + { AtCommand_StatAll, "@statsall", 60, atcommand_stat_all }, + { AtCommand_StatAll, "@allstats", 60, atcommand_stat_all }, // by fritz + { AtCommand_StatAll, "@allstat", 60, atcommand_stat_all }, // by fritz + { AtCommand_CharChangeSex, "@charchangesex", 60, atcommand_char_change_sex }, // by Yor + { AtCommand_CharBlock, "@block", 60, atcommand_char_block }, // by Yor + { AtCommand_CharBlock, "@charblock", 60, atcommand_char_block }, // by Yor + { AtCommand_CharBan, "@ban", 60, atcommand_char_ban }, // by Yor + { AtCommand_CharBan, "@banish", 60, atcommand_char_ban }, // by Yor + { AtCommand_CharBan, "@charban", 60, atcommand_char_ban }, // by Yor + { AtCommand_CharBan, "@charbanish", 60, atcommand_char_ban }, // by Yor + { AtCommand_CharUnBlock, "@unblock", 60, atcommand_char_unblock }, // by Yor + { AtCommand_CharUnBlock, "@charunblock", 60, atcommand_char_unblock }, // by Yor + { AtCommand_CharUnBan, "@unban", 60, atcommand_char_unban }, // by Yor + { AtCommand_CharUnBan, "@unbanish", 60, atcommand_char_unban }, // by Yor + { AtCommand_CharUnBan, "@charunban", 60, atcommand_char_unban }, // by Yor + { AtCommand_CharUnBan, "@charunbanish", 60, atcommand_char_unban }, // by Yor + { AtCommand_MountPeco, "@mountpeco", 20, atcommand_mount_peco }, // by Valaris + { AtCommand_CharMountPeco, "@charmountpeco", 50, atcommand_char_mount_peco }, // by Yor + { AtCommand_GuildSpy, "@guildspy", 60, atcommand_guildspy }, // [Syrus22] + { AtCommand_PartySpy, "@partyspy", 60, atcommand_partyspy }, // [Syrus22] + { AtCommand_RepairAll, "@repairall", 60, atcommand_repairall }, // [Valaris] + { AtCommand_GuildRecall, "@guildrecall", 60, atcommand_guildrecall }, // by Yor + { AtCommand_PartyRecall, "@partyrecall", 60, atcommand_partyrecall }, // by Yor +// { AtCommand_Nuke, "@nuke", 60, atcommand_nuke }, // [Valaris] + { AtCommand_Enablenpc, "@enablenpc", 80, atcommand_enablenpc }, // [] + { AtCommand_Disablenpc, "@disablenpc", 80, atcommand_disablenpc }, // [] + { AtCommand_ServerTime, "@time", 0, atcommand_servertime }, // by Yor + { AtCommand_ServerTime, "@date", 0, atcommand_servertime }, // by Yor + { AtCommand_ServerTime, "@server_date", 0, atcommand_servertime }, // by Yor + { AtCommand_ServerTime, "@serverdate", 0, atcommand_servertime }, // by Yor + { AtCommand_ServerTime, "@server_time", 0, atcommand_servertime }, // by Yor + { AtCommand_ServerTime, "@servertime", 0, atcommand_servertime }, // by Yor + { AtCommand_CharDelItem, "@chardelitem", 60, atcommand_chardelitem }, // by Yor + { AtCommand_Jail, "@jail", 60, atcommand_jail }, // by Yor + { AtCommand_UnJail, "@unjail", 60, atcommand_unjail }, // by Yor + { AtCommand_UnJail, "@discharge", 60, atcommand_unjail }, // by Yor + { AtCommand_Disguise, "@disguise", 20, atcommand_disguise }, // [Valaris] + { AtCommand_UnDisguise, "@undisguise", 20, atcommand_undisguise }, // by Yor + { AtCommand_IgnoreList, "@ignorelist", 0, atcommand_ignorelist }, // by Yor + { AtCommand_CharIgnoreList, "@charignorelist", 20, atcommand_charignorelist }, // by Yor + { AtCommand_IgnoreList, "@inall", 20, atcommand_inall }, // by Yor + { AtCommand_ExAll, "@exall", 20, atcommand_exall }, // by Yor + { AtCommand_CharDisguise, "@chardisguise", 60, atcommand_chardisguise }, // Kalaspuff + { AtCommand_CharUnDisguise, "@charundisguise", 60, atcommand_charundisguise }, // Kalaspuff + { AtCommand_EMail, "@email", 0, atcommand_email }, // by Yor + { AtCommand_Effect, "@effect", 40, atcommand_effect }, // by Apple + { AtCommand_Char_Item_List, "@charitemlist", 40, atcommand_character_item_list }, // by Yor + { AtCommand_Char_Storage_List, "@charstoragelist", 40, atcommand_character_storage_list }, // by Yor + { AtCommand_Char_Cart_List, "@charcartlist", 40, atcommand_character_cart_list }, // by Yor + { AtCommand_Follow, "@follow", 10, atcommand_follow }, // by MouseJstr + { AtCommand_AddWarp, "@addwarp", 20, atcommand_addwarp }, // by MouseJstr + { AtCommand_SkillOn, "@skillon", 20, atcommand_skillon }, // by MouseJstr + { AtCommand_SkillOff, "@skilloff", 20, atcommand_skilloff }, // by MouseJstr + { AtCommand_Killer, "@killer", 60, atcommand_killer }, // by MouseJstr + { AtCommand_NpcMove, "@npcmove", 20, atcommand_npcmove }, // by MouseJstr + { AtCommand_Killable, "@killable", 40, atcommand_killable }, // by MouseJstr + { AtCommand_CharKillable, "@charkillable", 40, atcommand_charkillable }, // by MouseJstr + { AtCommand_Chareffect, "@chareffect", 40, atcommand_chareffect }, // MouseJstr + //{ AtCommand_Chardye, "@chardye", 40, atcommand_chardye }, // MouseJstr + //{ AtCommand_Charhairstyle, "@charhairstyle", 40, atcommand_charhairstyle }, // MouseJstr + //{ AtCommand_Charhaircolor, "@charhaircolor", 40, atcommand_charhaircolor }, // MouseJstr + { AtCommand_Dropall, "@dropall", 40, atcommand_dropall }, // MouseJstr + { AtCommand_Chardropall, "@chardropall", 40, atcommand_chardropall }, // MouseJstr + { AtCommand_Storeall, "@storeall", 40, atcommand_storeall }, // MouseJstr + { AtCommand_Charstoreall, "@charstoreall", 40, atcommand_charstoreall }, // MouseJstr + { AtCommand_Skillid, "@skillid", 40, atcommand_skillid }, // MouseJstr + { AtCommand_Useskill, "@useskill", 40, atcommand_useskill }, // MouseJstr + { AtCommand_Rain, "@rain", 99, atcommand_rain }, + { AtCommand_Snow, "@snow", 99, atcommand_snow }, + { AtCommand_Sakura, "@sakura", 99, atcommand_sakura }, + { AtCommand_Fog, "@fog", 99, atcommand_fog }, + { AtCommand_Leaves, "@leaves", 99, atcommand_leaves }, + //{ AtCommand_Shuffle, "@shuffle", 99, atcommand_shuffle }, + //{ AtCommand_Maintenance, "@maintenance", 99, atcommand_maintenance }, + //{ AtCommand_Misceffect, "@misceffect", 60, atcommand_misceffect }, + { AtCommand_Summon, "@summon", 60, atcommand_summon }, + { AtCommand_AdjGmLvl, "@adjgmlvl", 99, atcommand_adjgmlvl }, + { AtCommand_AdjCmdLvl, "@adjcmdlvl", 99, atcommand_adjcmdlvl }, + { AtCommand_Trade, "@trade", 60, atcommand_trade }, + { AtCommand_UnMute, "@unmute", 60, atcommand_unmute }, // [Valaris] + +#ifndef TXT_ONLY // sql-only commands + { AtCommand_CheckMail, "@checkmail", 1, atcommand_listmail }, // [Valaris] + { AtCommand_ListMail, "@listmail", 1, atcommand_listmail }, // [Valaris] + { AtCommand_ListNewMail, "@listnewmail", 1, atcommand_listmail }, // [Valaris] + { AtCommand_ReadMail, "@readmail", 1, atcommand_readmail }, // [Valaris] + { AtCommand_DeleteMail, "@deletemail", 1, atcommand_readmail }, // [Valaris] + { AtCommand_SendMail, "@sendmail", 1, atcommand_sendmail }, // [Valaris] + { AtCommand_SendPriorityMail, "@sendprioritymail",80, atcommand_sendmail }, // [Valaris] + { AtCommand_RefreshOnline, "@refreshonline", 99, atcommand_refreshonline }, // [Valaris] +#endif /* TXT_ONLY */ + +// add new commands before this line + { AtCommand_Unknown, NULL, 1, NULL } +}; + +/*==================================================== + * This function return the name of the job (by [Yor]) + *---------------------------------------------------- + */ +char * job_name(int class) { + switch (class) { + case 0: return "Novice"; + case 1: return "Swordsman"; + case 2: return "Mage"; + case 3: return "Archer"; + case 4: return "Acolyte"; + case 5: return "Merchant"; + case 6: return "Thief"; + case 7: return "Knight"; + case 8: return "Priest"; + case 9: return "Wizard"; + case 10: return "Blacksmith"; + case 11: return "Hunter"; + case 12: return "Assassin"; + case 13: return "Knight 2"; + case 14: return "Crusader"; + case 15: return "Monk"; + case 16: return "Sage"; + case 17: return "Rogue"; + case 18: return "Alchemist"; + case 19: return "Bard"; + case 20: return "Dancer"; + case 21: return "Crusader 2"; + case 22: return "Wedding"; + case 23: return "Super Novice"; + case 4001: return "Novice High"; + case 4002: return "Swordsman High"; + case 4003: return "Mage High"; + case 4004: return "Archer High"; + case 4005: return "Acolyte High"; + case 4006: return "Merchant High"; + case 4007: return "Thief High"; + case 4008: return "Lord Knight"; + case 4009: return "High Priest"; + case 4010: return "High Wizard"; + case 4011: return "Whitesmith"; + case 4012: return "Sniper"; + case 4013: return "Assassin Cross"; + case 4014: return "Peko Knight"; + case 4015: return "Paladin"; + case 4016: return "Champion"; + case 4017: return "Professor"; + case 4018: return "Stalker"; + case 4019: return "Creator"; + case 4020: return "Clown"; + case 4021: return "Gypsy"; + case 4022: return "Peko Paladin"; + case 4023: return "Baby Novice"; + case 4024: return "Baby Swordsman"; + case 4025: return "Baby Mage"; + case 4026: return "Baby Archer"; + case 4027: return "Baby Acolyte"; + case 4028: return "Baby Merchant"; + case 4029: return "Baby Thief"; + case 4030: return "Baby Knight"; + case 4031: return "Baby Priest"; + case 4032: return "Baby Wizard"; + case 4033: return "Baby Blacksmith"; + case 4034: return "Baby Hunter"; + case 4035: return "Baby Assassin"; + case 4036: return "Baby Peco Knight"; + case 4037: return "Baby Crusader"; + case 4038: return "Baby Monk"; + case 4039: return "Baby Sage"; + case 4040: return "Baby Rogue"; + case 4041: return "Baby Alchemist"; + case 4042: return "Baby Bard"; + case 4043: return "Baby Dancer"; + case 4044: return "Baby Peco Crusader"; + case 4045: return "Super Baby"; + } + return "Unknown Job"; +} + +//----------------------------------------------------------- +// Return the message string of the specified number by [Yor] +//----------------------------------------------------------- +char * msg_txt(int msg_number) { + if (msg_number >= 0 && msg_number < (int)(sizeof(msg_table) / sizeof(msg_table[0])) && + msg_table[msg_number] != NULL && msg_table[msg_number][0] != '\0') + return msg_table[msg_number]; + + return "??"; +} + +//------------------------------------------------------------ +// E-mail check: return 0 (not correct) or 1 (valid). by [Yor] +//------------------------------------------------------------ +int e_mail_check(unsigned char *email) { + char ch; + unsigned char* last_arobas; + + // athena limits + if (strlen(email) < 3 || strlen(email) > 39) + return 0; + + // part of RFC limits (official reference of e-mail description) + if (strchr(email, '@') == NULL || email[strlen(email)-1] == '@') + return 0; + + if (email[strlen(email)-1] == '.') + return 0; + + last_arobas = strrchr(email, '@'); + + if (strstr(last_arobas, "@.") != NULL || + strstr(last_arobas, "..") != NULL) + return 0; + + for(ch = 1; ch < 32; ch++) { + if (strchr(last_arobas, ch) != NULL) { + return 0; + break; + } + } + + if (strchr(last_arobas, ' ') != NULL || + strchr(last_arobas, ';') != NULL) + return 0; + + // all correct + return 1; +} + +/*========================================== + * get_atcommand_level @コマンドの必要レベルを取得 + *------------------------------------------ + */ +int get_atcommand_level(const AtCommandType type) { + int i; + + for (i = 0; atcommand_info[i].type != AtCommand_None; i++) + if (atcommand_info[i].type == type) + return atcommand_info[i].level; + + return 100; // 100: command can not be used +} + +/*========================================== + *is_atcommand @コマンドに存在するかどうか確認する + *------------------------------------------ + */ +AtCommandType +is_atcommand(const int fd, struct map_session_data* sd, const char* message, int gmlvl) { + const char* str = message; + int s_flag = 0; + AtCommandInfo info; + AtCommandType type; + + nullpo_retr(AtCommand_None, sd); + + if (!message || !*message) + return AtCommand_None; + + memset(&info, 0, sizeof(info)); + str += strlen(sd->status.name); + while (*str && (isspace(*str) || (s_flag == 0 && *str == ':'))) { + if (*str == ':') + s_flag = 1; + str++; + } + if (!*str) + return AtCommand_None; + + type = atcommand(gmlvl > 0 ? gmlvl : pc_isGM(sd), str, &info); + if (type != AtCommand_None) { + char command[100]; + char output[200]; + const char* p = str; + memset(command, '\0', sizeof(command)); + memset(output, '\0', sizeof(output)); + while (*p && !isspace(*p)) + p++; + if (p - str >= sizeof(command)) // too long + return AtCommand_Unknown; + strncpy(command, str, p - str); + while (isspace(*p)) + p++; + + if (type == AtCommand_Unknown || info.proc == NULL) { + sprintf(output, msg_table[153], command); // %s is Unknown Command. + clif_displaymessage(fd, output); + } else { + if (info.proc(fd, sd, command, p) != 0) { + // Command can not be executed + sprintf(output, msg_table[154], command); // %s failed. + clif_displaymessage(fd, output); + } + } + + return info.type; + } + + return AtCommand_None; +} + +/*========================================== + * + *------------------------------------------ + */ +AtCommandType atcommand(const int level, const char* message, struct AtCommandInfo* info) { + char* p = (char *)message; // it's 'char' and not 'const char' to have possibility to modify the first character if necessary + + if (!info) + return AtCommand_None; + if (battle_config.atc_gmonly != 0 && !level) // level = pc_isGM(sd) + return AtCommand_None; + if (!p || !*p) { + fprintf(stderr, "at command message is empty\n"); + return AtCommand_None; + } + + if (*p == command_symbol) { // check first char. + char command[101]; + int i = 0; + memset(info, 0, sizeof(AtCommandInfo)); + sscanf(p, "%100s", command); + command[sizeof(command)-1] = '\0'; + + while (atcommand_info[i].type != AtCommand_Unknown) { + if (strcmpi(command+1, atcommand_info[i].command+1) == 0 && level >= atcommand_info[i].level) { + p[0] = atcommand_info[i].command[0]; // set correct first symbol for after. + break; + } + i++; + } + + if (atcommand_info[i].type == AtCommand_Unknown) { + // doesn't return Unknown if player is normal player (display the text, not display: unknown command) + if (level == 0) + return AtCommand_None; + else + return AtCommand_Unknown; + } + memcpy(info, &atcommand_info[i], sizeof atcommand_info[i]); + } else { + return AtCommand_None; + } + + return info->type; +} + +/*========================================== + * + *------------------------------------------ + */ +static int atkillmonster_sub(struct block_list *bl, va_list ap) { + int flag = va_arg(ap, int); + + nullpo_retr(0, bl); + + if (flag) + mob_damage(NULL, (struct mob_data *)bl, ((struct mob_data *)bl)->hp, 2); + else + mob_delete((struct mob_data *)bl); + + return 0; +} + +#ifndef TXT_ONLY +static int atkillnpc_sub(struct block_list *bl, va_list ap) +{ + int flag = va_arg(ap,int); + + nullpo_retr(0, bl); + + npc_delete((struct npc_data *)bl); + + flag = 0; + + return 0; +} + +void rehash( const int fd, struct map_session_data* sd ) +{ + int map_id = 0; + + int LOADED_MAPS = map_num; + + for (map_id = 0; map_id < LOADED_MAPS;map_id++) { + + if (map_id > LOADED_MAPS) + break; + + map_foreachinarea(atkillmonster_sub, map_id, 0, 0, map[map_id].xs, map[map_id].ys, BL_MOB, 0); + map_foreachinarea(atkillnpc_sub, map_id, 0, 0, map[map_id].xs, map[map_id].ys, BL_NPC, 0); + } +} + +#endif /* not TXT_ONLY */ +/*========================================== + * Read Message Data + *------------------------------------------ + */ +int msg_config_read(const char *cfgName) { + int msg_number; + char line[1024], w1[1024], w2[1024]; + FILE *fp; + + if ((fp = fopen(cfgName, "r")) == NULL) { + printf("Messages file not found: %s\n", cfgName); + return 1; + } + + while(fgets(line, sizeof(line)-1, fp)) { + if (line[0] == '/' && line[1] == '/') + continue; + if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) == 2) { + if (strcmpi(w1, "import") == 0) { + msg_config_read(w2); + } else { + msg_number = atoi(w1); + if (msg_number >= 0 && msg_number < (int)(sizeof(msg_table) / sizeof(msg_table[0]))) + strcpy(msg_table[msg_number], w2); + // printf("message #%d: '%s'.\n", msg_number, msg_table[msg_number]); + } + } + } + fclose(fp); + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +static AtCommandInfo* get_atcommandinfo_byname(const char* name) { + int i; + + for (i = 0; atcommand_info[i].type != AtCommand_Unknown; i++) + if (strcmpi(atcommand_info[i].command + 1, name) == 0) + return &atcommand_info[i]; + + return NULL; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_config_read(const char *cfgName) { + char line[1024], w1[1024], w2[1024]; + AtCommandInfo* p; + FILE* fp; + + if ((fp = fopen(cfgName, "r")) == NULL) { + printf("At commands configuration file not found: %s\n", cfgName); + return 1; + } + + while (fgets(line, sizeof(line)-1, fp)) { + if (line[0] == '/' && line[1] == '/') + continue; + + if (sscanf(line, "%1023[^:]:%1023s", w1, w2) != 2) + continue; + p = get_atcommandinfo_byname(w1); + if (p != NULL) { + p->level = atoi(w2); + if (p->level > 100) + p->level = 100; + else if (p->level < 0) + p->level = 0; + } + + if (strcmpi(w1, "import") == 0) + atcommand_config_read(w2); + else if (strcmpi(w1, "command_symbol") == 0 && w2[0] > 31 && + w2[0] != '/' && // symbol of standard ragnarok GM commands + w2[0] != '%') // symbol of party chat speaking + command_symbol = w2[0]; + } + fclose(fp); + + return 0; +} + +/*========================================== +// @ command processing functions + *------------------------------------------ + */ + +/*========================================== + * @rura+ + *------------------------------------------ + */ +int atcommand_rurap( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char map_name[100]; + char character[100]; + int x = 0, y = 0; + struct map_session_data *pl_sd; + int m; + + memset(map_name, '\0', sizeof(map_name)); + memset(character, '\0', sizeof(character)); + + if (!message || !*message || sscanf(message, "%99s %d %d %99[^\n]", map_name, &x, &y, character) < 4) { + clif_displaymessage(fd, "Usage: @charwarp/@rura+ "); + return -1; + } + + if (x <= 0) + x = rand() % 399 + 1; + if (y <= 0) + y = rand() % 399 + 1; + if (strstr(map_name, ".gat") == NULL && strstr(map_name, ".afm") == NULL && strlen(map_name) < 13) // 16 - 4 (.gat) + strcat(map_name, ".gat"); + + if ((pl_sd = map_nick2sd(character)) != NULL) { + if (pc_isGM(sd) >= pc_isGM(pl_sd)) { // you can rura+ only lower or same GM level + if (x > 0 && x < 400 && y > 0 && y < 400) { + m = map_mapname2mapid(map_name); + if (m >= 0 && map[m].flag.nowarpto && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp someone to this map."); + return -1; + } + if (pl_sd->bl.m >= 0 && map[pl_sd->bl.m].flag.nowarp && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp this player from its actual map."); + return -1; + } + if (pc_setpos(pl_sd, map_name, x, y, 3) == 0) { + clif_displaymessage(pl_sd->fd, msg_table[0]); // Warped. + clif_displaymessage(fd, msg_table[15]); // Player warped (message sends to player too). + } else { + clif_displaymessage(fd, msg_table[1]); // Map not found. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[2]); // Coordinates out of range. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[81]); // Your GM level don't authorise you to do this action on this player. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[3]); // Character not found. + return -1; + } + + return 0; +} + +// @rura +/*========================================== + * + *------------------------------------------ + */ +int atcommand_rura( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char map_name[100]; + int x = 0, y = 0; + int m; + + memset(map_name, '\0', sizeof(map_name)); + + if (!message || !*message || sscanf(message, "%99s %d %d", map_name, &x, &y) < 1) { + clif_displaymessage(fd, "Please, enter a map (usage: @warp/@rura/@mapmove )."); + return -1; + } + + if (x <= 0) + x = rand() % 399 + 1; + if (y <= 0) + y = rand() % 399 + 1; + + if (strstr(map_name, ".gat") == NULL && strstr(map_name, ".afm") == NULL && strlen(map_name) < 13) // 16 - 4 (.gat) + strcat(map_name, ".gat"); + + if (x > 0 && x < 400 && y > 0 && y < 400) { + m = map_mapname2mapid(map_name); + if (m >= 0 && map[m].flag.nowarpto && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you to this map."); + return -1; + } + if (sd->bl.m >= 0 && map[sd->bl.m].flag.nowarp && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you from your actual map."); + return -1; + } + if (pc_setpos(sd, map_name, x, y, 3) == 0) + clif_displaymessage(fd, msg_table[0]); // Warped. + else { + clif_displaymessage(fd, msg_table[1]); // Map not found. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[2]); // Coordinates out of range. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_where( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char character[100]; + char output[200]; + struct map_session_data *pl_sd; + + memset(character, '\0', sizeof(character)); + memset(output, '\0', sizeof(output)); + + if (sscanf(message, "%99[^\n]", character) < 1) + strcpy(character, sd->status.name); + + if ((pl_sd = map_nick2sd(character)) != NULL && + !((battle_config.hide_GM_session || (pl_sd->status.option & OPTION_HIDE)) && (pc_isGM(pl_sd) > pc_isGM(sd)))) { // you can look only lower or same level + sprintf(output, "%s: %s (%d,%d)", pl_sd->status.name, pl_sd->mapname, pl_sd->bl.x, pl_sd->bl.y); + clif_displaymessage(fd, output); + } else { + clif_displaymessage(fd, msg_table[3]); // Character not found. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_jumpto( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char character[100]; + char output[200]; + struct map_session_data *pl_sd; + + memset(character, '\0', sizeof(character)); + memset(output, '\0', sizeof(output)); + + if (!message || !*message || sscanf(message, "%99[^\n]", character) < 1) { + clif_displaymessage(fd, "Please, enter a player name (usage: @jumpto/@warpto/@goto )."); + return -1; + } + + if ((pl_sd = map_nick2sd(character)) != NULL) { + if (pl_sd->bl.m >= 0 && map[pl_sd->bl.m].flag.nowarpto && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you to the map of this player."); + return -1; + } + if (sd->bl.m >= 0 && map[sd->bl.m].flag.nowarp && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you from your actual map."); + return -1; + } + pc_setpos(sd, pl_sd->mapname, pl_sd->bl.x, pl_sd->bl.y, 3); + sprintf(output, msg_table[4], character); // Jump to %s + clif_displaymessage(fd, output); + } else { + clif_displaymessage(fd, msg_table[3]); // Character not found. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_jump( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char output[200]; + int x = 0, y = 0; + + memset(output, '\0', sizeof(output)); + + sscanf(message, "%d %d", &x, &y); + + if (x <= 0) + x = rand() % 399 + 1; + if (y <= 0) + y = rand() % 399 + 1; + if (x > 0 && x < 400 && y > 0 && y < 400) { + if (sd->bl.m >= 0 && map[sd->bl.m].flag.nowarpto && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you to your actual map."); + return -1; + } + if (sd->bl.m >= 0 && map[sd->bl.m].flag.nowarp && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you from your actual map."); + return -1; + } + pc_setpos(sd, sd->mapname, x, y, 3); + sprintf(output, msg_table[5], x, y); // Jump to %d %d + clif_displaymessage(fd, output); + } else { + clif_displaymessage(fd, msg_table[2]); // Coordinates out of range. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_who( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char output[200]; + struct map_session_data *pl_sd; + int i, j, count; + int pl_GM_level, GM_level; + char match_text[100]; + char player_name[24]; + + memset(output, '\0', sizeof(output)); + memset(match_text, '\0', sizeof(match_text)); + memset(player_name, '\0', sizeof(player_name)); + + if (sscanf(message, "%99[^\n]", match_text) < 1) + strcpy(match_text, ""); + for (j = 0; match_text[j]; j++) + match_text[j] = tolower(match_text[j]); + + count = 0; + GM_level = pc_isGM(sd); + for (i = 0; i < fd_max; i++) { + if (session[i] && (pl_sd = session[i]->session_data) && pl_sd->state.auth) { + pl_GM_level = pc_isGM(pl_sd); + if (!((battle_config.hide_GM_session || (pl_sd->status.option & OPTION_HIDE)) && (pl_GM_level > GM_level))) { // you can look only lower or same level + memcpy(player_name, pl_sd->status.name, 24); + for (j = 0; player_name[j]; j++) + player_name[j] = tolower(player_name[j]); + if (strstr(player_name, match_text) != NULL) { // search with no case sensitive + if (pl_GM_level > 0) + sprintf(output, "Name: %s (GM:%d) | Location: %s %d %d", pl_sd->status.name, pl_GM_level, pl_sd->mapname, pl_sd->bl.x, pl_sd->bl.y); + else + sprintf(output, "Name: %s | Location: %s %d %d", pl_sd->status.name, pl_sd->mapname, pl_sd->bl.x, pl_sd->bl.y); + clif_displaymessage(fd, output); + count++; + } + } + } + } + + if (count == 0) + clif_displaymessage(fd, msg_table[28]); // No player found. + else if (count == 1) + clif_displaymessage(fd, msg_table[29]); // 1 player found. + else { + sprintf(output, msg_table[30], count); // %d players found. + clif_displaymessage(fd, output); + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_who2( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char output[200]; + struct map_session_data *pl_sd; + int i, j, count; + int pl_GM_level, GM_level; + char match_text[100]; + char player_name[24]; + + memset(output, '\0', sizeof(output)); + memset(match_text, '\0', sizeof(match_text)); + memset(player_name, '\0', sizeof(player_name)); + + if (sscanf(message, "%99[^\n]", match_text) < 1) + strcpy(match_text, ""); + for (j = 0; match_text[j]; j++) + match_text[j] = tolower(match_text[j]); + + count = 0; + GM_level = pc_isGM(sd); + for (i = 0; i < fd_max; i++) { + if (session[i] && (pl_sd = session[i]->session_data) && pl_sd->state.auth) { + pl_GM_level = pc_isGM(pl_sd); + if (!((battle_config.hide_GM_session || (pl_sd->status.option & OPTION_HIDE)) && (pl_GM_level > GM_level))) { // you can look only lower or same level + memcpy(player_name, pl_sd->status.name, 24); + for (j = 0; player_name[j]; j++) + player_name[j] = tolower(player_name[j]); + if (strstr(player_name, match_text) != NULL) { // search with no case sensitive + if (pl_GM_level > 0) + sprintf(output, "Name: %s (GM:%d) | BLvl: %d | Job: %s (Lvl: %d)", pl_sd->status.name, pl_GM_level, pl_sd->status.base_level, job_name(pl_sd->status.class), pl_sd->status.job_level); + else + sprintf(output, "Name: %s | BLvl: %d | Job: %s (Lvl: %d)", pl_sd->status.name, pl_sd->status.base_level, job_name(pl_sd->status.class), pl_sd->status.job_level); + clif_displaymessage(fd, output); + count++; + } + } + } + } + + if (count == 0) + clif_displaymessage(fd, msg_table[28]); // No player found. + else if (count == 1) + clif_displaymessage(fd, msg_table[29]); // 1 player found. + else { + sprintf(output, msg_table[30], count); // %d players found. + clif_displaymessage(fd, output); + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_who3( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char temp0[100]; + char temp1[100]; + char output[200]; + struct map_session_data *pl_sd; + int i, j, count; + int pl_GM_level, GM_level; + char match_text[100]; + char player_name[24]; + struct guild *g; + struct party *p; + + memset(temp0, '\0', sizeof(temp0)); + memset(temp1, '\0', sizeof(temp1)); + memset(output, '\0', sizeof(output)); + memset(match_text, '\0', sizeof(match_text)); + memset(player_name, '\0', sizeof(player_name)); + + if (sscanf(message, "%99[^\n]", match_text) < 1) + strcpy(match_text, ""); + for (j = 0; match_text[j]; j++) + match_text[j] = tolower(match_text[j]); + + count = 0; + GM_level = pc_isGM(sd); + for (i = 0; i < fd_max; i++) { + if (session[i] && (pl_sd = session[i]->session_data) && pl_sd->state.auth) { + pl_GM_level = pc_isGM(pl_sd); + if (!((battle_config.hide_GM_session || (pl_sd->status.option & OPTION_HIDE)) && (pl_GM_level > GM_level))) { // you can look only lower or same level + memcpy(player_name, pl_sd->status.name, 24); + for (j = 0; player_name[j]; j++) + player_name[j] = tolower(player_name[j]); + if (strstr(player_name, match_text) != NULL) { // search with no case sensitive + g = guild_search(pl_sd->status.guild_id); + if (g == NULL) + sprintf(temp1, "None"); + else + sprintf(temp1, "%s", g->name); + p = party_search(pl_sd->status.party_id); + if (p == NULL) + sprintf(temp0, "None"); + else + sprintf(temp0, "%s", p->name); + if (pl_GM_level > 0) + sprintf(output, "Name: %s (GM:%d) | Party: '%s' | Guild: '%s'", pl_sd->status.name, pl_GM_level, temp0, temp1); + else + sprintf(output, "Name: %s | Party: '%s' | Guild: '%s'", pl_sd->status.name, temp0, temp1); + clif_displaymessage(fd, output); + count++; + } + } + } + } + + if (count == 0) + clif_displaymessage(fd, msg_table[28]); // No player found. + else if (count == 1) + clif_displaymessage(fd, msg_table[29]); // 1 player found. + else { + sprintf(output, msg_table[30], count); // %d players found. + clif_displaymessage(fd, output); + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_whomap( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char output[200]; + struct map_session_data *pl_sd; + int i, count; + int pl_GM_level, GM_level; + int map_id; + char map_name[100]; + + memset(output, '\0', sizeof(output)); + memset(map_name, '\0', sizeof(map_name)); + + if (!message || !*message) + map_id = sd->bl.m; + else { + sscanf(message, "%99s", map_name); + if (strstr(map_name, ".gat") == NULL && strstr(map_name, ".afm") == NULL && strlen(map_name) < 13) // 16 - 4 (.gat) + strcat(map_name, ".gat"); + if ((map_id = map_mapname2mapid(map_name)) < 0) + map_id = sd->bl.m; + } + + count = 0; + GM_level = pc_isGM(sd); + for (i = 0; i < fd_max; i++) { + if (session[i] && (pl_sd = session[i]->session_data) && pl_sd->state.auth) { + pl_GM_level = pc_isGM(pl_sd); + if (!((battle_config.hide_GM_session || (pl_sd->status.option & OPTION_HIDE)) && (pl_GM_level > GM_level))) { // you can look only lower or same level + if (pl_sd->bl.m == map_id) { + if (pl_GM_level > 0) + sprintf(output, "Name: %s (GM:%d) | Location: %s %d %d", pl_sd->status.name, pl_GM_level, pl_sd->mapname, pl_sd->bl.x, pl_sd->bl.y); + else + sprintf(output, "Name: %s | Location: %s %d %d", pl_sd->status.name, pl_sd->mapname, pl_sd->bl.x, pl_sd->bl.y); + clif_displaymessage(fd, output); + count++; + } + } + } + } + + if (count == 0) + sprintf(output, msg_table[54], map[map_id].name); // No player found in map '%s'. + else if (count == 1) + sprintf(output, msg_table[55], map[map_id].name); // 1 player found in map '%s'. + else { + sprintf(output, msg_table[56], count, map[map_id].name); // %d players found in map '%s'. + } + clif_displaymessage(fd, output); + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_whomap2( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char output[200]; + struct map_session_data *pl_sd; + int i, count; + int pl_GM_level, GM_level; + int map_id = 0; + char map_name[100]; + + memset(output, '\0', sizeof(output)); + memset(map_name, '\0', sizeof(map_name)); + + if (!message || !*message) + map_id = sd->bl.m; + else { + sscanf(message, "%99s", map_name); + if (strstr(map_name, ".gat") == NULL && strstr(map_name, ".afm") == NULL && strlen(map_name) < 13) // 16 - 4 (.gat) + strcat(map_name, ".gat"); + if ((map_id = map_mapname2mapid(map_name)) < 0) + map_id = sd->bl.m; + } + + count = 0; + GM_level = pc_isGM(sd); + for (i = 0; i < fd_max; i++) { + if (session[i] && (pl_sd = session[i]->session_data) && pl_sd->state.auth) { + pl_GM_level = pc_isGM(pl_sd); + if (!((battle_config.hide_GM_session || (pl_sd->status.option & OPTION_HIDE)) && (pl_GM_level > GM_level))) { // you can look only lower or same level + if (pl_sd->bl.m == map_id) { + if (pl_GM_level > 0) + sprintf(output, "Name: %s (GM:%d) | BLvl: %d | Job: %s (Lvl: %d)", pl_sd->status.name, pl_GM_level, pl_sd->status.base_level, job_name(pl_sd->status.class), pl_sd->status.job_level); + else + sprintf(output, "Name: %s | BLvl: %d | Job: %s (Lvl: %d)", pl_sd->status.name, pl_sd->status.base_level, job_name(pl_sd->status.class), pl_sd->status.job_level); + clif_displaymessage(fd, output); + count++; + } + } + } + } + + if (count == 0) + sprintf(output, msg_table[54], map[map_id].name); // No player found in map '%s'. + else if (count == 1) + sprintf(output, msg_table[55], map[map_id].name); // 1 player found in map '%s'. + else { + sprintf(output, msg_table[56], count, map[map_id].name); // %d players found in map '%s'. + } + clif_displaymessage(fd, output); + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_whomap3( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char temp0[100]; + char temp1[100]; + char output[200]; + struct map_session_data *pl_sd; + int i, count; + int pl_GM_level, GM_level; + int map_id = 0; + char map_name[100]; + struct guild *g; + struct party *p; + + memset(temp0, '\0', sizeof(temp0)); + memset(temp1, '\0', sizeof(temp1)); + memset(output, '\0', sizeof(output)); + memset(map_name, '\0', sizeof(map_name)); + + if (!message || !*message) + map_id = sd->bl.m; + else { + sscanf(message, "%99s", map_name); + if (strstr(map_name, ".gat") == NULL && strstr(map_name, ".afm") == NULL && strlen(map_name) < 13) // 16 - 4 (.gat) + strcat(map_name, ".gat"); + if ((map_id = map_mapname2mapid(map_name)) < 0) + map_id = sd->bl.m; + } + + count = 0; + GM_level = pc_isGM(sd); + for (i = 0; i < fd_max; i++) { + if (session[i] && (pl_sd = session[i]->session_data) && pl_sd->state.auth) { + pl_GM_level = pc_isGM(pl_sd); + if (!((battle_config.hide_GM_session || (pl_sd->status.option & OPTION_HIDE)) && (pl_GM_level > GM_level))) { // you can look only lower or same level + if (pl_sd->bl.m == map_id) { + g = guild_search(pl_sd->status.guild_id); + if (g == NULL) + sprintf(temp1, "None"); + else + sprintf(temp1, "%s", g->name); + p = party_search(pl_sd->status.party_id); + if (p == NULL) + sprintf(temp0, "None"); + else + sprintf(temp0, "%s", p->name); + if (pl_GM_level > 0) + sprintf(output, "Name: %s (GM:%d) | Party: '%s' | Guild: '%s'", pl_sd->status.name, pl_GM_level, temp0, temp1); + else + sprintf(output, "Name: %s | Party: '%s' | Guild: '%s'", pl_sd->status.name, temp0, temp1); + clif_displaymessage(fd, output); + count++; + } + } + } + } + + if (count == 0) + sprintf(output, msg_table[54], map[map_id].name); // No player found in map '%s'. + else if (count == 1) + sprintf(output, msg_table[55], map[map_id].name); // 1 player found in map '%s'. + else { + sprintf(output, msg_table[56], count, map[map_id].name); // %d players found in map '%s'. + } + clif_displaymessage(fd, output); + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_whogm( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char temp0[100]; + char temp1[100]; + char output[200]; + struct map_session_data *pl_sd; + int i, j, count; + int pl_GM_level, GM_level; + char match_text[100]; + char player_name[24]; + struct guild *g; + struct party *p; + + memset(temp0, '\0', sizeof(temp0)); + memset(temp1, '\0', sizeof(temp1)); + memset(output, '\0', sizeof(output)); + memset(match_text, '\0', sizeof(match_text)); + memset(player_name, '\0', sizeof(player_name)); + + if (sscanf(message, "%99[^\n]", match_text) < 1) + strcpy(match_text, ""); + for (j = 0; match_text[j]; j++) + match_text[j] = tolower(match_text[j]); + + count = 0; + GM_level = pc_isGM(sd); + for (i = 0; i < fd_max; i++) { + if (session[i] && (pl_sd = session[i]->session_data) && pl_sd->state.auth) { + pl_GM_level = pc_isGM(pl_sd); + if (pl_GM_level > 0) { + if (!((battle_config.hide_GM_session || (pl_sd->status.option & OPTION_HIDE)) && (pl_GM_level > GM_level))) { // you can look only lower or same level + memcpy(player_name, pl_sd->status.name, 24); + for (j = 0; player_name[j]; j++) + player_name[j] = tolower(player_name[j]); + if (strstr(player_name, match_text) != NULL) { // search with no case sensitive + sprintf(output, "Name: %s (GM:%d) | Location: %s %d %d", pl_sd->status.name, pl_GM_level, pl_sd->mapname, pl_sd->bl.x, pl_sd->bl.y); + clif_displaymessage(fd, output); + sprintf(output, " BLvl: %d | Job: %s (Lvl: %d)", pl_sd->status.base_level, job_name(pl_sd->status.class), pl_sd->status.job_level); + clif_displaymessage(fd, output); + g = guild_search(pl_sd->status.guild_id); + if (g == NULL) + sprintf(temp1, "None"); + else + sprintf(temp1, "%s", g->name); + p = party_search(pl_sd->status.party_id); + if (p == NULL) + sprintf(temp0, "None"); + else + sprintf(temp0, "%s", p->name); + sprintf(output, " Party: '%s' | Guild: '%s'", temp0, temp1); + clif_displaymessage(fd, output); + count++; + } + } + } + } + } + + if (count == 0) + clif_displaymessage(fd, msg_table[150]); // No GM found. + else if (count == 1) + clif_displaymessage(fd, msg_table[151]); // 1 GM found. + else { + sprintf(output, msg_table[152], count); // %d GMs found. + clif_displaymessage(fd, output); + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_save( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + pc_setsavepoint(sd, sd->mapname, sd->bl.x, sd->bl.y); + if (sd->status.pet_id > 0 && sd->pd) + intif_save_petdata(sd->status.account_id, &sd->pet); + pc_makesavestatus(sd); + chrif_save(sd); + storage_storage_save(sd); + clif_displaymessage(fd, msg_table[6]); // Character data respawn point saved. + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_load( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int m; + + m = map_mapname2mapid(sd->status.save_point.map); + if (m >= 0 && map[m].flag.nowarpto && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you to your save map."); + return -1; + } + if (sd->bl.m >= 0 && map[sd->bl.m].flag.nowarp && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you from your actual map."); + return -1; + } + + pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 0); + clif_displaymessage(fd, msg_table[7]); // Warping to respawn point. + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_speed( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char output[200]; + int speed; + + memset(output, '\0', sizeof(output)); + + if (!message || !*message) { + sprintf(output, "Please, enter a speed value (usage: @speed <%d-%d>).", MIN_WALK_SPEED, MAX_WALK_SPEED); + clif_displaymessage(fd, output); + return -1; + } + + speed = atoi(message); + if (speed >= MIN_WALK_SPEED && speed <= MAX_WALK_SPEED) { + sd->speed = speed; + //sd->walktimer = x; + //この文を追加 by れ + clif_updatestatus(sd, SP_SPEED); + clif_displaymessage(fd, msg_table[8]); // Speed changed. + } else { + sprintf(output, "Please, enter a valid speed value (usage: @speed <%d-%d>).", MIN_WALK_SPEED, MAX_WALK_SPEED); + clif_displaymessage(fd, output); + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_storage( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + storage_storageopen(sd); + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_guildstorage( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + if (sd->status.guild_id > 0) + storage_guild_storageopen(sd); + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_option( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int param1 = 0, param2 = 0, param3 = 0; + + if (!message || !*message || sscanf(message, "%d %d %d", ¶m1, ¶m2, ¶m3) < 1 || param1 < 0 || param2 < 0 || param3 < 0) { + clif_displaymessage(fd, "Please, enter at least a option (usage: @option )."); + return -1; + } + + sd->opt1 = param1; + sd->opt2 = param2; + if (!(sd->status.option & CART_MASK) && param3 & CART_MASK) { + clif_cart_itemlist(sd); + clif_cart_equiplist(sd); + clif_updatestatus(sd, SP_CARTINFO); + } + sd->status.option = param3; + // fix pecopeco display + if (sd->status.class == 13 || sd->status.class == 21 || sd->status.class == 4014 || sd->status.class == 4022) { + if (!pc_isriding(sd)) { // sd have the new value... + if (sd->status.class == 13) + sd->status.class = sd->view_class = 7; + else if (sd->status.class == 21) + sd->status.class = sd->view_class = 14; + else if (sd->status.class == 4014) + sd->status.class = sd->view_class = 4008; + else if (sd->status.class == 4022) + sd->status.class = sd->view_class = 4015; + } + } else { + if (pc_isriding(sd)) { // sd have the new value... + if (sd->disguise > 0) { // temporary prevention of crash caused by peco + disguise, will look into a better solution [Valaris] (code added by [Yor]) + sd->status.option &= ~0x0020; + } else { + if (sd->status.class == 7) + sd->status.class = sd->view_class = 13; + else if (sd->status.class == 14) + sd->status.class = sd->view_class = 21; + else if (sd->status.class == 4008) + sd->status.class = sd->view_class = 4014; + else if (sd->status.class == 4015) + sd->status.class = sd->view_class = 4022; + else + sd->status.option &= ~0x0020; + } + } + } + + clif_changeoption(&sd->bl); + pc_calcstatus(sd, 0); + clif_displaymessage(fd, msg_table[9]); // Options changed. + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_hide( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + if (sd->status.option & OPTION_HIDE) { + sd->status.option &= ~OPTION_HIDE; + clif_displaymessage(fd, msg_table[10]); // Invisible: Off + } else { + sd->status.option |= OPTION_HIDE; + clif_displaymessage(fd, msg_table[11]); // Invisible: On + } + clif_changeoption(&sd->bl); + + return 0; +} + +/*========================================== + * 転職する upperを指定すると転生や養子にもなれる + *------------------------------------------ + */ +int atcommand_jobchange( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int job = 0, upper = -1; + + if (!message || !*message || sscanf(message, "%d %d", &job, &upper) < 1) { + clif_displaymessage(fd, "Please, enter job ID (usage: @job/@jobchange )."); + return -1; + } + + if (job == 37 ||job == 45) + return 0; + + if ((job >= 0 && job < MAX_PC_CLASS)) { + + // fix pecopeco display + if ((job != 13 && job != 21 && job != 4014 && job != 4022)) { + if (pc_isriding(sd)) { + if (sd->status.class == 13) + sd->status.class = sd->view_class = 7; + if (sd->status.class == 21) + sd->status.class = sd->view_class = 14; + if (sd->status.class == 4014) + sd->status.class = sd->view_class = 4008; + if (sd->status.class == 4022) + sd->status.class = sd->view_class = 4015; + sd->status.option &= ~0x0020; + clif_changeoption(&sd->bl); + pc_calcstatus(sd, 0); + } + } else { + if (!pc_isriding(sd)) { + if (job == 13) + job = 7; + if (job == 21) + job = 14; + if (job == 4014) + job = 4008; + if (job == 4022) + job = 4015; + } + } + + if (pc_jobchange(sd, job, upper) == 0) + clif_displaymessage(fd, msg_table[12]); // Your job has been changed. + else { + clif_displaymessage(fd, msg_table[155]); // Impossible to change your job. + return -1; + } + } else { + clif_displaymessage(fd, "Please, enter a valid job ID (usage: @job/@jobchange )."); + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_die( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + pc_damage(NULL, sd, sd->status.hp + 1); + clif_displaymessage(fd, msg_table[13]); // A pity! You've died. + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_kill( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char character[100]; + struct map_session_data *pl_sd; + + memset(character, '\0', sizeof(character)); + + if (!message || !*message || sscanf(message, "%99[^\n]", character) < 1) { + clif_displaymessage(fd, "Please, enter a player name (usage: @kill )."); + return -1; + } + + if ((pl_sd = map_nick2sd(character)) != NULL) { + if (pc_isGM(sd) >= pc_isGM(pl_sd)) { // you can kill only lower or same level + pc_damage(NULL, pl_sd, pl_sd->status.hp + 1); + clif_displaymessage(fd, msg_table[14]); // Character killed. + } else { + clif_displaymessage(fd, msg_table[81]); // Your GM level don't authorise you to do this action on this player. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[3]); // Character not found. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_alive( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + sd->status.hp = sd->status.max_hp; + sd->status.sp = sd->status.max_sp; + pc_setstand(sd); + if (battle_config.pc_invincible_time > 0) + pc_setinvincibletimer(sd, battle_config.pc_invincible_time); + clif_updatestatus(sd, SP_HP); + clif_updatestatus(sd, SP_SP); + clif_resurrection(&sd->bl, 1); + clif_displaymessage(fd, msg_table[16]); // You've been revived! It's a miracle! + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_kami( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char output[200]; + + memset(output, '\0', sizeof(output)); + + if (!message || !*message) { + clif_displaymessage(fd, "Please, enter a message (usage: @kami )."); + return -1; + } + + sscanf(message, "%199[^\n]", output); + intif_GMmessage(output, strlen(output) + 1, (*(command + 5) == 'b') ? 0x10 : 0); + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_heal( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int hp = 0, sp = 0; // [Valaris] thanks to fov + + sscanf(message, "%d %d", &hp, &sp); + + if (hp == 0 && sp == 0) { + hp = sd->status.max_hp - sd->status.hp; + sp = sd->status.max_sp - sd->status.sp; + } else { + if (hp > 0 && (hp > sd->status.max_hp || hp > (sd->status.max_hp - sd->status.hp))) // fix positiv overflow + hp = sd->status.max_hp - sd->status.hp; + else if (hp < 0 && (hp < -sd->status.max_hp || hp < (1 - sd->status.hp))) // fix negativ overflow + hp = 1 - sd->status.hp; + if (sp > 0 && (sp > sd->status.max_sp || sp > (sd->status.max_sp - sd->status.sp))) // fix positiv overflow + sp = sd->status.max_sp - sd->status.sp; + else if (sp < 0 && (sp < -sd->status.max_sp || sp < (1 - sd->status.sp))) // fix negativ overflow + sp = 1 - sd->status.sp; + } + + if (hp > 0) // display like heal + clif_heal(fd, SP_HP, hp); + else if (hp < 0) // display like damage + clif_damage(&sd->bl,&sd->bl, gettick(), 0, 0, -hp, 0 , 4, 0); + if (sp > 0) // no display when we lost SP + clif_heal(fd, SP_SP, sp); + + if (hp != 0 || sp != 0) { + pc_heal(sd, hp, sp); + if (hp >= 0 && sp >= 0) + clif_displaymessage(fd, msg_table[17]); // HP, SP recovered. + else + clif_displaymessage(fd, msg_table[156]); // HP or/and SP modified. + } else { + clif_displaymessage(fd, msg_table[157]); // HP and SP are already with the good value. + return -1; + } + + return 0; +} + +/*========================================== + * @item command (usage: @item ) (modified by [Yor] for pet_egg) + *------------------------------------------ + */ +int atcommand_item( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char item_name[100]; + int number = 0, item_id, flag; + struct item item_tmp; + struct item_data *item_data; + int get_count, i, pet_id; + + memset(item_name, '\0', sizeof(item_name)); + + if (!message || !*message || sscanf(message, "%99s %d", item_name, &number) < 1) { + clif_displaymessage(fd, "Please, enter an item name/id (usage: @item [quantity])."); + return -1; + } + + if (number <= 0) + number = 1; + + item_id = 0; + if ((item_data = itemdb_searchname(item_name)) != NULL || + (item_data = itemdb_exists(atoi(item_name))) != NULL) + item_id = item_data->nameid; + + if (item_id >= 500) { + get_count = number; + // check pet egg + pet_id = search_petDB_index(item_id, PET_EGG); + if (item_data->type == 4 || item_data->type == 5 || + item_data->type == 7 || item_data->type == 8) { + get_count = 1; + } + for (i = 0; i < number; i += get_count) { + // if pet egg + if (pet_id >= 0) { + sd->catch_target_class = pet_db[pet_id].class; + intif_create_pet(sd->status.account_id, sd->status.char_id, + pet_db[pet_id].class, mob_db[pet_db[pet_id].class].lv, + pet_db[pet_id].EggID, 0, pet_db[pet_id].intimate, + 100, 0, 1, pet_db[pet_id].jname); + // if not pet egg + } else { + memset(&item_tmp, 0, sizeof(item_tmp)); + item_tmp.nameid = item_id; + item_tmp.identify = 1; + if ((flag = pc_additem((struct map_session_data*)sd, &item_tmp, get_count))) + clif_additem((struct map_session_data*)sd, 0, 0, flag); + } + } + clif_displaymessage(fd, msg_table[18]); // Item created. + } else { + clif_displaymessage(fd, msg_table[19]); // Invalid item ID or name. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_item2( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + struct item item_tmp; + struct item_data *item_data; + char item_name[100]; + int item_id, number = 0; + int identify = 0, refine = 0, attr = 0; + int c1 = 0, c2 = 0, c3 = 0, c4 = 0; + int flag; + int loop, get_count, i; + + memset(item_name, '\0', sizeof(item_name)); + + if (!message || !*message || sscanf(message, "%99s %d %d %d %d %d %d %d %d", item_name, &number, &identify, &refine, &attr, &c1, &c2, &c3, &c4) < 9) { + clif_displaymessage(fd, "Please, enter all informations (usage: @item2 "); + clif_displaymessage(fd, " )."); + return -1; + } + + if (number <= 0) + number = 1; + + item_id = 0; + if ((item_data = itemdb_searchname(item_name)) != NULL || + (item_data = itemdb_exists(atoi(item_name))) != NULL) + item_id = item_data->nameid; + + if (item_id > 500) { + loop = 1; + get_count = number; + if (item_data->type == 4 || item_data->type == 5 || + item_data->type == 7 || item_data->type == 8) { + loop = number; + get_count = 1; + if (item_data->type == 7) { + identify = 1; + refine = 0; + } + if (item_data->type == 8) + refine = 0; + if (refine > 10) + refine = 10; + } else { + identify = 1; + refine = attr = 0; + } + for (i = 0; i < loop; i++) { + memset(&item_tmp, 0, sizeof(item_tmp)); + item_tmp.nameid = item_id; + item_tmp.identify = identify; + item_tmp.refine = refine; + item_tmp.attribute = attr; + item_tmp.card[0] = c1; + item_tmp.card[1] = c2; + item_tmp.card[2] = c3; + item_tmp.card[3] = c4; + if ((flag = pc_additem(sd, &item_tmp, get_count))) + clif_additem(sd, 0, 0, flag); + } + clif_displaymessage(fd, msg_table[18]); // Item created. + } else { + clif_displaymessage(fd, msg_table[19]); // Invalid item ID or name. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_itemreset( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int i; + + for (i = 0; i < MAX_INVENTORY; i++) { + if (sd->status.inventory[i].amount && sd->status.inventory[i].equip == 0) + pc_delitem(sd, i, sd->status.inventory[i].amount, 0); + } + clif_displaymessage(fd, msg_table[20]); // All of your items have been removed. + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_itemcheck( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + pc_checkitem(sd); + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_baselevelup( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int level, i; + + if (!message || !*message || (level = atoi(message)) == 0) { + clif_displaymessage(fd, "Please, enter a level adjustement (usage: @lvup/@blevel/@baselvlup )."); + return -1; + } + + if (level > 0) { + if (sd->status.base_level == battle_config.maximum_level) { // check for max level by Valaris + clif_displaymessage(fd, msg_table[47]); // Base level can't go any higher. + return -1; + } // End Addition + if (level > battle_config.maximum_level || level > (battle_config.maximum_level - sd->status.base_level)) // fix positiv overflow + level = battle_config.maximum_level - sd->status.base_level; + for (i = 1; i <= level; i++) + sd->status.status_point += (sd->status.base_level + i + 14) / 5; + sd->status.base_level += level; + clif_updatestatus(sd, SP_BASELEVEL); + clif_updatestatus(sd, SP_NEXTBASEEXP); + clif_updatestatus(sd, SP_STATUSPOINT); + pc_calcstatus(sd, 0); + pc_heal(sd, sd->status.max_hp, sd->status.max_sp); + clif_misceffect(&sd->bl, 0); + clif_displaymessage(fd, msg_table[21]); // Base level raised. + } else { + if (sd->status.base_level == 1) { + clif_displaymessage(fd, msg_table[158]); // Base level can't go any lower. + return -1; + } + if (level < -battle_config.maximum_level || level < (1 - sd->status.base_level)) // fix negativ overflow + level = 1 - sd->status.base_level; + if (sd->status.status_point > 0) { + for (i = 0; i > level; i--) + sd->status.status_point -= (sd->status.base_level + i + 14) / 5; + if (sd->status.status_point < 0) + sd->status.status_point = 0; + clif_updatestatus(sd, SP_STATUSPOINT); + } // to add: remove status points from stats + sd->status.base_level += level; + clif_updatestatus(sd, SP_BASELEVEL); + clif_updatestatus(sd, SP_NEXTBASEEXP); + pc_calcstatus(sd, 0); + clif_displaymessage(fd, msg_table[22]); // Base level lowered. + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_joblevelup( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int up_level = 50, level; + + if (!message || !*message || (level = atoi(message)) == 0) { + clif_displaymessage(fd, "Please, enter a level adjustement (usage: @joblvup/@jlevel/@joblvlup )."); + return -1; + } + + if (sd->status.class == 0 || sd->status.class == 4001) + up_level -= 40; + else if ((sd->status.class > 4007 && sd->status.class < 4024) || sd->status.class == 23) + up_level += 20; + + if (level > 0) { + if (sd->status.job_level == up_level) { + clif_displaymessage(fd, msg_table[23]); // Job level can't go any higher. + return -1; + } + if (level > up_level || level > (up_level - sd->status.job_level)) // fix positiv overflow + level = up_level - sd->status.job_level; + sd->status.job_level += level; + clif_updatestatus(sd, SP_JOBLEVEL); + clif_updatestatus(sd, SP_NEXTJOBEXP); + sd->status.skill_point += level; + clif_updatestatus(sd, SP_SKILLPOINT); + pc_calcstatus(sd, 0); + clif_misceffect(&sd->bl, 1); + clif_displaymessage(fd, msg_table[24]); // Job level raised. + } else { + if (sd->status.job_level == 1) { + clif_displaymessage(fd, msg_table[159]); // Job level can't go any lower. + return -1; + } + if (level < -up_level || level < (1 - sd->status.job_level)) // fix negativ overflow + level = 1 - sd->status.job_level; + sd->status.job_level += level; + clif_updatestatus(sd, SP_JOBLEVEL); + clif_updatestatus(sd, SP_NEXTJOBEXP); + if (sd->status.skill_point > 0) { + sd->status.skill_point += level; + if (sd->status.skill_point < 0) + sd->status.skill_point = 0; + clif_updatestatus(sd, SP_SKILLPOINT); + } // to add: remove status points from skills + pc_calcstatus(sd, 0); + clif_displaymessage(fd, msg_table[25]); // Job level lowered. + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_help( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char buf[2048], w1[2048], w2[2048]; + int i, gm_level; + FILE* fp; + + memset(buf, '\0', sizeof(buf)); + + if ((fp = fopen(help_txt, "r")) != NULL) { + clif_displaymessage(fd, msg_table[26]); // Help commands: + gm_level = pc_isGM(sd); + while(fgets(buf, sizeof(buf) - 1, fp) != NULL) { + if (buf[0] == '/' && buf[1] == '/') + continue; + for (i = 0; buf[i] != '\0'; i++) { + if (buf[i] == '\r' || buf[i] == '\n') { + buf[i] = '\0'; + break; + } + } + if (sscanf(buf, "%2047[^:]:%2047[^\n]", w1, w2) < 2) + clif_displaymessage(fd, buf); + else if (gm_level >= atoi(w1)) + clif_displaymessage(fd, w2); + } + fclose(fp); + } else { + clif_displaymessage(fd, msg_table[27]); // File help.txt not found. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_gm( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char password[100]; + + memset(password, '\0', sizeof(password)); + + if (!message || !*message || sscanf(message, "%99[^\n]", password) < 1) { + clif_displaymessage(fd, "Please, enter a password (usage: @gm )."); + return -1; + } + + if (pc_isGM(sd)) { // a GM can not use this function. only a normal player (become gm is not for gm!) + clif_displaymessage(fd, msg_table[50]); // You already have some GM powers. + return -1; + } else + chrif_changegm(sd->status.account_id, password, strlen(password) + 1); + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_pvpoff( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + struct map_session_data *pl_sd; + int i; + + if (battle_config.pk_mode) { //disable command if server is in PK mode [Valaris] + clif_displaymessage(fd, msg_table[52]); // This option cannot be used in PK Mode. + return -1; + } + + if (map[sd->bl.m].flag.pvp) { + map[sd->bl.m].flag.pvp = 0; + clif_send0199(sd->bl.m, 0); + for (i = 0; i < fd_max; i++) { //人数分ループ + if (session[i] && (pl_sd = session[i]->session_data) && pl_sd->state.auth) { + if (sd->bl.m == pl_sd->bl.m) { + clif_pvpset(pl_sd, 0, 0, 2); + if (pl_sd->pvp_timer != -1) { + delete_timer(pl_sd->pvp_timer, pc_calc_pvprank_timer); + pl_sd->pvp_timer = -1; + } + } + } + } + clif_displaymessage(fd, msg_table[31]); // PvP: Off. + } else { + clif_displaymessage(fd, msg_table[160]); // PvP is already Off. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_pvpon( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + struct map_session_data *pl_sd; + int i; + + if (battle_config.pk_mode) { //disable command if server is in PK mode [Valaris] + clif_displaymessage(fd, msg_table[52]); // This option cannot be used in PK Mode. + return -1; + } + + if (!map[sd->bl.m].flag.pvp && !map[sd->bl.m].flag.nopvp) { + map[sd->bl.m].flag.pvp = 1; + clif_send0199(sd->bl.m, 1); + for (i = 0; i < fd_max; i++) { + if (session[i] && (pl_sd = session[i]->session_data) && pl_sd->state.auth) { + if (sd->bl.m == pl_sd->bl.m && pl_sd->pvp_timer == -1) { + pl_sd->pvp_timer = add_timer(gettick() + 200, + pc_calc_pvprank_timer, pl_sd->bl.id, 0); + pl_sd->pvp_rank = 0; + pl_sd->pvp_lastusers = 0; + pl_sd->pvp_point = 5; + } + } + } + clif_displaymessage(fd, msg_table[32]); // PvP: On. + } else { + clif_displaymessage(fd, msg_table[161]); // PvP is already On. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_gvgoff( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + if (map[sd->bl.m].flag.gvg) { + map[sd->bl.m].flag.gvg = 0; + clif_send0199(sd->bl.m, 0); + clif_displaymessage(fd, msg_table[33]); // GvG: Off. + } else { + clif_displaymessage(fd, msg_table[162]); // GvG is already Off. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_gvgon( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + if (!map[sd->bl.m].flag.gvg) { + map[sd->bl.m].flag.gvg = 1; + clif_send0199(sd->bl.m, 3); + clif_displaymessage(fd, msg_table[34]); // GvG: On. + } else { + clif_displaymessage(fd, msg_table[163]); // GvG is already On. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_model( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int hair_style = 0, hair_color = 0, cloth_color = 0; + char output[200]; + + memset(output, '\0', sizeof(output)); + + if (!message || !*message || sscanf(message, "%d %d %d", &hair_style, &hair_color, &cloth_color) < 1) { + sprintf(output, "Please, enter at least a value (usage: @model ).", + MIN_HAIR_STYLE, MAX_HAIR_STYLE, MIN_HAIR_COLOR, MAX_HAIR_COLOR, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR); + clif_displaymessage(fd, output); + return -1; + } + + if (hair_style >= MIN_HAIR_STYLE && hair_style <= MAX_HAIR_STYLE && + hair_color >= MIN_HAIR_COLOR && hair_color <= MAX_HAIR_COLOR && + cloth_color >= MIN_CLOTH_COLOR && cloth_color <= MAX_CLOTH_COLOR) { + //服の色変更 + if (cloth_color != 0 && sd->status.sex == 1 && (sd->status.class == 12 || sd->status.class == 17)) { + //服の色未実装職の判定 + clif_displaymessage(fd, msg_table[35]); // You can't use this command with this class. + return -1; + } else { + pc_changelook(sd, LOOK_HAIR, hair_style); + pc_changelook(sd, LOOK_HAIR_COLOR, hair_color); + pc_changelook(sd, LOOK_CLOTHES_COLOR, cloth_color); + clif_displaymessage(fd, msg_table[36]); // Appearence changed. + } + } else { + clif_displaymessage(fd, msg_table[37]); // An invalid number was specified. + return -1; + } + + return 0; +} + +/*========================================== + * @dye && @ccolor + *------------------------------------------ + */ +int atcommand_dye(const int fd, struct map_session_data* sd, const char* command, const char* message) +{ + int cloth_color = 0; + char output[200]; + + memset(output, '\0', sizeof(output)); + + if (!message || !*message || sscanf(message, "%d", &cloth_color) < 1) { + sprintf(output, "Please, enter a clothes color (usage: @dye/@ccolor ).", MIN_CLOTH_COLOR, MAX_CLOTH_COLOR); + clif_displaymessage(fd, output); + return -1; + } + + if (cloth_color >= MIN_CLOTH_COLOR && cloth_color <= MAX_CLOTH_COLOR) { + if (cloth_color != 0 && sd->status.sex == 1 && (sd->status.class == 12 || sd->status.class == 17)) { + clif_displaymessage(fd, msg_table[35]); // You can't use this command with this class. + return -1; + } else { + pc_changelook(sd, LOOK_CLOTHES_COLOR, cloth_color); + clif_displaymessage(fd, msg_table[36]); // Appearence changed. + } + } else { + clif_displaymessage(fd, msg_table[37]); // An invalid number was specified. + return -1; + } + + return 0; +} + +/*========================================== + * @chardye by [MouseJstr] + *------------------------------------------ + */ +int +atcommand_chardye(const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + return 0; +} + +/*========================================== + * @hairstyle && @hstyle + *------------------------------------------ + */ +int atcommand_hair_style(const int fd, struct map_session_data* sd, const char* command, const char* message) +{ + int hair_style = 0; + char output[200]; + + memset(output, '\0', sizeof(output)); + + if (!message || !*message || sscanf(message, "%d", &hair_style) < 1) { + sprintf(output, "Please, enter a hair style (usage: @hairstyle/@hstyle ).", MIN_HAIR_STYLE, MAX_HAIR_STYLE); + clif_displaymessage(fd, output); + return -1; + } + + if (hair_style >= MIN_HAIR_STYLE && hair_style <= MAX_HAIR_STYLE) { + if (hair_style != 0 && sd->status.sex == 1 && (sd->status.class == 12 || sd->status.class == 17)) { + clif_displaymessage(fd, msg_table[35]); // You can't use this command with this class. + return -1; + } else { + pc_changelook(sd, LOOK_HAIR, hair_style); + clif_displaymessage(fd, msg_table[36]); // Appearence changed. + } + } else { + clif_displaymessage(fd, msg_table[37]); // An invalid number was specified. + return -1; + } + + return 0; +} + +/*========================================== + * @charhairstyle by [MouseJstr] + *------------------------------------------ + */ +int +atcommand_charhairstyle(const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + return 0; +} + +/*========================================== + * @haircolor && @hcolor + *------------------------------------------ + */ +int atcommand_hair_color(const int fd, struct map_session_data* sd, const char* command, const char* message) +{ + int hair_color = 0; + char output[200]; + + memset(output, '\0', sizeof(output)); + + if (!message || !*message || sscanf(message, "%d", &hair_color) < 1) { + sprintf(output, "Please, enter a hair color (usage: @haircolor/@hcolor ).", MIN_HAIR_COLOR, MAX_HAIR_COLOR); + clif_displaymessage(fd, output); + return -1; + } + + if (hair_color >= MIN_HAIR_COLOR && hair_color <= MAX_HAIR_COLOR) { + if (hair_color != 0 && sd->status.sex == 1 && (sd->status.class == 12 || sd->status.class == 17)) { + clif_displaymessage(fd, msg_table[35]); // You can't use this command with this class. + return -1; + } else { + pc_changelook(sd, LOOK_HAIR_COLOR, hair_color); + clif_displaymessage(fd, msg_table[36]); // Appearence changed. + } + } else { + clif_displaymessage(fd, msg_table[37]); // An invalid number was specified. + return -1; + } + + return 0; +} + +/*========================================== + * @charhaircolor by [MouseJstr] + *------------------------------------------ + */ +int +atcommand_charhaircolor(const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + return 0; +} + +/*========================================== + * @go [city_number/city_name]: improved by [yor] to add city names and help + *------------------------------------------ + */ +int atcommand_go( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int i; + int town; + char map_name[100]; + char output[200]; + int m; + + struct { char map[16]; int x, y; } data[] = { + { "prontera.gat", 156, 191 }, // 0=Prontera + { "morocc.gat", 156, 93 }, // 1=Morroc + { "geffen.gat", 119, 59 }, // 2=Geffen + { "payon.gat", 162, 233 }, // 3=Payon + { "alberta.gat", 192, 147 }, // 4=Alberta + { "izlude.gat", 128, 114 }, // 5=Izlude + { "aldebaran.gat",140, 131 }, // 6=Al de Baran + { "xmas.gat", 147, 134 }, // 7=Lutie + { "comodo.gat", 209, 143 }, // 8=Comodo + { "yuno.gat", 157, 51 }, // 9=Yuno + { "amatsu.gat", 198, 84 }, // 10=Amatsu + { "gonryun.gat", 160, 120 }, // 11=Gon Ryun + { "umbala.gat", 89, 157 }, // 12=Umbala + { "niflheim.gat", 21, 153 }, // 13=Niflheim + { "louyang.gat", 217, 40 }, // 14=Lou Yang + { "new_1-1.gat", 53, 111 }, // 15=Start point + { "sec_pri.gat", 23, 61 }, // 16=Prison + }; + + memset(map_name, '\0', sizeof(map_name)); + memset(output, '\0', sizeof(output)); + + // get the number + town = atoi(message); + + // if no value, display all value + if (!message || !*message || sscanf(message, "%99s", map_name) < 1 || town < -3 || town >= (int)(sizeof(data) / sizeof(data[0]))) { + clif_displaymessage(fd, msg_table[38]); // Invalid location number or name. + clif_displaymessage(fd, msg_table[82]); // Please, use one of this number/name: + clif_displaymessage(fd, "-3=(Memo point 2) 4=Alberta 11=Gon Ryun"); + clif_displaymessage(fd, "-2=(Memo point 1) 5=Izlude 12=Umbala"); + clif_displaymessage(fd, "-1=(Memo point 0) 6=Al de Baran 13=Niflheim"); + clif_displaymessage(fd, " 0=Prontera 7=Lutie 14=Lou Yang"); + clif_displaymessage(fd, " 1=Morroc 8=Comodo 15=Start point"); + clif_displaymessage(fd, " 2=Geffen 9=Yuno 16=Prison"); + clif_displaymessage(fd, " 3=Payon 10=Amatsu"); + return -1; + } else { + // get possible name of the city and add .gat if not in the name + map_name[sizeof(map_name)-1] = '\0'; + for (i = 0; map_name[i]; i++) + map_name[i] = tolower(map_name[i]); + if (strstr(map_name, ".gat") == NULL && strstr(map_name, ".afm") == NULL && strlen(map_name) < 13) // 16 - 4 (.gat) + strcat(map_name, ".gat"); + // try to see if it's a name, and not a number (try a lot of possibilities, write errors and abbreviations too) + if (strncmp(map_name, "prontera.gat", 3) == 0) { // 3 first characters + town = 0; + } else if (strncmp(map_name, "morocc.gat", 3) == 0) { // 3 first characters + town = 1; + } else if (strncmp(map_name, "geffen.gat", 3) == 0) { // 3 first characters + town = 2; + } else if (strncmp(map_name, "payon.gat", 3) == 0 || // 3 first characters + strncmp(map_name, "paion.gat", 3) == 0) { // writing error (3 first characters) + town = 3; + } else if (strncmp(map_name, "alberta.gat", 3) == 0) { // 3 first characters + town = 4; + } else if (strncmp(map_name, "izlude.gat", 3) == 0 || // 3 first characters + strncmp(map_name, "islude.gat", 3) == 0) { // writing error (3 first characters) + town = 5; + } else if (strncmp(map_name, "aldebaran.gat", 3) == 0 || // 3 first characters + strcmp(map_name, "al.gat") == 0) { // al (de baran) + town = 6; + } else if (strncmp(map_name, "lutie.gat", 3) == 0 || // name of the city, not name of the map (3 first characters) + strcmp(map_name, "christmas.gat") == 0 || // name of the symbol + strncmp(map_name, "xmas.gat", 3) == 0 || // 3 first characters + strncmp(map_name, "x-mas.gat", 3) == 0) { // writing error (3 first characters) + town = 7; + } else if (strncmp(map_name, "comodo.gat", 3) == 0) { // 3 first characters + town = 8; + } else if (strncmp(map_name, "yuno.gat", 3) == 0) { // 3 first characters + town = 9; + } else if (strncmp(map_name, "amatsu.gat", 3) == 0 || // 3 first characters + strncmp(map_name, "ammatsu.gat", 3) == 0) { // writing error (3 first characters) + town = 10; + } else if (strncmp(map_name, "gonryun.gat", 3) == 0) { // 3 first characters + town = 11; + } else if (strncmp(map_name, "umbala.gat", 3) == 0) { // 3 first characters + town = 12; + } else if (strncmp(map_name, "niflheim.gat", 3) == 0) { // 3 first characters + town = 13; + } else if (strncmp(map_name, "louyang.gat", 3) == 0) { // 3 first characters + town = 14; + } else if (strncmp(map_name, "new_1-1.gat", 3) == 0 || // 3 first characters (or "newbies") + strncmp(map_name, "startpoint.gat", 3) == 0 || // name of the position (3 first characters) + strncmp(map_name, "begining.gat", 3) == 0) { // name of the position (3 first characters) + town = 15; + } else if (strncmp(map_name, "sec_pri.gat", 3) == 0 || // 3 first characters + strncmp(map_name, "prison.gat", 3) == 0 || // name of the position (3 first characters) + strncmp(map_name, "jails.gat", 3) == 0) { // name of the position + town = 16; + } + + if (town >= -3 && town <= -1) { + if (sd->status.memo_point[-town-1].map[0]) { + m = map_mapname2mapid(sd->status.memo_point[-town-1].map); + if (m >= 0 && map[m].flag.nowarpto && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you to this memo map."); + return -1; + } + if (sd->bl.m >= 0 && map[sd->bl.m].flag.nowarp && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you from your actual map."); + return -1; + } + if (pc_setpos(sd, sd->status.memo_point[-town-1].map, sd->status.memo_point[-town-1].x, sd->status.memo_point[-town-1].y, 3) == 0) { + clif_displaymessage(fd, msg_table[0]); // Warped. + } else { + clif_displaymessage(fd, msg_table[1]); // Map not found. + return -1; + } + } else { + sprintf(output, msg_table[164], -town-1); // Your memo point #%d doesn't exist. + clif_displaymessage(fd, output); + return -1; + } + } else if (town >= 0 && town < (int)(sizeof(data) / sizeof(data[0]))) { + m = map_mapname2mapid(data[town].map); + if (m >= 0 && map[m].flag.nowarpto && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you to this destination map."); + return -1; + } + if (sd->bl.m >= 0 && map[sd->bl.m].flag.nowarp && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp you from your actual map."); + return -1; + } + if (pc_setpos(sd, data[town].map, data[town].x, data[town].y, 3) == 0) { + clif_displaymessage(fd, msg_table[0]); // Warped. + } else { + clif_displaymessage(fd, msg_table[1]); // Map not found. + return -1; + } + } else { // if you arrive here, you have an error in town variable when reading of names + clif_displaymessage(fd, msg_table[38]); // Invalid location number or name. + return -1; + } + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_monster( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char name[100]; + char monster[100]; + char output[200]; + int mob_id; + int number = 0; + int x = 0, y = 0; + int count; + int i, j, k; + int mx, my, range; + + memset(name, '\0', sizeof(name)); + memset(monster, '\0', sizeof(monster)); + memset(output, '\0', sizeof(output)); + + if (!message || !*message || + (sscanf(message, "\"%[^\"]\" %99s %d %d %d", name, monster, &number, &x, &y) < 2 && + sscanf(message, "%99s \"%[^\"]\" %d %d %d", monster, name, &number, &x, &y) < 2 && + sscanf(message, "%99s %99s %d %d %d", name, monster, &number, &x, &y) < 2)) { + clif_displaymessage(fd, msg_table[80]); // Give a display name and monster name/id please. + return -1; + } + + if ((mob_id = mobdb_searchname(monster)) == 0) // check name first (to avoid possible name begining by a number) + mob_id = mobdb_checkid(atoi(monster)); + + if (mob_id == 0) { + clif_displaymessage(fd, msg_table[40]); // Invalid monster ID or name. + return -1; + } + + if (mob_id == 1288) { + clif_displaymessage(fd, msg_table[83]); // Cannot spawn emperium. + return -1; + } + + if (number <= 0) + number = 1; + + if (strlen(name) < 1) + strcpy(name, "--ja--"); + + // If value of atcommand_spawn_quantity_limit directive is greater than or equal to 1 and quantity of monsters is greater than value of the directive + if (battle_config.atc_spawn_quantity_limit >= 1 && number > battle_config.atc_spawn_quantity_limit) + number = battle_config.atc_spawn_quantity_limit; + + if (battle_config.etc_log) + printf("%s monster='%s' name='%s' id=%d count=%d (%d,%d)\n", command, monster, name, mob_id, number, x, y); + + count = 0; + range = sqrt(number) / 2; + range = range * 2 + 5; // calculation of an odd number (+ 4 area around) + for (i = 0; i < number; i++) { + j = 0; + k = 0; + while(j++ < 8 && k == 0) { // try 8 times to spawn the monster (needed for close area) + if (x <= 0) + mx = sd->bl.x + (rand() % range - (range / 2)); + else + mx = x; + if (y <= 0) + my = sd->bl.y + (rand() % range - (range / 2)); + else + my = y; + k = mob_once_spawn((struct map_session_data*)sd, "this", mx, my, name, mob_id, 1, ""); + } + count += (k != 0) ? 1 : 0; + } + + if (count != 0) + if (number == count) + clif_displaymessage(fd, msg_table[39]); // All monster summoned! + else { + sprintf(output, msg_table[240], count); // %d monster(s) summoned! + clif_displaymessage(fd, output); + } + else { + clif_displaymessage(fd, msg_table[40]); // Invalid monster ID or name. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_spawn( + const int fd, struct map_session_data* sd, + const char* command, const char* message) { + char name[100]; + char monster[100]; + char output[200]; + int mob_id; + int number = 0; + int x = 0, y = 0; + int count; + int i, j, k; + int mx, my, range; + + memset(name, '\0', sizeof(name)); + memset(monster, '\0', sizeof(monster)); + memset(output, '\0', sizeof(output)); + + if (!message || !*message || + (sscanf(message, "\"%[^\"]\" %99s %d %d %d", name, monster, &number, &x, &y) < 2 && + sscanf(message, "%99s \"%[^\"]\" %d %d %d", monster, name, &number, &x, &y) < 2 && + sscanf(message, "%99s %d %99s %d %d", monster, &number, name, &x, &y) < 1)) { + clif_displaymessage(fd, msg_table[143]); // Give a monster name/id please. + return -1; + } + + // If monster identifier/name argument is a name + if ((mob_id = mobdb_searchname(monster)) == 0) // check name first (to avoid possible name begining by a number) + mob_id = mobdb_checkid(atoi(monster)); + + if (mob_id == 0) { + clif_displaymessage(fd, msg_table[40]); // Invalid monster ID or name. + return -1; + } + + if (mob_id == 1288) { + clif_displaymessage(fd, msg_table[83]); // Cannot spawn emperium. + return -1; + } + + if (number <= 0) + number = 1; + + if (strlen(name) < 1) + strcpy(name, "--ja--"); + + // If value of atcommand_spawn_quantity_limit directive is greater than or equal to 1 and quantity of monsters is greater than value of the directive + if (battle_config.atc_spawn_quantity_limit >= 1 && number > battle_config.atc_spawn_quantity_limit) + number = battle_config.atc_spawn_quantity_limit; + + if (battle_config.etc_log) + printf("%s monster='%s' name='%s' id=%d count=%d (%d,%d)\n", command, monster, name, mob_id, number, x, y); + + count = 0; + range = sqrt(number) / 2; + range = range * 2 + 5; // calculation of an odd number (+ 4 area around) + for (i = 0; i < number; i++) { + j = 0; + k = 0; + while(j++ < 8 && k == 0) { // try 8 times to spawn the monster (needed for close area) + if (x <= 0) + mx = sd->bl.x + (rand() % range - (range / 2)); + else + mx = x; + if (y <= 0) + my = sd->bl.y + (rand() % range - (range / 2)); + else + my = y; + k = mob_once_spawn((struct map_session_data*)sd, "this", mx, my, name, mob_id, 1, ""); + } + count += (k != 0) ? 1 : 0; + } + + if (count != 0) + if (number == count) + clif_displaymessage(fd, msg_table[39]); // All monster summoned! + else { + sprintf(output, msg_table[240], count); // %d monster(s) summoned! + clif_displaymessage(fd, output); + } + else { + clif_displaymessage(fd, msg_table[40]); // Invalid monster ID or name. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +void atcommand_killmonster_sub( + const int fd, struct map_session_data* sd, const char* message, + const int drop) +{ + int map_id; + char map_name[100]; + + memset(map_name, '\0', sizeof(map_name)); + + if (!message || !*message || sscanf(message, "%99s", map_name) < 1) + map_id = sd->bl.m; + else { + if (strstr(map_name, ".gat") == NULL && strstr(map_name, ".afm") == NULL && strlen(map_name) < 13) // 16 - 4 (.gat) + strcat(map_name, ".gat"); + if ((map_id = map_mapname2mapid(map_name)) < 0) + map_id = sd->bl.m; + } + + map_foreachinarea(atkillmonster_sub, map_id, 0, 0, map[map_id].xs, map[map_id].ys, BL_MOB, drop); + + clif_displaymessage(fd, msg_table[165]); // All monsters killed! + + return; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_killmonster( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + atcommand_killmonster_sub(fd, sd, message, 1); + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_killmonster2( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + atcommand_killmonster_sub(fd, sd, message, 0); + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_refine( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int i, position = 0, refine = 0, current_position, final_refine; + int count; + char output[200]; + + memset(output, '\0', sizeof(output)); + + if (!message || !*message || sscanf(message, "%d %d", &position, &refine) < 2) { + clif_displaymessage(fd, "Please, enter a position and a amount (usage: @refine <+/- amount>)."); + return -1; + } + + if (refine < -10) + refine = -10; + else if (refine > 10) + refine = 10; + else if (refine == 0) + refine = 1; + + count = 0; + for (i = 0; i < MAX_INVENTORY; i++) { + if (sd->status.inventory[i].nameid && // 該当個所の装備を精錬する + (sd->status.inventory[i].equip & position || + (sd->status.inventory[i].equip && !position))) { + final_refine = sd->status.inventory[i].refine + refine; + if (final_refine > 10) + final_refine = 10; + else if (final_refine < 0) + final_refine = 0; + if (sd->status.inventory[i].refine != final_refine) { + sd->status.inventory[i].refine = final_refine; + current_position = sd->status.inventory[i].equip; + pc_unequipitem(sd, i, 0); + clif_refine(fd, sd, 0, i, sd->status.inventory[i].refine); + clif_delitem(sd, i, 1); + clif_additem(sd, i, 1, 0); + pc_equipitem(sd, i, current_position); + clif_misceffect((struct block_list*)&sd->bl, 3); + count++; + } + } + } + + if (count == 0) + clif_displaymessage(fd, msg_table[166]); // No item has been refined! + else if (count == 1) + clif_displaymessage(fd, msg_table[167]); // 1 item has been refined! + else { + sprintf(output, msg_table[168], count); // %d items have been refined! + clif_displaymessage(fd, output); + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_produce( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char item_name[100]; + int item_id, attribute = 0, star = 0; + int flag = 0; + struct item_data *item_data; + struct item tmp_item; + char output[200]; + + memset(output, '\0', sizeof(output)); + memset(item_name, '\0', sizeof(item_name)); + + if (!message || !*message || sscanf(message, "%99s %d %d", item_name, &attribute, &star) < 1) { + clif_displaymessage(fd, "Please, enter at least an item name/id (usage: @produce <# of very's>)."); + return -1; + } + + item_id = 0; + if ((item_data = itemdb_searchname(item_name)) != NULL || + (item_data = itemdb_exists(atoi(item_name))) != NULL) + item_id = item_data->nameid; + + if (itemdb_exists(item_id) && + (item_id <= 500 || item_id > 1099) && + (item_id < 4001 || item_id > 4148) && + (item_id < 7001 || item_id > 10019) && + itemdb_isequip(item_id)) { + if (attribute < MIN_ATTRIBUTE || attribute > MAX_ATTRIBUTE) + attribute = ATTRIBUTE_NORMAL; + if (star < MIN_STAR || star > MAX_STAR) + star = 0; + memset(&tmp_item, 0, sizeof tmp_item); + tmp_item.nameid = item_id; + tmp_item.amount = 1; + tmp_item.identify = 1; + tmp_item.card[0] = 0x00ff; + tmp_item.card[1] = ((star * 5) << 8) + attribute; + *((unsigned long *)(&tmp_item.card[2])) = sd->char_id; + clif_produceeffect(sd, 0, item_id); // 製造エフェクトパケット + clif_misceffect(&sd->bl, 3); // 他人にも成功を通知 + if ((flag = pc_additem(sd, &tmp_item, 1))) + clif_additem(sd, 0, 0, flag); + } else { + if (battle_config.error_log) + printf("@produce NOT WEAPON [%d]\n", item_id); + if (item_id != 0 && itemdb_exists(item_id)) + sprintf(output, msg_table[169], item_id, item_data->name); // This item (%d: '%s') is not an equipment. + else + sprintf(output, msg_table[170]); // This item is not an equipment. + clif_displaymessage(fd, output); + return -1; + } + + return 0; +} + +/*========================================== + * Sub-function to display actual memo points + *------------------------------------------ + */ +void atcommand_memo_sub(struct map_session_data* sd) { + int i; + char output[200]; + + memset(output, '\0', sizeof(output)); + + clif_displaymessage(sd->fd, "Your actual memo positions are (except respawn point):"); + for (i = MIN_PORTAL_MEMO; i <= MAX_PORTAL_MEMO; i++) { + if (sd->status.memo_point[i].map[0]) + sprintf(output, "%d - %s (%d,%d)", i, sd->status.memo_point[i].map, sd->status.memo_point[i].x, sd->status.memo_point[i].y); + else + sprintf(output, msg_table[171], i); // %d - void + clif_displaymessage(sd->fd, output); + } + + return; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_memo( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int position = 0; + char output[200]; + + memset(output, '\0', sizeof(output)); + + if (!message || !*message || sscanf(message, "%d", &position) < 1) + atcommand_memo_sub(sd); + else { + if (position >= MIN_PORTAL_MEMO && position <= MAX_PORTAL_MEMO) { + if (sd->bl.m >= 0 && map[sd->bl.m].flag.nowarpto && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to memo this map."); + return -1; + } + if (sd->status.memo_point[position].map[0]) { + sprintf(output, msg_table[172], position, sd->status.memo_point[position].map, sd->status.memo_point[position].x, sd->status.memo_point[position].y); // You replace previous memo position %d - %s (%d,%d). + clif_displaymessage(fd, output); + } + memcpy(sd->status.memo_point[position].map, map[sd->bl.m].name, 24); + sd->status.memo_point[position].x = sd->bl.x; + sd->status.memo_point[position].y = sd->bl.y; + clif_skill_memo(sd, 0); + if (pc_checkskill(sd, AL_WARP) <= (position + 1)) + clif_displaymessage(fd, msg_table[173]); // Note: you don't have the 'Warp' skill level to use it. + atcommand_memo_sub(sd); + } else { + sprintf(output, "Please, enter a valid position (usage: @memo ).", MIN_PORTAL_MEMO, MAX_PORTAL_MEMO); + clif_displaymessage(fd, output); + atcommand_memo_sub(sd); + return -1; + } + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_gat( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char output[200]; + int y; + + memset(output, '\0', sizeof(output)); + + for (y = 2; y >= -2; y--) { + sprintf(output, "%s (x= %d, y= %d) %02X %02X %02X %02X %02X", + map[sd->bl.m].name, sd->bl.x - 2, sd->bl.y + y, + map_getcell(sd->bl.m, sd->bl.x - 2, sd->bl.y + y), + map_getcell(sd->bl.m, sd->bl.x - 1, sd->bl.y + y), + map_getcell(sd->bl.m, sd->bl.x, sd->bl.y + y), + map_getcell(sd->bl.m, sd->bl.x + 1, sd->bl.y + y), + map_getcell(sd->bl.m, sd->bl.x + 2, sd->bl.y + y)); + clif_displaymessage(fd, output); + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_packet( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int x = 0, y = 0; + + if (!message || !*message || sscanf(message, "%d %d", &x, &y) < 2) { + clif_displaymessage(fd, "Please, enter a status type/flag (usage: @packet )."); + return -1; + } + + clif_status_change(&sd->bl, x, y); + + return 0; +} + +/*========================================== + * @stpoint (Rewritten by [Yor]) + *------------------------------------------ + */ +int atcommand_statuspoint( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int point, new_status_point; + + if (!message || !*message || (point = atoi(message)) == 0) { + clif_displaymessage(fd, "Please, enter a number (usage: @stpoint )."); + return -1; + } + + new_status_point = (int)sd->status.status_point + point; + if (point > 0 && (point > 0x7FFF || new_status_point > 0x7FFF)) // fix positiv overflow + new_status_point = 0x7FFF; + else if (point < 0 && (point < -0x7FFF || new_status_point < 0)) // fix negativ overflow + new_status_point = 0; + + if (new_status_point != (int)sd->status.status_point) { + sd->status.status_point = (short)new_status_point; + clif_updatestatus(sd, SP_STATUSPOINT); + clif_displaymessage(fd, msg_table[174]); // Number of status points changed! + } else { + if (point < 0) + clif_displaymessage(fd, msg_table[41]); // Impossible to decrease the number/value. + else + clif_displaymessage(fd, msg_table[149]); // Impossible to increase the number/value. + return -1; + } + + return 0; +} + +/*========================================== + * @skpoint (Rewritten by [Yor]) + *------------------------------------------ + */ +int atcommand_skillpoint( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int point, new_skill_point; + + if (!message || !*message || (point = atoi(message)) == 0) { + clif_displaymessage(fd, "Please, enter a number (usage: @skpoint )."); + return -1; + } + + new_skill_point = (int)sd->status.skill_point + point; + if (point > 0 && (point > 0x7FFF || new_skill_point > 0x7FFF)) // fix positiv overflow + new_skill_point = 0x7FFF; + else if (point < 0 && (point < -0x7FFF || new_skill_point < 0)) // fix negativ overflow + new_skill_point = 0; + + if (new_skill_point != (int)sd->status.skill_point) { + sd->status.skill_point = (short)new_skill_point; + clif_updatestatus(sd, SP_SKILLPOINT); + clif_displaymessage(fd, msg_table[175]); // Number of skill points changed! + } else { + if (point < 0) + clif_displaymessage(fd, msg_table[41]); // Impossible to decrease the number/value. + else + clif_displaymessage(fd, msg_table[149]); // Impossible to increase the number/value. + return -1; + } + + return 0; +} + +/*========================================== + * @zeny (Rewritten by [Yor]) + *------------------------------------------ + */ +int atcommand_zeny( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int zeny, new_zeny; + + if (!message || !*message || (zeny = atoi(message)) == 0) { + clif_displaymessage(fd, "Please, enter an amount (usage: @zeny )."); + return -1; + } + + new_zeny = sd->status.zeny + zeny; + if (zeny > 0 && (zeny > MAX_ZENY || new_zeny > MAX_ZENY)) // fix positiv overflow + new_zeny = MAX_ZENY; + else if (zeny < 0 && (zeny < -MAX_ZENY || new_zeny < 0)) // fix negativ overflow + new_zeny = 0; + + if (new_zeny != sd->status.zeny) { + sd->status.zeny = new_zeny; + clif_updatestatus(sd, SP_ZENY); + clif_displaymessage(fd, msg_table[176]); // Number of zenys changed! + } else { + if (zeny < 0) + clif_displaymessage(fd, msg_table[41]); // Impossible to decrease the number/value. + else + clif_displaymessage(fd, msg_table[149]); // Impossible to increase the number/value. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_param( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int i, index, value = 0, new_value; + const char* param[] = { "@str", "@agi", "@vit", "@int", "@dex", "@luk", NULL }; + short* status[] = { + &sd->status.str, &sd->status.agi, &sd->status.vit, + &sd->status.int_, &sd->status.dex, &sd->status.luk + }; + char output[200]; + + memset(output, '\0', sizeof(output)); + + if (!message || !*message || sscanf(message, "%d", &value) < 1 || value == 0) { + sprintf(output, "Please, enter a valid value (usage: @str,@agi,@vit,@int,@dex,@luk <+/-adjustement>)."); + clif_displaymessage(fd, output); + return -1; + } + + index = -1; + for (i = 0; param[i] != NULL; i++) { + if (strcmpi(command, param[i]) == 0) { + index = i; + break; + } + } + if (index < 0 || index > MAX_STATUS_TYPE) { // normaly impossible... + sprintf(output, "Please, enter a valid value (usage: @str,@agi,@vit,@int,@dex,@luk <+/-adjustement>)."); + clif_displaymessage(fd, output); + return -1; + } + + new_value = (int)*status[index] + value; + if (value > 0 && (value > battle_config.max_parameter || new_value > battle_config.max_parameter)) // fix positiv overflow + new_value = battle_config.max_parameter; + else if (value < 0 && (value < -battle_config.max_parameter || new_value < 1)) // fix negativ overflow + new_value = 1; + + if (new_value != (int)*status[index]) { + *status[index] = new_value; + clif_updatestatus(sd, SP_STR + index); + clif_updatestatus(sd, SP_USTR + index); + pc_calcstatus(sd, 0); + clif_displaymessage(fd, msg_table[42]); // Stat changed. + } else { + if (value < 0) + clif_displaymessage(fd, msg_table[41]); // Impossible to decrease the number/value. + else + clif_displaymessage(fd, msg_table[149]); // Impossible to increase the number/value. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +//** Stat all by fritz (rewritten by [Yor]) +int atcommand_stat_all( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int index, count, value = 0, new_value; + short* status[] = { + &sd->status.str, &sd->status.agi, &sd->status.vit, + &sd->status.int_, &sd->status.dex, &sd->status.luk + }; + + if (!message || !*message || sscanf(message, "%d", &value) < 1 || value == 0) + value = battle_config.max_parameter; + + count = 0; + for (index = 0; index < (int)(sizeof(status) / sizeof(status[0])); index++) { + + new_value = (int)*status[index] + value; + if (value > 0 && (value > battle_config.max_parameter || new_value > battle_config.max_parameter)) // fix positiv overflow + new_value = battle_config.max_parameter; + else if (value < 0 && (value < -battle_config.max_parameter || new_value < 1)) // fix negativ overflow + new_value = 1; + + if (new_value != (int)*status[index]) { + *status[index] = new_value; + clif_updatestatus(sd, SP_STR + index); + clif_updatestatus(sd, SP_USTR + index); + pc_calcstatus(sd, 0); + count++; + } + } + + if (count > 0) // if at least 1 stat modified + clif_displaymessage(fd, msg_table[84]); // All stats changed! + else { + if (value < 0) + clif_displaymessage(fd, msg_table[177]); // Impossible to decrease a stat. + else + clif_displaymessage(fd, msg_table[178]); // Impossible to increase a stat. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_guildlevelup( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int level = 0; + short added_level; + struct guild *guild_info; + + if (!message || !*message || sscanf(message, "%d", &level) < 1 || level == 0) { + clif_displaymessage(fd, "Please, enter a valid level (usage: @guildlvup/@guildlvlup <# of levels>)."); + return -1; + } + + if (sd->status.guild_id <= 0 || (guild_info = guild_search(sd->status.guild_id)) == NULL) { + clif_displaymessage(fd, msg_table[43]); // You're not in a guild. + return -1; + } + if (strcmp(sd->status.name, guild_info->master) != 0) { + clif_displaymessage(fd, msg_table[44]); // You're not the master of your guild. + return -1; + } + + added_level = (short)level; + if (level > 0 && (level > MAX_GUILDLEVEL || added_level > ((short)MAX_GUILDLEVEL - guild_info->guild_lv))) // fix positiv overflow + added_level = (short)MAX_GUILDLEVEL - guild_info->guild_lv; + else if (level < 0 && (level < -MAX_GUILDLEVEL || added_level < (1 - guild_info->guild_lv))) // fix negativ overflow + added_level = 1 - guild_info->guild_lv; + + if (added_level != 0) { + intif_guild_change_basicinfo(guild_info->guild_id, GBI_GUILDLV, &added_level, 2); + clif_displaymessage(fd, msg_table[179]); // Guild level changed. + } else { + clif_displaymessage(fd, msg_table[45]); // Guild level change failed. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_makeegg( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + struct item_data *item_data; + int id, pet_id; + + if (!message || !*message) { + clif_displaymessage(fd, "Please, enter a monter/egg name/id (usage: @makeegg )."); + return -1; + } + + if ((item_data = itemdb_searchname(message)) != NULL) // for egg name + id = item_data->nameid; + else if ((id = mobdb_searchname(message)) == 0) // for monster name + id = atoi(message); + + pet_id = search_petDB_index(id, PET_CLASS); + if (pet_id < 0) + pet_id = search_petDB_index(id, PET_EGG); + if (pet_id >= 0) { + sd->catch_target_class = pet_db[pet_id].class; + intif_create_pet( + sd->status.account_id, sd->status.char_id, + pet_db[pet_id].class, mob_db[pet_db[pet_id].class].lv, + pet_db[pet_id].EggID, 0, pet_db[pet_id].intimate, + 100, 0, 1, pet_db[pet_id].jname); + } else { + clif_displaymessage(fd, msg_table[180]); // The monter/egg name/id doesn't exist. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_hatch( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + if (sd->status.pet_id <= 0) + clif_sendegg(sd); + else { + clif_displaymessage(fd, msg_table[181]); // You already have a pet. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_petfriendly( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int friendly; + int t; + + if (!message || !*message || (friendly = atoi(message)) < 0) { + clif_displaymessage(fd, "Please, enter a valid value (usage: @petfriendly <0-1000>)."); + return -1; + } + + if (sd->status.pet_id > 0 && sd->pd) { + if (friendly >= 0 && friendly <= 1000) { + if (friendly != sd->pet.intimate) { + t = sd->pet.intimate; + sd->pet.intimate = friendly; + clif_send_petstatus(sd); + if (battle_config.pet_status_support) { + if ((sd->pet.intimate > 0 && t <= 0) || + (sd->pet.intimate <= 0 && t > 0)) { + if (sd->bl.prev != NULL) + pc_calcstatus(sd, 0); + else + pc_calcstatus(sd, 2); + } + } + clif_displaymessage(fd, msg_table[182]); // Pet friendly value changed! + } else { + clif_displaymessage(fd, msg_table[183]); // Pet friendly is already the good value. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[37]); // An invalid number was specified. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[184]); // Sorry, but you have no pet. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_pethungry( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + int hungry; + + if (!message || !*message || (hungry = atoi(message)) < 0) { + clif_displaymessage(fd, "Please, enter a valid number (usage: @pethungry <0-100>)."); + return -1; + } + + if (sd->status.pet_id > 0 && sd->pd) { + if (hungry >= 0 && hungry <= 100) { + if (hungry != sd->pet.hungry) { + sd->pet.hungry = hungry; + clif_send_petstatus(sd); + clif_displaymessage(fd, msg_table[185]); // Pet hungry value changed! + } else { + clif_displaymessage(fd, msg_table[186]); // Pet hungry is already the good value. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[37]); // An invalid number was specified. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[184]); // Sorry, but you have no pet. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_petrename( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + if (sd->status.pet_id > 0 && sd->pd) { + if (sd->pet.rename_flag != 0) { + sd->pet.rename_flag = 0; + intif_save_petdata(sd->status.account_id, &sd->pet); + clif_send_petstatus(sd); + clif_displaymessage(fd, msg_table[187]); // You can now rename your pet. + } else { + clif_displaymessage(fd, msg_table[188]); // You can already rename your pet. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[184]); // Sorry, but you have no pet. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_charpetrename( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char character[100]; + struct map_session_data *pl_sd; + + memset(character, '\0', sizeof(character)); + + if (!message || !*message || sscanf(message, "%99[^\n]", character) < 1) { + clif_displaymessage(fd, "Please, enter a player name (usage: @charpetrename )."); + return -1; + } + + if ((pl_sd = map_nick2sd(character)) != NULL) { + if (pl_sd->status.pet_id > 0 && pl_sd->pd) { + if (pl_sd->pet.rename_flag != 0) { + pl_sd->pet.rename_flag = 0; + intif_save_petdata(pl_sd->status.account_id, &pl_sd->pet); + clif_send_petstatus(pl_sd); + clif_displaymessage(fd, msg_table[189]); // This player can now rename his/her pet. + } else { + clif_displaymessage(fd, msg_table[190]); // This player can already rename his/her pet. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[191]); // Sorry, but this player has no pet. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[3]); // Character not found. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_recall( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char character[100]; + char output[200]; + struct map_session_data *pl_sd; + + memset(character, '\0', sizeof(character)); + memset(output, '\0', sizeof(output)); + + if (!message || !*message || sscanf(message, "%99[^\n]", character) < 1) { + clif_displaymessage(fd, "Please, enter a player name (usage: @recall )."); + return -1; + } + + if ((pl_sd = map_nick2sd(character)) != NULL) { + if (pc_isGM(sd) >= pc_isGM(pl_sd)) { // you can recall only lower or same level + if (sd->bl.m >= 0 && map[sd->bl.m].flag.nowarpto && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp somenone to your actual map."); + return -1; + } + if (pl_sd->bl.m >= 0 && map[pl_sd->bl.m].flag.nowarp && battle_config.any_warp_GM_min_level > pc_isGM(sd)) { + clif_displaymessage(fd, "You are not authorised to warp this player from its actual map."); + return -1; + } + pc_setpos(pl_sd, sd->mapname, sd->bl.x, sd->bl.y, 2); + sprintf(output, msg_table[46], character); // %s recalled! + clif_displaymessage(fd, output); + } else { + clif_displaymessage(fd, msg_table[81]); // Your GM level don't authorise you to do this action on this player. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[3]); // Character not found. + return -1; + } + + return 0; +} + +/*========================================== + * 対象キャラクターを転職させる upper指定で転生や養子も可能 + *------------------------------------------ + */ +int atcommand_character_job( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char character[100]; + struct map_session_data* pl_sd; + int job = 0, upper = -1; + + memset(character, '\0', sizeof(character)); + + if (!message || !*message) { + clif_displaymessage(fd, "Please, enter a job and a player name (usage: @charjob/@charjobchange )."); + return -1; + } + + if (sscanf(message, "%d %d %99[^\n]", &job, &upper, character) < 3) { //upper指定してある + upper = -1; + if (sscanf(message, "%d %99[^\n]", &job, character) < 2) { //upper指定してない上に何か足りない + clif_displaymessage(fd, "Please, enter a job and a player name (usage: @charjob/@charjobchange )."); + return -1; + } + } + + if ((pl_sd = map_nick2sd(character)) != NULL) { + if (pc_isGM(sd) >= pc_isGM(pl_sd)) { // you can change job only to lower or same level + if ((job >= 0 && job < MAX_PC_CLASS)) { + + // fix pecopeco display + if ((job != 13 && job != 21 && job != 4014 && job != 4022)) { + if (pc_isriding(sd)) { + if (pl_sd->status.class == 13) + pl_sd->status.class = pl_sd->view_class = 7; + if (pl_sd->status.class == 21) + pl_sd->status.class = pl_sd->view_class = 14; + if (pl_sd->status.class == 4014) + pl_sd->status.class = pl_sd->view_class = 4008; + if (pl_sd->status.class == 4022) + pl_sd->status.class = pl_sd->view_class = 4015; + pl_sd->status.option &= ~0x0020; + clif_changeoption(&pl_sd->bl); + pc_calcstatus(pl_sd, 0); + } + } else { + if (!pc_isriding(sd)) { + if (job == 13) + job = 7; + if (job == 21) + job = 14; + if (job == 4014) + job = 4008; + if (job == 4022) + job = 4015; + } + } + + if (pc_jobchange(pl_sd, job, upper) == 0) + clif_displaymessage(fd, msg_table[48]); // Character's job changed. + else { + clif_displaymessage(fd, msg_table[192]); // Impossible to change the character's job. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[49]); // Invalid job ID. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[81]); // Your GM level don't authorise you to do this action on this player. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[3]); // Character not found. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_revive( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char character[100]; + struct map_session_data *pl_sd; + + memset(character, '\0', sizeof(character)); + + if (!message || !*message || sscanf(message, "%99[^\n]", character) < 1) { + clif_displaymessage(fd, "Please, enter a player name (usage: @revive )."); + return -1; + } + + if ((pl_sd = map_nick2sd(character)) != NULL) { + pl_sd->status.hp = pl_sd->status.max_hp; + pc_setstand(pl_sd); + if (battle_config.pc_invincible_time > 0) + pc_setinvincibletimer(sd, battle_config.pc_invincible_time); + clif_updatestatus(pl_sd, SP_HP); + clif_updatestatus(pl_sd, SP_SP); + clif_resurrection(&pl_sd->bl, 1); + clif_displaymessage(fd, msg_table[51]); // Character revived. + } else { + clif_displaymessage(fd, msg_table[3]); // Character not found. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_character_stats( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char character[100]; + char job_jobname[100]; + char output[200]; + struct map_session_data *pl_sd; + int i; + + memset(character, '\0', sizeof(character)); + memset(job_jobname, '\0', sizeof(job_jobname)); + memset(output, '\0', sizeof(output)); + + if (!message || !*message || sscanf(message, "%99[^\n]", character) < 1) { + clif_displaymessage(fd, "Please, enter a player name (usage: @charstats )."); + return -1; + } + + if ((pl_sd = map_nick2sd(character)) != NULL) { + struct { + const char* format; + int value; + } output_table[] = { + { "Base Level - %d", pl_sd->status.base_level }, + { job_jobname, pl_sd->status.job_level }, + { "Hp - %d", pl_sd->status.hp }, + { "MaxHp - %d", pl_sd->status.max_hp }, + { "Sp - %d", pl_sd->status.sp }, + { "MaxSp - %d", pl_sd->status.max_sp }, + { "Str - %3d", pl_sd->status.str }, + { "Agi - %3d", pl_sd->status.agi }, + { "Vit - %3d", pl_sd->status.vit }, + { "Int - %3d", pl_sd->status.int_ }, + { "Dex - %3d", pl_sd->status.dex }, + { "Luk - %3d", pl_sd->status.luk }, + { "Zeny - %d", pl_sd->status.zeny }, + { NULL, 0 } + }; + sprintf(job_jobname, "Job - %s %s", job_name(pl_sd->status.class), "(level %d)"); + sprintf(output, msg_table[53], pl_sd->status.name); // '%s' stats: + clif_displaymessage(fd, output); + for (i = 0; output_table[i].format != NULL; i++) { + sprintf(output, output_table[i].format, output_table[i].value); + clif_displaymessage(fd, output); + } + } else { + clif_displaymessage(fd, msg_table[3]); // Character not found. + return -1; + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +//** Character Stats All by fritz +int atcommand_character_stats_all(const int fd, struct map_session_data* sd, const char* command, const char* message) +{ + char output[1024], gmlevel[1024]; + int i; + int count; + struct map_session_data *pl_sd; + + memset(output, '\0', sizeof(output)); + memset(gmlevel, '\0', sizeof(gmlevel)); + + count = 0; + for(i = 0; i < fd_max; i++) { + if (session[i] && (pl_sd = session[i]->session_data) && pl_sd->state.auth) { + + if (pc_isGM(pl_sd) > 0) + sprintf(gmlevel, "| GM Lvl: %d", pc_isGM(pl_sd)); + else + sprintf(gmlevel, " "); + + sprintf(output, "Name: %s | BLvl: %d | Job: %s (Lvl: %d) | HP: %d/%d | SP: %d/%d", pl_sd->status.name, pl_sd->status.base_level, job_name(pl_sd->status.class), pl_sd->status.job_level, pl_sd->status.hp, pl_sd->status.max_hp, pl_sd->status.sp, pl_sd->status.max_sp); + clif_displaymessage(fd, output); + sprintf(output, "STR: %d | AGI: %d | VIT: %d | INT: %d | DEX: %d | LUK: %d | Zeny: %d %s", pl_sd->status.str, pl_sd->status.agi, pl_sd->status.vit, pl_sd->status.int_, pl_sd->status.dex, pl_sd->status.luk, pl_sd->status.zeny, gmlevel); + clif_displaymessage(fd, output); + clif_displaymessage(fd, "--------"); + count++; + } + } + + if (count == 0) + clif_displaymessage(fd, msg_table[28]); // No player found. + else if (count == 1) + clif_displaymessage(fd, msg_table[29]); // 1 player found. + else { + sprintf(output, msg_table[30], count); // %d players found. + clif_displaymessage(fd, output); + } + + return 0; +} + +/*========================================== + * + *------------------------------------------ + */ +int atcommand_character_option( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char character[100]; + int opt1 = 0, opt2 = 0, opt3 = 0; + struct map_session_data* pl_sd; + + memset(character, '\0', sizeof(character)); + + if (!message || !*message || sscanf(message, "%d %d %d %99[^\n]", &opt1, &opt2, &opt3, character) < 4 || opt1 < 0 || opt2 < 0 || opt3 < 0) { + clif_displaymessage(fd, "Please, enter valid options and a player name (usage: @charoption )."); + return -1; + } + + if ((pl_sd = map_nick2sd(character)) != NULL) { + if (pc_isGM(sd) >= pc_isGM(pl_sd)) { // you can change option only to lower or same level + pl_sd->opt1 = opt1; + pl_sd->opt2 = opt2; + pl_sd->status.option = opt3; + // fix pecopeco display + if (pl_sd->status.class == 13 || pl_sd->status.class == 21 || pl_sd->status.class == 4014 || pl_sd->status.class == 4022) { + if (!pc_isriding(pl_sd)) { // pl_sd have the new value... + if (pl_sd->status.class == 13) + pl_sd->status.class = pl_sd->view_class = 7; + else if (pl_sd->status.class == 21) + pl_sd->status.class = pl_sd->view_class = 14; + else if (pl_sd->status.class == 4014) + pl_sd->status.class = pl_sd->view_class = 4008; + else if (pl_sd->status.class == 4022) + pl_sd->status.class = pl_sd->view_class = 4015; + } + } else { + if (pc_isriding(pl_sd)) { // pl_sd have the new value... + if (pl_sd->disguise > 0) { // temporary prevention of crash caused by peco + disguise, will look into a better solution [Valaris] (code added by [Yor]) + pl_sd->status.option &= ~0x0020; + } else { + if (pl_sd->status.class == 7) + pl_sd->status.class = pl_sd->view_class = 13; + else if (pl_sd->status.class == 14) + pl_sd->status.class = pl_sd->view_class = 21; + else if (pl_sd->status.class == 4008) + pl_sd->status.class = pl_sd->view_class = 4014; + else if (pl_sd->status.class == 4015) + pl_sd->status.class = pl_sd->view_class = 4022; + else + pl_sd->status.option &= ~0x0020; + } + } + } + clif_changeoption(&pl_sd->bl); + pc_calcstatus(pl_sd, 0); + clif_displaymessage(fd, msg_table[58]); // Character's options changed. + } else { + clif_displaymessage(fd, msg_table[81]); // Your GM level don't authorise you to do this action on this player. + return -1; + } + } else { + clif_displaymessage(fd, msg_table[3]); // Character not found. + return -1; + } + + return 0; +} + +/*========================================== + * charchangesex command (usage: charchangesex ) + *------------------------------------------ + */ +int atcommand_char_change_sex( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char character[100]; + + memset(character, '\0', sizeof(character)); + + if (!message || !*message || sscanf(message, "%99[^\n]", character) < 1) { + clif_displaymessage(fd, "Please, enter a player name (usage: @charchangesex )."); + return -1; + } + + // check player name + if (strlen(character) < 4) { + clif_displaymessage(fd, msg_table[86]); // Sorry, but a player name have at least 4 characters. + return -1; + } else if (strlen(character) > 23) { + clif_displaymessage(fd, msg_table[87]); // Sorry, but a player name have 23 characters maximum. + return -1; + } else { + chrif_char_ask_name(sd->status.account_id, character, 5, 0, 0, 0, 0, 0, 0); // type: 5 - changesex + clif_displaymessage(fd, msg_table[88]); // Character name sends to char-server to ask it. + } + + return 0; +} + +/*========================================== + * charblock command (usage: charblock ) + * This command do a definitiv ban on a player + *------------------------------------------ + */ +int atcommand_char_block( + const int fd, struct map_session_data* sd, + const char* command, const char* message) +{ + char character[100]; + + memset(character, '\0', sizeof(character)); + + if (!message || !*message || sscanf(message, "%99[^\n]", character) < 1) { + clif_displaymessage(fd, "Please, enter a player name (usage: @charblock/@block )."); + return -1; + } + + // check player name + if (strlen(character) < 4) { + clif_displaymessage(fd, msg_table[86]); // Sorry, but a player name have at least 4 characters. + return -1; + } else if (strlen(character) > 23) { + clif_displaymessage(fd, msg_table[87]); // Sorry, but a player name have 23 characters maximum. + return -1; + } else { + chrif_char_ask_name(sd->status.account_id, character, 1, 0, 0, 0, 0, 0, 0); // type: 1 - block + clif_displaymessage(fd, msg_table[88]); // Character name sends to char-server to ask it. + } + + return 0; +} + +/*========================================== + * charban command (usage: charban