[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include <boost/thread/thread.hpp> |
---|
| 30 | |
---|
| 31 | #include "IRC.h" |
---|
| 32 | #include "ConsoleCommand.h" |
---|
| 33 | #include "TclThreadManager.h" |
---|
| 34 | #include "CoreIncludes.h" |
---|
| 35 | #include "util/Convert.h" |
---|
| 36 | |
---|
| 37 | #define IRC_TCL_THREADID 1421421421 |
---|
| 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
| 41 | SetConsoleCommand(IRC, say, true).setAccessLevel(AccessLevel::User); |
---|
| 42 | SetConsoleCommand(IRC, msg, false).setAccessLevel(AccessLevel::User); |
---|
| 43 | SetConsoleCommand(IRC, nick, false).setAccessLevel(AccessLevel::User); |
---|
| 44 | |
---|
| 45 | IRC* instance_irc = &IRC::getInstance(); |
---|
| 46 | |
---|
| 47 | IRC::IRC() |
---|
| 48 | { |
---|
| 49 | RegisterRootObject(IRC); |
---|
| 50 | this->bundle_ = 0; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void IRC::initialize() |
---|
| 54 | { |
---|
| 55 | unsigned int threadID = (unsigned int)IRC_TCL_THREADID; |
---|
| 56 | TclThreadManager::createID(threadID); |
---|
| 57 | this->bundle_ = TclThreadManager::getInstance().getInterpreterBundle(threadID); |
---|
| 58 | |
---|
| 59 | try |
---|
| 60 | { |
---|
| 61 | this->bundle_->interpreter_->def("orxonox::irc::say", IRC::tcl_say, Tcl::variadic()); |
---|
| 62 | this->bundle_->interpreter_->def("orxonox::irc::privmsg", IRC::tcl_privmsg, Tcl::variadic()); |
---|
| 63 | this->bundle_->interpreter_->def("orxonox::irc::action", IRC::tcl_action, Tcl::variadic()); |
---|
| 64 | this->bundle_->interpreter_->def("orxonox::irc::info", IRC::tcl_info, Tcl::variadic()); |
---|
| 65 | } |
---|
| 66 | catch (Tcl::tcl_error const &e) |
---|
| 67 | { COUT(1) << "Tcl (IRC) error: " << e.what(); } |
---|
| 68 | catch (std::exception const &e) |
---|
| 69 | { COUT(1) << "Error while initializing Tcl (IRC): " << e.what(); } |
---|
| 70 | |
---|
| 71 | this->nickname_ = "orx" + getConvertedValue<int, std::string>((unsigned int)rand()); |
---|
| 72 | TclThreadManager::execute(threadID, "set nickname " + this->nickname_); |
---|
| 73 | TclThreadManager::execute(threadID, "source irc.tcl"); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | IRC& IRC::getInstance() |
---|
| 77 | { |
---|
| 78 | static IRC instance; |
---|
| 79 | return instance; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | bool IRC::eval(const std::string& command) |
---|
| 83 | { |
---|
| 84 | if (!IRC::getInstance().bundle_) |
---|
| 85 | { |
---|
| 86 | IRC::getInstance().initialize(); |
---|
| 87 | COUT(1) << "Error: IRC client wasn't yet initialized, please try again." << std::endl; |
---|
| 88 | return false; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | try |
---|
| 92 | { |
---|
| 93 | IRC::getInstance().bundle_->interpreter_->eval(command); |
---|
| 94 | return true; |
---|
| 95 | } |
---|
| 96 | catch (Tcl::tcl_error const &e) |
---|
| 97 | { COUT(1) << "Tcl (IRC) error: " << e.what(); } |
---|
| 98 | catch (std::exception const &e) |
---|
| 99 | { COUT(1) << "Error while executing Tcl (IRC): " << e.what(); } |
---|
| 100 | |
---|
| 101 | return false; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void IRC::say(const std::string& message) |
---|
| 105 | { |
---|
| 106 | if (IRC::eval("irk::say $conn #orxonox {" + message + "}")) |
---|
| 107 | IRC::tcl_say(Tcl::object(), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message)); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void IRC::msg(const std::string& channel, const std::string& message) |
---|
| 111 | { |
---|
| 112 | if (IRC::eval("irk::say $conn " + channel + " {" + message + "}")) |
---|
| 113 | IRC::tcl_privmsg(Tcl::object(channel), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message)); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void IRC::nick(const std::string& nickname) |
---|
| 117 | { |
---|
| 118 | if (IRC::eval("irk::nick $conn " + nickname)) |
---|
| 119 | IRC::getInstance().nickname_ = nickname; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | void IRC::tcl_say(Tcl::object const &channel, Tcl::object const &nick, Tcl::object const &args) |
---|
| 123 | { |
---|
| 124 | COUT(0) << "IRC> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << std::endl; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | void IRC::tcl_privmsg(Tcl::object const &query, Tcl::object const &nick, Tcl::object const &args) |
---|
| 128 | { |
---|
| 129 | COUT(0) << "IRC (" << query.get() << ")> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << std::endl; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | void IRC::tcl_action(Tcl::object const &channel, Tcl::object const &nick, Tcl::object const &args) |
---|
| 133 | { |
---|
| 134 | COUT(0) << "IRC> * " << nick.get() << " " << stripEnclosingBraces(args.get()) << std::endl; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | void IRC::tcl_info(Tcl::object const &channel, Tcl::object const &args) |
---|
| 138 | { |
---|
| 139 | COUT(0) << "IRC> --> " << stripEnclosingBraces(args.get()) << std::endl; |
---|
| 140 | } |
---|
| 141 | } |
---|