8cd1e6f5df31ba027780d1d4235a8f0617f2eb11
[NeonServV5.git] / src / cmd_neonserv_invite.c
1 /* cmd_neonserv_invite.c - NeonServ v5.1
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "cmd_neonserv.h"
19
20 /*
21 * argv[0] - nick / *auth
22 */
23 static USERAUTH_CALLBACK(neonserv_cmd_invite_nick_lookup);
24 static void neonserv_cmd_invite_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nick, char *auth);
25 static TIMEQ_CALLBACK(neonserv_cmd_invite_timeout_timeout);
26 static struct neonserv_cmd_invite_timeout* neonserv_cmd_invite_add_timeout(char *nick, char *chan);
27 static int neonserv_cmd_invite_is_timeout(char *nick, char *chan);
28 static void neonserv_cmd_invite_del_timeout(struct neonserv_cmd_invite_timeout *timeout);
29
30
31 struct neonserv_cmd_invite_cache {
32     struct ClientSocket *client, *textclient;
33     struct UserNode *user;
34     struct ChanNode *chan;
35     struct Event *event;
36     char *nick;
37 };
38
39 struct neonserv_cmd_invite_timeout {
40     char *nick;
41     char *chan;
42     
43     struct neonserv_cmd_invite_timeout *next;
44 };
45
46 static struct neonserv_cmd_invite_timeout *first_timeout = NULL, *last_timeout = NULL;
47
48 CMD_BIND(neonserv_cmd_invite) {
49     if(neonserv_cmd_invite_is_timeout(argv[0], chan->name)) {
50         reply(getTextBot(), user, "NS_INVITE_TIMEOUT", argv[0], chan->name);
51         return;
52     }
53     struct UserNode *cuser = getUserByNick(argv[0]);
54     if(!cuser) {
55         cuser = createTempUser(argv[0]);
56         cuser->flags |= USERFLAG_ISTMPUSER;
57     } else if(getChanUser(cuser, chan)) {
58         reply(getTextBot(), user, "NS_INVITE_ON_CHAN", cuser->nick, chan->name);
59         /* BUG
60          This check does not work if the user is invisible (CHMODE +D/+d)
61          to fix this we'd need to request the full userlist...
62          this is really senseless to invite a simple user so we simply mark this bug as unsolvable.
63         */
64         return;
65     }
66     if(cuser->flags & USERFLAG_ISAUTHED) {
67         neonserv_cmd_invite_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth);
68     } else {
69         struct neonserv_cmd_invite_cache *cache = malloc(sizeof(*cache));
70         if (!cache) {
71             perror("malloc() failed");
72             return;
73         }
74         cache->client = client;
75         cache->textclient = getTextBot();
76         cache->user = user;
77         cache->chan = chan;
78         cache->event = event;
79         cache->nick = strdup(argv[0]);
80         get_userauth(cuser, neonserv_cmd_invite_nick_lookup, cache);
81     }
82 }
83
84 static USERAUTH_CALLBACK(neonserv_cmd_invite_nick_lookup) {
85     struct neonserv_cmd_invite_cache *cache = data;
86     if(!user) {
87         //USER_DOES_NOT_EXIST
88         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
89     } else
90         neonserv_cmd_invite_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, ((user->flags & USERFLAG_ISAUTHED) ? user->auth : NULL));
91     free(cache->nick);
92     free(cache);
93 }
94
95 static void neonserv_cmd_invite_async1(struct ClientSocket *client, struct ClientSocket *textclient, struct UserNode *user, struct ChanNode *chan, struct Event *event, char *nick, char *auth) {
96     if(auth) {
97         MYSQL_RES *res;
98         MYSQL_ROW row;
99         printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
100         res = mysql_use();
101         if ((row = mysql_fetch_row(res)) != NULL) {
102             //check if the user has set noinvite
103             printf_mysql_query("SELECT `id` FROM `noinvite` WHERE `uid` = '%s' AND `cid` = '%d'", row[0], chan->channel_id);
104             res = mysql_use();
105             if ((row = mysql_fetch_row(res)) != NULL) {
106                 reply(textclient, user, "NS_INVITE_RESTRICTION", nick, chan->name);
107                 return;
108             }
109         }
110     }
111     struct neonserv_cmd_invite_timeout *timeout = neonserv_cmd_invite_add_timeout(nick, chan->name);
112     timeq_add(INVITE_TIMEOUT, neonserv_cmd_invite_timeout_timeout, timeout);
113     putsock(client, "INVITE %s %s", nick, chan->name);
114     struct UserNode *tmpu = getUserByNick(nick);
115     if(!tmpu) {
116         tmpu = createTempUser(nick);
117         tmpu->flags |= USERFLAG_ISTMPUSER | (auth ? USERFLAG_ISAUTHED : 0);
118         if(auth)
119             strcpy(tmpu->auth, auth);
120     }
121     reply(textclient, tmpu, "NS_INVITE_DONE_USER", chan->name, user->nick, client->user->nick);
122     reply(textclient, user, "NS_INVITE_DONE", nick, chan->name);
123 }
124
125 static TIMEQ_CALLBACK(neonserv_cmd_invite_timeout_timeout) {
126     struct neonserv_cmd_invite_timeout *entry = data;
127     neonserv_cmd_invite_del_timeout(entry);
128 }
129
130 static struct neonserv_cmd_invite_timeout* neonserv_cmd_invite_add_timeout(char *nick, char *chan) {
131     struct neonserv_cmd_invite_timeout *entry = malloc(sizeof(*entry));
132     if (!entry) {
133         perror("malloc() failed");
134         return NULL;
135     }
136     entry->next = NULL;
137     entry->nick = strdup(nick);
138     entry->chan = strdup(chan);
139     if(last_timeout) {
140         last_timeout->next = entry;
141         last_timeout = entry;
142     } else {
143         last_timeout = entry;
144         first_timeout = entry;
145     }
146     return entry;
147 }
148
149 static int neonserv_cmd_invite_is_timeout(char *nick, char *chan) {
150     if(!first_timeout) return 0;
151     struct neonserv_cmd_invite_timeout *entry;
152     for(entry = first_timeout; entry; entry = entry->next) {
153         if(!stricmp(entry->nick, nick) && !stricmp(entry->chan, chan))
154             return 1;
155     }
156     return 0;
157 }
158
159 static void neonserv_cmd_invite_del_timeout(struct neonserv_cmd_invite_timeout *timeout) {
160     struct neonserv_cmd_invite_timeout *entry, *prev = NULL;
161     for(entry = first_timeout; entry; entry = entry->next) {
162         if(entry == timeout) {
163             if(prev)
164                 prev->next = entry->next;
165             else
166                 first_timeout = entry->next;
167             break;
168         } else
169             prev = entry;
170     }
171     if(last_timeout == timeout)
172         last_timeout = prev;
173     free(timeout->nick);
174     free(timeout->chan);
175     free(timeout);
176 }