]> jfr.im git - solanum.git/blobdiff - src/cache.c
Merge pull request #53 from ShadowNinja/clarify_U+R
[solanum.git] / src / cache.c
index 58a2f8e4ddfbfb5350bc6493e8108e16d40ccc2e..88dab60d6e4908f2068a59749b97e330e73859c1 100644 (file)
@@ -41,6 +41,7 @@
 #include "cache.h"
 #include "irc_dictionary.h"
 #include "numeric.h"
+#include "send.h"
 
 struct cachefile *user_motd = NULL;
 struct cachefile *oper_motd = NULL;
@@ -62,8 +63,8 @@ init_cache(void)
 {
        /* allocate the emptyline */
        emptyline = rb_malloc(sizeof(struct cacheline));
-       emptyline->data[0] = ' ';
-       emptyline->data[1] = '\0';
+       emptyline->data = rb_strdup(" ");
+
        user_motd_changed[0] = '\0';
 
        user_motd = cache_file(MPATH, "ircd.motd", 0);
@@ -74,7 +75,7 @@ init_cache(void)
        help_dict_user = irc_dictionary_create(strcasecmp);
 }
 
-/* 
+/*
  * removes tabs from src, replaces with 8 spaces, and returns the length
  * of the new string.  if the new string would be greater than destlen,
  * it is truncated to destlen - 1
@@ -93,7 +94,7 @@ untabify(char *dest, const char *src, size_t destlen)
                        for(i = 0; i < 8 && x < destlen - 1; i++, x++, d++)
                                *d = ' ';
                        s++;
-               } else 
+               } else
                {
                        *d++ = *s++;
                        x++;
@@ -135,14 +136,26 @@ cache_file(const char *filename, const char *shortname, int flags)
 
                if(!EmptyString(line))
                {
+                       char untabline[BUFSIZE];
+
                        lineptr = rb_malloc(sizeof(struct cacheline));
-                       untabify(lineptr->data, line, sizeof(lineptr->data));
+
+                       untabify(untabline, line, sizeof(untabline));
+                       lineptr->data = rb_strdup(untabline);
+
                        rb_dlinkAddTail(lineptr, &lineptr->linenode, &cacheptr->contents);
                }
                else
                        rb_dlinkAddTailAlloc(emptyline, &cacheptr->contents);
        }
 
+       if (0 == rb_dlink_list_length(&cacheptr->contents))
+       {
+               /* No contents. Don't cache it after all. */
+               rb_free(cacheptr);
+               cacheptr = NULL;
+       }
+
        fclose(in);
        return cacheptr;
 }
@@ -176,8 +189,8 @@ cache_links(void *unused)
                /* if the below is ever modified, change LINKSLINELEN */
                links_line = rb_malloc(LINKSLINELEN);
                rb_snprintf(links_line, LINKSLINELEN, "%s %s :1 %s",
-                          target_p->name, me.name, 
-                          target_p->info[0] ? target_p->info : 
+                          target_p->name, me.name,
+                          target_p->info[0] ? target_p->info :
                            "(Unknown Location)");
 
                rb_dlinkAddTailAlloc(links_line, &links_cache_list);
@@ -202,7 +215,11 @@ free_cachefile(struct cachefile *cacheptr)
        RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cacheptr->contents.head)
        {
                if(ptr->data != emptyline)
-                       rb_free(ptr->data);
+               {
+                       struct cacheline *line = ptr->data;
+                       rb_free(line->data);
+                       rb_free(line);
+               }
        }
 
        rb_free(cacheptr);
@@ -220,7 +237,7 @@ load_help(void)
 {
        DIR *helpfile_dir = NULL;
        struct dirent *ldirent= NULL;
-       char filename[MAXPATHLEN];
+       char filename[PATH_MAX];
        struct cachefile *cacheptr;
        struct DictionaryIter iter;
 
@@ -246,6 +263,8 @@ load_help(void)
 
        while((ldirent = readdir(helpfile_dir)) != NULL)
        {
+               if(ldirent->d_name[0] == '.')
+                       continue;
                rb_snprintf(filename, sizeof(filename), "%s/%s", HPATH, ldirent->d_name);
                cacheptr = cache_file(filename, ldirent->d_name, HELP_OPER);
                irc_dictionary_add(help_dict_oper, cacheptr->name, cacheptr);
@@ -259,6 +278,8 @@ load_help(void)
 
        while((ldirent = readdir(helpfile_dir)) != NULL)
        {
+               if(ldirent->d_name[0] == '.')
+                       continue;
                rb_snprintf(filename, sizeof(filename), "%s/%s", UHPATH, ldirent->d_name);
 
 #if defined(S_ISLNK) && defined(HAVE_LSTAT)
@@ -322,12 +343,12 @@ cache_user_motd(void)
 {
        struct stat sb;
        struct tm *local_tm;
-       
-       if(stat(MPATH, &sb) == 0) 
+
+       if(stat(MPATH, &sb) == 0)
        {
                local_tm = localtime(&sb.st_mtime);
 
-               if(local_tm != NULL) 
+               if(local_tm != NULL)
                {
                        rb_snprintf(user_motd_changed, sizeof(user_motd_changed),
                                 "%d/%d/%d %d:%d",
@@ -335,7 +356,7 @@ cache_user_motd(void)
                                 1900 + local_tm->tm_year, local_tm->tm_hour,
                                 local_tm->tm_min);
                }
-       } 
+       }
        free_cachefile(user_motd);
        user_motd = cache_file(MPATH, "ircd.motd", 0);
 }
@@ -356,7 +377,7 @@ send_oper_motd(struct Client *source_p)
        if(oper_motd == NULL || rb_dlink_list_length(&oper_motd->contents) == 0)
                return;
 
-       sendto_one(source_p, form_str(RPL_OMOTDSTART), 
+       sendto_one(source_p, form_str(RPL_OMOTDSTART),
                   me.name, source_p->name);
 
        RB_DLINK_FOREACH(ptr, oper_motd->contents.head)
@@ -366,6 +387,6 @@ send_oper_motd(struct Client *source_p)
                           me.name, source_p->name, lineptr->data);
        }
 
-       sendto_one(source_p, form_str(RPL_ENDOFOMOTD), 
+       sendto_one(source_p, form_str(RPL_ENDOFOMOTD),
                   me.name, source_p->name);
 }