| 1 |
/* Main processing code for Services. |
| 2 |
* |
| 3 |
* IRC Services is copyright (c) 1996-2009 Andrew Church. |
| 4 |
* E-mail: <achurch@achurch.org> |
| 5 |
* Parts written by Andrew Kempe and others. |
| 6 |
* This program is free but copyrighted software; see the file GPL.txt for |
| 7 |
* details. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "services.h" |
| 11 |
#include "modules.h" |
| 12 |
#include "messages.h" |
| 13 |
|
| 14 |
static int cb_recvmsg = -1; |
| 15 |
|
| 16 |
/*************************************************************************/ |
| 17 |
/*************************************************************************/ |
| 18 |
|
| 19 |
/* split_buf: Split a buffer into arguments and store a pointer to the |
| 20 |
* argument vector in argv_ptr; return the argument count. |
| 21 |
* The argument vector will point to a static buffer; |
| 22 |
* subsequent calls will overwrite this buffer. |
| 23 |
* If colon_special is non-zero, then treat a parameter with a |
| 24 |
* leading ':' as the last parameter of the line, per the IRC |
| 25 |
* RFC. Destroys the buffer by side effect. |
| 26 |
*/ |
| 27 |
|
| 28 |
static char **sbargv = NULL; /* File scope so process_cleanup() can free it */ |
| 29 |
|
| 30 |
int |
| 31 |
split_buf(char *buf, char ***argv_ptr, int colon_special) |
| 32 |
{ |
| 33 |
static int argvsize = 8; |
| 34 |
int argc; |
| 35 |
char *s; |
| 36 |
|
| 37 |
if (!sbargv) |
| 38 |
sbargv = smalloc(sizeof(char *) * argvsize); |
| 39 |
argc = 0; |
| 40 |
while (*buf) |
| 41 |
{ |
| 42 |
if (argc == argvsize) |
| 43 |
{ |
| 44 |
argvsize += 8; |
| 45 |
sbargv = srealloc(sbargv, sizeof(char *) * argvsize); |
| 46 |
} |
| 47 |
if (*buf == ':' && colon_special) |
| 48 |
{ |
| 49 |
sbargv[argc++] = buf + 1; |
| 50 |
*buf = 0; |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
s = strpbrk(buf, " "); |
| 55 |
if (s) |
| 56 |
{ |
| 57 |
*s++ = 0; |
| 58 |
while (*s == ' ') |
| 59 |
s++; |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
s = buf + strlen(buf); |
| 64 |
} |
| 65 |
sbargv[argc++] = buf; |
| 66 |
buf = s; |
| 67 |
} |
| 68 |
} |
| 69 |
*argv_ptr = sbargv; |
| 70 |
return argc; |
| 71 |
} |
| 72 |
|
| 73 |
/*************************************************************************/ |
| 74 |
/*************************************************************************/ |
| 75 |
|
| 76 |
int |
| 77 |
process_init(int ac, char **av) |
| 78 |
{ |
| 79 |
cb_recvmsg = register_callback("receive message"); |
| 80 |
if (cb_recvmsg < 0) |
| 81 |
{ |
| 82 |
log("process_init: register_callback() failed\n"); |
| 83 |
return 0; |
| 84 |
} |
| 85 |
return 1; |
| 86 |
} |
| 87 |
|
| 88 |
/*************************************************************************/ |
| 89 |
|
| 90 |
void |
| 91 |
process_cleanup(void) |
| 92 |
{ |
| 93 |
unregister_callback(cb_recvmsg); |
| 94 |
free(sbargv); |
| 95 |
sbargv = NULL; |
| 96 |
} |
| 97 |
|
| 98 |
/*************************************************************************/ |
| 99 |
|
| 100 |
/* process: Main processing routine. Takes the string in inbuf (global |
| 101 |
* variable) and does something appropriate with it. */ |
| 102 |
|
| 103 |
void |
| 104 |
process(void) |
| 105 |
{ |
| 106 |
char source[64]; |
| 107 |
char cmd[64]; |
| 108 |
char buf[512]; /* Longest legal IRC command line */ |
| 109 |
char *s; |
| 110 |
int ac; /* Parameters for the command */ |
| 111 |
char **av; |
| 112 |
|
| 113 |
|
| 114 |
/* If debugging, log the buffer. */ |
| 115 |
log_debug(1, "Received: %s", inbuf); |
| 116 |
|
| 117 |
/* First make a copy of the buffer so we have the original in case we |
| 118 |
* crash - in that case, we want to know what we crashed on. */ |
| 119 |
strbcpy(buf, inbuf); |
| 120 |
|
| 121 |
/* Split the buffer into pieces. */ |
| 122 |
if (*buf == ':') |
| 123 |
{ |
| 124 |
s = strpbrk(buf, " "); |
| 125 |
if (!s) |
| 126 |
return; |
| 127 |
*s = 0; |
| 128 |
while (isspace(*++s)) |
| 129 |
; |
| 130 |
strbcpy(source, buf + 1); |
| 131 |
strmove(buf, s); |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
*source = 0; |
| 136 |
} |
| 137 |
if (!*buf) |
| 138 |
return; |
| 139 |
s = strpbrk(buf, " "); |
| 140 |
if (s) |
| 141 |
{ |
| 142 |
*s = 0; |
| 143 |
while (isspace(*++s)) |
| 144 |
; |
| 145 |
} |
| 146 |
else |
| 147 |
s = buf + strlen(buf); |
| 148 |
strbcpy(cmd, buf); |
| 149 |
ac = split_buf(s, &av, 1); |
| 150 |
|
| 151 |
/* Do something with the message. */ |
| 152 |
if (call_callback_4(cb_recvmsg, source, cmd, ac, av) <= 0) |
| 153 |
{ |
| 154 |
Message *m = find_message(cmd); |
| 155 |
if (m) |
| 156 |
{ |
| 157 |
if (m->func) |
| 158 |
m->func(source, ac, av); |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
log("unknown message from server (%s)", inbuf); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/* Finally, clear the first byte of `inbuf' to signal that we're |
| 167 |
* finished processing. */ |
| 168 |
*inbuf = 0; |
| 169 |
} |
| 170 |
|
| 171 |
/*************************************************************************/ |
| 172 |
|
| 173 |
/* |
| 174 |
* Local variables: |
| 175 |
* c-file-style: "stroustrup" |
| 176 |
* c-file-offsets: ((case-label . *) (statement-case-intro . *)) |
| 177 |
* indent-tabs-mode: nil |
| 178 |
* End: |
| 179 |
* |
| 180 |
* vim: expandtab shiftwidth=4: |
| 181 |
*/ |