Anope IRC Services  Version 1.8
epona2anope.c
Go to the documentation of this file.
1 /*
2  * IRC - Internet Relay Chat, epona2anope.c
3  * (C) Copyright 2005-2006, Florian Schulze (Certus)
4  *
5  * Based on the original code of Anope, (C) 2003-2014 Anope Team
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License (see it online
9  * at http://www.gnu.org/copyleft/gpl.html) as published by the Free
10  * Software Foundation;
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * This program tries to convert epona-1.4.15+ dbs to anope standard dbs.
18  * At the moment this only affects chanserv dbs.
19  *
20  * - Certus
21  * February 26, 2005
22  *
23  * Added win32 fix. Who needs that anyways? :P
24  * - Certus
25  * July 20, 2006
26  */
27 
28 
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <ctype.h>
34 #include <time.h>
35 
36 #ifndef _WIN32
37 #include <unistd.h>
38 #else
39 #include "sysconf.h"
40 #include <windows.h>
41 #endif
42 
43 /* Some SUN fixs */
44 #ifdef __sun
45 /* Solaris specific code, types that do not exist in Solaris'
46  * * sys/types.h
47  * **/
48 #undef u_int8_t
49 #undef u_int16_t
50 #undef u_int32_t
51 #undef u_int_64_t
52 #define u_int8_t uint8_t
53 #define u_int16_t uint16_t
54 #define u_int32_t uint32_t
55 #define u_int64_t uint64_t
56 
57 #ifndef INADDR_NONE
58 #define INADDR_NONE (-1)
59 #endif
60 
61 #endif
62 
63 
64 /* CONFIGURATION BLOCK */
65 
66 #define CHAN_DB_EPONA "chan1.db"
67 #define CHAN_DB_ANOPE "chan.db"
68 
69 /* END OF CONFIGURATION BLOCK */
70 
71 #ifndef _WIN32
72 #define C_LBLUE "\033[1;34m"
73 #define C_NONE "\033[m"
74 #else
75 #define C_LBLUE ""
76 #define C_NONE ""
77 #endif
78 
79 #define getc_db(f) (fgetc((f)->fp))
80 #define HASH(nick) ((tolower((nick)[0])&31)<<5 | (tolower((nick)[1])&31))
81 #define HASH2(chan) ((chan)[1] ? ((chan)[1]&31)<<5 | ((chan)[2]&31) : 0)
82 #define read_buffer(buf,f) (read_db((f),(buf),sizeof(buf)) == sizeof(buf))
83 #define write_buffer(buf,f) (write_db((f),(buf),sizeof(buf)) == sizeof(buf))
84 #define read_db(f,buf,len) (fread((buf),1,(len),(f)->fp))
85 #define write_db(f,buf,len) (fwrite((buf),1,(len),(f)->fp))
86 #define read_int8(ret,f) ((*(ret)=fgetc((f)->fp))==EOF ? -1 : 0)
87 #define write_int8(val,f) (fputc((val),(f)->fp)==EOF ? -1 : 0)
88 #define SAFE(x) do { \
89  if ((x) < 0) { \
90  printf("Error, the database is broken, trying to continue... no guarantee.\n"); \
91  } \
92 } while (0)
93 #define READ(x) do { \
94  if ((x) < 0) { \
95  printf("Error, the database is broken, trying to continue... no guarantee.\n"); \
96  exit(0); \
97  } \
98 } while (0)
99 
100 typedef int16_t int16;
101 typedef u_int16_t uint16;
102 typedef int32_t int32;
103 typedef u_int32_t uint32;
104 typedef struct memo_ Memo;
105 typedef struct dbFILE_ dbFILE;
106 typedef struct nickalias_ NickAlias;
107 typedef struct nickcore_ NickCore;
108 typedef struct chaninfo_ ChannelInfo;
109 typedef struct botinfo_ BotInfo;
110 typedef struct badword_ BadWord;
111 typedef struct hostcore_ HostCore;
112 
113 struct memo_ {
114  uint32 number; /* Index number -- not necessarily array position! */
115  int16 flags; /* Flags */
116  time_t time; /* When was it sent? */
117  char sender[32]; /* Name of the sender */
118  char *text;
119 };
120 
121 struct dbFILE_ {
122  int mode; /* 'r' for reading, 'w' for writing */
123  FILE *fp; /* The normal file descriptor */
124  char filename[1024]; /* Name of the database file */
125 };
126 
127 typedef struct {
128  int16 memocount; /* Current # of memos */
129  int16 memomax; /* Max # of memos one can hold*/
130  Memo *memos; /* Pointer to original memos */
131 } MemoInfo;
132 
133 typedef struct {
134  int16 in_use; /* 1 if this entry is in use, else 0 */
135  int16 level;
136  NickCore *nc; /* Guaranteed to be non-NULL if in use, NULL if not */
137  time_t last_seen;
138 } ChanAccess;
139 
140 typedef struct {
141  int16 in_use; /* Always 0 if not in use */
142  int16 is_nick; /* 1 if a regged nickname, 0 if a nick!user@host mask */
144  union {
145  char *mask; /* Guaranteed to be non-NULL if in use, NULL if not */
146  NickCore *nc; /* Same */
147  } u;
148  char *reason;
149  char *creator;
150  time_t addtime;
151 } AutoKick;
152 
153 struct nickcore_ {
154  NickCore *next, *prev;
155 
156  char *display; /* How the nick is displayed */
157  int unused; /* Used for nick collisions */
158 };
159 
160 struct chaninfo_ {
161  ChannelInfo *next, *prev;
162 
163  char name[64]; /* Channel name */
164  char *founder; /* Who registered the channel */
165  char *successor; /* Who gets the channel if the founder nick is dropped or expires */
166  char founderpass[32]; /* Channel password */
167  char *desc; /* Description */
168  char *url; /* URL */
169  char *email; /* Email address */
170  time_t time_registered; /* When was it registered */
171  time_t last_used; /* When was it used hte last time */
172  char *last_topic; /* Last topic on the channel */
173  char last_topic_setter[32]; /* Who set the last topic */
174  time_t last_topic_time; /* When the last topic was set */
175  uint32 flags; /* Flags */
176  char *forbidby; /* if forbidden: who did it */
177  char *forbidreason; /* if forbidden: why */
178  int16 bantype; /* Bantype */
179  int16 *levels; /* Access levels for commands */
180  int16 accesscount; /* # of pple with access */
181  ChanAccess *access; /* List of authorized users */
182  int16 akickcount; /* # of akicked pple */
183  AutoKick *akick; /* List of users to kickban */
184  uint32 mlock_on, mlock_off; /* See channel modes below */
185  uint32 mlock_limit; /* 0 if no limit */
186  char *mlock_key; /* NULL if no key */
187  char *mlock_flood; /* NULL if no +f */
188  char *mlock_joinrate; /* NULL if no +j */
189  char *mlock_redirect; /* NULL if no +L */
190  char *entry_message; /* Notice sent on entering channel */
191  MemoInfo memos; /* Memos */
192  char *bi; /* Bot used on this channel */
193  uint32 botflags; /* BS_* below */
194  int16 *ttb; /* Times to ban for each kicker */
195  int16 bwcount; /* Badword count */
196  BadWord *badwords; /* For BADWORDS kicker */
197  int16 capsmin, capspercent; /* For CAPS kicker */
198  int16 floodlines, floodsecs; /* For FLOOD kicker */
199  int16 repeattimes; /* For REPEAT kicker */
200 };
201 
202 struct botinfo_ {
203  BotInfo *next, *prev;
204  char *nick; /* Nickname of the bot */
205  char *user; /* Its user name */
206  char *host; /* Its hostname */
207  char *real; /* Its real name */
208  int16 flags; /* Bot flags */
209  time_t created; /* Birth date */
210  int16 chancount; /* Number of channels that use the bot. */
211 };
212 
213 struct badword_ {
215  char *word;
217 };
218 
219 dbFILE *open_db_write(const char *service, const char *filename, int version);
220 dbFILE *open_db_read(const char *service, const char *filename, int version);
221 NickCore *findcore(const char *nick, int version);
222 ChannelInfo *cs_findchan(const char *chan);
223 char *strscpy(char *d, const char *s, size_t len);
225 int mystricmp(const char *s1, const char *s2);
226 int write_string(const char *s, dbFILE * f);
227 int write_ptr(const void *ptr, dbFILE * f);
228 int read_int16(int16 * ret, dbFILE * f);
229 int read_uint16(uint16 * ret, dbFILE * f);
230 int read_int32(int32 * ret, dbFILE * f);
231 int read_uint32(uint32 * ret, dbFILE * f);
232 int read_string(char **ret, dbFILE * f);
233 int write_int16(uint16 val, dbFILE * f);
234 int write_int32(uint32 val, dbFILE * f);
235 int read_ptr(void **ret, dbFILE * f);
236 void alpha_insert_chan(ChannelInfo * ci);
237 void close_db(dbFILE * f);
238 
241 
242 int main(int argc, char *argv[])
243 {
244  dbFILE *f;
245  int i;
246  long countr = 0, countw = 0;
247  /* Unused variables - why? -GD
248  NickCore *nc, *ncnext;
249  */
250 
251  printf("\n"C_LBLUE"Epona to Anope DB converter by Certus"C_NONE"\n\n");
252 
253  /* Section I: Reading */
254  if ((f = open_db_read("ChanServ", CHAN_DB_EPONA, 17))) {
255  ChannelInfo *ci, **last, *prev;
256  int c;
257 
258  printf("Trying to convert channel database...\n");
259 
260  for (i = 0; i < 256; i++) {
261  int16 tmp16;
262  int32 tmp32;
263  int n_levels;
264  char *s;
265  int n_ttb;
266  /* Unused variable - why? -GD
267  int J;
268  */
269 
270  last = &chanlists[i];
271  prev = NULL;
272 
273  while ((c = getc_db(f)) == 1) {
274  int j;
275 
276  if (c != 1) {
277  printf("Invalid format in %s.\n", CHAN_DB_EPONA);
278  exit(0);
279  }
280 
281  ci = calloc(sizeof(ChannelInfo), 1);
282  *last = ci;
283  last = &ci->next;
284  ci->prev = prev;
285  prev = ci;
286  READ(read_buffer(ci->name, f));
287  READ(read_string(&ci->founder, f));
288  READ(read_string(&ci->successor, f));
289  READ(read_buffer(ci->founderpass, f));
290  READ(read_string(&ci->desc, f));
291  if (!ci->desc)
292  ci->desc = strdup("");
293  READ(read_string(&ci->url, f));
294  READ(read_string(&ci->email, f));
295  READ(read_int32(&tmp32, f));
296  ci->time_registered = tmp32;
297  READ(read_int32(&tmp32, f));
298  ci->last_used = tmp32;
299  READ(read_string(&ci->last_topic, f));
301  READ(read_int32(&tmp32, f));
302  ci->last_topic_time = tmp32;
303  READ(read_uint32(&ci->flags, f));
304  /* Temporary flags cleanup */
305  ci->flags &= ~0x80000000;
306  READ(read_string(&ci->forbidby, f));
307  READ(read_string(&ci->forbidreason, f));
308  READ(read_int16(&tmp16, f));
309  ci->bantype = tmp16;
310  READ(read_int16(&tmp16, f));
311  n_levels = tmp16;
312  ci->levels = calloc(36 * sizeof(*ci->levels), 1);
313  for (j = 0; j < n_levels; j++) {
314  if (j < 36)
315  READ(read_int16(&ci->levels[j], f));
316  else
317  READ(read_int16(&tmp16, f));
318  }
319  READ(read_int16(&ci->accesscount, f));
320  if (ci->accesscount) {
321  ci->access = calloc(ci->accesscount, sizeof(ChanAccess));
322  for (j = 0; j < ci->accesscount; j++) {
323  READ(read_int16(&ci->access[j].in_use, f));
324  if (ci->access[j].in_use) {
325  READ(read_int16(&ci->access[j].level, f));
326  READ(read_string(&s, f));
327  if (s) {
328  ci->access[j].nc = findcore(s, 0);
329  free(s);
330  }
331  if (ci->access[j].nc == NULL)
332  ci->access[j].in_use = 0;
333  READ(read_int32(&tmp32, f));
334  ci->access[j].last_seen = tmp32;
335  }
336  }
337  } else {
338  ci->access = NULL;
339  }
340  READ(read_int16(&ci->akickcount, f));
341  if (ci->akickcount) {
342  ci->akick = calloc(ci->akickcount, sizeof(AutoKick));
343  for (j = 0; j < ci->akickcount; j++) {
344  SAFE(read_int16(&ci->akick[j].flags, f));
345  if (ci->akick[j].flags & 0x0001) {
346  SAFE(read_string(&s, f));
347  if (ci->akick[j].flags & 0x0002) {
348  ci->akick[j].u.nc = findcore(s, 0);
349  if (!ci->akick[j].u.nc)
350  ci->akick[j].flags &= ~0x0001;
351  free(s);
352  } else {
353  ci->akick[j].u.mask = s;
354  }
355  SAFE(read_string(&s, f));
356  if (ci->akick[j].flags & 0x0001)
357  ci->akick[j].reason = s;
358  else if (s)
359  free(s);
360  SAFE(read_string(&s, f));
361  if (ci->akick[j].flags & 0x0001) {
362  ci->akick[j].creator = s;
363  } else if (s) {
364  free(s);
365  }
366  SAFE(read_int32(&tmp32, f));
367  if (ci->akick[j].flags & 0x0001)
368  ci->akick[j].addtime = tmp32;
369  }
370  }
371  } else {
372  ci->akick = NULL;
373  }
374  READ(read_uint32(&ci->mlock_on, f));
375  READ(read_uint32(&ci->mlock_off, f));
376  READ(read_uint32(&ci->mlock_limit, f));
377  READ(read_string(&ci->mlock_key, f));
378  READ(read_string(&ci->mlock_flood, f));
379  READ(read_string(&ci->mlock_joinrate, f));
380  READ(read_string(&ci->mlock_redirect, f));
381  READ(read_int16(&ci->memos.memocount, f));
382  READ(read_int16(&ci->memos.memomax, f));
383  if (ci->memos.memocount) {
384  Memo *memos;
385  memos = calloc(sizeof(Memo) * ci->memos.memocount, 1);
386  ci->memos.memos = memos;
387  for (j = 0; j < ci->memos.memocount; j++, memos++) {
388  READ(read_uint32(&memos->number, f));
389  READ(read_int16(&memos->flags, f));
390  READ(read_int32(&tmp32, f));
391  memos->time = tmp32;
392  READ(read_buffer(memos->sender, f));
393  READ(read_string(&memos->text, f));
394  }
395  }
396  READ(read_string(&ci->entry_message, f));
397 
398  /* BotServ options */
399  READ(read_string(&ci->bi, f));
400  READ(read_int32(&tmp32, f));
401  ci->botflags = tmp32;
402  READ(read_int16(&tmp16, f));
403  n_ttb = tmp16;
404  ci->ttb = calloc(2 * 8, 1);
405  for (j = 0; j < n_ttb; j++) {
406  if (j < 8)
407  READ(read_int16(&ci->ttb[j], f));
408  else
409  READ(read_int16(&tmp16, f));
410  }
411  for (j = n_ttb; j < 8; j++)
412  ci->ttb[j] = 0;
413  READ(read_int16(&tmp16, f));
414  ci->capsmin = tmp16;
415  READ(read_int16(&tmp16, f));
416  ci->capspercent = tmp16;
417  READ(read_int16(&tmp16, f));
418  ci->floodlines = tmp16;
419  READ(read_int16(&tmp16, f));
420  ci->floodsecs = tmp16;
421  READ(read_int16(&tmp16, f));
422  ci->repeattimes = tmp16;
423 
424  READ(read_int16(&ci->bwcount, f));
425  if (ci->bwcount) {
426  ci->badwords = calloc(ci->bwcount, sizeof(BadWord));
427  for (j = 0; j < ci->bwcount; j++) {
428  SAFE(read_int16(&ci->badwords[j].in_use, f));
429  if (ci->badwords[j].in_use) {
430  SAFE(read_string(&ci->badwords[j].word, f));
431  SAFE(read_int16(&ci->badwords[j].type, f));
432  }
433  }
434  } else {
435  ci->badwords = NULL;
436  }
437  countr++;
438  } /* getc_db() */
439  *last = NULL;
440  } /* for() loop */
441  close_db(f);
442  }
443 
444  /* II: Saving */
445  {
446  if ((f = open_db_write("ChanServ", CHAN_DB_ANOPE, 16))) {
447  ChannelInfo *ci;
448  Memo *memos;
449  /* Unused variable - why? -GD
450  static time_t lastwarn = 0;
451  */
452 
453  for (i = 0; i < 256; i++) {
454  int16 tmp16;
455  for (ci = chanlists[i]; ci; ci = ci->next) {
456  int j;
457  SAFE(write_int8(1, f));
458  SAFE(write_buffer(ci->name, f));
459  if (ci->founder)
460  SAFE(write_string(ci->founder, f));
461  else
462  SAFE(write_string(NULL, f));
463  if (ci->successor)
464  SAFE(write_string(ci->successor, f));
465  else
466  SAFE(write_string(NULL, f));
467  SAFE(write_buffer(ci->founderpass, f));
468  SAFE(write_string(ci->desc, f));
469  SAFE(write_string(ci->url, f));
470  SAFE(write_string(ci->email, f));
472  SAFE(write_int32(ci->last_used, f));
473  SAFE(write_string(ci->last_topic, f));
476  SAFE(write_int32(ci->flags, f));
477  SAFE(write_string(ci->forbidby, f));
478  SAFE(write_string(ci->forbidreason, f));
479  SAFE(write_int16(ci->bantype, f));
480  tmp16 = 36;
481  SAFE(write_int16(tmp16, f));
482  for (j = 0; j < 36; j++)
483  SAFE(write_int16(ci->levels[j], f));
484 
485  SAFE(write_int16(ci->accesscount, f));
486  for (j = 0; j < ci->accesscount; j++) {
487  SAFE(write_int16(ci->access[j].in_use, f));
488  if (ci->access[j].in_use) {
489  SAFE(write_int16(ci->access[j].level, f));
490  SAFE(write_string(ci->access[j].nc->display, f));
491  SAFE(write_int32(ci->access[j].last_seen, f));
492  }
493  }
494  SAFE(write_int16(ci->akickcount, f));
495  for (j = 0; j < ci->akickcount; j++) {
496  SAFE(write_int16(ci->akick[j].flags, f));
497  if (ci->akick[j].flags & 0x0001) {
498  if (ci->akick[j].flags & 0x0002)
499  SAFE(write_string(ci->akick[j].u.nc->display, f));
500  else
501  SAFE(write_string(ci->akick[j].u.mask, f));
502  SAFE(write_string(ci->akick[j].reason, f));
503  SAFE(write_string(ci->akick[j].creator, f));
504  SAFE(write_int32(ci->akick[j].addtime, f));
505  }
506  }
507 
508  SAFE(write_int32(ci->mlock_on, f));
509  SAFE(write_int32(ci->mlock_off, f));
510  SAFE(write_int32(ci->mlock_limit, f));
511  SAFE(write_string(ci->mlock_key, f));
512  SAFE(write_string(ci->mlock_flood, f));
514  SAFE(write_int16(ci->memos.memocount, f));
515  SAFE(write_int16(ci->memos.memomax, f));
516  memos = ci->memos.memos;
517  for (j = 0; j < ci->memos.memocount; j++, memos++) {
518  SAFE(write_int32(memos->number, f));
519  SAFE(write_int16(memos->flags, f));
520  SAFE(write_int32(memos->time, f));
521  SAFE(write_buffer(memos->sender, f));
522  SAFE(write_string(memos->text, f));
523  }
525  if (ci->bi)
526  SAFE(write_string(ci->bi, f));
527  else
528  SAFE(write_string(NULL, f));
529  SAFE(write_int32(ci->botflags, f));
530  tmp16 = 8;
531  SAFE(write_int16(tmp16, f));
532  for (j = 0; j < 8; j++)
533  SAFE(write_int16(ci->ttb[j], f));
534  SAFE(write_int16(ci->capsmin, f));
535  SAFE(write_int16(ci->capspercent, f));
536  SAFE(write_int16(ci->floodlines, f));
537  SAFE(write_int16(ci->floodsecs, f));
538  SAFE(write_int16(ci->repeattimes, f));
539 
540  SAFE(write_int16(ci->bwcount, f));
541  for (j = 0; j < ci->bwcount; j++) {
542  SAFE(write_int16(ci->badwords[j].in_use, f));
543  if (ci->badwords[j].in_use) {
544  SAFE(write_string(ci->badwords[j].word, f));
545  SAFE(write_int16(ci->badwords[j].type, f));
546  }
547  }
548  countw++;
549  } /* for (chanlists[i]) */
550  SAFE(write_int8(0, f));
551  } /* for (i) */
552  close_db(f);
553  printf("%ld channels read, %ld channels written. New database saved as %s.\n", countr, countw, CHAN_DB_ANOPE);
554  }
555  }
556 
557  printf("\n\nConverting is now done.\n");
558  return 0;
559 } /* End of main() */
560 
561 /* Open a database file for reading and check for the version */
562 dbFILE *open_db_read(const char *service, const char *filename, int version)
563 {
564  dbFILE *f;
565  FILE *fp;
566  int myversion;
567 
568  f = calloc(sizeof(*f), 1);
569  if (!f) {
570  printf("Can't allocate memory for %s database %s.\n", service, filename);
571  exit(0);
572  }
573  strscpy(f->filename, filename, sizeof(f->filename));
574  f->mode = 'r';
575  fp = fopen(f->filename, "rb");
576  if (!fp) {
577  printf("Can't read %s database %s.\n", service, f->filename);
578  free(f);
579  return NULL;
580  }
581  f->fp = fp;
582  myversion = fgetc(fp) << 24 | fgetc(fp) << 16 | fgetc(fp) << 8 | fgetc(fp);
583  if (feof(fp)) {
584  printf("Error reading version number on %s: End of file detected.\n", f->filename);
585  exit(0);
586  } else if (myversion < version) {
587  printf("Unsuported database version (%d) on %s.\n", myversion, f->filename);
588  exit(0);
589  }
590  return f;
591 }
592 
593 /* Open a database file for reading and check for the version */
594 dbFILE *open_db_write(const char *service, const char *filename, int version)
595 {
596  dbFILE *f;
597  int fd;
598 
599  f = calloc(sizeof(*f), 1);
600  if (!f) {
601  printf("Can't allocate memory for %s database %s.\n", service, filename);
602  exit(0);
603  }
604  strscpy(f->filename, filename, sizeof(f->filename));
605  filename = f->filename;
606 #ifndef _WIN32
607  unlink(filename);
608 #else
609  DeleteFile(filename);
610 #endif
611  f->mode = 'w';
612 #ifndef _WIN32
613  fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
614 #else
615  fd = open(filename, O_WRONLY | O_CREAT | O_EXCL | _O_BINARY, 0666);
616 #endif
617  f->fp = fdopen(fd, "wb"); /* will fail and return NULL if fd < 0 */
618  if (!f->fp || !write_file_version(f, version)) {
619  printf("Can't write to %s database %s.\n", service, filename);
620  if (f->fp) {
621  fclose(f->fp);
622 #ifndef _WIN32
623  unlink(filename);
624 #else
625  DeleteFile(filename);
626 #endif
627  }
628  free(f);
629  return NULL;
630  }
631  return f;
632 }
633 
634 /* Close it */
635 void close_db(dbFILE * f)
636 {
637  fclose(f->fp);
638  free(f);
639 }
640 
641 int read_int16(int16 * ret, dbFILE * f)
642 {
643  int c1, c2;
644 
645  c1 = fgetc(f->fp);
646  c2 = fgetc(f->fp);
647  if (c1 == EOF || c2 == EOF)
648  return -1;
649  *ret = c1 << 8 | c2;
650  return 0;
651 }
652 
653 int read_uint16(uint16 * ret, dbFILE * f)
654 {
655  int c1, c2;
656 
657  c1 = fgetc(f->fp);
658  c2 = fgetc(f->fp);
659  if (c1 == EOF || c2 == EOF)
660  return -1;
661  *ret = c1 << 8 | c2;
662  return 0;
663 }
664 
665 int write_int16(uint16 val, dbFILE * f)
666 {
667  if (fputc((val >> 8) & 0xFF, f->fp) == EOF
668  || fputc(val & 0xFF, f->fp) == EOF)
669  return -1;
670  return 0;
671 }
672 
673 
674 int read_int32(int32 * ret, dbFILE * f)
675 {
676  int c1, c2, c3, c4;
677 
678  c1 = fgetc(f->fp);
679  c2 = fgetc(f->fp);
680  c3 = fgetc(f->fp);
681  c4 = fgetc(f->fp);
682  if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF)
683  return -1;
684  *ret = c1 << 24 | c2 << 16 | c3 << 8 | c4;
685  return 0;
686 }
687 
688 int read_uint32(uint32 * ret, dbFILE * f)
689 {
690  int c1, c2, c3, c4;
691 
692  c1 = fgetc(f->fp);
693  c2 = fgetc(f->fp);
694  c3 = fgetc(f->fp);
695  c4 = fgetc(f->fp);
696  if (c1 == EOF || c2 == EOF || c3 == EOF || c4 == EOF)
697  return -1;
698  *ret = c1 << 24 | c2 << 16 | c3 << 8 | c4;
699  return 0;
700 }
701 
702 int write_int32(uint32 val, dbFILE * f)
703 {
704  if (fputc((val >> 24) & 0xFF, f->fp) == EOF)
705  return -1;
706  if (fputc((val >> 16) & 0xFF, f->fp) == EOF)
707  return -1;
708  if (fputc((val >> 8) & 0xFF, f->fp) == EOF)
709  return -1;
710  if (fputc((val) & 0xFF, f->fp) == EOF)
711  return -1;
712  return 0;
713 }
714 
715 
716 int read_ptr(void **ret, dbFILE * f)
717 {
718  int c;
719 
720  c = fgetc(f->fp);
721  if (c == EOF)
722  return -1;
723  *ret = (c ? (void *) 1 : (void *) 0);
724  return 0;
725 }
726 
727 int write_ptr(const void *ptr, dbFILE * f)
728 {
729  if (fputc(ptr ? 1 : 0, f->fp) == EOF)
730  return -1;
731  return 0;
732 }
733 
734 
735 int read_string(char **ret, dbFILE * f)
736 {
737  char *s;
738  uint16 len;
739 
740  if (read_uint16(&len, f) < 0)
741  return -1;
742  if (len == 0) {
743  *ret = NULL;
744  return 0;
745  }
746  s = calloc(len, 1);
747  if (len != fread(s, 1, len, f->fp)) {
748  free(s);
749  return -1;
750  }
751  *ret = s;
752  return 0;
753 }
754 
755 int write_string(const char *s, dbFILE * f)
756 {
757  uint32 len;
758 
759  if (!s)
760  return write_int16(0, f);
761  len = strlen(s);
762  if (len > 65534)
763  len = 65534;
764  if (write_int16((uint16) (len + 1), f) < 0)
765  return -1;
766  if (len > 0 && fwrite(s, 1, len, f->fp) != len)
767  return -1;
768  if (fputc(0, f->fp) == EOF)
769  return -1;
770  return 0;
771 }
772 
773 NickCore *findcore(const char *nick, int unused)
774 {
775  NickCore *nc;
776 
777  for (nc = nclists[HASH(nick)]; nc; nc = nc->next) {
778  if (!mystricmp(nc->display, nick))
779  if ((nc->unused && unused) || (!nc->unused && !unused))
780  return nc;
781  }
782 
783  return NULL;
784 }
785 
787 {
788  FILE *fp = f->fp;
789  if (fputc(version >> 24 & 0xFF, fp) < 0 ||
790  fputc(version >> 16 & 0xFF, fp) < 0 ||
791  fputc(version >> 8 & 0xFF, fp) < 0 ||
792  fputc(version & 0xFF, fp) < 0) {
793  printf("Error writing version number on %s.\n", f->filename);
794  exit(0);
795  }
796  return 1;
797 }
798 
799 /* strscpy: Copy at most len-1 characters from a string to a buffer, and
800  * add a null terminator after the last character copied.
801  */
802 
803 char *strscpy(char *d, const char *s, size_t len)
804 {
805  char *d_orig = d;
806 
807  if (!len)
808  return d;
809  while (--len && (*d++ = *s++));
810  *d = '\0';
811  return d_orig;
812 }
813 
814 int mystricmp(const char *s1, const char *s2)
815 {
816  register int c;
817 
818  while ((c = tolower(*s1)) == tolower(*s2)) {
819  if (c == 0)
820  return 0;
821  s1++;
822  s2++;
823  }
824  if (c < tolower(*s2))
825  return -1;
826  return 1;
827 }
828 
829 ChannelInfo *cs_findchan(const char *chan)
830 {
831  ChannelInfo *ci;
832  for (ci = chanlists[tolower(chan[1])]; ci; ci = ci->next) {
833  if (!mystricmp(ci->name, chan))
834  return ci;
835  }
836  return NULL;
837 }
838 
840 {
841  ChannelInfo *ptr, *prev;
842  char *chan = ci->name;
843 
844  for (prev = NULL, ptr = chanlists[tolower(chan[1])];
845  ptr != NULL && mystricmp(ptr->name, chan) < 0;
846  prev = ptr, ptr = ptr->next);
847  ci->prev = prev;
848  ci->next = ptr;
849  if (!prev)
850  chanlists[tolower(chan[1])] = ci;
851  else
852  prev->next = ci;
853  if (ptr)
854  ptr->prev = ci;
855 }
856 
char * url
Definition: services.h:660
E char * strdup(const char *s)
Definition: compat.c:94
NickCore * prev
Definition: services.h:540
int read_uint32(uint32 *ret, dbFILE *f)
Definition: epona2anope.c:688
uint16 akickcount
Definition: services.h:678
char filename[MAXPATHLEN]
Definition: datafiles.h:29
int read_string(char **ret, dbFILE *f)
Definition: epona2anope.c:735
char sender[NICKMAX]
Definition: services.h:494
int unused
Definition: db-merger.c:201
ChannelInfo * chanlists[256]
Definition: epona2anope.c:239
uint32 botflags
Definition: services.h:700
int mystricmp(const char *s1, const char *s2)
Definition: epona2anope.c:814
char * word
Definition: services.h:642
int16 capspercent
Definition: services.h:705
int read_int32(int32 *ret, dbFILE *f)
Definition: epona2anope.c:674
int16 flags
Definition: epona2anope.c:115
dbFILE * open_db_read(const char *service, const char *filename, int version)
Definition: epona2anope.c:562
uint16 type
Definition: services.h:643
char * mlock_redirect
Definition: services.h:685
time_t last_used
Definition: services.h:664
#define C_NONE
Definition: epona2anope.c:73
int16 accesscount
Definition: epona2anope.c:180
char * mlock_flood
Definition: services.h:684
ChannelInfo * prev
Definition: services.h:653
int16 type
Definition: epona2anope.c:216
char founderpass[PASSMAX]
Definition: services.h:658
#define getc_db(f)
Definition: epona2anope.c:79
char * mlock_joinrate
Definition: epona2anope.c:188
int16 level
Definition: services.h:594
ChannelInfo * cs_findchan(const char *chan)
Definition: epona2anope.c:829
int16 chancount
Definition: services.h:579
#define CHAN_DB_EPONA
Definition: epona2anope.c:66
#define SAFE(x)
Definition: epona2anope.c:88
int16 memocount
Definition: services.h:505
union AutoKick::@1 u
int read_ptr(void **ret, dbFILE *f)
Definition: epona2anope.c:716
int read_int16(int16 *ret, dbFILE *f)
Definition: epona2anope.c:641
char name[CHANMAX]
Definition: services.h:654
int16 flags
Definition: epona2anope.c:143
time_t last_topic_time
Definition: services.h:667
char last_topic_setter[NICKMAX]
Definition: services.h:666
char * forbidreason
Definition: services.h:671
char * last_topic
Definition: services.h:665
int16 bantype
Definition: services.h:673
char * email
Definition: services.h:661
time_t last_seen
Definition: services.h:596
NickCore * findcore(const char *nick, int version)
Definition: epona2anope.c:773
char * display
Definition: services.h:542
uint16 flags
Definition: services.h:492
void alpha_insert_chan(ChannelInfo *ci)
Definition: epona2anope.c:839
int write_ptr(const void *ptr, dbFILE *f)
Definition: epona2anope.c:727
BadWord * badwords
Definition: services.h:704
#define C_LBLUE
Definition: epona2anope.c:72
u_int16_t uint16
Definition: epona2anope.c:101
FILE * fp
Definition: datafiles.h:26
uint32 flags
Definition: services.h:669
char * entry_message
Definition: services.h:688
int16 memomax
Definition: services.h:505
char * text
Definition: services.h:495
int write_int32(uint32 val, dbFILE *f)
Definition: epona2anope.c:702
int16 flags
Definition: services.h:577
uint16 bwcount
Definition: services.h:703
NickCore * next
Definition: services.h:540
int16 in_use
Definition: epona2anope.c:214
int32_t int32
Definition: db-merger.c:122
#define write_buffer(buf, f)
Definition: epona2anope.c:83
char * reason
Definition: services.h:628
uint16 accesscount
Definition: services.h:676
BotInfo * prev
Definition: services.h:571
uint32 mlock_off
Definition: services.h:681
Command * c
Definition: ns_recover.c:17
char * real
Definition: services.h:576
u_int32_t uint32
Definition: db-merger.c:123
NickCore * nc
Definition: services.h:626
uint32 number
Definition: services.h:491
NickCore * nc
Definition: services.h:595
uint32 mlock_limit
Definition: services.h:682
int16_t int16
Definition: db-merger.c:120
int write_int16(uint16 val, dbFILE *f)
Definition: epona2anope.c:665
int32_t int32
Definition: epona2anope.c:102
int mode
Definition: datafiles.h:25
int write_file_version(dbFILE *f, uint32 version)
Definition: epona2anope.c:786
char * mask
Definition: services.h:625
#define HASH(nick)
Definition: epona2anope.c:80
char * strscpy(char *d, const char *s, size_t len)
Definition: epona2anope.c:803
int16 floodlines
Definition: services.h:706
time_t created
Definition: services.h:578
#define tolower
Definition: services.h:190
char * host
Definition: services.h:575
BotInfo * next
Definition: services.h:571
char * mlock_key
Definition: services.h:683
int write_string(const char *s, dbFILE *f)
Definition: epona2anope.c:755
MemoInfo memos
Definition: services.h:690
int16 floodsecs
Definition: services.h:706
int16_t int16
Definition: epona2anope.c:100
int16 * ttb
Definition: services.h:701
char * nick
Definition: services.h:573
time_t time_registered
Definition: services.h:663
time_t addtime
Definition: services.h:631
#define CHAN_DB_ANOPE
Definition: epona2anope.c:67
dbFILE * open_db_write(const char *service, const char *filename, int version)
Definition: epona2anope.c:594
AutoKick * akick
Definition: services.h:679
NickCore * nclists[1024]
Definition: epona2anope.c:240
ChanAccess * access
Definition: services.h:677
int16 capsmin
Definition: services.h:705
char * creator
Definition: services.h:630
uint16 in_use
Definition: services.h:641
int read_uint16(uint16 *ret, dbFILE *f)
Definition: epona2anope.c:653
int16 * levels
Definition: services.h:674
#define read_buffer(buf, f)
Definition: epona2anope.c:82
char version[1024]
Definition: version.sh.c:24
time_t time
Definition: services.h:493
uint16 flags
Definition: services.h:623
int16 repeattimes
Definition: services.h:707
char * desc
Definition: services.h:659
int16 in_use
Definition: epona2anope.c:134
Memo * memos
Definition: services.h:506
#define write_int8(val, f)
Definition: epona2anope.c:87
uint16 in_use
Definition: services.h:593
BotInfo * bi
Definition: services.h:699
u_int32_t uint32
Definition: epona2anope.c:103
NickCore * founder
Definition: services.h:655
int main(int argc, char *argv[])
Definition: epona2anope.c:242
char * user
Definition: services.h:574
char * forbidby
Definition: services.h:670
ChannelInfo * next
Definition: services.h:653
int16 akickcount
Definition: epona2anope.c:182
void close_db(dbFILE *f)
Definition: epona2anope.c:635
uint32 mlock_on
Definition: services.h:681
int16 bwcount
Definition: epona2anope.c:195
#define READ(x)
Definition: epona2anope.c:93
NickCore * successor
Definition: services.h:656
u_int16_t uint16
Definition: db-merger.c:121