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 struct ptydims dims;
\r
108 if( _SysIOCtl(1, PTY_IOCTL_GETDIMS, &dims) ) {
\r
109 perror("Can't get terminal dimensions");
\r
113 giTerminal_Width = dims.W;
\r
114 giTerminal_Height = dims.H;
\r
117 printf("\x1B[?1047h");
\r
118 printf("\x1B[%i;%ir", 0, giTerminal_Height-1);
\r
120 SetCursorPos(giTerminal_Height-1, 0);
\r
121 printf("[(status)] ");
\r
123 // HACK: Static server entry
\r
124 // UCC (University [of Western Australia] Computer Club) IRC Server
\r
125 gWindow_Status.Server = Server_Connect( "UCC", "130.95.13.18", 6667 );
\r
126 // Freenode (#osdev)
\r
127 // gWindow_Status.Server = Server_Connect( "Freenode", "89.16.176.16", 6667 );
\r
128 // gWindow_Status.Server = Server_Connect( "Host", "10.0.2.2", 6667 );
\r
130 // gWindow_Status.Server = Server_Connect( "BitlBee", "192.168.1.39", 6667 );
\r
132 if( !gWindow_Status.Server )
\r
135 SetCursorPos(giTerminal_Height-1, 0);
\r
136 printf("[(status)] ");
\r
138 readline_info = Readline_Init(1);
\r
142 fd_set readfds, errorfds;
\r
147 FD_ZERO(&errorfds);
\r
148 FD_SET(0, &readfds); // stdin
\r
150 // Fill server FDs in fd_set
\r
151 for( srv = gpServers; srv; srv = srv->Next )
\r
153 FD_SET(srv->FD, &readfds);
\r
154 FD_SET(srv->FD, &errorfds);
\r
155 if( srv->FD > maxFD )
\r
159 rv = _SysSelect(maxFD+1, &readfds, 0, &errorfds, NULL, 0);
\r
160 if( rv == -1 ) break;
\r
162 if(FD_ISSET(0, &readfds))
\r
165 char *cmd = Readline_NonBlock(readline_info);
\r
170 ParseUserCommand(cmd);
\r
174 SetCursorPos(giTerminal_Height-1, 0);
\r
175 printf("\x1B[K"); // Clear line
\r
176 if( gpCurrentWindow->Name[0] )
\r
177 printf("[%s:%s] ", gpCurrentWindow->Server->Name, gpCurrentWindow->Name);
\r
179 printf("[(status)] ");
\r
185 for( srv = gpServers; srv; srv = srv->Next )
\r
187 if(FD_ISSET(srv->FD, &readfds))
\r
189 if( ProcessIncoming(srv) != 0 ) {
\r
195 if(FD_ISSET(srv->FD, &errorfds))
\r
207 for( srv = gpServers; srv; srv = srv->Next )
\r
208 _SysClose(srv->FD);
\r
214 * \todo Actually implement correctly :)
\r
216 int ParseArguments(int argc, const char *argv[])
\r
222 void Cmd_join(char *ArgString)
\r
225 char *channel_name = GetValue(ArgString, &pos);
\r
227 if( gpCurrentWindow->Server )
\r
229 writef(gpCurrentWindow->Server->FD, "JOIN :%s\n", channel_name);
\r
233 void Cmd_quit(char *ArgString)
\r
235 const char *quit_message = ArgString;
\r
236 if( quit_message == NULL || quit_message[0] == '\0' )
\r
237 quit_message = "/quit - Acess2 IRC Client";
\r
239 for( tServer *srv = gpServers; srv; srv = srv->Next )
\r
241 writef(srv->FD, "QUIT :%s\n", quit_message);
\r
247 void Cmd_window(char *ArgString)
\r
250 char *window_id = GetValue(ArgString, &pos);
\r
251 int window_num = atoi(window_id);
\r
253 if( window_num > 0 )
\r
256 window_num --; // Move to base 0
\r
257 // Get `window_num`th window
\r
258 for( win = gpWindows; win && window_num--; win = win->Next );
\r
260 gpCurrentWindow = win;
\r
263 // Otherwise, silently ignore
\r
268 for( tWindow *win = gpWindows; win; win = win->Next, window_num ++ )
\r
270 if( win->Name[0] ) {
\r
271 Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "",
\r
272 "%i: %s/%s", window_num, win->Server->Name, win->Name);
\r
275 Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "",
\r
276 "%i: (status)", window_num);
\r
284 void (*Fcn)(char *ArgString);
\r
286 {"join", Cmd_join},
\r
287 {"quit", Cmd_quit},
\r
288 {"window", Cmd_window},
\r
289 {"win", Cmd_window},
\r
292 const int ciNumCommands = sizeof(caCommands)/sizeof(caCommands[0]);
\r
295 * \brief Handle a line from the prompt
\r
297 int ParseUserCommand(char *String)
\r
299 if( String[0] == '/' )
\r
304 command = GetValue(String, &pos)+1;
\r
306 // TODO: Prefix matches
\r
308 for( int i = 0; i < ciNumCommands; i ++ )
\r
310 if( strcmp(command, caCommands[i].Name) == 0 ) {
\r
315 if( cmdIdx != -1 ) {
\r
316 caCommands[cmdIdx].Fcn(String+pos);
\r
320 Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "", "Unknown command %s", command);
\r
326 // - Only send if server is valid and window name is non-empty
\r
327 if( gpCurrentWindow->Server && gpCurrentWindow->Name[0] )
\r
329 Message_Append(gpCurrentWindow->Server, MSG_TYPE_STANDARD,
\r
330 gsNickname, gpCurrentWindow->Name, String);
\r
331 writef(gpCurrentWindow->Server->FD,
\r
332 "PRIVMSG %s :%s\n", gpCurrentWindow->Name,
\r
342 * \brief Connect to a server
\r
344 tServer *Server_Connect(const char *Name, const char *AddressString, short PortNumber)
\r
348 ret = calloc(1, sizeof(tServer) + strlen(Name) + 1);
\r
350 strcpy(ret->Name, Name);
\r
352 // Connect to the remove server
\r
353 ret->FD = OpenTCP( AddressString, PortNumber );
\r
354 if( ret->FD == -1 ) {
\r
355 fprintf(stderr, "%s: Unable to create socket\n", Name);
\r
359 // Append to open list
\r
360 ret->Next = gpServers;
\r
363 // Read some initial data
\r
364 Message_Append(NULL, MSG_TYPE_SERVER, Name, "", "Connection opened");
\r
365 ProcessIncoming(ret);
\r
368 writef(ret->FD, "USER %s %s %s : %s\n", gsUsername, gsHostname, AddressString, gsRealName);
\r
369 writef(ret->FD, "NICK %s\n", gsNickname);
\r
370 Message_Append(NULL, MSG_TYPE_SERVER, Name, "", "Identified");
\r
371 //printf("%s: Identified\n", Name);
\r
376 tMessage *Message_AppendF(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message, ...)
\r
380 va_start(args, Message);
\r
381 len = vsnprintf(NULL, 0, Message, args);
\r
384 vsnprintf(buf, len+1, Message, args);
\r
385 return Message_Append(Server, Type, Source, Dest, buf);
\r
389 tMessage *Message_Append(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message)
\r
391 tWindow *win = NULL;
\r
392 int msgLen = strlen(Message);
\r
394 // Server==NULL indicates an internal message
\r
395 if( Server == NULL || Source[0] == '\0' )
\r
397 win = &gWindow_Status;
\r
399 // Determine if it's a channel or PM
\r
400 else if( Dest[0] == '#' || Dest[0] == '&' ) // TODO: Better determining here
\r
402 for(win = gpWindows; win; win = win->Next)
\r
404 if( win->Server == Server && strcmp(win->Name, Dest) == 0 )
\r
410 win = Window_Create(Server, Dest);
\r
414 else if( strcmp(Dest, Server->Nick) != 0 )
\r
416 // Umm... message for someone who isn't us?
\r
417 win = &gWindow_Status; // Stick it in the status window, just in case
\r
421 else if( strchr(Source, '.') ) // TODO: And again, less hack please
\r
424 for(win = gpWindows; win; win = win->Next)
\r
426 if( win->Server == Server && strcmp(win->Name, Source) == 0 )
\r
433 win = &gWindow_Status;
\r
440 for(win = gpWindows; win; win = win->Next)
\r
442 if( win->Server == Server && strcmp(win->Name, Source) == 0 )
\r
448 win = Window_Create(Server, Dest);
\r
452 // Create message cache
\r
453 _SysDebug("Win (%s) msg: <%s> %s", win->Name, Source, Message);
\r
455 ret = malloc( sizeof(tMessage) + msgLen + 1 + strlen(Source) + 1 );
\r
456 ret->Source = ret->Data + msgLen + 1;
\r
457 strcpy(ret->Source, Source);
\r
458 strcpy(ret->Data, Message);
\r
460 ret->Server = Server;
\r
462 // Append to window message list
\r
463 ret->Next = win->Messages;
\r
464 win->Messages = ret;
\r
466 // Print now if current window
\r
467 if( win == gpCurrentWindow )
\r
470 SetCursorPos(giTerminal_Height-2, 0);
\r
471 int prefixlen = strlen(Source) + 3;
\r
472 int avail = giTerminal_Width - prefixlen;
\r
473 int msglen = strlen(Message);
\r
474 printf("\x1B[T"); // Scroll down 1 (free space below)
\r
475 printf("[%s] %.*s\n", Source, avail, Message);
\r
476 while( msglen > avail ) {
\r
480 SetCursorPos(giTerminal_Height-2, prefixlen);
\r
481 printf("%.*s\n", avail, Message);
\r
489 tWindow *Window_Create(tServer *Server, const char *Name)
\r
491 tWindow *ret, *prev = NULL;
\r
494 // Get the end of the list
\r
495 // TODO: Cache this instead
\r
496 for( ret = gpCurrentWindow; ret; prev = ret, ret = ret->Next )
\r
499 ret = malloc(sizeof(tWindow) + strlen(Name) + 1);
\r
500 ret->Messages = NULL;
\r
501 ret->Server = Server;
\r
502 ret->ActivityLevel = 1;
\r
503 strcpy(ret->Name, Name);
\r
506 ret->Next = prev->Next;
\r
509 else { // Shouldn't happen really
\r
510 ret->Next = gpWindows;
\r
514 // printf("Win %i %s:%s created\n", num, Server->Name, Name);
\r
519 void Redraw_Screen(void)
\r
524 printf("\x1B[2J"); // Clear screen
\r
525 printf("\x1B[0;0H"); // Reset cursor
\r
527 msg = gpCurrentWindow->Messages;
\r
529 // TODO: Title bar?
\r
531 // Note: This renders from the bottom up
\r
532 for( y = giTerminal_Height - 1; y -- && msg; msg = msg->Next)
\r
534 int msglen = strlen(msg->Data);
\r
535 int prefix_len = 3 + strlen(msg->Source);
\r
536 int line_avail = giTerminal_Width - prefix_len;
\r
537 int i = 0, done = 0;
\r
539 y -= msglen / line_avail; // Extra lines (y-- above handles the 1 line case)
\r
540 SetCursorPos(y, 0);
\r
541 printf("[%s] ", msg->Source);
\r
543 while(done < msglen) {
\r
544 done += printf("%.*s", line_avail, msg->Data+done);
\r
546 SetCursorPos(y+i, prefix_len);
\r
550 // Bottom line is rendered by the prompt
\r
553 void Cmd_PRIVMSG(tServer *Server, const char *Dest, const char *Src, const char *Message)
\r
555 Message_Append(Server, MSG_TYPE_STANDARD, Dest, Src, Message);
\r
556 //printf("<%s:%s:%s> %s\n", Server->Name, Dest, Src, Message);
\r
561 void ParseServerLine(tServer *Server, char *Line)
\r
566 // _SysDebug("[%s] %s", Server->Name, Line);
\r
572 ident = GetValue(Line, &pos); // Ident (user or server)
\r
573 cmd = GetValue(Line, &pos);
\r
576 if( isdigit(cmd[0]) && isdigit(cmd[1]) && isdigit(cmd[2]) )
\r
578 char *user, *message;
\r
580 num = (cmd[0] - '0') * 100;
\r
581 num += (cmd[1] - '0') * 10;
\r
582 num += (cmd[2] - '0') * 1;
\r
584 user = GetValue(Line, &pos);
\r
586 if( Line[pos] == ':' ) {
\r
587 message = Line + pos + 1;
\r
590 message = GetValue(Line, &pos);
\r
596 user = message; // Channel
\r
597 message = Line + pos + 1; // Topic
\r
598 Message_AppendF(Server, MSG_TYPE_SERVER, user, user, "Topic: %s", message);
\r
600 case 333: // Topic set by
\r
601 user = message; // Channel
\r
602 message = GetValue(Line, &pos); // User
\r
603 GetValue(Line, &pos); // Timestamp
\r
604 Message_AppendF(Server, MSG_TYPE_SERVER, user, user, "Set by %s", message);
\r
606 case 353: // /NAMES list
\r
607 // <user> = <channel> :list
\r
608 // '=' was eaten in and set to message
\r
609 user = GetValue(Line, &pos); // Actually channel
\r
610 message = Line + pos + 1; // List
\r
611 Message_AppendF(Server, MSG_TYPE_SERVER, user, user, "Names: %s", message);
\r
613 case 366: // end of /NAMES list
\r
614 // <user> <channel> :msg
\r
616 message = Line + pos + 1;
\r
617 Message_Append(Server, MSG_TYPE_SERVER, user, user, message);
\r
619 case 372: // MOTD Data
\r
620 case 375: // MOTD Start
\r
621 case 376: // MOTD End
\r
624 //printf("[%s] %i %s\n", Server->Name, num, message);
\r
625 Message_Append(Server, MSG_TYPE_SERVER, ident, user, message);
\r
629 else if( strcmp(cmd, "NOTICE") == 0 )
\r
631 char *class, *message;
\r
633 class = GetValue(Line, &pos);
\r
634 _SysDebug("NOTICE class='%s'", class);
\r
636 if( Line[pos] == ':' ) {
\r
637 message = Line + pos + 1;
\r
640 message = GetValue(Line, &pos);
\r
643 //printf("[%s] NOTICE %s: %s\n", Server->Name, ident, message);
\r
644 char *ident_bang = strchr(ident, '!');
\r
646 *ident_bang = '\0';
\r
648 Message_Append(Server, MSG_TYPE_NOTICE, ident, "", message);
\r
650 else if( strcmp(cmd, "PRIVMSG") == 0 )
\r
652 char *dest, *message;
\r
653 dest = GetValue(Line, &pos);
\r
655 if( Line[pos] == ':' ) {
\r
656 message = Line + pos + 1;
\r
659 message = GetValue(Line, &pos);
\r
662 // TODO: Catch when the privmsg is addressed to the user
\r
664 // Cmd_PRIVMSG(Server, dest, ident, message);
\r
665 char *ident_bang = strchr(ident, '!');
\r
667 *ident_bang = '\0';
\r
669 Message_Append(Server, MSG_TYPE_STANDARD, ident, dest, message);
\r
671 else if( strcmp(cmd, "JOIN" ) == 0 )
\r
674 channel = Line + pos + 1;
\r
675 Window_Create(Server, channel);
\r
679 Message_AppendF(Server, MSG_TYPE_SERVER, "", "", "Unknown message %s (%s)\n", cmd, Line+pos);
\r
683 cmd = GetValue(Line, &pos);
\r
685 if( strcmp(cmd, "PING") == 0 ) {
\r
686 writef(Server->FD, "PONG %s\n", gsHostname);
\r
690 // Command to client
\r
691 Message_AppendF(NULL, MSG_TYPE_UNK, "", "", "Client Command: %s", Line);
\r
697 * \brief Process incoming lines from the server
\r
699 int ProcessIncoming(tServer *Server)
\r
701 char *ptr, *newline;
\r
704 // While there is data in the buffer, read it into user memory and
\r
705 // process it line by line
\r
706 // ioctl#8 on a TCP client gets the number of bytes in the recieve buffer
\r
707 // - Used to avoid blocking
\r
709 while( (len = _SysIOCtl(Server->FD, 8, NULL)) > 0 )
\r
713 len = _SysRead(Server->FD, &Server->InBuf[Server->ReadPos], BUFSIZ - Server->ReadPos);
\r
717 Server->InBuf[Server->ReadPos + len] = '\0';
\r
719 // Break into lines
\r
720 ptr = Server->InBuf;
\r
721 while( (newline = strchr(ptr, '\n')) )
\r
724 if( newline[-1] == '\r' ) newline[-1] = '\0';
\r
725 ParseServerLine(Server, ptr);
\r
729 // Handle incomplete lines
\r
730 if( ptr - Server->InBuf < len + Server->ReadPos ) {
\r
731 // Update the read position
\r
732 // InBuf ReadPos ptr ReadPos+len
\r
733 // | old | new used | new unused |
\r
734 Server->ReadPos = len + Server->ReadPos - (ptr - Server->InBuf);
\r
735 // Copy stuff back (moving "new unused" to the start of the buffer)
\r
736 memcpy(Server->InBuf, ptr, Server->ReadPos);
\r
739 Server->ReadPos = 0;
\r
749 * \brief Write a formatted string to a file descriptor
\r
752 int writef(int FD, const char *Format, ...)
\r
757 va_start(args, Format);
\r
758 len = vsnprintf(NULL, 0, Format, args);
\r
763 va_start(args, Format);
\r
764 vsnprintf(buf, len+1, Format, args);
\r
767 return _SysWrite(FD, buf, len);
\r
772 * \brief Initialise a TCP connection to \a AddressString on port \a PortNumber
\r
774 int OpenTCP(const char *AddressString, short PortNumber)
\r
777 char addrBuffer[8];
\r
779 // Parse IP Address
\r
780 addrType = Net_ParseAddress(AddressString, addrBuffer);
\r
781 if( addrType == 0 ) {
\r
782 fprintf(stderr, "Unable to parse '%s' as an IP address\n", AddressString);
\r
783 _SysDebug("Unable to parse '%s' as an IP address\n", AddressString);
\r
787 // Finds the interface for the destination address
\r
788 fd = Net_OpenSocket(addrType, addrBuffer, "tcpc");
\r
790 fprintf(stderr, "Unable to open TCP Client to '%s'\n", AddressString);
\r
791 _SysDebug("Unable to open TCP client to '%s'\n", AddressString);
\r
795 // Set remote port and address
\r
796 // printf("Setting port and remote address\n");
\r
797 _SysIOCtl(fd, 5, &PortNumber);
\r
798 _SysIOCtl(fd, 6, addrBuffer);
\r
801 // printf("Initiating connection\n");
\r
802 if( _SysIOCtl(fd, 7, NULL) == 0 ) {
\r
803 // Shouldn't happen :(
\r
804 fprintf(stderr, "Unable to start connection\n");
\r
808 // Return descriptor
\r
813 * \brief Read a space-separated value from a string
\r
815 char *GetValue(char *Src, int *Ofs)
\r
818 char *ret = Src + pos;
\r
821 if( !Src ) return NULL;
\r
823 while( *ret == ' ' ) ret ++;
\r
825 end = strchr(ret, ' ');
\r
830 end = ret + strlen(ret) - 1;
\r
834 while( *ret == ' ' ) end ++;
\r
840 void SetCursorPos(int Row, int Col)
\r
842 printf("\x1B[%i;%iH", Col, Row);
\r
845 static inline int isdigit(int ch)
\r
847 return '0' <= ch && ch < '9';
\r