| 1 |
/* |
| 2 |
* atheme-services: A collection of minimalist IRC services |
| 3 |
* base36uid.c: UID management. |
| 4 |
* |
| 5 |
* Copyright (c) 2005-2007 Atheme Project (http://www.atheme.org) |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
* purpose with or without fee is hereby granted, provided that the above |
| 9 |
* copyright notice and this permission notice appear in all copies. |
| 10 |
* |
| 11 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 12 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 13 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 14 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 15 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 16 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 17 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 19 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 20 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "atheme.h" |
| 25 |
|
| 26 |
DECLARE_MODULE_V1("protocol/base36uid", true, _modinit, NULL, PACKAGE_STRING, VENDOR_STRING); |
| 27 |
|
| 28 |
static char new_uid[10]; /* allow for \0 */ |
| 29 |
static unsigned int uindex = 0; |
| 30 |
|
| 31 |
static void base36_uid_init(const char *sid) |
| 32 |
{ |
| 33 |
unsigned int i; |
| 34 |
char buf[BUFSIZE]; |
| 35 |
|
| 36 |
if (ircd->uses_p10) |
| 37 |
{ |
| 38 |
me.numeric = sstrdup(uinttobase64(buf, (uint64_t) atoi(me.numeric), 2)); |
| 39 |
uindex = 5; |
| 40 |
} |
| 41 |
else |
| 42 |
uindex = 9; |
| 43 |
|
| 44 |
|
| 45 |
memset(new_uid, 0, sizeof(new_uid)); |
| 46 |
|
| 47 |
if (me.numeric != NULL) |
| 48 |
memcpy(new_uid, me.numeric, strlen(me.numeric)); |
| 49 |
else |
| 50 |
return; |
| 51 |
|
| 52 |
for (i = 0; i < strlen(me.numeric); i++) |
| 53 |
if (new_uid[i] == '\0') |
| 54 |
new_uid[i] = 'A'; |
| 55 |
|
| 56 |
for (i = strlen(me.numeric); i < uindex; i++) |
| 57 |
new_uid[i] = 'A'; |
| 58 |
} |
| 59 |
|
| 60 |
static void add_one_to_uid(unsigned int i) |
| 61 |
{ |
| 62 |
if (i != strlen(me.numeric)) /* Not reached server SID portion yet? */ |
| 63 |
{ |
| 64 |
if (new_uid[i] == 'Z') |
| 65 |
new_uid[i] = '0'; |
| 66 |
else if (new_uid[i] == '9') |
| 67 |
{ |
| 68 |
new_uid[i] = 'A'; |
| 69 |
add_one_to_uid(i - 1); |
| 70 |
} |
| 71 |
else |
| 72 |
new_uid[i] = new_uid[i] + 1; |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
if (new_uid[i] == 'Z') |
| 77 |
for (i = strlen(me.numeric); i < 9; i++) |
| 78 |
new_uid[i] = 'A'; |
| 79 |
else |
| 80 |
new_uid[i] = new_uid[i] + 1; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
static const char *base36_uid_get(void) |
| 85 |
{ |
| 86 |
add_one_to_uid(uindex - 1); /* index from 0 */ |
| 87 |
return (new_uid); |
| 88 |
} |
| 89 |
|
| 90 |
uid_provider_t base36_gen = { |
| 91 |
.uid_init = base36_uid_init, |
| 92 |
.uid_get = base36_uid_get, |
| 93 |
}; |
| 94 |
|
| 95 |
void _modinit(module_t *m) |
| 96 |
{ |
| 97 |
uid_provider_impl = &base36_gen; |
| 98 |
} |
| 99 |
|
| 100 |
void _moddeinit(module_unload_intent_t intent) |
| 101 |
{ |
| 102 |
uid_provider_impl = NULL; |
| 103 |
} |