I've just finished edit db-merger.c from ./src/tools/ to get human readable format of nickserv information...
if sombody interested in this, here is the code:
/*
* IRC - Internet Relay Chat, nickserv-info.c
* (C) Copyright 2005, Florian Schulze (Certus)
*
* Based on the original code of Anope, (C) 2003 Anope Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License (see it online
* at
http://www.gnu.org/copyleft/gpl.html) as published by the Free
* Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This program tries to represent nickserv standard anope-1.6.x+ database
* to human readable format...
*
* Edited my Maksimov V. Andrei
*
maksimov.andrei@gmail.com *
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <time.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include "sysconf.h"
#include <windows.h>
#endif
/* Some SUN fixs */
#ifdef __sun
/* Solaris specific code, types that do not exist in Solaris'
* sys/types.h
**/
#undef u_int8_t
#undef u_int16_t
#undef u_int32_t
#undef u_int_64_t
#define u_int8_t uint8_t
#define u_int16_t uint16_t
#define u_int32_t uint32_t
#define u_int64_t uint64_t
#ifndef INADDR_NONE
#define INADDR_NONE (-1)
#endif
#endif
/* CONFIGURATION BLOCK */
#define NICK_DB_1 "nick1.db"
/* END OF CONFIGURATION BLOCK */
#ifndef _WIN32
#define C_LBLUE "\033[1;34m"
#define C_NONE "\033[m"
#else
#define C_LBLUE ""
#define C_NONE ""
#endif
#define getc_db(f) (fgetc((f)->fp))
#define HASH(nick) ((tolower((nick)[0])&31)<<5 | (tolower((nick)[1])&31))
#define HASH2(chan) ((chan)[1] ? ((chan)[1]&31)<<5 | ((chan)[2]&31) : 0)
#define read_buffer(buf,f) (read_db((f),(buf),sizeof(buf)) == sizeof(buf))
#define write_buffer(buf,f) (write_db((f),(buf),sizeof(buf)) == sizeof(buf))
#define read_db(f,buf,len) (fread((buf),1,(len),(f)->fp))
#define write_db(f,buf,len) (fwrite((buf),1,(len),(f)->fp))
#define read_int8(ret,f) ((*(ret)=fgetc((f)->fp))==EOF ? -1 : 0)
#define write_int8(val,f) (fputc((val),(f)->fp)==EOF ? -1 : 0)
#define SAFE(x) do { \
if ((x) < 0) { \
printf("Error, the database is broken, trying to continue... no guarantee.\n"); \
} \
} while (0)
#define READ(x) do { \
if ((x) < 0) { \
printf("Error, the database is broken, trying to continue... no guarantee.\n"); \
exit(0); \
} \
} while (0)
typedef int16_t int16;
typedef u_int16_t uint16;
typedef int32_t int32;
typedef u_int32_t uint32;
typedef struct memo_ Memo;
typedef struct dbFILE_ dbFILE;
typedef struct nickalias_ NickAlias;
typedef struct nickcore_ NickCore;
typedef struct chaninfo_ ChannelInfo;
typedef struct botinfo_ BotInfo;
typedef struct badword_ BadWord;
typedef struct hostcore_ HostCore;
struct memo_ {
uint32 number; /* Index number -- not necessarily array position! */
uint16 flags; /* Flags */
time_t time; /* When was it sent? */
char sender[32]; /* Name of the sender */
char *text;
};
struct dbFILE_ {
int mode; /* 'r' for reading, 'w' for writing */
FILE *fp; /* The normal file descriptor */
char filename[1024]; /* Name of the database file */
};
typedef struct {
int16 memocount; /* Current # of memos */
int16 memomax; /* Max # of memos one can hold*/
Memo *memos; /* Pointer to original memos */
} MemoInfo;
typedef struct {
uint16 in_use; /* 1 if this entry is in use, else 0 */
int16 level;
NickCore *nc; /* Guaranteed to be non-NULL if in use, NULL if not */
time_t last_seen;
} ChanAccess;
typedef struct {
int16 in_use; /* Always 0 if not in use */
int16 is_nick; /* 1 if a regged nickname, 0 if a nick!user@host mask */
uint16 flags;
union {
char *mask; /* Guaranteed to be non-NULL if in use, NULL if not */
NickCore *nc; /* Same */
} u;
char *reason;
char *creator;
time_t addtime;
} AutoKick;
struct nickalias_ {
NickAlias *next, *prev;
char *nick; /* Nickname */
char *last_quit; /* Last quit message */
char *last_realname; /* Last realname */
char *last_usermask; /* Last usermask */
time_t time_registered; /* When the nick was registered */
time_t last_seen; /* When it was seen online for the last time */
uint16 status; /* See NS_* below */
NickCore *nc; /* I'm an alias of this */
};
struct nickcore_ {
NickCore *next, *prev;
char *display; /* How the nick is displayed */
char *pass; /* Password of the nicks */
char *email; /* E-mail associated to the nick */
char *greet; /* Greet associated to the nick */
uint32 icq; /* ICQ # associated to the nick */
char *url; /* URL associated to the nick */
uint32 flags; /* See NI_* below */
uint16 language; /* Language selected by nickname owner (LANG_*) */
uint16 accesscount; /* # of entries */
char **access; /* Array of strings */
MemoInfo memos; /* Memo information */
uint16 channelcount; /* Number of channels currently registered */
uint16 channelmax; /* Maximum number of channels allowed */
int unused; /* Used for nick collisions */
int aliascount; /* How many aliases link to us? Remove the core if 0 */
};
struct chaninfo_ {
ChannelInfo *next, *prev;
char name[64]; /* Channel name */
char *founder; /* Who registered the channel */
char *successor; /* Who gets the channel if the founder nick is dropped or expires */
char founderpass[32]; /* Channel password */
char *desc; /* Description */
char *url; /* URL */
char *email; /* Email address */
time_t time_registered; /* When was it registered */
time_t last_used; /* When was it used hte last time */
char *last_topic; /* Last topic on the channel */
char last_topic_setter[32]; /* Who set the last topic */
time_t last_topic_time; /* When the last topic was set */
uint32 flags; /* Flags */
char *forbidby; /* if forbidden: who did it */
char *forbidreason; /* if forbidden: why */
int16 bantype; /* Bantype */
int16 *levels; /* Access levels for commands */
uint16 accesscount; /* # of pple with access */
ChanAccess *access; /* List of authorized users */
uint16 akickcount; /* # of akicked pple */
AutoKick *akick; /* List of users to kickban */
uint32 mlock_on, mlock_off; /* See channel modes below */
uint32 mlock_limit; /* 0 if no limit */
char *mlock_key; /* NULL if no key */
char *mlock_flood; /* NULL if no +f */
char *mlock_redirect; /* NULL if no +L */
char *entry_message; /* Notice sent on entering channel */
MemoInfo memos; /* Memos */
char *bi; /* Bot used on this channel */
uint32 botflags; /* BS_* below */
int16 *ttb; /* Times to ban for each kicker */
uint16 bwcount; /* Badword count */
BadWord *badwords; /* For BADWORDS kicker */
int16 capsmin, capspercent; /* For CAPS kicker */
int16 floodlines, floodsecs; /* For FLOOD kicker */
int16 repeattimes; /* For REPEAT kicker */
};
struct botinfo_ {
BotInfo *next, *prev;
char *nick; /* Nickname of the bot */
char *user; /* Its user name */
char *host; /* Its hostname */
char *real; /* Its real name */
int16 flags; /* Bot flags */
time_t created; /* Birth date */
int16 chancount; /* Number of channels that use the bot. */
};
struct badword_ {
uint16 in_use;
char *word;
uint16 type;
};
struct hostcore_ {
HostCore *next, *last;
char *nick; /* Owner of the vHost */
char *vIdent; /* vIdent for the user */
char *vHost; /* Vhost for this user */
char *creator; /* Oper Nick of the oper who set the vhost */
time_t time; /* Date/Time vHost was set */
};
dbFILE *open_db_write(const char *service, const char *filename, int version);
dbFILE *open_db_read(const char *service, const char *filename, int version);
NickCore *findcore(const char *nick, int version);
NickAlias *findnick(const char *nick);
BotInfo *findbot(char *nick);
ChannelInfo *cs_findchan(const char *chan);
char *strscpy(char *d, const char *s, size_t len);
int write_file_version(dbFILE * f, uint32 version);
int mystricmp(const char *s1, const char *s2);
int delnick(NickAlias *na, int donttouchthelist);
int write_string(const char *s, dbFILE * f);
int write_ptr(const void *ptr, dbFILE * f);
int read_int16(int16 * ret, dbFILE * f);
int read_int32(int32 * ret, dbFILE * f);
int read_uint16(uint16 * ret, dbFILE * f);
int read_uint32(uint32 * ret, dbFILE * f);
int read_string(char **ret, dbFILE * f);
int write_int16(uint16 val, dbFILE * f);
int write_int32(uint32 val, dbFILE * f);
int read_ptr(void **ret, dbFILE * f);
int delcore(NickCore *nc);
void alpha_insert_chan(ChannelInfo * ci);
void insert_bot(BotInfo * bi);
void close_db(dbFILE * f);
ChannelInfo *chanlists[256];
NickAlias *nalists[1024];
NickCore *nclists[1024];
BotInfo *botlists[256];
int preferfirst = 0, prefersecond = 0, preferoldest = 0, prefernewest = 0;
int nonick = 0, nochan = 0, nobot = 0, nohost = 0;
int main(int argc, char *argv[])
{
dbFILE *f;
int i;
NickCore *nc, *ncnext;
HostCore *firsthc = NULL;
printf("\n"C_LBLUE"DB Merger v0.4 beta for Anope IRC Services by Certus"C_NONE"\n\n");
if (argc >= 2) {
if (!mystricmp(argv[1], "--PREFEROLDEST")) {
printf("Preferring oldest database entries on collision.\n");
preferoldest = 1;
} else if (!mystricmp(argv[1], "--PREFERFIRST")) {
printf("Preferring first database's entries on collision .\n");
preferfirst = 1;
} else if (!mystricmp(argv[1], "--PREFERSECOND")) {
printf("Preferring second database's entries on collision.\n");
prefersecond = 1;
} else if (!mystricmp(argv[1], "--PREFERNEWEST")) {
printf("Preferring newest database entries on collision.\n");
prefernewest = 1;
}
}
/* Section I: Nicks */
/* Ia: First database */
if ((f = open_db_read("NickServ", NICK_DB_1, 13))) {
NickAlias *na, **nalast, *naprev;
NickCore *nc, **nclast, *ncprev;
int16 tmp16;
int32 tmp32;
int i, j, c;
printf("Trying to merge nicks...\n");
/* Nick cores */
for (i = 0; i < 1024; i++) {
nclast = &nclists
;
ncprev = NULL;
while ((c = getc_db(f)) == 1) {
if (c != 1) {
printf("Invalid format in %s.\n", NICK_DB_1);
exit(0);
}
nc = calloc(1, sizeof(NickCore));
nc->aliascount = 0;
nc->unused = 0;
*nclast = nc;
nclast = &nc->next;
nc->prev = ncprev;
ncprev = nc;
printf("--------------------------\n");
READ(read_string(&nc->display, f));
printf("Group: %s\n", nc->display);
READ(read_string(&nc->pass, f));
printf("Pass: %s\n", nc->pass);
READ(read_string(&nc->email, f));
printf("Email: %s\n", nc->email);
READ(read_string(&nc->greet, f));
READ(read_uint32(&nc->icq, f));
if (nc->icq) {
printf("Icq: %d\n", nc->icq);
}
READ(read_string(&nc->url, f));
if (nc->url) {
printf("Url: %s\n", nc->url);
}
READ(read_uint32(&nc->flags, f));
READ(read_uint16(&nc->language, f));
READ(read_uint16(&nc->accesscount, f));
if (nc->accesscount) {
char **access;
access = calloc(sizeof(char *) * nc->accesscount, 1);
nc->access = access;
for (j = 0; j < nc->accesscount; j++, access++)
READ(read_string(access, f));
}
READ(read_int16(&nc->memos.memocount, f));
if (nc->memos.memocount) {
printf("Memo.memocount: %d\n", nc->memos.memocount);
}
READ(read_int16(&nc->memos.memomax, f));
if (nc->memos.memocount) {
Memo *memos;
memos = calloc(sizeof(Memo) * nc->memos.memocount, 1);
nc->memos.memos = memos;
for (j = 0; j < nc->memos.memocount; j++, memos++) {
READ(read_uint32(&memos->number, f));
READ(read_uint16(&memos->flags, f));
READ(read_int32(&tmp32, f));
memos->time = tmp32;
READ(read_buffer(memos->sender, f));
READ(read_string(&memos->text, f));
}
}
READ(read_uint16(&nc->channelcount, f));
READ(read_int16(&tmp16, f));
} /* getc_db() */
*nclast = NULL;
} /* for() loop */
/* Nick aliases */
for (i = 0; i < 1024; i++) {
char *s = NULL;
nalast = &nalists;
naprev = NULL;
while ((c = getc_db(f)) == 1) {
if (c != 1) {
printf("Invalid format in %s.\n", NICK_DB_1);
exit(0);
}
na = calloc(1, sizeof(NickAlias));
READ(read_string(&na->nick, f));
if (na->nick) {
printf("Nick: %s\n", na->nick);
}
READ(read_string(&na->last_usermask, f));
if (na->last_usermask) {
printf("Last_usermask: %s\n", na->last_usermask);
}
READ(read_string(&na->last_realname, f));
if (na->last_realname) {
printf("Last_realname: %s\n", na->last_realname);
}
READ(read_string(&na->last_quit, f));
if (na->last_quit) {
printf("Last_quit: %s\n", na->last_quit);
}
READ(read_int32(&tmp32, f));
na->time_registered = tmp32;
if (na->time_registered) {
printf("Time registered: %s\n", ctime(&na->time_registered));
}
READ(read_int32(&tmp32, f));
na->last_seen = tmp32;
READ(read_uint16(&na->status, f));
READ(read_string(&s, f));
na->nc = findcore(s, 0);
na->nc->aliascount++;
free(s);
*nalast = na;
nalast = &na->next;
na->prev = naprev;
naprev = na;
} /* getc_db() */
*nalast = NULL;
} /* for() loop */
close_db(f); /* End of section Ia */
} else
nonick = 1;
/* CLEAN THE CORES */
for (i = 0; i < 1024; i++) {
for (nc = nclists; nc; nc = ncnext) {
ncnext = nc->next;
if (nc->aliascount < 1) {
printf("Deleting core %s (%s).\n", nc->display, nc->email);
delcore(nc);
}
}
}
/* MERGING DONE \o/ HURRAY! */
printf("\n\nReading is now done. I give NO guarantee for your DBs.\n");
return 0;
} /* End of main() */
/* Open a database file for reading and check for the version */
dbFILE *open_db_read(const char *service, const char *filename, int version)
{
dbFILE *f;
FILE *fp;
int myversion;
f = calloc(sizeof(*f), 1);
if (!f) {
printf("Can't allocate memory for %s database %s.\n", service, filename);
exit(0);
}
strscpy(f->filename, filename, sizeof(f->filename));
f->mode = 'r';
fp = fopen(f->filename, "rb");
if (!fp) {
printf("Can't read %s database %s.\n", service, f->filename);
free(f);
return NULL;
}
f->fp = fp;
myversion = fgetc(fp) << 24 | fgetc(fp) << 16 | fgetc(fp) << 8 | fgetc(fp);
if (feof(fp)) {
printf("Error reading version number on %s: End of file detected.\n", f->filename);
exit(0);
} else if (myversion < version) {
printf("Unsuported database version (%d) on %s.\n", myversion, f->filename);
exit(0);
}
return f;
}
/* Open a database file for reading and check for the version */
dbFILE *open_db_write(const char *service, const char *filename, int version)
{
dbFILE *f;
int fd;
f = calloc(sizeof(*f), 1);
if (!f) {
printf("Can't allocate memory for %s database %s.\n", service, filename);
exit(0);
}
strscpy(f->filename, filename, sizeof(f->filename));
filename = f->filename;
#ifndef _WIN32
unlink(filename);
#else
DeleteFile(filename);
#endif
f->mode = 'w';
#ifndef _WIN32
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
#else
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL | _O_BINARY, 0666);
#endif
f->fp = fdopen(fd, "wb"); /* will fail and return NULL if fd < 0 */
if (!f->fp || !write_file_version(f, version)) {
printf("Can't write to %s database %s.\n", service, filename);
if (f->fp) {
fclose(f->fp);
#ifndef _WIN32
unlink(filename);
#else
DeleteFile(filename);
#endif
}
free(f);
return NULL;
}
return f;
}
/* Close it */
void close_db(dbFILE * f)
{
fclose(f->fp);
free(f);
}
int read_int16(int16 * ret, dbFILE * f)
{
int c1, c2;
c1 = fgetc(f->fp);
c2 = fgetc(f->fp);
if (c1 == EOF || c2 == EOF)
return -1;
*ret = c1 << 8 | c2;
return 0;
}
int read_uint16(uint16 * ret, dbFILE * f)
{
int c1, c2;
c1 = fgetc(f->fp);
c2 = fgetc(f->fp);
if (c1 == EOF || c2 == EOF)
return -1;
*ret = c1 << 8 | c2;
return 0;
}
int write_int16(uint16 val, dbFILE * f)
{
if (fputc((val >>
& 0xFF, f->fp) == EOF
|| fputc(val & 0xFF, f->fp) == EOF)
return -1;
return 0;
}
int read_int32(int32 * ret, dbFILE * f)
{
int c1, c2, c3, c4;
c1 = fgetc(f->fp);
c2 = fgetc(f->fp);
c3 = fgetc(f->fp);
c4 = fgetc(f->fp);
if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF)
return -1;
*ret = c1 << 24 | c2 << 16 | c3 << 8 | c4;
return 0;
}
int read_uint32(uint32 * ret, dbFILE * f)
{
int c1, c2, c3, c4;
c1 = fgetc(f->fp);
c2 = fgetc(f->fp);
c3 = fgetc(f->fp);
c4 = fgetc(f->fp);
if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF)
return -1;
*ret = c1 << 24 | c2 << 16 | c3 << 8 | c4;
return 0;
}
int write_int32(uint32 val, dbFILE * f)
{
if (fputc((val >> 24) & 0xFF, f->fp) == EOF)
return -1;
if (fputc((val >> 16) & 0xFF, f->fp) == EOF)
return -1;
if (fputc((val >>
& 0xFF, f->fp) == EOF)
return -1;
if (fputc((val) & 0xFF, f->fp) == EOF)
return -1;
return 0;
}
int read_ptr(void **ret, dbFILE * f)
{
int c;
c = fgetc(f->fp);
if (c == EOF)
return -1;
*ret = (c ? (void *) 1 : (void *) 0);
return 0;
}
int write_ptr(const void *ptr, dbFILE * f)
{
if (fputc(ptr ? 1 : 0, f->fp) == EOF)
return -1;
return 0;
}
int read_string(char **ret, dbFILE * f)
{
char *s;
uint16 len;
if (read_uint16(&len, f) < 0)
return -1;
if (len == 0) {
*ret = NULL;
return 0;
}
s = calloc(len, 1);
if (len != fread(s, 1, len, f->fp)) {
free(s);
return -1;
}
*ret = s;
return 0;
}
int write_string(const char *s, dbFILE * f)
{
uint32 len;
if (!s)
return write_int16(0, f);
len = strlen(s);
if (len > 65534)
len = 65534;
if (write_int16((uint16) (len + 1), f) < 0)
return -1;
if (len > 0 && fwrite(s, 1, len, f->fp) != len)
return -1;
if (fputc(0, f->fp) == EOF)
return -1;
return 0;
}
NickCore *findcore(const char *nick, int unused)
{
NickCore *nc;
for (nc = nclists[HASH(nick)]; nc; nc = nc->next) {
if (!mystricmp(nc->display, nick))
if ((nc->unused && unused) || (!nc->unused && !unused))
return nc;
}
return NULL;
}
NickAlias *findnick(const char *nick)
{
NickAlias *na;
for (na = nalists[HASH(nick)]; na; na = na->next) {
if (!mystricmp(na->nick, nick))
return na;
}
return NULL;
}
int write_file_version(dbFILE * f, uint32 version)
{
FILE *fp = f->fp;
if (fputc(version >> 24 & 0xFF, fp) < 0 ||
fputc(version >> 16 & 0xFF, fp) < 0 ||
fputc(version >> 8 & 0xFF, fp) < 0 ||
fputc(version & 0xFF, fp) < 0) {
printf("Error writing version number on %s.\n", f->filename);
exit(0);
}
return 1;
}
/* strscpy: Copy at most len-1 characters from a string to a buffer, and
* add a null terminator after the last character copied.
*/
char *strscpy(char *d, const char *s, size_t len)
{
char *d_orig = d;
if (!len)
return d;
while (--len && (*d++ = *s++));
*d = '\0';
return d_orig;
}
int mystricmp(const char *s1, const char *s2)
{
register int c;
while ((c = tolower(*s1)) == tolower(*s2)) {
if (c == 0)
return 0;
s1++;
s2++;
}
if (c < tolower(*s2))
return -1;
return 1;
}
int delnick(NickAlias *na, int donttouchthelist)
{
if (!donttouchthelist) {
/* Remove us from the aliases list */
if (na->next)
na->next->prev = na->prev;
if (na->prev)
na->prev->next = na->next;
else
nalists[HASH(na->nick)] = na->next;
}
/* free() us */
free(na->nick);
if (na->last_usermask)
free(na->last_usermask);
if (na->last_realname)
free(na->last_realname);
if (na->last_quit)
free(na->last_quit);
free(na);
return 1;
}
int delcore(NickCore *nc)
{
int i;
/* Remove the core from the list */
if (nc->next)
nc->next->prev = nc->prev;
if (nc->prev)
nc->prev->next = nc->next;
else
nclists[HASH(nc->display)] = nc->next;
free(nc->display);
if (nc->pass)
free(nc->pass);
if (nc->email)
free(nc->email);
if (nc->greet)
free(nc->greet);
if (nc->url)
free(nc->url);
if (nc->access) {
for (i = 0; i < nc->accesscount; i++) {
if (nc->access)
free(nc->access);
}
free(nc->access);
}
if (nc->memos.memos) {
for (i = 0; i < nc->memos.memocount; i++) {
if (nc->memos.memos.text)
free(nc->memos.memos.text);
}
free(nc->memos.memos);
}
free(nc);
return 1;
}
void insert_bot(BotInfo * bi)
{
BotInfo *ptr, *prev;
for (prev = NULL, ptr = botlists[tolower(*bi->nick)];
ptr != NULL && mystricmp(ptr->nick, bi->nick) < 0;
prev = ptr, ptr = ptr->next);
bi->prev = prev;
bi->next = ptr;
if (!prev)
botlists[tolower(*bi->nick)] = bi;
else
prev->next = bi;
if (ptr)
ptr->prev = bi;
}
BotInfo *findbot(char *nick)
{
BotInfo *bi;
for (bi = botlists[tolower(*nick)]; bi; bi = bi->next)
if (!mystricmp(nick, bi->nick))
return bi;
return NULL;
}
ChannelInfo *cs_findchan(const char *chan)
{
ChannelInfo *ci;
for (ci = chanlists[tolower(chan[1])]; ci; ci = ci->next) {
if (!mystricmp(ci->name, chan))
return ci;
}
return NULL;
}
void alpha_insert_chan(ChannelInfo * ci)
{
ChannelInfo *ptr, *prev;
char *chan = ci->name;
for (prev = NULL, ptr = chanlists[tolower(chan[1])];
ptr != NULL && mystricmp(ptr->name, chan) < 0;
prev = ptr, ptr = ptr->next);
ci->prev = prev;
ci->next = ptr;
if (!prev)
chanlists[tolower(chan[1])] = ci;
else
prev->next = ci;
if (ptr)
ptr->prev = ci;
}
no special commands to compile, just only " gcc -o merger nickserv-info.c "
so, get the stdout and fined what you need...
Next version will be with mysql support....