Permalink
Browse files

Various improvements eg rc file (.quirc)

  • Loading branch information...
1 parent 5a53f10 commit 709dc6b3817d5515519d7157fef1e62358e015ed Sound and Fury committed Jul 27, 2010
Showing with 155 additions and 40 deletions.
  1. +1 −0 .gitignore
  2. +5 −2 Makefile
  3. +36 −0 bits.c
  4. +12 −0 bits.h
  5. +89 −16 quirc.c
  6. +12 −22 ttyesc.c
View
@@ -1,3 +1,4 @@
*~
quirc
*.o
+.quirc-shadow
View
@@ -5,8 +5,8 @@ CFLAGS ?= -Wall
all: quirc
-quirc: quirc.c ttyraw.o ttyraw.h ttyesc.o ttyesc.h irc.o irc.h numeric.h version.h
- $(CC) $(CFLAGS) -o quirc quirc.c ttyraw.o ttyesc.o irc.o
+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 bits.o
ttyraw.o: ttyraw.c ttyraw.h
$(CC) $(CFLAGS) -o ttyraw.o -c ttyraw.c
@@ -17,3 +17,6 @@ ttyesc.o: ttyesc.c ttyesc.h
irc.o: irc.c irc.h
$(CC) $(CFLAGS) -o irc.o -c irc.c
+bits.o: bits.c bits.h
+ $(CC) $(CFLAGS) -o bits.o -c bits.c
+
View
@@ -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);
+}
View
@@ -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)
View
@@ -28,6 +28,7 @@
#include "ttyraw.h"
#include "ttyesc.h"
#include "irc.h"
+#include "bits.h"
#include "numeric.h"
#include "version.h"
@@ -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;
sprintf(fname, "quIRC %hhu.%hhu.%hhu%s", VERSION_MAJ, VERSION_MIN, VERSION_REV, VERSION_TXT);
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 arg;
for(arg=1;arg<argc;arg++)
@@ -79,6 +114,14 @@ int main(int argc, char *argv[])
{
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);
if(e)
@@ -220,7 +263,9 @@ int main(int argc, char *argv[])
default:
printf(CLA "\n");
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;
}
}
@@ -231,6 +276,17 @@ int main(int argc, char *argv[])
sprintf(pong, "PONG %s %s", uname, sender+1);
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)
{
char *dest=strtok(NULL, " \t");
@@ -245,9 +301,21 @@ int main(int argc, char *argv[])
src[maxnlen-1]=src[strlen(src)-1];
src[maxnlen]=0;
}
- printf(CLA "\n");
- printf(LOCATE, height-2, 1+max(maxnlen-strlen(src), 0));
- printf(CLA "<%s> %s\n" CLA "\n", src, msg);
+ if((*msg==1) && !strncmp(msg, "\001ACTION ", 8))
+ {
+ 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)
{
@@ -265,7 +333,7 @@ int main(int argc, char *argv[])
printf(CLA "\n");
printf(LOCATE, height-2, 1+max(maxnlen-strlen(src), 0));
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();
}
else if(strcmp(cmd, "JOIN")==0)
@@ -280,8 +348,9 @@ int main(int argc, char *argv[])
setcol(2, 0, true, false);
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);
+ join=true;
}
else
{
@@ -291,7 +360,7 @@ int main(int argc, char *argv[])
src[maxnlen-1]=src[strlen(src)-1];
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();
}
@@ -307,7 +376,7 @@ int main(int argc, char *argv[])
setcol(2, 0, true, false);
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
{
@@ -317,7 +386,7 @@ int main(int argc, char *argv[])
src[maxnlen-1]=src[strlen(src)-1];
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();
}
@@ -333,7 +402,7 @@ int main(int argc, char *argv[])
setcol(3, 0, true, false);
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
{
@@ -343,7 +412,7 @@ int main(int argc, char *argv[])
src[maxnlen-1]=src[strlen(src)-1];
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();
}
@@ -359,7 +428,7 @@ int main(int argc, char *argv[])
setcol(4, 0, true, false);
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
{
@@ -369,7 +438,7 @@ int main(int argc, char *argv[])
src[maxnlen-1]=src[strlen(src)-1];
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();
}
@@ -378,7 +447,7 @@ int main(int argc, char *argv[])
printf(CLA "\n");
printf(LOCATE, height-2, 1);
setcol(3, 4, false, false);
- printf(CLA "<? %s\n" CLA "\n", pdata);
+ printf(CLA "<? %s" CLR "\n" CLA "\n", pdata);
resetcol();
}
}
@@ -600,8 +669,12 @@ int main(int argc, char *argv[])
else if(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);
+ 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
{
@@ -646,7 +719,7 @@ int main(int argc, char *argv[])
irc_tx(serverhandle, pmsg);
printf(LOCATE, height-2, 1+max(maxnlen-strlen(nick), 0));
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();
}
else
View
@@ -10,30 +10,20 @@
int setcol(int fore, int back, bool hi, bool ul)
{
- if(fore>7)
+ if((fore<0)||(fore>7))
return(1);
- if(back>7)
+ if((back<0)||(back>7))
return(2);
- if(fore>=0)
- {
- putchar('\033');
- putchar('[');
- putchar('0'+(hi?1:0)+(ul?4:0));
- putchar(';');
- putchar('3');
- putchar('0'+fore);
- putchar('m');
- }
- if(back>=0)
- {
- putchar('\033');
- putchar('[');
- putchar('0'+(hi?1:0)+(ul?4:0));
- putchar(';');
- putchar('4');
- putchar('0'+back);
- putchar('m');
- }
+ putchar('\033');
+ putchar('[');
+ putchar('0'+(hi?1:0)+(ul?4:0));
+ putchar(';');
+ putchar('3');
+ putchar('0'+fore);
+ putchar(';');
+ putchar('4');
+ putchar('0'+back);
+ putchar('m');
fflush(stdout);
return(0);
}

0 comments on commit 709dc6b

Please sign in to comment.