Crossfire Server, Branches 1.12  R18729
dialog.c
Go to the documentation of this file.
1 /*
2  CrossFire, A Multiplayer game for X-windows
3 
4  Copyright (C) 2008 Crossfire Development Team
5  Copyright (C) 1992 Frank Tore Johansen
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21  The authors can be reached via e-mail at crossfire-devel@real-time.com
22 */
23 
29 #include <string.h>
30 #include "global.h"
31 #include "define.h"
32 #include "object.h"
33 #include "dialog.h"
34 
39 void free_dialog_information(object *op) {
40  struct_dialog_message *current, *next;
41  struct_dialog_reply *currep, *nextrep;
42 
44  return;
45 
47  if (!op->dialog_information)
48  return;
49 
50  current = op->dialog_information->all_messages;
51  while (current) {
52  next = current->next;
53  free(current->match);
54  free(current->message);
55  currep = current->replies;
56  while (currep) {
57  nextrep = currep->next;
58  free(currep->reply);
59  free(currep->message);
60  currep = nextrep;
61  }
62  free(current);
63  current = next;
64  }
65 
66  currep = op->dialog_information->all_replies;
67  while (currep) {
68  nextrep = currep->next;
69  free(currep->reply);
70  free(currep->message);
71  free(currep);
72  currep = nextrep;
73  }
74 
75  free(op->dialog_information);
76  op->dialog_information = NULL;
77 }
78 
86 static int matches(const char *exp, const char *text) {
87  char *pipe, *save, *msg;
88  int match = 0;
89 
90  if (exp[0] == '*')
91  return 1;
92 
93  msg = strdup(exp);
94 
95  pipe = strtok_r(msg, "|", &save);
96  while (pipe) {
97  if (re_cmp(text, pipe)) {
98  match = 1;
99  break;
100  }
101  pipe = strtok_r(NULL, "|", &save);
102  }
103 
104  free(msg);
105  return match;
106 }
107 
114 static void parse_dialog_information(object *op) {
115  struct_dialog_message *message = NULL, *last = NULL;
116  struct_dialog_reply *reply = NULL;
117  char *current, *save, *msg, *cp;
118  int len;
119  /* Used for constructing message with */
120  char *tmp = NULL;
121  size_t tmplen = 0;
122 
124  return;
126 
128 
129  if (!op->msg)
130  return;
131 
132  msg = strdup(op->msg);
133  current = strtok_r(msg, "\n", &save);
134 
135  while (current) {
136  if (strncmp(current, "@match ", 7) == 0) {
137  if (message) {
138  message->message = tmp;
139  tmp = NULL;
140  tmplen = 0;
141  }
142 
143  message = (struct_dialog_message *)calloc(1, sizeof(struct_dialog_message));
144  if (last)
145  last->next = message;
146  else
147  op->dialog_information->all_messages = message;
148  last = message;
149 
150  message->match = strdup(current+7);
151  } else if ((strncmp(current, "@reply ", 7) == 0 && (len = 7)) || (strncmp(current, "@question ", 10) == 0 && (len = 10))) {
152  if (message) {
153  reply = (struct_dialog_reply *)calloc(1, sizeof(struct_dialog_reply));
154  reply->type = (len == 7 ? rt_reply : rt_question);
155  cp = strchr(current+len, ' ');
156  if (cp) {
157  *cp = '\0';
158  reply->reply = strdup(current+len);
159  reply->message = strdup(cp+1);
160  } else {
161  reply->reply = strdup(current+len);
162  reply->message = strdup(reply->reply);
163  LOG(llevDebug, "Warning: @reply/@question without message for %s!\n", op->name);
164  }
165  reply->next = message->replies;
166  message->replies = reply;
167 
168  reply = (struct_dialog_reply *)calloc(1, sizeof(struct_dialog_reply));
169  reply->reply = strdup(message->replies->reply);
170  reply->message = strdup(message->replies->message);
171  reply->type = message->replies->type;
172  reply->next = op->dialog_information->all_replies;
173  op->dialog_information->all_replies = reply;
174  } else
175  LOG(llevDebug, "Warning: @reply not in @match block for %s!\n", op->name);
176  } else if (message) {
177  /* Needed to set initial \0 */
178  int wasnull = FALSE;
179  tmplen += strlen(current)+2;
180  if (!tmp)
181  wasnull = TRUE;
182  tmp = realloc(tmp, tmplen*sizeof(char));
183  if (!tmp)
185  if (wasnull)
186  tmp[0] = 0;
187  strncat(tmp, current, tmplen-strlen(tmp)-1);
188  strncat(tmp, "\n", tmplen-strlen(tmp)-1);
189  }
190  current = strtok_r(NULL, "\n", &save);
191  }
192 
193  if (message) {
194  if (!tmp)
195  message->message = strdup("");
196  else
197  message->message = tmp;
198  tmp = NULL;
199  tmplen = 0;
200  }
201 
202  free(msg);
203 }
204 
214 int get_dialog_message(object *op, const char *text, struct_dialog_message **message, struct_dialog_reply **reply) {
215  if (!QUERY_FLAG(op, FLAG_DIALOG_PARSED))
217 
218  for (*message = op->dialog_information->all_messages; *message; *message = (*message)->next) {
219  if (matches((*message)->match, text)) {
220  break;
221  }
222  }
223  if (!*message)
224  return 0;
225 
226  for (*reply = op->dialog_information->all_replies; *reply; *reply = (*reply)->next) {
227  if (strcmp((*reply)->reply, text) == 0)
228  break;
229  }
230 
231  return 1;
232 }
int get_dialog_message(object *op, const char *text, struct_dialog_message **message, struct_dialog_reply **reply)
Definition: dialog.c:214
#define FALSE
Definition: exp.c:42
#define OUT_OF_MEMORY
Definition: define.h:94
#define SET_FLAG(xyz, p)
Definition: define.h:510
static void parse_dialog_information(object *op)
Definition: dialog.c:114
char * message
Definition: dialog.h:11
void free_dialog_information(object *op)
Definition: dialog.c:39
struct struct_dialog_message * next
Definition: dialog.h:20
struct struct_dialog_reply * all_replies
Definition: dialog.h:24
#define strtok_r(x, y, z)
Definition: win32.h:73
static int matches(const char *exp, const char *text)
Definition: dialog.c:86
Definition: dialog.h:5
const char * name
Definition: object.h:167
void fatal(int err)
Definition: glue.c:60
const char * re_cmp(const char *, const char *)
Definition: re-cmp.c:70
#define TRUE
Definition: exp.c:41
#define QUERY_FLAG(xyz, p)
Definition: define.h:514
#define CLEAR_FLAG(xyz, p)
Definition: define.h:512
struct struct_dialog_information * dialog_information
Definition: object.h:158
const char * msg
Definition: object.h:175
struct struct_dialog_reply * replies
Definition: dialog.h:19
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:63
struct struct_dialog_message * all_messages
Definition: dialog.h:25
#define FLAG_DIALOG_PARSED
Definition: define.h:539
struct struct_dialog_reply * next
Definition: dialog.h:13
reply_type type
Definition: dialog.h:12