]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/configreader.cpp
And fix a bug
[user/henk/code/inspircd.git] / src / configreader.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                     E-mail:
7  *              <brain@chatspike.net>
8  *              <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *          the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "inspircd_config.h"
18 #include "configreader.h"
19 #include <string>
20 #include <sstream>
21 #include <iostream>
22 #include <fstream>
23 #include "inspircd.h"
24 #include "inspstring.h"
25 #include "helperfuncs.h"
26 #include "userprocess.h"
27 #include "xline.h"
28
29 extern time_t TIME;
30
31 std::vector<std::string> old_module_names, new_module_names, added_modules, removed_modules;
32
33 ServerConfig::ServerConfig(InspIRCd* Instance) : ServerInstance(Instance)
34 {
35         this->ClearStack();
36         *TempDir = *ServerName = *Network = *ServerDesc = *AdminName = '\0';
37         *HideWhoisServer = *AdminEmail = *AdminNick = *diepass = *restartpass = '\0';
38         *CustomVersion = *motd = *rules = *PrefixQuit = *DieValue = *DNSServer = '\0';
39         *OperOnlyStats = *ModPath = *MyExecutable = *DisabledCommands = *PID = '\0';
40         log_file = NULL;
41         NoUserDns = forcedebug = OperSpyWhois = nofork = HideBans = HideSplits = false;
42         writelog = AllowHalfop = true;
43         dns_timeout = DieDelay = 5;
44         MaxTargets = 20;
45         NetBufferSize = 10240;
46         SoftLimit = MAXCLIENTS;
47         MaxConn = SOMAXCONN;
48         MaxWhoResults = 100;
49         debugging = 0;
50         LogLevel = DEFAULT;
51         maxbans.clear();
52 }
53
54 void ServerConfig::ClearStack()
55 {
56         include_stack.clear();
57 }
58
59 Module* ServerConfig::GetIOHook(int port)
60 {
61         std::map<int,Module*>::iterator x = IOHookModule.find(port);
62         return (x != IOHookModule.end() ? x->second : NULL);
63 }
64
65 bool ServerConfig::AddIOHook(int port, Module* iomod)
66 {
67         if (!GetIOHook(port))
68         {
69                 IOHookModule[port] = iomod;
70                 return true;
71         }
72         else
73         {
74                 ModuleException err("Port already hooked by another module");
75                 throw(err);
76                 return false;
77         }
78 }
79
80 bool ServerConfig::DelIOHook(int port)
81 {
82         std::map<int,Module*>::iterator x = IOHookModule.find(port);
83         if (x != IOHookModule.end())
84         {
85                 IOHookModule.erase(x);
86                 return true;
87         }
88         return false;
89 }
90
91 bool ServerConfig::CheckOnce(char* tag, bool bail, userrec* user)
92 {
93         int count = ConfValueEnum(this->config_data, tag);
94         
95         if (count > 1)
96         {
97                 if (bail)
98                 {
99                         printf("There were errors in your configuration:\nYou have more than one <%s> tag, this is not permitted.\n",tag);
100                         InspIRCd::Exit(ERROR);
101                 }
102                 else
103                 {
104                         if (user)
105                         {
106                                 user->WriteServ("There were errors in your configuration:");
107                                 user->WriteServ("You have more than one <%s> tag, this is not permitted.\n",tag);
108                         }
109                         else
110                         {
111                                 ServerInstance->WriteOpers("There were errors in the configuration file:");
112                                 ServerInstance->WriteOpers("You have more than one <%s> tag, this is not permitted.\n",tag);
113                         }
114                 }
115                 return false;
116         }
117         if (count < 1)
118         {
119                 if (bail)
120                 {
121                         printf("There were errors in your configuration:\nYou have not defined a <%s> tag, this is required.\n",tag);
122                         InspIRCd::Exit(ERROR);
123                 }
124                 else
125                 {
126                         if (user)
127                         {
128                                 user->WriteServ("There were errors in your configuration:");
129                                 user->WriteServ("You have not defined a <%s> tag, this is required.",tag);
130                         }
131                         else
132                         {
133                                 ServerInstance->WriteOpers("There were errors in the configuration file:");
134                                 ServerInstance->WriteOpers("You have not defined a <%s> tag, this is required.",tag);
135                         }
136                 }
137                 return false;
138         }
139         return true;
140 }
141
142 bool NoValidation(ServerConfig* conf, const char* tag, const char* value, void* data)
143 {
144         log(DEBUG,"No validation for <%s:%s>",tag,value);
145         return true;
146 }
147
148 bool ValidateTempDir(ServerConfig* conf, const char* tag, const char* value, void* data)
149 {
150         char* x = (char*)data;
151         if (!*x)
152                 strlcpy(x,"/tmp",1024);
153         return true;
154 }
155  
156 bool ValidateMaxTargets(ServerConfig* conf, const char* tag, const char* value, void* data)
157 {
158         int* x = (int*)data;
159         if ((*x < 0) || (*x > 31))
160         {
161                 log(DEFAULT,"WARNING: <options:maxtargets> value is greater than 31 or less than 0, set to 20.");
162                 *x = 20;
163         }
164         return true;
165 }
166
167 bool ValidateSoftLimit(ServerConfig* conf, const char* tag, const char* value, void* data)
168 {
169         int* x = (int*)data;    
170         if ((*x < 1) || (*x > MAXCLIENTS))
171         {
172                 log(DEFAULT,"WARNING: <options:softlimit> value is greater than %d or less than 0, set to %d.",MAXCLIENTS,MAXCLIENTS);
173                 *x = MAXCLIENTS;
174         }
175         return true;
176 }
177
178 bool ValidateMaxConn(ServerConfig* conf, const char* tag, const char* value, void* data)
179 {
180         int* x = (int*)data;    
181         if (*x > SOMAXCONN)
182                 log(DEFAULT,"WARNING: <options:somaxconn> value may be higher than the system-defined SOMAXCONN value!");
183         if (!*x)
184                 *x = SOMAXCONN;
185         return true;
186 }
187
188 bool ValidateDnsTimeout(ServerConfig* conf, const char* tag, const char* value, void* data)
189 {
190         int* x = (int*)data;
191         if (!*x)
192                 *x = 5;
193         return true;
194 }
195
196 bool InitializeDisabledCommands(const char* data, InspIRCd* ServerInstance)
197 {
198         std::stringstream dcmds(data);
199         std::string thiscmd;
200
201         /* Enable everything first */
202         for (nspace::hash_map<std::string,command_t*>::iterator x = ServerInstance->Parser->cmdlist.begin(); x != ServerInstance->Parser->cmdlist.end(); x++)
203                 x->second->Disable(false);
204
205         /* Now disable all the ones which the user wants disabled */
206         while (dcmds >> thiscmd)
207         {
208                 nspace::hash_map<std::string,command_t*>::iterator cm = ServerInstance->Parser->cmdlist.find(thiscmd);
209                 if (cm != ServerInstance->Parser->cmdlist.end())
210                 {
211                         log(DEBUG,"Disabling command '%s'",cm->second->command.c_str());
212                         cm->second->Disable(true);
213                 }
214         }
215         return true;
216 }
217
218 bool ValidateDnsServer(ServerConfig* conf, const char* tag, const char* value, void* data)
219 {
220         char* x = (char*)data;
221         if (!*x)
222         {
223                 // attempt to look up their nameserver from /etc/resolv.conf
224                 log(DEFAULT,"WARNING: <dns:server> not defined, attempting to find working server in /etc/resolv.conf...");
225                 ifstream resolv("/etc/resolv.conf");
226                 std::string nameserver;
227                 bool found_server = false;
228
229                 if (resolv.is_open())
230                 {
231                         while (resolv >> nameserver)
232                         {
233                                 if ((nameserver == "nameserver") && (!found_server))
234                                 {
235                                         resolv >> nameserver;
236                                         strlcpy(x,nameserver.c_str(),MAXBUF);
237                                         found_server = true;
238                                         log(DEFAULT,"<dns:server> set to '%s' as first resolver in /etc/resolv.conf.",nameserver.c_str());
239                                 }
240                         }
241
242                         if (!found_server)
243                         {
244                                 log(DEFAULT,"/etc/resolv.conf contains no viable nameserver entries! Defaulting to nameserver '127.0.0.1'!");
245                                 strlcpy(x,"127.0.0.1",MAXBUF);
246                         }
247                 }
248                 else
249                 {
250                         log(DEFAULT,"/etc/resolv.conf can't be opened! Defaulting to nameserver '127.0.0.1'!");
251                         strlcpy(x,"127.0.0.1",MAXBUF);
252                 }
253         }
254         return true;
255 }
256
257 bool ValidateModPath(ServerConfig* conf, const char* tag, const char* value, void* data)
258 {
259         char* x = (char*)data;  
260         if (!*x)
261                 strlcpy(x,MOD_PATH,MAXBUF);
262         return true;
263 }
264
265
266 bool ValidateServerName(ServerConfig* conf, const char* tag, const char* value, void* data)
267 {
268         char* x = (char*)data;
269         if (!strchr(x,'.'))
270         {
271                 log(DEFAULT,"WARNING: <server:name> '%s' is not a fully-qualified domain name. Changed to '%s%c'",x,x,'.');
272                 charlcat(x,'.',MAXBUF);
273         }
274         //strlower(x);
275         return true;
276 }
277
278 bool ValidateNetBufferSize(ServerConfig* conf, const char* tag, const char* value, void* data)
279 {
280         if ((!conf->NetBufferSize) || (conf->NetBufferSize > 65535) || (conf->NetBufferSize < 1024))
281         {
282                 log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
283                 conf->NetBufferSize = 10240;
284         }
285         return true;
286 }
287
288 bool ValidateMaxWho(ServerConfig* conf, const char* tag, const char* value, void* data)
289 {
290         if ((!conf->MaxWhoResults) || (conf->MaxWhoResults > 65535) || (conf->MaxWhoResults < 1))
291         {
292                 log(DEFAULT,"No MaxWhoResults specified or size out of range, setting to default of 128.");
293                 conf->MaxWhoResults = 128;
294         }
295         return true;
296 }
297
298 bool ValidateLogLevel(ServerConfig* conf, const char* tag, const char* value, void* data)
299 {
300         const char* dbg = (const char*)data;
301         conf->LogLevel = DEFAULT;
302         if (!strcmp(dbg,"debug"))
303         {
304                 conf->LogLevel = DEBUG;
305                 conf->debugging = 1;
306         }
307         else if (!strcmp(dbg,"verbose"))
308                 conf->LogLevel = VERBOSE;
309         else if (!strcmp(dbg,"default"))
310                 conf->LogLevel = DEFAULT;
311         else if (!strcmp(dbg,"sparse"))
312                 conf->LogLevel = SPARSE;
313         else if (!strcmp(dbg,"none"))
314                 conf->LogLevel = NONE;
315         return true;
316 }
317
318 bool ValidateMotd(ServerConfig* conf, const char* tag, const char* value, void* data)
319 {
320         conf->ReadFile(conf->MOTD,conf->motd);
321         return true;
322 }
323
324 bool ValidateRules(ServerConfig* conf, const char* tag, const char* value, void* data)
325 {
326         conf->ReadFile(conf->RULES,conf->rules);
327         return true;
328 }
329
330 /* Callback called before processing the first <connect> tag
331  */
332 bool InitConnect(ServerConfig* conf, const char* tag)
333 {
334         log(DEFAULT,"Reading connect classes...");
335         conf->Classes.clear();
336         return true;
337 }
338
339 /* Callback called to process a single <connect> tag
340  */
341 bool DoConnect(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
342 {
343         ConnectClass c;
344         char* allow = (char*)values[0]; /* Yeah, there are a lot of values. Live with it. */
345         char* deny = (char*)values[1];
346         char* password = (char*)values[2];
347         int* timeout = (int*)values[3];
348         int* pingfreq = (int*)values[4];
349         int* flood = (int*)values[5];
350         int* threshold = (int*)values[6];
351         int* sendq = (int*)values[7];
352         int* recvq = (int*)values[8];
353         int* localmax = (int*)values[9];
354         int* globalmax = (int*)values[10];
355
356         if (*allow)
357         {
358                 c.host = allow;
359                 c.type = CC_ALLOW;
360                 c.pass = password;
361                 c.registration_timeout = *timeout;
362                 c.pingtime = *pingfreq;
363                 c.flood = *flood;
364                 c.threshold = *threshold;
365                 c.sendqmax = *sendq;
366                 c.recvqmax = *recvq;
367                 c.maxlocal = *localmax;
368                 c.maxglobal = *globalmax;
369
370
371                 if (c.maxlocal == 0)
372                         c.maxlocal = 3;
373                 if (c.maxglobal == 0)
374                         c.maxglobal = 3;
375                 if (c.threshold == 0)
376                 {
377                         c.threshold = 1;
378                         c.flood = 999;
379                         log(DEFAULT,"Warning: Connect allow line '%s' has no flood/threshold settings. Setting this tag to 999 lines in 1 second.",c.host.c_str());
380                 }
381                 if (c.sendqmax == 0)
382                         c.sendqmax = 262114;
383                 if (c.recvqmax == 0)
384                         c.recvqmax = 4096;
385                 if (c.registration_timeout == 0)
386                         c.registration_timeout = 90;
387                 if (c.pingtime == 0)
388                         c.pingtime = 120;
389                 conf->Classes.push_back(c);
390         }
391         else
392         {
393                 c.host = deny;
394                 c.type = CC_DENY;
395                 conf->Classes.push_back(c);
396                 log(DEBUG,"Read connect class type DENY, host=%s",deny);
397         }
398
399         return true;
400 }
401
402 /* Callback called when there are no more <connect> tags
403  */
404 bool DoneConnect(ServerConfig* conf, const char* tag)
405 {
406         log(DEBUG,"DoneConnect called for tag: %s",tag);
407         return true;
408 }
409
410 /* Callback called before processing the first <uline> tag
411  */
412 bool InitULine(ServerConfig* conf, const char* tag)
413 {
414         conf->ulines.clear();
415         return true;
416 }
417
418 /* Callback called to process a single <uline> tag
419  */
420 bool DoULine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
421 {
422         char* server = (char*)values[0];
423         log(DEBUG,"Read ULINE '%s'",server);
424         conf->ulines.push_back(server);
425         return true;
426 }
427
428 /* Callback called when there are no more <uline> tags
429  */
430 bool DoneULine(ServerConfig* conf, const char* tag)
431 {
432         return true;
433 }
434
435 /* Callback called before processing the first <module> tag
436  */
437 bool InitModule(ServerConfig* conf, const char* tag)
438 {
439         old_module_names.clear();
440         new_module_names.clear();
441         added_modules.clear();
442         removed_modules.clear();
443         for (std::vector<std::string>::iterator t = conf->module_names.begin(); t != conf->module_names.end(); t++)
444         {
445                 old_module_names.push_back(*t);
446         }
447         return true;
448 }
449
450 /* Callback called to process a single <module> tag
451  */
452 bool DoModule(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
453 {
454         char* modname = (char*)values[0];
455         new_module_names.push_back(modname);
456         return true;
457 }
458
459 /* Callback called when there are no more <module> tags
460  */
461 bool DoneModule(ServerConfig* conf, const char* tag)
462 {
463         // now create a list of new modules that are due to be loaded
464         // and a seperate list of modules which are due to be unloaded
465         for (std::vector<std::string>::iterator _new = new_module_names.begin(); _new != new_module_names.end(); _new++)
466         {
467                 bool added = true;
468
469                 for (std::vector<std::string>::iterator old = old_module_names.begin(); old != old_module_names.end(); old++)
470                 {
471                         if (*old == *_new)
472                                 added = false;
473                 }
474
475                 if (added)
476                         added_modules.push_back(*_new);
477         }
478
479         for (std::vector<std::string>::iterator oldm = old_module_names.begin(); oldm != old_module_names.end(); oldm++)
480         {
481                 bool removed = true;
482                 for (std::vector<std::string>::iterator newm = new_module_names.begin(); newm != new_module_names.end(); newm++)
483                 {
484                         if (*newm == *oldm)
485                                 removed = false;
486                 }
487
488                 if (removed)
489                         removed_modules.push_back(*oldm);
490         }
491         return true;
492 }
493
494 /* Callback called before processing the first <banlist> tag
495  */
496 bool InitMaxBans(ServerConfig* conf, const char* tag)
497 {
498         conf->maxbans.clear();
499         return true;
500 }
501
502 /* Callback called to process a single <banlist> tag
503  */
504 bool DoMaxBans(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
505 {
506         char* channel = (char*)values[0];
507         int* limit = (int*)values[1];
508         conf->maxbans[channel] = *limit;
509         return true;
510 }
511
512 /* Callback called when there are no more <banlist> tags.
513  */
514 bool DoneMaxBans(ServerConfig* conf, const char* tag)
515 {
516         return true;
517 }
518
519 void ServerConfig::Read(bool bail, userrec* user)
520 {
521         char debug[MAXBUF];             /* Temporary buffer for debugging value */
522         char* data[12];                 /* Temporary buffers for reading multiple occurance tags into */
523         void* ptr[12];                  /* Temporary pointers for passing to callbacks */
524         int r_i[12];                    /* Temporary array for casting */
525         int rem = 0, add = 0;           /* Number of modules added, number of modules removed */
526         std::ostringstream errstr;      /* String stream containing the error output */
527
528         /* These tags MUST occur and must ONLY occur once in the config file */
529         static char* Once[] = { "server", "admin", "files", "power", "options", "pid", NULL };
530
531         /* These tags can occur ONCE or not at all */
532         static InitialConfig Values[] = {
533                 {"options",             "softlimit",                    &this->SoftLimit,               DT_INTEGER, ValidateSoftLimit},
534                 {"options",             "somaxconn",                    &this->MaxConn,                 DT_INTEGER, ValidateMaxConn},
535                 {"server",              "name",                         &this->ServerName,              DT_CHARPTR, ValidateServerName},
536                 {"server",              "description",                  &this->ServerDesc,              DT_CHARPTR, NoValidation},
537                 {"server",              "network",                      &this->Network,                 DT_CHARPTR, NoValidation},
538                 {"admin",               "name",                         &this->AdminName,               DT_CHARPTR, NoValidation},
539                 {"admin",               "email",                        &this->AdminEmail,              DT_CHARPTR, NoValidation},
540                 {"admin",               "nick",                         &this->AdminNick,               DT_CHARPTR, NoValidation},
541                 {"files",               "motd",                         &this->motd,                    DT_CHARPTR, ValidateMotd},
542                 {"files",               "rules",                        &this->rules,                   DT_CHARPTR, ValidateRules},
543                 {"power",               "diepass",                      &this->diepass,                 DT_CHARPTR, NoValidation},      
544                 {"power",               "pauseval",                     &this->DieDelay,                DT_INTEGER, NoValidation},
545                 {"power",               "restartpass",                  &this->restartpass,             DT_CHARPTR, NoValidation},
546                 {"options",             "prefixquit",                   &this->PrefixQuit,              DT_CHARPTR, NoValidation},
547                 {"die",                 "value",                        &this->DieValue,                DT_CHARPTR, NoValidation},
548                 {"options",             "loglevel",                     &debug,                         DT_CHARPTR, ValidateLogLevel},
549                 {"options",             "netbuffersize",                &this->NetBufferSize,           DT_INTEGER, ValidateNetBufferSize},
550                 {"options",             "maxwho",                       &this->MaxWhoResults,           DT_INTEGER, ValidateMaxWho},
551                 {"options",             "allowhalfop",                  &this->AllowHalfop,             DT_BOOLEAN, NoValidation},
552                 {"dns",                 "server",                       &this->DNSServer,               DT_CHARPTR, ValidateDnsServer},
553                 {"dns",                 "timeout",                      &this->dns_timeout,             DT_INTEGER, ValidateDnsTimeout},
554                 {"options",             "moduledir",                    &this->ModPath,                 DT_CHARPTR, ValidateModPath},
555                 {"disabled",            "commands",                     &this->DisabledCommands,        DT_CHARPTR, NoValidation},
556                 {"options",             "operonlystats",                &this->OperOnlyStats,           DT_CHARPTR, NoValidation},
557                 {"options",             "customversion",                &this->CustomVersion,           DT_CHARPTR, NoValidation},
558                 {"options",             "hidesplits",                   &this->HideSplits,              DT_BOOLEAN, NoValidation},
559                 {"options",             "hidebans",                     &this->HideBans,                DT_BOOLEAN, NoValidation},
560                 {"options",             "hidewhois",                    &this->HideWhoisServer,         DT_CHARPTR, NoValidation},
561                 {"options",             "operspywhois",                 &this->OperSpyWhois,            DT_BOOLEAN, NoValidation},
562                 {"options",             "tempdir",                      &this->TempDir,                 DT_CHARPTR, ValidateTempDir},
563                 {"options",             "nouserdns",                    &this->NoUserDns,               DT_BOOLEAN, NoValidation},
564                 {"options",             "syntaxhints",                  &this->SyntaxHints,             DT_BOOLEAN, NoValidation},
565                 {"pid",                 "file",                         &this->PID,                     DT_CHARPTR, NoValidation},
566                 {NULL}
567         };
568
569         /* These tags can occur multiple times, and therefore they have special code to read them
570          * which is different to the code for reading the singular tags listed above.
571          */
572         static MultiConfig MultiValues[] = {
573
574                 {"connect",
575                                 {"allow",       "deny",         "password",     "timeout",      "pingfreq",     "flood",
576                                 "threshold",    "sendq",        "recvq",        "localmax",     "globalmax",    NULL},
577                                 {DT_CHARPTR,    DT_CHARPTR,     DT_CHARPTR,     DT_INTEGER,     DT_INTEGER,     DT_INTEGER,
578                                  DT_INTEGER,    DT_INTEGER,     DT_INTEGER,     DT_INTEGER,     DT_INTEGER},
579                                 InitConnect, DoConnect, DoneConnect},
580
581                 {"uline",
582                                 {"server",      NULL},
583                                 {DT_CHARPTR},
584                                 InitULine,DoULine,DoneULine},
585
586                 {"banlist",
587                                 {"chan",        "limit",        NULL},
588                                 {DT_CHARPTR,    DT_INTEGER},
589                                 InitMaxBans, DoMaxBans, DoneMaxBans},
590
591                 {"module",
592                                 {"name",        NULL},
593                                 {DT_CHARPTR},
594                                 InitModule, DoModule, DoneModule},
595
596                 {"badip",
597                                 {"reason",      "ipmask",       NULL},
598                                 {DT_CHARPTR,    DT_CHARPTR},
599                                 InitXLine, DoZLine, DoneXLine},
600
601                 {"badnick",
602                                 {"reason",      "nick",         NULL},
603                                 {DT_CHARPTR,    DT_CHARPTR},
604                                 InitXLine, DoQLine, DoneXLine},
605
606                 {"badhost",
607                                 {"reason",      "host",         NULL},
608                                 {DT_CHARPTR,    DT_CHARPTR},
609                                 InitXLine, DoKLine, DoneXLine},
610
611                 {"exception",
612                                 {"reason",      "host",         NULL},
613                                 {DT_CHARPTR,    DT_CHARPTR},
614                                 InitXLine, DoELine, DoneXLine},
615
616                 {"type",
617                                 {"name",        "classes",      NULL},
618                                 {DT_CHARPTR,    DT_CHARPTR},
619                                 InitTypes, DoType, DoneClassesAndTypes},
620
621                 {"class",
622                                 {"name",        "commands",     NULL},
623                                 {DT_CHARPTR,    DT_CHARPTR},
624                                 InitClasses, DoClass, DoneClassesAndTypes},
625
626                 {NULL}
627         };
628
629         include_stack.clear();
630
631         /* Load and parse the config file, if there are any errors then explode */
632         
633         /* Make a copy here so if it fails then we can carry on running with an unaffected config */
634         ConfigDataHash newconfig;
635         
636         if (this->LoadConf(newconfig, CONFIG_FILE, errstr))
637         {
638                 /* If we succeeded, set the ircd config to the new one */
639                 this->config_data = newconfig;
640                 
641 /*              int c = 1;
642                 std::string last;
643                 
644                 for(ConfigDataHash::const_iterator i = this->config_data.begin(); i != this->config_data.end(); i++)
645                 {
646                         c = (i->first != last) ? 1 : c+1;
647                         last = i->first;
648                         
649                         std::cout << "[" << i->first << " " << c << "/" << this->config_data.count(i->first) << "]" << std::endl;
650                         
651                         for(KeyValList::const_iterator j = i->second.begin(); j != i->second.end(); j++)
652                                 std::cout << "\t" << j->first << " = " << j->second << std::endl;
653                         
654                         std::cout << "[/" << i->first << " " << c << "/" << this->config_data.count(i->first) << "]" << std::endl;
655                 }
656  */     }
657         else
658         {
659                 log(DEFAULT, "There were errors in your configuration:\n%s", errstr.str().c_str());
660
661                 if (bail)
662                 {
663                         /* Unneeded because of the log() aboive? */
664                         printf("There were errors in your configuration:\n%s",errstr.str().c_str());
665                         InspIRCd::Exit(ERROR);
666                 }
667                 else
668                 {
669                         std::string errors = errstr.str();
670                         std::string::size_type start;
671                         unsigned int prefixlen;
672                         
673                         start = 0;
674                         /* ":ServerInstance->Config->ServerName NOTICE user->nick :" */
675                         prefixlen = strlen(this->ServerName) + strlen(user->nick) + 11;
676         
677                         if (user)
678                         {
679                                 user->WriteServ("NOTICE %s :There were errors in the configuration file:",user->nick);
680                                 
681                                 while(start < errors.length())
682                                 {
683                                         user->WriteServ("NOTICE %s :%s",user->nick, errors.substr(start, 510 - prefixlen).c_str());
684                                         start += 510 - prefixlen;
685                                 }
686                         }
687                         else
688                         {
689                                 ServerInstance->WriteOpers("There were errors in the configuration file:");
690                                 
691                                 while(start < errors.length())
692                                 {
693                                         ServerInstance->WriteOpers(errors.substr(start, 360).c_str());
694                                         start += 360;
695                                 }
696                         }
697
698                         return;
699                 }
700         }
701
702         /* Check we dont have more than one of singular tags, or any of them missing
703          */
704         for (int Index = 0; Once[Index]; Index++)
705                 if (!CheckOnce(Once[Index],bail,user))
706                         return;
707
708         /* Read the values of all the tags which occur once or not at all, and call their callbacks.
709          */
710         for (int Index = 0; Values[Index].tag; Index++)
711         {
712                 int* val_i = (int*) Values[Index].val;
713                 char* val_c = (char*) Values[Index].val;
714
715                 switch (Values[Index].datatype)
716                 {
717                         case DT_CHARPTR:
718                                 /* Assuming MAXBUF here, potentially unsafe */
719                                 ConfValue(this->config_data, Values[Index].tag, Values[Index].value, 0, val_c, MAXBUF);
720                         break;
721
722                         case DT_INTEGER:
723                                 ConfValueInteger(this->config_data, Values[Index].tag, Values[Index].value, 0, *val_i);
724                         break;
725
726                         case DT_BOOLEAN:
727                                 *val_i = ConfValueBool(this->config_data, Values[Index].tag, Values[Index].value, 0);
728                         break;
729
730                         case DT_NOTHING:
731                         break;
732                 }
733
734                 Values[Index].validation_function(this, Values[Index].tag, Values[Index].value, Values[Index].val);
735         }
736
737         /* Claim memory for use when reading multiple tags
738          */
739         for (int n = 0; n < 12; n++)
740                 data[n] = new char[MAXBUF];
741
742         /* Read the multiple-tag items (class tags, connect tags, etc)
743          * and call the callbacks associated with them. We have three
744          * callbacks for these, a 'start', 'item' and 'end' callback.
745          */
746         
747         /* XXX - Make this use ConfValueInteger and so on */
748         for (int Index = 0; MultiValues[Index].tag; Index++)
749         {
750                 MultiValues[Index].init_function(this, MultiValues[Index].tag);
751
752                 int number_of_tags = ConfValueEnum(this->config_data, MultiValues[Index].tag);
753
754                 for (int tagnum = 0; tagnum < number_of_tags; tagnum++)
755                 {
756                         for (int valuenum = 0; MultiValues[Index].items[valuenum]; valuenum++)
757                         {
758                                 ConfValue(this->config_data, MultiValues[Index].tag, MultiValues[Index].items[valuenum], tagnum, data[valuenum], MAXBUF);
759
760                                 switch (MultiValues[Index].datatype[valuenum])
761                                 {
762                                         case DT_CHARPTR:
763                                                 ptr[valuenum] = data[valuenum];
764                                         break;
765                                         case DT_INTEGER:
766                                                 r_i[valuenum] = atoi(data[valuenum]);
767                                                 ptr[valuenum] = &r_i[valuenum];
768                                         break;
769                                         case DT_BOOLEAN:
770                                                 r_i[valuenum] = ((*data[valuenum] == tolower('y')) || (*data[valuenum] == tolower('t')) || (*data[valuenum] == '1'));
771                                                 ptr[valuenum] = &r_i[valuenum];
772                                         break;
773                                         default:
774                                         break;
775                                 }
776                         }
777                         MultiValues[Index].validation_function(this, MultiValues[Index].tag, (char**)MultiValues[Index].items, ptr, MultiValues[Index].datatype);
778                 }
779
780                 MultiValues[Index].finish_function(this, MultiValues[Index].tag);
781         }
782
783         /* Free any memory we claimed
784          */
785         for (int n = 0; n < 12; n++)
786                 delete[] data[n];
787
788         // write once here, to try it out and make sure its ok
789         ServerInstance->WritePID(this->PID);
790
791         log(DEFAULT,"Done reading configuration file, InspIRCd is now starting.");
792
793         /* If we're rehashing, let's load any new modules, and unload old ones
794          */
795         if (!bail)
796         {
797                 ServerInstance->stats->BoundPortCount = ServerInstance->BindPorts(false);
798
799                 if (!removed_modules.empty())
800                         for (std::vector<std::string>::iterator removing = removed_modules.begin(); removing != removed_modules.end(); removing++)
801                         {
802                                 if (ServerInstance->UnloadModule(removing->c_str()))
803                                 {
804                                         ServerInstance->WriteOpers("*** REHASH UNLOADED MODULE: %s",removing->c_str());
805
806                                         if (user)
807                                                 user->WriteServ("973 %s %s :Module %s successfully unloaded.",user->nick, removing->c_str(), removing->c_str());
808
809                                         rem++;
810                                 }
811                                 else
812                                 {
813                                         if (user)
814                                                 user->WriteServ("972 %s %s :Failed to unload module %s: %s",user->nick, removing->c_str(), removing->c_str(), ServerInstance->ModuleError());
815                                 }
816                         }
817
818                 if (!added_modules.empty())
819                 for (std::vector<std::string>::iterator adding = added_modules.begin(); adding != added_modules.end(); adding++)
820                 {
821                         if (ServerInstance->LoadModule(adding->c_str()))
822                         {
823                                 ServerInstance->WriteOpers("*** REHASH LOADED MODULE: %s",adding->c_str());
824
825                                 if (user)
826                                         user->WriteServ("975 %s %s :Module %s successfully loaded.",user->nick, adding->c_str(), adding->c_str());
827
828                                 add++;
829                         }
830                         else
831                         {
832                                 if (user)
833                                         user->WriteServ("974 %s %s :Failed to load module %s: %s",user->nick, adding->c_str(), adding->c_str(), ServerInstance->ModuleError());
834                         }
835                 }
836
837                 log(DEFAULT,"Successfully unloaded %lu of %lu modules and loaded %lu of %lu modules.",(unsigned long)rem,(unsigned long)removed_modules.size(),(unsigned long)add,(unsigned long)added_modules.size());
838         }
839 }
840
841 bool ServerConfig::LoadConf(ConfigDataHash &target, const char* filename, std::ostringstream &errorstream)
842 {
843         std::ifstream conf(filename);
844         std::string line;
845         char ch;
846         long linenumber;
847         bool in_tag;
848         bool in_quote;
849         bool in_comment;
850         
851         linenumber = 1;
852         in_tag = false;
853         in_quote = false;
854         in_comment = false;
855         
856         /* Check if the file open failed first */
857         if (!conf)
858         {
859                 errorstream << "LoadConf: Couldn't open config file: " << filename << std::endl;
860                 return false;
861         }
862         
863         /* Fix the chmod of the file to restrict it to the current user and group */
864         chmod(filename,0600);
865         
866         for (unsigned int t = 0; t < include_stack.size(); t++)
867         {
868                 if (std::string(filename) == include_stack[t])
869                 {
870                         errorstream << "File " << filename << " is included recursively (looped inclusion)." << std::endl;
871                         return false;
872                 }
873         }
874         
875         /* It's not already included, add it to the list of files we've loaded */
876         include_stack.push_back(filename);
877         
878         /* Start reading characters... */       
879         while(conf.get(ch))
880         {
881                 /*
882                  * Here we try and get individual tags on separate lines,
883                  * this would be so easy if we just made people format
884                  * their config files like that, but they don't so...
885                  * We check for a '<' and then know the line is over when
886                  * we get a '>' not inside quotes. If we find two '<' and
887                  * no '>' then die with an error.
888                  */
889                 
890                 if((ch == '#') && !in_quote)
891                         in_comment = true;
892                 
893                 if(((ch == '\n') || (ch == '\r')) && in_quote)
894                 {
895                         errorstream << "Got a newline within a quoted section, this is probably a typo: " << filename << ":" << linenumber << std::endl;
896                         return false;
897                 }
898                 
899                 switch(ch)
900                 {
901                         case '\n':
902                                 linenumber++;
903                         case '\r':
904                                 in_comment = false;
905                         case '\0':
906                                 continue;
907                         case '\t':
908                                 ch = ' ';
909                 }
910                 
911                 if(in_comment)
912                         continue;
913
914                 /* XXX: Added by Brain, May 1st 2006 - Escaping of characters.
915                  * Note that this WILL NOT usually allow insertion of newlines,
916                  * because a newline is two characters long. Use it primarily to
917                  * insert the " symbol.
918                  *
919                  * Note that this also involves a further check when parsing the line,
920                  * which can be found below.
921                  */
922                 if ((ch == '\\') && (in_quote) && (in_tag))
923                 {
924                         line += ch;
925                         log(DEBUG,"Escape sequence in config line.");
926                         char real_character;
927                         if (conf.get(real_character))
928                         {
929                                 log(DEBUG,"Escaping %c", real_character);
930                                 line += real_character;
931                                 continue;
932                         }
933                         else
934                         {
935                                 errorstream << "End of file after a \\, what did you want to escape?: " << filename << ":" << linenumber << std::endl;
936                                 return false;
937                         }
938                 }
939
940                 line += ch;
941                 
942                 if(ch == '<')
943                 {
944                         if(in_tag)
945                         {
946                                 if(!in_quote)
947                                 {
948                                         errorstream << "Got another opening < when the first one wasn't closed: " << filename << ":" << linenumber << std::endl;
949                                         return false;
950                                 }
951                         }
952                         else
953                         {
954                                 if(in_quote)
955                                 {
956                                         errorstream << "We're in a quote but outside a tag, interesting. " << filename << ":" << linenumber << std::endl;
957                                         return false;
958                                 }
959                                 else
960                                 {
961                                         // errorstream << "Opening new config tag on line " << linenumber << std::endl;
962                                         in_tag = true;
963                                 }
964                         }
965                 }
966                 else if(ch == '"')
967                 {
968                         if(in_tag)
969                         {
970                                 if(in_quote)
971                                 {
972                                         // errorstream << "Closing quote in config tag on line " << linenumber << std::endl;
973                                         in_quote = false;
974                                 }
975                                 else
976                                 {
977                                         // errorstream << "Opening quote in config tag on line " << linenumber << std::endl;
978                                         in_quote = true;
979                                 }
980                         }
981                         else
982                         {
983                                 if(in_quote)
984                                 {
985                                         errorstream << "Found a (closing) \" outside a tag: " << filename << ":" << linenumber << std::endl;
986                                 }
987                                 else
988                                 {
989                                         errorstream << "Found a (opening) \" outside a tag: " << filename << ":" << linenumber << std::endl;
990                                 }
991                         }
992                 }
993                 else if(ch == '>')
994                 {
995                         if(!in_quote)
996                         {
997                                 if(in_tag)
998                                 {
999                                         // errorstream << "Closing config tag on line " << linenumber << std::endl;
1000                                         in_tag = false;
1001
1002                                         /*
1003                                          * If this finds an <include> then ParseLine can simply call
1004                                          * LoadConf() and load the included config into the same ConfigDataHash
1005                                          */
1006                                         
1007                                         if(!this->ParseLine(target, line, linenumber, errorstream))
1008                                                 return false;
1009                                         
1010                                         line.clear();
1011                                 }
1012                                 else
1013                                 {
1014                                         errorstream << "Got a closing > when we weren't inside a tag: " << filename << ":" << linenumber << std::endl;
1015                                         return false;
1016                                 }
1017                         }
1018                 }
1019         }
1020         
1021         return true;
1022 }
1023
1024 bool ServerConfig::LoadConf(ConfigDataHash &target, const std::string &filename, std::ostringstream &errorstream)
1025 {
1026         return this->LoadConf(target, filename.c_str(), errorstream);
1027 }
1028
1029 bool ServerConfig::ParseLine(ConfigDataHash &target, std::string &line, long linenumber, std::ostringstream &errorstream)
1030 {
1031         std::string tagname;
1032         std::string current_key;
1033         std::string current_value;
1034         KeyValList results;
1035         bool got_name;
1036         bool got_key;
1037         bool in_quote;
1038         
1039         got_name = got_key = in_quote = false;
1040         
1041         // std::cout << "ParseLine(data, '" << line << "', " << linenumber << ", stream)" << std::endl;
1042         
1043         for(std::string::iterator c = line.begin(); c != line.end(); c++)
1044         {
1045                 if(!got_name)
1046                 {
1047                         /* We don't know the tag name yet. */
1048                         
1049                         if(*c != ' ')
1050                         {
1051                                 if(*c != '<')
1052                                 {
1053                                         tagname += *c;
1054                                 }
1055                         }
1056                         else
1057                         {
1058                                 /* We got to a space, we should have the tagname now. */
1059                                 if(tagname.length())
1060                                 {
1061                                         got_name = true;
1062                                 }
1063                         }
1064                 }
1065                 else
1066                 {
1067                         /* We have the tag name */
1068                         if (!got_key)
1069                         {
1070                                 /* We're still reading the key name */
1071                                 if (*c != '=')
1072                                 {
1073                                         if (*c != ' ')
1074                                         {
1075                                                 current_key += *c;
1076                                         }
1077                                 }
1078                                 else
1079                                 {
1080                                         /* We got an '=', end of the key name. */
1081                                         got_key = true;
1082                                 }
1083                         }
1084                         else
1085                         {
1086                                 /* We have the key name, now we're looking for quotes and the value */
1087
1088                                 /* Correctly handle escaped characters here.
1089                                  * See the XXX'ed section above.
1090                                  */
1091                                 if ((*c == '\\') && (in_quote))
1092                                 {
1093                                         c++;
1094                                         current_value += *c;
1095                                         continue;
1096                                 }
1097                                 if (*c == '"')
1098                                 {
1099                                         if (!in_quote)
1100                                         {
1101                                                 /* We're not already in a quote. */
1102                                                 in_quote = true;
1103                                         }
1104                                         else
1105                                         {
1106                                                 /* Leaving quotes, we have the value */
1107                                                 results.push_back(KeyVal(current_key, current_value));
1108                                                 
1109                                                 // std::cout << "<" << tagname << ":" << current_key << "> " << current_value << std::endl;
1110                                                 
1111                                                 in_quote = false;
1112                                                 got_key = false;
1113                                                 
1114                                                 if((tagname == "include") && (current_key == "file"))
1115                                                 {
1116                                                         if(!this->DoInclude(target, current_value, errorstream))
1117                                                                 return false;
1118                                                 }
1119                                                 
1120                                                 current_key.clear();
1121                                                 current_value.clear();
1122                                         }
1123                                 }
1124                                 else
1125                                 {
1126                                         if(in_quote)
1127                                         {
1128                                                 current_value += *c;
1129                                         }
1130                                 }
1131                         }
1132                 }
1133         }
1134         
1135         /* Finished parsing the tag, add it to the config hash */
1136         target.insert(std::pair<std::string, KeyValList > (tagname, results));
1137         
1138         return true;
1139 }
1140
1141 bool ServerConfig::DoInclude(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream)
1142 {
1143         std::string confpath;
1144         std::string newfile;
1145         std::string::size_type pos;
1146         
1147         confpath = CONFIG_FILE;
1148         newfile = file;
1149         
1150         for (std::string::iterator c = newfile.begin(); c != newfile.end(); c++)
1151         {
1152                 if (*c == '\\')
1153                 {
1154                         *c = '/';
1155                 }
1156         }
1157
1158         if (file[0] != '/')
1159         {
1160                 if((pos = confpath.find("/inspircd.conf")) != std::string::npos)
1161                 {
1162                         /* Leaves us with just the path */
1163                         newfile = confpath.substr(0, pos) + std::string("/") + newfile;
1164                 }
1165                 else
1166                 {
1167                         errorstream << "Couldn't get config path from: " << confpath << std::endl;
1168                         return false;
1169                 }
1170         }
1171         
1172         return LoadConf(target, newfile, errorstream);
1173 }
1174
1175 bool ServerConfig::ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length)
1176 {
1177         std::string value;
1178         bool r = ConfValue(target, std::string(tag), std::string(var), index, value);
1179         strlcpy(result, value.c_str(), length);
1180         return r;
1181 }
1182
1183 bool ServerConfig::ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result)
1184 {
1185         ConfigDataHash::size_type pos = index;
1186         if((pos >= 0) && (pos < target.count(tag)))
1187         {
1188                 ConfigDataHash::const_iterator iter = target.find(tag);
1189                 
1190                 for(int i = 0; i < index; i++)
1191                         iter++;
1192                 
1193                 for(KeyValList::const_iterator j = iter->second.begin(); j != iter->second.end(); j++)
1194                 {
1195                         if(j->first == var)
1196                         {
1197                                 result = j->second;
1198                                 return true;
1199                         }
1200                 }
1201         }
1202         else if(pos == 0)
1203         {
1204                 log(DEBUG, "No <%s> tags in config file.", tag.c_str());
1205         }
1206         else
1207         {
1208                 log(DEBUG, "ConfValue got an out-of-range index %d, there are only %d occurences of %s", pos, target.count(tag), tag.c_str());
1209         }
1210         
1211         return false;
1212 }
1213         
1214 bool ServerConfig::ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, int index, int &result)
1215 {
1216         return ConfValueInteger(target, std::string(tag), std::string(var), index, result);
1217 }
1218
1219 bool ServerConfig::ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, int &result)
1220 {
1221         std::string value;
1222         std::istringstream stream;
1223         bool r = ConfValue(target, tag, var, index, value);
1224         stream.str(value);
1225         if(!(stream >> result))
1226                 return false;
1227         return r;
1228 }
1229         
1230 bool ServerConfig::ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, int index)
1231 {
1232         return ConfValueBool(target, std::string(tag), std::string(var), index);
1233 }
1234
1235 bool ServerConfig::ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, int index)
1236 {
1237         std::string result;
1238         if(!ConfValue(target, tag, var, index, result))
1239                 return false;
1240         
1241         return ((result == "yes") || (result == "true") || (result == "1"));
1242 }
1243         
1244 int ServerConfig::ConfValueEnum(ConfigDataHash &target, const char* tag)
1245 {
1246         return target.count(tag);
1247 }
1248
1249 int ServerConfig::ConfValueEnum(ConfigDataHash &target, const std::string &tag)
1250 {
1251         return target.count(tag);
1252 }
1253         
1254 int ServerConfig::ConfVarEnum(ConfigDataHash &target, const char* tag, int index)
1255 {
1256         return ConfVarEnum(target, std::string(tag), index);
1257 }
1258
1259 int ServerConfig::ConfVarEnum(ConfigDataHash &target, const std::string &tag, int index)
1260 {
1261         ConfigDataHash::size_type pos = index;
1262         
1263         if((pos >= 0) && (pos < target.count(tag)))
1264         {
1265                 ConfigDataHash::const_iterator iter = target.find(tag);
1266                 
1267                 for(int i = 0; i < index; i++)
1268                         iter++;
1269                 
1270                 return iter->second.size();
1271         }
1272         else if(pos == 0)
1273         {
1274                 log(DEBUG, "No <%s> tags in config file.", tag.c_str());
1275         }
1276         else
1277         {
1278                 log(DEBUG, "ConfVarEnum got an out-of-range index %d, there are only %d occurences of %s", pos, target.count(tag), tag.c_str());
1279         }
1280         
1281         return 0;
1282 }
1283
1284 /** Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
1285  */
1286 bool ServerConfig::ReadFile(file_cache &F, const char* fname)
1287 {
1288         FILE* file;
1289         char linebuf[MAXBUF];
1290
1291         F.clear();
1292         file =  fopen(fname,"r");
1293
1294         if (file)
1295         {
1296                 while (!feof(file))
1297                 {
1298                         fgets(linebuf,sizeof(linebuf),file);
1299                         linebuf[strlen(linebuf)-1]='\0';
1300
1301                         if (!*linebuf)
1302                         {
1303                                 strcpy(linebuf," ");
1304                         }
1305
1306                         if (!feof(file))
1307                         {
1308                                 F.push_back(linebuf);
1309                         }
1310                 }
1311
1312                 fclose(file);
1313         }
1314         else
1315                 return false;
1316
1317         return true;
1318 }
1319
1320 bool ServerConfig::FileExists(const char* file)
1321 {
1322         FILE *input;
1323         if ((input = fopen (file, "r")) == NULL)
1324         {
1325                 return false;
1326         }
1327         else
1328         {
1329                 fclose(input);
1330                 return true;
1331         }
1332 }
1333
1334 char* ServerConfig::CleanFilename(char* name)
1335 {
1336         char* p = name + strlen(name);
1337         while ((p != name) && (*p != '/')) p--;
1338         return (p != name ? ++p : p);
1339 }
1340
1341
1342 bool ServerConfig::DirValid(const char* dirandfile)
1343 {
1344         char work[MAXBUF];
1345         char buffer[MAXBUF];
1346         char otherdir[MAXBUF];
1347         int p;
1348
1349         strlcpy(work, dirandfile, MAXBUF);
1350         p = strlen(work);
1351
1352         // we just want the dir
1353         while (*work)
1354         {
1355                 if (work[p] == '/')
1356                 {
1357                         work[p] = '\0';
1358                         break;
1359                 }
1360
1361                 work[p--] = '\0';
1362         }
1363
1364         // Get the current working directory
1365         if (getcwd(buffer, MAXBUF ) == NULL )
1366                 return false;
1367
1368         chdir(work);
1369
1370         if (getcwd(otherdir, MAXBUF ) == NULL )
1371                 return false;
1372
1373         chdir(buffer);
1374
1375         size_t t = strlen(work);
1376
1377         if (strlen(otherdir) >= t)
1378         {
1379                 otherdir[t] = '\0';
1380
1381                 if (!strcmp(otherdir,work))
1382                 {
1383                         return true;
1384                 }
1385
1386                 return false;
1387         }
1388         else
1389         {
1390                 return false;
1391         }
1392 }
1393
1394 std::string ServerConfig::GetFullProgDir(char** argv, int argc)
1395 {
1396         char work[MAXBUF];
1397         char buffer[MAXBUF];
1398         char otherdir[MAXBUF];
1399         int p;
1400
1401         strlcpy(work,argv[0],MAXBUF);
1402         p = strlen(work);
1403
1404         // we just want the dir
1405         while (*work)
1406         {
1407                 if (work[p] == '/')
1408                 {
1409                         work[p] = '\0';
1410                         break;
1411                 }
1412
1413                 work[p--] = '\0';
1414         }
1415
1416         // Get the current working directory
1417         if (getcwd(buffer, MAXBUF) == NULL)
1418                 return "";
1419
1420         chdir(work);
1421
1422         if (getcwd(otherdir, MAXBUF) == NULL)
1423                 return "";
1424
1425         chdir(buffer);
1426         return otherdir;
1427 }
1428
1429