SHARE
TWEET

Untitled

a guest May 19th, 2017 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***************************************************************************************/
  2. /* Anope Module : cs_chanban_info.c : v1.x                                             */
  3. /* Phil Lavin - phil@geekshed.net                                                      */
  4. /*                                                                                     */
  5. /* (c) 2010 Phil Lavin                                                                 */
  6. /*                                                                                     */
  7. /* Fairly heavily based on ircd_community_info.c by katsklaw                           */
  8. /*                                                                                     */
  9. /* This program is free software; you can redistribute it and/or modify it under the   */
  10. /* terms of the GNU General Public License as published by the Free Software           */
  11. /* Foundation; either version 1, or (at your option) any later version.                */
  12. /*                                                                                     */
  13. /*  This program is distributed in the hope that it will be useful, but WITHOUT ANY    */
  14. /*  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A    */
  15. /*  PARTICULAR PURPOSE.  See the GNU General Public License for more details.          */
  16. /*                                                                                     */
  17. /***************************************************************************************/
  18.  
  19. #include <module.h>
  20.  
  21. #define AUTHOR "Phil"
  22. #define VERSION "1.0.0"
  23.  
  24. /* Default database name */
  25. #define DEFAULT_DB_NAME "chanban_info.db"
  26.  
  27. /* Multi-language stuff */
  28. #define LANG_NUM_STRINGS   5
  29.  
  30. #define CCHANBAN_ADD_SUCCESS  0
  31. #define CCHANBAN_DEL_SUCCESS  1
  32. #define CCHANBAN_HELP         2
  33. #define CCHANBAN_HELP_CMD     3
  34.  
  35. /*************************************************************************/
  36.  
  37. char *chanBanDBName = NULL;
  38.  
  39. int myAddBanAppealInfo(User * u);
  40. int myChanInfo(User * u);
  41.  
  42. int mBanAppealHelp(User * u);
  43. int mMainSetHelp(User * u);
  44. void m_AddLanguages(void);
  45.  
  46. int mLoadData(void);
  47. int mSaveData(int argc, char **argv);
  48. int mBackupData(int argc, char **argv);
  49. int mLoadConfig();
  50. int mEventReload(int argc, char **argv);
  51.  
  52. /*************************************************************************/
  53.  
  54. /**
  55. * AnopeInit is called when the module is loaded
  56. * @param argc Argument count
  57. * @param argv Argument list
  58. * @return MOD_CONT to allow the module, MOD_STOP to stop it
  59. **/
  60. int AnopeInit(int argc, char **argv)
  61. {
  62.     Command *c;
  63.     EvtHook *hook = NULL;
  64.  
  65.     int status;
  66.  
  67.     moduleAddAuthor(AUTHOR);
  68.     moduleAddVersion(VERSION);
  69.  
  70.     alog("cs_chanban_info: Loading configuration directives...");
  71.     if (mLoadConfig()) {
  72.         return MOD_STOP;
  73.     }
  74.  
  75.     c = createCommand("SET", myAddBanAppealInfo, NULL, -1, -1, -1, -1, -1);
  76.     status = moduleAddCommand(CHANSERV, c, MOD_HEAD);
  77.  
  78.     c = createCommand("SET", NULL, NULL, -1, -1, -1, -1, -1);
  79.     moduleAddHelp(c, mMainSetHelp);
  80.     status = moduleAddCommand(CHANSERV, c, MOD_TAIL);
  81.  
  82.     c = createCommand("SET BANINFO", NULL, NULL, -1, -1, -1, -1, -1);
  83.     moduleAddHelp(c, mBanAppealHelp);
  84.     moduleAddCommand(CHANSERV, c, MOD_HEAD);
  85.  
  86.     c = createCommand("INFO", myChanInfo, NULL, -1, -1, -1, -1, -1);
  87.     status = moduleAddCommand(CHANSERV, c, MOD_TAIL);
  88.  
  89.     hook = createEventHook(EVENT_DB_SAVING, mSaveData);
  90.     status = moduleAddEventHook(hook);
  91.  
  92.     hook = createEventHook(EVENT_DB_BACKUP, mBackupData);
  93.     status = moduleAddEventHook(hook);
  94.  
  95.     hook = createEventHook(EVENT_RELOAD, mEventReload);
  96.     status = moduleAddEventHook(hook);
  97.  
  98.     mLoadData();
  99.     m_AddLanguages();
  100.  
  101.     return MOD_CONT;
  102. }
  103.  
  104. /**
  105. * Unload the module
  106. **/
  107. void AnopeFini(void)
  108. {
  109.     char *av[1];
  110.  
  111.     av[0] = sstrdup(EVENT_START);
  112.     mSaveData(1, av);
  113.     free(av[0]);
  114.  
  115.     if (chanBanDBName)
  116.     free(chanBanDBName);
  117. }
  118.  
  119. /*************************************************************************/
  120.  
  121. /**
  122. * Provide the user interface to add/remove/update ban appeal information
  123. * about a channel.
  124. * @param u The user who executed this command
  125. * @return MOD_CONT if we want to process other commands in this command
  126. * stack, MOD_STOP if we dont
  127. **/
  128. int myAddBanAppealInfo(User * u)
  129. {
  130.     char *text = NULL;
  131.     char *cmd = NULL;
  132.     char *chan = NULL;
  133.     char *info = NULL;
  134.     ChannelInfo *ci = NULL;
  135.  
  136.     /* Get the last buffer anope recived */
  137.     text = moduleGetLastBuffer();
  138.     if (text) {
  139.         chan = myStrGetToken(text, ' ', 0);
  140.         cmd = myStrGetToken(text, ' ', 1);
  141.         info = myStrGetTokenRemainder(text, ' ', 2);
  142.  
  143.         if (cmd && chan) {
  144.             alog("in 167");
  145.  
  146.             /* Get channel if it exists */
  147.             if (!(ci = cs_findchan(chan))) {
  148.                 alog("in 467");
  149.                 notice_lang(s_ChanServ, u, CHAN_X_NOT_REGISTERED,
  150.                 chan);
  151.                 return MOD_STOP;
  152.             }
  153.  
  154.             if (strcasecmp(cmd, "BANINFO") == 0) {
  155.                 alog("in 199");
  156.  
  157.                 /* Info has been specified - i.e. ban info is being set */
  158.                 if (info) {
  159.                     /* Add the module data to the channel */
  160.                     moduleAddData(&ci->moduleData, "chanbaninfo", info);
  161.                     moduleNoticeLang(s_ChanServ, u,
  162.                     CCHANBAN_ADD_SUCCESS, chan);
  163.                     free(info);
  164.                 }
  165.                 /* Info has NOT been specified - i.e. ban info is being unset */
  166.                 else {
  167.                     /* Del the module data from the channel */
  168.                     moduleDelData(&ci->moduleData, "chanbaninfo");
  169.                     moduleNoticeLang(s_ChanServ, u,
  170.                     CCHANBAN_DEL_SUCCESS, chan);
  171.                 }
  172.  
  173.                 free(cmd);
  174.                 free(chan);
  175.                 return MOD_STOP;
  176.             }
  177.  
  178.             alog("in 123");
  179.  
  180.             free(cmd);
  181.             free(chan);
  182.         }
  183.  
  184.         alog("in 456");
  185.     }
  186.  
  187.     alog("in 789");
  188.  
  189.     return MOD_CONT;
  190. }
  191.  
  192. /*************************************************************************/
  193.  
  194. /**
  195. * Called after a user does a /msg chanserv info chan
  196. * @param u The user who requested info
  197. * @return MOD_CONT to continue processing commands or MOD_STOP to stop
  198. **/
  199. int myChanInfo(User * u)
  200. {
  201.     char *text = NULL;
  202.     char *chan = NULL;
  203.     char *info = NULL;
  204.     ChannelInfo *ci = NULL;
  205.  
  206.     /* Get the last buffer anope recived */
  207.     text = moduleGetLastBuffer();
  208.     if (text) {
  209.         chan = myStrGetToken(text, ' ', 0);
  210.         if (chan) {
  211.             if ((ci = cs_findchan(chan))) {
  212.                 /* If we have any info on this channel */
  213.                 if ((info = moduleGetData(&ci->moduleData, "chanbaninfo"))) {
  214.                     notice_user(s_ChanServ, u, "Ban appeal information: %s", info);
  215.                     free(info);
  216.                 }
  217.             }
  218.             free(chan);
  219.         }
  220.     }
  221.     return MOD_CONT;
  222. }
  223.  
  224. /*************************************************************************/
  225.  
  226. /**
  227. * Load data from the db file, and populate our ChanBanInfo lines
  228. * @return 0 for success
  229. **/
  230. int mLoadData(void)
  231. {
  232.     int ret = 0;
  233.     FILE *in;
  234.  
  235.     char *type = NULL;
  236.     char *name = NULL;
  237.     char *info = NULL;
  238.     int len = 0;
  239.  
  240.     ChannelInfo *ci = NULL;
  241.  
  242.     /* will _never_ be this big thanks to the 512 limit of a message */
  243.     char buffer[2000];
  244.     if ((in = fopen(chanBanDBName, "r")) == NULL) {
  245.         alog("cs_chanban_info: WARNING: can not open the database file! (it might not exist, this is not fatal)");
  246.         ret = 1;
  247.     } else {
  248.         while (fgets(buffer, 1500, in)) {
  249.             type = myStrGetToken(buffer, ' ', 0);
  250.             name = myStrGetToken(buffer, ' ', 1);
  251.             info = myStrGetTokenRemainder(buffer, ' ', 2);
  252.             if (type) {
  253.                 if (name) {
  254.                     if (info) {
  255.                         len = strlen(info);
  256.                         /* Take the \n from the end of the line */
  257.                         info[len - 1] = '\0';
  258.                         if (stricmp(type, "C") == 0) {
  259.                             if ((ci = cs_findchan(name))) {
  260.                                 moduleAddData(&ci->moduleData, "chanbaninfo",
  261.                                 info);
  262.                             }
  263.                         }
  264.                         free(info);
  265.                     }
  266.                     free(name);
  267.                 }
  268.                 free(type);
  269.             }
  270.         }
  271.     }
  272.     return ret;
  273. }
  274.  
  275. /**
  276. * Save all our data to our db file
  277. * First walk through the nick CORE list, and any nick core which has
  278. * oper info attached to it, write to the file.
  279. * Next do the same again for ChannelInfos
  280. * @return 0 for success
  281. **/
  282. int mSaveData(int argc, char **argv)
  283. {
  284.     ChannelInfo *ci = NULL;
  285.     int i = 0;
  286.     int ret = 0;
  287.     FILE *out;
  288.     char *info = NULL;
  289.  
  290.     if (argc >= 1) {
  291.         if (!stricmp(argv[0], EVENT_START)) {
  292.             if ((out = fopen(chanBanDBName, "w")) == NULL) {
  293.                 alog("cs_chanban_info: ERROR: can not open the database file!");
  294.                 anope_cmd_global(s_OperServ,
  295.                 "cs_chanban_info.c: ERROR: can not open the database file!");
  296.                 ret = 1;
  297.             } else {
  298.                 for (i = 0; i < 256; i++) {
  299.                     for (ci = chanlists[i]; ci; ci = ci->next) {
  300.                         /* If we have any info on this channel */
  301.                         if ((info = moduleGetData(&ci->moduleData, "chanbaninfo"))) {
  302.                             fprintf(out, "C %s %s\n", ci->name, info);
  303.                             free(info);
  304.                         }
  305.                     }
  306.                 }
  307.                 fclose(out);
  308.             }
  309.         } else {
  310.             ret = 0;
  311.         }
  312.     }
  313.  
  314.     return ret;
  315. }
  316.  
  317. /**
  318. * Backup our databases using the commands provided by Anope
  319. * @return MOD_CONT
  320. **/
  321. int mBackupData(int argc, char **argv)
  322. {
  323.     ModuleDatabaseBackup(chanBanDBName);
  324.  
  325.     return MOD_CONT;
  326. }
  327.  
  328. /**
  329. * Load the configuration directives from Services configuration file.
  330. * @return 0 for success
  331. **/
  332. int mLoadConfig(void)
  333. {
  334.     char *tmp = NULL;
  335.  
  336.     Directive directivas[] = {
  337.         {"chanBanDBName", {{PARAM_STRING, PARAM_RELOAD, &tmp}}},
  338.     };
  339.  
  340.     Directive *d = &directivas[0];
  341.     moduleGetConfigDirective(d);
  342.  
  343.     if (chanBanDBName)
  344.     free(chanBanDBName);
  345.  
  346.     if (tmp) {
  347.         chanBanDBName = tmp;
  348.     } else {
  349.         chanBanDBName = sstrdup(DEFAULT_DB_NAME);
  350.         alog("cs_chanban_info: chanBanDBName is not defined in Services configuration file, using default %s", chanBanDBName);
  351.     }
  352.  
  353.     alog("cs_chanban_info: Directive ChanBan loaded (%s)...", chanBanDBName);
  354.  
  355.     return 0;
  356. }
  357.  
  358. /**
  359. * Manage the RELOAD EVENT
  360. * @return MOD_CONT
  361. **/
  362. int mEventReload(int argc, char **argv)
  363. {
  364.     int ret = 0;
  365.     if (argc >= 1) {
  366.         if (!stricmp(argv[0], EVENT_START)) {
  367.             alog("cs_chanban_info: Reloading configuration directives...");
  368.             ret = mLoadConfig();
  369.         } else {
  370.             /* Nothing for now */
  371.         }
  372.     }
  373.  
  374.     if (ret)
  375.     alog("cs_chanban_info: ERROR: An error has occured while reloading the configuration file");
  376.  
  377.     return MOD_CONT;
  378. }
  379.  
  380. /*************************************************************************/
  381.  
  382. /**
  383. * manages the multilanguage stuff
  384. **/
  385. void m_AddLanguages(void)
  386. {
  387.     char *langtable_en_us[] = {
  388.         /* CCHANBAN_ADD_SUCCESS */
  389.         "Ban appeal info line has been added to %s",
  390.         /* CCHANBAN_DEL_SUCCESS */
  391.         "Ban appeal info line has been removed from %s",
  392.         /* CCHANBAN_HELP */
  393.         "Syntax: SET channel BANINFO [info]\n"
  394.         "Associates the given ban appeal information with the channel. This\n"
  395.         "information will be displayed whenever someone requests information on\n"
  396.         "the channel with the INFO command. If no parameter is given, the currently\n"
  397.         "associated ban appeal information will be deleted" ,
  398.         /* CCHANBAN_HELP_CMD */
  399.         "    BANINFO         Add / Del a ban appeal line to a channel"
  400.     };
  401.  
  402.     moduleInsertLanguage(LANG_EN_US, LANG_NUM_STRINGS, langtable_en_us);
  403. }
  404.  
  405. /*************************************************************************/
  406.  
  407. int mBanAppealHelp(User * u)
  408. {
  409.     moduleNoticeLang(s_ChanServ, u, CCHANBAN_HELP);
  410.  
  411.     return MOD_CONT;
  412. }
  413.  
  414. int mMainSetHelp(User * u)
  415. {
  416.     moduleNoticeLang(s_ChanServ, u, CCHANBAN_HELP_CMD);
  417.  
  418.     return MOD_CONT;
  419. }
  420.  
  421. /*************************************************************************/
  422.  
  423. /* EOF */
RAW Paste Data
Top