changed Makefile; build all commands as an own file
[NeonServV5.git] / cmd_neonserv_invite.c
1
2 #include "cmd_neonserv.h"
3
4 /*
5 * argv[0] - nick / *auth
6 */
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);
13
14
15 struct neonserv_cmd_invite_cache {
16     struct ClientSocket *client, *textclient;
17     struct UserNode *user;
18     struct ChanNode *chan;
19     struct Event *event;
20     char *nick;
21 };
22
23 struct neonserv_cmd_invite_timeout {
24     char *nick;
25     char *chan;
26     
27     struct neonserv_cmd_invite_timeout *next;
28 };
29
30 static struct neonserv_cmd_invite_timeout *first_timeout = NULL, *last_timeout = NULL;
31
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);
35         return;
36     }
37     struct UserNode *cuser = getUserByNick(argv[0]);
38     if(!cuser) {
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);
43         /* BUG
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.
47         */
48         return;
49     }
50     if(cuser->flags & USERFLAG_ISAUTHED) {
51         neonserv_cmd_invite_async1(client, getTextBot(), user, chan, event, argv[0], cuser->auth);
52     } else {
53         struct neonserv_cmd_invite_cache *cache = malloc(sizeof(*cache));
54         if (!cache) {
55             perror("malloc() failed");
56             return;
57         }
58         cache->client = client;
59         cache->textclient = getTextBot();
60         cache->user = user;
61         cache->chan = chan;
62         cache->event = event;
63         cache->nick = strdup(argv[0]);
64         get_userauth(cuser, neonserv_cmd_invite_nick_lookup, cache);
65     }
66 }
67
68 static USERAUTH_CALLBACK(neonserv_cmd_invite_nick_lookup) {
69     struct neonserv_cmd_invite_cache *cache = data;
70     if(!user) {
71         //USER_DOES_NOT_EXIST
72         reply(cache->textclient, cache->user, "NS_USER_UNKNOWN", cache->nick);
73     } else
74         neonserv_cmd_invite_async1(cache->client, cache->textclient, cache->user, cache->chan, cache->event, user->nick, ((user->flags & USERFLAG_ISAUTHED) ? user->auth : NULL));
75     free(cache->nick);
76     free(cache);
77 }
78
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) {
80     if(auth) {
81         MYSQL_RES *res;
82         MYSQL_ROW row;
83         printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
84         res = mysql_use();
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);
88             res = mysql_use();
89             if ((row = mysql_fetch_row(res)) != NULL) {
90                 reply(textclient, user, "NS_INVITE_RESTRICTION", nick, chan->name);
91                 return;
92             }
93         }
94     }
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);
99     if(!tmpu) {
100         tmpu = createTempUser(nick);
101         tmpu->flags |= USERFLAG_ISTMPUSER | (auth ? USERFLAG_ISAUTHED : 0);
102         if(auth)
103             strcpy(tmpu->auth, auth);
104     }
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);
107 }
108
109 static TIMEQ_CALLBACK(neonserv_cmd_invite_timeout_timeout) {
110     struct neonserv_cmd_invite_timeout *entry = data;
111     neonserv_cmd_invite_del_timeout(entry);
112 }
113
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));
116     if (!entry) {
117         perror("malloc() failed");
118         return NULL;
119     }
120     entry->next = NULL;
121     entry->nick = strdup(nick);
122     entry->chan = strdup(chan);
123     if(last_timeout) {
124         last_timeout->next = entry;
125         last_timeout = entry;
126     } else {
127         last_timeout = entry;
128         first_timeout = entry;
129     }
130     return entry;
131 }
132
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))
138             return 1;
139     }
140     return 0;
141 }
142
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) {
147             if(prev)
148                 prev->next = entry->next;
149             else
150                 first_timeout = entry->next;
151             break;
152         } else
153             prev = entry;
154     }
155     if(last_timeout == timeout)
156         last_timeout = prev;
157     free(timeout->nick);
158     free(timeout->chan);
159     free(timeout);
160 }