X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Firc_src%2Fmain.c;h=20223bbf78bdebd845d7327b721a31854178f96a;hb=5b6ff5b0698142d4f17a693c0d340df10b375926;hp=0c90c5014df0ebf667863d0678e7e56f5ec4e93f;hpb=a200e721cb12cbfdb09e32089bd31b4ad84baa67;p=tpg%2Facess2.git diff --git a/Usermode/Applications/irc_src/main.c b/Usermode/Applications/irc_src/main.c index 0c90c501..20223bbf 100644 --- a/Usermode/Applications/irc_src/main.c +++ b/Usermode/Applications/irc_src/main.c @@ -59,18 +59,21 @@ 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 + int SetCursorPos(int Row, int Col); int writef(int FD, const char *Format, ...); int OpenTCP(const char *AddressString, short PortNumber); char *GetValue(char *Str, int *Ofs); static inline int isdigit(int ch); // === GLOBALS === -char *gsUsername = "root"; +char *gsUsername = "user"; char *gsHostname = "acess"; char *gsRealName = "Acess2 IRC Client"; char *gsNickname = "acess"; @@ -81,8 +84,16 @@ tWindow gWindow_Status = { }; tWindow *gpWindows = &gWindow_Status; tWindow *gpCurrentWindow = &gWindow_Status; + int giTerminal_Width = 80; + int giTerminal_Height = 25; // ==== CODE ==== +void ExitHandler(void) +{ + printf("\x1B[?1047l"); + printf("Quit\n"); +} + int main(int argc, const char *argv[], const char *envp[]) { int tmp; @@ -91,13 +102,29 @@ int main(int argc, const char *argv[], const char *envp[]) // Parse Command line if( (tmp = ParseArguments(argc, argv)) ) return tmp; + atexit(ExitHandler); + + giTerminal_Width = ioctl(1, 5, NULL); // getset_width + giTerminal_Height = ioctl(1, 6, NULL); // getset_height + + 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; + SetCursorPos(giTerminal_Height-1, 0); + printf("[(status)] "); readline_info = Readline_Init(1); for( ;; ) @@ -133,6 +160,13 @@ int main(int argc, const char *argv[], const char *envp[]) ParseUserCommand(cmd); } 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 + printf("[(status)] "); } } @@ -188,21 +222,23 @@ int ParseUserCommand(char *String) if( gpCurrentWindow->Server ) { - writef(gpCurrentWindow->Server->FD, "JOIN %s\n", channel_name); + writef(gpCurrentWindow->Server->FD, "JOIN :%s\n", channel_name); } } else if( strcmp(command, "/quit") == 0 ) { - char *quit_message = GetValue(String, &pos); + char *quit_message = String + pos; tServer *srv; - if( !quit_message ) + 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); + writef(srv->FD, "QUIT :%s\n", quit_message); } + + exit(0); } else if( strcmp(command, "/window") == 0 || strcmp(command, "/win") == 0 || strcmp(command, "/w") == 0 ) { @@ -217,20 +253,30 @@ int ParseUserCommand(char *String) for( win = gpWindows; win && window_num--; win = win->Next ); if( win ) { gpCurrentWindow = win; - if( win->Name[0] ) - printf("[%s:%s] ", win->Server->Name, win->Name); - else - printf("[(status)] ", win->Server->Name, win->Name); + 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); + } + } + } } 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 @@ -239,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 @@ -272,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; @@ -290,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; } @@ -359,6 +421,27 @@ tMessage *Message_Append(tServer *Server, int Type, const char *Source, const ch ret->Next = win->Messages; win->Messages = ret; + //TODO: Set location + + { + int pos = SetCursorPos(giTerminal_Height-2, 0); + 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); + } + return ret; } @@ -387,14 +470,49 @@ tWindow *Window_Create(tServer *Server, const char *Name) gpWindows = ret; } - printf("Win %i %s:%s created\n", num, Server->Name, Name); +// printf("Win %i %s:%s created\n", num, Server->Name, 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) { - printf("<%s:%s:%s> %s\n", Server->Name, Dest, Src, Message); + Message_Append(Server, MSG_TYPE_STANDARD, Dest, Src, Message); + //printf("<%s:%s:%s> %s\n", Server->Name, Dest, Src, Message); } /** @@ -403,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 == ':' ) @@ -430,8 +550,24 @@ 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); + //printf("[%s] %i %s\n", Server->Name, num, message); Message_Append(Server, MSG_TYPE_SERVER, ident, user, message); break; } @@ -449,7 +585,7 @@ void ParseServerLine(tServer *Server, char *Line) message = GetValue(Line, &pos); } - printf("[%s] NOTICE %s: %s\n", Server->Name, ident, message); + //printf("[%s] NOTICE %s: %s\n", Server->Name, ident, message); Message_Append(Server, MSG_TYPE_NOTICE, ident, "", message); } else if( strcmp(cmd, "PRIVMSG") == 0 ) @@ -463,18 +599,31 @@ 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 ) + { + char *channel; + channel = GetValue(Line, &pos) + 1; + Window_Create(Server, channel); + } 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); + } } } @@ -495,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; } @@ -549,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); } } @@ -576,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? @@ -595,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"); @@ -639,6 +788,18 @@ char *GetValue(char *Src, int *Ofs) return ret; } +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 rv; +} + static inline int isdigit(int ch) { return '0' <= ch && ch < '9';