Files
energymech/src/toybox.c

376 lines
6.9 KiB
C
Raw Normal View History

2014-03-08 19:56:21 -05:00
/*
EnergyMech, IRC bot software
Copyright (c) 2000-2018 proton
2014-03-08 19:56:21 -05:00
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 2 of the License, 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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define TOYBOX_C
#include "config.h"
#ifdef TOYBOX
#include "defines.h"
#include "structs.h"
#include "global.h"
#include "h.h"
#include "text.h"
#include "mcmd.h"
#define BIGSAY_DEFAULTFONT "default"
#define FONT_EXTENSION ".bigchars"
2018-03-27 17:28:48 +02:00
BigC *newchar;
BigC *orig_fontlist = NULL;
int orig_charlines;
int orig_charheight;
int orig_spacewidth;
int orig_kerning;
2014-03-08 19:56:21 -05:00
int read_bigcharset_callback(char *rest)
{
Strp *sp,**pp;
char *opt;
int *n,sz;
if (charlines)
{
charlines--;
sz = strlen(rest);
if (sz > newchar->width)
newchar->width = sz;
2018-03-26 00:43:24 +02:00
append_strp(&newchar->data,rest);
2014-03-08 19:56:21 -05:00
return(FALSE);
}
opt = chop(&rest);
n = NULL;
2018-04-04 06:04:58 +02:00
if (!stringcasecmp(opt,"chars") && charheight)
2014-03-08 19:56:21 -05:00
{
charlines = charheight;
opt = chop(&rest);
set_mallocdoer(read_bigcharset_callback);
newchar = (BigC*)Calloc(sizeof(BigC) + strlen(opt));
/* Calloc sets to zero
newchar->width = 0;
newchar->data = NULL;
*/
newchar->next = fontlist;
fontlist = newchar;
2018-04-04 06:04:58 +02:00
stringcpy(newchar->chars,opt);
2014-03-08 19:56:21 -05:00
}
else
2018-04-04 06:04:58 +02:00
if (!stringcasecmp(opt,"spacewidth"))
2014-03-08 19:56:21 -05:00
{
n = &spacewidth;
}
else
2018-04-04 06:04:58 +02:00
if (!stringcasecmp(opt,"charheight"))
2014-03-08 19:56:21 -05:00
{
n = &charheight;
}
else
2018-04-04 06:04:58 +02:00
if (!stringcasecmp(opt,"kerning"))
2014-03-08 19:56:21 -05:00
{
n = &kerning;
}
else
2018-04-04 06:04:58 +02:00
if (!stringcasecmp(opt,"fontname"))
2014-03-08 19:56:21 -05:00
{
opt = chop(&rest);
2018-04-04 06:04:58 +02:00
if (fontname && !stringcasecmp(fontname,opt))
2014-03-08 19:56:21 -05:00
{
fontlist = orig_fontlist;
charlines = orig_charlines;
charheight = orig_charheight;
spacewidth = orig_spacewidth;
kerning = orig_kerning;
orig_fontlist = NULL;
return(TRUE);
}
Free((char**)&fontname);
set_mallocdoer(read_bigcharset_callback);
2018-04-04 06:04:58 +02:00
fontname = stringdup(opt);
2014-03-08 19:56:21 -05:00
}
if (n)
{
2018-04-04 06:04:58 +02:00
*n = asc2int(rest);
2014-03-08 19:56:21 -05:00
if (errno) *n = 0;
}
return(FALSE);
}
int read_bigcharset(char *fname)
{
BigC *bigc;
Strp *sp;
int fd;
if ((fd = open(fname,O_RDONLY)) < 0)
{
2018-04-04 06:04:58 +02:00
stringcat(fname,FONT_EXTENSION);
2014-03-08 19:56:21 -05:00
if ((fd = open(fname,O_RDONLY)) < 0)
return(-1);
}
orig_fontlist = fontlist;
orig_charlines = charlines;
orig_charheight = charheight;
orig_spacewidth = spacewidth;
orig_kerning = kerning;
charlines = 0;
spacewidth = 0;
charheight = 0;
kerning = 0;
readline(fd,&read_bigcharset_callback); /* readline closes fd */
/*
* free the old font if there is one
*/
while(orig_fontlist)
{
bigc = orig_fontlist;
orig_fontlist = bigc->next;
2018-03-27 23:26:34 +02:00
if (bigc->data)
purge_strplist(bigc->data);
2014-03-08 19:56:21 -05:00
Free((char**)&bigc);
}
return(0);
}
/*
*
*
*
*/
void do_bigsay(COMMAND_ARGS)
{
/*
* on_msg checks CARGS + CAXS
*/
BigC *bigc;
Strp *sp;
char output[MSGLEN];
char *pt,*tail,*temp,*temp2;
2014-03-08 19:56:21 -05:00
int i,x,sz;
#ifdef DEBUG
debug("(do_bigsay) rest = \"%s\"\n",rest);
#endif /* DEBUG */
2018-04-04 06:04:58 +02:00
stringcpy(output,BIGSAY_DEFAULTFONT);
2014-03-08 19:56:21 -05:00
if (read_bigcharset(output) < 0)
{
to_user(from,ERR_FILEOPEN,output);
return;
}
#define OEND (output + MSGLEN - 1)
2014-03-08 19:56:21 -05:00
for(i=0;i<charheight;i++)
{
sz = 0;
*output = 0;
tail = output;
for(pt=rest;*pt;pt++)
{
/* find a matching character */
if (*pt == ' ')
{
x = spacewidth;
while(x-- && tail < OEND)
2014-03-08 19:56:21 -05:00
*(tail++) = ' ';
*tail = 0;
continue;
}
for(bigc=fontlist;bigc;bigc=bigc->next)
{
if (STRCHR(bigc->chars,*pt))
{
sp = bigc->data;
for(x=0;x<i;x++)
if (sp) sp = sp->next;
2018-03-27 17:28:48 +02:00
temp = (sp) ? sp->p : "";
temp2 = tail;
while(*temp && tail < OEND)
*(tail++) = *(temp++);
while(tail < (temp2 + bigc->width) && tail < OEND)
*(tail++) = ' ';
2014-03-08 19:56:21 -05:00
if (pt[1])
{
x = kerning;
while(x-- && tail < OEND)
*(tail++) = ' ';
2014-03-08 19:56:21 -05:00
}
*tail = 0;
2014-03-08 19:56:21 -05:00
break;
}
}
}
temp = NULL;
for(tail=output;*tail;tail++)
{
if (!temp && *tail == ' ')
temp = tail;
if (*tail != ' ')
temp = NULL;
}
if (temp)
{
if (temp == output)
temp++;
*temp = 0;
}
to_user_q(from,FMT_PLAIN,output);
}
}
void do_random_msg(COMMAND_ARGS)
{
2018-04-04 06:04:58 +02:00
const char *filename;
const char *message;
2014-03-08 19:56:21 -05:00
filename = CurrentCmd->cmdarg;
if (*rest)
{
to = chop(&rest);
if (ischannel(to) && (get_authaccess(from,to) < cmdaccess))
return;
}
else
if (!ischannel(to))
{
usage(from); /* usage for CurrentCmd->name */
return;
}
if ((message = randstring(filename)) == NULL)
{
to_user(from,ERR_FILEOPEN,filename);
return;
}
/*
* send message to target nick/channel
*/
to_server("PRIVMSG %s :%s\n",to,message);
/*
* if its not a channel we send a copy to the user who did the command also
*/
if (!ischannel(to))
{
to_user(from,"(%s) %s",to,message);
}
}
void do_randtopic(COMMAND_ARGS)
{
/*
* on_msg checks CAXS
*/
Chan *chan = CurrentChan;
char *topic;
/*
* the bot can set a random topic if chanmode is -t
*/
if (chan->bot_is_op || chan->topprot == FALSE)
{
if ((topic = randstring(RANDTOPICSFILE)) == NULL)
{
to_user(from,ERR_FILEOPEN,RANDTOPICSFILE);
return;
}
to_server("TOPIC %s :%s\n",to,topic);
to_user(from,TEXT_TOPICCHANGED,to);
return;
}
to_user(from,ERR_NOTOPPED,to);
}
void do_8ball(COMMAND_ARGS)
{
char *message;
if ((message = randstring(RAND8BALLFILE)) == NULL)
{
to_user_q(from,ERR_FILEOPEN,RAND8BALLFILE);
return;
}
to_user_q(from,FMT_PLAIN,message);
}
2018-03-26 01:52:24 +02:00
char *ascii_from;
int read_ascii(char *rest)
{
to_user_q(ascii_from,FMT_PLAIN,rest);
}
void do_ascii(COMMAND_ARGS)
{
char fname[MSGLEN];
int fd;
#ifdef DEBUG
2018-03-27 01:48:21 +02:00
if (STRCHR(rest,'/'))
{
debug("(do_ascii) '/' not permitted in filename\n");
ascii_badfile:
to_user_q(from,"%s","Bad filename or file does not exist");
return;
}
2018-04-04 06:04:58 +02:00
stringcat(stringcpy(fname,"ascii/"),rest);
2018-03-26 01:52:24 +02:00
debug("(do_ascii) file = \"%s\"\n",fname);
if (is_safepath(fname,FILE_MUST_EXIST) != FILE_IS_SAFE)
2018-03-27 01:48:21 +02:00
{
debug("(do_ascii) is_safepath() not safe\n");
goto ascii_badfile;
}
2018-03-26 01:52:24 +02:00
if ((fd = open(fname,O_RDONLY)) < 0)
2018-03-27 01:48:21 +02:00
{
debug("(do_ascii) open(%s): %s\n",fname,strerror(errno));
goto ascii_badfile;
}
#else
if (STRCHR(rest,'/'))
{
ascii_badfile:
to_user_q(from,"%s","Bad filename or file does not exist");
2018-03-26 01:52:24 +02:00
return;
2018-03-27 01:48:21 +02:00
}
2018-04-04 06:04:58 +02:00
stringcat(stringcpy2(fname,"ascii/"),rest);
2018-03-27 01:48:21 +02:00
if (is_safepath(fname,FILE_MUST_EXIST) != FILE_IS_SAFE)
goto ascii_badfile;
if ((fd = open(fname,O_RDONLY)) < 0)
goto ascii_badfile;
#endif
2018-03-26 01:52:24 +02:00
ascii_from = from;
readline(fd,&read_ascii); /* readline closes fd */
}
2014-03-08 19:56:21 -05:00
#endif /* TOYBOX */