| 1 |
sysadm |
1.1 |
/*
|
| 2 |
|
|
* Copyright (C) 2001 Dizzy (dizzy@roedu.net)
|
| 3 |
|
|
* Copyright (C) 2004 Donny Redmond (dredmond@linuxmail.org)
|
| 4 |
|
|
*
|
| 5 |
|
|
* This program is free software; you can redistribute it and/or
|
| 6 |
|
|
* modify it under the terms of the GNU General Public License
|
| 7 |
|
|
* as published by the Free Software Foundation; either version 2
|
| 8 |
|
|
* of the License, or (at your option) any later version.
|
| 9 |
|
|
*
|
| 10 |
|
|
* This program is distributed in the hope that it will be useful,
|
| 11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 13 |
|
|
* GNU General Public License for more details.
|
| 14 |
|
|
*
|
| 15 |
|
|
* You should have received a copy of the GNU General Public License
|
| 16 |
|
|
* along with this program; if not, write to the Free Software
|
| 17 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 18 |
|
|
*/
|
| 19 |
|
|
#define MAIL_INTERNAL_ACCESS
|
| 20 |
|
|
#include "common/setup_before.h"
|
| 21 |
|
|
#include <stdio.h>
|
| 22 |
|
|
#ifdef HAVE_STDDEF_H
|
| 23 |
|
|
# include <stddef.h>
|
| 24 |
|
|
#else
|
| 25 |
|
|
# ifndef NULL
|
| 26 |
|
|
# define NULL ((void *)0)
|
| 27 |
|
|
# endif
|
| 28 |
|
|
#endif
|
| 29 |
|
|
#ifdef STDC_HEADERS
|
| 30 |
|
|
# include <stdlib.h>
|
| 31 |
|
|
#else
|
| 32 |
|
|
# ifdef HAVE_MALLOC_H
|
| 33 |
|
|
# include <malloc.h>
|
| 34 |
|
|
# endif
|
| 35 |
|
|
#endif
|
| 36 |
|
|
#ifdef HAVE_STRING_H
|
| 37 |
|
|
# include <string.h>
|
| 38 |
|
|
#else
|
| 39 |
|
|
# ifdef HAVE_STRINGS_H
|
| 40 |
|
|
# include <strings.h>
|
| 41 |
|
|
# endif
|
| 42 |
|
|
#endif
|
| 43 |
|
|
#include "compat/strcasecmp.h"
|
| 44 |
|
|
#include <ctype.h>
|
| 45 |
|
|
#include <errno.h>
|
| 46 |
|
|
#include "compat/strerror.h"
|
| 47 |
|
|
#ifdef TIME_WITH_SYS_TIME
|
| 48 |
|
|
# include <sys/time.h>
|
| 49 |
|
|
# include <time.h>
|
| 50 |
|
|
#else
|
| 51 |
|
|
# ifdef HAVE_SYS_TIME_H
|
| 52 |
|
|
# include <sys/time.h>
|
| 53 |
|
|
# else
|
| 54 |
|
|
# include <time.h>
|
| 55 |
|
|
# endif
|
| 56 |
|
|
#endif
|
| 57 |
|
|
#ifdef HAVE_SYS_TYPES_H
|
| 58 |
|
|
# include <sys/types.h>
|
| 59 |
|
|
#endif
|
| 60 |
|
|
#ifdef HAVE_SYS_STAT_H
|
| 61 |
|
|
# include <sys/stat.h>
|
| 62 |
|
|
#endif
|
| 63 |
|
|
#ifdef HAVE_UNISTD_H
|
| 64 |
|
|
# include <unistd.h>
|
| 65 |
|
|
#endif
|
| 66 |
|
|
#include "compat/statmacros.h"
|
| 67 |
|
|
#include "compat/mkdir.h"
|
| 68 |
|
|
#include "compat/pdir.h"
|
| 69 |
|
|
#ifdef HAVE_FCNTL_H
|
| 70 |
|
|
# include <fcntl.h>
|
| 71 |
|
|
#else
|
| 72 |
|
|
# ifdef HAVE_SYS_FILE_H
|
| 73 |
|
|
# include <sys/file.h>
|
| 74 |
|
|
# endif
|
| 75 |
|
|
#endif
|
| 76 |
|
|
#include "message.h"
|
| 77 |
|
|
#include "connection.h"
|
| 78 |
|
|
#include "common/util.h"
|
| 79 |
|
|
#include "common/eventlog.h"
|
| 80 |
|
|
#include "common/xalloc.h"
|
| 81 |
|
|
#include "account.h"
|
| 82 |
|
|
#include "prefs.h"
|
| 83 |
|
|
#include "mail.h"
|
| 84 |
|
|
#include "common/setup_after.h"
|
| 85 |
|
|
|
| 86 |
|
|
|
| 87 |
|
|
static int identify_mail_function(const char *);
|
| 88 |
|
|
static void mail_usage(t_connection*);
|
| 89 |
|
|
static void mail_func_send(t_connection*,const char *);
|
| 90 |
|
|
static void mail_func_read(t_connection*,const char *);
|
| 91 |
|
|
static void mail_func_delete(t_connection*,const char *);
|
| 92 |
|
|
static int get_mail_quota(t_account *);
|
| 93 |
|
|
|
| 94 |
|
|
/* Mail API */
|
| 95 |
|
|
/* for now this functions are only for internal use */
|
| 96 |
|
|
static t_mailbox * mailbox_open(t_account *, t_mbox_mode mode);
|
| 97 |
|
|
static int mailbox_count(t_mailbox *);
|
| 98 |
|
|
static int mailbox_deliver(t_mailbox *, const char *, const char *);
|
| 99 |
|
|
static t_mail * mailbox_read(t_mailbox *, unsigned int);
|
| 100 |
|
|
static void mailbox_unread(t_mail*);
|
| 101 |
|
|
static struct maillist_struct * mailbox_get_list(t_mailbox *);
|
| 102 |
|
|
static void mailbox_unget_list(struct maillist_struct *);
|
| 103 |
|
|
static int mailbox_delete(t_mailbox *, unsigned int);
|
| 104 |
|
|
static int mailbox_delete_all(t_mailbox *);
|
| 105 |
|
|
static void mailbox_close(t_mailbox *);
|
| 106 |
|
|
|
| 107 |
|
|
static char * clean_str(char *);
|
| 108 |
|
|
|
| 109 |
|
|
|
| 110 |
|
|
static t_mailbox * mailbox_open(t_account * user, t_mbox_mode mode) {
|
| 111 |
|
|
t_mailbox * rez;
|
| 112 |
|
|
char * path;
|
| 113 |
|
|
char const * maildir;
|
| 114 |
|
|
|
| 115 |
|
|
rez=xmalloc(sizeof(t_mailbox));
|
| 116 |
|
|
|
| 117 |
|
|
maildir=prefs_get_maildir();
|
| 118 |
|
|
if (mode & mbox_mode_write)
|
| 119 |
|
|
p_mkdir(maildir,S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
| 120 |
|
|
|
| 121 |
|
|
path=xmalloc(strlen(maildir)+1+8+1);
|
| 122 |
|
|
if (maildir[0]!='\0' && maildir[strlen(maildir)-1]=='/')
|
| 123 |
|
|
sprintf(path,"%s%06u",maildir,account_get_uid(user));
|
| 124 |
|
|
else
|
| 125 |
|
|
sprintf(path,"%s/%06u",maildir,account_get_uid(user));
|
| 126 |
|
|
|
| 127 |
|
|
if (mode & mbox_mode_write)
|
| 128 |
|
|
p_mkdir(path,S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH);
|
| 129 |
|
|
|
| 130 |
|
|
if ((rez->maildir=p_opendir(path))==NULL) {
|
| 131 |
|
|
if (mode & mbox_mode_write)
|
| 132 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"error opening maildir");
|
| 133 |
|
|
xfree(path);
|
| 134 |
|
|
xfree(rez);
|
| 135 |
|
|
return NULL;
|
| 136 |
|
|
}
|
| 137 |
|
|
|
| 138 |
|
|
rez->uid=account_get_uid(user);
|
| 139 |
|
|
rez->path=path;
|
| 140 |
|
|
return rez;
|
| 141 |
|
|
}
|
| 142 |
|
|
|
| 143 |
|
|
static int mailbox_count(t_mailbox *mailbox) {
|
| 144 |
|
|
char const * dentry;
|
| 145 |
|
|
int count=0;
|
| 146 |
|
|
|
| 147 |
|
|
/* consider NULL mailbox as empty mailbox */
|
| 148 |
|
|
if (mailbox==NULL) return 0;
|
| 149 |
|
|
|
| 150 |
|
|
if (mailbox->maildir==NULL) {
|
| 151 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL maildir");
|
| 152 |
|
|
return -1;
|
| 153 |
|
|
}
|
| 154 |
|
|
p_rewinddir(mailbox->maildir);
|
| 155 |
|
|
while ((dentry=p_readdir(mailbox->maildir))!=NULL) if (dentry[0]!='.') count++;
|
| 156 |
|
|
return count;
|
| 157 |
|
|
}
|
| 158 |
|
|
|
| 159 |
|
|
static int mailbox_deliver(t_mailbox * mailbox, const char * sender, const char * message) {
|
| 160 |
|
|
FILE * fd;
|
| 161 |
|
|
char * filename;
|
| 162 |
|
|
|
| 163 |
|
|
if (mailbox==NULL) {
|
| 164 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL mailbox");
|
| 165 |
|
|
return -1;
|
| 166 |
|
|
}
|
| 167 |
|
|
if (mailbox->maildir==NULL) {
|
| 168 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL maildir");
|
| 169 |
|
|
return -1;
|
| 170 |
|
|
}
|
| 171 |
|
|
if (mailbox->path==NULL) {
|
| 172 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL path");
|
| 173 |
|
|
return -1;
|
| 174 |
|
|
}
|
| 175 |
|
|
filename=xmalloc(strlen(mailbox->path)+1+15+1);
|
| 176 |
|
|
sprintf(filename,"%s/%015lu",mailbox->path,(unsigned long)time(NULL));
|
| 177 |
|
|
if ((fd=fopen(filename,"wb"))==NULL) {
|
| 178 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL file descriptor. check permissions");
|
| 179 |
|
|
xfree(filename);
|
| 180 |
|
|
return -1;
|
| 181 |
|
|
}
|
| 182 |
|
|
fprintf(fd,"%s\n",sender); /* write the sender on the first line of message */
|
| 183 |
|
|
fprintf(fd,"%s\n",message); /* then write the actual message */
|
| 184 |
|
|
fclose(fd);
|
| 185 |
|
|
xfree(filename);
|
| 186 |
|
|
return 0;
|
| 187 |
|
|
}
|
| 188 |
|
|
|
| 189 |
|
|
static t_mail * mailbox_read(t_mailbox * mailbox, unsigned int idx) {
|
| 190 |
|
|
char const * dentry;
|
| 191 |
|
|
unsigned int i;
|
| 192 |
|
|
t_mail * rez;
|
| 193 |
|
|
FILE * fd;
|
| 194 |
|
|
char * filename;
|
| 195 |
|
|
|
| 196 |
|
|
if (mailbox==NULL) {
|
| 197 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL mailbox");
|
| 198 |
|
|
return NULL;
|
| 199 |
|
|
}
|
| 200 |
|
|
if (mailbox->maildir==NULL) {
|
| 201 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL maildir");
|
| 202 |
|
|
return NULL;
|
| 203 |
|
|
}
|
| 204 |
|
|
rez=xmalloc(sizeof(t_mail));
|
| 205 |
|
|
p_rewinddir(mailbox->maildir);
|
| 206 |
|
|
dentry = NULL; /* if idx < 1 we should not crash :) */
|
| 207 |
|
|
for(i=0;i<idx && (dentry=p_readdir(mailbox->maildir))!=NULL;i++)
|
| 208 |
|
|
if (dentry[0]=='.') i--;
|
| 209 |
|
|
if (dentry==NULL) {
|
| 210 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"index out of range");
|
| 211 |
|
|
xfree(rez);
|
| 212 |
|
|
return NULL;
|
| 213 |
|
|
}
|
| 214 |
|
|
rez->timestamp=atoi(dentry);
|
| 215 |
|
|
filename=xmalloc(strlen(dentry)+1+strlen(mailbox->path)+1);
|
| 216 |
|
|
sprintf(filename,"%s/%s",mailbox->path,dentry);
|
| 217 |
|
|
if ((fd=fopen(filename,"rb"))==NULL) {
|
| 218 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"error while opening message");
|
| 219 |
|
|
xfree(rez);
|
| 220 |
|
|
xfree(filename);
|
| 221 |
|
|
return NULL;
|
| 222 |
|
|
}
|
| 223 |
|
|
xfree(filename);
|
| 224 |
|
|
rez->sender=xmalloc(256);
|
| 225 |
|
|
fgets(rez->sender,256,fd); /* maybe 256 isnt the right value to bound a line but right now its all i have :) */
|
| 226 |
|
|
clean_str(rez->sender);
|
| 227 |
|
|
rez->message=xmalloc(256);
|
| 228 |
|
|
fgets(rez->message,256,fd);
|
| 229 |
|
|
clean_str(rez->message);
|
| 230 |
|
|
fclose(fd);
|
| 231 |
|
|
rez->timestamp=atoi(dentry);
|
| 232 |
|
|
return rez;
|
| 233 |
|
|
}
|
| 234 |
|
|
|
| 235 |
|
|
static void mailbox_unread(t_mail * mail) {
|
| 236 |
|
|
if (mail==NULL)
|
| 237 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL mail");
|
| 238 |
|
|
else {
|
| 239 |
|
|
if (mail->sender==NULL)
|
| 240 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL sender");
|
| 241 |
|
|
else xfree(mail->sender);
|
| 242 |
|
|
if (mail->message==NULL)
|
| 243 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL message");
|
| 244 |
|
|
else xfree(mail->message);
|
| 245 |
|
|
xfree(mail);
|
| 246 |
|
|
}
|
| 247 |
|
|
}
|
| 248 |
|
|
|
| 249 |
|
|
static struct maillist_struct * mailbox_get_list(t_mailbox *mailbox) {
|
| 250 |
|
|
char const * dentry;
|
| 251 |
|
|
FILE * fd;
|
| 252 |
|
|
struct maillist_struct *rez=NULL, *p=NULL,*q;
|
| 253 |
|
|
char *sender,*filename;
|
| 254 |
|
|
|
| 255 |
|
|
if (mailbox==NULL) {
|
| 256 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL mailbox");
|
| 257 |
|
|
return NULL;
|
| 258 |
|
|
}
|
| 259 |
|
|
if (mailbox->maildir==NULL) {
|
| 260 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL maildir");
|
| 261 |
|
|
return NULL;
|
| 262 |
|
|
}
|
| 263 |
|
|
filename=xmalloc(strlen(mailbox->path)+1+15+1);
|
| 264 |
|
|
p_rewinddir(mailbox->maildir);
|
| 265 |
|
|
for(;(dentry=p_readdir(mailbox->maildir))!=NULL;)
|
| 266 |
|
|
if (dentry[0]!='.') {
|
| 267 |
|
|
q=xmalloc(sizeof(struct maillist_struct));
|
| 268 |
|
|
sprintf(filename,"%s/%s",mailbox->path,dentry);
|
| 269 |
|
|
if ((fd=fopen(filename,"rb"))==NULL) {
|
| 270 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"error while opening message file");
|
| 271 |
|
|
xfree(filename);
|
| 272 |
|
|
xfree(q);
|
| 273 |
|
|
return rez;
|
| 274 |
|
|
}
|
| 275 |
|
|
sender=xmalloc(256);
|
| 276 |
|
|
fgets(sender,256,fd);
|
| 277 |
|
|
clean_str(sender);
|
| 278 |
|
|
fclose(fd);
|
| 279 |
|
|
q->sender=sender;
|
| 280 |
|
|
q->timestamp=atoi(dentry);
|
| 281 |
|
|
q->next=NULL;
|
| 282 |
|
|
if (p==NULL) rez=q;
|
| 283 |
|
|
else p->next=q;
|
| 284 |
|
|
p=q;
|
| 285 |
|
|
}
|
| 286 |
|
|
xfree(filename);
|
| 287 |
|
|
return rez;
|
| 288 |
|
|
}
|
| 289 |
|
|
|
| 290 |
|
|
static void mailbox_unget_list(struct maillist_struct * maill) {
|
| 291 |
|
|
struct maillist_struct *p, *q;
|
| 292 |
|
|
|
| 293 |
|
|
for(p=maill;p!=NULL;p=q) {
|
| 294 |
|
|
if (p->sender!=NULL) xfree(p->sender);
|
| 295 |
|
|
q=p->next;
|
| 296 |
|
|
xfree(p);
|
| 297 |
|
|
}
|
| 298 |
|
|
}
|
| 299 |
|
|
|
| 300 |
|
|
static int mailbox_delete(t_mailbox * mailbox, unsigned int idx) {
|
| 301 |
|
|
char * filename;
|
| 302 |
|
|
char const * dentry;
|
| 303 |
|
|
unsigned int i;
|
| 304 |
|
|
int rez;
|
| 305 |
|
|
|
| 306 |
|
|
if (mailbox==NULL) {
|
| 307 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL mailbox");
|
| 308 |
|
|
return -1;
|
| 309 |
|
|
}
|
| 310 |
|
|
if (mailbox->maildir==NULL) {
|
| 311 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL maildir");
|
| 312 |
|
|
return -1;
|
| 313 |
|
|
}
|
| 314 |
|
|
p_rewinddir(mailbox->maildir);
|
| 315 |
|
|
dentry = NULL; /* if idx < 1 we should not crash :) */
|
| 316 |
|
|
for(i=0;i<idx && (dentry=p_readdir(mailbox->maildir))!=NULL;i++)
|
| 317 |
|
|
if (dentry[0]=='.') i--;
|
| 318 |
|
|
if (dentry==NULL) {
|
| 319 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"index out of range");
|
| 320 |
|
|
return -1;
|
| 321 |
|
|
}
|
| 322 |
|
|
filename=xmalloc(strlen(dentry)+1+strlen(mailbox->path)+1);
|
| 323 |
|
|
sprintf(filename,"%s/%s",mailbox->path,dentry);
|
| 324 |
|
|
rez=remove(filename);
|
| 325 |
|
|
if (rez<0) {
|
| 326 |
|
|
eventlog(eventlog_level_info,__FUNCTION__,"could not remove file \"%s\" (remove: %s)",filename,pstrerror(errno));
|
| 327 |
|
|
}
|
| 328 |
|
|
xfree(filename);
|
| 329 |
|
|
return rez;
|
| 330 |
|
|
}
|
| 331 |
|
|
|
| 332 |
|
|
static int mailbox_delete_all(t_mailbox * mailbox) {
|
| 333 |
|
|
char * filename;
|
| 334 |
|
|
char const * dentry;
|
| 335 |
|
|
int count;
|
| 336 |
|
|
|
| 337 |
|
|
if (mailbox==NULL) {
|
| 338 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL mailbox");
|
| 339 |
|
|
return -1;
|
| 340 |
|
|
}
|
| 341 |
|
|
if (mailbox->maildir==NULL) {
|
| 342 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL maildir");
|
| 343 |
|
|
return -1;
|
| 344 |
|
|
}
|
| 345 |
|
|
filename=xmalloc(strlen(mailbox->path)+1+15+1);
|
| 346 |
|
|
p_rewinddir(mailbox->maildir);
|
| 347 |
|
|
count = 0;
|
| 348 |
|
|
while ((dentry=p_readdir(mailbox->maildir))!=NULL)
|
| 349 |
|
|
if (dentry[0]!='.') {
|
| 350 |
|
|
sprintf(filename,"%s/%s",mailbox->path,dentry);
|
| 351 |
|
|
if (!remove(filename)) count++;
|
| 352 |
|
|
}
|
| 353 |
|
|
xfree(filename);
|
| 354 |
|
|
return count;
|
| 355 |
|
|
}
|
| 356 |
|
|
|
| 357 |
|
|
static void mailbox_close(t_mailbox *mailbox) {
|
| 358 |
|
|
if (mailbox==NULL) return;
|
| 359 |
|
|
if (mailbox->maildir!=NULL) {
|
| 360 |
|
|
p_closedir(mailbox->maildir);
|
| 361 |
|
|
} else {
|
| 362 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL maildir");
|
| 363 |
|
|
}
|
| 364 |
|
|
if (mailbox->path) xfree(mailbox->path);
|
| 365 |
|
|
xfree(mailbox);
|
| 366 |
|
|
}
|
| 367 |
|
|
|
| 368 |
|
|
static char * clean_str(char * str) {
|
| 369 |
|
|
char *p;
|
| 370 |
|
|
|
| 371 |
|
|
for(p=str;*p!='\0';p++)
|
| 372 |
|
|
if (*p=='\n' || *p=='\r') {
|
| 373 |
|
|
*p='\0'; break;
|
| 374 |
|
|
}
|
| 375 |
|
|
return str;
|
| 376 |
|
|
}
|
| 377 |
|
|
|
| 378 |
|
|
extern int handle_mail_command(t_connection * c, char const * text)
|
| 379 |
|
|
{
|
| 380 |
|
|
unsigned int i,j;
|
| 381 |
|
|
char comm[MAX_FUNC_LEN];
|
| 382 |
|
|
|
| 383 |
|
|
if (!prefs_get_mail_support()) {
|
| 384 |
|
|
message_send_text(c,message_type_error,c,"This server has NO mail support.");
|
| 385 |
|
|
return -1;
|
| 386 |
|
|
}
|
| 387 |
|
|
|
| 388 |
|
|
for (i=0; text[i]!=' ' && text[i]!='\0'; i++); /* skip /mail command */
|
| 389 |
|
|
for (; text[i]==' '; i++); /* skip any spaces after it */
|
| 390 |
|
|
|
| 391 |
|
|
for (j=0; text[i]!=' ' && text[i]!='\0' && j<sizeof(comm)-1; i++) /* get function */
|
| 392 |
|
|
if (j<sizeof(comm)-1) comm[j++] = text[i];
|
| 393 |
|
|
comm[j] = '\0';
|
| 394 |
|
|
|
| 395 |
|
|
switch (identify_mail_function(comm)) {
|
| 396 |
|
|
case MAIL_FUNC_SEND:
|
| 397 |
|
|
mail_func_send(c,text+i);
|
| 398 |
|
|
break;
|
| 399 |
|
|
case MAIL_FUNC_READ:
|
| 400 |
|
|
mail_func_read(c,text+i);
|
| 401 |
|
|
break;
|
| 402 |
|
|
case MAIL_FUNC_DELETE:
|
| 403 |
|
|
mail_func_delete(c,text+i);
|
| 404 |
|
|
break;
|
| 405 |
|
|
case MAIL_FUNC_HELP:
|
| 406 |
|
|
message_send_text(c,message_type_info,c,"The mail command supports the following patterns.");
|
| 407 |
|
|
mail_usage(c);
|
| 408 |
|
|
break;
|
| 409 |
|
|
default:
|
| 410 |
|
|
message_send_text(c,message_type_error,c,"The command its incorrect. Use one of the following patterns.");
|
| 411 |
|
|
mail_usage(c);
|
| 412 |
|
|
}
|
| 413 |
|
|
return 0;
|
| 414 |
|
|
}
|
| 415 |
|
|
|
| 416 |
|
|
static int identify_mail_function(const char *funcstr) {
|
| 417 |
|
|
if (strcasecmp(funcstr,"send")==0 ||
|
| 418 |
|
|
strcasecmp(funcstr,"s")==0)
|
| 419 |
|
|
return MAIL_FUNC_SEND;
|
| 420 |
|
|
if (strcasecmp(funcstr,"read")==0 ||
|
| 421 |
|
|
strcasecmp(funcstr,"r")==0 ||
|
| 422 |
|
|
strcasecmp(funcstr,"")==0)
|
| 423 |
|
|
return MAIL_FUNC_READ;
|
| 424 |
|
|
if (strcasecmp(funcstr,"delete")==0 ||
|
| 425 |
|
|
strcasecmp(funcstr,"del")==0)
|
| 426 |
|
|
return MAIL_FUNC_DELETE;
|
| 427 |
|
|
if (strcasecmp(funcstr,"help")==0 ||
|
| 428 |
|
|
strcasecmp(funcstr,"h")==0)
|
| 429 |
|
|
return MAIL_FUNC_HELP;
|
| 430 |
|
|
return MAIL_FUNC_UNKNOWN;
|
| 431 |
|
|
}
|
| 432 |
|
|
|
| 433 |
|
|
static int get_mail_quota(t_account * user) {
|
| 434 |
|
|
int quota;
|
| 435 |
|
|
char const * user_quota;
|
| 436 |
|
|
|
| 437 |
|
|
user_quota=account_get_strattr(user,"BNET\\auth\\mailquota");
|
| 438 |
|
|
if (user_quota==NULL) quota=prefs_get_mail_quota();
|
| 439 |
|
|
else {
|
| 440 |
|
|
quota=atoi(user_quota);
|
| 441 |
|
|
if (quota<1) quota=1;
|
| 442 |
|
|
if (quota>MAX_MAIL_QUOTA) quota=MAX_MAIL_QUOTA;
|
| 443 |
|
|
}
|
| 444 |
|
|
return quota;
|
| 445 |
|
|
}
|
| 446 |
|
|
|
| 447 |
|
|
static void mail_func_send(t_connection * c, const char * str) {
|
| 448 |
|
|
int i;
|
| 449 |
|
|
char *dest;
|
| 450 |
|
|
char const *p,*myname;
|
| 451 |
|
|
t_account * recv;
|
| 452 |
|
|
t_mailbox * mailbox;
|
| 453 |
|
|
|
| 454 |
|
|
if (c==NULL) {
|
| 455 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection");
|
| 456 |
|
|
return;
|
| 457 |
|
|
}
|
| 458 |
|
|
if (str==NULL) {
|
| 459 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL command string");
|
| 460 |
|
|
return;
|
| 461 |
|
|
}
|
| 462 |
|
|
for(i=0;str[i]==' ';i++); /* skip any spaces */
|
| 463 |
|
|
if (str[i]=='\0') { /* the %mail send command has no receiver */
|
| 464 |
|
|
message_send_text(c,message_type_error,c,"You must specify the receiver");
|
| 465 |
|
|
message_send_text(c,message_type_error,c,"Syntax: /mail send <receiver> <message>");
|
| 466 |
|
|
return;
|
| 467 |
|
|
}
|
| 468 |
|
|
p=str+i; /* set ip at the start of receiver string */
|
| 469 |
|
|
for(i=1;p[i]!=' ' && p[i]!='\0';i++); /* skip the receiver string */
|
| 470 |
|
|
if (p[i]=='\0') { /* it seems user forgot to write any message */
|
| 471 |
|
|
message_send_text(c,message_type_error,c,"Your message is empty!");
|
| 472 |
|
|
message_send_text(c,message_type_error,c,"Syntax: /mail send <receiver> <message>");
|
| 473 |
|
|
return;
|
| 474 |
|
|
}
|
| 475 |
|
|
dest=xmalloc(i+1);
|
| 476 |
|
|
memmove(dest,p,i); dest[i]='\0'; /* copy receiver in his separate string */
|
| 477 |
|
|
if ((recv=accountlist_find_account(dest))==NULL) { /* is dest a valid account on this server ? */
|
| 478 |
|
|
message_send_text(c,message_type_error,c,"Receiver UNKNOWN!");
|
| 479 |
|
|
xfree(dest);
|
| 480 |
|
|
return;
|
| 481 |
|
|
}
|
| 482 |
|
|
xfree(dest); /* free dest here, the sooner the better */
|
| 483 |
|
|
if ((mailbox=mailbox_open(recv, mbox_mode_write))==NULL) {
|
| 484 |
|
|
message_send_text(c,message_type_error,c,"There was an error completing your request!");
|
| 485 |
|
|
return;
|
| 486 |
|
|
}
|
| 487 |
|
|
if (get_mail_quota(recv)<=mailbox_count(mailbox)) { /* check quota */
|
| 488 |
|
|
message_send_text(c,message_type_error,c,"Receiver has reached his mail quota. Your message will NOT be sent.");
|
| 489 |
|
|
mailbox_close(mailbox);
|
| 490 |
|
|
return;
|
| 491 |
|
|
}
|
| 492 |
|
|
myname=conn_get_username(c); /* who am i ? */
|
| 493 |
|
|
if (mailbox_deliver(mailbox,myname,p+i+1)<0)
|
| 494 |
|
|
message_send_text(c,message_type_error,c,"There was an error completing your request!");
|
| 495 |
|
|
else
|
| 496 |
|
|
message_send_text(c,message_type_info,c,"Your mail has been sent successfully.");
|
| 497 |
|
|
mailbox_close(mailbox);
|
| 498 |
|
|
}
|
| 499 |
|
|
|
| 500 |
|
|
static void mail_func_read(t_connection * c, const char * str) {
|
| 501 |
|
|
t_account * user;
|
| 502 |
|
|
t_mailbox * mailbox;
|
| 503 |
|
|
const char *p;
|
| 504 |
|
|
char tmp[256];
|
| 505 |
|
|
int i;
|
| 506 |
|
|
|
| 507 |
|
|
if (c==NULL) {
|
| 508 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection");
|
| 509 |
|
|
return;
|
| 510 |
|
|
}
|
| 511 |
|
|
if (str==NULL) {
|
| 512 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL command string");
|
| 513 |
|
|
return;
|
| 514 |
|
|
}
|
| 515 |
|
|
for(i=0;str[i]==' ';i++);
|
| 516 |
|
|
p=str+i;
|
| 517 |
|
|
if ((user=conn_get_account(c))==NULL) {
|
| 518 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL account");
|
| 519 |
|
|
return;
|
| 520 |
|
|
}
|
| 521 |
|
|
|
| 522 |
|
|
mailbox=mailbox_open(user, mbox_mode_read);
|
| 523 |
|
|
if (*p=='\0') { /* user wants to see the mail summary */
|
| 524 |
|
|
struct maillist_struct *maill, *mp;
|
| 525 |
|
|
unsigned int idx;
|
| 526 |
|
|
|
| 527 |
|
|
if (!mailbox_count(mailbox)) {
|
| 528 |
|
|
message_send_text(c,message_type_info,c,"You have no mail.");
|
| 529 |
|
|
mailbox_close(mailbox);
|
| 530 |
|
|
return;
|
| 531 |
|
|
}
|
| 532 |
|
|
if ((maill=mailbox_get_list(mailbox))==NULL) {
|
| 533 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL maillist");
|
| 534 |
|
|
mailbox_close(mailbox);
|
| 535 |
|
|
return;
|
| 536 |
|
|
}
|
| 537 |
|
|
sprintf(tmp,"You have %d messages. Your mail qouta is set to %d.",mailbox_count(mailbox),get_mail_quota(user));
|
| 538 |
|
|
message_send_text(c,message_type_info,c,tmp);
|
| 539 |
|
|
message_send_text(c,message_type_info,c,"ID Sender Date");
|
| 540 |
|
|
message_send_text(c,message_type_info,c,"-------------------------------------");
|
| 541 |
|
|
for(mp=maill,idx=1;mp!=NULL;mp=mp->next,idx++) {
|
| 542 |
|
|
sprintf(tmp,"%02u %-14s %s",idx,mp->sender,ctime(&mp->timestamp));
|
| 543 |
|
|
clean_str(tmp); /* ctime() appends an newline that we get cleaned */
|
| 544 |
|
|
message_send_text(c,message_type_info,c,tmp);
|
| 545 |
|
|
}
|
| 546 |
|
|
message_send_text(c,message_type_info,c,"Use /mail read <ID> to read the content of any message");
|
| 547 |
|
|
mailbox_unget_list(maill);
|
| 548 |
|
|
}
|
| 549 |
|
|
else { /* user wants to read a message */
|
| 550 |
|
|
int idx;
|
| 551 |
|
|
t_mail * mail;
|
| 552 |
|
|
|
| 553 |
|
|
for(i=0;p[i]>='0' && p[i]<='9' && p[i]!='\0';i++);
|
| 554 |
|
|
if (p[i]!='\0' && p[i]!=' ') {
|
| 555 |
|
|
message_send_text(c,message_type_error,c,"Invalid index. Please use /mail read <index> where <index> is a number.");
|
| 556 |
|
|
mailbox_close(mailbox);
|
| 557 |
|
|
return;
|
| 558 |
|
|
}
|
| 559 |
|
|
idx=atoi(p);
|
| 560 |
|
|
if (idx<1 || idx>mailbox_count(mailbox)) {
|
| 561 |
|
|
message_send_text(c,message_type_error,c,"That index is out of range.");
|
| 562 |
|
|
mailbox_close(mailbox);
|
| 563 |
|
|
return;
|
| 564 |
|
|
}
|
| 565 |
|
|
if ((mail=mailbox_read(mailbox,idx))==NULL) {
|
| 566 |
|
|
message_send_text(c,message_type_error,c,"There was an error completing your request.");
|
| 567 |
|
|
mailbox_close(mailbox);
|
| 568 |
|
|
return;
|
| 569 |
|
|
}
|
| 570 |
|
|
sprintf(tmp,"Message #%d from %s on %s:",idx,mail->sender,clean_str(ctime(&mail->timestamp)));
|
| 571 |
|
|
message_send_text(c,message_type_info,c,tmp);
|
| 572 |
|
|
message_send_text(c,message_type_info,c,mail->message);
|
| 573 |
|
|
mailbox_unread(mail);
|
| 574 |
|
|
}
|
| 575 |
|
|
mailbox_close(mailbox);
|
| 576 |
|
|
}
|
| 577 |
|
|
|
| 578 |
|
|
static void mail_func_delete(t_connection * c, const char * str) {
|
| 579 |
|
|
t_account * user;
|
| 580 |
|
|
t_mailbox * mailbox;
|
| 581 |
|
|
const char * p;
|
| 582 |
|
|
char tmp[256]; /* that should be enough */
|
| 583 |
|
|
int i;
|
| 584 |
|
|
|
| 585 |
|
|
if (c==NULL) {
|
| 586 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection");
|
| 587 |
|
|
return;
|
| 588 |
|
|
}
|
| 589 |
|
|
if (str==NULL) {
|
| 590 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL command string");
|
| 591 |
|
|
return;
|
| 592 |
|
|
}
|
| 593 |
|
|
for(i=0;str[i]==' ';i++);
|
| 594 |
|
|
p=str+i;
|
| 595 |
|
|
if (*p=='\0') {
|
| 596 |
|
|
message_send_text(c,message_type_error,c,"Please specify which message to delete. Use the following syntax: /mail delete {<index>|all} .");
|
| 597 |
|
|
return;
|
| 598 |
|
|
}
|
| 599 |
|
|
if ((user=conn_get_account(c))==NULL) {
|
| 600 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL account");
|
| 601 |
|
|
return;
|
| 602 |
|
|
}
|
| 603 |
|
|
if ((mailbox=mailbox_open(user, mbox_mode_write))==NULL) {
|
| 604 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL mailbox");
|
| 605 |
|
|
return;
|
| 606 |
|
|
}
|
| 607 |
|
|
if (strcmp(p,"all")==0) {
|
| 608 |
|
|
int rez;
|
| 609 |
|
|
|
| 610 |
|
|
if ((rez=mailbox_delete_all(mailbox))<0) {
|
| 611 |
|
|
message_send_text(c,message_type_error,c,"There was an error completing your request.");
|
| 612 |
|
|
mailbox_close(mailbox);
|
| 613 |
|
|
return;
|
| 614 |
|
|
}
|
| 615 |
|
|
sprintf(tmp,"Successfuly deleted %d messages.",rez);
|
| 616 |
|
|
message_send_text(c,message_type_info,c,tmp);
|
| 617 |
|
|
}
|
| 618 |
|
|
else {
|
| 619 |
|
|
int idx;
|
| 620 |
|
|
|
| 621 |
|
|
for(i=0;p[i]>='0' && p[i]<='9' && p[i]!='\0';i++);
|
| 622 |
|
|
if (p[i]!='\0' && p[i]!=' ') {
|
| 623 |
|
|
message_send_text(c,message_type_error,c,"Invalid index. Please use /mail delete {<index>|all} where <index> is a number.");
|
| 624 |
|
|
mailbox_close(mailbox);
|
| 625 |
|
|
return;
|
| 626 |
|
|
}
|
| 627 |
|
|
idx=atoi(p);
|
| 628 |
|
|
if (idx<1 || idx>mailbox_count(mailbox)) {
|
| 629 |
|
|
message_send_text(c,message_type_error,c,"That index is out of range.");
|
| 630 |
|
|
mailbox_close(mailbox);
|
| 631 |
|
|
return;
|
| 632 |
|
|
}
|
| 633 |
|
|
if (mailbox_delete(mailbox,idx)<0) {
|
| 634 |
|
|
message_send_text(c,message_type_error,c,"There was an error completing your request.");
|
| 635 |
|
|
mailbox_close(mailbox);
|
| 636 |
|
|
return;
|
| 637 |
|
|
}
|
| 638 |
|
|
sprintf(tmp,"Succesfully deleted message #%02d.",idx);
|
| 639 |
|
|
message_send_text(c,message_type_info,c,tmp);
|
| 640 |
|
|
}
|
| 641 |
|
|
mailbox_close(mailbox);
|
| 642 |
|
|
}
|
| 643 |
|
|
|
| 644 |
|
|
static void mail_usage(t_connection * c) {
|
| 645 |
|
|
message_send_text(c,message_type_info,c,"to print this information:");
|
| 646 |
|
|
message_send_text(c,message_type_info,c," /mail help");
|
| 647 |
|
|
message_send_text(c,message_type_info,c,"to print an index of you messages:");
|
| 648 |
|
|
message_send_text(c,message_type_info,c," /mail [read]");
|
| 649 |
|
|
message_send_text(c,message_type_info,c,"to send a message:");
|
| 650 |
|
|
message_send_text(c,message_type_info,c," /mail send <receiver> <message>");
|
| 651 |
|
|
message_send_text(c,message_type_info,c,"to read a message:");
|
| 652 |
|
|
message_send_text(c,message_type_info,c," /mail read <index num>");
|
| 653 |
|
|
message_send_text(c,message_type_info,c,"to delete a message:");
|
| 654 |
|
|
message_send_text(c,message_type_info,c," /mail delete {<index>|all}");
|
| 655 |
|
|
message_send_text(c,message_type_info,c,"Commands may be abbreviated as follows:");
|
| 656 |
|
|
message_send_text(c,message_type_info,c," help: h");
|
| 657 |
|
|
message_send_text(c,message_type_info,c," read: r");
|
| 658 |
|
|
message_send_text(c,message_type_info,c," send: s");
|
| 659 |
|
|
message_send_text(c,message_type_info,c," delete: del");
|
| 660 |
|
|
}
|
| 661 |
|
|
|
| 662 |
|
|
extern char const * check_mail(t_connection const * c) {
|
| 663 |
|
|
t_account * user;
|
| 664 |
|
|
t_mailbox * mailbox;
|
| 665 |
|
|
static char tmp[64];
|
| 666 |
|
|
int count;
|
| 667 |
|
|
|
| 668 |
|
|
if (!(c)) {
|
| 669 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection");
|
| 670 |
|
|
return "";
|
| 671 |
|
|
}
|
| 672 |
|
|
|
| 673 |
|
|
if (!(user=conn_get_account(c)))
|
| 674 |
|
|
return "";
|
| 675 |
|
|
|
| 676 |
|
|
mailbox=mailbox_open(user, mbox_mode_read);
|
| 677 |
|
|
count = mailbox_count(mailbox);
|
| 678 |
|
|
mailbox_close(mailbox);
|
| 679 |
|
|
|
| 680 |
|
|
if (count == 0)
|
| 681 |
|
|
{
|
| 682 |
|
|
return "You have no mail.";
|
| 683 |
|
|
}
|
| 684 |
|
|
else
|
| 685 |
|
|
{
|
| 686 |
|
|
sprintf(tmp,"You have %d message(s) in your mailbox.",count);
|
| 687 |
|
|
return tmp;
|
| 688 |
|
|
}
|
| 689 |
|
|
}
|