summaryrefslogtreecommitdiff
path: root/plugins/rejoin.cpp.stabil
blob: 3098668fc8b10623f24b3ae2dfcd782ff9d3cecb (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* TODO
 * 470 forwarding to another channel implementieren
 * evtl timeout implementieren
 * aus config folgendes lesen:
 *   rejoinen?
 *   wie lange warten?
 * 
 */

#include <iostream>
#include <string>
#include <list>

using namespace std;

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

struct entry_t
{
	char state;
	int wait;
	time_t last;
	string name;
};

extern "C" void init(int* csize, int* conndefault, int* chandefault, int* sessdefault)
{
	*csize=sizeof(list<entry_t>*);
	*conndefault=PFLAGS_EXEC_ONEVENT; *chandefault=0; *sessdefault=0;
}

extern "C" void plugin (plugincontext* context, ircmessage msg, TPluginParentLight* parent, TConfigReadOnly& config, int reason)
{
	list<entry_t> *liste=*static_cast<list<entry_t> **>(context->data);
	if (liste==0)
	{
		liste=new list<entry_t>;
		*static_cast<list<entry_t> **>(context->data)=liste;
	}
	
	if (reason&PFLAGS_EXEC_ONEVENT)
	{
		if ((msg.command=="KICK") && (lcase(ntharg(msg.params,2))==lcase(parent->get_parent()->get_nick()))) //wir wurden gekickt? sauerei!
		{
			cout << "we got kicked from " << ntharg(msg.params,1) << endl;
			if (config.get_valid_boolean(parent->get_parent()->get_networkname() + "." + ntharg(msg.params,1) + ".rejoin", false))
			{
				cout << "  trying to rejoin" << endl;
					
				entry_t tmp;
				tmp.state=1;
				tmp.last=time(NULL)+1; //fake time to wait 1 second ONCE
				tmp.wait=0;
				tmp.name=ntharg(msg.params,1);
				liste->push_back(tmp);

				context->flags |= PFLAGS_EXEC_ALWAYS;
			}
		}
		//evtl aus datei lesen?
//		parent->get_parent()->send ("join #DrunkenMan" NEWLINE);
		
//		if (config.is_string(parent->get_parent()->get_networkname()+".pass"))
//			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"));

	}
	
	if (reason&PFLAGS_EXEC_ALWAYS)
	{
		for (list<entry_t>::iterator it=liste->begin(); it!=liste->end(); it++)
		{
			switch (it->state)
			{
				case 1:
					if (time(NULL)>(it->last+it->wait))
					{
						cout << "DEBUG: waiting is over" << endl;
						parent->get_parent()->send("join "+it->name);
						it->state=2;
					}
					break;
				case 2: //waiting for JOIN answer			
					if ((ucase(msg.command)=="JOIN") && (ucase(msg.origin)==ucase(parent->get_parent()->get_nick())))
					{
						it->state=99;
						cout << "!!!!!!!SUCCESS" << endl;
					}
					else
					{
						if ((lcase(ntharg(msg.params,1))==lcase(parent->get_parent()->get_nick())) && (lcase(ntharg(msg.params, 2))==lcase(it->name)))
						{
							cout << "!!!!!!!!betrifft uns" << endl;
							int numcmd=atoi(msg.command.c_str());
							switch (numcmd)
							{
								case 471: //channel is full
									it->last=time(NULL);
									it->state=1;
									it->wait++;
									break;
								case 473: //inviteonly
									parent->get_parent()->send("PRIVMSG ChanServ :invite "+it->name);
									it->state=3;
									break;
								case 474: //banned
									parent->get_parent()->send("PRIVMSG ChanServ :unban "+it->name);
									it->state=3;
									break;
								case 475: //bad key, no such chan, too many chans. fatal.
								case 403:
								case 405:
									it->state=99;
									cout << "!!!!!!!!!FATAL" << endl;
									break;
							}
						}
					}
					break;
				
				case 3: //waiting for CHANSERV answer
					if ( ((msg.command=="401") && (ucase(ntharg(msg.params,2))=="CHANSERV")) || ((msg.command=="NOTICE") && (ucase(msg.origin)=="CHANSERV")) )
					{
						//answer arrived or no chanserv?
						it->last=time(NULL);
						it->state=1;
					}

					break;
			}
		}
		
		bool temp=true;
		while (temp)
		{
			temp=false;
			for (list<entry_t>::iterator it=liste->begin(); it!=liste->end(); it++)
			{
				if (it->state==99)
				{
					cout << "cleanup: removing entry for channel " << it->name << "..." << endl;
					liste->erase(it);
					temp=true;
					break;
				}
			}
		}
		
	}
}