os_sgline.c

Go to the documentation of this file.
00001 /* OperServ core functions
00002  *
00003  * (C) 2003-2007 Anope Team
00004  * Contact us at info@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church. 
00010  * 
00011  * $Id: os_sgline.c 1288 2007-08-29 18:40:53Z geniusdex $
00012  *
00013  */
00014 /*************************************************************************/
00015 
00016 #include "module.h"
00017 
00018 int sgline_view_callback(SList * slist, int number, void *item,
00019                          va_list args);
00020 int sgline_view(int number, SXLine * sx, User * u, int *sent_header);
00021 int sgline_list_callback(SList * slist, int number, void *item,
00022                          va_list args);
00023 int sgline_list(int number, SXLine * sx, User * u, int *sent_header);
00024 int do_sgline(User * u);
00025 
00026 void myOperServHelp(User * u);
00027 
00034 int AnopeInit(int argc, char **argv)
00035 {
00036     Command *c;
00037 
00038     moduleAddAuthor("Anope");
00039     moduleAddVersion("$Id: os_sgline.c 1288 2007-08-29 18:40:53Z geniusdex $");
00040     moduleSetType(CORE);
00041 
00042     c = createCommand("SGLINE", do_sgline, is_services_oper,
00043                       OPER_HELP_SGLINE, -1, -1, -1, -1);
00044     moduleAddCommand(OPERSERV, c, MOD_UNIQUE);
00045 
00046     moduleSetOperHelp(myOperServHelp);
00047 
00048     if (!ircd->sgline) {
00049         return MOD_STOP;
00050     }
00051     return MOD_CONT;
00052 }
00053 
00057 void AnopeFini(void)
00058 {
00059 
00060 }
00061 
00062 
00067 void myOperServHelp(User * u)
00068 {
00069     if (is_services_oper(u)) {
00070         notice_lang(s_OperServ, u, OPER_HELP_CMD_SGLINE);
00071     }
00072 }
00073 
00079 int do_sgline(User * u)
00080 {
00081     char *cmd = strtok(NULL, " ");
00082 
00083     if (!cmd)
00084         cmd = "";
00085 
00086     if (!stricmp(cmd, "ADD")) {
00087         int deleted = 0;
00088         char *expiry, *mask, *reason;
00089         time_t expires;
00090 
00091         mask = strtok(NULL, ":");
00092         if (mask && *mask == '+') {
00093             expiry = mask;
00094             mask = strchr(expiry, ' ');
00095             if (mask) {
00096                 *mask = 0;
00097                 mask++;
00098             }
00099         } else {
00100             expiry = NULL;
00101         }
00102 
00103         expires = expiry ? dotime(expiry) : SGLineExpiry;
00104         /* If the expiry given does not contain a final letter, it's in days,
00105          * said the doc. Ah well.
00106          */
00107         if (expiry && isdigit(expiry[strlen(expiry) - 1]))
00108             expires *= 86400;
00109         /* Do not allow less than a minute expiry time */
00110         if (expires != 0 && expires < 60) {
00111             notice_lang(s_OperServ, u, BAD_EXPIRY_TIME);
00112             return MOD_CONT;
00113         } else if (expires > 0) {
00114             expires += time(NULL);
00115         }
00116 
00117         if (mask && (reason = strtok(NULL, ""))) {
00118             /* Clean up the last character of the mask if it is a space
00119              * See bug #761
00120              */
00121             size_t masklen = strlen(mask);
00122             if (mask[masklen - 1] == ' ')
00123                 mask[masklen - 1] = '\0';
00124             
00125             /* We first do some sanity check on the proposed mask. */
00126 
00127             if (mask && strspn(mask, "*?") == strlen(mask)) {
00128                 notice_lang(s_OperServ, u, USERHOST_MASK_TOO_WIDE, mask);
00129                 return MOD_CONT;
00130             }
00131 
00132             deleted = add_sgline(u, mask, u->nick, expires, reason);
00133             if (deleted < 0)
00134                 return MOD_CONT;
00135             else if (deleted)
00136                 notice_lang(s_OperServ, u, OPER_SGLINE_DELETED_SEVERAL,
00137                             deleted);
00138             notice_lang(s_OperServ, u, OPER_SGLINE_ADDED, mask);
00139 
00140             if (WallOSSGLine) {
00141                 char buf[128];
00142 
00143                 if (!expires) {
00144                     strcpy(buf, "does not expire");
00145                 } else {
00146                     int wall_expiry = expires - time(NULL);
00147                     char *s = NULL;
00148 
00149                     if (wall_expiry >= 86400) {
00150                         wall_expiry /= 86400;
00151                         s = "day";
00152                     } else if (wall_expiry >= 3600) {
00153                         wall_expiry /= 3600;
00154                         s = "hour";
00155                     } else if (wall_expiry >= 60) {
00156                         wall_expiry /= 60;
00157                         s = "minute";
00158                     }
00159 
00160                     snprintf(buf, sizeof(buf), "expires in %d %s%s",
00161                              wall_expiry, s,
00162                              (wall_expiry == 1) ? "" : "s");
00163                 }
00164 
00165                 anope_cmd_global(s_OperServ,
00166                                  "%s added an SGLINE for %s (%s)", u->nick,
00167                                  mask, buf);
00168             }
00169 
00170             if (readonly)
00171                 notice_lang(s_OperServ, u, READ_ONLY_MODE);
00172 
00173         } else {
00174             syntax_error(s_OperServ, u, "SGLINE", OPER_SGLINE_SYNTAX);
00175         }
00176 
00177     } else if (!stricmp(cmd, "DEL")) {
00178 
00179         char *mask;
00180         int res = 0;
00181 
00182         mask = strtok(NULL, "");
00183 
00184         if (!mask) {
00185             syntax_error(s_OperServ, u, "SGLINE", OPER_SGLINE_SYNTAX);
00186             return MOD_CONT;
00187         }
00188 
00189         if (sglines.count == 0) {
00190             notice_lang(s_OperServ, u, OPER_SGLINE_LIST_EMPTY);
00191             return MOD_CONT;
00192         }
00193 
00194         if (isdigit(*mask) && strspn(mask, "1234567890,-") == strlen(mask)) {
00195             /* Deleting a range */
00196             res = slist_delete_range(&sglines, mask, NULL);
00197             if (res == 0) {
00198                 notice_lang(s_OperServ, u, OPER_SGLINE_NO_MATCH);
00199                 return MOD_CONT;
00200             } else if (res == 1) {
00201                 notice_lang(s_OperServ, u, OPER_SGLINE_DELETED_ONE);
00202             } else {
00203                 notice_lang(s_OperServ, u, OPER_SGLINE_DELETED_SEVERAL,
00204                             res);
00205             }
00206         } else {
00207             if ((res = slist_indexof(&sglines, mask)) == -1) {
00208                 notice_lang(s_OperServ, u, OPER_SGLINE_NOT_FOUND, mask);
00209                 return MOD_CONT;
00210             }
00211 
00212             slist_delete(&sglines, res);
00213             notice_lang(s_OperServ, u, OPER_SGLINE_DELETED, mask);
00214         }
00215 
00216         if (readonly)
00217             notice_lang(s_OperServ, u, READ_ONLY_MODE);
00218 
00219     } else if (!stricmp(cmd, "LIST")) {
00220         char *mask;
00221         int res, sent_header = 0;
00222 
00223         if (sglines.count == 0) {
00224             notice_lang(s_OperServ, u, OPER_SGLINE_LIST_EMPTY);
00225             return MOD_CONT;
00226         }
00227 
00228         mask = strtok(NULL, "");
00229 
00230         if (!mask || (isdigit(*mask)
00231                       && strspn(mask, "1234567890,-") == strlen(mask))) {
00232             res =
00233                 slist_enum(&sglines, mask, &sgline_list_callback, u,
00234                            &sent_header);
00235             if (res == 0) {
00236                 notice_lang(s_OperServ, u, OPER_SGLINE_NO_MATCH);
00237                 return MOD_CONT;
00238             }
00239         } else {
00240             int i;
00241             char *amask;
00242 
00243             for (i = 0; i < sglines.count; i++) {
00244                 amask = ((SXLine *) sglines.list[i])->mask;
00245                 if (!stricmp(mask, amask)
00246                     || match_wild_nocase(mask, amask))
00247                     sgline_list(i + 1, sglines.list[i], u, &sent_header);
00248             }
00249 
00250             if (!sent_header)
00251                 notice_lang(s_OperServ, u, OPER_SGLINE_NO_MATCH);
00252             else {
00253                 notice_lang(s_OperServ, u, END_OF_ANY_LIST, "SGLine");
00254             }
00255         }
00256     } else if (!stricmp(cmd, "VIEW")) {
00257         char *mask;
00258         int res, sent_header = 0;
00259 
00260         if (sglines.count == 0) {
00261             notice_lang(s_OperServ, u, OPER_SGLINE_LIST_EMPTY);
00262             return MOD_CONT;
00263         }
00264 
00265         mask = strtok(NULL, "");
00266 
00267         if (!mask || (isdigit(*mask)
00268                       && strspn(mask, "1234567890,-") == strlen(mask))) {
00269             res =
00270                 slist_enum(&sglines, mask, &sgline_view_callback, u,
00271                            &sent_header);
00272             if (res == 0) {
00273                 notice_lang(s_OperServ, u, OPER_SGLINE_NO_MATCH);
00274                 return MOD_CONT;
00275             }
00276         } else {
00277             int i;
00278             char *amask;
00279 
00280             for (i = 0; i < sglines.count; i++) {
00281                 amask = ((SXLine *) sglines.list[i])->mask;
00282                 if (!stricmp(mask, amask)
00283                     || match_wild_nocase(mask, amask))
00284                     sgline_view(i + 1, sglines.list[i], u, &sent_header);
00285             }
00286 
00287             if (!sent_header)
00288                 notice_lang(s_OperServ, u, OPER_SGLINE_NO_MATCH);
00289         }
00290     } else if (!stricmp(cmd, "CLEAR")) {
00291         slist_clear(&sglines, 1);
00292         notice_lang(s_OperServ, u, OPER_SGLINE_CLEAR);
00293     } else {
00294         syntax_error(s_OperServ, u, "SGLINE", OPER_SGLINE_SYNTAX);
00295     }
00296     return MOD_CONT;
00297 }
00298 
00299 /* Lists an SGLINE entry, prefixing it with the header if needed */
00300 
00301 int sgline_view(int number, SXLine * sx, User * u, int *sent_header)
00302 {
00303     char timebuf[32], expirebuf[256];
00304     struct tm tm;
00305 
00306     if (!sx)
00307         return 0;
00308 
00309     if (!*sent_header) {
00310         notice_lang(s_OperServ, u, OPER_SGLINE_VIEW_HEADER);
00311         *sent_header = 1;
00312     }
00313 
00314     tm = *localtime(&sx->seton);
00315     strftime_lang(timebuf, sizeof(timebuf), u, STRFTIME_SHORT_DATE_FORMAT,
00316                   &tm);
00317     expire_left(u->na, expirebuf, sizeof(expirebuf), sx->expires);
00318     notice_lang(s_OperServ, u, OPER_SGLINE_VIEW_FORMAT, number, sx->mask,
00319                 sx->by, timebuf, expirebuf, sx->reason);
00320 
00321     return 1;
00322 }
00323 
00324 /* Callback for enumeration purposes */
00325 
00326 int sgline_view_callback(SList * slist, int number, void *item,
00327                          va_list args)
00328 {
00329     User *u = va_arg(args, User *);
00330     int *sent_header = va_arg(args, int *);
00331 
00332     return sgline_view(number, item, u, sent_header);
00333 }
00334 
00335 /* Lists an SGLINE entry, prefixing it with the header if needed */
00336 
00337 int sgline_list(int number, SXLine * sx, User * u, int *sent_header)
00338 {
00339     if (!sx)
00340         return 0;
00341 
00342     if (!*sent_header) {
00343         notice_lang(s_OperServ, u, OPER_SGLINE_LIST_HEADER);
00344         *sent_header = 1;
00345     }
00346 
00347     notice_lang(s_OperServ, u, OPER_SGLINE_LIST_FORMAT, number, sx->mask,
00348                 sx->reason);
00349 
00350     return 1;
00351 }
00352 
00353 /* Callback for enumeration purposes */
00354 
00355 int sgline_list_callback(SList * slist, int number, void *item,
00356                          va_list args)
00357 {
00358     User *u = va_arg(args, User *);
00359     int *sent_header = va_arg(args, int *);
00360 
00361     return sgline_list(number, item, u, sent_header);
00362 }

Generated on Sun Dec 30 09:26:48 2007 for Anope by  doxygen 1.5.1-20070107