Applications/IRC - Cleaning up command code
authorJohn Hodge <[email protected]>
Tue, 14 May 2013 03:38:47 +0000 (11:38 +0800)
committerJohn Hodge <[email protected]>
Tue, 14 May 2013 03:38:47 +0000 (11:38 +0800)
Usermode/Applications/irc_src/main.c

index 4ca2415..b96fcc9 100644 (file)
@@ -207,6 +207,79 @@ int ParseArguments(int argc, const char *argv[])
        return 0;\r
 }\r
 \r
+\r
+void Cmd_join(char *ArgString)\r
+{\r
+        int    pos=0;\r
+       char    *channel_name = GetValue(ArgString, &pos);\r
+       \r
+       if( gpCurrentWindow->Server )\r
+       {\r
+               writef(gpCurrentWindow->Server->FD, "JOIN :%s\n", channel_name);\r
+       }\r
+}\r
+\r
+void Cmd_quit(char *ArgString)\r
+{\r
+       const char *quit_message = ArgString;\r
+       if( quit_message == NULL || quit_message[0] == '\0' )\r
+               quit_message = "/quit - Acess2 IRC Client";\r
+       \r
+       for( tServer *srv = gpServers; srv; srv = srv->Next )\r
+       {\r
+               writef(srv->FD, "QUIT :%s\n", quit_message);\r
+       }\r
+       \r
+       exit(0);\r
+}\r
+\r
+void Cmd_window(char *ArgString)\r
+{\r
+        int    pos = 0;\r
+       char    *window_id = GetValue(ArgString, &pos);\r
+        int    window_num = atoi(window_id);\r
+       \r
+       if( window_num > 0 )\r
+       {\r
+               tWindow *win;\r
+               window_num --;  // Move to base 0\r
+               // Get `window_num`th window\r
+               for( win = gpWindows; win && window_num--; win = win->Next );\r
+               if( win ) {\r
+                       gpCurrentWindow = win;\r
+                       Redraw_Screen();\r
+               }\r
+               // Otherwise, silently ignore\r
+       }\r
+       else\r
+       {\r
+               window_num = 1;\r
+               for( tWindow *win = gpWindows; win; win = win->Next, window_num ++ )\r
+               {\r
+                       if( win->Name[0] ) {\r
+                               Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "",\r
+                                       "%i: %s/%s", window_num, win->Server->Name, win->Name);\r
+                       }\r
+                       else {\r
+                               Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "",\r
+                                       "%i: (status)", window_num);\r
+                       }\r
+               }\r
+       }\r
+}\r
+\r
+const struct {\r
+       const char *Name;\r
+       void    (*Fcn)(char *ArgString);\r
+} caCommands[] = {\r
+       {"join", Cmd_join},\r
+       {"quit", Cmd_quit},\r
+       {"window", Cmd_window},\r
+       {"win",    Cmd_window},\r
+       {"w",      Cmd_window},\r
+};\r
+const int ciNumCommands = sizeof(caCommands)/sizeof(caCommands[0]);\r
+\r
 /**\r
  * \brief Handle a line from the prompt\r
  */\r
@@ -217,65 +290,19 @@ int ParseUserCommand(char *String)
                char    *command;\r
                 int    pos = 0;\r
                \r
-               command = GetValue(String, &pos);\r
-               \r
-               if( strcmp(command, "/join") == 0 )\r
-               {\r
-                       char    *channel_name = GetValue(String, &pos);\r
-                       \r
-                       if( gpCurrentWindow->Server )\r
-                       {\r
-                               writef(gpCurrentWindow->Server->FD, "JOIN :%s\n",  channel_name);\r
-                       }\r
-               }\r
-               else if( strcmp(command, "/quit") == 0 )\r
+               command = GetValue(String, &pos)+1;\r
+\r
+               // TODO: Prefix matches\r
+                int    cmdIdx = -1;\r
+               for( int i = 0; i < ciNumCommands; i ++ )\r
                {\r
-                       char    *quit_message = String + pos;\r
-                       tServer *srv;\r
-                       \r
-                       if( quit_message == NULL || quit_message[0] == '\0' )\r
-                               quit_message = "/quit - Acess2 IRC Client";\r
-                       \r
-                       for( srv = gpServers; srv; srv = srv->Next )\r
-                       {\r
-                               writef(srv->FD, "QUIT :%s\n", quit_message);\r
+                       if( strcmp(command, caCommands[i].Name) == 0 ) {\r
+                               cmdIdx = i;\r
+                               break;\r
                        }\r
-                       \r
-                       exit(0);\r
                }\r
-               else if( strcmp(command, "/window") == 0 || strcmp(command, "/win") == 0 || strcmp(command, "/w") == 0 )\r
-               {\r
-                       char    *window_id = GetValue(String, &pos);\r
-                        int    window_num = atoi(window_id);\r
-                       \r
-                       if( window_num > 0 )\r
-                       {\r
-                               tWindow *win;\r
-                               window_num --;  // Move to base 0\r
-                               // Get `window_num`th window\r
-                               for( win = gpWindows; win && window_num--; win = win->Next );\r
-                               if( win ) {\r
-                                       gpCurrentWindow = win;\r
-                                       Redraw_Screen();\r
-                               }\r
-                               // Otherwise, silently ignore\r
-                       }\r
-                       else\r
-                       {\r
-                               tWindow *win;\r
-                               window_num = 1;\r
-                               for( win = gpWindows; win; win = win->Next, window_num ++ )\r
-                               {\r
-                                       if( win->Name[0] ) {\r
-                                               Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "",\r
-                                                       "%i: %s/%s", window_num, win->Server->Name, win->Name);\r
-                                       }\r
-                                       else {\r
-                                               Message_AppendF(NULL, MSG_TYPE_SERVER, "client", "",\r
-                                                       "%i: (status)", window_num);\r
-                                       }\r
-                               }\r
-                       }\r
+               if( cmdIdx != -1 ) {\r
+                       caCommands[cmdIdx].Fcn(String+pos);\r
                }\r
                else\r
                {\r

UCC git Repository :: git.ucc.asn.au