Permalink
Browse files

Refactor PING, MODE and KILL handling

  • Loading branch information...
Sound and Fury
Sound and Fury committed Aug 16, 2010
1 parent 4050262 commit e928ecfece06a08bed1ce5ef5e3060660ade48f0
Showing with 71 additions and 34 deletions.
  1. +61 −0 irc.c
  2. +6 −1 irc.h
  3. +3 −32 quirc.c
  4. +1 −1 version.h
View
61 irc.c
@@ -267,3 +267,64 @@ int irc_numeric(char *cmd, int b) // TODO check the strtok()s for NULLs
}
return(num);
}
+
+int rx_ping(int fd)
+{
+ char *sender=strtok(NULL, " ");
+ char pong[8+strlen(username)+strlen(sender)];
+ sprintf(pong, "PONG %s %s", username, sender+1);
+ return(irc_tx(fd, pong));
+}
+
+int rx_mode(int fd, bool *join, int b)
+{
+ if(chan && !*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);
+ buf_print(b, c_join[0], jmsg, true);
+ *join=true;
+ }
+ return(0);
+}
+
+int rx_kill(int fd, int b, fd_set *master)
+{
+ char *dest=strtok(NULL, " \t"); // user to be killed
+ if(strcmp(dest, bufs[b].nick)==0) // if it's us, we disconnect from the server
+ {
+ close(fd);
+ FD_CLR(fd, master);
+ int b2;
+ for(b2=1;b2<nbufs;b2++)
+ {
+ while((b2<nbufs) && ((bufs[b2].server==b) || (bufs[b2].server==0)))
+ {
+ free_buffer(b2);
+ if(b2==cbuf)
+ cbuf=0;
+ }
+ }
+ redraw_buffer();
+ }
+ else // if it's not us, generate quit messages into the relevant channel tabs
+ {
+ int b2;
+ for(b2=1;b2<nbufs;b2++)
+ {
+ while((b2<nbufs) && ((bufs[b2].server==b) || (bufs[b2].server==0)))
+ {
+ if(n_cull(&bufs[b2].nlist, dest))
+ {
+ char kmsg[24+strlen(dest)+strlen(bufs[b].bname)];
+ sprintf(kmsg, "=%s= has left %s (killed)", dest, bufs[b].bname);
+ buf_print(b2, c_quit[1], kmsg, true);
+ }
+ }
+ }
+ }
+ return(0);
+}
View
7 irc.h
@@ -29,4 +29,9 @@ int irc_connect(char *server, char *portno, char *nick, char *username, char *fu
int autoconnect(fd_set *master, int *fdmax);
int irc_tx(int fd, char * packet);
int irc_rx(int fd, char ** data);
-int irc_numeric(char *cmd, int b); // strtok() state leaks across the boundaries of this function, beware!
+
+// 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(int fd, 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_kill(int fd, int b, fd_set *master);
View
35 quirc.c
@@ -188,44 +188,15 @@ int main(int argc, char *argv[])
}
else if(strcmp(cmd, "PING")==0)
{
- char *sender=strtok(NULL, " ");
- char pong[8+strlen(username)+strlen(sender)];
- sprintf(pong, "PONG %s %s", username, sender+1);
- irc_tx(fd, pong);
+ rx_ping(fd);
}
else if(strcmp(cmd, "MODE")==0)
{
- if(chan && !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);
- buf_print(b, c_join[0], jmsg, true);
- join=true;
- }
- // apart from using it as a trigger, we don't look at modes just yet
+ rx_mode(fd, &join, b);
}
else if(strcmp(cmd, "KILL")==0)
{
- char *dest=strtok(NULL, " \t"); // user to be killed
- if(strcmp(dest, bufs[b].nick)==0) // if it's us
- {
- close(fd);
- FD_CLR(fd, &master);
- int b2;
- for(b2=1;b2<nbufs;b2++)
- {
- while((b2<nbufs) && ((bufs[b2].server==b) || (bufs[b2].server==0)))
- {
- free_buffer(b2);
- if(b2==cbuf)
- cbuf=0;
- }
- }
- redraw_buffer();
- }
+ rx_kill(fd, b, &master);
}
else if(strcmp(cmd, "ERROR")==0) // assume it's fatal
{
View
@@ -9,5 +9,5 @@
#define VERSION_MAJ 0 // Major version
#define VERSION_MIN 4 // Minor version
#define VERSION_REV 8 // Revision number
-#define VERSION_TXT "10 gfaeadbe" // Rest of git describe
+#define VERSION_TXT "10 g4050262" // Rest of git describe
#define CC_VERSION "gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4) " // last line of cc -v

0 comments on commit e928ecf

Please sign in to comment.