]> jfr.im git - irc/quakenet/newserv.git/blame - authext/authext.c
More chanserv refactoring
[irc/quakenet/newserv.git] / authext / authext.c
CommitLineData
57583275 1#include "authext.h"
2#include "../core/nsmalloc.h"
3#include "../core/error.h"
4#include "../lib/sstring.h"
5#include "../lib/irc_string.h"
6
7#include <string.h>
8
9#define ALLOCUNIT 100
10
11#define authnamehash(x) ((x)%AUTHNAMEHASHSIZE)
12
13authname *freeauthnames;
14authname *authnametable[AUTHNAMEHASHSIZE];
15
16sstring *authnameextnames[MAXAUTHNAMEEXTS];
17
18void _init() {
19 freeauthnames=NULL;
20 memset(authnametable,0,sizeof(authnametable));
21}
22
23void _fini() {
24 nsfreeall(POOL_AUTHEXT);
25}
26
27authname *newauthname() {
28 authname *anp;
29 int i;
30
31 if (freeauthnames==NULL) {
32 freeauthnames=(authname *)nsmalloc(POOL_AUTHEXT, ALLOCUNIT*sizeof(authname));
33 for (i=0;i<(ALLOCUNIT-1);i++) {
34 freeauthnames[i].next=&(freeauthnames[i+1]);
35 }
36 freeauthnames[ALLOCUNIT-1].next=NULL;
37 }
38
39 anp=freeauthnames;
40 freeauthnames=anp->next;
41
42 return anp;
43}
44
45void freeauthname (authname *anp) {
46 anp->next=freeauthnames;
47 freeauthnames=anp;
48}
49
50int registerauthnameext(const char *name) {
51 int i;
52
53 if (findauthnameext(name)!=-1) {
54 Error("nick",ERR_WARNING,"Tried to register duplicate authname extension %s",name);
55 return -1;
56 }
57
58 for (i=0;i<MAXAUTHNAMEEXTS;i++) {
59 if (authnameextnames[i]==NULL) {
60 authnameextnames[i]=getsstring(name,100);
61 return i;
62 }
63 }
64
65 Error("nick",ERR_WARNING,"Tried to register too many authname extensions: %s",name);
66 return -1;
67}
68
69int findauthnameext(const char *name) {
70 int i;
71
72 for (i=0;i<MAXAUTHNAMEEXTS;i++) {
73 if (authnameextnames[i]!=NULL && !ircd_strcmp(name,authnameextnames[i]->content)) {
74 return i;
75 }
76 }
77
78 return -1;
79}
80
81void releaseauthnameext(int index) {
82 int i;
83 authname *anp;
84
85 freesstring(authnameextnames[index]);
86 authnameextnames[index]=NULL;
87
88 for (i=0;i<AUTHNAMEHASHSIZE;i++) {
89 for (anp=authnametable[i];anp;anp=anp->next) {
90 anp->exts[index]=NULL;
91 }
92 }
93}
94
95authname *findauthname(unsigned long userid) {
96 authname *anp;
97
98 if(!userid)
99 return NULL;
100
101 for (anp=authnametable[authnamehash(userid)];anp;anp=(authname *)anp->next)
102 if (userid==anp->userid)
103 return anp;
104
105 return NULL;
106}
107
108authname *findorcreateauthname(unsigned long userid) {
109 authname *anp;
110 unsigned int thehash=authnamehash(userid);
111
112 if(!userid)
113 return NULL;
114
115 for (anp=authnametable[thehash];anp;anp=(authname *)anp->next)
116 if (userid==anp->userid)
117 return anp;
118
119 anp=newauthname();
120 anp->userid=userid;
121 anp->usercount=0;
122 anp->marker=0;
123 anp->nicks=NULL;
124 memset(anp->exts, 0, MAXAUTHNAMEEXTS * sizeof(void *));
125 anp->next=(struct authname *)authnametable[thehash];
126 authnametable[thehash]=anp;
127
128 return anp;
129}
130
131void releaseauthname(authname *anp) {
132 authname **manp;
133 int i;
134 if (anp->usercount==0) {
135 for(i=0;i<MAXAUTHNAMEEXTS;i++)
136 if(anp->exts[i]!=NULL)
137 return;
138
139 for(manp=&(authnametable[authnamehash(anp->userid)]);*manp;manp=(authname **)&((*manp)->next)) {
140 if ((*manp)==anp) {
141 (*manp)=(authname *)anp->next;
142 freeauthname(anp);
143 return;
144 }
145 }
146 Error("nick",ERR_ERROR,"Unable to remove authname %lu from hashtable",anp->userid);
147 }
148}
149
150unsigned int nextauthnamemarker() {
151 int i;
152 authname *anp;
153 static unsigned int authnamemarker=0;
154
155 authnamemarker++;
156 if (!authnamemarker) {
157 /* If we wrapped to zero, zap the marker on all records */
158 for (i=0;i<AUTHNAMEHASHSIZE;i++)
159 for (anp=authnametable[i];anp;anp=anp->next)
160 anp->marker=0;
161 authnamemarker++;
162 }
163
164 return authnamemarker;
165}