added HandleInfoHandler.c with lookup_authname()
[NeonServV5.git] / HandleInfoHandler.c
1
2 #include "HandleInfoHandler.h"
3 #include "ClientSocket.h"
4 #include "UserNode.h"
5 #include "IRCEvents.h"
6 #include "tools.h"
7
8 #define AUTHSERV_NICK "AuthServ"
9
10 struct HandleInfoQueueEntry {
11     void *callback;
12     void *data;
13 };
14
15 static struct HandleInfoQueueEntry *first_entry = NULL, *last_entry = NULL;
16
17 static struct HandleInfoQueueEntry* addHandleInfoQueueEntry(struct ClientSocket *client) {
18     struct HandleInfoQueueEntry *entry = malloc(sizeof(*entry));
19     if (!entry)
20     {
21         perror("malloc() failed");
22         return NULL;
23     }
24     entry->next = NULL;
25     entry->client = client;
26     if(last_entry)
27         last_entry->next = entry;
28     else
29         last_entry = entry;
30     if(!first_entry)
31         first_entry = entry;
32     return entry;
33 }
34
35 static struct HandleInfoQueueEntry* getNextHandleInfoQueueEntry(struct ClientSocket *client, int freeEntry) {
36     if(!first_entry) return NULL;
37     struct HandleInfoQueueEntry *entry;
38     for(entry = first_entry; entry; entry = entry->next) {
39         if(entry->client == client)
40             break;
41     }
42     if(entry == NULL) return NULL;
43     if(freeEntry) {
44         if(entry == first_entry)
45             first_entry = entry->next;
46         if(entry == last_entry) {
47             struct HandleInfoQueueEntry *last;
48             for(last = first_entry; last; last = last->next)
49                 if(last->next == NULL) break;
50             last_entry = last;
51         }
52     }
53     return entry;
54 }
55
56 void lookup_authname(char *auth, authlookup_callback_t callback, void *data) {
57     struct ClientSocket *bot;
58     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
59         if(bot->flags & SOCKET_FLAG_PREFERRED)
60             break;
61     }
62     if(bot == NULL) return;
63     struct HandleInfoQueueEntry* entry = addHandleInfoQueueEntry(bot);
64     entry->callback = callback;
65     entry->data = data;
66     putsock(bot, "PRIVMSG " AUTHSERV_NICK " :HANDLEINFO *%s", auth);
67 }
68
69 static void recv_notice(struct UserNode *user, struct UserNode *target, char *message) {
70     if(stricmp(user->nick, AUTHSERV_NICK)) return;
71     struct ClientSocket *bot;
72     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
73         if(bot->user == target) break;
74     }
75     if(!bot) return;
76     char *auth;
77     int match = 0, exists = 0;
78     char *tmp;
79     //messages to parse:
80     //  Account * has not been registered.
81     //  Account information for Skynet:
82     if(!match("Account * has not been registered.", message)) {
83         match = 1;
84         tmp = strstr(message, "\002");
85         auth = tmp+1;
86         tmp = strstr(auth, "\002");
87         *tmp = '\0';
88     }
89     if(!match("Account information for *", message)) {
90         match = 1;
91         exists = 1;
92         tmp = strstr(message, "\002");
93         auth = tmp+1;
94         tmp = strstr(auth, "\002");
95         *tmp = '\0';
96     }
97     if(match) {
98         struct HandleInfoQueueEntry* entry = getNextHandleInfoQueueEntry(client, 1);
99         authlookup_callback_t *callback = entry->callback;
100         callback(auth, exists, entry->data);
101         free(entry);
102     }
103 }
104
105 void init_handleinfohandler() {
106     bind_privnotice(recv_notice);
107 }
108
109 void free_handleinfohandler() {
110     struct HandleInfoQueueEntry *entry, *next;
111     for(entry = first_entry; entry; entry = next) {
112         next = entry->next;
113         free(entry);
114     }
115     first_entry = NULL;
116     last_entry = NULL;
117 }