]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspircd.cpp
2ec9417080b94a11effc60eb2e1a35fb5251e568
[user/henk/code/inspircd.git] / src / inspircd.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "configreader.h"
16 #include <signal.h>
17 #include <dirent.h>
18 #include <exception>
19 #include <fstream>
20 #include <unistd.h>
21 #include "modules.h"
22 #include "mode.h"
23 #include "xline.h"
24 #include "socketengine.h"
25 #include "inspircd_se_config.h"
26 #include "socket.h"
27 #include "typedefs.h"
28 #include "command_parse.h"
29 #include "exitcodes.h"
30 #include <dlfcn.h>
31 #include <getopt.h>
32
33
34 using irc::sockets::NonBlocking;
35 using irc::sockets::Blocking;
36 using irc::sockets::insp_ntoa;
37 using irc::sockets::insp_inaddr;
38 using irc::sockets::insp_sockaddr;
39
40 InspIRCd* SI = NULL;
41
42 void InspIRCd::AddServerName(const std::string &servername)
43 {
44         if(find(servernames.begin(), servernames.end(), servername) == servernames.end())
45                 servernames.push_back(servername); /* Wasn't already there. */
46 }
47
48 const char* InspIRCd::FindServerNamePtr(const std::string &servername)
49 {
50         servernamelist::iterator iter = find(servernames.begin(), servernames.end(), servername);
51
52         if(iter == servernames.end())
53         {
54                 AddServerName(servername);
55                 iter = --servernames.end();
56         }
57
58         return iter->c_str();
59 }
60
61 bool InspIRCd::FindServerName(const std::string &servername)
62 {
63         return (find(servernames.begin(), servernames.end(), servername) != servernames.end());
64 }
65
66 void InspIRCd::Exit(int status)
67 {
68         if (SI)
69         {
70                 SI->SendError("Exiting with status " + ConvToStr(status) + " (" + std::string(ExitCodes[status]) + ")");
71                 SI->Cleanup();
72         }
73         exit (status);
74 }
75
76 void InspIRCd::Cleanup()
77 {
78         std::vector<std::string> mymodnames;
79         int MyModCount = this->GetModuleCount();
80
81         for (unsigned int i = 0; i < stats->BoundPortCount; i++)
82         {
83                 /* This calls the constructor and closes the listening socket */
84                 delete Config->openSockfd[i];
85                 Config->openSockfd[i] = NULL;
86         }
87         stats->BoundPortCount = 0;
88
89         /* Close all client sockets, or the new process inherits them */
90         for (std::vector<userrec*>::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
91         {
92                 (*i)->SetWriteError("Server shutdown");
93                 (*i)->CloseSocket();
94         }
95
96         /* We do this more than once, so that any service providers get a
97          * chance to be unhooked by the modules using them, but then get
98          * a chance to be removed themsleves.
99          */
100         for (int tries = 0; tries < 3; tries++)
101         {
102                 MyModCount = this->GetModuleCount();
103                 mymodnames.clear();
104
105                 /* Unload all modules, so they get a chance to clean up their listeners */
106                 for (int j = 0; j <= MyModCount; j++)
107                         mymodnames.push_back(Config->module_names[j]);
108
109                 for (int k = 0; k <= MyModCount; k++)
110                         this->UnloadModule(mymodnames[k].c_str());
111         }
112
113         /* Close logging */
114         this->Logger->Close();
115 }
116
117 void InspIRCd::Restart(const std::string &reason)
118 {
119         /* SendError flushes each client's queue,
120          * regardless of writeability state
121          */
122         this->SendError(reason);
123
124         this->Cleanup();
125
126         /* Figure out our filename (if theyve renamed it, we're boned) */
127         std::string me = Config->MyDir + "/inspircd";
128
129         char* argv[10];
130         int endp = 2;
131         argv[0] = Config->argv[0];
132         argv[1] = "--restart";
133         if (Config->forcedebug)
134                 argv[endp++] = "--debug";
135         if (Config->nofork)
136                 argv[endp++] = "--nofork";
137         if (!Config->writelog)
138                 argv[endp++] = "--nolog";
139         if (*this->LogFileName)
140         {
141                 argv[endp++] = "--logfile";
142                 argv[endp++] = this->LogFileName;
143         }
144
145         argv[endp] = NULL;
146
147         if (execv(me.c_str(), argv) == -1)
148         {
149                 /* Will raise a SIGABRT if not trapped */
150                 throw CoreException(std::string("Failed to execv()! error: ") + strerror(errno));
151         }
152 }
153
154 void InspIRCd::Start()
155 {
156         printf("\033[1;32mInspire Internet Relay Chat Server, compiled %s at %s\n",__DATE__,__TIME__);
157         printf("(C) InspIRCd Development Team.\033[0m\n\n");
158         printf("Developers:\t\t\033[1;32mBrain, FrostyCoolSlug, w00t, Om, Special, pippijn, peavey\033[0m\n");
159         printf("Others:\t\t\t\033[1;32mSee /INFO Output\033[0m\n");
160 }
161
162 void InspIRCd::Rehash(int status)
163 {
164         SI->WriteOpers("*** Rehashing config file %s due to SIGHUP",ServerConfig::CleanFilename(CONFIG_FILE));
165         SI->CloseLog();
166         SI->OpenLog(SI->Config->argv, SI->Config->argc);
167         SI->RehashUsersAndChans();
168         FOREACH_MOD_I(SI, I_OnGarbageCollect, OnGarbageCollect());
169         SI->Config->Read(false,NULL);
170         SI->ResetMaxBans();
171         SI->Res->Rehash();
172         SI->BuildISupport();
173         FOREACH_MOD_I(SI,I_OnRehash,OnRehash(NULL,""));
174 }
175
176 void InspIRCd::ResetMaxBans()
177 {
178         for (chan_hash::const_iterator i = chanlist->begin(); i != chanlist->end(); i++)
179                 i->second->ResetMaxBans();
180 }
181
182
183 /** Because hash_map doesnt free its buckets when we delete items (this is a 'feature')
184  * we must occasionally rehash the hash (yes really).
185  * We do this by copying the entries from the old hash to a new hash, causing all
186  * empty buckets to be weeded out of the hash. We dont do this on a timer, as its
187  * very expensive, so instead we do it when the user types /REHASH and expects a
188  * short delay anyway.
189  */
190 void InspIRCd::RehashUsersAndChans()
191 {
192         user_hash* old_users = this->clientlist;
193         chan_hash* old_chans = this->chanlist;
194
195         this->clientlist = new user_hash();
196         this->chanlist = new chan_hash();
197
198         for (user_hash::const_iterator n = old_users->begin(); n != old_users->end(); n++)
199                 this->clientlist->insert(*n);
200
201         delete old_users;
202
203         for (chan_hash::const_iterator n = old_chans->begin(); n != old_chans->end(); n++)
204                 this->chanlist->insert(*n);
205
206         delete old_chans;
207 }
208
209 void InspIRCd::CloseLog()
210 {
211         this->Logger->Close();
212 }
213
214 void InspIRCd::SetSignals()
215 {
216         signal(SIGALRM, SIG_IGN);
217         signal(SIGHUP, InspIRCd::Rehash);
218         signal(SIGPIPE, SIG_IGN);
219         signal(SIGTERM, InspIRCd::Exit);
220         signal(SIGCHLD, SIG_IGN);
221 }
222
223 bool InspIRCd::DaemonSeed()
224 {
225         int childpid;
226         if ((childpid = fork ()) < 0)
227                 return false;
228         else if (childpid > 0)
229         {
230                 /* We wait here for the child process to kill us,
231                  * so that the shell prompt doesnt come back over
232                  * the output.
233                  * Sending a kill with a signal of 0 just checks
234                  * if the child pid is still around. If theyre not,
235                  * they threw an error and we should give up.
236                  */
237                 while (kill(childpid, 0) != -1)
238                         sleep(1);
239                 exit(0);
240         }
241         setsid ();
242         umask (007);
243         printf("InspIRCd Process ID: \033[1;32m%lu\033[0m\n",(unsigned long)getpid());
244
245         rlimit rl;
246         if (getrlimit(RLIMIT_CORE, &rl) == -1)
247         {
248                 this->Log(DEFAULT,"Failed to getrlimit()!");
249                 return false;
250         }
251         else
252         {
253                 rl.rlim_cur = rl.rlim_max;
254                 if (setrlimit(RLIMIT_CORE, &rl) == -1)
255                         this->Log(DEFAULT,"setrlimit() failed, cannot increase coredump size.");
256         }
257
258         return true;
259 }
260
261 void InspIRCd::WritePID(const std::string &filename)
262 {
263         std::string fname = (filename.empty() ? "inspircd.pid" : filename);
264         if (*(fname.begin()) != '/')
265         {
266                 std::string::size_type pos;
267                 std::string confpath = CONFIG_FILE;
268                 if ((pos = confpath.find("/inspircd.conf")) != std::string::npos)
269                 {
270                         /* Leaves us with just the path */
271                         fname = confpath.substr(0, pos) + std::string("/") + fname;
272                 }
273         }
274         std::ofstream outfile(fname.c_str());
275         if (outfile.is_open())
276         {
277                 outfile << getpid();
278                 outfile.close();
279         }
280         else
281         {
282                 printf("Failed to write PID-file '%s', exiting.\n",fname.c_str());
283                 this->Log(DEFAULT,"Failed to write PID-file '%s', exiting.",fname.c_str());
284                 Exit(EXIT_STATUS_PID);
285         }
286 }
287
288 std::string InspIRCd::GetRevision()
289 {
290         return REVISION;
291 }
292
293 InspIRCd::InspIRCd(int argc, char** argv)
294         : ModCount(-1), duration_m(60), duration_h(60*60), duration_d(60*60*24), duration_w(60*60*24*7), duration_y(60*60*24*365), GlobalCulls(this)
295 {
296         int found_ports = 0;
297         FailedPortList pl;
298         int do_nofork = 0, do_debug = 0, do_nolog = 0, do_restart = 0;    /* flag variables */
299         char c = 0;
300
301         modules.resize(255);
302         factory.resize(255);
303
304         this->unregistered_count = 0;
305
306         this->clientlist = new user_hash();
307         this->chanlist = new chan_hash();
308
309         this->Config = new ServerConfig(this);
310
311         this->Config->argv = argv;
312         this->Config->argc = argc;
313
314         this->Config->opertypes.clear();
315         this->Config->operclass.clear();
316         this->SNO = new SnomaskManager(this);
317         this->Start();
318         this->TIME = this->OLDTIME = this->startup_time = time(NULL);
319         this->time_delta = 0;
320         this->next_call = this->TIME + 3;
321         srand(this->TIME);
322
323         if (!ServerConfig::FileExists(CONFIG_FILE))
324         {
325                 printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
326                 this->Log(DEFAULT,"main: no config");
327                 printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
328                 Exit(EXIT_STATUS_CONFIG);
329         }
330
331         *this->LogFileName = 0;
332
333         struct option longopts[] =
334         {
335                 { "nofork",     no_argument,            &do_nofork,     1       },
336                 { "logfile",    required_argument,      NULL,           'f'     },
337                 { "debug",      no_argument,            &do_debug,      1       },
338                 { "nolog",      no_argument,            &do_nolog,      1       },
339                 { "restart",    no_argument,            &do_restart,    1       },
340                 { 0, 0, 0, 0 }
341         };
342
343         while ((c = getopt_long_only(argc, argv, ":f:", longopts, NULL)) != -1)
344         {
345                 switch (c)
346                 {
347                         case 'f':
348                                 /* Log filename was set */
349                                 strlcpy(LogFileName, optarg, MAXBUF);
350                                 printf("LOG: Setting logfile to %s\n", LogFileName);
351                         break;
352                         case 0:
353                                 /* getopt_long_only() set an int variable, just keep going */
354                         break;
355                         default:
356                                 /* Unknown parameter! DANGER, INTRUDER.... err.... yeah. */
357                                 printf("Usage: %s [--nofork] [--nolog] [--debug] [--logfile <filename>] [--restart]\n", argv[0]);
358                                 Exit(EXIT_STATUS_ARGV);
359                         break;
360                 }
361         }
362
363         /* Set the finished argument values */
364         Config->nofork = do_nofork;
365         Config->forcedebug = do_debug;
366         Config->writelog = !do_nolog;
367
368         strlcpy(Config->MyExecutable,argv[0],MAXBUF);
369
370         this->OpenLog(argv, argc);
371         this->stats = new serverstats();
372         this->Timers = new TimerManager(this);
373         this->Parser = new CommandParser(this);
374         this->XLines = new XLineManager(this);
375         Config->ClearStack();
376         Config->Read(true, NULL);
377         this->CheckRoot();
378         this->Modes = new ModeParser(this);
379         this->AddServerName(Config->ServerName);
380         CheckDie();
381         InitializeDisabledCommands(Config->DisabledCommands, this);
382         stats->BoundPortCount = BindPorts(true, found_ports, pl);
383
384         for(int t = 0; t < 255; t++)
385                 Config->global_implementation[t] = 0;
386
387         memset(&Config->implement_lists,0,sizeof(Config->implement_lists));
388
389         printf("\n");
390         this->SetSignals();
391
392         if (!Config->nofork)
393         {
394                 if (!this->DaemonSeed())
395                 {
396                         printf("ERROR: could not go into daemon mode. Shutting down.\n");
397                         Log(DEFAULT,"ERROR: could not go into daemon mode. Shutting down.");
398                         Exit(EXIT_STATUS_FORK);
399                 }
400         }
401
402         /* Because of limitations in kqueue on freebsd, we must fork BEFORE we
403          * initialize the socket engine.
404          */
405         SocketEngineFactory* SEF = new SocketEngineFactory();
406         SE = SEF->Create(this);
407         delete SEF;
408
409         this->Res = new DNS(this);
410
411         this->LoadAllModules();
412         /* Just in case no modules were loaded - fix for bug #101 */
413         this->BuildISupport();
414
415         if ((stats->BoundPortCount == 0) && (found_ports > 0))
416         {
417                 printf("\nERROR: I couldn't bind any ports! Are you sure you didn't start InspIRCd twice?\n");
418                 Log(DEFAULT,"ERROR: I couldn't bind any ports! Are you sure you didn't start InspIRCd twice?");
419                 Exit(EXIT_STATUS_BIND);
420         }
421
422         if (stats->BoundPortCount != (unsigned int)found_ports)
423         {
424                 printf("\nWARNING: Not all your client ports could be bound --\nstarting anyway with %ld of %d client ports bound.\n\n", stats->BoundPortCount, found_ports);
425                 printf("The following port%s failed to bind:\n", found_ports - stats->BoundPortCount != 1 ? "s" : "");
426                 int j = 1;
427                 for (FailedPortList::iterator i = pl.begin(); i != pl.end(); i++, j++)
428                 {
429                         printf("%d.\tIP: %s\tPort: %lu\n", j, i->first.empty() ? "<all>" : i->first.c_str(), (unsigned long)i->second);
430                 }
431         }
432
433         /* Add the listening sockets used for client inbound connections
434          * to the socket engine
435          */
436         for (unsigned long count = 0; count < stats->BoundPortCount; count++)
437         {
438                 if (!SE->AddFd(Config->openSockfd[count]))
439                 {
440                         printf("\nEH? Could not add listener to socketengine. You screwed up, aborting.\n");
441                         Log(DEFAULT,"EH? Could not add listener to socketengine. You screwed up, aborting.");
442                         Exit(EXIT_STATUS_INTERNAL);
443                 }
444         }
445
446         if (!Config->nofork && !do_restart)
447         {
448                 int closed = 0;
449
450                 if (kill(getppid(), SIGTERM) == -1)
451                 {
452                         printf("Error killing parent process: %s\n",strerror(errno));
453                         Log(DEFAULT,"Error killing parent process: %s",strerror(errno));
454                 }
455
456                 /*Log(DEBUG,"Fileno(stdin, stdout, stderr): %d %d %d", fileno(stdin), fileno(stdout), fileno(stderr));*/
457
458                 if (stdin)
459                 {
460                         fclose(stdin);
461                         closed++;
462                 }
463
464                 if (stdout)
465                 {
466                         fclose(stdout);
467                         closed++;
468                 }
469
470                 if (stderr)
471                 {
472                         fclose(stderr);
473                         closed++;
474                 }
475
476                 Log(DEBUG,"%d system descriptors closed.", closed);
477         }
478
479         printf("\nInspIRCd is now running!\n");
480         Log(DEFAULT,"Startup complete, Initial TS=%d.", time(NULL));
481
482         this->WritePID(Config->PID);
483 }
484
485 std::string InspIRCd::GetVersionString()
486 {
487         char versiondata[MAXBUF];
488         char dnsengine[] = "singlethread-object";
489         if (*Config->CustomVersion)
490         {
491                 snprintf(versiondata,MAXBUF,"%s %s :%s",VERSION,Config->ServerName,Config->CustomVersion);
492         }
493         else
494         {
495                 snprintf(versiondata,MAXBUF,"%s %s :%s [FLAGS=%s,%s,%s]",VERSION,Config->ServerName,SYSTEM,REVISION,SE->GetName().c_str(),dnsengine);
496         }
497         return versiondata;
498 }
499
500 char* InspIRCd::ModuleError()
501 {
502         return MODERR;
503 }
504
505 void InspIRCd::EraseFactory(int j)
506 {
507         int v = 0;
508         for (std::vector<ircd_module*>::iterator t = factory.begin(); t != factory.end(); t++)
509         {
510                 if (v == j)
511                 {
512                         delete *t;
513                         factory.erase(t);
514                         factory.push_back(NULL);
515                         return;
516                 }
517                 v++;
518         }
519 }
520
521 void InspIRCd::EraseModule(int j)
522 {
523         int v1 = 0;
524         for (ModuleList::iterator m = modules.begin(); m!= modules.end(); m++)
525         {
526                 if (v1 == j)
527                 {
528                         DELETE(*m);
529                         modules.erase(m);
530                         modules.push_back(NULL);
531                         break;
532                 }
533                 v1++;
534         }
535         int v2 = 0;
536         for (std::vector<std::string>::iterator v = Config->module_names.begin(); v != Config->module_names.end(); v++)
537         {
538                 if (v2 == j)
539                 {
540                        Config->module_names.erase(v);
541                        break;
542                 }
543                 v2++;
544         }
545
546 }
547
548 void InspIRCd::MoveTo(std::string modulename,int slot)
549 {
550         unsigned int v2 = 256;
551         for (unsigned int v = 0; v < Config->module_names.size(); v++)
552         {
553                 if (Config->module_names[v] == modulename)
554                 {
555                         // found an instance, swap it with the item at the end
556                         v2 = v;
557                         break;
558                 }
559         }
560         if ((v2 != (unsigned int)slot) && (v2 < 256))
561         {
562                 // Swap the module names over
563                 Config->module_names[v2] = Config->module_names[slot];
564                 Config->module_names[slot] = modulename;
565                 // now swap the module factories
566                 ircd_module* temp = factory[v2];
567                 factory[v2] = factory[slot];
568                 factory[slot] = temp;
569                 // now swap the module objects
570                 Module* temp_module = modules[v2];
571                 modules[v2] = modules[slot];
572                 modules[slot] = temp_module;
573                 // now swap the implement lists (we dont
574                 // need to swap the global or recount it)
575                 for (int n = 0; n < 255; n++)
576                 {
577                         char x = Config->implement_lists[v2][n];
578                         Config->implement_lists[v2][n] = Config->implement_lists[slot][n];
579                         Config->implement_lists[slot][n] = x;
580                 }
581         }
582 }
583
584 void InspIRCd::MoveAfter(std::string modulename, std::string after)
585 {
586         for (unsigned int v = 0; v < Config->module_names.size(); v++)
587         {
588                 if (Config->module_names[v] == after)
589                 {
590                         MoveTo(modulename, v);
591                         return;
592                 }
593         }
594 }
595
596 void InspIRCd::MoveBefore(std::string modulename, std::string before)
597 {
598         for (unsigned int v = 0; v < Config->module_names.size(); v++)
599         {
600                 if (Config->module_names[v] == before)
601                 {
602                         if (v > 0)
603                         {
604                                 MoveTo(modulename, v-1);
605                         }
606                         else
607                         {
608                                 MoveTo(modulename, v);
609                         }
610                         return;
611                 }
612         }
613 }
614
615 void InspIRCd::MoveToFirst(std::string modulename)
616 {
617         MoveTo(modulename,0);
618 }
619
620 void InspIRCd::MoveToLast(std::string modulename)
621 {
622         MoveTo(modulename,this->GetModuleCount());
623 }
624
625 void InspIRCd::BuildISupport()
626 {
627         // the neatest way to construct the initial 005 numeric, considering the number of configure constants to go in it...
628         std::stringstream v;
629         v << "WALLCHOPS WALLVOICES MODES=" << MAXMODES << " CHANTYPES=# PREFIX=" << this->Modes->BuildPrefixes() << " MAP MAXCHANNELS=" << Config->MaxChans << " MAXBANS=60 VBANLIST NICKLEN=" << NICKMAX-1;
630         v << " CASEMAPPING=rfc1459 STATUSMSG=@%+ CHARSET=ascii TOPICLEN=" << MAXTOPIC << " KICKLEN=" << MAXKICK << " MAXTARGETS=" << Config->MaxTargets << " AWAYLEN=";
631         v << MAXAWAY << " CHANMODES=" << this->Modes->ChanModes() << " FNC NETWORK=" << Config->Network << " MAXPARA=32";
632         Config->data005 = v.str();
633         FOREACH_MOD_I(this,I_On005Numeric,On005Numeric(Config->data005));
634         Config->Update005();
635 }
636
637 bool InspIRCd::UnloadModule(const char* filename)
638 {
639         std::string filename_str = filename;
640         for (unsigned int j = 0; j != Config->module_names.size(); j++)
641         {
642                 if (Config->module_names[j] == filename_str)
643                 {
644                         if (modules[j]->GetVersion().Flags & VF_STATIC)
645                         {
646                                 this->Log(DEFAULT,"Failed to unload STATIC module %s",filename);
647                                 snprintf(MODERR,MAXBUF,"Module not unloadable (marked static)");
648                                 return false;
649                         }
650                         std::pair<int,std::string> intercount = GetInterfaceInstanceCount(modules[j]);
651                         if (intercount.first > 0)
652                         {
653                                 this->Log(DEFAULT,"Failed to unload module %s, being used by %d other(s) via interface '%s'",filename, intercount.first, intercount.second.c_str());
654                                 snprintf(MODERR,MAXBUF,"Module not unloadable (Still in use by %d other module%s which %s using its interface '%s') -- unload dependent modules first!",
655                                                 intercount.first,
656                                                 intercount.first > 1 ? "s" : "",
657                                                 intercount.first > 1 ? "are" : "is",
658                                                 intercount.second.c_str());
659                                 return false;
660                         }
661                         /* Give the module a chance to tidy out all its metadata */
662                         for (chan_hash::iterator c = this->chanlist->begin(); c != this->chanlist->end(); c++)
663                         {
664                                 modules[j]->OnCleanup(TYPE_CHANNEL,c->second);
665                         }
666                         for (user_hash::iterator u = this->clientlist->begin(); u != this->clientlist->end(); u++)
667                         {
668                                 modules[j]->OnCleanup(TYPE_USER,u->second);
669                         }
670
671                         /* Tidy up any dangling resolvers */
672                         this->Res->CleanResolvers(modules[j]);
673
674                         FOREACH_MOD_I(this,I_OnUnloadModule,OnUnloadModule(modules[j],Config->module_names[j]));
675
676                         for(int t = 0; t < 255; t++)
677                         {
678                                 Config->global_implementation[t] -= Config->implement_lists[j][t];
679                         }
680
681                         /* We have to renumber implement_lists after unload because the module numbers change!
682                          */
683                         for(int j2 = j; j2 < 254; j2++)
684                         {
685                                 for(int t = 0; t < 255; t++)
686                                 {
687                                         Config->implement_lists[j2][t] = Config->implement_lists[j2+1][t];
688                                 }
689                         }
690
691                         // found the module
692                         Parser->RemoveCommands(filename);
693                         this->EraseModule(j);
694                         this->EraseFactory(j);
695                         this->Log(DEFAULT,"Module %s unloaded",filename);
696                         this->ModCount--;
697                         BuildISupport();
698                         return true;
699                 }
700         }
701         this->Log(DEFAULT,"Module %s is not loaded, cannot unload it!",filename);
702         snprintf(MODERR,MAXBUF,"Module not loaded");
703         return false;
704 }
705
706 bool InspIRCd::LoadModule(const char* filename)
707 {
708         /* Do we have a glob pattern in the filename?
709          * The user wants to load multiple modules which
710          * match the pattern.
711          */
712         if (strchr(filename,'*') || (strchr(filename,'?')))
713         {
714                 int n_match = 0;
715                 DIR* library = opendir(Config->ModPath);
716                 if (library)
717                 {
718                         /* Try and locate and load all modules matching the pattern */
719                         dirent* entry = NULL;
720                         while ((entry = readdir(library)))
721                         {
722                                 if (this->MatchText(entry->d_name, filename))
723                                 {
724                                         if (!this->LoadModule(entry->d_name))
725                                                 n_match++;
726                                 }
727                         }
728                         closedir(library);
729                 }
730                 /* Loadmodule will now return false if any one of the modules failed
731                  * to load (but wont abort when it encounters a bad one) and when 1 or
732                  * more modules were actually loaded.
733                  */
734                 return (n_match > 0);
735         }
736
737         char modfile[MAXBUF];
738         snprintf(modfile,MAXBUF,"%s/%s",Config->ModPath,filename);
739         std::string filename_str = filename;
740
741         if (!ServerConfig::DirValid(modfile))
742         {
743                 this->Log(DEFAULT,"Module %s is not within the modules directory.",modfile);
744                 snprintf(MODERR,MAXBUF,"Module %s is not within the modules directory.",modfile);
745                 return false;
746         }
747         if (ServerConfig::FileExists(modfile))
748         {
749
750                 for (unsigned int j = 0; j < Config->module_names.size(); j++)
751                 {
752                         if (Config->module_names[j] == filename_str)
753                         {
754                                 this->Log(DEFAULT,"Module %s is already loaded, cannot load a module twice!",modfile);
755                                 snprintf(MODERR,MAXBUF,"Module already loaded");
756                                 return false;
757                         }
758                 }
759                 try
760                 {
761                         ircd_module* a = new ircd_module(this, modfile);
762                         factory[this->ModCount+1] = a;
763                         if (factory[this->ModCount+1]->LastError())
764                         {
765                                 this->Log(DEFAULT,"Unable to load %s: %s",modfile,factory[this->ModCount+1]->LastError());
766                                 snprintf(MODERR,MAXBUF,"Loader/Linker error: %s",factory[this->ModCount+1]->LastError());
767                                 return false;
768                         }
769                         if ((long)factory[this->ModCount+1]->factory != -1)
770                         {
771                                 Module* m = factory[this->ModCount+1]->factory->CreateModule(this);
772
773                                 Version v = m->GetVersion();
774
775                                 if (v.API != API_VERSION)
776                                 {
777                                         delete m;
778                                         delete a;
779                                         this->Log(DEFAULT,"Unable to load %s: Incorrect module API version: %d (our version: %d)",modfile,v.API,API_VERSION);
780                                         snprintf(MODERR,MAXBUF,"Loader/Linker error: Incorrect module API version: %d (our version: %d)",v.API,API_VERSION);
781                                         return false;
782                                 }
783                                 else
784                                 {
785                                         this->Log(DEFAULT,"New module introduced: %s (API version %d, Module version %d.%d.%d.%d)%s", filename, v.API, v.Major, v.Minor, v.Revision, v.Build, (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
786                                 }
787
788                                 modules[this->ModCount+1] = m;
789                                 /* save the module and the module's classfactory, if
790                                  * this isnt done, random crashes can occur :/ */
791                                 Config->module_names.push_back(filename);
792
793                                 char* x = &Config->implement_lists[this->ModCount+1][0];
794                                 for(int t = 0; t < 255; t++)
795                                         x[t] = 0;
796
797                                 modules[this->ModCount+1]->Implements(x);
798
799                                 for(int t = 0; t < 255; t++)
800                                         Config->global_implementation[t] += Config->implement_lists[this->ModCount+1][t];
801                         }
802                         else
803                         {
804                                 this->Log(DEFAULT,"Unable to load %s",modfile);
805                                 snprintf(MODERR,MAXBUF,"Factory function failed: Probably missing init_module() entrypoint.");
806                                 return false;
807                         }
808                 }
809                 catch (CoreException& modexcept)
810                 {
811                         this->Log(DEFAULT,"Unable to load %s: ",modfile,modexcept.GetReason());
812                         snprintf(MODERR,MAXBUF,"Factory function of %s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
813                         return false;
814                 }
815         }
816         else
817         {
818                 this->Log(DEFAULT,"InspIRCd: startup: Module Not Found %s",modfile);
819                 snprintf(MODERR,MAXBUF,"Module file could not be found");
820                 return false;
821         }
822         this->ModCount++;
823         FOREACH_MOD_I(this,I_OnLoadModule,OnLoadModule(modules[this->ModCount],filename_str));
824         // now work out which modules, if any, want to move to the back of the queue,
825         // and if they do, move them there.
826         std::vector<std::string> put_to_back;
827         std::vector<std::string> put_to_front;
828         std::map<std::string,std::string> put_before;
829         std::map<std::string,std::string> put_after;
830         for (unsigned int j = 0; j < Config->module_names.size(); j++)
831         {
832                 if (modules[j]->Prioritize() == PRIORITY_LAST)
833                         put_to_back.push_back(Config->module_names[j]);
834                 else if (modules[j]->Prioritize() == PRIORITY_FIRST)
835                         put_to_front.push_back(Config->module_names[j]);
836                 else if ((modules[j]->Prioritize() & 0xFF) == PRIORITY_BEFORE)
837                         put_before[Config->module_names[j]] = Config->module_names[modules[j]->Prioritize() >> 8];
838                 else if ((modules[j]->Prioritize() & 0xFF) == PRIORITY_AFTER)
839                         put_after[Config->module_names[j]] = Config->module_names[modules[j]->Prioritize() >> 8];
840         }
841         for (unsigned int j = 0; j < put_to_back.size(); j++)
842                 MoveToLast(put_to_back[j]);
843         for (unsigned int j = 0; j < put_to_front.size(); j++)
844                 MoveToFirst(put_to_front[j]);
845         for (std::map<std::string,std::string>::iterator j = put_before.begin(); j != put_before.end(); j++)
846                 MoveBefore(j->first,j->second);
847         for (std::map<std::string,std::string>::iterator j = put_after.begin(); j != put_after.end(); j++)
848                 MoveAfter(j->first,j->second);
849         BuildISupport();
850         return true;
851 }
852
853 void InspIRCd::DoOneIteration(bool process_module_sockets)
854 {
855         static rusage ru;
856
857         /* time() seems to be a pretty expensive syscall, so avoid calling it too much.
858          * Once per loop iteration is pleanty.
859          */
860         OLDTIME = TIME;
861         TIME = time(NULL);
862
863         /* Run background module timers every few seconds
864          * (the docs say modules shouldnt rely on accurate
865          * timing using this event, so we dont have to
866          * time this exactly).
867          */
868         if (TIME != OLDTIME)
869         {
870                 if (TIME < OLDTIME)
871                         WriteOpers("*** \002EH?!\002 -- Time is flowing BACKWARDS in this dimension! Clock drifted backwards %d secs.",abs(OLDTIME-TIME));
872                 if ((TIME % 3600) == 0)
873                 {
874                         this->RehashUsersAndChans();
875                         FOREACH_MOD_I(this, I_OnGarbageCollect, OnGarbageCollect());
876                 }
877                 Timers->TickTimers(TIME);
878                 this->DoBackgroundUserStuff(TIME);
879
880                 if ((TIME % 5) == 0)
881                 {
882                         XLines->expire_lines();
883                         FOREACH_MOD_I(this,I_OnBackgroundTimer,OnBackgroundTimer(TIME));
884                         Timers->TickMissedTimers(TIME);
885                 }
886
887                 if (!getrusage(0, &ru))
888                 {
889                         gettimeofday(&this->stats->LastSampled, NULL);
890                         this->stats->LastCPU = ru.ru_utime;
891                 }
892         }
893
894         /* Call the socket engine to wait on the active
895          * file descriptors. The socket engine has everything's
896          * descriptors in its list... dns, modules, users,
897          * servers... so its nice and easy, just one call.
898          * This will cause any read or write events to be
899          * dispatched to their handlers.
900          */
901         SE->DispatchEvents();
902
903         /* if any users was quit, take them out */
904         GlobalCulls.Apply();
905
906 }
907
908 bool InspIRCd::IsIdent(const char* n)
909 {
910         if (!n || !*n)
911                 return false;
912
913         for (char* i = (char*)n; *i; i++)
914         {
915                 if ((*i >= 'A') && (*i <= '}'))
916                 {
917                         continue;
918                 }
919                 if (((*i >= '0') && (*i <= '9')) || (*i == '-') || (*i == '.'))
920                 {
921                         continue;
922                 }
923                 return false;
924         }
925         return true;
926 }
927
928
929 int InspIRCd::Run()
930 {
931         while (true)
932         {
933                 DoOneIteration(true);
934         }
935         /* This is never reached -- we hope! */
936         return 0;
937 }
938
939 /**********************************************************************************/
940
941 /**
942  * An ircd in four lines! bwahahaha. ahahahahaha. ahahah *cough*.
943  */
944
945 int main(int argc, char** argv)
946 {
947         SI = new InspIRCd(argc, argv);
948         SI->Run();
949         delete SI;
950         return 0;
951 }
952
953 /* this returns true when all modules are satisfied that the user should be allowed onto the irc server
954  * (until this returns true, a user will block in the waiting state, waiting to connect up to the
955  * registration timeout maximum seconds)
956  */
957 bool InspIRCd::AllModulesReportReady(userrec* user)
958 {
959         if (!Config->global_implementation[I_OnCheckReady])
960                 return true;
961
962         for (int i = 0; i <= this->GetModuleCount(); i++)
963         {
964                 if (Config->implement_lists[i][I_OnCheckReady])
965                 {
966                         int res = modules[i]->OnCheckReady(user);
967                         if (!res)
968                                 return false;
969                 }
970         }
971         return true;
972 }
973
974 int InspIRCd::GetModuleCount()
975 {
976         return this->ModCount;
977 }
978
979 time_t InspIRCd::Time(bool delta)
980 {
981         if (delta)
982                 return TIME + time_delta;
983         return TIME;
984 }
985
986 int InspIRCd::SetTimeDelta(int delta)
987 {
988         int old = time_delta;
989         time_delta = delta;
990         this->Log(DEBUG, "Time delta set to %d (was %d)", time_delta, old);
991         return old;
992 }
993
994 void InspIRCd::AddLocalClone(userrec* user)
995 {
996         clonemap::iterator x = local_clones.find(user->GetIPString());
997         if (x != local_clones.end())
998                 x->second++;
999         else
1000                 local_clones[user->GetIPString()] = 1;
1001 }
1002
1003 void InspIRCd::AddGlobalClone(userrec* user)
1004 {
1005         clonemap::iterator y = global_clones.find(user->GetIPString());
1006         if (y != global_clones.end())
1007                 y->second++;
1008         else
1009                 global_clones[user->GetIPString()] = 1;
1010 }
1011
1012 int InspIRCd::GetTimeDelta()
1013 {
1014         return time_delta;
1015 }
1016
1017 bool FileLogger::Readable()
1018 {
1019         return false;
1020 }
1021
1022 void FileLogger::HandleEvent(EventType et, int errornum)
1023 {
1024         this->WriteLogLine("");
1025         if (log)
1026                 ServerInstance->SE->DelFd(this);
1027 }
1028
1029 void FileLogger::WriteLogLine(const std::string &line)
1030 {
1031         if (line.length())
1032                 buffer.append(line);
1033
1034         if (log)
1035         {
1036                 int written = fprintf(log,"%s",buffer.c_str());
1037                 if ((written >= 0) && (written < (int)buffer.length()))
1038                 {
1039                         buffer.erase(0, buffer.length());
1040                         ServerInstance->SE->AddFd(this);
1041                 }
1042                 else if (written == -1)
1043                 {
1044                         if (errno == EAGAIN)
1045                                 ServerInstance->SE->AddFd(this);
1046                 }
1047                 else
1048                 {
1049                         /* Wrote the whole buffer, and no need for write callback */
1050                         buffer = "";
1051                 }
1052
1053                 if (writeops++ % 20)
1054                 {
1055                         fflush(log);
1056                 }
1057         }
1058 }
1059
1060 void FileLogger::Close()
1061 {
1062         if (log)
1063         {
1064                 int flags = fcntl(fileno(log), F_GETFL, 0);
1065                 fcntl(fileno(log), F_SETFL, flags ^ O_NONBLOCK);
1066                 if (buffer.size())
1067                         fprintf(log,"%s",buffer.c_str());
1068
1069                 ServerInstance->SE->DelFd(this);
1070
1071                 fflush(log);
1072                 fclose(log);
1073         }
1074
1075         buffer = "";
1076 }
1077
1078 FileLogger::FileLogger(InspIRCd* Instance, FILE* logfile) : ServerInstance(Instance), log(logfile), writeops(0)
1079 {
1080         if (log)
1081         {
1082                 irc::sockets::NonBlocking(fileno(log));
1083                 this->SetFd(fileno(log));
1084                 buffer = "";
1085         }
1086 }
1087
1088 FileLogger::~FileLogger()
1089 {
1090         this->Close();
1091 }
1092