X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Firc_src%2Fmain.c;h=b96fcc9e7442bd282eff399091a24dee1c488598;hb=6df6a4c5493bd641a028c435be54c4d087f12000;hp=9e5a0757c754d5e47abe45957c24594d206a1e1e;hpb=52fad670ab81459de0ff1bd0fa99a3396a6999e3;p=tpg%2Facess2.git diff --git a/Usermode/Applications/irc_src/main.c b/Usermode/Applications/irc_src/main.c index 9e5a0757..b96fcc9e 100644 --- a/Usermode/Applications/irc_src/main.c +++ b/Usermode/Applications/irc_src/main.c @@ -78,7 +78,7 @@ char *gsNickname = "acess"; tServer *gpServers; tWindow gWindow_Status = { NULL, NULL, NULL, // No next, empty list, no server - 0, "" // No activity, empty name (rendered as status) + 0, {""} // No activity, empty name (rendered as status) }; tWindow *gpWindows = &gWindow_Status; tWindow *gpCurrentWindow = &gWindow_Status; @@ -207,6 +207,79 @@ int ParseArguments(int argc, const char *argv[]) return 0; } + +void Cmd_join(char *ArgString) +{ + int pos=0; + char *channel_name = GetValue(ArgString, &pos); + + if( gpCurrentWindow->Server ) + { + writef(gpCurrentWindow->Server->FD, "JOIN :%s\n", channel_name); + } +} + +void Cmd_quit(char *ArgString) +{ + const char *quit_message = ArgString; + if( quit_message == NULL || quit_message[0] == '\0' ) + quit_message = "/quit - Acess2 IRC Client"; + + for( tServer *srv = gpServers; srv; srv = srv->Next ) + { + writef(srv->FD, "QUIT :%s\n", quit_message); + } + + exit(0); +} + +void Cmd_window(char *ArgString) +{ + int pos = 0; + char *window_id = GetValue(ArgString, &pos); + int window_num = atoi(window_id); + + if( window_num > 0 ) + { + tWindow *win; + window_num --; // Move to base 0 + // Get `window_num`th window + for( win = gpWindows; win && window_num--; win = win->Next ); + if( win ) { + gpCurrentWindow = win; + Redraw_Screen(); + } + // Otherwise, silently ignore + } + else + { + window_num = 1; + for( tWindow *win = gpWindows; win; win = win->Next, window_num ++ ) + { + if( win->Name[0] ) { + Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "", + "%i: %s/%s", window_num, win->Server->Name, win->Name); + } + else { + Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "", + "%i: (status)", window_num); + } + } + } +} + +const struct { + const char *Name; + void (*Fcn)(char *ArgString); +} caCommands[] = { + {"join", Cmd_join}, + {"quit", Cmd_quit}, + {"window", Cmd_window}, + {"win", Cmd_window}, + {"w", Cmd_window}, +}; +const int ciNumCommands = sizeof(caCommands)/sizeof(caCommands[0]); + /** * \brief Handle a line from the prompt */ @@ -217,65 +290,19 @@ int ParseUserCommand(char *String) char *command; int pos = 0; - command = GetValue(String, &pos); - - if( strcmp(command, "/join") == 0 ) - { - char *channel_name = GetValue(String, &pos); - - if( gpCurrentWindow->Server ) - { - writef(gpCurrentWindow->Server->FD, "JOIN :%s\n", channel_name); - } - } - else if( strcmp(command, "/quit") == 0 ) + command = GetValue(String, &pos)+1; + + // TODO: Prefix matches + int cmdIdx = -1; + for( int i = 0; i < ciNumCommands; i ++ ) { - char *quit_message = String + pos; - tServer *srv; - - if( quit_message == NULL || quit_message[0] == '\0' ) - quit_message = "/quit - Acess2 IRC Client"; - - for( srv = gpServers; srv; srv = srv->Next ) - { - writef(srv->FD, "QUIT :%s\n", quit_message); + if( strcmp(command, caCommands[i].Name) == 0 ) { + cmdIdx = i; + break; } - - exit(0); } - else if( strcmp(command, "/window") == 0 || strcmp(command, "/win") == 0 || strcmp(command, "/w") == 0 ) - { - char *window_id = GetValue(String, &pos); - int window_num = atoi(window_id); - - if( window_num > 0 ) - { - tWindow *win; - window_num --; // Move to base 0 - // Get `window_num`th window - for( win = gpWindows; win && window_num--; win = win->Next ); - if( win ) { - gpCurrentWindow = win; - Redraw_Screen(); - } - // Otherwise, silently ignore - } - else - { - tWindow *win; - window_num = 1; - for( win = gpWindows; win; win = win->Next, window_num ++ ) - { - if( win->Name[0] ) { - Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "", - "%i: %s/%s", window_num, win->Server->Name, win->Name); - } - else { - Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "", - "%i: (status)", window_num); - } - } - } + if( cmdIdx != -1 ) { + caCommands[cmdIdx].Fcn(String+pos); } else { @@ -340,7 +367,7 @@ tMessage *Message_AppendF(tServer *Server, int Type, const char *Source, const c va_list args; int len; va_start(args, Message); - len = vsnprintf(NULL, 1000, Message, args); + len = vsnprintf(NULL, 0, Message, args); { char buf[len+1]; vsnprintf(buf, len+1, Message, args); @@ -578,6 +605,7 @@ void ParseServerLine(tServer *Server, char *Line) Message_Append(Server, MSG_TYPE_SERVER, user, user, message); break; case 372: // MOTD Data + case 375: // MOTD Start case 376: // MOTD End default: @@ -601,6 +629,10 @@ void ParseServerLine(tServer *Server, char *Line) } //printf("[%s] NOTICE %s: %s\n", Server->Name, ident, message); + char *ident_bang = strchr(ident, '!'); + if( ident_bang ) { + *ident_bang = '\0'; + } Message_Append(Server, MSG_TYPE_NOTICE, ident, "", message); } else if( strcmp(cmd, "PRIVMSG") == 0 ) @@ -618,7 +650,10 @@ void ParseServerLine(tServer *Server, char *Line) // TODO: Catch when the privmsg is addressed to the user // Cmd_PRIVMSG(Server, dest, ident, message); - *strchr(ident, '!') = '\0'; // Hello SIGSEGV + char *ident_bang = strchr(ident, '!'); + if( ident_bang ) { + *ident_bang = '\0'; + } Message_Append(Server, MSG_TYPE_STANDARD, ident, dest, message); } else if( strcmp(cmd, "JOIN" ) == 0 ) @@ -708,7 +743,7 @@ int writef(int FD, const char *Format, ...) int len; va_start(args, Format); - len = vsnprintf(NULL, 1000, Format, args); + len = vsnprintf(NULL, 0, Format, args); va_end(args); {