summaryrefslogtreecommitdiff
path: root/plugins/keepnick.cpp
blob: e5a80499bdb43cf4972594080c80d49d6fa8cc7d (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
#include <iostream>
#include <string>

using namespace std;

#include "../TConnectionInterface.h"
#include "../TPluginParentLight.h"
#include "../TUserList.h"
#include "../mytypes.h"
#include "../myfuncs.h"
#include "../TConfigReadOnly.h"


#define WAITING_NS_ID 1
#define WAITING_NS_GHOST 2
#define NONE 0



extern "C" void init(int* csize, int* conndefault, int* chandefault, int* sessdefault)
{
	*csize=1; *conndefault=PFLAGS_EXEC_ONCREATE | PFLAGS_EXEC_ONEVENT; *chandefault=0; *sessdefault=0;
}

extern "C" void plugin (plugincontext* context, ircmessage msg, TPluginParentLight* parent, TConfigReadOnly& config, int reason)
{
	bool check=false;
	char *state=(char*) context->data;
	
	if (reason & PFLAGS_EXEC_ONCREATE)
		check=true;
	
	if (reason & PFLAGS_EXEC_ONEVENT)
	{
		if (config.get_valid_boolean(parent->get_parent()->get_networkname() + ".paranoidnick", false))
			if (ucase(msg.command)=="NICK")
				if (ucase(msg.content)==ucase(parent->get_parent()->get_nick())) //our nick got changed? i don't want that!  //content weil wir NACH dem updaten gerufen werden
					check=true;
		
		switch (*state)
		{
			case WAITING_NS_ID:
				if ((msg.command=="401") && (ucase(ntharg(msg.params,2))=="NICKSERV")) //no such nick/channel. no nickserv on that server?
				{
					cout << "no nickserv" << endl;
					parent->get_parent()->send("NICK "+config.get_string(parent->get_parent()->get_networkname() + ".nick"));
					*state=NONE;
				}
				else if ((msg.command=="NOTICE") && (ucase(msg.origin)=="NICKSERV")) //a notice from nickserv?
				{
					cout << "nickserv answered!" << endl;
					parent->get_parent()->send("PRIVMSG nickserv :ghost "+config.get_string(parent->get_parent()->get_networkname() + ".nick"));
					*state=WAITING_NS_GHOST;
				}
				break;
			case WAITING_NS_GHOST:
				if ((msg.command=="401") && (ucase(ntharg(msg.params,2))=="NICKSERV")) //no such nick/channel. no nickserv on that server?
				{
					cout << "no nickserv" << endl;
					parent->get_parent()->send("NICK "+config.get_string(parent->get_parent()->get_networkname() + ".nick"));
					*state=NONE;
				}
				else if ((msg.command=="NOTICE") && (ucase(msg.origin)=="NICKSERV")) //a notice from nickserv?
				{
					cout << "nickserv answered!" << endl;
					parent->get_parent()->send("NICK "+config.get_string(parent->get_parent()->get_networkname() + ".nick"));
					*state=NONE;
				}
				break;
				
		}
	}

	if (check)
	{
		if (ucase(parent->get_parent()->get_nick())!=ucase(config.get_string(parent->get_parent()->get_networkname() + ".nick")))
		{
			cout << "that's not the nick we want! trying to change it" << endl;
			
			parent->get_parent()->send("PRIVMSG nickserv :identify "+config.get_string(parent->get_parent()->get_networkname() + ".nick")+" "+config.get_string(parent->get_parent()->get_networkname() + ".pass"));
			*state=WAITING_NS_ID;
		}
	}
}