added experimental multi thread support
[NeonServV5.git] / src / HandleInfoHandler.c
1 /* HandleInfoHandler.c - NeonServ v5.3
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17
18 #include "HandleInfoHandler.h"
19 #include "ClientSocket.h"
20 #include "UserNode.h"
21 #include "ChanNode.h"
22 #include "IRCEvents.h"
23 #include "tools.h"
24
25 #define AUTHSERV_NICK "AuthServ"
26
27 #define MAXCALLBACKS 3
28
29 struct HandleInfoQueueEntry {
30     char *auth;
31     void *callback[MAXCALLBACKS];
32     void *data[MAXCALLBACKS];
33     
34     struct HandleInfoQueueEntry *next;
35 };
36
37 static struct HandleInfoQueueEntry* addHandleInfoQueueEntry(struct ClientSocket *client) {
38     struct HandleInfoQueueEntry *entry = malloc(sizeof(*entry));
39     if (!entry)
40     {
41         perror("malloc() failed");
42         return NULL;
43     }
44     SYNCHRONIZE(cache_sync);
45     entry->next = NULL;
46     if(client->handleinfo_last)
47         client->handleinfo_last->next = entry;
48     else
49         client->handleinfo_first = entry;
50     client->handleinfo_last = entry;
51     DESYNCHRONIZE(cache_sync);
52     return entry;
53 }
54
55 static struct HandleInfoQueueEntry* getNextHandleInfoQueueEntry(struct ClientSocket *client, int freeEntry) {
56     if(!client->handleinfo_first) return NULL;
57     SYNCHRONIZE(cache_sync);
58     struct HandleInfoQueueEntry *entry = client->handleinfo_first;
59     if(freeEntry) {
60         client->handleinfo_first = entry->next;
61         if(entry == client->handleinfo_last) {
62             client->handleinfo_last = NULL;
63         }
64     }
65     DESYNCHRONIZE(cache_sync);
66     return entry;
67 }
68
69 void clear_handleinfoqueue(struct ClientSocket *client) {
70     if(!client->handleinfo_first) return;
71     SYNCHRONIZE(cache_sync);
72     struct HandleInfoQueueEntry *entry, *next;
73     for(entry = client->handleinfo_first; entry; entry = next) {
74         next = entry->next;
75         free(entry);
76     }
77     client->handleinfo_last = NULL;
78     client->handleinfo_first = NULL;
79     DESYNCHRONIZE(cache_sync);
80 }
81
82 void lookup_authname(char *auth, authlookup_callback_t callback, void *data) {
83     struct ClientSocket *bot;
84     struct HandleInfoQueueEntry* entry;
85     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
86         for(entry = bot->handleinfo_first; entry; entry = entry->next) {
87             if(!stricmp(entry->auth, auth)) {
88                 int i = 0;
89                 for(i = 1; i < MAXCALLBACKS; i++) {
90                     if(!entry->callback[i]) {
91                         entry->callback[i] = callback;
92                         entry->data[i] = data;
93                         return;
94                     }
95                 }
96             }
97         }
98         if(bot->flags & SOCKET_FLAG_PREFERRED)
99             break;
100     }
101     if(bot == NULL) return;
102     entry = addHandleInfoQueueEntry(bot);
103     int i;
104     entry->auth = strdup(auth);
105     entry->callback[0] = callback;
106     for(i = 1; i < MAXCALLBACKS; i++)
107         entry->callback[i] = NULL;
108     entry->data[0] = data;
109     for(i = 1; i < MAXCALLBACKS; i++)
110         entry->data[i] = NULL;
111     putsock(bot, "PRIVMSG " AUTHSERV_NICK " :ACCOUNTINFO *%s", auth);
112 }
113
114 static void recv_notice(struct UserNode *user, struct UserNode *target, char *message) {
115     if(stricmp(user->nick, AUTHSERV_NICK)) return;
116     struct ClientSocket *bot;
117     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
118         if(bot->user == target) break;
119     }
120     if(!bot) return;
121     char *auth = NULL;
122     int do_match = 0, exists = 0;
123     char *tmp;
124     //messages to parse:
125     //  Account * has not been registered.
126     //  Account information for Skynet:
127     if(!match("Account * has not been registered.", message)) {
128         do_match = 1;
129         tmp = strstr(message, "\002");
130         auth = tmp+1;
131         tmp = strstr(auth, "\002");
132         *tmp = '\0';
133     }
134     if(!match("Account information for *", message)) {
135         do_match = 1;
136         exists = 1;
137         tmp = strstr(message, "\002");
138         auth = tmp+1;
139         tmp = strstr(auth, "\002");
140         *tmp = '\0';
141     }
142     if(do_match) {
143         struct HandleInfoQueueEntry* entry = getNextHandleInfoQueueEntry(bot, 1);
144         if(entry) {
145             authlookup_callback_t *callback;
146             int i;
147             for(i = 0; i < MAXCALLBACKS; i++) {
148                 callback = entry->callback[i];
149                 if(!callback) break;
150                 callback(auth, exists, entry->data[i]);
151             }
152             free(entry->auth);
153             free(entry);
154         }
155     }
156 }
157
158 void init_handleinfohandler() {
159     bind_privnotice(recv_notice);
160 }
161
162 void free_handleinfohandler() {
163     
164 }