muhstik

- irc flooding solution
git clone git://git.acid.vegas/muhstik.git
Log | Files | Refs | Archive | README

init.c (12272B)

      1 /* Muhstik, Copyright (C) 2001-2002, Louis Bavoil <mulder@gmx.fr>       */
      2 /*                        2003, roadr (bigmac@home.sirklabs.hu)         */
      3 /*                        2009-2011, Leon Kaiser <literalka@gnaa.eu>    */
      4 /*                                                                      */
      5 /* This program is free software; you can redistribute it and/or        */
      6 /* modify it under the terms of the GNU Library General Public License  */
      7 /* as published by the Free Software Foundation; either version 2       */
      8 /* of the License, or (at your option) any later version.               */
      9 /*                                                                      */
     10 /* This program is distributed in the hope that it will be useful,      */
     11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
     12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
     13 /* GNU Library General Public License for more details.                 */
     14 
     15 /* {{{ Header includes */
     16 #include <pthread.h>
     17 #include <stdio.h>
     18 #include <stdlib.h>
     19 #include <unistd.h>
     20 
     21 #include "../include/init.h"
     22 #include "../include/string.h"
     23 #include "../include/lists.h"
     24 #include "../include/print.h"
     25 /* }}} */
     26 /*{{{ Global variables */
     27 char *hostname;
     28 config_t conf;
     29 char *channel[MAX_CHANS];
     30 char *chankey[MAX_CHANS];
     31 pthread_attr_t attr;
     32 pthread_mutex_t mutex[MAX_MUTEX];
     33 /* }}} */
     34 /* {{{ check_options() */
     35 void check_options()
     36 {
     37      int i;
     38 
     39      for (i = 0; i < PROXYS && !conf.h[i][0]; ++i);
     40      {
     41           if (i == PROXYS && !conf.direct[0])
     42           {
     43                puts("You must supply a way of connection (proxy, socks, etc).");
     44                exit(EXIT_FAILURE);
     45           }
     46      }
     47 
     48      for (i = 0; i < PROXYS; ++i)
     49      {
     50           if (conf.h[i][0] && !conf.s[i][0])
     51           {
     52                puts("You must supply a server list for each proxy list.");
     53                exit(EXIT_FAILURE);
     54           }
     55      }
     56 
     57      for (i = 0; i < PROXYS; ++i)
     58      {
     59           if (!conf.h[i][0] && conf.s[i][0])
     60           {
     61                puts("You must supply a proxy list for each server list.");
     62                exit(EXIT_FAILURE);
     63           }
     64      }
     65 
     66      if (conf.use_wordlist && !conf.nicks[0])
     67      {
     68           puts("No nicknames to use.");
     69           exit(EXIT_FAILURE);
     70      }
     71 }
     72 /* }}} */
     73 /* {{{ init_*() */
     74 void init_threads()
     75 {
     76      int i;
     77 
     78      if (pthread_attr_init(&attr))
     79      {
     80           puts("pthread_attr_init failed");
     81           exit(EXIT_FAILURE);
     82      }
     83      if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
     84      {
     85           puts("pthread_attr_setdetachstate failed");
     86           exit(EXIT_FAILURE);
     87      }
     88 
     89      for (i = 0; i < MAX_MUTEX; ++i)
     90      {
     91           if (pthread_mutex_init(&mutex[i], NULL))
     92           {
     93                puts("pthread_mutex_init failed");
     94                exit(EXIT_FAILURE);
     95           }
     96      }
     97 }
     98 
     99 void init_hostname()
    100 {
    101      char name[MEDBUF];
    102 
    103      if (gethostname(name, sizeof(name)) == -1)
    104      {
    105           print_error("gethostname");
    106           exit(EXIT_FAILURE);
    107      }
    108      StrFirstToken(name);
    109      hostname = StrDuplicate(name);
    110 }
    111 
    112 void init_options(char **argv)
    113 {
    114      FILE *config, *f;
    115      unsigned i;
    116      unsigned j;
    117      char buffer[MEDBUF];
    118      char parm[MEDBUF];
    119 
    120      struct
    121      {
    122           char *arg;
    123           int *ptr;
    124      } init_int[] = {
    125           { "load",             &conf.load            },
    126           { "group",            &conf.group           },
    127           { "clones",           &conf.max_clones      },
    128           { "wait",             &conf.wait_socks      },
    129           { "rewind",           &conf.rewind_socks    },
    130           { "timeout",          &conf.timeout         },
    131           { "max_reco",         &conf.max_reco        },
    132           { "wait_reco",        &conf.wait_reco       },
    133           { "no_restricted",    &conf.no_restricted   },
    134           { "scan",             &conf.scan            },
    135           { "help",             &conf.help            },
    136           { "verbose",          &conf.verbose         },
    137           { "debug",            &conf.debug           },
    138           { "nocolor",          &conf.nocolor         },
    139           { "dalnet",           &conf.dalnet          },
    140           { "using_wordlist",   &conf.use_wordlist    },
    141           { "nick_length",      &conf.nick_length     },
    142           { "ident_length",     &conf.ident_length    },
    143           { "real_length",      &conf.real_length     },
    144           { "rejoin",           &conf.rejoin          },
    145           { "multi_op",         &conf.multi_op        },
    146           { "multi_kick",       &conf.multi_kick      },
    147           { "multi_deop",       &conf.multi_deop      },
    148           { "aggressive",       &conf.aggressive      },
    149           { "peace",            &conf.peace           },
    150           { "repeat",           &conf.repeat          },
    151           { "notice",           &conf.notice          },
    152           /* { "max_conns",        &conf.max_conns       }, */
    153           { NULL,               NULL                  }
    154      };
    155 
    156      struct
    157      {
    158           char *arg;
    159           FILE **ptr;
    160           short min;
    161           short max;
    162      } init_list[] = {
    163           { "sock4_list",             conf.h[SOCKS4],         0,      MAX_GROUPS   },
    164           { "sock4_server_list",      conf.s[SOCKS4],         0,      MAX_GROUPS   },
    165           { "sock5_list",             conf.h[SOCKS5],         0,      MAX_GROUPS   },
    166           { "sock5_server_list",      conf.s[SOCKS5],         0,      MAX_GROUPS   },
    167           { "proxy_list",             conf.h[PROXY],          0,      MAX_GROUPS   },
    168           { "proxy_server_list",      conf.s[PROXY],          0,      MAX_GROUPS   },
    169           { "cisco_list",             conf.h[CISCO],          0,      MAX_GROUPS   },
    170           { "cisco_server_list",      conf.s[CISCO],          0,      MAX_GROUPS   },
    171           { "vhost_list",             conf.h[VHOST],          0,      MAX_GROUPS   },
    172           { "vhost_server_list",      conf.s[VHOST],          0,      MAX_GROUPS   },
    173           { "direct_server_list",     conf.direct,            0,      2            },
    174           { NULL,                     NULL,                   0,      0            }
    175      };
    176 
    177      struct
    178      {
    179           char *arg;
    180           char **ptr;
    181           short min;
    182           short max;
    183      } init_save[] = {
    184           { "sock4_save",             conf.g[SOCKS4],         0,      MAX_GROUPS   },
    185           { "sock5_save",             conf.g[SOCKS5],         0,      MAX_GROUPS   },
    186           { "proxy_save",             conf.g[PROXY],          0,      MAX_GROUPS   },
    187           { "vhost_save",             conf.g[VHOST],          0,      MAX_GROUPS   },
    188           { "cisco_save",             conf.g[CISCO],          0,      MAX_GROUPS   },
    189           { NULL,                     NULL,                   0,      0            }
    190      };
    191 
    192      struct
    193      {
    194           char *arg;
    195           char **save;
    196           char **ptr;
    197           int max;
    198      } init_read[] = {
    199           { "aop_list",       &conf.userlist[AOP],   conf.aop,         MAX_AOPS      },
    200           { "jupe_list",      &conf.userlist[JUPE],  conf.jupe,        MAX_JUPES     },
    201           { "prot_list",      &conf.userlist[PROT],  conf.prot,        MAX_PROTS     },
    202           { "shit_list",      &conf.userlist[SHIT],  conf.shit,        MAX_SHITS     },
    203           { "nicks",          NULL,                  conf.nicks,       MAX_NICKS     },
    204           { "idents",         NULL,                  conf.idents,      MAX_IDENTS    },
    205           { "realnames",      NULL,                  conf.names,       MAX_NAMES     },
    206           { NULL,             NULL,                  NULL,             0             }
    207      };
    208 
    209      struct
    210      {
    211           char *arg;
    212           char **ptr;
    213      } init_str[] = {
    214           { "onstart",        &conf.batch             },
    215           { "chan",           channel                 },
    216           { "motd",           &conf.motd              },
    217           { NULL,             NULL                    }
    218      };
    219 
    220      /* Initialize the parameters */
    221      conf.load = 2000;
    222      conf.group = 1;
    223      conf.timeout = 30;
    224      conf.max_clones = 1;
    225 
    226      conf.multi_kick = 1;
    227      conf.multi_op = 4;
    228      conf.multi_deop = 4;
    229 
    230      conf.rejoin = 5;
    231      conf.dalnet = 1;
    232      conf.nick_length = 8;
    233      conf.ident_length = 8;
    234      conf.real_length = 8;
    235 
    236      if (!(config = fopen(argv[1], "r")))
    237      {
    238           puts("Cannot open the config file.");
    239           exit(EXIT_FAILURE);
    240      }
    241 
    242      while (!get_a_line(buffer, sizeof(buffer), config))
    243      {
    244           if (StrParam(parm, sizeof(parm), buffer, 0))
    245           {
    246                continue;
    247           }
    248 
    249           /* Initialize the integer parameters */
    250           for (i = 0; init_int[i].arg; ++i)
    251           {
    252                if (!StrCompare(init_int[i].arg, parm))
    253                {
    254                     if (!StrParam(parm, sizeof(parm), buffer, 1))
    255                     {
    256                          *init_int[i].ptr = atoi(parm);
    257                          break;
    258                     }
    259                }
    260           }
    261           if (init_int[i].arg)
    262           {
    263                continue;
    264           }
    265 
    266           /* Initialize the file descriptors */
    267           for (i = 0; init_list[i].arg; ++i)
    268           {
    269                if (!StrCompare(init_list[i].arg, parm))
    270                {
    271                     if (init_list[i].min < init_list[i].max-1)
    272                     {
    273                          if (!StrParam(parm, sizeof(parm), buffer, 1))
    274                          {
    275                               j = init_list[i].min++;
    276                               if (!((init_list[i].ptr)[j] = fopen(parm, "r")))
    277                               {
    278                                    printf("Cannot open a %s.\n", init_list[i].arg);
    279                                    exit(EXIT_FAILURE);
    280                               }
    281                               break;
    282                          }
    283                     }
    284                }
    285           }
    286           if (init_list[i].arg)
    287           {
    288                continue;
    289           }
    290 
    291           /* Initialize save filenames */
    292           for (i = 0; init_save[i].arg; ++i)
    293           {
    294                if (!StrCompare(init_save[i].arg, parm))
    295                {
    296                     if (init_save[i].min < init_save[i].max-1)
    297                     {
    298                          if (!StrParam(parm, sizeof(parm), buffer, 1))
    299                          {
    300                               j = init_save[i].min++;
    301                               (init_save[i].ptr)[j] = StrDuplicate(parm);
    302                               break;
    303                          }
    304                     }
    305                }
    306           }
    307           if (init_save[i].arg)
    308           {
    309                continue;
    310           }
    311 
    312           /* Initialize the lists to read */
    313           for (i = 0; init_read[i].arg; ++i)
    314           {
    315                if (!StrCompare(init_read[i].arg, parm))
    316                {
    317                     if (!StrParam(parm, sizeof(parm), buffer, 1))
    318                     {
    319                          if (init_read[i].save)
    320                          {
    321                               *init_read[i].save = StrDuplicate(parm);
    322                          }
    323                          if ((f = fopen(parm, "r")))
    324                          {
    325                               fill_table(init_read[i].ptr, init_read[i].max, f);
    326                               fclose(f);
    327                          }
    328                          else if (!init_read[i].save)
    329                          {
    330                               printf("Cannot open the %s.\n", init_read[i].arg);
    331                               exit(EXIT_FAILURE);
    332                          }
    333                          break;
    334                     }
    335                }
    336           }
    337           if (init_read[i].arg)
    338           {
    339                continue;
    340           }
    341 
    342           /* Initialize the string parameters */
    343           for (i = 0; init_str[i].arg; ++i)
    344           {
    345                if (!StrCompare(init_str[i].arg, parm))
    346                {
    347                     if (!StrParam(parm, sizeof(parm), buffer, 1))
    348                     {
    349                          *init_str[i].ptr = StrDuplicate(parm);
    350                          break;
    351                     }
    352                }
    353           }
    354           if (init_str[i].arg)
    355           {
    356                continue;
    357           }
    358      }
    359 
    360      fclose(config);
    361 }
    362 /* }}} */