]> jfr.im git - irc/evilnet/mod.auth.git/blob - auth.cc
Various fixes for mod.auth...
[irc/evilnet/mod.auth.git] / auth.cc
1 /**
2 * auth.cc
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 *
19 * $Id$
20 */
21
22 #include <string>
23
24 #include "client.h"
25 #include "auth.h"
26 #include "server.h"
27 #include "EConfig.h"
28 #include "StringTokenizer.h"
29
30 namespace gnuworld
31 {
32
33 /*
34 * Exported function used by moduleLoader to gain an
35 * instance of this module.
36 */
37
38 extern "C"
39 {
40 xClient* _gnuwinit(const std::string& args)
41 {
42 return new auth( args );
43 }
44
45 }
46
47 /**
48 * This constructor calls the base class constructor. The xClient
49 * constructor will open the configuration file given and retrieve
50 * basic client info (nick/user/host/etc).
51 * Any additional processing must be done here.
52 */
53 auth::auth( const std::string& configFileName )
54 : xClient( configFileName )
55 {
56 /* Load the config file */
57 EConfig authConfig(configFileName);
58
59 /* Load any settings we have */
60 maxAccountLen = ::atoi(authConfig.Require("maxAccountLen")->second.c_str());
61 operOnly = ::atoi(authConfig.Require("operOnly")->second.c_str());
62 secureOnly = ::atoi(authConfig.Require("secureOnly")->second.c_str());
63 }
64
65 auth::~auth()
66 {
67 /* No heap space allocated */
68 }
69
70 void auth::OnPrivateMessage( iClient* theClient,
71 const std::string& Message, bool secure)
72 {
73 if (operOnly && !theClient->isOper()) {
74 // if the client is not oper'd we act as the clientExample ;)
75 Notice(theClient, "You must be an IRC Operator to use this service.");
76 return;
77 }
78
79 if (secureOnly && !secure) {
80 Notice(theClient, "You must /msg %s@%s", nickName.c_str(),
81 getUplinkName().c_str());
82 return;
83 }
84
85 StringTokenizer st(Message);
86 if (st.empty())
87 return;
88
89 std::string account(st[0]);
90 if (!validAccount(account)) {
91 Notice(theClient, "%s is not a valid account name.", account.c_str());
92 return;
93 }
94 iClient* receiver = NULL;
95 if (st.size() > 1) {
96 if (!(receiver = Network->findNick(st[1]))) {
97 Notice(theClient, "Unable to find nickname %s", st[1].c_str());
98 return;
99 }
100 }
101
102 if (!theClient->isModeR()) {
103 if (receiver) {
104 MyUplink->UserLogin(receiver, account, this);
105 Notice(theClient, "%s is now authenticated as %s.",
106 receiver->getNickName().c_str(), account.c_str());
107 Notice(receiver, "You have been authenticated as %s by %s.",
108 account.c_str(), theClient->getNickName().c_str());
109 } else {
110 MyUplink->UserLogin(theClient, account, this);
111 Notice(theClient, "You are now authenticated as %s.",
112 account.c_str());
113 }
114 } else {
115 Notice(theClient, "You are already authenticated as %s!",
116 theClient->getAccount().c_str());
117 }
118
119 }
120
121 // Burst any channels.
122 void auth::BurstChannels()
123 {
124 xClient::BurstChannels();
125 }
126
127 bool auth::validAccount(const std::string& account) const
128 {
129 if (account.empty() || account.size() > maxAccountLen )
130 return false;
131
132 /*
133 * From IRCu:
134 * Nickname characters are in range 'A'..'}', '_', '-', '0'..'9'
135 * anything outside the above set will terminate nickname.
136 * In addition, the first character cannot be '-' or a Digit.
137 */
138 if (isdigit(account[0]))
139 return false;
140
141 for ( std::string::const_iterator sItr = account.begin();
142 sItr != account.end() ; ++sItr ) {
143 if ( *sItr >= 'A' && *sItr <= '}' )
144 // ok
145 continue;
146 if ( '_' == *sItr || '-' == *sItr )
147 // ok
148 continue;
149 if ( *sItr >= '0' && *sItr <= '9' )
150 // ok
151 continue;
152 // bad char
153 return false;
154 }
155 return true;
156 }
157
158 } // namespace gnuworld