Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************************************************/
- /* Anope Module : cs_chanban_info.c : v1.x */
- /* Phil Lavin - phil@geekshed.net */
- /* */
- /* (c) 2010 Phil Lavin */
- /* */
- /* Fairly heavily based on ircd_community_info.c by katsklaw */
- /* */
- /* This program is free software; you can redistribute it and/or modify it under the */
- /* terms of the GNU General Public License as published by the Free Software */
- /* Foundation; either version 1, or (at your option) any later version. */
- /* */
- /* This program is distributed in the hope that it will be useful, but WITHOUT ANY */
- /* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A */
- /* PARTICULAR PURPOSE. See the GNU General Public License for more details. */
- /* */
- /***************************************************************************************/
- #include <module.h>
- #define AUTHOR "Phil"
- #define VERSION "1.0.0"
- /* Default database name */
- #define DEFAULT_DB_NAME "chanban_info.db"
- /* Multi-language stuff */
- #define LANG_NUM_STRINGS 5
- #define CCHANBAN_ADD_SUCCESS 0
- #define CCHANBAN_DEL_SUCCESS 1
- #define CCHANBAN_HELP 2
- #define CCHANBAN_HELP_CMD 3
- /*************************************************************************/
- char *chanBanDBName = NULL;
- int myAddBanAppealInfo(User * u);
- int myChanInfo(User * u);
- int mBanAppealHelp(User * u);
- int mMainSetHelp(User * u);
- void m_AddLanguages(void);
- int mLoadData(void);
- int mSaveData(int argc, char **argv);
- int mBackupData(int argc, char **argv);
- int mLoadConfig();
- int mEventReload(int argc, char **argv);
- /*************************************************************************/
- /**
- * AnopeInit is called when the module is loaded
- * @param argc Argument count
- * @param argv Argument list
- * @return MOD_CONT to allow the module, MOD_STOP to stop it
- **/
- int AnopeInit(int argc, char **argv)
- {
- Command *c;
- EvtHook *hook = NULL;
- int status;
- moduleAddAuthor(AUTHOR);
- moduleAddVersion(VERSION);
- alog("cs_chanban_info: Loading configuration directives...");
- if (mLoadConfig()) {
- return MOD_STOP;
- }
- c = createCommand("SET", myAddBanAppealInfo, NULL, -1, -1, -1, -1, -1);
- status = moduleAddCommand(CHANSERV, c, MOD_HEAD);
- c = createCommand("SET", NULL, NULL, -1, -1, -1, -1, -1);
- moduleAddHelp(c, mMainSetHelp);
- status = moduleAddCommand(CHANSERV, c, MOD_TAIL);
- c = createCommand("SET BANINFO", NULL, NULL, -1, -1, -1, -1, -1);
- moduleAddHelp(c, mBanAppealHelp);
- moduleAddCommand(CHANSERV, c, MOD_HEAD);
- c = createCommand("INFO", myChanInfo, NULL, -1, -1, -1, -1, -1);
- status = moduleAddCommand(CHANSERV, c, MOD_TAIL);
- hook = createEventHook(EVENT_DB_SAVING, mSaveData);
- status = moduleAddEventHook(hook);
- hook = createEventHook(EVENT_DB_BACKUP, mBackupData);
- status = moduleAddEventHook(hook);
- hook = createEventHook(EVENT_RELOAD, mEventReload);
- status = moduleAddEventHook(hook);
- mLoadData();
- m_AddLanguages();
- return MOD_CONT;
- }
- /**
- * Unload the module
- **/
- void AnopeFini(void)
- {
- char *av[1];
- av[0] = sstrdup(EVENT_START);
- mSaveData(1, av);
- free(av[0]);
- if (chanBanDBName)
- free(chanBanDBName);
- }
- /*************************************************************************/
- /**
- * Provide the user interface to add/remove/update ban appeal information
- * about a channel.
- * @param u The user who executed this command
- * @return MOD_CONT if we want to process other commands in this command
- * stack, MOD_STOP if we dont
- **/
- int myAddBanAppealInfo(User * u)
- {
- char *text = NULL;
- char *cmd = NULL;
- char *chan = NULL;
- char *info = NULL;
- ChannelInfo *ci = NULL;
- /* Get the last buffer anope recived */
- text = moduleGetLastBuffer();
- if (text) {
- chan = myStrGetToken(text, ' ', 0);
- cmd = myStrGetToken(text, ' ', 1);
- info = myStrGetTokenRemainder(text, ' ', 2);
- if (cmd && chan) {
- alog("in 167");
- /* Get channel if it exists */
- if (!(ci = cs_findchan(chan))) {
- alog("in 467");
- notice_lang(s_ChanServ, u, CHAN_X_NOT_REGISTERED,
- chan);
- return MOD_STOP;
- }
- if (strcasecmp(cmd, "BANINFO") == 0) {
- alog("in 199");
- /* Info has been specified - i.e. ban info is being set */
- if (info) {
- /* Add the module data to the channel */
- moduleAddData(&ci->moduleData, "chanbaninfo", info);
- moduleNoticeLang(s_ChanServ, u,
- CCHANBAN_ADD_SUCCESS, chan);
- free(info);
- }
- /* Info has NOT been specified - i.e. ban info is being unset */
- else {
- /* Del the module data from the channel */
- moduleDelData(&ci->moduleData, "chanbaninfo");
- moduleNoticeLang(s_ChanServ, u,
- CCHANBAN_DEL_SUCCESS, chan);
- }
- free(cmd);
- free(chan);
- return MOD_STOP;
- }
- alog("in 123");
- free(cmd);
- free(chan);
- }
- alog("in 456");
- }
- alog("in 789");
- return MOD_CONT;
- }
- /*************************************************************************/
- /**
- * Called after a user does a /msg chanserv info chan
- * @param u The user who requested info
- * @return MOD_CONT to continue processing commands or MOD_STOP to stop
- **/
- int myChanInfo(User * u)
- {
- char *text = NULL;
- char *chan = NULL;
- char *info = NULL;
- ChannelInfo *ci = NULL;
- /* Get the last buffer anope recived */
- text = moduleGetLastBuffer();
- if (text) {
- chan = myStrGetToken(text, ' ', 0);
- if (chan) {
- if ((ci = cs_findchan(chan))) {
- /* If we have any info on this channel */
- if ((info = moduleGetData(&ci->moduleData, "chanbaninfo"))) {
- notice_user(s_ChanServ, u, "Ban appeal information: %s", info);
- free(info);
- }
- }
- free(chan);
- }
- }
- return MOD_CONT;
- }
- /*************************************************************************/
- /**
- * Load data from the db file, and populate our ChanBanInfo lines
- * @return 0 for success
- **/
- int mLoadData(void)
- {
- int ret = 0;
- FILE *in;
- char *type = NULL;
- char *name = NULL;
- char *info = NULL;
- int len = 0;
- ChannelInfo *ci = NULL;
- /* will _never_ be this big thanks to the 512 limit of a message */
- char buffer[2000];
- if ((in = fopen(chanBanDBName, "r")) == NULL) {
- alog("cs_chanban_info: WARNING: can not open the database file! (it might not exist, this is not fatal)");
- ret = 1;
- } else {
- while (fgets(buffer, 1500, in)) {
- type = myStrGetToken(buffer, ' ', 0);
- name = myStrGetToken(buffer, ' ', 1);
- info = myStrGetTokenRemainder(buffer, ' ', 2);
- if (type) {
- if (name) {
- if (info) {
- len = strlen(info);
- /* Take the \n from the end of the line */
- info[len - 1] = '\0';
- if (stricmp(type, "C") == 0) {
- if ((ci = cs_findchan(name))) {
- moduleAddData(&ci->moduleData, "chanbaninfo",
- info);
- }
- }
- free(info);
- }
- free(name);
- }
- free(type);
- }
- }
- }
- return ret;
- }
- /**
- * Save all our data to our db file
- * First walk through the nick CORE list, and any nick core which has
- * oper info attached to it, write to the file.
- * Next do the same again for ChannelInfos
- * @return 0 for success
- **/
- int mSaveData(int argc, char **argv)
- {
- ChannelInfo *ci = NULL;
- int i = 0;
- int ret = 0;
- FILE *out;
- char *info = NULL;
- if (argc >= 1) {
- if (!stricmp(argv[0], EVENT_START)) {
- if ((out = fopen(chanBanDBName, "w")) == NULL) {
- alog("cs_chanban_info: ERROR: can not open the database file!");
- anope_cmd_global(s_OperServ,
- "cs_chanban_info.c: ERROR: can not open the database file!");
- ret = 1;
- } else {
- for (i = 0; i < 256; i++) {
- for (ci = chanlists[i]; ci; ci = ci->next) {
- /* If we have any info on this channel */
- if ((info = moduleGetData(&ci->moduleData, "chanbaninfo"))) {
- fprintf(out, "C %s %s\n", ci->name, info);
- free(info);
- }
- }
- }
- fclose(out);
- }
- } else {
- ret = 0;
- }
- }
- return ret;
- }
- /**
- * Backup our databases using the commands provided by Anope
- * @return MOD_CONT
- **/
- int mBackupData(int argc, char **argv)
- {
- ModuleDatabaseBackup(chanBanDBName);
- return MOD_CONT;
- }
- /**
- * Load the configuration directives from Services configuration file.
- * @return 0 for success
- **/
- int mLoadConfig(void)
- {
- char *tmp = NULL;
- Directive directivas[] = {
- {"chanBanDBName", {{PARAM_STRING, PARAM_RELOAD, &tmp}}},
- };
- Directive *d = &directivas[0];
- moduleGetConfigDirective(d);
- if (chanBanDBName)
- free(chanBanDBName);
- if (tmp) {
- chanBanDBName = tmp;
- } else {
- chanBanDBName = sstrdup(DEFAULT_DB_NAME);
- alog("cs_chanban_info: chanBanDBName is not defined in Services configuration file, using default %s", chanBanDBName);
- }
- alog("cs_chanban_info: Directive ChanBan loaded (%s)...", chanBanDBName);
- return 0;
- }
- /**
- * Manage the RELOAD EVENT
- * @return MOD_CONT
- **/
- int mEventReload(int argc, char **argv)
- {
- int ret = 0;
- if (argc >= 1) {
- if (!stricmp(argv[0], EVENT_START)) {
- alog("cs_chanban_info: Reloading configuration directives...");
- ret = mLoadConfig();
- } else {
- /* Nothing for now */
- }
- }
- if (ret)
- alog("cs_chanban_info: ERROR: An error has occured while reloading the configuration file");
- return MOD_CONT;
- }
- /*************************************************************************/
- /**
- * manages the multilanguage stuff
- **/
- void m_AddLanguages(void)
- {
- char *langtable_en_us[] = {
- /* CCHANBAN_ADD_SUCCESS */
- "Ban appeal info line has been added to %s",
- /* CCHANBAN_DEL_SUCCESS */
- "Ban appeal info line has been removed from %s",
- /* CCHANBAN_HELP */
- "Syntax: SET channel BANINFO [info]\n"
- "Associates the given ban appeal information with the channel. This\n"
- "information will be displayed whenever someone requests information on\n"
- "the channel with the INFO command. If no parameter is given, the currently\n"
- "associated ban appeal information will be deleted" ,
- /* CCHANBAN_HELP_CMD */
- " BANINFO Add / Del a ban appeal line to a channel"
- };
- moduleInsertLanguage(LANG_EN_US, LANG_NUM_STRINGS, langtable_en_us);
- }
- /*************************************************************************/
- int mBanAppealHelp(User * u)
- {
- moduleNoticeLang(s_ChanServ, u, CCHANBAN_HELP);
- return MOD_CONT;
- }
- int mMainSetHelp(User * u)
- {
- moduleNoticeLang(s_ChanServ, u, CCHANBAN_HELP_CMD);
- return MOD_CONT;
- }
- /*************************************************************************/
- /* EOF */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement