diff options
-rw-r--r-- | rtclient/ticket.h | 31 | ||||
-rw-r--r-- | ticket.c | 383 |
2 files changed, 211 insertions, 203 deletions
diff --git a/rtclient/ticket.h b/rtclient/ticket.h index 7677b09..7d6f9ea 100644 --- a/rtclient/ticket.h +++ b/rtclient/ticket.h @@ -10,18 +10,23 @@ struct rtclient_ticket { enum rtclient_ticket_history_type { RTCLIENT_TICKET_HISTORY_TYPE_NONE = 0 + , RTCLIENT_TICKET_HISTORY_TYPE_ADD_LINK + , RTCLIENT_TICKET_HISTORY_TYPE_ADD_MEMBER + , RTCLIENT_TICKET_HISTORY_TYPE_ADD_MEMBERSHIP + , RTCLIENT_TICKET_HISTORY_TYPE_ADD_REMINDER + , RTCLIENT_TICKET_HISTORY_TYPE_ADD_WATCHER + , RTCLIENT_TICKET_HISTORY_TYPE_COMMENT + , RTCLIENT_TICKET_HISTORY_TYPE_COMMENT_EMAIL_RECORD + , RTCLIENT_TICKET_HISTORY_TYPE_CORRESPOND , RTCLIENT_TICKET_HISTORY_TYPE_CREATE + , RTCLIENT_TICKET_HISTORY_TYPE_DEL_WATCHER + , RTCLIENT_TICKET_HISTORY_TYPE_DISABLED , RTCLIENT_TICKET_HISTORY_TYPE_EMAIL_RECORD + , RTCLIENT_TICKET_HISTORY_TYPE_RESOLVE_REMINDER , RTCLIENT_TICKET_HISTORY_TYPE_SET , RTCLIENT_TICKET_HISTORY_TYPE_SET_WATCHER , RTCLIENT_TICKET_HISTORY_TYPE_STATUS -}; - -enum rtclient_ticket_history_field { - RTCLIENT_TICKET_HISTORY_FIELD_NONE = 0 - , RTCLIENT_TICKET_HISTORY_FIELD_PRIORITY - , RTCLIENT_TICKET_HISTORY_FIELD_STATUS - , RTCLIENT_TICKET_HISTORY_FIELD_OWNER + , RTCLIENT_TICKET_HISTORY_TYPE_SYSTEM_ERROR }; struct rtclient_ticket_history_attachment { @@ -30,12 +35,17 @@ struct rtclient_ticket_history_attachment { size_t size; }; +struct rtclient_ticket_history_attachment_list { + size_t length; + struct rtclient_ticket_history_attachment *attachments[]; +}; + struct rtclient_ticket_history { unsigned int id; unsigned int ticket; unsigned int time_taken; enum rtclient_ticket_history_type type; - enum rtclient_ticket_history_field field; + char *field; char *old_value; char *new_value; char *data; @@ -43,10 +53,7 @@ struct rtclient_ticket_history { char *content; char *creator; struct tm *created; - struct { - size_t length; - struct rtclient_ticket_history_attachment *attachments[]; - } *attachments; + struct rtclient_ticket_history_attachment_list *attachments; }; struct rtclient_ticket_history_list { @@ -5,6 +5,8 @@ typedef struct rtclient_ticket rtclient_ticket; typedef struct rtclient_ticket_history_attachment rtclient_ticket_history_attachment; typedef struct rtclient_ticket_history_list rtclient_ticket_history_list; +typedef struct rtclient_ticket_history_attachment rtclient_ticket_history_attachment; +typedef struct rtclient_ticket_history_attachment_list rtclient_ticket_history_attachment_list; void rtclient_ticket_new(const char *queue , const char *requestor @@ -78,7 +80,7 @@ static size_t history_handler(void *contents, size_t size, size_t nmemb ticket_history->ticket = 0; ticket_history->time_taken = 0; ticket_history->type = RTCLIENT_TICKET_HISTORY_TYPE_NONE; - ticket_history->field = RTCLIENT_TICKET_HISTORY_FIELD_NONE; + ticket_history->field = NULL; ticket_history->old_value = NULL; ticket_history->new_value = NULL; ticket_history->data = NULL; @@ -90,7 +92,10 @@ static size_t history_handler(void *contents, size_t size, size_t nmemb ticket_history->content = NULL; ticket_history->creator = NULL; ticket_history->created = NULL; - ticket_history->attachments = NULL; + ticket_history->attachments + = malloc + (sizeof(rtclient_ticket_history_attachment_list)); + ticket_history->attachments->length = 0; } } else { free(*listptr); @@ -115,204 +120,201 @@ static size_t history_l_handler(void *contents, size_t size, size_t nmemb char response[realsize + 1]; memcpy(response, contents, realsize); response[realsize] = '\0'; - char histories[strlen(response) + 1]; - strcpy(histories, response); rtclient_ticket_history_list **listptr = (rtclient_ticket_history_list **)writedata; + rtclient_ticket_history_list *list = *listptr; char *linesaveptr = NULL; char *line = strtok_r(response, "\n", &linesaveptr); if (strstr(line, "200 Ok")) { - line = strtok_r(NULL, "\n", &linesaveptr); - char *hashsaveptr = NULL; - char *hash = strtok_r(line, " ", &hashsaveptr); - hash = strtok_r(NULL, " ", &hashsaveptr); - char *length = strtok(hash, "/"); - (*listptr)->length = atoi(length); - rtclient_ticket_history_list *ptr = realloc(*listptr - , sizeof(*listptr) - + (*listptr)->length - * sizeof(struct rtclient_ticket_history)); - *listptr = ptr; - rtclient_ticket_history_list *list = *listptr; - - char *historysaveptr = NULL; - char *history = strtok_r(histories, "#", &historysaveptr); - history = strtok_r(NULL, "#", &historysaveptr); - char *linesaveptr = NULL, *line = NULL; char *tokensaveptr = NULL, *token = NULL; for (size_t i = 0; i < list->length; i++) { - list->histories[i] - = malloc(sizeof(struct rtclient_ticket_history)); struct rtclient_ticket_history *ticket_history = list->histories[i]; - line = strtok_r(history, "\n", &linesaveptr); + strtok_r(NULL, "\n", &linesaveptr); + line = strtok_r(NULL, "\n", &linesaveptr); + line = strtok_r(NULL, "\n", &linesaveptr); + + strtok_r(line, ":", &tokensaveptr); + token = strtok_r(NULL, ":", &tokensaveptr); + ticket_history->ticket = atoi(++token); + line = strtok_r(NULL, "\n", &linesaveptr); + + strtok_r(line, ":", &tokensaveptr); + token = strtok_r(NULL, ":", &tokensaveptr); + ticket_history->time_taken = atoi(++token); + line = strtok_r(NULL, "\n", &linesaveptr); + + strtok_r(line, ":", &tokensaveptr); + token = strtok_r(NULL, ":", &tokensaveptr); + if (!strcmp(++token, "AddLink")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_ADD_LINK; + else if (!strcmp(token, "AddMember")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_ADD_MEMBER; + else if (!strcmp(token, "AddMembership")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_ADD_MEMBERSHIP; + else if (!strcmp(token, "AddReminder")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_ADD_REMINDER; + else if (!strcmp(token, "AddWatcher")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_ADD_WATCHER; + else if (!strcmp(token, "Comment")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_COMMENT; + else if (!strcmp(token, "CommentEmailRecord")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_COMMENT_EMAIL_RECORD; + else if (!strcmp(token, "Correspond")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_CORRESPOND; + else if (!strcmp(token, "Create")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_CREATE; + else if (!strcmp(token, "DelWatcher")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_DEL_WATCHER; + else if (!strcmp(token, "Disabled")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_DISABLED; + else if (!strcmp(token, "EmailRecord")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_EMAIL_RECORD; + else if (!strcmp(token, "ResolveReminder")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_RESOLVE_REMINDER; + else if (!strcmp(token, "Set")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_SET; + else if (!strcmp(token, "SetWatcher")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_SET_WATCHER; + else if (!strcmp(token, "Status")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_STATUS; + else if (!strcmp(token, "SystemError")) + ticket_history->type + = RTCLIENT_TICKET_HISTORY_TYPE_SYSTEM_ERROR; + line = strtok_r(NULL, "\n", &linesaveptr); + + strtok_r(line, ":", &tokensaveptr); + token = strtok_r(NULL, ":", &tokensaveptr); + if (token && strcmp(token, "")) { + ticket_history->field = malloc(strlen(token)); + strcpy(ticket_history->field, ++token); + } + line = strtok_r(NULL, "\n", &linesaveptr); + + strtok_r(line, ":", &tokensaveptr); + token = strtok_r(NULL, ":", &tokensaveptr); + if (token && strcmp(token, "")) { + ticket_history->old_value = malloc(strlen(token)); + strcpy(ticket_history->old_value, ++token); + } + line = strtok_r(NULL, "\n", &linesaveptr); + + strtok_r(line, ":", &tokensaveptr); + token = strtok_r(NULL, ":", &tokensaveptr); + if (token && strcmp(token, "")) { + ticket_history->new_value = malloc(strlen(token)); + strcpy(ticket_history->new_value, ++token); + } + line = strtok_r(NULL, "\n", &linesaveptr); + + strtok_r(line, ":", &tokensaveptr); + token = strtok_r(NULL, ":", &tokensaveptr); + if (token && strcmp(token, "")) { + ticket_history->data = malloc(strlen(token)); + strcpy(ticket_history->data, ++token); + } + line = strtok_r(NULL, "\n", &linesaveptr); + + strtok_r(line, ":", &tokensaveptr); + token = strtok_r(NULL, ":", &tokensaveptr); + ticket_history->description = malloc(strlen(token)); + strcpy(ticket_history->description, ++token); + line = strtok_r(NULL, "\n", &linesaveptr); + + strtok_r(line, ":", &tokensaveptr); + token = strtok_r(NULL, ":", &tokensaveptr); + ticket_history->content = malloc(strlen(token)); + strcpy(ticket_history->content, ++token); + line = strtok_r(NULL, "\n", &linesaveptr); + while (strncmp(line, "Creator", 7)) { + char *ptr = realloc(ticket_history->content + , strlen(ticket_history->content) + + strlen(line) + 2); + ticket_history->content = ptr; + sprintf(ticket_history->content, "%s\n%s" + , ticket_history->content, line); + line = strtok_r(NULL, "\n", &linesaveptr); + } + + strtok_r(line, ":", &tokensaveptr); + token = strtok_r(NULL, ":", &tokensaveptr); + ticket_history->creator = malloc(strlen(token)); + strcpy(ticket_history->creator, ++token); + line = strtok_r(NULL, "\n", &linesaveptr); + + strtok_r(line, ": -", &tokensaveptr); + ticket_history->created = malloc(sizeof(struct tm)); + ticket_history->created->tm_wday = 0; + ticket_history->created->tm_yday = 0; + ticket_history->created->tm_isdst = -1; + token = strtok_r(NULL, ": -", &tokensaveptr); + ticket_history->created->tm_year = atoi(token) - 1900; + token = strtok_r(NULL, ": -", &tokensaveptr); + ticket_history->created->tm_mon = atoi(token) - 1; + token = strtok_r(NULL, ": -", &tokensaveptr); + ticket_history->created->tm_mday = atoi(token); + token = strtok_r(NULL, ": -", &tokensaveptr); + ticket_history->created->tm_hour = atoi(token); + token = strtok_r(NULL, ": -", &tokensaveptr); + ticket_history->created->tm_min = atoi(token); + token = strtok_r(NULL, ": -", &tokensaveptr); + ticket_history->created->tm_sec = atoi(token); line = strtok_r(NULL, "\n", &linesaveptr); - do { - token = strtok_r(line, ":", &tokensaveptr); - if (!strcmp(token, "id")) { - token = strtok_r(NULL, ":", &tokensaveptr); - ticket_history->id = atoi(++token); - } else if (!strcmp(token, "Ticket")) { - token = strtok_r(NULL, ":", &tokensaveptr); - ticket_history->ticket = atoi(++token); - } else if (!strcmp(token, "TimeTaken")) { - token = strtok_r(NULL, ":", &tokensaveptr); - ticket_history->time_taken = atoi(++token); - } else if (!strcmp(token, "Type")) { - token = strtok_r(NULL, ":", &tokensaveptr); - if (!strcmp(++token, "Create")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_CREATE; - else if (!strcmp(token, "EmailRecord")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_EMAIL_RECORD; - else if (!strcmp(token, "Set")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_SET; - else if (!strcmp(token, "SetWatcher")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_SET_WATCHER; - else if (!strcmp(token, "Status")) - ticket_history->type - = RTCLIENT_TICKET_HISTORY_TYPE_STATUS; - } else if (!strcmp(token, "Field")) { - token = strtok_r(NULL, ":", &tokensaveptr); - if (!strcmp(++token, "Priority")) - ticket_history->field - = RTCLIENT_TICKET_HISTORY_FIELD_PRIORITY; - else if (!strcmp(token, "Status")) - ticket_history->field - = RTCLIENT_TICKET_HISTORY_FIELD_STATUS; - else if (!strcmp(token, "Owner")) - ticket_history->field - = RTCLIENT_TICKET_HISTORY_FIELD_OWNER; - } else if (!strcmp(token, "OldValue")) { - token = strtok_r(NULL, ":", &tokensaveptr); - if (token && strcmp(token, "")) { - ticket_history->old_value - = malloc(strlen(token)); - strcpy(ticket_history->old_value - , ++token); - } - } else if (!strcmp(token, "NewValue")) { - token = strtok_r(NULL, ":", &tokensaveptr); - if (token && strcmp(token, "")) { - ticket_history->new_value - = malloc(strlen(token)); - strcpy(ticket_history->new_value - , ++token); - } - } else if (!strcmp(token, "Data")) { - token = strtok_r(NULL, ":", &tokensaveptr); - if (token && strcmp(token, "")) { - ticket_history->data - = malloc(strlen(token)); - strcpy(ticket_history->data - , ++token); - } - } else if (!strcmp(token, "Description")) { - token = strtok_r(NULL, ":", &tokensaveptr); - ticket_history->description - = malloc(strlen(token)); - strcpy(ticket_history->description - , ++token); - } else if (!strcmp(token, "Content")) { - if (ticket_history->type - == RTCLIENT_TICKET_HISTORY_TYPE_EMAIL_RECORD) { - ticket_history->content = NULL; - break; - } - token = strtok_r(NULL, ":", &tokensaveptr); - ticket_history->content - = malloc(strlen(token)); - strcpy(ticket_history->content, ++token); - /* - while ((history = strtok_r(NULL, "#" - , &historysaveptr))) { - list->length--; - char *ptr = realloc(ticket_history - ->content - , strlen - (ticket_history - ->content) - + strlen(history) - + 2); - ticket_history->content = ptr; - sprintf(ticket_history->content - , "%s#%s" - , ticket_history - ->content - , history); - } - */ - while ((line = strtok_r(NULL, "\n" - , &linesaveptr))) { - if (!strncmp(line, "Creator", 7)) { - ticket_history->creator - = malloc(strlen(line) - - 8); - line += 9; - strcpy(ticket_history->creator - , line); - break; - } - char *ptr = realloc(ticket_history - ->content - , strlen - (ticket_history - ->content) - + strlen(line) - + 2); - ticket_history->content = ptr; - sprintf(ticket_history->content - , "%s\n%s" - , ticket_history - ->content - , line); - } - } else if (!strcmp(token, "Creator")) { - token = strtok_r(NULL, ":", &tokensaveptr); - ticket_history->creator - = malloc(strlen(token)); - strcpy(ticket_history->creator, ++token); - } else if (!strcmp(token, "Created")) { - ticket_history->created - = malloc(sizeof(struct tm)); - ticket_history->created->tm_isdst = -1; - token = strtok_r(NULL, ":", &tokensaveptr); - char *tmsaveptr = NULL; - char *tm = strtok_r(token, " " - , &tmsaveptr); - char *datesaveptr = NULL; - char *date = strtok_r(tm, "-" - , &datesaveptr); - ticket_history->created->tm_year - = atoi(date) - 1900; - date = strtok_r(NULL, "-", &datesaveptr); - ticket_history->created->tm_mon - = atoi(date) - 1; - date = strtok_r(NULL, "-", &datesaveptr); - ticket_history->created->tm_mday - = atoi(date); - tm = strtok_r(NULL, " ", &tmsaveptr); - ticket_history->created->tm_hour - = atoi(tm); - token = strtok_r(NULL, ":", &tokensaveptr); - ticket_history->created->tm_min - = atoi(token); - token = strtok_r(NULL, ":", &tokensaveptr); - ticket_history->created->tm_sec - = atoi(token); - } else if (!strcmp(token, "Attachments")) { - token = strtok_r(NULL, ":", &tokensaveptr); - ticket_history->attachments = NULL; - break; + + line = strtok_r(NULL, "\n", &linesaveptr); + token = strtok_r(line, ": ()", &tokensaveptr); + if (!token) + break; + + size_t i = 0; + while (strcmp(line, "--")) { + i = ticket_history->attachments->length++; + ticket_history->attachments->attachments[i] + = malloc + (sizeof + (rtclient_ticket_history_attachment)); + rtclient_ticket_history_attachment *attachment + = ticket_history->attachments + ->attachments[i]; + + attachment->id = atoi(token); + token = strtok_r(NULL, ": ()", &tokensaveptr); + + if (!strcmp(token, "untitled")) + attachment->file_name = NULL; + else { + attachment->file_name + = malloc(strlen(token) + 1); + strcpy(attachment->file_name, token); } - } while ((line = strtok_r(NULL, "\n", &linesaveptr))); + token = strtok_r(NULL, ": ()", &tokensaveptr); + + size_t length = strlen(token); + char size[length]; + strncpy(size, token, length - 1); + attachment->size = atoi(size); - history = strtok_r(NULL, "#", &historysaveptr); + line = strtok_r(NULL, "\n", &linesaveptr); + token = strtok_r(line, ": ()", &tokensaveptr); + } } } else { free(*listptr); @@ -335,18 +337,17 @@ void rtclient_ticket_history(rtclient_ticket_history_list **listptr { *listptr = malloc(sizeof(rtclient_ticket_history_list)); (*listptr)->length = 0; + request(history_handler, (void *)listptr, NULL, "%s%u%s" + , "REST/1.0/ticket/", id, "/history"); if (long_format) request(history_l_handler, (void *)listptr, NULL, "%s%u%s" , "REST/1.0/ticket/", id, "/history?format=l"); - else - request(history_handler, (void *)listptr, NULL, "%s%u%s" - , "REST/1.0/ticket/", id, "/history"); } void rtclient_ticket_history_free(struct rtclient_ticket_history *history) { - if (history->attachments) { - for(size_t i = 0; i < history->attachments->length; i++) { + if (history->attachments->length) { + for (size_t i = 0; i < history->attachments->length; i++) { rtclient_ticket_history_attachment *attachment = history->attachments->attachments[i]; if (attachment->file_name) |