/* This file is part of the OdinMS Maple Story Server Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc> Matthias Butz <matze@odinms.de> Jan Christian Meyer <vimes@odinms.de> This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation. You may not use, modify or distribute this program under any other version of the GNU Affero General Public License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package handling.channel.handler; import client.MapleClient; import handling.world.World; import handling.world.guild.MapleBBSThread; import java.util.List; import tools.data.LittleEndianAccessor; import tools.packet.CWvsContext.GuildPacket; public class BBSHandler { private static String correctLength(final String in, final int maxSize) { if (in.length() > maxSize) { return in.substring(0, maxSize); } return in; } public static final void BBSOperation(final LittleEndianAccessor slea, final MapleClient c) { if (c.getPlayer().getGuildId() <= 0) { return; // expelled while viewing bbs or hax } int localthreadid = 0; final byte action = slea.readByte(); switch (action) { case 0: // start a new post if (!c.getPlayer().getCheatTracker().canBBS()) { c.getPlayer().dropMessage(1, "You may only start a new thread every 60 seconds."); return; } final boolean bEdit = slea.readByte() > 0; if (bEdit) { localthreadid = slea.readInt(); } final boolean bNotice = slea.readByte() > 0; final String title = correctLength(slea.readMapleAsciiString(), 25); String text = correctLength(slea.readMapleAsciiString(), 600); final int icon = slea.readInt(); if (icon >= 0x64 && icon <= 0x6a) { if (!c.getPlayer().haveItem(5290000 + icon - 0x64, 1, false, true)) { return; // hax, using an nx icon that s/he doesn't have } } else if (icon < 0 || icon > 2) { return; // hax, using an invalid icon } if (!bEdit) { newBBSThread(c, title, text, icon, bNotice); } else { editBBSThread(c, title, text, icon, localthreadid); } break; case 1: // delete a thread localthreadid = slea.readInt(); deleteBBSThread(c, localthreadid); break; case 2: // list threads int start = slea.readInt(); listBBSThreads(c, start * 10); break; case 3: // list thread + reply, followed by id (int) localthreadid = slea.readInt(); displayThread(c, localthreadid); break; case 4: // reply if (!c.getPlayer().getCheatTracker().canBBS()) { c.getPlayer().dropMessage(1, "You may only start a new reply every 60 seconds."); return; } localthreadid = slea.readInt(); text = correctLength(slea.readMapleAsciiString(), 25); newBBSReply(c, localthreadid, text); break; case 5: // delete reply localthreadid = slea.readInt(); int replyid = slea.readInt(); deleteBBSReply(c, localthreadid, replyid); break; } } private static void listBBSThreads(MapleClient c, int start) { if (c.getPlayer().getGuildId() <= 0) { return; } c.getSession().write(GuildPacket.BBSThreadList(World.Guild.getBBS(c.getPlayer().getGuildId()), start)); } private static void newBBSReply(final MapleClient c, final int localthreadid, final String text) { if (c.getPlayer().getGuildId() <= 0) { return; } World.Guild.addBBSReply(c.getPlayer().getGuildId(), localthreadid, text, c.getPlayer().getId()); displayThread(c, localthreadid); } private static void editBBSThread(final MapleClient c, final String title, final String text, final int icon, final int localthreadid) { if (c.getPlayer().getGuildId() <= 0) { return; // expelled while viewing? } World.Guild.editBBSThread(c.getPlayer().getGuildId(), localthreadid, title, text, icon, c.getPlayer().getId(), c.getPlayer().getGuildRank()); displayThread(c, localthreadid); } private static void newBBSThread(final MapleClient c, final String title, final String text, final int icon, final boolean bNotice) { if (c.getPlayer().getGuildId() <= 0) { return; // expelled while viewing? } displayThread(c, World.Guild.addBBSThread(c.getPlayer().getGuildId(), title, text, icon, bNotice, c.getPlayer().getId())); listBBSThreads(c, 0); } private static void deleteBBSThread(final MapleClient c, final int localthreadid) { if (c.getPlayer().getGuildId() <= 0) { return; } World.Guild.deleteBBSThread(c.getPlayer().getGuildId(), localthreadid, c.getPlayer().getId(), (int) c.getPlayer().getGuildRank()); } private static void deleteBBSReply(final MapleClient c, final int localthreadid, final int replyid) { if (c.getPlayer().getGuildId() <= 0) { return; } World.Guild.deleteBBSReply(c.getPlayer().getGuildId(), localthreadid, replyid, c.getPlayer().getId(), (int) c.getPlayer().getGuildRank()); displayThread(c, localthreadid); } private static void displayThread(final MapleClient c, final int localthreadid) { if (c.getPlayer().getGuildId() <= 0) { return; } final List<MapleBBSThread> bbsList = World.Guild.getBBS(c.getPlayer().getGuildId()); if (bbsList != null) { for (MapleBBSThread t : bbsList) { if (t != null && t.localthreadID == localthreadid) { c.getSession().write(GuildPacket.showThread(t)); } } } } }