2 #include "cmd_neonserv.h"
5 * argv[0] - nick / *auth
7 static USERAUTH_CALLBACK(neonserv_cmd_invite_nick_lookup);
8 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);
9 static TIMEQ_CALLBACK(neonserv_cmd_invite_timeout_timeout);
10 static struct neonserv_cmd_invite_timeout* neonserv_cmd_invite_add_timeout(char *nick, char *chan);
11 static int neonserv_cmd_invite_is_timeout(char *nick, char *chan);
12 static void neonserv_cmd_invite_del_timeout(struct neonserv_cmd_invite_timeout *timeout);
15 struct neonserv_cmd_invite_cache {
16 struct ClientSocket *client, *textclient;
17 struct UserNode *user;
18 struct ChanNode *chan;
23 struct neonserv_cmd_invite_timeout {
27 struct neonserv_cmd_invite_timeout *next;
30 static struct neonserv_cmd_invite_timeout *first_timeout = NULL, *last_timeout = NULL;
32 CMD_BIND(neonserv_cmd_invite) {
33 if(neonserv_cmd_invite_is_timeout(argv[0], chan->name)) {
34 reply(getTextBot(), user, "NS_INVITE_TIMEOUT", argv[0], chan->name);
37 struct UserNode *cuser = getUserByNick(argv[0]);
39 cuser = createTempUser(argv[0]);
40 cuser->flags |= USERFLAG_ISTMPUSER;
41 } else if(getChanUser(cuser, chan)) {
42 reply(getTextBot(), user, "NS_INVITE_ON_CHAN", cuser->nick, chan->name);
44 This check does not work if the user is invisible (CHMODE +D/+d)
45 to fix this we'd need to request the full userlist...
46 this is really senseless to invite a simple user so we simply mark this bug as unsolvable.
50 if(cuser->flags & USERFLAG_ISAUTHED) {
51 neonserv_cmd_invite_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth);
53 struct neonserv_cmd_invite_cache *cache = malloc(sizeof(*cache));
55 perror("malloc() failed");
58 cache->client = client;
59 cache->textclient = getTextBot();
63 cache->nick = strdup(argv[0]);
64 get_userauth(cuser, neonserv_cmd_invite_nick_lookup, cache);
68 static USERAUTH_CALLBACK(neonserv_cmd_invite_nick_lookup) {
69 struct neonserv_cmd_invite_cache *cache = data;
72 reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
74 neonserv_cmd_invite_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, ((user->flags & USERFLAG_ISAUTHED) ? user->auth : NULL));
79 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) {
83 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
85 if ((row = mysql_fetch_row(res)) != NULL) {
86 //check if the user has set noinvite
87 printf_mysql_query("SELECT `id` FROM `noinvite` WHERE `uid` = '%s' AND `cid` = '%d'", row[0], chan->channel_id);
89 if ((row = mysql_fetch_row(res)) != NULL) {
90 reply(textclient, user, "NS_INVITE_RESTRICTION", nick, chan->name);
95 struct neonserv_cmd_invite_timeout *timeout = neonserv_cmd_invite_add_timeout(nick, chan->name);
96 timeq_add(INVITE_TIMEOUT, neonserv_cmd_invite_timeout_timeout, timeout);
97 putsock(client, "INVITE %s %s", nick, chan->name);
98 struct UserNode *tmpu = getUserByNick(nick);
100 tmpu = createTempUser(nick);
101 tmpu->flags |= USERFLAG_ISTMPUSER | (auth ? USERFLAG_ISAUTHED : 0);
103 strcpy(tmpu->auth, auth);
105 reply(textclient, tmpu, "NS_INVITE_DONE_USER", chan->name, user->nick, client->user->nick);
106 reply(textclient, user, "NS_INVITE_DONE", nick, chan->name);
109 static TIMEQ_CALLBACK(neonserv_cmd_invite_timeout_timeout) {
110 struct neonserv_cmd_invite_timeout *entry = data;
111 neonserv_cmd_invite_del_timeout(entry);
114 static struct neonserv_cmd_invite_timeout* neonserv_cmd_invite_add_timeout(char *nick, char *chan) {
115 struct neonserv_cmd_invite_timeout *entry = malloc(sizeof(*entry));
117 perror("malloc() failed");
121 entry->nick = strdup(nick);
122 entry->chan = strdup(chan);
124 last_timeout->next = entry;
125 last_timeout = entry;
127 last_timeout = entry;
128 first_timeout = entry;
133 static int neonserv_cmd_invite_is_timeout(char *nick, char *chan) {
134 if(!first_timeout) return 0;
135 struct neonserv_cmd_invite_timeout *entry;
136 for(entry = first_timeout; entry; entry = entry->next) {
137 if(!stricmp(entry->nick, nick) && !stricmp(entry->chan, chan))
143 static void neonserv_cmd_invite_del_timeout(struct neonserv_cmd_invite_timeout *timeout) {
144 struct neonserv_cmd_invite_timeout *entry, *prev = NULL;
145 for(entry = first_timeout; entry; entry = entry->next) {
146 if(entry == timeout) {
148 prev->next = entry->next;
150 first_timeout = entry->next;
155 if(last_timeout == timeout)