Skip to content
Browse files

Allow multiple automatic connects and joins in .quirc

  • Loading branch information...
1 parent 6d8df99 commit e51598968e39a2bfc9023b0f236615ffd8adc34a Sound and Fury committed Sep 3, 2010
Showing with 153 additions and 50 deletions.
  1. +1 −0 buffer.c
  2. +1 −0 buffer.h
  3. +78 −10 config.c
  4. +25 −1 config.h
  5. +10 −6 input.c
  6. +27 −23 irc.c
  7. +3 −3 irc.h
  8. +8 −7 quirc.c
View
1 buffer.c
@@ -51,6 +51,7 @@ int init_buffer(int buf, btype type, char *bname, int nlines)
bufs[buf].namreply=false;
bufs[buf].live=false;
bufs[buf].casemapping=RFC1459;
+ bufs[buf].autoent=NULL;
return(0);
}
View
1 buffer.h
@@ -57,6 +57,7 @@ typedef struct _buf
bool namreply; // tab is in the middle of reading a list of NAMES replies (RPL_NAMREPLY)?
bool live; // tab is connected? when checking in a CHANNEL, remember to AND it with the parent's live (use LIVE(buf), defined further up this file)
cmap casemapping; // the SERVER's value is authoritative; the CHANNEL's value is ignored. STATUS's value is irrelevant.
+ servlist * autoent; // if this was opened by autoconnect(), this is filled in to point to the server's servlist entry
}
buffer;
View
88 config.c
@@ -16,6 +16,7 @@ int def_config(void)
full_width_colour=false;
hilite_tabstrip=false;
tsb=true; // show top status bar
+ autojoin=true;
char *cols=getenv("COLUMNS"), *rows=getenv("LINES");
if(cols) sscanf(cols, "%u", &width);
if(rows) sscanf(rows, "%u", &height);
@@ -32,12 +33,11 @@ int def_config(void)
printf("height set to minimum 5\n");
}
maxnlen=16;
- server=NULL;
+ servs=NULL;
portno="6667";
username="quirc";
fname=(char *)malloc(64+strlen(VERSION_TXT));
nick=strdup("ac");
- chan=NULL;
sprintf(fname, "quIRC %hhu.%hhu.%hhu%s%s : http://github.com/ec429/quIRC", VERSION_MAJ, VERSION_MIN, VERSION_REV, VERSION_TXT[0]?"-":"", VERSION_TXT);
sprintf(version, "%hhu.%hhu.%hhu%s%s", VERSION_MAJ, VERSION_MIN, VERSION_REV, VERSION_TXT[0]?"-":"", VERSION_TXT);
return(0);
@@ -128,17 +128,45 @@ int rcread(FILE *rcfp)
{
char *rest=strtok(NULL, "\n");
if(strcmp(cmd, "server")==0)
- server=strdup(rest);
+ {
+ servlist * new=(servlist *)malloc(sizeof(servlist));
+ new->next=servs;
+ new->name=strdup(rest);
+ new->nick=strdup(nick);
+ new->portno=strdup(portno);
+ new->join=false;
+ new->chans=NULL;
+ servs=new;
+ }
+ else if(servs && (strcmp(cmd, "*port")==0))
+ {
+ if(servs->portno) free(servs->portno);
+ servs->portno=strdup(rest);
+ }
else if(strcmp(cmd, "port")==0)
portno=strdup(rest);
else if(strcmp(cmd, "uname")==0)
username=strdup(rest);
else if(strcmp(cmd, "fname")==0)
fname=strdup(rest);
+ else if(servs && (strcmp(cmd, "*nick")==0))
+ {
+ if(servs->nick) free(servs->nick);
+ servs->nick=strdup(rest);
+ }
else if(strcmp(cmd, "nick")==0)
nick=strdup(rest);
- else if(strcmp(cmd, "chan")==0)
- chan=strdup(rest);
+ else if(servs && (strcmp(cmd, "*chan")==0))
+ {
+ chanlist * new=(chanlist *)malloc(sizeof(chanlist));
+ new->next=servs->chans;
+ new->name=strdup(rest);
+ if((new->key=strpbrk(new->name, " \t")))
+ {
+ *new->key++=0;
+ }
+ servs->chans=new;
+ }
else if(strcmp(cmd, "mnln")==0)
sscanf(rest, "%u", &maxnlen);
else if(strcmp(cmd, "mcc")==0)
@@ -251,18 +279,27 @@ signed int pargs(int argc, char *argv[])
}
else if((strcmp(argv[arg], "--no-server")==0)||(strcmp(argv[arg], "--no-auto-connect")==0)) // the "-auto" forms are from older versions; depr
{
- server=NULL;
+ freeservlist(servs);
+ servs=NULL;
}
- else if((strcmp(argv[arg], "--no-chan")==0)||(strcmp(argv[arg], "--no-auto-join")==0))
+ else if(servs && ((strcmp(argv[arg], "--no-chan")==0)||(strcmp(argv[arg], "--no-auto-join")==0)))
{
- chan=NULL;
+ autojoin=false;
}
else if((strcmp(argv[arg], "--check")==0)||(strcmp(argv[arg], "--lint")==0))
{
check=true;
}
else if(strncmp(argv[arg], "--server=", 9)==0)
- server=argv[arg]+9;
+ {
+ servs=(servlist *)malloc(sizeof(servlist));
+ servs->next=NULL;
+ servs->name=strdup(argv[arg]+9);
+ servs->nick=strdup(nick);
+ servs->portno=strdup(portno);
+ servs->join=false;
+ servs->chans=NULL;
+ }
else if(strncmp(argv[arg], "--port=", 7)==0)
portno=argv[arg]+7;
else if(strncmp(argv[arg], "--uname=", 8)==0)
@@ -272,7 +309,16 @@ signed int pargs(int argc, char *argv[])
else if(strncmp(argv[arg], "--nick=", 7)==0)
nick=strdup(argv[arg]+7);
else if(strncmp(argv[arg], "--chan=", 7)==0)
- chan=argv[arg]+7;
+ {
+ chanlist * new=(chanlist *)malloc(sizeof(chanlist));
+ new->next=servs->chans;
+ new->name=strdup(argv[arg]+7);
+ if((new->key=strpbrk(new->name, " \t")))
+ {
+ *new->key++=0;
+ }
+ servs->chans=new;
+ }
else
{
fprintf(stderr, "Unrecognised argument '%s'\n", argv[arg]);
@@ -281,3 +327,25 @@ signed int pargs(int argc, char *argv[])
if(check) return(0);
return(-1);
}
+
+void freeservlist(servlist * serv)
+{
+ if(serv)
+ {
+ if(serv->name) free(serv->name);
+ if(serv->nick) free(serv->nick);
+ if(serv->portno) free(serv->portno);
+ freechanlist(serv->chans);
+ freeservlist(serv->next);
+ }
+}
+
+void freechanlist(chanlist * chan)
+{
+ if(chan)
+ {
+ if(chan->name) free(chan->name);
+ if(chan->key) free(chan->key);
+ freechanlist(chan->next);
+ }
+}
View
26 config.h
@@ -11,6 +11,26 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
+
+typedef struct _chanlist
+{
+ char *name;
+ char *key;
+ struct _chanlist *next;
+}
+chanlist;
+
+typedef struct _servlist
+{
+ char *name;
+ char *portno;
+ char *nick;
+ bool join;
+ chanlist *chans;
+ struct _servlist *next;
+}
+servlist;
+
#include "bits.h"
#include "colour.h"
#include "text.h"
@@ -25,9 +45,13 @@ int maxnlen;
bool full_width_colour;
bool hilite_tabstrip;
bool tsb; // top status bar
-char *server, *portno, *username, *fname, *nick, *chan;
+bool autojoin;
+char *username, *fname, *nick, *portno;
+servlist *servs;
char version[16+strlen(VERSION_TXT)];
int def_config(void); // set these to their defaults
int rcread(FILE *rcfp); // read & parse the rc file.
signed int pargs(int argc, char *argv[]); // parse the cmdline args. If return is >=0, main should return it also
+void freeservlist(servlist * serv);
+void freechanlist(chanlist * chan);
View
16 input.c
@@ -619,21 +619,25 @@ int cmd_handle(char *inp, char **qmsg, fd_set *master, int *fdmax) // old state=
{
newport=args;
}
+ else if(bufs[bufs[cbuf].server].autoent && bufs[bufs[cbuf].server].autoent->portno)
+ {
+ newport=bufs[bufs[cbuf].server].autoent->portno;
+ }
else
{
newport=portno;
}
- char cstr[24+strlen(server)];
- sprintf(cstr, "quIRC - connecting to %s", server);
+ char cstr[24+strlen(bufs[bufs[cbuf].server].bname)];
+ sprintf(cstr, "quIRC - connecting to %s", bufs[bufs[cbuf].server].bname);
settitle(cstr);
- char dstr[30+strlen(server)+strlen(newport)];
- sprintf(dstr, "Connecting to %s on port %s...", server, newport);
+ char dstr[30+strlen(bufs[bufs[cbuf].server].bname)+strlen(newport)];
+ sprintf(dstr, "Connecting to %s on port %s...", bufs[bufs[cbuf].server].bname, newport);
setcolour(c_status);
printf(LOCATE, height-2, 1);
printf("%s" CLR "\n", dstr);
resetcol();
printf(CLA "\n");
- int serverhandle=irc_connect(server, newport, master, fdmax);
+ int serverhandle=irc_connect(bufs[bufs[cbuf].server].bname, newport, master, fdmax);
if(serverhandle)
{
int b=bufs[cbuf].server;
@@ -644,7 +648,7 @@ int cmd_handle(char *inp, char **qmsg, fd_set *master, int *fdmax) // old state=
bufs[b2].handle=serverhandle;
}
w_buf_print(cbuf, c_status, dstr, "/server: ");
- sprintf(cstr, "quIRC - connected to %s", server);
+ sprintf(cstr, "quIRC - connected to %s", bufs[bufs[cbuf].server].bname);
settitle(cstr);
}
}
View
50 irc.c
@@ -77,29 +77,30 @@ int irc_conn_rest(int b, char *nick, char *username, char *fullname)
return(0);
}
-int autoconnect(fd_set *master, int *fdmax)
+int autoconnect(fd_set *master, int *fdmax, servlist *serv)
{
- char cstr[36+strlen(server)+strlen(portno)];
- sprintf(cstr, "quIRC - connecting to %s", server);
+ char cstr[36+strlen(serv->name)+strlen(serv->portno)];
+ sprintf(cstr, "quIRC - connecting to %s", serv->name);
settitle(cstr);
- sprintf(cstr, "Connecting to %s on port %s...", server, portno);
+ sprintf(cstr, "Connecting to %s on port %s...", serv->name, serv->portno);
setcolour(c_status);
printf(CLA "\n");
printf(LOCATE, height-2, 1);
printf("%s" CLR "\n", cstr);
resetcol();
printf(CLA "\n");
- int serverhandle=irc_connect(server, portno, master, fdmax);
+ int serverhandle=irc_connect(serv->name, serv->portno, master, fdmax);
if(serverhandle)
{
bufs=(buffer *)realloc(bufs, ++nbufs*sizeof(buffer));
- init_buffer(1, SERVER, server, buflines);
- cbuf=1;
+ cbuf=nbufs-1;
+ init_buffer(cbuf, SERVER, serv->name, buflines);
bufs[cbuf].handle=serverhandle;
- bufs[cbuf].nick=strdup(nick);
+ bufs[cbuf].nick=strdup(serv->nick);
+ bufs[cbuf].autoent=serv;
bufs[cbuf].server=1;
- add_to_buffer(1, c_status, cstr);
- sprintf(cstr, "quIRC - connecting to %s", server);
+ add_to_buffer(cbuf, c_status, cstr);
+ sprintf(cstr, "quIRC - connecting to %s", serv->name);
settitle(cstr);
}
return(serverhandle);
@@ -534,18 +535,23 @@ int rx_ping(int fd)
return(irc_tx(fd, pong));
}
-int rx_mode(bool *join, int b)
+int rx_mode(servlist * serv, int b)
{
int fd=bufs[b].handle;
- if(chan && !*join)
+ if(autojoin && serv->chans && !serv->join)
{
- char joinmsg[8+strlen(chan)];
- sprintf(joinmsg, "JOIN %s", chan);
- irc_tx(fd, joinmsg);
- char jmsg[16+strlen(chan)];
- sprintf(jmsg, "auto-Joining %s", chan);
- w_buf_print(b, c_join[0], jmsg, "");
- *join=true;
+ chanlist * curr=serv->chans;
+ while(curr)
+ {
+ char joinmsg[8+strlen(curr->name)];
+ sprintf(joinmsg, "JOIN %s", curr->name);
+ irc_tx(fd, joinmsg);
+ char jmsg[16+strlen(curr->name)];
+ sprintf(jmsg, "auto-Joining %s", curr->name);
+ w_buf_print(b, c_join[0], jmsg, "");
+ curr=curr->next;
+ }
+ serv->join=true;
}
return(0);
}
@@ -799,7 +805,7 @@ int rx_topic(int b, char *packet)
return(match?0:1);
}
-int rx_join(int b, char *packet, char *pdata, bool *join)
+int rx_join(int b, char *packet, char *pdata)
{
char *dest=strtok(NULL, " \t");
char *src=packet+1;
@@ -810,8 +816,6 @@ int rx_join(int b, char *packet, char *pdata, bool *join)
{
char dstr[20+strlen(src)+strlen(dest+1)];
sprintf(dstr, "You (%s) have joined %s", src, dest+1);
- chan=strdup(dest+1);
- *join=true;
char cstr[16+strlen(src)+strlen(dest+1)];
sprintf(cstr, "quIRC - %s on %s", src, dest+1);
settitle(cstr);
@@ -827,7 +831,7 @@ int rx_join(int b, char *packet, char *pdata, bool *join)
if(b2>=nbufs)
{
bufs=(buffer *)realloc(bufs, ++nbufs*sizeof(buffer));
- init_buffer(nbufs-1, CHANNEL, chan, buflines);
+ init_buffer(nbufs-1, CHANNEL, dest+1, buflines);
bufs[nbufs-1].server=bufs[b].server;
cbuf=nbufs-1;
}
View
6 irc.h
@@ -41,7 +41,7 @@ cmap;
int irc_connect(char *server, char *portno, fd_set *master, int *fdmax); // non-blocking
int irc_conn_rest(int b, char *nick, char *username, char *fullname); // call this when the non-blocking connect() has finished
-int autoconnect(fd_set *master, int *fdmax);
+int autoconnect(fd_set *master, int *fdmax, servlist *serv);
int irc_tx(int fd, char * packet);
int irc_rx(int fd, char ** data);
void low_quote(char *from, char to[512]);
@@ -55,14 +55,14 @@ int irc_strncasecmp(char *c1, char *c2, int n, cmap casemapping);
// Received-IRC message handlers. strtok() state leaks across the boundaries of these functions, beware!
int irc_numeric(char *cmd, int b);
int rx_ping(int fd);
-int rx_mode(bool *join, int b); // the first MODE triggers auto-join. Apart from using it as a trigger, we don't look at modes just yet
+int rx_mode(servlist * serv, int b); // the first MODE triggers auto-join. Apart from using it as a trigger, we don't look at modes just yet
int rx_kill(int b, fd_set *master);
int rx_kick(int b);
int rx_error(int b, fd_set *master);
int rx_privmsg(int b, char *packet, char *pdata);
int rx_notice(int b, char *packet);
int rx_topic(int b, char *packet);
-int rx_join(int b, char *packet, char *pdata, bool *join);
+int rx_join(int b, char *packet, char *pdata);
int rx_part(int b, char *packet, char *pdata);
int rx_quit(int b, char *packet, char *pdata);
int rx_nick(int b, char *packet, char *pdata);
View
15 quirc.c
@@ -36,7 +36,6 @@ int main(int argc, char *argv[])
char *rcfile=".quirc";
char *home=getenv("HOME");
if(home) chdir(home);
- bool join=false;
FILE *rcfp=fopen(rcfile, "r");
if(rcfp)
{
@@ -73,12 +72,14 @@ int main(int argc, char *argv[])
FD_ZERO(&master);
FD_SET(STDIN_FILENO, &master);
int fdmax=STDIN_FILENO;
- int serverhandle=0;
- if(server)
+ int servers=0;
+ servlist *curr=servs;
+ while(curr)
{
- serverhandle = autoconnect(&master, &fdmax);
+ servers |= autoconnect(&master, &fdmax, curr);
+ curr=curr->next;
}
- if(!serverhandle)
+ if(!servers)
{
w_buf_print(0, c_status, "Not connected - use /server to connect", "");
}
@@ -152,7 +153,7 @@ int main(int argc, char *argv[])
}
else if(strcmp(cmd, "MODE")==0)
{
- rx_mode(&join, b);
+ rx_mode(bufs[b].autoent, b);
}
else if(strcmp(cmd, "KILL")==0)
{
@@ -180,7 +181,7 @@ int main(int argc, char *argv[])
}
else if(strcmp(cmd, "JOIN")==0)
{
- rx_join(b, packet, pdata, &join);
+ rx_join(b, packet, pdata);
}
else if(strcmp(cmd, "PART")==0)
{

0 comments on commit e515989

Please sign in to comment.
Something went wrong with that request. Please try again.