]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/helperfuncs.cpp
numbskull moment
[user/henk/code/inspircd.git] / src / helperfuncs.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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/errno.h>
25 #include <time.h>
26 #include <string>
27 #ifdef GCC3
28 #include <ext/hash_map>
29 #else
30 #include <hash_map>
31 #endif
32 #include <sstream>
33 #include <vector>
34 #include <deque>
35 #include <stdarg.h>
36 #include "connection.h"
37 #include "users.h"
38 #include "ctables.h"
39 #include "globals.h"
40 #include "modules.h"
41 #include "dynamic.h"
42 #include "wildcard.h"
43 #include "message.h"
44 #include "mode.h"
45 #include "xline.h"
46 #include "commands.h"
47 #include "inspstring.h"
48 #include "helperfuncs.h"
49 #include "hashcomp.h"
50 #include "typedefs.h"
51
52 extern int MODCOUNT;
53 extern std::vector<Module*> modules;
54 extern ServerConfig *Config;
55 extern InspIRCd* ServerInstance;
56 extern time_t TIME;
57 extern char lowermap[255];
58 static char list[MAXBUF];
59 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
60 static char already_sent[MAX_DESCRIPTORS];
61 extern std::vector<userrec*> all_opers;
62 extern user_hash clientlist;
63 extern chan_hash chanlist;
64
65 extern std::vector<userrec*> local_users;
66
67 void log(int level,char *text, ...)
68 {
69         va_list argsPtr;
70         struct tm * timeinfo;
71         if (level < Config->LogLevel)
72                 return;
73         char textbuffer[MAXBUF];
74         timeinfo = localtime(&TIME);
75
76         if (Config->log_file)
77         {
78                 char b[26];
79                 va_start (argsPtr, text);
80                 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
81                 va_end(argsPtr);
82                 strlcpy(b,asctime(timeinfo),26);
83                 b[24] = ':';    // we know this is the end of the time string
84                 if (Config->log_file)
85                         fprintf(Config->log_file,"%s %s\n",b,textbuffer);
86                 if (Config->nofork)
87                 {
88                         // nofork enabled? display it on terminal too
89                         printf("%s %s\n",b,textbuffer);
90                 }
91         }
92 }
93
94 void readfile(file_cache &F, const char* fname)
95 {
96         FILE* file;
97         char linebuf[MAXBUF];
98
99         log(DEBUG,"readfile: loading %s",fname);
100         F.clear();
101         file =  fopen(fname,"r");
102         if (file)
103         {
104                 while (!feof(file))
105                 {
106                         fgets(linebuf,sizeof(linebuf),file);
107                         linebuf[strlen(linebuf)-1]='\0';
108                         if (!*linebuf)
109                         {
110                                 strcpy(linebuf,"  ");
111                         }
112                         if (!feof(file))
113                         {
114                                 F.push_back(linebuf);
115                         }
116                 }
117                 fclose(file);
118         }
119         else
120         {
121                 log(DEBUG,"readfile: failed to load file: %s",fname);
122         }
123         log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
124 }
125
126 void Write_NoFormat(int sock, const char *text)
127 {
128         if ((sock < 0) || (!text))
129                 return;
130
131         char tb[MAXBUF];
132         int bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
133         chop(tb);
134         if (fd_ref_table[sock])
135         {
136                 if (Config->GetIOHook(fd_ref_table[sock]->port))
137                 {
138                         Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
139                 }
140                 else
141                 {
142                         fd_ref_table[sock]->AddWriteBuf(tb);
143                 }
144                 ServerInstance->stats->statsSent += bytes;
145         }
146         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
147 }
148
149 void Write(int sock,char *text, ...)
150 {
151         if (sock < 0)
152                 return;
153         if (!text)
154         {
155                 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
156                 return;
157         }
158         va_list argsPtr;
159         char textbuffer[MAXBUF],tb[MAXBUF];
160         va_start (argsPtr, text);
161         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
162         va_end(argsPtr);
163         int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
164         chop(tb);
165         if (fd_ref_table[sock])
166         {
167                 if (Config->GetIOHook(fd_ref_table[sock]->port))
168                 {
169                         Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
170                 }
171                 else
172                 {
173                         fd_ref_table[sock]->AddWriteBuf(tb);
174                 }
175                 ServerInstance->stats->statsSent += bytes;
176         }
177         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
178 }
179
180 void WriteServ_NoFormat(int sock, const char* text)
181 {
182         if ((sock < 0) || (!text))
183                 return;
184         char tb[MAXBUF];
185         int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
186         chop(tb);
187         if (fd_ref_table[sock])
188         {
189                 if (Config->GetIOHook(fd_ref_table[sock]->port))
190                 {
191                         Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
192                 }
193                 else
194                 {
195                         fd_ref_table[sock]->AddWriteBuf(tb);
196                 }
197                 ServerInstance->stats->statsSent += bytes;
198         }
199         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
200 }
201
202 /* write a server formatted numeric response to a single socket */
203
204 void WriteServ(int sock, char* text, ...)
205 {
206         if (sock < 0)
207                 return;
208         if (!text)
209         {
210                 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
211                 return;
212         }
213         va_list argsPtr;
214         va_start (argsPtr, text);
215         char textbuffer[MAXBUF],tb[MAXBUF];
216         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
217         va_end(argsPtr);
218         int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
219         chop(tb);
220         if (fd_ref_table[sock])
221         {
222                 if (Config->GetIOHook(fd_ref_table[sock]->port))
223                 {
224                         Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
225                 }
226                 else
227                 {
228                         fd_ref_table[sock]->AddWriteBuf(tb);
229                 }
230                 ServerInstance->stats->statsSent += bytes;
231         }
232         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
233 }
234
235 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
236 {
237         if ((sock < 0) || (!text) || (!user))
238                 return;
239         char tb[MAXBUF];
240         int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
241         chop(tb);
242         if (fd_ref_table[sock])
243         {
244                 if (Config->GetIOHook(fd_ref_table[sock]->port))
245                 {
246                         Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
247                 }
248                 else
249                 {
250                         fd_ref_table[sock]->AddWriteBuf(tb);
251                 }
252                 ServerInstance->stats->statsSent += bytes;
253         }
254         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
255 }
256
257 /* write text from an originating user to originating user */
258
259 void WriteFrom(int sock, userrec *user,char* text, ...)
260 {
261         if (sock < 0)
262                 return;
263         if ((!text) || (!user))
264         {
265                 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
266                 return;
267         }
268         va_list argsPtr;
269         va_start (argsPtr, text);
270         char textbuffer[MAXBUF],tb[MAXBUF];
271         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
272         va_end(argsPtr);
273         int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
274         chop(tb);
275         if (fd_ref_table[sock])
276         {
277                 if (Config->GetIOHook(fd_ref_table[sock]->port))
278                 {
279                         Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
280                 }
281                 else
282                 {
283                         fd_ref_table[sock]->AddWriteBuf(tb);
284                 }
285                 ServerInstance->stats->statsSent += bytes;
286         }
287         else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
288 }
289
290 /* write text to an destination user from a source user (e.g. user privmsg) */
291
292 void WriteTo(userrec *source, userrec *dest,char *data, ...)
293 {
294         if ((!dest) || (!data))
295         {
296                 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
297                 return;
298         }
299         if (!IS_LOCAL(dest))
300                 return;
301         char textbuffer[MAXBUF],tb[MAXBUF];
302         va_list argsPtr;
303         va_start (argsPtr, data);
304         vsnprintf(textbuffer, MAXBUF, data, argsPtr);
305         va_end(argsPtr);
306         chop(tb);
307
308         // if no source given send it from the server.
309         if (!source)
310         {
311                 WriteServ_NoFormat(dest->fd,textbuffer);
312         }
313         else
314         {
315                 WriteFrom_NoFormat(dest->fd,source,textbuffer);
316         }
317 }
318
319 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
320 {
321         if ((!dest) || (!data))
322                 return;
323         if (!source)
324         {
325                 WriteServ_NoFormat(dest->fd,data);
326         }
327         else
328         {
329                 WriteFrom_NoFormat(dest->fd,source,data);
330         }
331 }
332
333 /* write formatted text from a source user to all users on a channel
334  * including the sender (NOT for privmsg, notice etc!) */
335
336 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
337 {
338         if ((!Ptr) || (!user) || (!text))
339         {
340                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
341                 return;
342         }
343         char textbuffer[MAXBUF];
344         va_list argsPtr;
345         va_start (argsPtr, text);
346         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
347         va_end(argsPtr);
348
349         std::map<char*,char*> *ulist= Ptr->GetUsers();
350         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
351         {
352                 char* o = i->second;
353                 userrec* otheruser = (userrec*)o;
354                 if (otheruser->fd != FD_MAGIC_NUMBER)
355                         WriteTo_NoFormat(user,otheruser,textbuffer);
356         }
357 }
358
359 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
360 {
361         if ((!Ptr) || (!user) || (!text))
362         {
363                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
364                 return;
365         }
366         std::map<char*,char*> *ulist= Ptr->GetUsers();
367         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
368         {
369                 char* o = i->second;
370                 userrec* otheruser = (userrec*)o;
371                 if (otheruser->fd != FD_MAGIC_NUMBER)
372                         WriteTo_NoFormat(user,otheruser,text);
373         }
374 }
375
376
377 /* write formatted text from a source user to all users on a channel
378  * including the sender (NOT for privmsg, notice etc!) doesnt send to
379  * users on remote servers */
380
381 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
382 {
383         if ((!Ptr) || (!text))
384         {
385                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
386                 return;
387         }
388         char textbuffer[MAXBUF];
389         va_list argsPtr;
390         va_start (argsPtr, text);
391         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
392         va_end(argsPtr);
393
394         std::map<char*,char*> *ulist= Ptr->GetUsers();
395         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
396         {
397                 char* o = i->second;
398                 userrec* otheruser = (userrec*)o;
399                 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
400                 {
401                         if (!user)
402                         {
403                                 WriteServ_NoFormat(otheruser->fd,textbuffer);
404                         }
405                         else
406                         {
407                                 WriteTo_NoFormat(user,otheruser,textbuffer);
408                         }
409                 }
410         }
411 }
412
413 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
414 {
415         if ((!Ptr) || (!text))
416         {
417                 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
418                 return;
419         }
420         std::map<char*,char*> *ulist= Ptr->GetUsers();
421         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
422         {
423                 char* o = i->second;
424                 userrec* otheruser = (userrec*)o;
425                 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
426                 {
427                         if (!user)
428                         {
429                                 WriteServ_NoFormat(otheruser->fd,text);
430                         }
431                         else
432                         {
433                                 WriteTo_NoFormat(user,otheruser,text);
434                         }
435                 }
436         }
437 }
438
439
440
441 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
442 {
443         if ((!Ptr) || (!text))
444         {
445                 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
446                 return;
447         }
448         char textbuffer[MAXBUF];
449         va_list argsPtr;
450         va_start (argsPtr, text);
451         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
452         va_end(argsPtr);
453
454
455         std::map<char*,char*> *ulist= Ptr->GetUsers();
456         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
457         {
458                 char* o = i->second;
459                 userrec* otheruser = (userrec*)o;
460                 if (IS_LOCAL(otheruser))
461                         WriteServ_NoFormat(otheruser->fd,textbuffer);
462         }
463 }
464
465 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
466 {
467         if ((!Ptr) || (!text))
468         {
469                 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
470                 return;
471         }
472         std::map<char*,char*> *ulist= Ptr->GetUsers();
473         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
474         {
475                 char* o = i->second;
476                 userrec* otheruser = (userrec*)o;
477                 if (IS_LOCAL(otheruser))
478                         WriteServ_NoFormat(otheruser->fd,text);
479         }
480 }
481
482
483
484 /* write formatted text from a source user to all users on a channel except
485  * for the sender (for privmsg etc) */
486
487 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
488 {
489         if ((!Ptr) || (!user) || (!text))
490         {
491                 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
492                 return;
493         }
494         char textbuffer[MAXBUF];
495         va_list argsPtr;
496         va_start (argsPtr, text);
497         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
498         va_end(argsPtr);
499
500         std::map<char*,char*> *ulist;
501         switch (status)
502         {
503                 case '@':
504                         ulist = Ptr->GetOppedUsers();
505                 break;
506                 case '%':
507                         ulist = Ptr->GetHalfoppedUsers();
508                 break;
509                 case '+':
510                         ulist = Ptr->GetVoicedUsers();
511                 break;
512                 default:
513                         ulist = Ptr->GetUsers();
514                 break;
515         }
516         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
517         {
518                 char* o = i->second;
519                 userrec* otheruser = (userrec*)o;
520                 if ((IS_LOCAL(otheruser)) && (user != otheruser))
521                         WriteFrom_NoFormat(otheruser->fd,user,textbuffer);
522         }
523 }
524
525 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
526 {
527         if ((!Ptr) || (!user) || (!text))
528         {
529                 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
530                 return;
531         }
532         std::map<char*,char*> *ulist;
533         switch (status)
534         {
535                 case '@':
536                         ulist = Ptr->GetOppedUsers();
537                 break;  
538                 case '%':
539                         ulist = Ptr->GetHalfoppedUsers();
540                 break;
541                 case '+':
542                         ulist = Ptr->GetVoicedUsers();
543                 break;
544                 default:
545                         ulist = Ptr->GetUsers();
546                 break;
547         }
548         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
549         {
550                 char* o = i->second;
551                 userrec* otheruser = (userrec*)o;
552                 if ((IS_LOCAL(otheruser)) && (user != otheruser))
553                         WriteFrom_NoFormat(otheruser->fd,user,text);
554         }
555 }
556
557 std::string GetServerDescription(char* servername)
558 {
559         std::string description = "";
560         FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
561         if (description != "")
562         {
563                 return description;
564         }
565         else
566         {
567                 return Config->ServerDesc; // not a remote server that can be found, it must be me.
568         }
569 }
570
571 /* write a formatted string to all users who share at least one common
572  * channel, including the source user e.g. for use in NICK */
573
574 void WriteCommon(userrec *u, char* text, ...)
575 {
576         if (!u)
577         {
578                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
579                 return;
580         }
581
582         if (u->registered != 7) {
583                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
584                 return;
585         }
586
587         char textbuffer[MAXBUF];
588         va_list argsPtr;
589         va_start (argsPtr, text);
590         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
591         va_end(argsPtr);
592
593         // FIX: Stops a message going to the same person more than once
594         memset(&already_sent,0,MAX_DESCRIPTORS);
595
596         bool sent_to_at_least_one = false;
597
598         unsigned int y = u->chans.size();
599         for (unsigned int i = 0; i < y; i++)
600         {
601                 if (u->chans[i].channel)
602                 {
603                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
604                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
605                         {
606                                 char* o = i->second;
607                                 userrec* otheruser = (userrec*)o;
608                                 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
609                                 {
610                                         already_sent[otheruser->fd] = 1;
611                                         WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
612                                         sent_to_at_least_one = true;
613                                 }
614                         }
615                 }
616         }
617         // if the user was not in any channels, no users will receive the text. Make sure the user
618         // receives their OWN message for WriteCommon
619         if (!sent_to_at_least_one)
620         {
621                 WriteFrom_NoFormat(u->fd,u,textbuffer);
622         }
623 }
624
625 void WriteCommon_NoFormat(userrec *u, const char* text)
626 {
627         if (!u)
628         {
629                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
630                 return;
631         }
632                 
633         if (u->registered != 7) {
634                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
635                 return;
636         }
637         // FIX: Stops a message going to the same person more than once
638         memset(&already_sent,0,MAX_DESCRIPTORS);
639                 
640         bool sent_to_at_least_one = false;
641                         
642         unsigned int y = u->chans.size();
643         for (unsigned int i = 0; i < y; i++)
644         {
645                 if (u->chans[i].channel)
646                 {
647                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
648                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
649                         {
650                                 char* o = i->second;
651                                 userrec* otheruser = (userrec*)o;
652                                 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
653                                 {
654                                         already_sent[otheruser->fd] = 1;
655                                         WriteFrom_NoFormat(otheruser->fd,u,text);
656                                         sent_to_at_least_one = true;
657                                 }
658                         }
659                 }
660         }
661         // if the user was not in any channels, no users will receive the text. Make sure the user
662         // receives their OWN message for WriteCommon
663         if (!sent_to_at_least_one)
664         {
665                 WriteFrom_NoFormat(u->fd,u,text);
666         }
667 }
668
669
670 /* write a formatted string to all users who share at least one common
671  * channel, NOT including the source user e.g. for use in QUIT */
672
673 void WriteCommonExcept(userrec *u, char* text, ...)
674 {
675         if (!u)
676         {
677                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
678                 return;
679         }
680
681         if (u->registered != 7) {
682                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
683                 return;
684         }
685
686         char textbuffer[MAXBUF];
687         char oper_quit[MAXBUF];
688         bool quit_munge = false;
689
690         va_list argsPtr;
691         va_start (argsPtr, text);
692         int total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
693         va_end(argsPtr);
694
695         if ((Config->HideSplits) && (total > 6))
696         {
697                 /* Yeah yeah, this is ugly. But its fast, live with it. */
698                 char* check = textbuffer;
699                 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
700                 {
701                         std::stringstream split(check);
702                         std::string server_one;
703                         std::string server_two;
704                         split >> server_one;
705                         split >> server_two;
706                         if ((FindServerName(server_one)) && (FindServerName(server_two)))
707                         {
708                                 strlcpy(oper_quit,textbuffer,MAXBUF);
709                                 strlcpy(check,"*.net *.split",MAXQUIT);
710                                 quit_munge = true;
711                         }
712                 }
713         }
714
715         memset(&already_sent,0,MAX_DESCRIPTORS);
716
717         unsigned int y = u->chans.size();
718         for (unsigned int i = 0; i < y; i++)
719         {
720                 if (u->chans[i].channel)
721                 {
722                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
723                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
724                         {
725                                 char* o = i->second;
726                                 userrec* otheruser = (userrec*)o;
727                                 if (u != otheruser)
728                                 {
729                                         if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
730                                         {
731                                                 already_sent[otheruser->fd] = 1;
732                                                 if (quit_munge)
733                                                 {
734                                                         if (*otheruser->oper)
735                                                         {
736                                                                 WriteFrom_NoFormat(otheruser->fd,u,oper_quit);
737                                                         }
738                                                         else
739                                                         {
740                                                                 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
741                                                         }
742                                                 }
743                                                 else WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
744                                         }
745                                 }
746                         }
747                 }
748         }
749 }
750
751 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
752 {
753         if (!u)
754         {
755                 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
756                 return;
757         }
758          
759         if (u->registered != 7) {
760                 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
761                 return;
762         }
763
764         memset(&already_sent,0,MAX_DESCRIPTORS);
765
766         unsigned int y = u->chans.size();
767         for (unsigned int i = 0; i < y; i++)
768         {
769                 if (u->chans[i].channel)
770                 {
771                         std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
772                         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
773                         {
774                                 char* o = i->second;
775                                 userrec* otheruser = (userrec*)o;
776                                 if (u != otheruser)
777                                 {
778                                         if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
779                                         {
780                                                 already_sent[otheruser->fd] = 1;
781                                                 WriteFrom_NoFormat(otheruser->fd,u,text);
782                                         }
783                                 }
784                         }
785                 }
786         }
787 }
788
789
790
791 void WriteOpers(char* text, ...)
792 {
793         if (!text)
794         {
795                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
796                 return;
797         }
798
799         char textbuffer[MAXBUF];
800         va_list argsPtr;
801         va_start (argsPtr, text);
802         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
803         va_end(argsPtr);
804
805         for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
806         {
807                 userrec* a = *i;
808                 if (IS_LOCAL(a))
809                 {
810                         if (strchr(a->modes,'s'))
811                         {
812                                 // send server notices to all with +s
813                                 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
814                         }
815                 }
816         }
817 }
818
819 void ServerNoticeAll(char* text, ...)
820 {
821         if (!text)
822                 return;
823
824         char textbuffer[MAXBUF];
825         va_list argsPtr;
826         va_start (argsPtr, text);
827         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
828         va_end(argsPtr);
829
830         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
831         {
832                 userrec* t = (userrec*)(*i);
833                 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
834         }
835 }
836
837 void ServerPrivmsgAll(char* text, ...)
838 {
839         if (!text)
840                 return;
841
842         char textbuffer[MAXBUF];
843         va_list argsPtr;
844         va_start (argsPtr, text);
845         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
846         va_end(argsPtr);
847
848         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
849         {
850                 userrec* t = (userrec*)(*i);
851                 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
852         }
853 }
854
855 void WriteMode(const char* modes, int flags, const char* text, ...)
856 {
857         if ((!text) || (!modes) || (!flags))
858         {
859                 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
860                 return;
861         }
862
863         char textbuffer[MAXBUF];
864         va_list argsPtr;
865         va_start (argsPtr, text);
866         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
867         va_end(argsPtr);
868         int modelen = strlen(modes);
869
870         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
871         {
872                 userrec* t = (userrec*)(*i);
873                 bool send_to_user = false;
874                 if (flags == WM_AND)
875                 {
876                         send_to_user = true;
877                         for (int n = 0; n < modelen; n++)
878                         {
879                                 if (!hasumode(t,modes[n]))
880                                 {
881                                         send_to_user = false;
882                                         break;
883                                 }
884                         }
885                 }
886                 else if (flags == WM_OR)
887                 {
888                         send_to_user = false;
889                         for (int n = 0; n < modelen; n++)
890                         {
891                                 if (hasumode(t,modes[n]))
892                                 {
893                                         send_to_user = true;
894                                         break;
895                                 }
896                         }
897                 }
898                 if (send_to_user)
899                 {
900                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
901                 }
902         }
903 }
904
905 void NoticeAll(userrec *source, bool local_only, char* text, ...)
906 {
907         if ((!text) || (!source))
908         {
909                 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
910                 return;
911         }
912
913         char textbuffer[MAXBUF];
914         va_list argsPtr;
915         va_start (argsPtr, text);
916         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
917         va_end(argsPtr);
918
919         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
920         {
921                 userrec* t = (userrec*)(*i);
922                 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
923         }
924
925 }
926
927
928 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
929 {
930         if ((!text) || (!source))
931         {
932                 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
933                 return;
934         }
935
936         char textbuffer[MAXBUF];
937         va_list argsPtr;
938         va_start (argsPtr, text);
939         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
940         va_end(argsPtr);
941
942         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
943         {
944                 userrec* t = (userrec*)(*i);
945                 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
946                 {
947                         WriteTo(source,t,"WALLOPS :%s",textbuffer);
948                 }
949         }
950 }
951
952 /* convert a string to lowercase. Note following special circumstances
953  * taken from RFC 1459. Many "official" server branches still hold to this
954  * rule so i will too;
955  *
956  *  Because of IRC's scandanavian origin, the characters {}| are
957  *  considered to be the lower case equivalents of the characters []\,
958  *  respectively. This is a critical issue when determining the
959  *  equivalence of two nicknames.
960  */
961
962 void strlower(char *n)
963 {
964         if (n)
965         {
966                 for (char* t = n; *t; t++)
967                         *t = lowermap[(unsigned)*t];
968         }
969 }
970
971 /* Find a user record by nickname and return a pointer to it */
972
973 userrec* Find(std::string nick)
974 {
975         user_hash::iterator iter = clientlist.find(nick);
976
977         if (iter == clientlist.end())
978                 /* Couldn't find it */
979                 return NULL;
980
981         return iter->second;
982 }
983
984 /* find a channel record by channel name and return a pointer to it */
985
986 chanrec* FindChan(const char* chan)
987 {
988         if (!chan)
989         {
990                 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
991                 return NULL;
992         }
993
994         chan_hash::iterator iter = chanlist.find(chan);
995
996         if (iter == chanlist.end())
997                 /* Couldn't find it */
998                 return NULL;
999
1000         return iter->second;
1001 }
1002
1003
1004 long GetMaxBans(char* name)
1005 {
1006         char CM[MAXBUF];
1007         for (int count = 0; count < Config->ConfValueEnum("banlist",&Config->config_f); count++)
1008         {
1009                 Config->ConfValue("banlist","chan",count,CM,&Config->config_f);
1010                 if (match(name,CM))
1011                 {
1012                         Config->ConfValue("banlist","limit",count,CM,&Config->config_f);
1013                         return atoi(CM);
1014                 }
1015         }
1016         return 64;
1017 }
1018
1019 void purge_empty_chans(userrec* u)
1020 {
1021
1022         int purge = 0;
1023
1024         // firstly decrement the count on each channel
1025         for (unsigned int f = 0; f < u->chans.size(); f++)
1026         {
1027                 if (u->chans[f].channel)
1028                 {
1029                         u->chans[f].channel->DelUser((char*)u);
1030                 }
1031         }
1032
1033         for (unsigned int i = 0; i < u->chans.size(); i++)
1034         {
1035                 if (u->chans[i].channel)
1036                 {
1037                         if (!usercount(u->chans[i].channel))
1038                         {
1039                                 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1040                                 /* kill the record */
1041                                 if (i2 != chanlist.end())
1042                                 {
1043                                         log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1044                                         if (i2->second)
1045                                         {
1046                                                 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1047                                                 delete i2->second;
1048                                         }
1049                                         chanlist.erase(i2);
1050                                         purge++;
1051                                         u->chans[i].channel = NULL;
1052                                 }
1053                         }
1054                         else
1055                         {
1056                                 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1057                         }
1058                 }
1059         }
1060         log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1061
1062         DeleteOper(u);
1063 }
1064
1065
1066 char* chanmodes(chanrec *chan, bool showkey)
1067 {
1068         static char scratch[MAXBUF];
1069         static char sparam[MAXBUF];
1070         char* offset = scratch;
1071
1072         if (!chan)
1073         {
1074                 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1075                 *scratch = '\0';
1076                 return scratch;
1077         }
1078
1079         *scratch = '\0';
1080         *sparam = '\0';
1081         if (chan->binarymodes & CM_NOEXTERNAL)
1082                 *offset++ = 'n';
1083         if (chan->binarymodes & CM_TOPICLOCK)
1084                 *offset++ = 't';
1085         if (*chan->key)
1086                 *offset++ = 'k';
1087         if (chan->limit)
1088                 *offset++ = 'l';
1089         if (chan->binarymodes & CM_INVITEONLY)
1090                 *offset++ = 'i';
1091         if (chan->binarymodes & CM_MODERATED)
1092                 *offset++ = 'm';
1093         if (chan->binarymodes & CM_SECRET)
1094                 *offset++ = 's';
1095         if (chan->binarymodes & CM_PRIVATE)
1096                 *offset++ = 'p';
1097         if (*chan->key)
1098         {
1099                 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1100         }
1101         if (chan->limit)
1102         {
1103                 char foo[24];
1104                 sprintf(foo," %lu",(unsigned long)chan->limit);
1105                 strlcat(sparam,foo,MAXBUF);
1106         }
1107         if (*chan->custom_modes)
1108         {
1109                 for (char* t = chan->custom_modes; *t; t++)
1110                         *offset++ = *t;
1111                 for (int z = 0; chan->custom_modes[z]; z++)
1112                 {
1113                         std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1114                         if (extparam != "")
1115                         {
1116                                 strlcat(sparam," ",MAXBUF);
1117                                 strlcat(sparam,extparam.c_str(),MAXBUF);
1118                         }
1119                 }
1120         }
1121         /* Null terminate scratch */
1122         *offset = '\0';
1123         strlcat(scratch,sparam,MAXMODES);
1124         return scratch;
1125 }
1126
1127
1128 /* compile a userlist of a channel into a string, each nick seperated by
1129  * spaces and op, voice etc status shown as @ and + */
1130
1131 void userlist(userrec *user,chanrec *c)
1132 {
1133         if ((!c) || (!user))
1134         {
1135                 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1136                 return;
1137         }
1138
1139         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1140
1141         std::map<char*,char*> *ulist= c->GetUsers();
1142         for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1143         {
1144                 char* o = i->second;
1145                 userrec* otheruser = (userrec*)o;
1146                 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1147                 {
1148                         /* user is +i, and source not on the channel, does not show
1149                          * nick in NAMES list */
1150                         continue;
1151                 }
1152                 strlcat(list,cmode(otheruser,c),MAXBUF);
1153                 strlcat(list,otheruser->nick,MAXBUF);
1154                 strlcat(list," ",MAXBUF);
1155                 if (strlen(list)>(480-NICKMAX))
1156                 {
1157                         /* list overflowed into
1158                          * multiple numerics */
1159                         WriteServ_NoFormat(user->fd,list);
1160                         snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1161                 }
1162         }
1163         /* if whats left in the list isnt empty, send it */
1164         if (list[strlen(list)-1] != ':')
1165         {
1166                 WriteServ_NoFormat(user->fd,list);
1167         }
1168 }
1169
1170 /* return a count of the users on a specific channel accounting for
1171  * invisible users who won't increase the count. e.g. for /LIST */
1172
1173 int usercount_i(chanrec *c)
1174 {
1175         int count = 0;
1176
1177         if (!c)
1178         {
1179                 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1180                 return 0;
1181         }
1182
1183         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1184         {
1185                         if (has_channel(i->second,c))
1186                         {
1187                                 if (i->second->registered == 7)
1188                                 {
1189                                         if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1190                                         {
1191                                                 /* user is +i, and source not on the channel, does not show
1192                                                  * nick in NAMES list */
1193                                                 continue;
1194                                         }
1195                                         count++;
1196                                 }
1197                         }
1198         }
1199         log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1200         return count;
1201 }
1202
1203
1204 int usercount(chanrec *c)
1205 {
1206         return (c ? c->GetUserCounter() : 0);
1207 }
1208
1209
1210 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1211
1212 ConnectClass GetClass(userrec *user)
1213 {
1214         for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1215         {
1216                 if (match(user->host,i->host.c_str()))
1217                 {
1218                         return *i;
1219                 }
1220         }
1221         return *(Config->Classes.begin());
1222 }
1223
1224 /* sends out an error notice to all connected clients (not to be used
1225  * lightly!) */
1226
1227 void send_error(char *s)
1228 {
1229         log(DEBUG,"send_error: %s",s);
1230         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1231         {
1232                 userrec* t = (userrec*)(*i);
1233                 if (t->registered == 7)
1234                 {
1235                         WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1236                 }
1237                 else
1238                 {
1239                         // fix - unregistered connections receive ERROR, not NOTICE
1240                         Write(t->fd,"ERROR :%s",s);
1241                 }
1242         }
1243 }
1244
1245 void Error(int status)
1246 {
1247         signal (SIGALRM, SIG_IGN);
1248         signal (SIGPIPE, SIG_IGN);
1249         signal (SIGTERM, SIG_IGN);
1250         signal (SIGABRT, SIG_IGN);
1251         signal (SIGSEGV, SIG_IGN);
1252         signal (SIGURG, SIG_IGN);
1253         signal (SIGKILL, SIG_IGN);
1254         log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1255         send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1256         Exit(status);
1257 }
1258
1259 // this function counts all users connected, wether they are registered or NOT.
1260 int usercnt(void)
1261 {
1262         return clientlist.size();
1263 }
1264
1265 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1266 int registered_usercount(void)
1267 {
1268         int c = 0;
1269         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1270         {
1271                 if (i->second->registered == 7) c++;
1272         }
1273         return c;
1274 }
1275
1276 int usercount_invisible(void)
1277 {
1278         int c = 0;
1279         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1280         {
1281                 if ((i->second->registered == 7) && (strchr(i->second->modes,'i'))) c++;
1282         }
1283         return c;
1284 }
1285
1286 int usercount_opers(void)
1287 {
1288         return all_opers.size();
1289 }
1290
1291 int usercount_unknown(void)
1292 {
1293         int c = 0;
1294         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1295         {
1296                 userrec* t = (userrec*)(*i);
1297                 if (t->registered != 7)
1298                         c++;
1299         }
1300         return c;
1301 }
1302
1303 long chancount(void)
1304 {
1305         return chanlist.size();
1306 }
1307
1308 long local_count()
1309 {
1310         int c = 0;
1311         for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1312         {
1313                 userrec* t = (userrec*)(*i);
1314                 if (t->registered == 7) c++;
1315         }
1316         return c;
1317 }
1318
1319 void ShowMOTD(userrec *user)
1320 {
1321         static char mbuf[MAXBUF];
1322         static char crud[MAXBUF];
1323         std::string WholeMOTD = "";
1324         if (!Config->MOTD.size())
1325         {
1326                 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1327                 return;
1328         }
1329         snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1330         snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1331         WholeMOTD = WholeMOTD + mbuf;
1332         for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1333                 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1334         snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1335         WholeMOTD = WholeMOTD + mbuf;
1336         // only one write operation
1337         if (Config->GetIOHook(user->port))
1338         {
1339                 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1340         }
1341         else
1342         {
1343                 user->AddWriteBuf(WholeMOTD);
1344         }
1345         ServerInstance->stats->statsSent += WholeMOTD.length();
1346 }
1347
1348 void ShowRULES(userrec *user)
1349 {
1350         if (!Config->RULES.size())
1351         {
1352                 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1353                 return;
1354         }
1355         WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1356         for (unsigned int i = 0; i < Config->RULES.size(); i++)
1357                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1358         WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1359 }
1360
1361 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1362 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1363 // registration timeout maximum seconds)
1364 bool AllModulesReportReady(userrec* user)
1365 {
1366         if (!Config->global_implementation[I_OnCheckReady])
1367                 return true;
1368         for (int i = 0; i <= MODCOUNT; i++)
1369         {
1370                 if (Config->implement_lists[i][I_OnCheckReady])
1371                 {
1372                         int res = modules[i]->OnCheckReady(user);
1373                         if (!res)
1374                                 return false;
1375                 }
1376         }
1377         return true;
1378 }
1379
1380 bool DirValid(char* dirandfile)
1381 {
1382         char work[MAXBUF];
1383         char buffer[MAXBUF], otherdir[MAXBUF];
1384         strlcpy(work,dirandfile,MAXBUF);
1385         int p = strlen(work);
1386         // we just want the dir
1387         while (*work)
1388         {
1389                 if (work[p] == '/')
1390                 {
1391                         work[p] = '\0';
1392                         break;
1393                 }
1394                 work[p--] = '\0';
1395         }
1396         // Get the current working directory
1397         if( getcwd( buffer, MAXBUF ) == NULL )
1398                 return false;
1399         chdir(work);
1400         if( getcwd( otherdir, MAXBUF ) == NULL )
1401                 return false;
1402         chdir(buffer);
1403         if (strlen(otherdir) >= strlen(work))
1404         {
1405                 otherdir[strlen(work)] = '\0';
1406                 if (!strcmp(otherdir,work))
1407                 {
1408                         return true;
1409                 }
1410                 return false;
1411         }
1412         else return false;
1413 }
1414
1415 std::string GetFullProgDir(char** argv, int argc)
1416 {
1417         char work[MAXBUF];
1418         char buffer[MAXBUF], otherdir[MAXBUF];
1419         strlcpy(work,argv[0],MAXBUF);
1420         int p = strlen(work);
1421         // we just want the dir
1422         while (*work)
1423         {
1424                 if (work[p] == '/')
1425                 {
1426                         work[p] = '\0';
1427                         break;
1428                 }
1429                 work[p--] = '\0';
1430         }
1431         // Get the current working directory
1432         if( getcwd( buffer, MAXBUF ) == NULL )
1433                 return "";
1434         chdir(work);
1435         if( getcwd( otherdir, MAXBUF ) == NULL )
1436                 return "";
1437         chdir(buffer);
1438         return otherdir;
1439 }
1440
1441 int InsertMode(std::string &output, const char* mode, unsigned short section)
1442 {
1443         unsigned short currsection = 1;
1444         unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1445         
1446         if(section > 4 || section == 0)
1447         {
1448                 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1449                 return 0;
1450         }
1451         
1452         for(; pos < output.size(); pos++)
1453         {
1454                 if(section == currsection)
1455                         break;
1456                         
1457                 if(output[pos] == ',')
1458                         currsection++;
1459         }
1460         
1461         output.insert(pos, mode);
1462         return 1;
1463 }