ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/opercmd.c
(Generate patch)

Comparing hopm/trunk/src/opercmd.c (file contents):
Revision 5092 by michael, Tue Dec 23 20:12:39 2014 UTC vs.
Revision 5114 by michael, Wed Dec 24 22:12:05 2014 UTC

# Line 43 | Line 43 | along with this program; if not, write t
43   #include "list.h"
44   #include "stats.h"
45  
46 /* List of active commands */
46  
47 < list_t *COMMANDS = NULL;
47 > list_t *COMMANDS = NULL;  /* List of active commands */
48  
49  
50   static struct Command *command_create(unsigned short type, char *param, char *irc_nick, struct ChannelConf *target);
# Line 56 | Line 55 | static void cmd_stat(char *, char *, str
55   static void cmd_fdstat(char *, char *, struct ChannelConf *);
56  
57   static struct OperCommandHash COMMAND_TABLE[] =
58 <   {
59 <      {"CHECK",  cmd_check  },
60 <      {"SCAN",   cmd_check  },
61 <      {"STAT",   cmd_stat   },
62 <      {"STATS",  cmd_stat   },
63 <      {"STATUS", cmd_stat   },
64 <      {"FDSTAT", cmd_fdstat },
65 <   };
67 <
58 > {
59 >  {"CHECK",  cmd_check  },
60 >  {"SCAN",   cmd_check  },
61 >  {"STAT",   cmd_stat   },
62 >  {"STATS",  cmd_stat   },
63 >  {"STATUS", cmd_stat   },
64 >  {"FDSTAT", cmd_fdstat },
65 > };
66  
67  
68   /* command_init
# Line 75 | Line 73 | static struct OperCommandHash COMMAND_TA
73   * Return: NONE
74   *
75   */
76 <
77 < void command_init()
76 > void
77 > command_init(void)
78   {
79 <   if(COMMANDS == NULL)
80 <      COMMANDS = list_create();
79 >  if (COMMANDS == NULL)
80 >    COMMANDS = list_create();
81   }
82  
85
86
87
83   /* command_timer
84   *
85   *    Perform ~1 second actions.
# Line 94 | Line 89 | void command_init()
89   * Return: NONE
90   *
91   */
92 <
93 < void command_timer()
92 > void
93 > command_timer(void)
94   {
95 +  static unsigned short interval;
96 +  node_t *node, *next;
97 +  struct Command *cs;
98 +  time_t present;
99 +
100 +  /* Only perform command removal every COMMANDINTERVAL seconds */
101 +  if (interval++ < COMMANDINTERVAL)
102 +    return;
103 +  else
104 +    interval = 0;
105  
106 <   static unsigned short interval;
102 <
103 <   node_t *node, *next;
104 <   struct Command *cs;
105 <   time_t present;
106 >  time(&present);
107  
108 <   /* Only perform command removal every COMMANDINTERVAL seconds */
109 <   if(interval++ < COMMANDINTERVAL)
108 >  LIST_FOREACH_SAFE(node, next, COMMANDS->head)
109 >  {
110 >    cs = node->data;
111 >
112 >    if ((present - cs->added) > COMMANDTIMEOUT)
113 >    {
114 >      command_free(cs);
115 >      list_remove(COMMANDS, node);
116 >      node_free(node);
117 >    }
118 >    else   /* Since the queue is in order, it's also ordered by time, no nodes after this will be timed out */
119        return;
120 <   else
111 <      interval = 0;
112 <
113 <   time(&present);
114 <
115 <   LIST_FOREACH_SAFE(node, next, COMMANDS->head)
116 <   {
117 <      cs = node->data;
118 <      if((present - cs->added) > COMMANDTIMEOUT)
119 <      {
120 <         command_free(cs);
121 <         list_remove(COMMANDS, node);
122 <         node_free(node);
123 <      }
124 <      else   /* Since the queue is in order, it's also ordered by time, no nodes after this will be timed out */
125 <         return;
126 <   }
120 >  }
121   }
122  
129
123   /* command_parse
124   *
125   *    Parse a command to hopm (sent to a channel hopm is on). The command is parsed
# Line 141 | Line 134 | void command_timer()
134   *    source_p: Operator (hopefully) that sent the command.
135   *
136   */
137 <
138 < void command_parse(char *command, char *msg, struct ChannelConf *target,
139 <      struct UserInfo *source_p)
137 > void
138 > command_parse(char *command, char *msg, struct ChannelConf *target,
139 >              struct UserInfo *source_p)
140   {
141 <   unsigned int i;
142 <   char *param; /* Parsed parameters */
143 <   struct Command *cs;
144 <   node_t *node;
145 <
146 <   if(OPT_DEBUG)
147 <   {
148 <      log_printf("COMMAND -> Parsing command (%s) from %s [%s]", command,
149 <            source_p->irc_nick, target->name);
150 <   }
141 >  char *param;  /* Parsed parameters */
142 >  struct Command *cs;
143 >  node_t *node;
144 >
145 >  if (OPT_DEBUG)
146 >    log_printf("COMMAND -> Parsing command (%s) from %s [%s]", command,
147 >               source_p->irc_nick, target->name);
148 >
149 >  /* Only allow COMMANDMAX commands in the queue */
150 >  if (LIST_SIZE(COMMANDS) >= COMMANDMAX)
151 >    return;
152 >
153 >  /*
154 >   * Parameter is the first character in command after the first space.
155 >   * param will be NULL if:
156 >   * 1. There was no space
157 >   * 2. There was a space but it was the last character in command, in which case
158 >   *    param = '\0'
159 >   */
160  
161 <   /* Only allow COMMANDMAX commands in the queue */
162 <   if(LIST_SIZE(COMMANDS) >= COMMANDMAX)
161 <      return;
161 >  /* Skip past the botname/!all */
162 >  command = strchr(command, ' ');
163  
164 <   /* Parameter is the first character in command after the first space.
165 <      param will be NULL if:
166 <      1. There was no space
167 <      2. There was a space but it was the last character in command, in which case
167 <         param = '\0'
164 >  /* TBD: skip leading spaces if there's more than one */
165 >  /*
166 >   * There is no command OR there is at least nothing
167 >   * past that first space.
168     */
169 +  if (command == NULL || *++command == '\0')
170 +    return;
171  
172 <   /* Skip past the botname/!all */
173 <   command = strchr(command, ' ');
172 < /* TBD: skip leading spaces if there's more than one */
173 <   /* There is no command OR
174 <      there is at least nothing
175 <      past that first space.  */
176 <   if (command == NULL || *++command == '\0')
177 <      return;
172 >  /* Find the parameters */
173 >  param = strchr(command, ' ');
174  
175 +  if (param)
176 +  {
177 +    *param = '\0';
178 +    param++;
179 +  }
180 +  else
181 +    param = "";
182  
183 <   /* Find the parameters */
181 <   param = strchr(command, ' ');
183 >  log_printf("COMMAND -> parsed [%s] [%s]", command, param);
184  
185 <   if(param != NULL)
186 <   {
187 <      *param = '\0';
188 <      param++;
189 <   }
190 <   else
191 <      param = "";
192 <
193 <   log_printf("COMMAND -> parsed [%s] [%s]", command, param);
194 <
195 <   /* Lookup the command in the table */
194 <   for(i = 0; i < sizeof(COMMAND_TABLE) / sizeof(struct OperCommandHash); i++)
195 <   {
196 <      if(strcasecmp(command, COMMAND_TABLE[i].command) == 0)
197 <      {
198 <         /* Queue this command */
199 <         cs = command_create(i, param, source_p->irc_nick, target);
200 <         node = node_create(cs);
201 <         list_add(COMMANDS, node);
202 <      }
203 <   }
185 >  /* Lookup the command in the table */
186 >  for (unsigned int i = 0; i < sizeof(COMMAND_TABLE) / sizeof(struct OperCommandHash); ++i)
187 >  {
188 >    if (strcasecmp(command, COMMAND_TABLE[i].command) == 0)
189 >    {
190 >      /* Queue this command */
191 >      cs = command_create(i, param, source_p->irc_nick, target);
192 >      node = node_create(cs);
193 >      list_add(COMMANDS, node);
194 >    }
195 >  }
196  
197 <   irc_send("USERHOST %s", source_p->irc_nick);
197 >  irc_send("USERHOST %s", source_p->irc_nick);
198   }
199  
208
209
210
200   /* command_create
201   *
202   *    Create a Command struct.
# Line 221 | Line 210 | void command_parse(char *command, char *
210   * Return:
211   *    Pointer to new Command
212   */
213 <
214 < static struct Command *command_create(unsigned short type, char *param, char *irc_nick, struct ChannelConf *target)
213 > static struct Command *
214 > command_create(unsigned short type, char *param, char *irc_nick, struct ChannelConf *target)
215   {
216 <   struct Command *ret;
228 <
229 <   ret = MyMalloc(sizeof *ret);
230 <
231 <   ret->type = type;
216 >  struct Command *ret = MyMalloc(sizeof *ret);
217  
218 <   if(param != NULL)
234 <      ret->param = xstrdup(param);
235 <   else
236 <      ret->param = NULL;
218 >  ret->type = type;
219  
220 <   ret->irc_nick = xstrdup(irc_nick);
221 <   ret->target = target; /* FIXME: This needs fixed if rehash is implemented */
220 >  if (param)
221 >    ret->param = xstrdup(param);
222 >  else
223 >    ret->param = NULL;
224  
225 <   time(&(ret->added));
225 >  ret->irc_nick = xstrdup(irc_nick);
226 >  ret->target = target;  /* FIXME: This needs fixed if rehash is implemented */
227  
228 <   return ret;
228 >  time(&(ret->added));
229  
230 +  return ret;
231   }
232  
247
248
233   /* command_free
234   *
235   *   Free a command struct
# Line 255 | Line 239 | static struct Command *command_create(un
239   *  
240   * Return: NONE
241   */
242 <
243 < static void command_free(struct Command *command)
242 > static void
243 > command_free(struct Command *command)
244   {
245 <   if(command->param != NULL)
246 <      MyFree(command->param);
263 <   MyFree(command->irc_nick);
264 <   MyFree(command);
265 < }
266 <
267 <
245 >  if (command->param)
246 >    MyFree(command->param);
247  
248 +  MyFree(command->irc_nick);
249 +  MyFree(command);
250 + }
251  
252   /* command_userhost
253   *
254   *    A 302 reply was received. The reply is parsed to check if the
255 < *    user was an operator. If so any commands they had queued are
255 > *    user was an operator. If so any commands they had queued are
256   *    executed.
257   *  
258   * Parameters:
# Line 279 | Line 261 | static void command_free(struct Command
261   * Return: NONE
262   *
263   */
264 <
265 < void command_userhost(char *reply)
264 > void
265 > command_userhost(char *reply)
266   {
267 <   node_t *node, *next;
268 <   struct Command *cs;
269 <   char *tmp;
270 <
271 <   int oper = 0;
272 <
273 <   tmp = strchr(reply, '=');
274 <
275 <   /* They quit, ignore it */
276 <   if (!tmp)
277 <      return;
278 <
279 <   /* Operators have a * flag in a USERHOST reply */
280 <   if (*(tmp - 1) == '*')
281 <      oper = 1;
282 <
283 <   /* Null terminate it so tmp = the oper's nick */
302 <  if(oper)
303 <     *(--tmp) = '\0';
267 >  node_t *node, *next;
268 >  char *tmp;
269 >  int oper = 0;
270 >
271 >  tmp = strchr(reply, '=');
272 >
273 >  /* They quit, ignore it */
274 >  if (tmp == NULL)
275 >    return;
276 >
277 >  /* Operators have a * flag in a USERHOST reply */
278 >  if (*(tmp - 1) == '*')
279 >    oper = 1;
280 >
281 >  /* Null terminate it so tmp = the oper's nick */
282 >  if (oper)
283 >    *(--tmp) = '\0';
284    else
285 <     *(tmp) = '\0';
285 >    *(tmp) = '\0';
286  
287 <   /* Find any queued commands that match this user */
288 <   LIST_FOREACH_SAFE(node, next, COMMANDS->head)
289 <   {
290 <      cs = node->data;
291 <
292 <      if(strcmp(cs->irc_nick, reply) == 0)
293 <      {
294 <         if(oper)
295 <            COMMAND_TABLE[cs->type].handler(cs->param, cs->irc_nick, cs->target);
296 <
297 <         /* Cleanup the command */
298 <         command_free(cs);
299 <         list_remove(COMMANDS, node);
300 <         node_free(node);
301 <      }
302 <   }
287 >  /* Find any queued commands that match this user */
288 >  LIST_FOREACH_SAFE(node, next, COMMANDS->head)
289 >  {
290 >    struct Command *cs = node->data;
291 >
292 >    if (strcmp(cs->irc_nick, reply) == 0)
293 >    {
294 >      if (oper)
295 >        COMMAND_TABLE[cs->type].handler(cs->param, cs->irc_nick, cs->target);
296 >
297 >      /* Cleanup the command */
298 >      command_free(cs);
299 >      list_remove(COMMANDS, node);
300 >      node_free(node);
301 >    }
302 >  }
303   }
304  
325
326
305   /* cmd_check
306   *
307   *    Start a manual scan on given IP. Parameter MUST be an IP. HOPM should not
# Line 335 | Line 313 | void command_userhost(char *reply)
313   *    target: channel command was sent to
314   *
315   */
316 <
317 < static void cmd_check(char *param, char *source, struct ChannelConf *target)
316 > static void
317 > cmd_check(char *param, char *source, struct ChannelConf *target)
318   {
319    scan_manual(param, target);
320   }
321  
344
345
322   /* cmd_stat
323   *
324   *   Send output of stats to channel.
# Line 352 | Line 328 | static void cmd_check(char *param, char
328   *    source: irc_nick of user who requested the command
329   *    target: channel command was sent to
330   */
331 <
332 < static void cmd_stat(char *param, char *source, struct ChannelConf *target)
331 > static void
332 > cmd_stat(char *param, char *source, struct ChannelConf *target)
333   {
334    stats_output(target->name);
335   }
336  
361
337   /* cmd_fdstat
338   *
339   *   Send output of stats to channel.
# Line 368 | Line 343 | static void cmd_stat(char *param, char *
343   *    source: irc_nick of user who requested the command
344   *    target: channel command was sent to
345   */
346 <
347 < static void cmd_fdstat(char *param, char *source, struct ChannelConf *target)
346 > static void
347 > cmd_fdstat(char *param, char *source, struct ChannelConf *target)
348   {
349    fdstats_output(target->name);
350   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines