fixing cache.c compilation - code stolen from ratbox3, with irc dictionary addition

This commit is contained in:
Valery Yatsko 2008-04-02 04:15:58 +04:00
parent e335494516
commit 0ccdff98ac
2 changed files with 282 additions and 264 deletions

View file

@ -1,50 +1,51 @@
/* $Id: cache.h 6 2005-09-10 01:02:21Z nenolod $ */ /* $Id: cache.h 24250 2007-08-22 19:15:08Z androsyn $ */
#ifndef INCLUDED_CACHE_H #ifndef INCLUDED_CACHE_H
#define INCLUDED_CACHE_H #define INCLUDED_CACHE_H
#include "client.h" #define HELP_MAX 100
#define HELP_MAX 100 #define CACHELINELEN 81
#define CACHEFILELEN 30
#define CACHELINELEN 81 /* two servernames, a gecos, three spaces, ":1", '\0' */
#define CACHEFILELEN 30 #define LINKSLINELEN (HOSTLEN + HOSTLEN + REALLEN + 6)
#define HELP_USER 0x001 #define HELP_USER 0x001
#define HELP_OPER 0x002 #define HELP_OPER 0x002
struct Client; struct Client;
struct cachefile struct cachefile
{ {
char name[CACHEFILELEN]; char name[CACHEFILELEN];
rb_dlink_list contents; rb_dlink_list contents;
int flags; int flags;
}; };
struct cacheline struct cacheline
{ {
char data[CACHELINELEN]; char data[CACHELINELEN];
rb_dlink_node linenode; rb_dlink_node linenode;
}; };
extern struct cachefile *user_motd; extern struct cachefile *user_motd;
extern struct cachefile *oper_motd; extern struct cachefile *oper_motd;
extern struct cacheline *emptyline; extern struct cacheline *emptyline;
extern char user_motd_changed[MAX_DATE_STRING]; extern char user_motd_changed[MAX_DATE_STRING];
extern rb_dlink_list links_cache_list;
extern void init_cache(void);
extern struct cachefile *cache_file(const char *, const char *, int); void init_cache(void);
extern void free_cachefile(struct cachefile *); struct cachefile *cache_file(const char *, const char *, int);
void cache_links(void *unused);
extern void load_help(void); void free_cachefile(struct cachefile *);
extern void send_user_motd(struct Client *); void load_help(void);
extern void send_oper_motd(struct Client *);
void send_user_motd(struct Client *);
void cache_user_motd(void);
struct Dictionary; struct Dictionary;
extern struct Dictionary *help_dict_oper; extern struct Dictionary *help_dict_oper;
extern struct Dictionary *help_dict_user; extern struct Dictionary *help_dict_user;
#endif #endif

View file

@ -1,161 +1,186 @@
/* /*
* ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd). * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
* cache.c - code for caching files * cache.c - code for caching files
* *
* Copyright (C) 2003 Lee Hardy <lee@leeh.co.uk> * Copyright (C) 2003 Lee Hardy <lee@leeh.co.uk>
* Copyright (C) 2003-2005 ircd-ratbox development team * Copyright (C) 2003-2005 ircd-ratbox development team
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are * modification, are permitted provided that the following conditions are
* met: * met:
* *
* 1.Redistributions of source code must retain the above copyright notice, * 1.Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. * this list of conditions and the following disclaimer.
* 2.Redistributions in binary form must reproduce the above copyright * 2.Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3.The name of the author may not be used to endorse or promote products * 3.The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: cache.c 3436 2007-05-02 19:56:40Z jilles $ * $Id: cache.c 25119 2008-03-13 16:57:05Z androsyn $
*/ */
#include "stdinc.h" #include "stdinc.h"
#include "ircd_defs.h" #include "ratbox_lib.h"
#include "common.h" #include "struct.h"
#include "s_conf.h" #include "s_conf.h"
#include "client.h" #include "client.h"
#include "hash.h" #include "hash.h"
#include "cache.h" #include "cache.h"
#include "sprintf_irc.h" #include "match.h"
#include "irc_dictionary.h" #include "ircd.h"
#include "numeric.h" #include "numeric.h"
#include "send.h"
static BlockHeap *cachefile_heap = NULL;
static BlockHeap *cacheline_heap = NULL; struct cachefile *user_motd = NULL;
struct cachefile *oper_motd = NULL;
struct cachefile *user_motd = NULL; struct cacheline *emptyline = NULL;
struct cachefile *oper_motd = NULL; rb_dlink_list links_cache_list;
char user_motd_changed[MAX_DATE_STRING]; char user_motd_changed[MAX_DATE_STRING];
struct Dictionary *help_dict_oper = NULL; struct Dictionary *help_dict_oper = NULL;
struct Dictionary *help_dict_user = NULL; struct Dictionary *help_dict_user = NULL;
/* init_cache() /* init_cache()
* *
* inputs - * inputs -
* outputs - * outputs -
* side effects - inits the file/line cache blockheaps, loads motds * side effects - inits the file/line cache blockheaps, loads motds
*/ */
void void
init_cache(void) init_cache(void)
{ {
cachefile_heap = BlockHeapCreate(sizeof(struct cachefile), CACHEFILE_HEAP_SIZE); /* allocate the emptyline */
cacheline_heap = BlockHeapCreate(sizeof(struct cacheline), CACHELINE_HEAP_SIZE); emptyline = rb_malloc(sizeof(struct cacheline));
emptyline->data[0] = ' ';
user_motd_changed[0] = '\0'; emptyline->data[1] = '\0';
user_motd_changed[0] = '\0';
user_motd = cache_file(MPATH, "ircd.motd", 0);
oper_motd = cache_file(OPATH, "opers.motd", 0); user_motd = cache_file(MPATH, "ircd.motd", 0);
oper_motd = cache_file(OPATH, "opers.motd", 0);
memset(&links_cache_list, 0, sizeof(links_cache_list));
help_dict_oper = irc_dictionary_create(strcasecmp); help_dict_oper = irc_dictionary_create(strcasecmp);
help_dict_user = irc_dictionary_create(strcasecmp); help_dict_user = irc_dictionary_create(strcasecmp);
} }
/* cache_file() /* cache_file()
* *
* inputs - file to cache, files "shortname", flags to set * inputs - file to cache, files "shortname", flags to set
* outputs - pointer to file cached, else NULL * outputs - pointer to file cached, else NULL
* side effects - * side effects -
*/ */
struct cachefile * struct cachefile *
cache_file(const char *filename, const char *shortname, int flags) cache_file(const char *filename, const char *shortname, int flags)
{ {
FILE *in; FILE *in;
struct cachefile *cacheptr; struct cachefile *cacheptr;
struct cacheline *lineptr; struct cacheline *lineptr;
char line[BUFSIZE]; char line[BUFSIZE];
char *p; char *p;
if((in = fopen(filename, "r")) == NULL) if((in = fopen(filename, "r")) == NULL)
return NULL; return NULL;
if(strcmp(shortname, "ircd.motd") == 0)
{ cacheptr = rb_malloc(sizeof(struct cachefile));
struct stat sb;
struct tm *local_tm; rb_strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name));
cacheptr->flags = flags;
if(fstat(fileno(in), &sb) < 0)
return NULL; /* cache the file... */
while(fgets(line, sizeof(line), in) != NULL)
local_tm = localtime(&sb.st_mtime); {
if((p = strpbrk(line, "\r\n")) != NULL)
if(local_tm != NULL) *p = '\0';
rb_snprintf(user_motd_changed, sizeof(user_motd_changed),
"%d/%d/%d %d:%d", if(!EmptyString(line))
local_tm->tm_mday, local_tm->tm_mon + 1, {
1900 + local_tm->tm_year, local_tm->tm_hour, lineptr = rb_malloc(sizeof(struct cacheline));
local_tm->tm_min); rb_strlcpy(lineptr->data, line, sizeof(lineptr->data));
} rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents);
}
cacheptr = BlockHeapAlloc(cachefile_heap); else
rb_dlinkAddTailAlloc(emptyline, &cacheptr->contents);
strlcpy(cacheptr->name, shortname, sizeof(cacheptr->name)); }
cacheptr->flags = flags;
fclose(in);
/* cache the file... */ return cacheptr;
while(fgets(line, sizeof(line), in) != NULL) }
{
if((p = strchr(line, '\n')) != NULL) void
*p = '\0'; cache_links(void *unused)
{
lineptr = BlockHeapAlloc(cacheline_heap); struct Client *target_p;
if(EmptyString(line)) rb_dlink_node *ptr;
strlcpy(lineptr->data, " ", sizeof(lineptr->data)); rb_dlink_node *next_ptr;
else char *links_line;
strlcpy(lineptr->data, line, sizeof(lineptr->data));
rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents); RB_DLINK_FOREACH_SAFE(ptr, next_ptr, links_cache_list.head)
} {
rb_free(ptr->data);
fclose(in); rb_free_rb_dlink_node(ptr);
return cacheptr; }
}
links_cache_list.head = links_cache_list.tail = NULL;
/* free_cachefile() links_cache_list.length = 0;
*
* inputs - cachefile to free RB_DLINK_FOREACH(ptr, global_serv_list.head)
* outputs - {
* side effects - cachefile and its data is free'd target_p = ptr->data;
*/
void /* skip ourselves (done in /links) and hidden servers */
free_cachefile(struct cachefile *cacheptr) if(IsMe(target_p) ||
{ (IsHidden(target_p) && !ConfigServerHide.disable_hidden))
rb_dlink_node *ptr; continue;
rb_dlink_node *next_ptr;
/* if the below is ever modified, change LINKSLINELEN */
if(cacheptr == NULL) links_line = rb_malloc(LINKSLINELEN);
return; rb_snprintf(links_line, LINKSLINELEN, "%s %s :1 %s",
target_p->name, me.name,
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head) target_p->info[0] ? target_p->info :
{ "(Unknown Location)");
BlockHeapFree(cacheline_heap, ptr->data);
} rb_dlinkAddTailAlloc(links_line, &links_cache_list);
}
BlockHeapFree(cachefile_heap, cacheptr); }
}
/* free_cachefile()
*
* inputs - cachefile to free
* outputs -
* side effects - cachefile and its data is free'd
*/
void
free_cachefile(struct cachefile *cacheptr)
{
rb_dlink_node *ptr;
rb_dlink_node *next_ptr;
if(cacheptr == NULL)
return;
RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head)
{
if(ptr->data != emptyline)
rb_free(ptr->data);
}
rb_free(cacheptr);
}
/* load_help() /* load_help()
* *
* inputs - * inputs -
@ -210,65 +235,57 @@ load_help(void)
} }
closedir(helpfile_dir); closedir(helpfile_dir);
} }
/* send_user_motd() /* send_user_motd()
* *
* inputs - client to send motd to * inputs - client to send motd to
* outputs - client is sent motd if exists, else ERR_NOMOTD * outputs - client is sent motd if exists, else ERR_NOMOTD
* side effects - * side effects -
*/ */
void void
send_user_motd(struct Client *source_p) send_user_motd(struct Client *source_p)
{ {
struct cacheline *lineptr; struct cacheline *lineptr;
rb_dlink_node *ptr; rb_dlink_node *ptr;
const char *myname = get_id(&me, source_p); const char *myname = get_id(&me, source_p);
const char *nick = get_id(source_p, source_p); const char *nick = get_id(source_p, source_p);
if(user_motd == NULL || rb_dlink_list_length(&user_motd->contents) == 0)
if(user_motd == NULL || rb_dlink_list_length(&user_motd->contents) == 0) {
{ sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick);
sendto_one(source_p, form_str(ERR_NOMOTD), myname, nick); return;
return; }
} SetCork(source_p);
sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name);
sendto_one(source_p, form_str(RPL_MOTDSTART), myname, nick, me.name);
RB_DLINK_FOREACH(ptr, user_motd->contents.head)
RB_DLINK_FOREACH(ptr, user_motd->contents.head) {
{ lineptr = ptr->data;
lineptr = ptr->data; sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data);
sendto_one(source_p, form_str(RPL_MOTD), myname, nick, lineptr->data); }
} ClearCork(source_p);
sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick);
sendto_one(source_p, form_str(RPL_ENDOFMOTD), myname, nick); }
}
void
/* send_oper_motd() cache_user_motd(void)
* {
* inputs - client to send motd to struct stat sb;
* outputs - client is sent oper motd if exists struct tm *local_tm;
* side effects -
*/ if(stat(MPATH, &sb) == 0)
void {
send_oper_motd(struct Client *source_p) local_tm = localtime(&sb.st_mtime);
{
struct cacheline *lineptr; if(local_tm != NULL)
rb_dlink_node *ptr; {
rb_snprintf(user_motd_changed, sizeof(user_motd_changed),
if(oper_motd == NULL || rb_dlink_list_length(&oper_motd->contents) == 0) "%d/%d/%d %d:%d",
return; local_tm->tm_mday, local_tm->tm_mon + 1,
1900 + local_tm->tm_year, local_tm->tm_hour,
sendto_one(source_p, form_str(RPL_OMOTDSTART), local_tm->tm_min);
me.name, source_p->name); }
}
RB_DLINK_FOREACH(ptr, oper_motd->contents.head) free_cachefile(user_motd);
{ user_motd = cache_file(MPATH, "ircd.motd", 0);
lineptr = ptr->data; }
sendto_one(source_p, form_str(RPL_OMOTD),
me.name, source_p->name, lineptr->data);
}
sendto_one(source_p, form_str(RPL_ENDOFOMOTD),
me.name, source_p->name);
}