Skip to content

Commit

Permalink
Various improvements eg rc file (.quirc)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sound and Fury committed Jul 27, 2010
1 parent 5a53f10 commit 709dc6b
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
*~ *~
quirc quirc
*.o *.o
.quirc-shadow
7 changes: 5 additions & 2 deletions Makefile
Expand Up @@ -5,8 +5,8 @@ CFLAGS ?= -Wall


all: quirc all: quirc


quirc: quirc.c ttyraw.o ttyraw.h ttyesc.o ttyesc.h irc.o irc.h numeric.h version.h quirc: quirc.c ttyraw.o ttyraw.h ttyesc.o ttyesc.h irc.o irc.h bits.o bits.h numeric.h version.h
$(CC) $(CFLAGS) -o quirc quirc.c ttyraw.o ttyesc.o irc.o $(CC) $(CFLAGS) -o quirc quirc.c ttyraw.o ttyesc.o irc.o bits.o


ttyraw.o: ttyraw.c ttyraw.h ttyraw.o: ttyraw.c ttyraw.h
$(CC) $(CFLAGS) -o ttyraw.o -c ttyraw.c $(CC) $(CFLAGS) -o ttyraw.o -c ttyraw.c
Expand All @@ -17,3 +17,6 @@ ttyesc.o: ttyesc.c ttyesc.h
irc.o: irc.c irc.h irc.o: irc.c irc.h
$(CC) $(CFLAGS) -o irc.o -c irc.c $(CC) $(CFLAGS) -o irc.o -c irc.c


bits.o: bits.c bits.h
$(CC) $(CFLAGS) -o bits.o -c bits.c

36 changes: 36 additions & 0 deletions bits.c
@@ -0,0 +1,36 @@
#include "bits.h"

char * fgetl(FILE *fp)
{
char * lout = (char *)malloc(81);
int i=0;
signed int c;
while(!feof(fp))
{
c=fgetc(fp);
if(c==EOF) // EOF without '\n' - we'd better put an '\n' in
c='\n';
if(c!=0)
{
lout[i++]=c;
if((i%80)==0)
{
if((lout=(char *)realloc(lout, i+81))==NULL)
{
printf("\nNot enough memory to store input!\n");
free(lout);
return(NULL);
}
}
}
if(c=='\n') // we do want to keep them this time
break;
}
lout[i]=0;
char *nlout=(char *)realloc(lout, i+1);
if(nlout==NULL)
{
return(lout); // it doesn't really matter (assuming realloc is a decent implementation and hasn't nuked the original pointer), we'll just have to temporarily waste a bit of memory
}
return(nlout);
}
12 changes: 12 additions & 0 deletions bits.h
@@ -0,0 +1,12 @@
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010 Edward Cree
See quirc.c for license information
bits: general helper functions
*/

#include <stdio.h>
#include <stdlib.h>

char * fgetl(FILE *); // gets a line of string data; returns a malloc-like pointer (preserves trailing \n)
105 changes: 89 additions & 16 deletions quirc.c
Expand Up @@ -28,6 +28,7 @@
#include "ttyraw.h" #include "ttyraw.h"
#include "ttyesc.h" #include "ttyesc.h"
#include "irc.h" #include "irc.h"
#include "bits.h"
#include "numeric.h" #include "numeric.h"
#include "version.h" #include "version.h"


Expand Down Expand Up @@ -57,6 +58,40 @@ int main(int argc, char *argv[])
char *server=NULL, *portno="6667", *uname="quirc", *fname=(char *)malloc(20+strlen(VERSION_TXT)), *nick="ac", *chan=NULL; char *server=NULL, *portno="6667", *uname="quirc", *fname=(char *)malloc(20+strlen(VERSION_TXT)), *nick="ac", *chan=NULL;
sprintf(fname, "quIRC %hhu.%hhu.%hhu%s", VERSION_MAJ, VERSION_MIN, VERSION_REV, VERSION_TXT); sprintf(fname, "quIRC %hhu.%hhu.%hhu%s", VERSION_MAJ, VERSION_MIN, VERSION_REV, VERSION_TXT);
char *qmsg=fname; char *qmsg=fname;
char *rcfile=".quirc";
char *rcshad=".quirc-shadow";
bool join=false;
FILE *rcfp=fopen(rcfile, "r");
if(rcfp)
{
while(!feof(rcfp))
{
char *line=fgetl(rcfp);
if(*line!='#') // #lines are comments
{
char *cmd=strtok(line, " \t");
if(*cmd=='c')
{
// it's a colour specifier; custom colours aren't done yet
}
else
{
if(strcmp(cmd, "server")==0)
server=strdup(strtok(NULL, "\n"));
else if(strcmp(cmd, "nick")==0)
nick=strdup(strtok(NULL, "\n"));
else if(strcmp(cmd, "chan")==0)
chan=strdup(strtok(NULL, "\n"));
else
{
fprintf(stderr, "Unrecognised cmd %s in .quirc (ignoring)\n", cmd);
}
}
}
free(line);
}
fclose(rcfp);
}
int maxnlen=16; int maxnlen=16;
int arg; int arg;
for(arg=1;arg<argc;arg++) for(arg=1;arg<argc;arg++)
Expand All @@ -79,6 +114,14 @@ int main(int argc, char *argv[])
{ {
sscanf(argv[arg]+9, "%u", &height); sscanf(argv[arg]+9, "%u", &height);
} }
else if(strcmp(argv[arg], "--no-auto-connect")==0)
{
server=NULL;
}
else if(strcmp(argv[arg], "--no-auto-join")==0)
{
chan=NULL;
}
} }
int e=ttyraw(STDOUT_FILENO); int e=ttyraw(STDOUT_FILENO);
if(e) if(e)
Expand Down Expand Up @@ -220,7 +263,9 @@ int main(int argc, char *argv[])
default: default:
printf(CLA "\n"); printf(CLA "\n");
printf(LOCATE, height-2, 1); printf(LOCATE, height-2, 1);
printf(CLA "<<%d?\n" CLA "\n", num); setcol(1, 6, false, true);
printf(CLA "<<%d?" CLR "\n" CLA "\n", num);
resetcol();
break; break;
} }
} }
Expand All @@ -231,6 +276,17 @@ int main(int argc, char *argv[])
sprintf(pong, "PONG %s %s", uname, sender+1); sprintf(pong, "PONG %s %s", uname, sender+1);
irc_tx(serverhandle, pong); irc_tx(serverhandle, pong);
} }
else if(strcmp(cmd, "MODE")==0)
{
if(chan && !join)
{
char joinmsg[8+strlen(chan)];
sprintf(joinmsg, "JOIN %s", chan);
irc_tx(serverhandle, joinmsg);
join=true;
}
// apart from using it as a trigger, we don't look at modes just yet
}
else if(strcmp(cmd, "PRIVMSG")==0) else if(strcmp(cmd, "PRIVMSG")==0)
{ {
char *dest=strtok(NULL, " \t"); char *dest=strtok(NULL, " \t");
Expand All @@ -245,9 +301,21 @@ int main(int argc, char *argv[])
src[maxnlen-1]=src[strlen(src)-1]; src[maxnlen-1]=src[strlen(src)-1];
src[maxnlen]=0; src[maxnlen]=0;
} }
printf(CLA "\n"); if((*msg==1) && !strncmp(msg, "\001ACTION ", 8))
printf(LOCATE, height-2, 1+max(maxnlen-strlen(src), 0)); {
printf(CLA "<%s> %s\n" CLA "\n", src, msg); msg[strlen(msg)-1]=0; // remove trailing \001
printf(CLA "\n");
printf(LOCATE, height-2, 3+max(maxnlen-strlen(src), 0));
setcol(0, 3, false, true);
printf(CLA "%s %s" CLR "\n" CLA "\n", src, msg+8);
resetcol();
}
else
{
printf(CLA "\n");
printf(LOCATE, height-2, 1+max(maxnlen-strlen(src), 0));
printf(CLA "<%s> %s" CLR "\n" CLA "\n", src, msg);
}
} }
else if(strcmp(cmd, "NOTICE")==0) else if(strcmp(cmd, "NOTICE")==0)
{ {
Expand All @@ -265,7 +333,7 @@ int main(int argc, char *argv[])
printf(CLA "\n"); printf(CLA "\n");
printf(LOCATE, height-2, 1+max(maxnlen-strlen(src), 0)); printf(LOCATE, height-2, 1+max(maxnlen-strlen(src), 0));
setcol(7, 0, true, false); setcol(7, 0, true, false);
printf(CLA "<%s> %s\n" CLA "\n", src, msg); printf(CLA "<%s> %s" CLR "\n" CLA "\n", src, msg);
resetcol(); resetcol();
} }
else if(strcmp(cmd, "JOIN")==0) else if(strcmp(cmd, "JOIN")==0)
Expand All @@ -280,8 +348,9 @@ int main(int argc, char *argv[])
setcol(2, 0, true, false); setcol(2, 0, true, false);
if(strcmp(src, nick)==0) if(strcmp(src, nick)==0)
{ {
printf(CLA "You (%s) have joined %s\n" CLA "\n", src, dest+1); printf(CLA "You (%s) have joined %s" CLR "\n" CLA "\n", src, dest+1);
chan=strdup(dest+1); chan=strdup(dest+1);
join=true;
} }
else else
{ {
Expand All @@ -291,7 +360,7 @@ int main(int argc, char *argv[])
src[maxnlen-1]=src[strlen(src)-1]; src[maxnlen-1]=src[strlen(src)-1];
src[maxnlen]=0; src[maxnlen]=0;
} }
printf(CLA "=%s= has joined %s\n" CLA "\n", src, dest+1); printf(CLA "=%s= has joined %s" CLR "\n" CLA "\n", src, dest+1);
} }
resetcol(); resetcol();
} }
Expand All @@ -307,7 +376,7 @@ int main(int argc, char *argv[])
setcol(2, 0, true, false); setcol(2, 0, true, false);
if(strcmp(src, nick)==0) if(strcmp(src, nick)==0)
{ {
printf(CLA "You (%s) have left %s\n" CLA "\n", src, dest); printf(CLA "You (%s) have left %s" CLR "\n" CLA "\n", src, dest);
} }
else else
{ {
Expand All @@ -317,7 +386,7 @@ int main(int argc, char *argv[])
src[maxnlen-1]=src[strlen(src)-1]; src[maxnlen-1]=src[strlen(src)-1];
src[maxnlen]=0; src[maxnlen]=0;
} }
printf(CLA "=%s= has left %s\n" CLA "\n", src, dest); printf(CLA "=%s= has left %s" CLR "\n" CLA "\n", src, dest);
} }
resetcol(); resetcol();
} }
Expand All @@ -333,7 +402,7 @@ int main(int argc, char *argv[])
setcol(3, 0, true, false); setcol(3, 0, true, false);
if(strcmp(src, nick)==0) if(strcmp(src, nick)==0)
{ {
printf(CLA "You (%s) have left %s (%s)\n" CLA "\n", src, server, dest+1); // this shouldn't happen??? printf(CLA "You (%s) have left %s (%s)" CLR "\n" CLA "\n", src, server, dest+1); // this shouldn't happen???
} }
else else
{ {
Expand All @@ -343,7 +412,7 @@ int main(int argc, char *argv[])
src[maxnlen-1]=src[strlen(src)-1]; src[maxnlen-1]=src[strlen(src)-1];
src[maxnlen]=0; src[maxnlen]=0;
} }
printf(CLA "=%s= has left %s (%s)\n" CLA "\n", src, server, dest+1); printf(CLA "=%s= has left %s (%s)" CLR "\n" CLA "\n", src, server, dest+1);
} }
resetcol(); resetcol();
} }
Expand All @@ -359,7 +428,7 @@ int main(int argc, char *argv[])
setcol(4, 0, true, false); setcol(4, 0, true, false);
if(strcmp(dest+1, nick)==0) if(strcmp(dest+1, nick)==0)
{ {
printf(CLA "You (%s) are now known as %s\n" CLA "\n", src, dest+1); printf(CLA "You (%s) are now known as %s" CLR "\n" CLA "\n", src, dest+1);
} }
else else
{ {
Expand All @@ -369,7 +438,7 @@ int main(int argc, char *argv[])
src[maxnlen-1]=src[strlen(src)-1]; src[maxnlen-1]=src[strlen(src)-1];
src[maxnlen]=0; src[maxnlen]=0;
} }
printf(CLA "=%s= is now known as %s\n" CLA "\n", src, dest+1); printf(CLA "=%s= is now known as %s" CLR "\n" CLA "\n", src, dest+1);
} }
resetcol(); resetcol();
} }
Expand All @@ -378,7 +447,7 @@ int main(int argc, char *argv[])
printf(CLA "\n"); printf(CLA "\n");
printf(LOCATE, height-2, 1); printf(LOCATE, height-2, 1);
setcol(3, 4, false, false); setcol(3, 4, false, false);
printf(CLA "<? %s\n" CLA "\n", pdata); printf(CLA "<? %s" CLR "\n" CLA "\n", pdata);
resetcol(); resetcol();
} }
} }
Expand Down Expand Up @@ -600,8 +669,12 @@ int main(int argc, char *argv[])
else if(args) else if(args)
{ {
char privmsg[32+strlen(chan)+strlen(args)]; char privmsg[32+strlen(chan)+strlen(args)];
sprintf(privmsg, "PRIVMSG %s :ACTION\001%s\001", chan, args); sprintf(privmsg, "PRIVMSG %s :\001ACTION %s\001", chan, args);
irc_tx(serverhandle, privmsg); irc_tx(serverhandle, privmsg);
printf(LOCATE, height-2, 3+max(maxnlen-strlen(nick), 0));
setcol(0, 3, false, true);
printf(CLA "%s %s" CLR "\n" CLA "\n", nick, args);
resetcol();
} }
else else
{ {
Expand Down Expand Up @@ -646,7 +719,7 @@ int main(int argc, char *argv[])
irc_tx(serverhandle, pmsg); irc_tx(serverhandle, pmsg);
printf(LOCATE, height-2, 1+max(maxnlen-strlen(nick), 0)); printf(LOCATE, height-2, 1+max(maxnlen-strlen(nick), 0));
setcol(7, 0, false, true); setcol(7, 0, false, true);
printf(CLA "<%s> %s\n" CLA "\n", nick, inp); printf(CLA "<%s> %s" CLR "\n" CLA "\n", nick, inp);
resetcol(); resetcol();
} }
else else
Expand Down
34 changes: 12 additions & 22 deletions ttyesc.c
Expand Up @@ -10,30 +10,20 @@


int setcol(int fore, int back, bool hi, bool ul) int setcol(int fore, int back, bool hi, bool ul)
{ {
if(fore>7) if((fore<0)||(fore>7))
return(1); return(1);
if(back>7) if((back<0)||(back>7))
return(2); return(2);
if(fore>=0) putchar('\033');
{ putchar('[');
putchar('\033'); putchar('0'+(hi?1:0)+(ul?4:0));
putchar('['); putchar(';');
putchar('0'+(hi?1:0)+(ul?4:0)); putchar('3');
putchar(';'); putchar('0'+fore);
putchar('3'); putchar(';');
putchar('0'+fore); putchar('4');
putchar('m'); putchar('0'+back);
} putchar('m');
if(back>=0)
{
putchar('\033');
putchar('[');
putchar('0'+(hi?1:0)+(ul?4:0));
putchar(';');
putchar('4');
putchar('0'+back);
putchar('m');
}
fflush(stdout); fflush(stdout);
return(0); return(0);
} }
Expand Down

0 comments on commit 709dc6b

Please sign in to comment.