X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=Usermode%2FApplications%2Firc_src%2Fmain.c;h=20223bbf78bdebd845d7327b721a31854178f96a;hb=5b6ff5b0698142d4f17a693c0d340df10b375926;hp=24919aa0aa97e9a1cd8b415eaa42383f32aaffd4;hpb=768eb85491125c0073e7739641c2b317dc8d1605;p=tpg%2Facess2.git diff --git a/Usermode/Applications/irc_src/main.c b/Usermode/Applications/irc_src/main.c index 24919aa0..20223bbf 100644 --- a/Usermode/Applications/irc_src/main.c +++ b/Usermode/Applications/irc_src/main.c @@ -59,8 +59,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 +91,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 +110,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 +162,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 +227,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 +253,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 +263,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 +285,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 +320,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,7 +352,7 @@ 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; } @@ -397,17 +425,20 @@ tMessage *Message_Append(tServer *Server, int Type, const char *Source, const ch { int pos = SetCursorPos(giTerminal_Height-2, 0); - printf("\x1B[T"); // Scroll down 1 (free space below) - #if 1 if( win == gpCurrentWindow ) { - printf("[%s] %s\n", Source, Message); + 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); + } } - #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); } @@ -444,6 +475,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); @@ -456,6 +521,8 @@ void ParseServerLine(tServer *Server, char *Line) { int pos = 0; char *ident, *cmd; + + _SysDebug("Server %s: Line = %s", Server->Name, Line); // Message? if( *Line == ':' ) @@ -483,6 +550,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); @@ -516,7 +599,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 ) @@ -527,13 +610,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); + } } } @@ -554,7 +644,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; } @@ -608,7 +698,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); } } @@ -635,7 +725,7 @@ int OpenTCP(const char *AddressString, short PortNumber) return -1; } - printf("iface = '%s'\n", iface); +// printf("iface = '%s'\n", iface); // Open client socket // TODO: Move this out to libnet? @@ -654,12 +744,12 @@ int OpenTCP(const char *AddressString, short PortNumber) } // 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"); @@ -700,12 +790,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)