summaryrefslogtreecommitdiff
path: root/src/cmd_whowas.cpp
blob: be3ee0add9eafb0f90ef7b164e50933276a93225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
 * See: http://www.inspircd.org/wiki/index.php/Credits
 *
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

#include "configreader.h"
#include "users.h"
#include "commands/cmd_whowas.h"

extern "C" command_t* init_command(InspIRCd* Instance)
{
	return new cmd_whowas(Instance);
}

CmdResult cmd_whowas::Handle (const char** parameters, int pcnt, userrec* user)
{
	/* if whowas disabled in config */
	if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
	{
		user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
		return CMD_FAILURE;
	}
                        
	irc::whowas::whowas_users::iterator i = ServerInstance->whowas.find(parameters[0]);

	ServerInstance->Log(DEBUG,"Entered cmd_whowas");

	if (i == ServerInstance->whowas.end())
	{
		ServerInstance->Log(DEBUG,"No such nick in whowas");
		user->WriteServ("406 %s %s :There was no such nickname",user->nick,parameters[0]);
		user->WriteServ("369 %s %s :End of WHOWAS",user->nick,parameters[0]);
		return CMD_FAILURE;
	}
	else
	{
		ServerInstance->Log(DEBUG,"Whowas set found");
		irc::whowas::whowas_set* grp = i->second;
		if (grp->size())
		{
			for (irc::whowas::whowas_set::iterator ux = grp->begin(); ux != grp->end(); ux++)
			{
				ServerInstance->Log(DEBUG,"Spool whowas entry");
				irc::whowas::WhoWasGroup* u = *ux;
				time_t rawtime = u->signon;
				tm *timeinfo;
				char b[MAXBUF];
	
				timeinfo = localtime(&rawtime);
				
				/* XXX - 'b' could be only 25 chars long and then strlcpy() would terminate it for us too? */
				strlcpy(b,asctime(timeinfo),MAXBUF);
				b[24] = 0;

				user->WriteServ("314 %s %s %s %s * :%s",user->nick,parameters[0],u->ident,u->dhost,u->gecos);
				
				if(*user->oper)
					user->WriteServ("379 %s %s :was connecting from *@%s", user->nick, parameters[0], u->host);
				
				if(*ServerInstance->Config->HideWhoisServer && !(*user->oper))
					user->WriteServ("312 %s %s %s :%s",user->nick,parameters[0], ServerInstance->Config->HideWhoisServer, b);
				else
					user->WriteServ("312 %s %s %s :%s",user->nick,parameters[0], u->server, b);
			}
		}
		else
		{
			ServerInstance->Log(DEBUG,"Oops, empty whowas set found");
			user->WriteServ("406 %s %s :There was no such nickname",user->nick,parameters[0]);
			user->WriteServ("369 %s %s :End of WHOWAS",user->nick,parameters[0]);
			return CMD_FAILURE;
		}
	}

	user->WriteServ("369 %s %s :End of WHOWAS",user->nick,parameters[0]);
	return CMD_SUCCESS;
}