Commit 2b697487 authored by Romain Bignon's avatar Romain Bignon

set server's hostname and peer hostname

parent 8743412c
......@@ -4,6 +4,11 @@ path {
users = /var/lib/bitlbee/users
}
irc {
#hostname = localhost.localdomain
}
# Log function
logging {
# What you want to log
......
......@@ -49,6 +49,9 @@ Bitlbee::Bitlbee()
section = conf.AddSection("path", "Path information", false);
section->AddItem(new ConfigItem_string("users", "Users directory"));
section = conf.AddSection("irc", "Server information", false);
section->AddItem(new ConfigItem_string("hostname", "Server hostname", " "));
section = conf.AddSection("logging", "Log informations", false);
section->AddItem(new ConfigItem_string("level", "Logging level"));
section->AddItem(new ConfigItem_bool("to_syslog", "Log error and warnings to syslog"));
......@@ -71,7 +74,8 @@ int Bitlbee::main(int argc, char** argv)
}
b_log.SetLoggedFlags(conf.GetSection("logging")->GetItem("level")->String(), conf.GetSection("logging")->GetItem("to_syslog")->Boolean());
irc = new IRC(0);
irc = new IRC(0, conf.GetSection("irc")->GetItem("hostname")->String());
b_log.setIRC(irc);
loop = g_main_new(FALSE);
g_main_run(loop);
......@@ -89,6 +93,10 @@ int Bitlbee::main(int argc, char** argv)
b_log[W_ERR] << "Error while loading:";
b_log[W_ERR] << e.Reason();
}
catch(IRCAuthError &e)
{
b_log[W_ERR] << "Unable to start the IRC daemon";
}
return EXIT_FAILURE;
}
......
......@@ -21,31 +21,57 @@
#include "../log.h"
#include "irc.h"
#include "message.h"
#include "nick.h"
IRC::IRC(int _fd)
IRC::IRC(int _fd, string _hostname)
: fd(_fd),
hostname("localhost.localdomain")
hostname("localhost.localdomain"),
userNick(NULL)
{
struct sockaddr_storage sock;
socklen_t socklen = sizeof(sock);
if(getsockname(fd, (struct sockaddr*) &sock, &socklen) == 0)
/* Get the user's hostname. */
string userhost = "localhost.localdomain";
if(getpeername(fd, (struct sockaddr*) &sock, &socklen) == 0)
{
char buf[NI_MAXHOST+1];
if( getnameinfo((struct sockaddr *) &sock, socklen, buf, NI_MAXHOST, NULL, 0, 0 ) == 0)
if(getnameinfo((struct sockaddr *)&sock, socklen, buf, NI_MAXHOST, NULL, 0, 0) == 0)
userhost = buf;
}
userNick = new Nick("AUTH", "auth", userhost);
/* Get my own hostname (if not given in arguments) */
if(_hostname.empty() || _hostname == " ")
{
if(getsockname(fd, (struct sockaddr*) &sock, &socklen) == 0)
{
hostname = buf;
char buf[NI_MAXHOST+1];
if( getnameinfo((struct sockaddr *) &sock, socklen, buf, NI_MAXHOST, NULL, 0, 0 ) == 0)
hostname = buf;
}
}
sendmsg(Message(MSG_NOTICE).setSender(this).setReceiver("AUTH").addArg("BitlBee-IRCd initialized, please go on"));
else if(_hostname.find(" ") != string::npos)
{
/* An hostname can't contain any space. */
b_log[W_ERR] << "'" << _hostname << "' is not a valid server hostname";
throw IRCAuthError();
}
else
hostname = _hostname;
sendmsg(Message(MSG_NOTICE).setSender(this).setReceiver(userNick).addArg("BitlBee-IRCd initialized, please go on"));
}
IRC::~IRC()
{
delete userNick;
}
bool IRC::sendmsg(Message msg)
bool IRC::sendmsg(Message msg) const
{
string s = msg.format();
write(fd, s.c_str(), s.size());
......
......@@ -19,22 +19,32 @@
#define IRC_H
#include <string>
#include <exception>
#include "message.h"
using std::string;
class IRCAuthError : public std::exception {};
class Nick;
class IRC
{
int fd;
string hostname;
Nick* userNick;
public:
IRC(int fd);
IRC(int fd, string hostname="");
~IRC();
bool sendmsg(Message msg);
bool sendmsg(Message msg) const;
string getServerName() const { return hostname; }
Nick* getUserNick() const { return userNick; }
};
#endif /* IRC_H */
......@@ -49,7 +49,7 @@ string Message::format() const
return buf;
}
Message& Message::setSender(Nick* nick)
Message& Message::setSender(const Nick* nick)
{
assert (nick != NULL);
......@@ -59,7 +59,7 @@ Message& Message::setSender(Nick* nick)
return *this;
}
Message& Message::setSender(IRC* me)
Message& Message::setSender(const IRC* me)
{
assert (me != NULL);
......@@ -67,7 +67,7 @@ Message& Message::setSender(IRC* me)
return *this;
}
Message& Message::setReceiver(Nick* nick)
Message& Message::setReceiver(const Nick* nick)
{
assert (nick != NULL);
......
......@@ -56,9 +56,9 @@ public:
Message(string command);
~Message();
Message& setSender(Nick* nick);
Message& setSender(IRC* me);
Message& setReceiver(Nick* nick);
Message& setSender(const Nick* nick);
Message& setSender(const IRC* me);
Message& setReceiver(const Nick* nick);
Message& setReceiver(string r);
Message& addArg(string);
......
......@@ -17,9 +17,13 @@
#include "nick.h"
Nick::Nick(std::string _nickname, std::string _ident, std::string _hostname)
Nick::Nick(string _nickname, string _ident, string _hostname)
: nickname(_nickname),
ident(_ident),
hostname(_hostname)
{
}
Nick::~Nick()
{
}
......@@ -19,19 +19,20 @@
#define NICK_H
#include <string>
using std::string;
class Nick
{
std::string nickname, ident, hostname;
string nickname, ident, hostname;
public:
Nick(std::string nick, std::string ident, std::string hostname);
Nick(string nick, string ident, string hostname);
~Nick();
std::string getNickname() const { return nickname; }
std::string getIdent() const { return ident; }
std::string getHostname() const { return hostname; }
string getNickname() const { return nickname; }
string getIdent() const { return ident; }
string getHostname() const { return hostname; }
};
#endif /* NICK_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment