commit 875ecc84ab7b4c74134ccff381001c33f586a686
parent 26f1a62b5f73ebeee6076dfa2ab2b7d162fc4936
Author: Claudio Alessi <smoppy@gmail.com>
Date: Sun, 23 Jul 2017 16:56:19 +0200
Implement /close command.
Fix a memory leak along the way.
Diffstat:
M | circo.c | | | 39 | ++++++++++++++++++++++++++++++++------- |
1 file changed, 32 insertions(+), 7 deletions(-)
diff --git a/circo.c b/circo.c
@@ -81,6 +81,7 @@ int bufpos(char *buf, int len, int *line, int *off);
void cleanup(void);
void cmd_msg(char *cmd, char *s);
void cmd_quit(char *cmd, char *s);
+void cmd_close(char *cmd, char *s);
void cmd_server(char *cmd, char *s);
void cmdln_chldel(const Arg *arg);
void cmdln_chrdel(const Arg *arg);
@@ -222,6 +223,30 @@ cmd_quit(char *cmd, char *s) {
}
void
+cmd_close(char *cmd, char *s) {
+ Buffer *bs, *b;
+
+ if(!*s)
+ s = sel->name;
+ bs = getbuf("status");
+ b = getbuf(s);
+ if(!b) {
+ printb(bs, "%s: unknown buffer.\n", s);
+ return;
+ }
+ if(b == bs) {
+ printb(bs, "Cannot close the status.\n");
+ return;
+ }
+ if(b == sel)
+ sel = sel->next ? sel->next : buffers;
+ detach(b);
+ free(b);
+ if(*s == '#' || *s == '&')
+ sout("PART :%s", s);
+}
+
+void
cmd_server(char *cmd, char *s) {
char *h, *p;
@@ -606,17 +631,17 @@ parsesrv(void) {
/* XXX */
}
else if(!strcmp("PART", cmd)) {
- if(strcmp(usr, nick)) {
- printb(getbuf(par), "PART %s (%s)\n", usr, txt);
- return;
- }
b = getbuf(par);
- if(b != sel) {
- detach(b);
+ if(!b)
+ return;
+ if(strcmp(usr, nick)) {
+ printb(b, "PART %s (%s)\n", usr, txt);
return;
}
- sel = sel->next ? sel->next : buffers;
+ if(b == sel)
+ sel = sel->next ? sel->next : buffers;
detach(b);
+ free(b);
}
else if(!strcmp("PING", cmd)) {
sout("PONG %s", txt);