1 /* HandleInfoHandler.c - NeonServ v5.3
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "HandleInfoHandler.h"
19 #include "ClientSocket.h"
22 #include "IRCEvents.h"
25 #define AUTHSERV_NICK "AuthServ"
27 #define MAXCALLBACKS 3
29 struct HandleInfoQueueEntry {
31 void *callback[MAXCALLBACKS];
32 void *data[MAXCALLBACKS];
34 struct HandleInfoQueueEntry *next;
37 static struct HandleInfoQueueEntry* addHandleInfoQueueEntry(struct ClientSocket *client) {
38 struct HandleInfoQueueEntry *entry = malloc(sizeof(*entry));
41 perror("malloc() failed");
44 SYNCHRONIZE(cache_sync);
46 if(client->handleinfo_last)
47 client->handleinfo_last->next = entry;
49 client->handleinfo_first = entry;
50 client->handleinfo_last = entry;
51 DESYNCHRONIZE(cache_sync);
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;
60 client->handleinfo_first = entry->next;
61 if(entry == client->handleinfo_last) {
62 client->handleinfo_last = NULL;
65 DESYNCHRONIZE(cache_sync);
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) {
77 client->handleinfo_last = NULL;
78 client->handleinfo_first = NULL;
79 DESYNCHRONIZE(cache_sync);
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)) {
89 for(i = 1; i < MAXCALLBACKS; i++) {
90 if(!entry->callback[i]) {
91 entry->callback[i] = callback;
92 entry->data[i] = data;
98 if(bot->flags & SOCKET_FLAG_PREFERRED)
101 if(bot == NULL) return;
102 entry = addHandleInfoQueueEntry(bot);
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);
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;
122 int do_match = 0, exists = 0;
125 // Account * has not been registered.
126 // Account information for Skynet:
127 if(!match("Account * has not been registered.", message)) {
129 tmp = strstr(message, "\002");
131 tmp = strstr(auth, "\002");
134 if(!match("Account information for *", message)) {
137 tmp = strstr(message, "\002");
139 tmp = strstr(auth, "\002");
143 struct HandleInfoQueueEntry* entry = getNextHandleInfoQueueEntry(bot, 1);
145 authlookup_callback_t *callback;
147 for(i = 0; i < MAXCALLBACKS; i++) {
148 callback = entry->callback[i];
150 callback(auth, exists, entry->data[i]);
158 void init_handleinfohandler() {
159 bind_privnotice(recv_notice);
162 void free_handleinfohandler() {