1 /* cmd_neonserv_invite.c - NeonServ v5.5
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "cmd_neonserv.h"
21 * argv[0] - nick / *auth
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);
31 struct neonserv_cmd_invite_cache {
32 struct ClientSocket *client, *textclient;
33 struct UserNode *user;
34 struct ChanNode *chan;
39 struct neonserv_cmd_invite_timeout {
43 struct neonserv_cmd_invite_timeout *next;
46 static struct neonserv_cmd_invite_timeout *first_timeout = NULL, *last_timeout = NULL;
48 CMD_BIND(neonserv_cmd_invite) {
49 if(neonserv_cmd_invite_is_timeout(argv[0], chan->name)) {
50 reply(textclient, user, "NS_INVITE_TIMEOUT", argv[0], chan->name);
53 struct UserNode *cuser = getUserByNick(argv[0]);
55 cuser = createTempUser(argv[0]);
57 reply(textclient, user, "NS_USER_UNKNOWN", argv[0]);
60 cuser->flags |= USERFLAG_ISTMPUSER;
61 } else if(getChanUser(cuser, chan)) {
62 reply(textclient, user, "NS_INVITE_ON_CHAN", cuser->nick, chan->name);
64 This check does not work if the user is invisible (CHMODE +D/+d)
65 to fix this we'd need to request the full userlist...
66 this is really senseless to invite a simple user so we simply mark this bug as unsolvable.
70 if(cuser->flags & USERFLAG_ISAUTHED) {
71 neonserv_cmd_invite_async1(client, textclient, user, chan, event, argv[0], cuser->auth);
73 struct neonserv_cmd_invite_cache *cache = malloc(sizeof(*cache));
75 perror("malloc() failed");
78 cache->client = client;
79 cache->textclient = textclient;
83 cache->nick = strdup(argv[0]);
84 get_userauth(cuser, module_id, neonserv_cmd_invite_nick_lookup, cache);
88 static USERAUTH_CALLBACK(neonserv_cmd_invite_nick_lookup) {
89 struct neonserv_cmd_invite_cache *cache = data;
92 reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
94 neonserv_cmd_invite_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, ((user->flags & USERFLAG_ISAUTHED) ? user->auth : NULL));
99 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) {
100 if(auth && (!(user->flags & USERFLAG_ISAUTHED) || stricmp(auth, user->auth))) {
103 printf_mysql_query("SELECT `user_id`, `user_block_invites` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
105 if ((row = mysql_fetch_row(res)) != NULL) {
106 //check if the user has blocked invites globally
107 if(!strcmp(row[1], "1")) {
108 reply(textclient, user, "NS_INVITE_GLOBALLY_BLOCKED", nick);
111 //check if the user has set noinvite
112 printf_mysql_query("SELECT `id` FROM `noinvite` WHERE `uid` = '%s' AND `cid` = '%d'", row[0], chan->channel_id);
114 if ((row = mysql_fetch_row(res)) != NULL) {
115 reply(textclient, user, "NS_INVITE_RESTRICTION", nick, chan->name);
120 struct neonserv_cmd_invite_timeout *timeout = neonserv_cmd_invite_add_timeout(nick, chan->name);
121 timeq_add(INVITE_TIMEOUT, module_id, neonserv_cmd_invite_timeout_timeout, timeout);
122 putsock(client, "INVITE %s %s", nick, chan->name);
123 struct UserNode *tmpu = getUserByNick(nick);
125 tmpu = createTempUser(nick);
126 tmpu->flags |= USERFLAG_ISTMPUSER | (auth ? USERFLAG_ISAUTHED : 0);
128 strcpy(tmpu->auth, auth);
130 reply(textclient, tmpu, "NS_INVITE_DONE_USER", chan->name, user->nick, client->user->nick);
131 reply(textclient, user, "NS_INVITE_DONE", nick, chan->name);
134 static TIMEQ_CALLBACK(neonserv_cmd_invite_timeout_timeout) {
135 struct neonserv_cmd_invite_timeout *entry = data;
136 neonserv_cmd_invite_del_timeout(entry);
139 static struct neonserv_cmd_invite_timeout* neonserv_cmd_invite_add_timeout(char *nick, char *chan) {
140 struct neonserv_cmd_invite_timeout *entry = malloc(sizeof(*entry));
142 perror("malloc() failed");
146 entry->nick = strdup(nick);
147 entry->chan = strdup(chan);
149 last_timeout->next = entry;
150 last_timeout = entry;
152 last_timeout = entry;
153 first_timeout = entry;
158 static int neonserv_cmd_invite_is_timeout(char *nick, char *chan) {
159 if(!first_timeout) return 0;
160 struct neonserv_cmd_invite_timeout *entry;
161 for(entry = first_timeout; entry; entry = entry->next) {
162 if(!stricmp(entry->nick, nick) && !stricmp(entry->chan, chan))
168 static void neonserv_cmd_invite_del_timeout(struct neonserv_cmd_invite_timeout *timeout) {
169 struct neonserv_cmd_invite_timeout *entry, *prev = NULL;
170 for(entry = first_timeout; entry; entry = entry->next) {
171 if(entry == timeout) {
173 prev->next = entry->next;
175 first_timeout = entry->next;
180 if(last_timeout == timeout)