Permalink
Browse files

Improved, more general tag-creation

  • Loading branch information...
1 parent 0bc05de commit 4c0aaded536a2958e1b6ec9d694f940f578b15df Sound and Fury committed Sep 25, 2010
Showing with 83 additions and 55 deletions.
  1. +12 −3 bits.c
  2. +1 −3 bits.h
  3. +9 −8 input.c
  4. +59 −39 irc.c
  5. +2 −2 quirc.c
View
15 bits.c
@@ -237,8 +237,17 @@ void scrush(char **buf, int len)
}
}
-void mktag(char *buf, char *from, bool priv)
+char *mktag(char *fmt, char *from)
{
- memset(buf, ' ', maxnlen+8);
- sprintf(buf+maxnlen-strlen(from), priv?"(from %s) ":"<%s> ", from);
+ char *rv=NULL;
+ if(strlen(from)<=maxnlen)
+ {
+ rv=(char *)malloc(strlen(fmt)+maxnlen);
+ if(rv)
+ {
+ memset(rv, ' ', maxnlen+strlen(fmt)-1);
+ sprintf(rv+maxnlen-strlen(from), fmt, from);
+ }
+ }
+ return(rv);
}
View
4 bits.h
@@ -15,8 +15,6 @@
#include "colour.h"
#include "config.h"
-#define TAGLEN (maxnlen+9) // size of buffer needed for mktag()
-
// helper fn macros
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
@@ -28,4 +26,4 @@ void append_char(char **buf, int *l, int *i, char c); // adds a character to a s
void append_str(char **buf, int *l, int *i, char *str); // adds a string to a string buffer in heap (and realloc()s if needed)
void crush(char **buf, int len);
void scrush(char **buf, int len);
-void mktag(char *buf, char *from, bool priv);
+char *mktag(char *fmt, char *from);
View
17 input.c
@@ -1104,10 +1104,12 @@ int cmd_handle(char *inp, char **qmsg, fd_set *master, int *fdmax) // old state=
irc_tx(bufs[cbuf].handle, privmsg);
while(text[strlen(text)-1]=='\n')
text[strlen(text)-1]=0; // stomp out trailing newlines, they break things
- char tag[TAGLEN];
- memset(tag, ' ', maxnlen+8);
- sprintf(tag+maxnlen+2-strlen(dest), "(to %s) ", dest);
+ char *cdest=strdup(dest);
+ crush(&cdest, maxnlen);
+ char *tag=mktag(" (to %s)", cdest);
+ free(cdest);
w_buf_print(cbuf, c_msg[0], text, tag);
+ free(tag);
}
else
{
@@ -1154,12 +1156,12 @@ int cmd_handle(char *inp, char **qmsg, fd_set *master, int *fdmax) // old state=
args[strlen(args)-1]=0; // stomp out trailing newlines, they break things
char *cnick=strdup(bufs[bufs[b2].server].nick);
crush(&cnick, maxnlen);
- char tag[TAGLEN];
- mktag(tag, cnick, false);
+ char *tag=mktag(cnick, "<%s> ");
free(cnick);
bool al=bufs[b2].alert; // save alert status...
int hi=bufs[b2].hi_alert;
w_buf_print(b2, c_msg[0], args, tag);
+ free(tag);
bufs[b2].alert=al; // and restore it
bufs[b2].hi_alert=hi;
}
@@ -1200,11 +1202,10 @@ int cmd_handle(char *inp, char **qmsg, fd_set *master, int *fdmax) // old state=
args[strlen(args)-1]=0; // stomp out trailing newlines, they break things
char *cnick=strdup(bufs[bufs[cbuf].server].nick);
crush(&cnick, maxnlen);
- char tag[TAGLEN];
- memset(tag, ' ', maxnlen+3);
- sprintf(tag+maxnlen+2-strlen(cnick), "%s ", cnick);
+ char *tag=mktag(" %s ", cnick);
free(cnick);
w_buf_print(cbuf, c_actn[0], args, tag);
+ free(tag);
}
else
{
View
98 irc.c
@@ -895,9 +895,9 @@ int rx_privmsg(message pkt, int b, bool notice)
}
else
{
- char tag[TAGLEN];
- mktag(tag, from, false);
+ char *tag=mktag(from, "<%s> ");
w_buf_print(b2, notice?c_notice[1]:c_msg[1], pkt.args[1], tag);
+ free(tag);
if(ha)
bufs[b2].hi_alert=5;
}
@@ -915,9 +915,9 @@ int rx_privmsg(message pkt, int b, bool notice)
}
else
{
- char tag[TAGLEN];
- mktag(tag, from, true);
+ char *tag=mktag(from, "(from %s) ");
w_buf_print(b, notice?c_notice[1]:c_msg[1], pkt.args[1], tag);
+ free(tag);
if(!notice)
bufs[b].hi_alert=5;
}
@@ -946,24 +946,24 @@ int rx_topic(message pkt, int b)
bool match=false;
if(pkt.nargs<2)
{
- char tag[maxnlen+20];
- sprintf(tag, "%s removed the Topic", from);
+ char *tag=mktag("%s ", from);
int b2;
for(b2=0;b2<nbufs;b2++)
{
if((bufs[b2].server==b) && (bufs[b2].type==CHANNEL) && (irc_strcasecmp(pkt.args[0], bufs[b2].bname, bufs[b].casemapping)==0))
{
- w_buf_print(b2, c_notice[1], "", tag);
+ w_buf_print(b2, c_notice[1], "removed the Topic", tag);
match=true;
if(bufs[b2].topic) free(bufs[b2].topic);
bufs[b2].topic=NULL;
}
}
+ free(tag);
}
else
{
- char tag[maxnlen+20];
- sprintf(tag, "%s set the Topic to ", from);
+
+ char *tag=mktag("%s set the Topic to ", from);
int b2;
for(b2=0;b2<nbufs;b2++)
{
@@ -975,6 +975,7 @@ int rx_topic(message pkt, int b)
bufs[b2].topic=strdup(pkt.args[1]);
}
}
+ free(tag);
}
free(from);
return(match?0:1);
@@ -1019,19 +1020,24 @@ int rx_join(message pkt, int b)
}
else
{
- int b2;
+ char *from=strdup(src);
+ crush(&from, maxnlen);
+ char *tag=mktag("=%s= ", from);
+ free(from);
bool match=false;
+ int b2;
for(b2=0;b2<nbufs;b2++)
{
if((bufs[b2].server==b) && (bufs[b2].type==CHANNEL) && (irc_strcasecmp(pkt.args[0], bufs[b2].bname, bufs[b].casemapping)==0))
{
match=true;
- char dstr[16+strlen(src)+strlen(pkt.args[0])];
- sprintf(dstr, "=%s= has joined %s", src, pkt.args[0]);
- w_buf_print(b2, c_join[1], dstr, "");
+ char dstr[16+strlen(pkt.args[0])];
+ sprintf(dstr, "has joined %s", pkt.args[0]);
+ w_buf_print(b2, c_join[1], dstr, tag);
n_add(&bufs[b2].nlist, src);
}
}
+ free(tag);
if(!match)
{
e_buf_print(b, c_err, pkt, "Bad destination: ");
@@ -1071,28 +1077,33 @@ int rx_part(message pkt, int b)
}
else
{
- int b2;
+ char *from=strdup(src);
+ crush(&from, maxnlen);
+ char *tag=mktag("=%s= ", from);
+ free(from);
bool match=false;
+ int b2;
for(b2=0;b2<nbufs;b2++)
{
if((bufs[b2].server==b) && (bufs[b2].type==CHANNEL) && (irc_strcasecmp(pkt.args[0], bufs[b2].bname, bufs[b].casemapping)==0))
{
match=true;
if(pkt.nargs<2)
{
- char dstr[16+strlen(src)+strlen(pkt.args[0])];
- sprintf(dstr, "=%s= has left %s", src, pkt.args[0]);
- w_buf_print(b2, c_part[1], dstr, "");
+ char dstr[16+strlen(pkt.args[0])];
+ sprintf(dstr, "has left %s", pkt.args[0]);
+ w_buf_print(b2, c_part[1], dstr, tag);
}
else
{
- char dstr[24+strlen(src)+strlen(pkt.args[0])+strlen(pkt.args[1])];
- sprintf(dstr, "=%s= has left %s (Part: %s)", src, pkt.args[0], pkt.args[1]);
- w_buf_print(b2, c_part[1], dstr, "");
+ char dstr[24+strlen(pkt.args[0])+strlen(pkt.args[1])];
+ sprintf(dstr, "has left %s (Part: %s)", pkt.args[0], pkt.args[1]);
+ w_buf_print(b2, c_part[1], dstr, tag);
}
n_cull(&bufs[b2].nlist, src);
}
}
+ free(tag);
if(!match)
{
e_buf_print(b, c_err, pkt, "Bad destination: ");
@@ -1113,19 +1124,24 @@ int rx_quit(message pkt, int b)
}
else
{
+ char *from=strdup(src);
+ crush(&from, maxnlen);
+ char *tag=mktag("=%s= ", from);
+ free(from);
int b2;
for(b2=0;b2<nbufs;b2++)
{
if((bufs[b2].server==b) && (bufs[b2].type==CHANNEL))
{
if(n_cull(&bufs[b2].nlist, src))
{
- char dstr[24+strlen(src)+strlen(bufs[b].bname)+strlen(reason)];
- sprintf(dstr, "=%s= has left %s (%s)", src, bufs[b].bname, reason);
- w_buf_print(b2, c_quit[1], dstr, "");
+ char dstr[24+strlen(bufs[b].bname)+strlen(reason)];
+ sprintf(dstr, "has left %s (%s)", bufs[b].bname, reason);
+ w_buf_print(b2, c_quit[1], dstr, tag);
}
}
}
+ free(tag);
}
return(0);
}
@@ -1157,6 +1173,10 @@ int rx_nick(message pkt, int b)
}
else
{
+ char *from=strdup(src);
+ crush(&from, maxnlen);
+ char *tag=mktag("=%s= ", from);
+ free(from);
int b2;
bool match=false;
for(b2=0;b2<nbufs;b2++)
@@ -1167,12 +1187,13 @@ int rx_nick(message pkt, int b)
if(n_cull(&bufs[b2].nlist, src))
{
n_add(&bufs[b2].nlist, pkt.args[0]);
- char dstr[30+strlen(src)+strlen(pkt.args[0])];
- sprintf(dstr, "=%s= is now known as %s", src, pkt.args[0]);
- w_buf_print(b2, c_nick[1], dstr, "");
+ char dstr[30+strlen(pkt.args[0])];
+ sprintf(dstr, "is now known as %s", pkt.args[0]);
+ w_buf_print(b2, c_nick[1], dstr, tag);
}
}
}
+ free(tag);
if(!match)
{
e_buf_print(b, c_err, pkt, "Bad destination: ");
@@ -1187,10 +1208,9 @@ int ctcp(char *msg, char *from, char *src, int b2, bool ha, bool notice, bool pr
if(strncmp(msg, "\001ACTION ", 8)==0)
{
msg[strlen(msg)-1]=0; // remove trailing \001
- char tag[maxnlen+4];
- memset(tag, ' ', maxnlen+3);
- sprintf(tag+maxnlen+2-strlen(from), "%s ", from);
+ char *tag=mktag(" %s ", from);
w_buf_print(b2, c_actn[1], msg+8, tag);
+ free(tag);
ha=ha||strstr(msg+8, bufs[bufs[b2].server].nick);
if(ha)
bufs[b2].hi_alert=5;
@@ -1199,9 +1219,9 @@ int ctcp(char *msg, char *from, char *src, int b2, bool ha, bool notice, bool pr
{
if(notice)
{
- char tag[TAGLEN];
- mktag(tag, from, priv);
+ char *tag=mktag(from, "(from %s) ");
w_buf_print(b2, c_notice[1], msg, tag);
+ free(tag);
if(ha)
bufs[b2].hi_alert=5;
}
@@ -1216,9 +1236,9 @@ int ctcp(char *msg, char *from, char *src, int b2, bool ha, bool notice, bool pr
{
if(notice)
{
- char tag[TAGLEN];
- mktag(tag, from, priv);
+ char *tag=mktag(from, "(from %s) ");
w_buf_print(b2, c_notice[1], msg, tag);
+ free(tag);
if(ha)
bufs[b2].hi_alert=5;
}
@@ -1233,9 +1253,9 @@ int ctcp(char *msg, char *from, char *src, int b2, bool ha, bool notice, bool pr
{
if(notice)
{
- char tag[TAGLEN];
- mktag(tag, from, priv);
+ char *tag=mktag(from, "(from %s) ");
w_buf_print(b2, c_notice[1], msg, tag);
+ free(tag);
if(ha)
bufs[b2].hi_alert=5;
}
@@ -1250,9 +1270,9 @@ int ctcp(char *msg, char *from, char *src, int b2, bool ha, bool notice, bool pr
{
if(notice)
{
- char tag[TAGLEN];
- mktag(tag, from, priv);
+ char *tag=mktag(from, "(from %s) ");
w_buf_print(b2, c_notice[1], msg, tag);
+ free(tag);
if(ha)
bufs[b2].hi_alert=5;
}
@@ -1265,9 +1285,9 @@ int ctcp(char *msg, char *from, char *src, int b2, bool ha, bool notice, bool pr
}
else
{
- char tag[TAGLEN];
- mktag(tag, from, priv);
+ char *tag=mktag(from, "(from %s) ");
w_buf_print(b2, c_notice[1], msg, tag);
+ free(tag);
if(ha)
bufs[b2].hi_alert=5;
char *cmd=msg+1;
View
@@ -376,10 +376,10 @@ int main(int argc, char *argv[])
irc_tx(bufs[cbuf].handle, pmsg);
char *cnick=strdup(bufs[bufs[cbuf].server].nick);
crush(&cnick, maxnlen);
- char tag[TAGLEN];
- mktag(tag, cnick, false);
+ char *tag=mktag(cnick, "<%s> ");
free(cnick);
w_buf_print(cbuf, c_msg[0], iinput, tag);
+ free(tag);
}
else
{

0 comments on commit 4c0aade

Please sign in to comment.