Mercurial > hg > nnchat
comparison nnchat.c @ 125:d03ebefb92a6
Improve internal command handling, use fork() and execlp() instead of system() for opening profiles into external browser's tabs.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 29 Oct 2010 16:59:35 +0300 |
parents | 0a07138e75bc |
children | 713879a7ca10 |
comparison
equal
deleted
inserted
replaced
124:fe4d5f3b486c | 125:d03ebefb92a6 |
---|---|
541 } | 541 } |
542 | 542 |
543 return 1; | 543 return 1; |
544 } | 544 } |
545 | 545 |
546 char * trimLeft(char *buf) | |
547 { | |
548 while (*buf != 0 && isspace(*buf)) buf++; | |
549 return buf; | |
550 } | |
546 | 551 |
547 int handleUserInput(const int sock, char *buf, size_t bufLen) | 552 int handleUserInput(const int sock, char *buf, size_t bufLen) |
548 { | 553 { |
549 char *tmpStr, tmpBuf[4096]; | 554 char *tmpStr, tmpBuf[4096]; |
550 BOOL result; | 555 BOOL result; |
559 if (*buf == 0) { | 564 if (*buf == 0) { |
560 return 1; | 565 return 1; |
561 } else if (!strncasecmp(buf, "/color ", 7)) { | 566 } else if (!strncasecmp(buf, "/color ", 7)) { |
562 /* Change color */ | 567 /* Change color */ |
563 int tmpInt; | 568 int tmpInt; |
564 if ((tmpInt = getHexColorDef(buf+7)) < 0) { | 569 if ((tmpInt = getHexColorDef(trimLeft(buf + 7))) < 0) { |
565 printMsg("Invalid color value '%s'\n", buf+7); | 570 printMsg("Invalid color value '%s'\n", buf+7); |
566 return 1; | 571 return 1; |
567 } | 572 } |
568 optUserColor = tmpInt; | 573 optUserColor = tmpInt; |
569 printMsg("Setting color to #%06x\n", optUserColor); | 574 printMsg("Setting color to #%06x\n", optUserColor); |
570 nn_send_msg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); | 575 nn_send_msg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); |
571 return 0; | 576 return 0; |
572 } else if (!strncasecmp(buf, "/w ", 3)) { | 577 } else if (!strncasecmp(buf, "/w ", 3)) { |
573 /* Open given username's profile via firefox in a new tab */ | 578 /* Open given username's profile via firefox in a new tab */ |
574 char *name = buf + 3, *browser = getenv("BROWSER"); | 579 char *name = trimLeft(buf + 3), |
575 while (*name && isspace(*name)) name++; | 580 *browser = getenv("BROWSER"); |
581 pid_t pid; | |
576 | 582 |
577 if (browser == NULL) | 583 if (browser == NULL) |
578 browser = "firefox"; | 584 browser = "firefox"; |
579 | 585 |
586 printMsg("Opening profile for: '%s'\n", name); | |
587 | |
580 tmpStr = nn_encode_str1(name); | 588 tmpStr = nn_encode_str1(name); |
581 snprintf(tmpBuf, sizeof(tmpBuf), "%s -remote \"openurl(http://www.newbienudes.com/profile/%s/,new-tab)\"", | 589 snprintf(tmpBuf, sizeof(tmpBuf), "openurl(http://www.newbienudes.com/profile/%s/,new-tab)", tmpStr); |
582 browser, tmpStr); | |
583 th_free(tmpStr); | 590 th_free(tmpStr); |
584 | 591 |
585 printMsg("Opening profile for: '%s'\n", name); | 592 if ((pid = fork()) < 0) { |
586 printMsg("%s\n", tmpBuf); | 593 printMsg("Could not create sub-process!\n"); |
587 system(tmpBuf); | 594 } else if (pid == 0) { |
595 execlp(browser, browser, "-remote", tmpBuf, NULL); | |
596 exit(errno); | |
597 } | |
588 return 0; | 598 return 0; |
589 } else if (!strncasecmp(buf, "/to ", 4)) { | 599 } else if (!strncasecmp(buf, "/to ", 4)) { |
590 /* Set private messaging target */ | 600 /* Set private messaging target */ |
591 th_free(setTarget); | 601 th_free(setTarget); |
592 setTarget = th_strdup(buf + 4); | 602 setTarget = th_strdup(trimLeft(buf + 4)); |
593 printMsg("Set prv target to '%s'\n", setTarget); | 603 printMsg("Set prv target to '%s'\n", setTarget); |
594 return 0; | 604 return 0; |
595 } else if (!strncasecmp(buf, "/who", 4)) { | 605 } else if (!strncasecmp(buf, "/who", 4)) { |
596 /* Alias /who to /listallusers */ | 606 /* Alias /who to /listallusers */ |
597 snprintf(tmpBuf, sizeof(tmpBuf), "/listallusers"); | 607 snprintf(tmpBuf, sizeof(tmpBuf), "/listallusers"); |