X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Firc_src%2Fmain.c;h=3f46d1587feac7b026e6e444c736a45c6a4d0722;hb=2da16f7b05b8c3f17ac136128a849db332b067ad;hp=9c7eccc6a6e1a8e133f2484d96bc55b4d2efb652;hpb=7941d6b368acb0abc17e6a77ffaf7b4c306b67ab;p=tpg%2Facess2.git diff --git a/Usermode/Applications/irc_src/main.c b/Usermode/Applications/irc_src/main.c index 9c7eccc6..3f46d158 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; @@ -59,8 +57,10 @@ enum eMessageTypes int ParseUserCommand(char *String); // --- tServer *Server_Connect(const char *Name, const char *AddressString, short PortNumber); +tMessage *Message_AppendF(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message, ...); tMessage *Message_Append(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message); tWindow *Window_Create(tServer *Server, const char *Name); +void Redraw_Screen(void); int ProcessIncoming(tServer *Server); // --- Helpers @@ -89,6 +89,7 @@ tWindow *gpCurrentWindow = &gWindow_Status; void ExitHandler(void) { printf("\x1B[?1047l"); + printf("Quit\n"); } int main(int argc, const char *argv[], const char *envp[]) @@ -107,17 +108,22 @@ int main(int argc, const char *argv[], const char *envp[]) printf("\x1B[?1047h"); printf("\x1B[%i;%ir", 0, giTerminal_Height-1); + SetCursorPos(giTerminal_Height-1, 0); + printf("[(status)] "); + // HACK: Static server entry // UCC (University [of Western Australia] Computer Club) IRC Server - gWindow_Status.Server = Server_Connect( "UCC", "130.95.13.18", 6667 ); +// gWindow_Status.Server = Server_Connect( "UCC", "130.95.13.18", 6667 ); + 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 ); if( !gWindow_Status.Server ) return -1; - readline_info = Readline_Init(1); - SetCursorPos(giTerminal_Height-1, 0); printf("[(status)] "); + readline_info = Readline_Init(1); for( ;; ) { @@ -154,6 +160,7 @@ int main(int argc, const char *argv[], const char *envp[]) free(cmd); // Prompt SetCursorPos(giTerminal_Height-1, 0); + printf("\x1B[K"); // Clear line if( gpCurrentWindow->Name[0] ) printf("[%s:%s] ", gpCurrentWindow->Server->Name, gpCurrentWindow->Name); else @@ -218,7 +225,7 @@ int ParseUserCommand(char *String) } else if( strcmp(command, "/quit") == 0 ) { - char *quit_message = GetValue(String, &pos); + char *quit_message = String + pos; tServer *srv; if( quit_message == NULL || quit_message[0] == '\0' ) @@ -244,6 +251,7 @@ int ParseUserCommand(char *String) for( win = gpWindows; win && window_num--; win = win->Next ); if( win ) { gpCurrentWindow = win; + Redraw_Screen(); } // Otherwise, silently ignore } @@ -253,18 +261,20 @@ int ParseUserCommand(char *String) window_num = 1; for( win = gpWindows; win; win = win->Next, window_num ++ ) { - char tmp[snprintf(NULL, 1000, "%i: %s/%s", window_num, win->Server->Name, win->Name)+1]; - snprintf(tmp, 1000, "%i: %s/%s", window_num, win->Server->Name, win->Name); - Message_Append(NULL, MSG_TYPE_SERVER, "client", "", tmp); + 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); + } } } } else { - int len = snprintf(NULL, 0, "Unknown command %s", command); - char buf[len+1]; - snprintf(buf, len+1, "Unknown command %s", command); - Message_Append(NULL, MSG_TYPE_SERVER, "client", "", buf); + Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "", "Unknown command %s", command); } } else @@ -273,6 +283,8 @@ int ParseUserCommand(char *String) // - Only send if server is valid and window name is non-empty if( gpCurrentWindow->Server && gpCurrentWindow->Name[0] ) { + Message_Append(gpCurrentWindow->Server, MSG_TYPE_STANDARD, + gsNickname, gpCurrentWindow->Name, String); writef(gpCurrentWindow->Server->FD, "PRIVMSG %s :%s\n", gpCurrentWindow->Name, String @@ -306,17 +318,31 @@ tServer *Server_Connect(const char *Name, const char *AddressString, short PortN gpServers = ret; // Read some initial data - printf("%s: Connection opened\n", Name); + Message_Append(NULL, MSG_TYPE_SERVER, Name, "", "Connection opened"); ProcessIncoming(ret); // Identify writef(ret->FD, "USER %s %s %s : %s\n", gsUsername, gsHostname, AddressString, gsRealName); writef(ret->FD, "NICK %s\n", gsNickname); - printf("%s: Identified\n", Name); + Message_Append(NULL, MSG_TYPE_SERVER, Name, "", "Identified"); + //printf("%s: Identified\n", Name); return ret; } +tMessage *Message_AppendF(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message, ...) +{ + va_list args; + int len; + va_start(args, Message); + len = vsnprintf(NULL, 1000, Message, args); + { + char buf[len+1]; + vsnprintf(buf, len+1, Message, args); + return Message_Append(Server, Type, Source, Dest, buf); + } +} + tMessage *Message_Append(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message) { tMessage *ret; @@ -324,15 +350,14 @@ tMessage *Message_Append(tServer *Server, int Type, const char *Source, const ch int msgLen = strlen(Message); // NULL servers are internal messages - if( Server == NULL ) + if( Server == NULL || Source[0] == '\0' ) { win = &gWindow_Status; } // 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 ) { @@ -395,19 +420,24 @@ tMessage *Message_Append(tServer *Server, int Type, const char *Source, const ch //TODO: Set location - #if 1 - if( win == gpCurrentWindow ) { + { int pos = SetCursorPos(giTerminal_Height-2, 0); - printf("\x1B[S"); // Scroll up 1 - printf("[%s] %s\n", Source, Message); + if( win == gpCurrentWindow ) { + int prefixlen = strlen(Source) + 3; + int avail = giTerminal_Width - prefixlen; + int msglen = strlen(Message); + printf("\x1B[T"); // Scroll down 1 (free space below) + 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); + } + } SetCursorPos(-1, pos); } - #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 return ret; } @@ -442,6 +472,40 @@ tWindow *Window_Create(tServer *Server, const char *Name) return ret; } +void Redraw_Screen(void) +{ + int y = 0; + tMessage *msg; + + printf("\x1B[2J"); // Clear screen + printf("\x1B[0;0H"); // Reset cursor + + msg = gpCurrentWindow->Messages; + + // TODO: Title bar? + + // Note: This renders from the bottom up + for( y = giTerminal_Height - 1; y -- && msg; msg = msg->Next) + { + int msglen = strlen(msg->Data); + int prefix_len = 3 + strlen(msg->Source); + int line_avail = giTerminal_Width - prefix_len; + int i = 0, done = 0; + + y -= msglen / line_avail; // Extra lines (y-- above handles the 1 line case) + SetCursorPos(y, 0); + printf("[%s] ", msg->Source); + + while(done < msglen) { + done += printf("%.*s", line_avail, msg->Data+done); + i ++; + SetCursorPos(y+i, prefix_len); + } + } + + // Bottom line is rendered by the prompt +} + void Cmd_PRIVMSG(tServer *Server, const char *Dest, const char *Src, const char *Message) { Message_Append(Server, MSG_TYPE_STANDARD, Dest, Src, Message); @@ -454,6 +518,8 @@ void ParseServerLine(tServer *Server, char *Line) { int pos = 0; char *ident, *cmd; + + _SysDebug("Server %s: Line = %s", Server->Name, Line); // Message? if( *Line == ':' ) @@ -481,6 +547,22 @@ void ParseServerLine(tServer *Server, char *Line) switch(num) { + case 353: // /NAMES list + // = :list +// GetValue(Line, &pos); // '=' + user = GetValue(Line, &pos); // Actually channel + message = Line + pos + 1; // List + Message_AppendF(Server, MSG_TYPE_SERVER, 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); + break; + case 372: // MOTD Data + case 376: // MOTD End + default: //printf("[%s] %i %s\n", Server->Name, num, message); Message_Append(Server, MSG_TYPE_SERVER, ident, user, message); @@ -492,6 +574,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; @@ -514,7 +597,7 @@ void ParseServerLine(tServer *Server, char *Line) else { message = GetValue(Line, &pos); } - Cmd_PRIVMSG(Server, dest, ident, message); +// Cmd_PRIVMSG(Server, dest, ident, message); Message_Append(Server, MSG_TYPE_STANDARD, ident, dest, message); } else if( strcmp(cmd, "JOIN" ) == 0 ) @@ -525,13 +608,20 @@ void ParseServerLine(tServer *Server, char *Line) } else { - printf("Unknown message %s (%s)\n", cmd, Line+pos); + Message_AppendF(Server, MSG_TYPE_SERVER, "", "", "Unknown message %s (%s)\n", cmd, Line+pos); } } - else { + else { + cmd = GetValue(Line, &pos); - // Command to client - printf("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); + } } } @@ -552,7 +642,7 @@ int ProcessIncoming(tServer *Server) { #endif // Read data - len = read(Server->FD, BUFSIZ - Server->ReadPos, &Server->InBuf[Server->ReadPos]); + len = read(Server->FD, &Server->InBuf[Server->ReadPos], BUFSIZ - Server->ReadPos); if( len == -1 ) { return -1; } @@ -606,7 +696,7 @@ int writef(int FD, const char *Format, ...) vsnprintf(buf, len+1, Format, args); va_end(args); - return write(FD, len, buf); + return write(FD, buf, len); } } @@ -616,48 +706,31 @@ 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"); +// printf("Setting port and remote address\n"); ioctl(fd, 5, &PortNumber); ioctl(fd, 6, addrBuffer); // Connect - printf("Initiating connection\n"); +// printf("Initiating connection\n"); if( ioctl(fd, 7, NULL) == 0 ) { // Shouldn't happen :( fprintf(stderr, "Unable to start connection\n"); @@ -698,12 +771,14 @@ char *GetValue(char *Src, int *Ofs) int SetCursorPos(int Row, int Col) { + int rv; if( Row == -1 ) { Row = Col / giTerminal_Width; Col = Col % giTerminal_Width; } + rv = ioctl(1, 9, NULL); // Ugh, constants printf("\x1B[%i;%iH", Col, Row); - return ioctl(1, 9, NULL); // Ugh, constants + return rv; } static inline int isdigit(int ch)