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