4 #include <acess/sys.h>
\r
9 #include <readline.h>
\r
10 #include <acess/devices/pty.h>
\r
13 typedef struct sServer {
\r
14 struct sServer *Next;
\r
16 char InBuf[BUFSIZ+1];
\r
21 typedef struct sMessage
\r
23 struct sMessage *Next;
\r
27 char *Source; // Pointer into `Data`
\r
31 typedef struct sWindow
\r
33 struct sWindow *Next;
\r
35 tServer *Server; //!< Canoical server (can be NULL)
\r
37 char Name[]; // Channel name / remote user
\r
43 MSG_TYPE_SERVER, // Server message
\r
45 MSG_TYPE_NOTICE, // NOTICE command
\r
46 MSG_TYPE_JOIN, // JOIN command
\r
47 MSG_TYPE_PART, // PART command
\r
48 MSG_TYPE_QUIT, // QUIT command
\r
50 MSG_TYPE_STANDARD, // Standard line
\r
51 MSG_TYPE_ACTION, // /me
\r
56 // === PROTOTYPES ===
\r
57 int ParseArguments(int argc, const char *argv[]);
\r
58 int ParseUserCommand(char *String);
\r
60 tServer *Server_Connect(const char *Name, const char *AddressString, short PortNumber);
\r
61 tMessage *Message_AppendF(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message, ...);
\r
62 tMessage *Message_Append(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message);
\r
63 tWindow *Window_Create(tServer *Server, const char *Name);
\r
64 void Redraw_Screen(void);
\r
66 int ProcessIncoming(tServer *Server);
\r
68 void SetCursorPos(int Row, int Col);
\r
69 int writef(int FD, const char *Format, ...);
\r
70 int OpenTCP(const char *AddressString, short PortNumber);
\r
71 char *GetValue(char *Str, int *Ofs);
\r
72 static inline int isdigit(int ch);
\r
75 char *gsUsername = "user";
\r
76 char *gsHostname = "acess";
\r
77 char *gsRealName = "Acess2 IRC Client";
\r
78 char *gsNickname = "acess";
\r
80 tWindow gWindow_Status = {
\r
81 NULL, NULL, NULL, // No next, empty list, no server
\r
82 0, {""} // No activity, empty name (rendered as status)
\r
84 tWindow *gpWindows = &gWindow_Status;
\r
85 tWindow *gpCurrentWindow = &gWindow_Status;
\r
86 int giTerminal_Width = 80;
\r
87 int giTerminal_Height = 25;
\r
90 void ExitHandler(void)
\r
92 printf("\x1B[?1047l");
\r
96 int main(int argc, const char *argv[], const char *envp[])
\r
99 tReadline *readline_info;
\r
101 // Parse Command line
\r
102 if( (tmp = ParseArguments(argc, argv)) ) return tmp;
\r
104 atexit(ExitHandler);
\r
107 if( _SysIOCtl(1, DRV_IOCTL_TYPE, NULL) != DRV_TYPE_TERMINAL ) {
\r
108 printf(stderr, "note: assuming 80x25, can't get terminal dimensions\n");
\r
109 giTerminal_Width = 80;
\r
110 giTerminal_Height = 25;
\r
113 struct ptydims dims;
\r
114 _SysIOCtl(1, PTY_IOCTL_GETDIMS, &dims);
\r
117 struct ptydims dims;
\r
118 if( _SysIOCtl(1, PTY_IOCTL_GETDIMS, &dims) ) {
\r
119 perror("Can't get terminal dimensions");
\r
123 >>>>>>> e7a76b0d8a0cc6aa77966509780973a6f8216ef7
124 giTerminal_Width = dims.W;
\r
125 giTerminal_Height = dims.H;
\r
128 printf("\x1B[?1047h");
\r
129 printf("\x1B[%i;%ir", 0, giTerminal_Height-1);
\r
131 SetCursorPos(giTerminal_Height-1, 0);
\r
132 printf("[(status)] ");
\r
134 // HACK: Static server entry
\r
135 // UCC (University [of Western Australia] Computer Club) IRC Server
\r
136 gWindow_Status.Server = Server_Connect( "UCC", "130.95.13.18", 6667 );
\r
137 // Freenode (#osdev)
\r
138 // gWindow_Status.Server = Server_Connect( "Freenode", "89.16.176.16", 6667 );
\r
139 // gWindow_Status.Server = Server_Connect( "Host", "10.0.2.2", 6667 );
\r
141 // gWindow_Status.Server = Server_Connect( "BitlBee", "192.168.1.39", 6667 );
\r
143 if( !gWindow_Status.Server )
\r
146 SetCursorPos(giTerminal_Height-1, 0);
\r
147 printf("[(status)] ");
\r
149 readline_info = Readline_Init(1);
\r
153 fd_set readfds, errorfds;
\r
158 FD_ZERO(&errorfds);
\r
159 FD_SET(0, &readfds); // stdin
\r
161 // Fill server FDs in fd_set
\r
162 for( srv = gpServers; srv; srv = srv->Next )
\r
164 FD_SET(srv->FD, &readfds);
\r
165 FD_SET(srv->FD, &errorfds);
\r
166 if( srv->FD > maxFD )
\r
170 rv = _SysSelect(maxFD+1, &readfds, 0, &errorfds, NULL, 0);
\r
171 if( rv == -1 ) break;
\r
173 if(FD_ISSET(0, &readfds))
\r
176 char *cmd = Readline_NonBlock(readline_info);
\r
181 ParseUserCommand(cmd);
\r
185 SetCursorPos(giTerminal_Height-1, 0);
\r
186 printf("\x1B[K"); // Clear line
\r
187 if( gpCurrentWindow->Name[0] )
\r
188 printf("[%s:%s] ", gpCurrentWindow->Server->Name, gpCurrentWindow->Name);
\r
190 printf("[(status)] ");
\r
196 for( srv = gpServers; srv; srv = srv->Next )
\r
198 if(FD_ISSET(srv->FD, &readfds))
\r
200 if( ProcessIncoming(srv) != 0 ) {
\r
206 if(FD_ISSET(srv->FD, &errorfds))
\r
218 for( srv = gpServers; srv; srv = srv->Next )
\r
219 _SysClose(srv->FD);
\r
225 * \todo Actually implement correctly :)
\r
227 int ParseArguments(int argc, const char *argv[])
\r
233 void Cmd_join(char *ArgString)
\r
236 char *channel_name = GetValue(ArgString, &pos);
\r
238 if( gpCurrentWindow->Server )
\r
240 writef(gpCurrentWindow->Server->FD, "JOIN :%s\n", channel_name);
\r
244 void Cmd_quit(char *ArgString)
\r
246 const char *quit_message = ArgString;
\r
247 if( quit_message == NULL || quit_message[0] == '\0' )
\r
248 quit_message = "/quit - Acess2 IRC Client";
\r
250 for( tServer *srv = gpServers; srv; srv = srv->Next )
\r
252 writef(srv->FD, "QUIT :%s\n", quit_message);
\r
258 void Cmd_window(char *ArgString)
\r
261 char *window_id = GetValue(ArgString, &pos);
\r
262 int window_num = atoi(window_id);
\r
264 if( window_num > 0 )
\r
267 window_num --; // Move to base 0
\r
268 // Get `window_num`th window
\r
269 for( win = gpWindows; win && window_num--; win = win->Next );
\r
271 gpCurrentWindow = win;
\r
274 // Otherwise, silently ignore
\r
279 for( tWindow *win = gpWindows; win; win = win->Next, window_num ++ )
\r
281 if( win->Name[0] ) {
\r
282 Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "",
\r
283 "%i: %s/%s", window_num, win->Server->Name, win->Name);
\r
286 Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "",
\r
287 "%i: (status)", window_num);
\r
295 void (*Fcn)(char *ArgString);
\r
297 {"join", Cmd_join},
\r
298 {"quit", Cmd_quit},
\r
299 {"window", Cmd_window},
\r
300 {"win", Cmd_window},
\r
303 const int ciNumCommands = sizeof(caCommands)/sizeof(caCommands[0]);
\r
306 * \brief Handle a line from the prompt
\r
308 int ParseUserCommand(char *String)
\r
310 if( String[0] == '/' )
\r
315 command = GetValue(String, &pos)+1;
\r
317 // TODO: Prefix matches
\r
319 for( int i = 0; i < ciNumCommands; i ++ )
\r
321 if( strcmp(command, caCommands[i].Name) == 0 ) {
\r
326 if( cmdIdx != -1 ) {
\r
327 caCommands[cmdIdx].Fcn(String+pos);
\r
331 Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "", "Unknown command %s", command);
\r
337 // - Only send if server is valid and window name is non-empty
\r
338 if( gpCurrentWindow->Server && gpCurrentWindow->Name[0] )
\r
340 Message_Append(gpCurrentWindow->Server, MSG_TYPE_STANDARD,
\r
341 gsNickname, gpCurrentWindow->Name, String);
\r
342 writef(gpCurrentWindow->Server->FD,
\r
343 "PRIVMSG %s :%s\n", gpCurrentWindow->Name,
\r
353 * \brief Connect to a server
\r
355 tServer *Server_Connect(const char *Name, const char *AddressString, short PortNumber)
\r
359 ret = calloc(1, sizeof(tServer) + strlen(Name) + 1);
\r
361 strcpy(ret->Name, Name);
\r
363 // Connect to the remove server
\r
364 ret->FD = OpenTCP( AddressString, PortNumber );
\r
365 if( ret->FD == -1 ) {
\r
366 fprintf(stderr, "%s: Unable to create socket\n", Name);
\r
370 // Append to open list
\r
371 ret->Next = gpServers;
\r
374 // Read some initial data
\r
375 Message_Append(NULL, MSG_TYPE_SERVER, Name, "", "Connection opened");
\r
376 ProcessIncoming(ret);
\r
379 writef(ret->FD, "USER %s %s %s : %s\n", gsUsername, gsHostname, AddressString, gsRealName);
\r
380 writef(ret->FD, "NICK %s\n", gsNickname);
\r
381 Message_Append(NULL, MSG_TYPE_SERVER, Name, "", "Identified");
\r
382 //printf("%s: Identified\n", Name);
\r
387 tMessage *Message_AppendF(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message, ...)
\r
391 va_start(args, Message);
\r
392 len = vsnprintf(NULL, 0, Message, args);
\r
395 vsnprintf(buf, len+1, Message, args);
\r
396 return Message_Append(Server, Type, Source, Dest, buf);
\r
400 tMessage *Message_Append(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message)
\r
402 tWindow *win = NULL;
\r
403 int msgLen = strlen(Message);
\r
405 // Server==NULL indicates an internal message
\r
406 if( Server == NULL || Source[0] == '\0' )
\r
408 win = &gWindow_Status;
\r
410 // Determine if it's a channel or PM
\r
411 else if( Dest[0] == '#' || Dest[0] == '&' ) // TODO: Better determining here
\r
413 for(win = gpWindows; win; win = win->Next)
\r
415 if( win->Server == Server && strcmp(win->Name, Dest) == 0 )
\r
421 win = Window_Create(Server, Dest);
\r
425 else if( strcmp(Dest, Server->Nick) != 0 )
\r
427 // Umm... message for someone who isn't us?
\r
428 win = &gWindow_Status; // Stick it in the status window, just in case
\r
432 else if( strchr(Source, '.') ) // TODO: And again, less hack please
\r
435 for(win = gpWindows; win; win = win->Next)
\r
437 if( win->Server == Server && strcmp(win->Name, Source) == 0 )
\r
444 win = &gWindow_Status;
\r
451 for(win = gpWindows; win; win = win->Next)
\r
453 if( win->Server == Server && strcmp(win->Name, Source) == 0 )
\r
459 win = Window_Create(Server, Dest);
\r
463 // Create message cache
\r
464 _SysDebug("Win (%s) msg: <%s> %s", win->Name, Source, Message);
\r
466 ret = malloc( sizeof(tMessage) + msgLen + 1 + strlen(Source) + 1 );
\r
467 ret->Source = ret->Data + msgLen + 1;
\r
468 strcpy(ret->Source, Source);
\r
469 strcpy(ret->Data, Message);
\r
471 ret->Server = Server;
\r
473 // Append to window message list
\r
474 ret->Next = win->Messages;
\r
475 win->Messages = ret;
\r
477 // Print now if current window
\r
478 if( win == gpCurrentWindow )
\r
481 SetCursorPos(giTerminal_Height-2, 0);
\r
482 int prefixlen = strlen(Source) + 3;
\r
483 int avail = giTerminal_Width - prefixlen;
\r
484 int msglen = strlen(Message);
\r
485 printf("\x1B[T"); // Scroll down 1 (free space below)
\r
486 printf("[%s] %.*s\n", Source, avail, Message);
\r
487 while( msglen > avail ) {
\r
491 SetCursorPos(giTerminal_Height-2, prefixlen);
\r
492 printf("%.*s\n", avail, Message);
\r
500 tWindow *Window_Create(tServer *Server, const char *Name)
\r
502 tWindow *ret, *prev = NULL;
\r
505 // Get the end of the list
\r
506 // TODO: Cache this instead
\r
507 for( ret = gpCurrentWindow; ret; prev = ret, ret = ret->Next )
\r
510 ret = malloc(sizeof(tWindow) + strlen(Name) + 1);
\r
511 ret->Messages = NULL;
\r
512 ret->Server = Server;
\r
513 ret->ActivityLevel = 1;
\r
514 strcpy(ret->Name, Name);
\r
517 ret->Next = prev->Next;
\r
520 else { // Shouldn't happen really
\r
521 ret->Next = gpWindows;
\r
525 // printf("Win %i %s:%s created\n", num, Server->Name, Name);
\r
530 void Redraw_Screen(void)
\r
535 printf("\x1B[2J"); // Clear screen
\r
536 printf("\x1B[0;0H"); // Reset cursor
\r
538 msg = gpCurrentWindow->Messages;
\r
540 // TODO: Title bar?
\r
542 // Note: This renders from the bottom up
\r
543 for( y = giTerminal_Height - 1; y -- && msg; msg = msg->Next)
\r
545 int msglen = strlen(msg->Data);
\r
546 int prefix_len = 3 + strlen(msg->Source);
\r
547 int line_avail = giTerminal_Width - prefix_len;
\r
548 int i = 0, done = 0;
\r
550 y -= msglen / line_avail; // Extra lines (y-- above handles the 1 line case)
\r
551 SetCursorPos(y, 0);
\r
552 printf("[%s] ", msg->Source);
\r
554 while(done < msglen) {
\r
555 done += printf("%.*s", line_avail, msg->Data+done);
\r
557 SetCursorPos(y+i, prefix_len);
\r
561 // Bottom line is rendered by the prompt
\r
564 void Cmd_PRIVMSG(tServer *Server, const char *Dest, const char *Src, const char *Message)
\r
566 Message_Append(Server, MSG_TYPE_STANDARD, Dest, Src, Message);
\r
567 //printf("<%s:%s:%s> %s\n", Server->Name, Dest, Src, Message);
\r
572 void ParseServerLine(tServer *Server, char *Line)
\r
577 // _SysDebug("[%s] %s", Server->Name, Line);
\r
583 ident = GetValue(Line, &pos); // Ident (user or server)
\r
584 cmd = GetValue(Line, &pos);
\r
587 if( isdigit(cmd[0]) && isdigit(cmd[1]) && isdigit(cmd[2]) )
\r
589 char *user, *message;
\r
591 num = (cmd[0] - '0') * 100;
\r
592 num += (cmd[1] - '0') * 10;
\r
593 num += (cmd[2] - '0') * 1;
\r
595 user = GetValue(Line, &pos);
\r
597 if( Line[pos] == ':' ) {
\r
598 message = Line + pos + 1;
\r
601 message = GetValue(Line, &pos);
\r
607 user = message; // Channel
\r
608 message = Line + pos + 1; // Topic
\r
609 Message_AppendF(Server, MSG_TYPE_SERVER, user, user, "Topic: %s", message);
\r
611 case 333: // Topic set by
\r
612 user = message; // Channel
\r
613 message = GetValue(Line, &pos); // User
\r
614 GetValue(Line, &pos); // Timestamp
\r
615 Message_AppendF(Server, MSG_TYPE_SERVER, user, user, "Set by %s", message);
\r
617 case 353: // /NAMES list
\r
618 // <user> = <channel> :list
\r
619 // '=' was eaten in and set to message
\r
620 user = GetValue(Line, &pos); // Actually channel
\r
621 message = Line + pos + 1; // List
\r
622 Message_AppendF(Server, MSG_TYPE_SERVER, user, user, "Names: %s", message);
\r
624 case 366: // end of /NAMES list
\r
625 // <user> <channel> :msg
\r
627 message = Line + pos + 1;
\r
628 Message_Append(Server, MSG_TYPE_SERVER, user, user, message);
\r
630 case 372: // MOTD Data
\r
631 case 375: // MOTD Start
\r
632 case 376: // MOTD End
\r
635 //printf("[%s] %i %s\n", Server->Name, num, message);
\r
636 Message_Append(Server, MSG_TYPE_SERVER, ident, user, message);
\r
640 else if( strcmp(cmd, "NOTICE") == 0 )
\r
642 char *class, *message;
\r
644 class = GetValue(Line, &pos);
\r
645 _SysDebug("NOTICE class='%s'", class);
\r
647 if( Line[pos] == ':' ) {
\r
648 message = Line + pos + 1;
\r
651 message = GetValue(Line, &pos);
\r
654 //printf("[%s] NOTICE %s: %s\n", Server->Name, ident, message);
\r
655 char *ident_bang = strchr(ident, '!');
\r
657 *ident_bang = '\0';
\r
659 Message_Append(Server, MSG_TYPE_NOTICE, ident, "", message);
\r
661 else if( strcmp(cmd, "PRIVMSG") == 0 )
\r
663 char *dest, *message;
\r
664 dest = GetValue(Line, &pos);
\r
666 if( Line[pos] == ':' ) {
\r
667 message = Line + pos + 1;
\r
670 message = GetValue(Line, &pos);
\r
673 // TODO: Catch when the privmsg is addressed to the user
\r
675 // Cmd_PRIVMSG(Server, dest, ident, message);
\r
676 char *ident_bang = strchr(ident, '!');
\r
678 *ident_bang = '\0';
\r
680 Message_Append(Server, MSG_TYPE_STANDARD, ident, dest, message);
\r
682 else if( strcmp(cmd, "JOIN" ) == 0 )
\r
685 channel = Line + pos + 1;
\r
686 Window_Create(Server, channel);
\r
690 Message_AppendF(Server, MSG_TYPE_SERVER, "", "", "Unknown message %s (%s)\n", cmd, Line+pos);
\r
694 cmd = GetValue(Line, &pos);
\r
696 if( strcmp(cmd, "PING") == 0 ) {
\r
697 writef(Server->FD, "PONG %s\n", gsHostname);
\r
701 // Command to client
\r
702 Message_AppendF(NULL, MSG_TYPE_UNK, "", "", "Client Command: %s", Line);
\r
708 * \brief Process incoming lines from the server
\r
710 int ProcessIncoming(tServer *Server)
\r
712 char *ptr, *newline;
\r
715 // While there is data in the buffer, read it into user memory and
\r
716 // process it line by line
\r
717 // ioctl#8 on a TCP client gets the number of bytes in the recieve buffer
\r
718 // - Used to avoid blocking
\r
720 while( (len = _SysIOCtl(Server->FD, 8, NULL)) > 0 )
\r
724 len = _SysRead(Server->FD, &Server->InBuf[Server->ReadPos], BUFSIZ - Server->ReadPos);
\r
728 Server->InBuf[Server->ReadPos + len] = '\0';
\r
730 // Break into lines
\r
731 ptr = Server->InBuf;
\r
732 while( (newline = strchr(ptr, '\n')) )
\r
735 if( newline[-1] == '\r' ) newline[-1] = '\0';
\r
736 ParseServerLine(Server, ptr);
\r
740 // Handle incomplete lines
\r
741 if( ptr - Server->InBuf < len + Server->ReadPos ) {
\r
742 // Update the read position
\r
743 // InBuf ReadPos ptr ReadPos+len
\r
744 // | old | new used | new unused |
\r
745 Server->ReadPos = len + Server->ReadPos - (ptr - Server->InBuf);
\r
746 // Copy stuff back (moving "new unused" to the start of the buffer)
\r
747 memcpy(Server->InBuf, ptr, Server->ReadPos);
\r
750 Server->ReadPos = 0;
\r
760 * \brief Write a formatted string to a file descriptor
\r
763 int writef(int FD, const char *Format, ...)
\r
768 va_start(args, Format);
\r
769 len = vsnprintf(NULL, 0, Format, args);
\r
774 va_start(args, Format);
\r
775 vsnprintf(buf, len+1, Format, args);
\r
778 return _SysWrite(FD, buf, len);
\r
783 * \brief Initialise a TCP connection to \a AddressString on port \a PortNumber
\r
785 int OpenTCP(const char *AddressString, short PortNumber)
\r
788 char addrBuffer[8];
\r
790 // Parse IP Address
\r
791 addrType = Net_ParseAddress(AddressString, addrBuffer);
\r
792 if( addrType == 0 ) {
\r
793 fprintf(stderr, "Unable to parse '%s' as an IP address\n", AddressString);
\r
794 _SysDebug("Unable to parse '%s' as an IP address\n", AddressString);
\r
798 // Finds the interface for the destination address
\r
799 fd = Net_OpenSocket(addrType, addrBuffer, "tcpc");
\r
801 fprintf(stderr, "Unable to open TCP Client to '%s'\n", AddressString);
\r
802 _SysDebug("Unable to open TCP client to '%s'\n", AddressString);
\r
806 // Set remote port and address
\r
807 // printf("Setting port and remote address\n");
\r
808 _SysIOCtl(fd, 5, &PortNumber);
\r
809 _SysIOCtl(fd, 6, addrBuffer);
\r
812 // printf("Initiating connection\n");
\r
813 if( _SysIOCtl(fd, 7, NULL) == 0 ) {
\r
814 // Shouldn't happen :(
\r
815 fprintf(stderr, "Unable to start connection\n");
\r
819 // Return descriptor
\r
824 * \brief Read a space-separated value from a string
\r
826 char *GetValue(char *Src, int *Ofs)
\r
829 char *ret = Src + pos;
\r
832 if( !Src ) return NULL;
\r
834 while( *ret == ' ' ) ret ++;
\r
836 end = strchr(ret, ' ');
\r
841 end = ret + strlen(ret) - 1;
\r
845 while( *ret == ' ' ) end ++;
\r
851 void SetCursorPos(int Row, int Col)
\r
853 printf("\x1B[%i;%iH", Col, Row);
\r
856 static inline int isdigit(int ch)
\r
858 return '0' <= ch && ch < '9';
\r