[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 | |
---|
[1792] | 29 | #include "IRC.h" |
---|
| 30 | |
---|
[3196] | 31 | #include <cpptcl/cpptcl.h> |
---|
| 32 | |
---|
| 33 | #include "util/Convert.h" |
---|
[5747] | 34 | #include "util/Exception.h" |
---|
[6417] | 35 | #include "util/StringUtils.h" |
---|
[1505] | 36 | #include "ConsoleCommand.h" |
---|
[3196] | 37 | #include "CoreIncludes.h" |
---|
[1505] | 38 | #include "TclThreadManager.h" |
---|
| 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
[1784] | 42 | static const unsigned int IRC_TCL_THREADID = 1421421421; |
---|
| 43 | |
---|
[1747] | 44 | SetConsoleCommand(IRC, say, true).accessLevel(AccessLevel::User); |
---|
| 45 | SetConsoleCommand(IRC, msg, false).accessLevel(AccessLevel::User); |
---|
| 46 | SetConsoleCommand(IRC, nick, false).accessLevel(AccessLevel::User); |
---|
[1505] | 47 | |
---|
| 48 | IRC::IRC() |
---|
| 49 | { |
---|
| 50 | RegisterRootObject(IRC); |
---|
[3304] | 51 | this->interpreter_ = 0; |
---|
[1505] | 52 | } |
---|
| 53 | |
---|
| 54 | void IRC::initialize() |
---|
| 55 | { |
---|
[3301] | 56 | unsigned int threadID = IRC_TCL_THREADID; |
---|
[3318] | 57 | this->interpreter_ = TclThreadManager::createWithId(threadID); |
---|
[1505] | 58 | |
---|
| 59 | try |
---|
| 60 | { |
---|
[3370] | 61 | this->interpreter_->def("::orxonox::irc::say", IRC::tcl_say, Tcl::variadic()); |
---|
| 62 | this->interpreter_->def("::orxonox::irc::privmsg", IRC::tcl_privmsg, Tcl::variadic()); |
---|
| 63 | this->interpreter_->def("::orxonox::irc::action", IRC::tcl_action, Tcl::variadic()); |
---|
| 64 | this->interpreter_->def("::orxonox::irc::info", IRC::tcl_info, Tcl::variadic()); |
---|
[1505] | 65 | } |
---|
| 66 | catch (Tcl::tcl_error const &e) |
---|
| 67 | { COUT(1) << "Tcl (IRC) error: " << e.what(); } |
---|
| 68 | |
---|
[3280] | 69 | this->nickname_ = "orx" + multi_cast<std::string>(static_cast<unsigned int>(rand())); |
---|
[1505] | 70 | TclThreadManager::execute(threadID, "set nickname " + this->nickname_); |
---|
| 71 | TclThreadManager::execute(threadID, "source irc.tcl"); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | IRC& IRC::getInstance() |
---|
| 75 | { |
---|
| 76 | static IRC instance; |
---|
| 77 | return instance; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | bool IRC::eval(const std::string& command) |
---|
| 81 | { |
---|
[3304] | 82 | if (!IRC::getInstance().interpreter_) |
---|
[1505] | 83 | { |
---|
| 84 | IRC::getInstance().initialize(); |
---|
| 85 | COUT(1) << "Error: IRC client wasn't yet initialized, please try again." << std::endl; |
---|
| 86 | return false; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | try |
---|
| 90 | { |
---|
[3304] | 91 | IRC::getInstance().interpreter_->eval(command); |
---|
[1505] | 92 | return true; |
---|
| 93 | } |
---|
| 94 | catch (Tcl::tcl_error const &e) |
---|
| 95 | { COUT(1) << "Tcl (IRC) error: " << e.what(); } |
---|
| 96 | |
---|
| 97 | return false; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void IRC::say(const std::string& message) |
---|
| 101 | { |
---|
[6417] | 102 | if (IRC::eval("irk::say $conn #orxonox {" + message + '}')) |
---|
[1505] | 103 | IRC::tcl_say(Tcl::object(), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message)); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void IRC::msg(const std::string& channel, const std::string& message) |
---|
| 107 | { |
---|
[6417] | 108 | if (IRC::eval("irk::say $conn " + channel + " {" + message + '}')) |
---|
[1505] | 109 | IRC::tcl_privmsg(Tcl::object(channel), Tcl::object(IRC::getInstance().nickname_), Tcl::object(message)); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void IRC::nick(const std::string& nickname) |
---|
| 113 | { |
---|
| 114 | if (IRC::eval("irk::nick $conn " + nickname)) |
---|
| 115 | IRC::getInstance().nickname_ = nickname; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | void IRC::tcl_say(Tcl::object const &channel, Tcl::object const &nick, Tcl::object const &args) |
---|
| 119 | { |
---|
| 120 | COUT(0) << "IRC> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << std::endl; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | void IRC::tcl_privmsg(Tcl::object const &query, Tcl::object const &nick, Tcl::object const &args) |
---|
| 124 | { |
---|
| 125 | COUT(0) << "IRC (" << query.get() << ")> " << nick.get() << ": " << stripEnclosingBraces(args.get()) << std::endl; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | void IRC::tcl_action(Tcl::object const &channel, Tcl::object const &nick, Tcl::object const &args) |
---|
| 129 | { |
---|
[6417] | 130 | COUT(0) << "IRC> * " << nick.get() << ' ' << stripEnclosingBraces(args.get()) << std::endl; |
---|
[1505] | 131 | } |
---|
| 132 | |
---|
| 133 | void IRC::tcl_info(Tcl::object const &channel, Tcl::object const &args) |
---|
| 134 | { |
---|
| 135 | COUT(0) << "IRC> --> " << stripEnclosingBraces(args.get()) << std::endl; |
---|
| 136 | } |
---|
| 137 | } |
---|