Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

flt_listhighlight.c

Go to the documentation of this file.
00001 
00008 /* {{{ Initial comments */
00009 /*
00010  * $LastChangedDate: 2004-04-01 18:34:17 +0200 (Thu, 01 Apr 2004) $
00011  * $LastChangedRevision: 50 $
00012  * $LastChangedBy: ckruse $
00013  *
00014  */
00015 /* }}} */
00016 
00017 /* {{{ Includes */
00018 #include "config.h"
00019 #include "defines.h"
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <ctype.h>
00025 #include <time.h>
00026 #include <sys/types.h>
00027 
00028 #include "readline.h"
00029 #include "hashlib.h"
00030 #include "utils.h"
00031 #include "configparser.h"
00032 #include "cfcgi.h"
00033 #include "template.h"
00034 #include "clientlib.h"
00035 /* }}} */
00036 
00037 struct {
00038   int HighlightOwnPostings;
00039   u_char *OwnPostingsColorF;
00040   u_char *OwnPostingsColorB;
00041   t_cf_hash *WhiteList;
00042   u_char *WhiteListColorF;
00043   u_char *WhiteListColorB;
00044   t_cf_hash *HighlightCategories;
00045   u_char *CategoryHighlightColorF;
00046   u_char *CategoryHighlightColorB;
00047   t_cf_hash *VIPList;
00048   u_char *VIPColorF;
00049   u_char *VIPColorB;
00050 } Cfg = { 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
00051 
00052 void parse_list(u_char *vips,t_cf_hash *hash) {
00053   if(vips) {
00054     u_char *ptr = vips;
00055     u_char *pos = ptr,*pre = ptr;
00056 
00057     while((pos = strstr(pos,",")) != NULL) {
00058       *pos = 0;
00059 
00060       cf_hash_set(hash,pre,pos-pre,"1",1);
00061 
00062       pre    = pos+1;
00063       *pos++ = ',';
00064     }
00065 
00066     cf_hash_set(hash,pre,strlen(pre),"1",1); /* argh! This sucks */
00067   }
00068 }
00069 
00070 u_char *tolwer(const u_char *str,register int *len) {
00071   register u_char *ptr = (u_char *)str,*ptr1;
00072   u_char *result = strdup(str);
00073 
00074   *len = 0;
00075   ptr1 = result;
00076 
00077   while(*ptr) {
00078     *ptr1++ = tolower(*ptr++);
00079     *len += 1;
00080   }
00081 
00082   return result;
00083 }
00084 
00085 int execute_filter(t_cf_hash *head,t_configuration *dc,t_configuration *vc,t_message *msg,unsigned long long tid,int mode) {
00086   t_name_value *uname = NULL;
00087   int len = 0;
00088   u_char *tmp;
00089   u_char *UserName = cf_hash_get(GlobalValues,"UserName",8);
00090 
00091   /*
00092    * Initialization
00093    */
00094   if(UserName) {
00095     uname = cfg_get_value(vc,"Name");
00096   }
00097 
00098   if(!Cfg.VIPList && !Cfg.WhiteList && !Cfg.HighlightCategories && !Cfg.HighlightOwnPostings) {
00099     return FLT_DECLINE;
00100   }
00101 
00102   if(Cfg.VIPList) {
00103     tmp = tolwer(msg->author,&len);
00104     if(cf_hash_get(Cfg.VIPList,tmp,len)) {
00105       tpl_cf_setvar(&msg->tpl,"vip","1",1,0);
00106     }
00107     free(tmp);
00108   }
00109 
00110   if(Cfg.WhiteList) {
00111     tmp = tolwer(msg->author,&len);
00112     if(cf_hash_get(Cfg.WhiteList,tmp,len)) {
00113       tpl_cf_setvar(&msg->tpl,"whitelist","1",1,0);
00114     }
00115     free(tmp);
00116   }
00117 
00118   if(Cfg.HighlightCategories && msg->category) {
00119     if(cf_hash_get(Cfg.HighlightCategories,msg->category,strlen(msg->category))) {
00120       tpl_cf_setvar(&msg->tpl,"cathigh","1",1,0);
00121     }
00122   }
00123 
00124   if(Cfg.HighlightOwnPostings && uname) {
00125     if(cf_strcasecmp(msg->author,uname->values[0]) == 0) {
00126       tpl_cf_setvar(&msg->tpl,"ownposting","1",1,0);
00127     }
00128   }
00129 
00130   return FLT_OK;
00131 }
00132 
00133 void flt_lh_cleanup(void) {
00134   if(Cfg.OwnPostingsColorF)       free(Cfg.OwnPostingsColorF);
00135   if(Cfg.OwnPostingsColorB)       free(Cfg.OwnPostingsColorB);
00136   if(Cfg.WhiteList)               cf_hash_destroy(Cfg.WhiteList);
00137   if(Cfg.WhiteListColorF)         free(Cfg.WhiteListColorF);
00138   if(Cfg.WhiteListColorB)         free(Cfg.WhiteListColorB);
00139   if(Cfg.HighlightCategories)     cf_hash_destroy(Cfg.HighlightCategories);
00140   if(Cfg.CategoryHighlightColorF) free(Cfg.CategoryHighlightColorF);
00141   if(Cfg.CategoryHighlightColorB) free(Cfg.CategoryHighlightColorB);
00142   if(Cfg.VIPList)                 cf_hash_destroy(Cfg.VIPList);
00143   if(Cfg.VIPColorF)               free(Cfg.VIPColorF);
00144   if(Cfg.VIPColorB)               free(Cfg.VIPColorB);
00145 }
00146 
00147 int flt_lh_set_colors(t_cf_hash *head,t_configuration *dc,t_configuration *vc,t_cf_template *begin,t_cf_template *end) {
00148   t_name_value *cs = cfg_get_value(dc,"ExternCharset");
00149 
00150   if(Cfg.VIPColorF || Cfg.VIPColorB) {
00151     tpl_cf_setvar(begin,"vipcol","1",1,0);
00152 
00153     if(Cfg.VIPColorF && *Cfg.VIPColorF) {
00154       cf_set_variable(begin,cs,"vipcolfg",Cfg.VIPColorF,strlen(Cfg.VIPColorF),1);
00155     }
00156     if(Cfg.VIPColorB && *Cfg.VIPColorB) {
00157       cf_set_variable(begin,cs,"vipcolbg",Cfg.VIPColorB,strlen(Cfg.VIPColorB),1);
00158     }
00159   }
00160 
00161   if(Cfg.WhiteListColorF || Cfg.WhiteListColorB) {
00162     tpl_cf_setvar(begin,"wlcol","1",1,0);
00163 
00164     if(Cfg.WhiteListColorF && *Cfg.WhiteListColorF) {
00165       cf_set_variable(begin,cs,"wlcolfg",Cfg.WhiteListColorF,strlen(Cfg.WhiteListColorF),0);
00166     }
00167     if(Cfg.WhiteListColorB && *Cfg.WhiteListColorB) {
00168       cf_set_variable(begin,cs,"wlcolbg",Cfg.WhiteListColorB,strlen(Cfg.WhiteListColorB),0);
00169     }
00170   }
00171 
00172   if(Cfg.OwnPostingsColorF || Cfg.OwnPostingsColorB) {
00173     tpl_cf_setvar(begin,"colorown","1",1,0);
00174 
00175     if(Cfg.OwnPostingsColorF && *Cfg.OwnPostingsColorF) {
00176       cf_set_variable(begin,cs,"colorownfg",Cfg.OwnPostingsColorF,strlen(Cfg.OwnPostingsColorF),1);
00177     }
00178     if(Cfg.OwnPostingsColorB && *Cfg.OwnPostingsColorB) {
00179       cf_set_variable(begin,cs,"colorownbg",Cfg.OwnPostingsColorB,strlen(Cfg.OwnPostingsColorB),1);
00180     }
00181   }
00182 
00183   if(Cfg.CategoryHighlightColorF || Cfg.CategoryHighlightColorB) {
00184     tpl_cf_setvar(begin,"cathighcolor","1",1,0);
00185 
00186     if(Cfg.CategoryHighlightColorF && *Cfg.CategoryHighlightColorF) {
00187       cf_set_variable(begin,cs,"cathighcolorfg",Cfg.CategoryHighlightColorF,strlen(Cfg.CategoryHighlightColorF),1);
00188     }
00189     if(Cfg.CategoryHighlightColorB && *Cfg.CategoryHighlightColorB) {
00190       cf_set_variable(begin,cs,"cathighcolorbg",Cfg.CategoryHighlightColorB,strlen(Cfg.CategoryHighlightColorB),1);
00191     }
00192   }
00193 
00194   return FLT_OK;
00195 }
00196 
00197 int flt_lh_set_cols_p(t_cf_hash *head,t_configuration *dc,t_configuration *vc,t_cl_thread *thread,t_cf_template *tpl) {
00198   return flt_lh_set_colors(head,dc,vc,tpl,NULL);
00199 }
00200 
00201 int flt_lh_handle_command(t_configfile *cf,t_conf_opt *opt,u_char **args,int argnum) {
00202   int len;
00203   u_char *list;
00204 
00205   if(cf_strcmp(opt->name,"HighlightOwnPostings") == 0) {
00206     Cfg.HighlightOwnPostings = !cf_strcmp(args[0],"yes");
00207   }
00208   else if(cf_strcmp(opt->name,"OwnPostingsColors") == 0) {
00209     if(Cfg.OwnPostingsColorF) free(Cfg.OwnPostingsColorF);
00210     if(Cfg.OwnPostingsColorB) free(Cfg.OwnPostingsColorB);
00211     Cfg.OwnPostingsColorF = strdup(args[0]);
00212     Cfg.OwnPostingsColorB = strdup(args[1]);
00213   }
00214   else if(cf_strcmp(opt->name,"WhiteList") == 0) {
00215     if(!Cfg.WhiteList) Cfg.WhiteList = cf_hash_new(NULL);
00216     list = tolwer(args[0],&len);
00217     parse_list(list,Cfg.WhiteList);
00218     free(list);
00219   }
00220   else if(cf_strcmp(opt->name,"WhiteListColors") == 0) {
00221     if(Cfg.WhiteListColorF) free(Cfg.WhiteListColorF);
00222     if(Cfg.WhiteListColorB) free(Cfg.WhiteListColorB);
00223     Cfg.WhiteListColorF = strdup(args[0]);
00224     Cfg.WhiteListColorB = strdup(args[1]);
00225   }
00226   else if(cf_strcmp(opt->name,"HighlightCategories") == 0) {
00227     if(!Cfg.HighlightCategories) Cfg.HighlightCategories = cf_hash_new(NULL);
00228     parse_list((u_char *)args[0],Cfg.HighlightCategories);
00229   }
00230   else if(cf_strcmp(opt->name,"CategoryHighlightColors") == 0) {
00231     if(Cfg.CategoryHighlightColorF) free(Cfg.CategoryHighlightColorF);
00232     if(Cfg.CategoryHighlightColorB) free(Cfg.CategoryHighlightColorB);
00233     Cfg.CategoryHighlightColorF = strdup(args[0]);
00234     Cfg.CategoryHighlightColorB = strdup(args[1]);
00235   }
00236   else if(cf_strcmp(opt->name,"VIPList") == 0) {
00237     if(!Cfg.VIPList) Cfg.VIPList = cf_hash_new(NULL);
00238     list = tolwer(args[0],&len);
00239     parse_list(list,Cfg.VIPList);
00240     free(list);
00241   }
00242   else if(cf_strcmp(opt->name,"VIPColors") == 0) {
00243     if(Cfg.VIPColorF) free(Cfg.VIPColorF);
00244     if(Cfg.VIPColorB) free(Cfg.VIPColorB);
00245     Cfg.VIPColorF = strdup(args[0]);
00246     Cfg.VIPColorB = strdup(args[1]);
00247   }
00248 
00249   return 0;
00250 }
00251 
00252 t_conf_opt config[] = {
00253   { "HighlightOwnPostings",    flt_lh_handle_command, NULL },
00254   { "OwnPostingsColors",       flt_lh_handle_command, NULL },
00255   { "WhiteList",               flt_lh_handle_command, NULL },
00256   { "WhiteListColors",         flt_lh_handle_command, NULL },
00257   { "HighlightCategories",     flt_lh_handle_command, NULL },
00258   { "CategoryHighlightColors", flt_lh_handle_command, NULL },
00259   { "VIPList",                 flt_lh_handle_command, NULL },
00260   { "VIPColors",               flt_lh_handle_command, NULL },
00261   { NULL, NULL, NULL }
00262 };
00263 
00264 t_handler_config handlers[] = {
00265   { VIEW_LIST_HANDLER, execute_filter    },
00266   { VIEW_INIT_HANDLER, flt_lh_set_colors },
00267   { POSTING_HANDLER,   flt_lh_set_cols_p },
00268   { 0, NULL }
00269 };
00270 
00271 t_module_config flt_listhighlight = {
00272   config,
00273   handlers,
00274   NULL,
00275   NULL,
00276   NULL,
00277   flt_lh_cleanup
00278 };
00279 
00280 /* eof */

Generated on Sun Apr 25 16:37:38 2004 for Classic Forum by doxygen 1.3.5