1 /* calcdb.c version 0.7 */ 2 /* all versions previous to 0.333 are completely public domain */ 3 /* all contributed code falls under the same BSD style license as */ 4 /* noted below unless the contributing author places a copyright */ 5 /* notice in their file/s. */ 6 7 8 /* 9 * * Copyright (c) 2001 David T. Stiles 10 * * All rights reserved. 11 * * 12 * * Redistribution and use in source and binary forms, with or without 13 * * modification, are permitted provided that the following conditions 14 * * are met: 15 * * 1. Redistributions of source code must retain the above copyright 16 * * notice, this list of conditions and the following disclaimer. 17 * * 2. Redistributions in binary form must reproduce the above copyright 18 * * notice, this list of conditions and the following disclaimer in the 19 * * documentation and/or other materials provided with the distribution. 20 * * 3. All advertising materials mentioning features or use of this software 21 * * must display the following acknowledgement: 22 * * This product includes software developed by David T. Stiles 23 * * 4. The name David T. Stiles may not be used to endorse or promote 24 * * products derived from this software without specific prior written 25 * * permission. 26 * * 27 * * THIS SOFTWARE IS PROVIDED BY DAVID T. STILES `AS IS'' AND ANY 28 * * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * * ARE DISCLAIMED. IN NO EVENT SHALL DAVID T. STILES BE LIABLE 31 * * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * * SUCH DAMAGE. 38 * */ 39 40 /* this code would not be possible without the patience and intelligence */ 41 /* provided by many of the people from #c/efnet. I thank all of you sincerely. */ 42 43 /* the calc database code, being rewritten on 12 January 2001 */ 44 45 #include "calcdb.h" 46 #include "bot.h" 47 #include "users.h" 48 49 50 /* 51 * this holds the name of the person/channel we will talk to. set in bot.c 52 */ 53 extern char MSGTO[MAXDATASIZE]; 54 55 56 /* 57 * variables needed by many functions in the module. 58 */ 59 60 static char **calc = NULL; /* will become an array of pointers, each pointer pointing to an array. */ 61 static long total_calcs = 0; /* ummmm, duh */ 62 static int MAXCALCS; /* duh again. passed into loaddb */ 63 static char CALCDB[MAXDATASIZE]; /* passed into loaddb, path/filename of the calc database */ 64 65 66 67 68 void owncalc( char *name, char *dbindex, char *nick ) 69 { 70 char string[MAXDATASIZE]; 71 register int x, index; 72 char tmpray[MAXDATASIZE]; 73 char calcname[MAXDATASIZE]; 74 char calcowner[MAXDATASIZE]; 75 76 if( name[0] ) strncpy( string, name, MAXDATASIZE ); 77 else strncpy( string, nick, MAXDATASIZE ); 78 79 tmpray[0] = '\0'; 80 for( x = atol( dbindex ); (x < total_calcs) && (x >= 0); x++ ) { 81 if( !(*(calc + x)) ) return; 82 index = chop( *(calc + x), calcname, 0, ' ' ); 83 index = chop( *(calc + x), calcowner, index, '|' ); 84 if( !strncasecmp( string, calcname, MAXDATASIZE ) ) { 85 strncat( tmpray, calcowner, (MAXDATASIZE - 50) ); 86 strncat( tmpray, " ", (MAXDATASIZE - 50) ); 87 } 88 } 89 90 snprintf( string, MAXDATASIZE, "PRIVMSG %s :index: %i. results: %s", MSGTO, x - 1, tmpray ); 91 send_irc_message( string ); 92 93 return; 94 } 95 96 97 98 void listcalc( char *name, char *dbindex, char *nick ) 99 { 100 register int x, index; 101 char tmpray[MAXDATASIZE]; 102 char calcname[MAXDATASIZE]; 103 char calcowner[MAXDATASIZE]; 104 char string[MAXDATASIZE]; 105 106 107 if( name[0] ) strncpy( string, name, MAXDATASIZE ); 108 else strncpy( string, nick, MAXDATASIZE ); 109 110 tmpray[0] = '\0'; 111 for( x = atol( dbindex ); (x < total_calcs) && (x >= 0); x++ ) { 112 if( !(*(calc + x)) ) return; 113 index = chop( *(calc + x), calcname, 0, ' ' ); 114 index = chop( *(calc + x), calcowner, index, '|' ); 115 if( strncasecmp( string, calcowner, MAXDATASIZE ) ) continue; 116 if( (strlen(tmpray) + strlen(calcname)) > (MAXDATASIZE - 50) ) break; 117 strncat( tmpray, calcname, (MAXDATASIZE - 50) ); 118 strncat( tmpray, " ", (MAXDATASIZE - 50) ); 119 } 120 121 snprintf( string, MAXDATASIZE, "PRIVMSG %s :last index: %i. results: %s", MSGTO, x - 1, tmpray ); 122 send_irc_message( string ); 123 124 return; 125 } 126 127 128 129 void searchcalc( char *searchkey, char *dbindex ) 130 { 131 register int x, index; 132 char tmpray[MAXDATASIZE]; 133 char calcname[MAXDATASIZE]; 134 char calcowner[MAXDATASIZE]; 135 char calcdata[MAXDATASIZE]; 136 char string[MAXDATASIZE]; 137 char *ptr = NULL; 138 139 140 if( !searchkey[0] ) strncpy( string, MSGTO, MAXDATASIZE ); 141 else strncpy( string, searchkey, MAXDATASIZE ); 142 143 tmpray[0] = '\0'; 144 for( x = atol( dbindex ); (x < total_calcs) && (x >= 0); x++ ) { 145 if( !(*(calc + x)) ) return; 146 index = chop( *(calc + x), calcname, 0, ' ' ); 147 index = chop( *(calc + x), calcowner, index, '|' ); 148 index = chop( *(calc + x), calcdata, index, '\0' ); 149 ptr = strstr( calcdata, string ); 150 if( ptr == NULL ) continue; 151 if( (strlen(tmpray) + strlen(calcname)) > (MAXDATASIZE - 50) ) break; 152 if( (strlen(tmpray) + strlen(calcname)) > (MAXDATASIZE - 50) ) break; 153 strncat( tmpray, calcname, (MAXDATASIZE - 50) ); 154 strncat( tmpray, " ", (MAXDATASIZE - 50) ); 155 } 156 157 snprintf( string, MAXDATASIZE, "PRIVMSG %s :index: %i. results: %s", MSGTO, x - 1, tmpray ); 158 send_irc_message( string ); 159 160 return; 161 } 162 163 164 165 void rmcalc( char *passwd, char *name, char *rmstring ) 166 { 167 int x; 168 char *ptr; 169 char sndmsg[MAXDATASIZE]; 170 171 if( !valid_login( name, passwd ) ){ 172 snprintf( sndmsg, MAXDATASIZE, "PRIVMSG %s :failed login", MSGTO ); 173 send_irc_message( sndmsg ); 174 return; 175 } 176 177 x = findcalc( rmstring ); 178 179 if( x == -1) { 180 snprintf( sndmsg, MAXDATASIZE, "PRIVMSG %s :%s not found.", MSGTO, rmstring ); 181 send_irc_message( sndmsg ); 182 return; 183 } 184 185 total_calcs--; 186 187 if( x == total_calcs ) { 188 free( *(calc + x) ); 189 snprintf( sndmsg, MAXDATASIZE, "PRIVMSG %s :%s removed.", MSGTO, rmstring ); 190 send_irc_message( sndmsg ); 191 savedb( CALCDB ); 192 return; 193 } 194 195 ptr = *(calc + x); 196 *(calc + x) = *(calc + total_calcs); 197 free( ptr ); 198 ptr = NULL; 199 snprintf( sndmsg, MAXDATASIZE, "PRIVMSG %s :%s removed.", MSGTO, rmstring ); 200 send_irc_message( sndmsg ); 201 202 savedb( CALCDB ); 203 204 return; 205 } 206 207 208 209 void mkcalc( char *pass, char *name, char *newcalc, char *newcalcdata ) 210 { 211 char sndmsg[MAXDATASIZE]; 212 213 if( !valid_login( name, pass ) ){ 214 snprintf( sndmsg, MAXDATASIZE, "PRIVMSG %s :failed login", MSGTO ); 215 send_irc_message( sndmsg ); 216 return; 217 } 218 219 if( newcalc[0] == '\0' ) { 220 snprintf( sndmsg, MAXDATASIZE, "PRIVMSG %s :No calc name provided.", MSGTO ); 221 send_irc_message( sndmsg ); 222 return; 223 } 224 225 if( newcalcdata[0] == '\0' ) { 226 snprintf( sndmsg, MAXDATASIZE, "PRIVMSG %s :You can not make an empty calc.", MSGTO ); 227 send_irc_message( sndmsg ); 228 return; 229 } 230 231 if( findcalc( newcalc ) != -1) { 232 snprintf( sndmsg, MAXDATASIZE, "PRIVMSG %s :calc exists", MSGTO ); 233 send_irc_message( sndmsg ); 234 return; 235 } 236 237 *(calc + total_calcs) = calloc( sizeof( char * ), MAXDATASIZE ); 238 if( !(*(calc + total_calcs)) ) return; 239 240 snprintf( *(calc + total_calcs), MAXDATASIZE, "%s %s|%s", newcalc, name, newcalcdata ); 241 snprintf( sndmsg, MAXDATASIZE, "PRIVMSG %s :calc %s added.", MSGTO, newcalc ); 242 send_irc_message( sndmsg ); 243 244 total_calcs++; 245 savedb( CALCDB ); 246 247 return; 248 } 249 250 251 252 void docalc( char *calcstring ) 253 { 254 int x, y; 255 char tmpray[MAXDATASIZE], calcray[MAXDATASIZE]; 256 257 x = findcalc( calcstring ); 258 259 if( x >= 0 ) { 260 y = chop( (*(calc + x)), calcray, 0, ' ' ); 261 y = chop( (*(calc + x)), calcray, y, '|' ); 262 y = chop( (*(calc + x)), calcray, y, '\n' ); 263 } 264 else strncpy( calcray, "calc not found.", MAXDATASIZE ); 265 266 snprintf( tmpray, MAXDATASIZE,"privmsg %s :%s", MSGTO, calcray ); 267 268 send_irc_message( tmpray ); 269 return; 270 } 271 272 273 274 int findcalc( char *string ) 275 { 276 register int x, index; 277 char tmpray[MAXDATASIZE]; 278 279 for( x = 0; x < total_calcs; x++ ) { 280 if( !(*(calc + x)) ) return -1; 281 index = chop( *(calc + x), tmpray, 0, ' ' ); 282 if( index ) if( !strncasecmp( string, tmpray, MAXDATASIZE ) ) return x; 283 } 284 285 return -1; 286 } 287 288 289 290 int savedb( char *filename ) 291 { 292 FILE *fp; 293 register int x; 294 295 fp = fopen( filename, "w" ); 296 if( !fp ) { puts( "could not open calcs" ); return 1; } 297 298 for( x = 0; x < total_calcs; x++ ) { 299 if( !(*(calc + x)) ) break; 300 fputs( *(calc + x), fp ); 301 fputc( '\n', fp ); 302 } 303 304 fclose( fp ); 305 return 0; 306 } 307 308 309 310 int loaddb( char *filename, int maxdbsize ) 311 { 312 FILE *fp; 313 register int x; 314 315 strncpy( CALCDB, filename, MAXDATASIZE ); 316 MAXCALCS = maxdbsize; 317 318 fp = fopen( CALCDB, "r" ); 319 if( !fp ) { puts( "could not open calcs" ); return 1; } 320 321 calc = calloc( sizeof( char * ), MAXCALCS ); 322 if( !calc ) { puts( "inital allocation error in loaddb()." ); return 1; } 323 324 for( x = 0; x < MAXCALCS; x++ ) { 325 *(calc + x) = calloc( sizeof( char * ), MAXDATASIZE ); 326 if( !(*(calc + x)) ) { puts( "memory allocation failed" ); break; } 327 fgets( *(calc + x), MAXDATASIZE, fp ); 328 if( feof( fp ) ) { 329 fclose( fp ); 330 free(*(calc + x)); 331 *(calc + x) = NULL; 332 return 0; 333 } 334 clean_message( *(calc + x) ); 335 total_calcs++; 336 } 337 338 fclose( fp ); 339 340 return 0; 341 } |