--- /dev/null
+
+#include "cmd_neonserv.h"
+
+/*
+* argv[0] - nick / *auth
+*/
+static USERAUTH_CALLBACK(neonserv_cmd_invite_nick_lookup);
+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);
+static TIMEQ_CALLBACK(neonserv_cmd_invite_timeout_timeout);
+static struct neonserv_cmd_invite_timeout* neonserv_cmd_invite_add_timeout(char *nick, char *chan);
+static int neonserv_cmd_invite_is_timeout(char *nick, char *chan);
+static void neonserv_cmd_invite_del_timeout(struct neonserv_cmd_invite_timeout *timeout);
+
+
+struct neonserv_cmd_invite_cache {
+ struct ClientSocket *client, *textclient;
+ struct UserNode *user;
+ struct ChanNode *chan;
+ struct Event *event;
+ char *nick;
+};
+
+struct neonserv_cmd_invite_timeout {
+ char *nick;
+ char *chan;
+
+ struct neonserv_cmd_invite_timeout *next;
+};
+
+static struct neonserv_cmd_invite_timeout *first_timeout = NULL, *last_timeout = NULL;
+
+CMD_BIND(neonserv_cmd_invite) {
+ if(neonserv_cmd_invite_is_timeout(argv[0], chan->name)) {
+ reply(getTextBot(), user, "NS_INVITE_TIMEOUT", argv[0], chan->name);
+ return;
+ }
+ struct UserNode *cuser = getUserByNick(argv[0]);
+ if(!cuser) {
+ cuser = createTempUser(argv[0]);
+ cuser->flags |= USERFLAG_ISTMPUSER;
+ } else if(getChanUser(cuser, chan)) {
+ reply(getTextBot(), user, "NS_INVITE_ON_CHAN", cuser->nick, chan->name);
+ /* BUG
+ This check does not work if the user is invisible (CHMODE +D/+d)
+ to fix this we'd need to request the full userlist...
+ this is really senseless to invite a simple user so we simply mark this bug as unsolvable.
+ */
+ return;
+ }
+ if(cuser->flags & USERFLAG_ISAUTHED) {
+ neonserv_cmd_invite_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth);
+ } else {
+ struct neonserv_cmd_invite_cache *cache = malloc(sizeof(*cache));
+ if (!cache) {
+ perror("malloc() failed");
+ return;
+ }
+ cache->client = client;
+ cache->textclient = getTextBot();
+ cache->user = user;
+ cache->chan = chan;
+ cache->event = event;
+ cache->nick = strdup(argv[0]);
+ get_userauth(cuser, neonserv_cmd_invite_nick_lookup, cache);
+ }
+}
+
+static USERAUTH_CALLBACK(neonserv_cmd_invite_nick_lookup) {
+ struct neonserv_cmd_invite_cache *cache = data;
+ if(!user) {
+ //USER_DOES_NOT_EXIST
+ reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
+ } else
+ neonserv_cmd_invite_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, ((user->flags & USERFLAG_ISAUTHED) ? user->auth : NULL));
+ free(cache->nick);
+ free(cache);
+}
+
+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) {
+ if(auth) {
+ MYSQL_RES *res;
+ MYSQL_ROW row;
+ printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
+ res = mysql_use();
+ if ((row = mysql_fetch_row(res)) != NULL) {
+ //check if the user has set noinvite
+ printf_mysql_query("SELECT `id` FROM `noinvite` WHERE `uid` = '%s' AND `cid` = '%d'", row[0], chan->channel_id);
+ res = mysql_use();
+ if ((row = mysql_fetch_row(res)) != NULL) {
+ reply(textclient, user, "NS_INVITE_RESTRICTION", nick, chan->name);
+ return;
+ }
+ }
+ }
+ struct neonserv_cmd_invite_timeout *timeout = neonserv_cmd_invite_add_timeout(nick, chan->name);
+ timeq_add(INVITE_TIMEOUT, neonserv_cmd_invite_timeout_timeout, timeout);
+ putsock(client, "INVITE %s %s", nick, chan->name);
+ struct UserNode *tmpu = getUserByNick(nick);
+ if(!tmpu) {
+ tmpu = createTempUser(nick);
+ tmpu->flags |= USERFLAG_ISTMPUSER | (auth ? USERFLAG_ISAUTHED : 0);
+ if(auth)
+ strcpy(tmpu->auth, auth);
+ }
+ reply(textclient, tmpu, "NS_INVITE_DONE_USER", chan->name, user->nick, client->user->nick);
+ reply(textclient, user, "NS_INVITE_DONE", nick, chan->name);
+}
+
+static TIMEQ_CALLBACK(neonserv_cmd_invite_timeout_timeout) {
+ struct neonserv_cmd_invite_timeout *entry = data;
+ neonserv_cmd_invite_del_timeout(entry);
+}
+
+static struct neonserv_cmd_invite_timeout* neonserv_cmd_invite_add_timeout(char *nick, char *chan) {
+ struct neonserv_cmd_invite_timeout *entry = malloc(sizeof(*entry));
+ if (!entry) {
+ perror("malloc() failed");
+ return NULL;
+ }
+ entry->next = NULL;
+ entry->nick = strdup(nick);
+ entry->chan = strdup(chan);
+ if(last_timeout) {
+ last_timeout->next = entry;
+ last_timeout = entry;
+ } else {
+ last_timeout = entry;
+ first_timeout = entry;
+ }
+ return entry;
+}
+
+static int neonserv_cmd_invite_is_timeout(char *nick, char *chan) {
+ if(!first_timeout) return 0;
+ struct neonserv_cmd_invite_timeout *entry;
+ for(entry = first_timeout; entry; entry = entry->next) {
+ if(!stricmp(entry->nick, nick) && !stricmp(entry->chan, chan))
+ return 1;
+ }
+ return 0;
+}
+
+static void neonserv_cmd_invite_del_timeout(struct neonserv_cmd_invite_timeout *timeout) {
+ struct neonserv_cmd_invite_timeout *entry, *prev = NULL;
+ for(entry = first_timeout; entry; entry = entry->next) {
+ if(entry == timeout) {
+ if(prev)
+ prev->next = entry->next;
+ else
+ first_timeout = entry->next;
+ break;
+ } else
+ prev = entry;
+ }
+ if(last_timeout == timeout)
+ last_timeout = prev;
+ free(timeout->nick);
+ free(timeout->chan);
+ free(timeout);
+}