-
-
Notifications
You must be signed in to change notification settings - Fork 32
Allow the option of parsing multiple post-connection commands. #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks for the PR and your kind words =). Regarding the scope of this PR, I can definitely understand the need for passing multiple initial commands. With the current PR implementation, however, there may be instances where more than the default of 5 (or however many are specified) initial commands would need to be specified and I would hate to impose a set limit. Of course, the user could change this value their liking and rebuild kirc, but i know from experience that most users are just installing from existing distribution repositories. Using a delimiter character would also work, but it means that the delimited char can no longer be used as part of a startup command (e.g. what. if I wanted to send a PM containing that delimiting char?). The only safe delimited char, imho, to use is '\n', but that is hard to use unless you are dealing with a text editor. With that said, I wonder if it would make more sense to read from STDIN? Here are three alternative proposals/solutions:
As an example for Option 3, a single startup command would look like this:
Then using a startup command script:
Personally, I am in favor of Option 3. Per usual, I am open to any other proposals or counter arguments. |
I did consider the implication of having a delimiter be a part of the start up command string and so also considered the possibility of an escape character; but I don't want to offload extra information to the user if not truly necessary. However, I do like the idea of reading from STDIN when the -x option is specified. As it turns out, when redirecting to STDIN the call to With this change even if the -x option is not specified but a user attempts to redirect to STDIN, the program will not break during the call to When That covers the two changes introduced in the recent commits. My main concern is the same as yours: breaking users' start up scripts who were utilizing -x "". Before introducing the change in these commits I tried my best to keep the same functionality as before in allowing an argument to -x but also reading from STDIN. Ultimately I ran into an issue with the string argument to the -x option being ignored when STDIN is redirected to. |
Correction: Regardless of what we choose, luckily we can use |
As an example in case you wanted to get a visual, the delimiter suggestion would look something like this - criscanedo@f8db275 It is a working copy so you can pull it and test it out. |
Maybe i am overlooking something, but couldn't we simplify this significantly by capturing STDIN to a buffer before we enable raw mode? For example, the following could be declared before // declared at the beginning, along with
// #define CBUF_SIZ 1024
// static char cbuf[1024];
// static int cmds = 0;
if (cmds > 0) {
int flag = 0;
// read STDIN until CBUF_SIZ reached or error -1 returned
for (int i = 0; (i < CBUF_SIZ) && (flag > -1); i++) {
flag = read(STDIN_FILENO, &cbuf[i], 1);
}
} We could then use a tokanizer to split the buffer at the '\n' chars and write each command char after // see the messageWrap() function as an example
if (cmds > 0) {
for (char *tok = strtok(cbuf, "\n"); tok != NULL; tok = strtok(NULL, "\n")) {
raw("%s\r\n", tok);
}
} |
Indeed that is what I tried initially and is what led me to discover the file descriptor STDIN_FILENO will no longer be associated with the terminal if STDIN is redirected to. That means the program is no longer able to read input from the terminal through STDIN_FILENO and was the reason behind creating a file descriptor for the associated terminal of the process to read input regardless of that happening: static void opentty()
{
if ((ttyinfd = open("/dev/tty", 0)) == -1) {
perror("failed to open /dev/tty");
exit(1);
}
} Additionally because STDIN_FILENO is no longer associated to a terminal with redirected STDIN, the following call to isatty() will cause the application to exit: static int enableRawMode(int fd) {
if (!isatty(STDIN_FILENO)) {
goto fatal;
}
...
} Though using the file descriptor for the terminal to read input ( The call to isatty() in readcmds() was to check if STDIN had buffered input and only then read from STDIN so as to prevent the program from waiting for user input if -x is specified with no input: static void readcmds(char *cmd) {
if (!isatty(STDIN_FILENO)) {
...
}
...
} Your changes certainly cover the required functionality so I'll go ahead and incorporate them to the branch and push it up shortly. Would you still want to consider an argument to -x if any? |
Including this into your change you should now be able to send commands read from both STDIN and as an argument: if (cmds > 0) {
for (char *tok = strtok(cbuf, "\n"); tok; tok = strtok(NULL, "\n")) {
raw("%s\r\n", tok);
}
if (inic) {
raw("%s\r\n", inic);
}
} |
@criscanedo the latest commit looks good. I will do some testing in a few hours and then merge (+version bump and new release tarballs shortly after). |
Awesome, thanks. Likewise I will also continue testing and will be on stand by if anything comes up. |
Since kirc is my IRC client of choice I have a shell script to start a connection from any shell. I run an IRC channel and thus run identify with ChanServ in addition to identify with NickServ upon connection. It didn't look like there was a way for kirc to accept multiple post-connection commands so I added a small bit of code to allow that type of functionality at the most basic level for myself. Perhaps this could also be useful to future kirc users.
Using my shell script as an example, this change would allow the 2 -x options below to be processed:
kirc -s irc.dal.net -p 6667 -o $logfile -n $IRCNICK -x "$identnick" -x "$identchan"
Alternatively, we could allow for multiple commands to be processed using one -x option instead by specifying a delimiter thus resulting in processing each token in just one string.
Let me know what you all think. Also, great work on kirc. To me it's an absolutely phenomenal client. It didn't take long for me to choose kirc over a handful of other clients that I was simultaneously comparing the other day.