X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Firc_src%2Fmain.c;h=b96fcc9e7442bd282eff399091a24dee1c488598;hb=6df6a4c5493bd641a028c435be54c4d087f12000;hp=952da4e8428da778c19172c5823ec8b80d1b0f4a;hpb=f36dc289101a45c839595bb88cf1bf942f2cf12b;p=tpg%2Facess2.git diff --git a/Usermode/Applications/irc_src/main.c b/Usermode/Applications/irc_src/main.c index 952da4e8..b96fcc9e 100644 --- a/Usermode/Applications/irc_src/main.c +++ b/Usermode/Applications/irc_src/main.c @@ -8,8 +8,6 @@ #include #include -#define BUFSIZ 1023 - // === TYPES === typedef struct sServer { struct sServer *Next; @@ -80,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; @@ -104,8 +102,8 @@ int main(int argc, const char *argv[], const char *envp[]) atexit(ExitHandler); - giTerminal_Width = ioctl(1, 5, NULL); // getset_width - giTerminal_Height = ioctl(1, 6, NULL); // getset_height + giTerminal_Width = _SysIOCtl(1, 5, NULL); // getset_width + giTerminal_Height = _SysIOCtl(1, 6, NULL); // getset_height printf("\x1B[?1047h"); printf("\x1B[%i;%ir", 0, giTerminal_Height-1); @@ -116,9 +114,11 @@ int main(int argc, const char *argv[], const char *envp[]) // HACK: Static server entry // UCC (University [of Western Australia] Computer Club) IRC Server gWindow_Status.Server = Server_Connect( "UCC", "130.95.13.18", 6667 ); + // Freenode (#osdev) // gWindow_Status.Server = Server_Connect( "Freenode", "89.16.176.16", 6667 ); // gWindow_Status.Server = Server_Connect( "Host", "10.0.2.2", 6667 ); -// gWindow_Status.Server = Server_Connect( "BitlBee", "192.168.1.34", 6667 ); + // Local server +// gWindow_Status.Server = Server_Connect( "BitlBee", "192.168.1.39", 6667 ); if( !gWindow_Status.Server ) return -1; @@ -194,7 +194,7 @@ int main(int argc, const char *argv[], const char *envp[]) { tServer *srv; for( srv = gpServers; srv; srv = srv->Next ) - close(srv->FD); + _SysClose(srv->FD); } return 0; } @@ -207,6 +207,82 @@ 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 + */ int ParseUserCommand(char *String) { if( String[0] == '/' ) @@ -214,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 { @@ -337,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); @@ -359,8 +389,7 @@ tMessage *Message_Append(tServer *Server, int Type, const char *Source, const ch // Determine if it's a channel or PM message else if( Dest[0] == '#' || Dest[0] == '&' ) // TODO: Better determining here { - tWindow *prev = NULL; - for(win = gpWindows; win; prev = win, win = win->Next) + for(win = gpWindows; win; win = win->Next) { if( win->Server == Server && strcmp(win->Name, Dest) == 0 ) { @@ -425,7 +454,6 @@ tMessage *Message_Append(tServer *Server, int Type, const char *Source, const ch { int pos = SetCursorPos(giTerminal_Height-2, 0); - #if 1 if( win == gpCurrentWindow ) { int prefixlen = strlen(Source) + 3; int avail = giTerminal_Width - prefixlen; @@ -434,17 +462,12 @@ tMessage *Message_Append(tServer *Server, int Type, const char *Source, const ch printf("[%s] %.*s\n", Source, avail, Message); while( msglen > avail ) { msglen -= avail; + Message += avail; printf("\x1B[T"); SetCursorPos(giTerminal_Height-2, prefixlen); printf("%.*s\n", avail, Message); } } - #else - if(win->Name[0]) - printf("%s/%s [%s] %s\n", win->Server->Name, win->Name, Source, Message); - else - printf("(status) [%s] %s\n", Source, Message); - #endif SetCursorPos(-1, pos); } @@ -528,11 +551,12 @@ void ParseServerLine(tServer *Server, char *Line) int pos = 0; char *ident, *cmd; - _SysDebug("Server %s: Line = %s", Server->Name, Line); + _SysDebug("[%s] %s", Server->Name, Line); // Message? if( *Line == ':' ) { + pos ++; ident = GetValue(Line, &pos); // Ident (user or server) cmd = GetValue(Line, &pos); @@ -556,20 +580,32 @@ void ParseServerLine(tServer *Server, char *Line) switch(num) { + case 332: // Topic + user = message; // Channel + message = Line + pos + 1; // Topic + Message_AppendF(Server, MSG_TYPE_SERVER, user, user, "Topic: %s", message); + break; + case 333: // Topic set by + user = message; // Channel + message = GetValue(Line, &pos); // User + GetValue(Line, &pos); // Timestamp + Message_AppendF(Server, MSG_TYPE_SERVER, user, user, "Set by %s", message); + break; case 353: // /NAMES list // = :list -// GetValue(Line, &pos); // '=' + // '=' was eaten in and set to message user = GetValue(Line, &pos); // Actually channel message = Line + pos + 1; // List - Message_AppendF(Server, MSG_TYPE_SERVER, user, "", "Names: %s", message); + Message_AppendF(Server, MSG_TYPE_SERVER, user, user, "Names: %s", message); break; case 366: // end of /NAMES list // :msg user = message; message = Line + pos + 1; - Message_Append(Server, MSG_TYPE_SERVER, user, "", message); + Message_Append(Server, MSG_TYPE_SERVER, user, user, message); break; case 372: // MOTD Data + case 375: // MOTD Start case 376: // MOTD End default: @@ -583,6 +619,7 @@ void ParseServerLine(tServer *Server, char *Line) char *class, *message; class = GetValue(Line, &pos); + _SysDebug("NOTICE class='%s'", class); if( Line[pos] == ':' ) { message = Line + pos + 1; @@ -592,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 ) @@ -605,13 +646,20 @@ void ParseServerLine(tServer *Server, char *Line) else { message = GetValue(Line, &pos); } + + // TODO: Catch when the privmsg is addressed to the user + // Cmd_PRIVMSG(Server, dest, ident, message); + 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 ) { char *channel; - channel = GetValue(Line, &pos) + 1; + channel = Line + pos + 1; Window_Create(Server, channel); } else @@ -619,10 +667,17 @@ void ParseServerLine(tServer *Server, char *Line) Message_AppendF(Server, MSG_TYPE_SERVER, "", "", "Unknown message %s (%s)\n", cmd, Line+pos); } } - else { + else { + cmd = GetValue(Line, &pos); - // Command to client - Message_AppendF(NULL, MSG_TYPE_UNK, "", "", "Client Command: %s", Line); + if( strcmp(cmd, "PING") == 0 ) { + writef(Server->FD, "PONG %s\n", gsHostname); + + } + else { + // Command to client + Message_AppendF(NULL, MSG_TYPE_UNK, "", "", "Client Command: %s", Line); + } } } @@ -639,11 +694,11 @@ int ProcessIncoming(tServer *Server) // ioctl#8 on a TCP client gets the number of bytes in the recieve buffer // - Used to avoid blocking #if NON_BLOCK_READ - while( (len = ioctl(Server->FD, 8, NULL)) > 0 ) + while( (len = _SysIOCtl(Server->FD, 8, NULL)) > 0 ) { #endif // Read data - len = read(Server->FD, BUFSIZ - Server->ReadPos, &Server->InBuf[Server->ReadPos]); + len = _SysRead(Server->FD, &Server->InBuf[Server->ReadPos], BUFSIZ - Server->ReadPos); if( len == -1 ) { return -1; } @@ -688,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); { @@ -697,7 +752,7 @@ int writef(int FD, const char *Format, ...) vsnprintf(buf, len+1, Format, args); va_end(args); - return write(FD, len, buf); + return _SysWrite(FD, buf, len); } } @@ -707,49 +762,32 @@ int writef(int FD, const char *Format, ...) int OpenTCP(const char *AddressString, short PortNumber) { int fd, addrType; - char *iface; char addrBuffer[8]; // Parse IP Address addrType = Net_ParseAddress(AddressString, addrBuffer); if( addrType == 0 ) { fprintf(stderr, "Unable to parse '%s' as an IP address\n", AddressString); + _SysDebug("Unable to parse '%s' as an IP address\n", AddressString); return -1; } // Finds the interface for the destination address - iface = Net_GetInterface(addrType, addrBuffer); - if( iface == NULL ) { - fprintf(stderr, "Unable to find a route to '%s'\n", AddressString); - return -1; - } - -// printf("iface = '%s'\n", iface); - - // Open client socket - // TODO: Move this out to libnet? - { - int len = snprintf(NULL, 100, "/Devices/ip/%s/tcpc", iface); - char path[len+1]; - snprintf(path, 100, "/Devices/ip/%s/tcpc", iface); - fd = open(path, OPENFLAG_READ|OPENFLAG_WRITE); - } - - free(iface); - + fd = Net_OpenSocket(addrType, addrBuffer, "tcpc"); if( fd == -1 ) { - fprintf(stderr, "Unable to open TCP Client for reading\n"); + fprintf(stderr, "Unable to open TCP Client to '%s'\n", AddressString); + _SysDebug("Unable to open TCP client to '%s'\n", AddressString); return -1; } // Set remote port and address // printf("Setting port and remote address\n"); - ioctl(fd, 5, &PortNumber); - ioctl(fd, 6, addrBuffer); + _SysIOCtl(fd, 5, &PortNumber); + _SysIOCtl(fd, 6, addrBuffer); // Connect // printf("Initiating connection\n"); - if( ioctl(fd, 7, NULL) == 0 ) { + if( _SysIOCtl(fd, 7, NULL) == 0 ) { // Shouldn't happen :( fprintf(stderr, "Unable to start connection\n"); return -1; @@ -794,7 +832,7 @@ int SetCursorPos(int Row, int Col) Row = Col / giTerminal_Width; Col = Col % giTerminal_Width; } - rv = ioctl(1, 9, NULL); // Ugh, constants + rv = _SysIOCtl(1, 9, NULL); // Ugh, constants printf("\x1B[%i;%iH", Col, Row); return rv; }