📄 mms_mmbox.c
字号:
state = mms_get_header_value(msg, octstr_imm("X-Mms-MM-State"));
flags = make_mm_flags(mms_get_header_values(msg, octstr_imm("X-Mms-MM-Flags")), flag_cmds);
if (state == NULL)
state = dfltstate ? octstr_duplicate(dfltstate) : octstr_create("Sent");
mms_replace_header_values(msg, "X-Mms-MM-Flags", flags);
mms_replace_header_value(msg, "X-Mms-MM-State", octstr_get_cstr(state));
s = mms_tobinary(msg);
msize = octstr_len(s);
octstr_write_to_socket(dfd, s);
sdf = octstr_create(df);
if ((nifd = update_mmbox_index(ifd, octstr_get_cstr(home), ITEM_ADD, sdf, state, flags, msize)) < 0 ) {
char fbuf[256];
sprintf(fbuf, "%s/%s", octstr_get_cstr(home), df);
unlink(fbuf);
octstr_destroy(sdf);
sdf = NULL;
goto done;
}
ifd = nifd;
octstr_replace(sdf, octstr_imm("/"), octstr_imm("-"));
done:
if (dfd > 0)
close(dfd);
if (ifd > 0)
close(ifd);
if (s)
octstr_destroy(s);
if (home)
octstr_destroy(home);
if (state)
octstr_destroy(state);
if (flags)
gwlist_destroy(flags, (gwlist_item_destructor_t *)octstr_destroy);
return sdf;
}
int mms_mmbox_modmsg(char *mmbox_root, char *user, Octstr *msgref,
Octstr *state, List *flag_cmds)
{
Octstr *sdf = octstr_duplicate(msgref);
Octstr *fname = NULL, *ftmp = NULL;
Octstr *home = user_mmbox_dir(mmbox_root,user);
Octstr *s = NULL;
List *flags = NULL;
Octstr *nstate = NULL;
int ifd = -1, nifd, tmpfd = -1;
MmsMsg *m = NULL;
int res = -1;
int msize;
octstr_replace(sdf, octstr_imm("-"), octstr_imm("/"));
if (!home)
goto done;
ifd = open_mmbox_index(octstr_get_cstr(home),1);
if (ifd < 0)
goto done;
fname = octstr_format("%S/%S", home, sdf);
s = octstr_read_file(octstr_get_cstr(fname));
if ( s == NULL || octstr_len(s) == 0) {
error(0, "mmbox.mod: failed to read data file [%s] - %s!",
octstr_get_cstr(fname), strerror(errno));
goto done;
}
m = mms_frombinary(s, octstr_imm("anon@anon"));
if (!m) {
error(0, "mmbox.mod: failed to read data file [%s]!",
octstr_get_cstr(fname));
goto done;
}
if (state == NULL)
nstate = mms_get_header_value(m, octstr_imm("X-Mms-MM-State"));
else {
nstate = octstr_duplicate(state);
mms_replace_header_value(m, "X-Mms-MM-State", octstr_get_cstr(nstate));
}
flags = mms_get_header_values(m, octstr_imm("X-Mms-MM-Flags"));
flags = make_mm_flags(flags, flag_cmds);
mms_replace_header_values(m, "X-Mms-MM-Flags", flags);
ftmp = octstr_format("%S.%ld.%d", fname, time(NULL), getpid());
tmpfd = open(octstr_get_cstr(ftmp), O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
if (tmpfd < 0)
goto done;
s = mms_tobinary(m);
msize = octstr_len(s);
octstr_write_to_socket(tmpfd, s);
rename(octstr_get_cstr(ftmp), octstr_get_cstr(fname));
close(tmpfd);
if ((nifd = update_mmbox_index(ifd, octstr_get_cstr(home), ITEM_MOD, sdf, nstate, flags, msize)) < 0) {
/* Not good, we wrote but could not update the index file. scream. */
error(0, "mmbox.mod: failed to update index file, home is %s!",
octstr_get_cstr(home));
goto done;
}
ifd = nifd;
res = 0;
done:
if (ifd > 0)
close(ifd);
if (fname)
octstr_destroy(fname);
if (ftmp)
octstr_destroy(ftmp);
if (sdf)
octstr_destroy(sdf);
if (s)
octstr_destroy(s);
if (home)
octstr_destroy(home);
if (nstate)
octstr_destroy(nstate);
if (flags)
gwlist_destroy(flags, (gwlist_item_destructor_t *)octstr_destroy);
if (m)
mms_destroy(m);
return res;
}
int mms_mmbox_delmsg(char *mmbox_root, char *user, Octstr *msgref)
{
Octstr *sdf = octstr_duplicate(msgref);
Octstr *fname = NULL, *home = user_mmbox_dir(mmbox_root,user);
Octstr *s = NULL;
int res = -1;
int ifd = -1, nifd;
octstr_replace(sdf, octstr_imm("-"), octstr_imm("/"));
if (!home)
goto done;
ifd = open_mmbox_index(octstr_get_cstr(home),1);
if (ifd < 0)
goto done;
fname = octstr_format("%S/%S", home, sdf);
if ((nifd = update_mmbox_index(ifd, octstr_get_cstr(home), ITEM_DEL, sdf, NULL, NULL,0)) < 0) {
error(0, "mmbox.del: failed to update index file, home is %s!",
octstr_get_cstr(home));
goto done;
}
unlink(octstr_get_cstr(fname));
ifd = nifd;
res = 0;
done:
if (ifd > 0)
close(ifd);
if (fname)
octstr_destroy(fname);
if (s)
octstr_destroy(s);
if (home)
octstr_destroy(home);
return 0;
}
static int string_in_list(Octstr *s, List *l)
{
int i, n;
for (i = 0, n = gwlist_len(l); i<n; i++) {
Octstr *x = gwlist_get(l,i);
char *p = octstr_get_cstr(x);
if (p[0] == '+' ||
p[0] == '/' || p[0] == '-')
p++;
if (octstr_str_compare(s, p) == 0)
return 1;
}
return 0;
}
static int _x_octstr_str_compare(Octstr *s, char *p)
{
return (octstr_str_compare(s,p) == 0);
}
static void replace_slash(char s[])
{
while (*s)
if (*s == '/')
*s++ = '-';
else
s++;
}
List *mms_mmbox_search(char *mmbox_root, char *user,
List *state, List *flag_cmds, int start, int limit,
List *msgrefs)
{
int tmpfd = -1;
FILE *fp = NULL;
char linbuf[1024];
Octstr *home = user_mmbox_dir(mmbox_root,user);
List *flags = NULL;
List *dflist = NULL;
int ifd = -1;
int ct;
ifd = open_mmbox_index(octstr_get_cstr(home),1);
if (ifd < 0)
goto done;
if ((tmpfd = dup(ifd)) < 0 ||
(fp = fdopen(tmpfd, "r")) == NULL) {
error(0, "mmbox.search_index: %s Failed to dup descriptor for index "
"file, fp = %p: error = %s\n", octstr_get_cstr(home),
fp, strerror(errno));
goto done;
}
flags = make_mm_flags(NULL, flag_cmds);
ct = 1;
dflist = gwlist_create();
while (fgets(linbuf, sizeof linbuf, fp) != NULL) {
char idx[128], xstate[32];
List *xflags = NULL;
int i, size;
int match = (!state && (!msgrefs || gwlist_len(msgrefs) == 0) && (!xflags || gwlist_len(xflags) == 0));
sscanf(linbuf, "%s %s %d%n", idx, xstate, &size, &i);
/* search: by id list if given, by states if given, by flags if given */
if (!match && state && gwlist_search(state, xstate,
(gwlist_item_matches_t *)_x_octstr_str_compare) != NULL)
match = 1;
/* For the rest we only match if nothing else matched. Save time */
replace_slash(idx);
if (!match && msgrefs &&
gwlist_search(msgrefs, idx,
(gwlist_item_matches_t *)_x_octstr_str_compare) != NULL)
match = 1;
if (!match &&
flag_cmds &&
((xflags = parse_string_list(linbuf + i)) != NULL &&
gwlist_search(xflags, flags, (gwlist_item_matches_t *)string_in_list) != NULL))
match = 1;
if (match && ct >= start && gwlist_len(dflist) <= limit) {
Octstr *x = octstr_create(idx);
/* octstr_replace(x, octstr_imm("/"), octstr_imm("-")); */
gwlist_append(dflist, x);
}
ct++;
if (xflags)
gwlist_destroy(xflags, (gwlist_item_destructor_t *)octstr_destroy);
}
done:
if (fp)
fclose(fp);
else if (tmpfd)
close(tmpfd);
if (ifd > 0)
close(ifd);
if (flags)
gwlist_destroy(flags, (gwlist_item_destructor_t *)octstr_destroy);
if (home)
octstr_destroy(home);
return dflist;
}
MmsMsg *mms_mmbox_get(char *mmbox_root, char *user, Octstr *msgref, unsigned long *msize)
{
Octstr *sdf = octstr_duplicate(msgref);
Octstr *fname = NULL, *home = user_mmbox_dir(mmbox_root,user);
Octstr *s = NULL;
int ifd = -1;
MmsMsg *m = NULL;
octstr_replace(sdf, octstr_imm("-"), octstr_imm("/"));
if (!home)
goto done;
ifd = open_mmbox_index(octstr_get_cstr(home),1); /* Grab a lock on the index file. */
if (ifd < 0)
goto done;
fname = octstr_format("%S/%S", home, sdf);
s = octstr_read_file(octstr_get_cstr(fname));
if (s) {
if (msize)
*msize = octstr_len(s);
m = mms_frombinary(s, octstr_imm("anon@anon"));
} else if (msize)
*msize = 0;
done:
if (ifd > 0)
close(ifd);
if (fname)
octstr_destroy(fname);
if (s)
octstr_destroy(s);
if (home)
octstr_destroy(home);
return m;
}
int mms_mmbox_count(char *mmbox_root, char *user, unsigned long *msgcount, unsigned long *byte_count)
{
int tmpfd = -1;
FILE *fp = NULL;
char linbuf[1024];
int ret = -1;
Octstr *home = user_mmbox_dir(mmbox_root,user);
int ifd = -1;
ifd = open_mmbox_index(octstr_get_cstr(home),1);
if (ifd < 0)
goto done;
if ((tmpfd = dup(ifd)) < 0 ||
(fp = fdopen(tmpfd, "r")) == NULL) {
error(0, "mmbox.count: %s Failed to dup descriptor for index "
"file, fp = %p: error = %s\n", octstr_get_cstr(home),
fp, strerror(errno));
goto done;
}
*msgcount = 0;
*byte_count = 0;
while (fgets(linbuf, sizeof linbuf, fp) != NULL) {
int size = 0;
sscanf(linbuf, "%*s %*s %d", &size);
++*msgcount;
*byte_count = *byte_count + size;
}
ret = 0;
done:
if (fp)
fclose(fp);
else if (tmpfd)
close(tmpfd);
if (ifd > 0)
close(ifd);
return ret;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -