Changeset 176b6ef in code
- Timestamp:
- Apr 22, 2015, 5:28:54 PM (10 years ago)
- Branches:
- master
- Children:
- 7ff6405
- Parents:
- 677a45b
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
icb.c
r677a45b r176b6ef 38 38 int icb_login(struct icb_session *, char *, char *, char *); 39 39 int icb_dowho(struct icb_session *, struct icb_group *); 40 char *icb_nextfield(char **, int);41 40 42 41 /* … … 76 75 { 77 76 char *msg = is->buffer; 77 int msglen = is->length; 78 78 unsigned char type; 79 char *wptr = NULL; 79 80 int res = 0; 80 81 … … 88 89 switch (type) { 89 90 case ICB_M_LOGIN: { 90 char *nick, *group, *client, *cmd; 91 92 client = icb_nextfield(&msg, 1); 93 nick = icb_nextfield(&msg, 1); 94 group = icb_nextfield(&msg, 1); 95 cmd = icb_nextfield(&msg, 1); 91 char client[ICB_MAXNICKLEN]; 92 char nick[ICB_MAXNICKLEN]; 93 char group[ICB_MAXGRPLEN]; 94 char cmd[ICB_MAXCMDLEN]; 95 96 if (icb_token(msg, msglen, &wptr, client, ICB_MAXNICKLEN, 97 ICB_M_SEP, 1) < 0) { 98 icb_error(is, "Invalid client"); 99 icbd_drop(is, NULL); 100 return (1); 101 } 102 if (icb_token(msg, msglen, &wptr, nick, ICB_MAXNICKLEN, 103 ICB_M_SEP, 1) <= 0) { 104 icb_error(is, "Invalid nick"); 105 icbd_drop(is, NULL); 106 return (1); 107 } 108 if (icb_token(msg, msglen, &wptr, group, ICB_MAXGRPLEN, 109 ICB_M_SEP, 1) < 0) { 110 icb_error(is, "Invalid group"); 111 icbd_drop(is, NULL); 112 return (1); 113 } 114 if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN, 115 ICB_M_SEP, 1) < 0) { 116 icb_error(is, "Invalid command"); 117 icbd_drop(is, NULL); 118 return (1); 119 } 96 120 if (strlen(cmd) > 0 && cmd[0] == 'w') { 97 121 icb_error(is, "Command not implemented"); … … 108 132 } 109 133 case ICB_M_OPEN: { 110 char *grpmsg; 111 112 grpmsg = icb_nextfield(&msg, 0); 113 icb_groupmsg(is, grpmsg); 134 icb_groupmsg(is, msg); 114 135 break; 115 136 } 116 137 case ICB_M_COMMAND: { 117 char *cmd, *arg; 118 119 cmd = icb_nextfield(&msg, 1); 120 arg = icb_nextfield(&msg, 0); 138 char cmd[ICB_MAXCMDLEN]; 139 char arg[ICB_MAXTOPICLEN]; 140 141 if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN, 142 ICB_M_SEP, 1) <= 0) { 143 icb_error(is, "Invalid command"); 144 icbd_drop(is, NULL); 145 return (1); 146 } 147 if (icb_token(msg, msglen, &wptr, arg, ICB_MAXTOPICLEN, 148 ICB_M_SEP, 1) < 0) { 149 icb_error(is, "Invalid argument"); 150 icbd_drop(is, NULL); 151 return (1); 152 } 121 153 icb_command(is, cmd, arg); 122 154 break; … … 286 318 { 287 319 void (*handler)(struct icb_session *, char *); 288 char command[ 32]; /* XXX */320 char command[ICB_MAXCMDLEN]; 289 321 290 322 icb_vis(command, cmd, sizeof command, VIS_SP); … … 586 618 587 619 /* 588 * icb_nextfield: advances through a given buffer returning pointer to the589 * beginning of the icb field or an empty string otherwise;590 * cleans up trailing spaces591 */592 char *593 icb_nextfield(char **buf, int notrspace)594 {595 char *start = *buf;596 char *end = NULL;597 598 while (*buf && **buf != '\0' && **buf != ICB_M_SEP)599 (*buf)++;600 if (*buf && **buf == ICB_M_SEP) {601 **buf = '\0';602 (*buf)++;603 }604 end = *buf;605 while (notrspace && end && end > start && *(--end) == ' ')606 *end = '\0';607 return (start);608 }609 610 /*611 620 * icb_sendfmt: formats a string and sends it over 612 621 */ … … 632 641 */ 633 642 int 634 icb_token(char *buf, int len, char **bufptr, char *dst, int dlen, int sep) 643 icb_token(char *buf, int len, char **bufptr, char *dst, int dstlen, int sep, 644 int trim) 635 645 { 636 646 char *start; 637 647 int i, ret; 638 648 639 if (buf == NULL || len <= 0 || d len <= 0)649 if (buf == NULL || len <= 0 || dst == NULL || dstlen <= 0) 640 650 return (0); 641 651 if (*bufptr == NULL) … … 646 656 /* copy and null terminate the token */ 647 657 ret = strlcpy(dst, start, 648 MIN(*bufptr - start + 1, d len));658 MIN(*bufptr - start + 1, dstlen)); 649 659 if (**bufptr != '\0') 650 660 (*bufptr)++; 661 if (ret > 0 && trim) 662 ret = icb_trim(dst, dstlen); 651 663 return (ret); 652 664 } … … 658 670 */ 659 671 if (*bufptr - start > 0) { 660 ret = strlcpy(dst, start, MIN(*bufptr - start + 1, dlen)); 672 ret = strlcpy(dst, start, MIN(*bufptr - start + 1, dstlen)); 673 if (ret > 0 && trim) 674 ret = icb_trim(dst, dstlen); 661 675 return (ret); 662 676 } 663 677 return (0); 678 } 679 680 /* 681 * icb_trim: trims trailing whitespace 682 */ 683 int 684 icb_trim(char *buf, int len) 685 { 686 char *p = buf; 687 int i; 688 689 for (i = 0; i < len && *p != '\0'; i++) 690 p++; 691 if (*p == '\0' && p - buf > 0) 692 p--; 693 while (p >= buf && isspace(*p)) { 694 *p = '\0'; 695 i--; 696 if (p > buf) 697 p--; 698 } 699 return (i); 664 700 } 665 701 -
icb.h
r677a45b r176b6ef 30 30 #define ICB_MAXTOPICLEN 160 31 31 #define ICB_MAXHOSTLEN 40 32 #define ICB_MAXCMDLEN 32 32 33 #define ICB_MTABLEN 50 /* XXX */ 33 34 … … 141 142 int, const char *, ...); 142 143 void icb_who(struct icb_session *, struct icb_group *); 143 int icb_token(char *, int, char **, char *, int, int); 144 int icb_token(char *, int, char **, char *, int, int, int); 145 int icb_trim(char *, int); 144 146 int icb_vis(char *, const char *, size_t, int); -
icbd.c
r677a45b r176b6ef 130 130 if (grplist) { 131 131 while (icb_token(grplist, strlen(grplist), &ptr, group, 132 ICB_MAXGRPLEN, ',' ) > 0)132 ICB_MAXGRPLEN, ',', 0) > 0) 133 133 if (icb_addgroup(NULL, group) == NULL) 134 134 err(EX_UNAVAILABLE, NULL);
Note:
See TracChangeset
for help on using the changeset viewer.