Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/qmail/qmail.c
1074 views
1
/*
2
* Mail check routines. Based on EPIC's mail check
3
*/
4
5
#include "irc.h"
6
#include "struct.h"
7
8
#include "hook.h"
9
#include "ircaux.h"
10
#include "output.h"
11
#include "lastlog.h"
12
#include "status.h"
13
#include "vars.h"
14
#include "window.h"
15
#include <sys/stat.h>
16
17
#include "module.h"
18
#define INIT_MODULE
19
#include "modval.h"
20
21
#ifndef UNIX_MAIL
22
#define UNIX_MAIL "~/Maildir"
23
#endif
24
25
/*
26
* check_mail_status: returns 0 if mail status has not changed, 1 if mail
27
* status has changed
28
*/
29
30
int check_qmail_status(void)
31
{
32
int count = 0;
33
DIR *dp;
34
struct dirent *dir;
35
static int c = 0;
36
char *m;
37
char *mail_path = NULL;
38
39
if (!get_int_var(MAIL_VAR))
40
return 0;
41
42
if (!(mail_path = get_dllstring_var("qmaildir")))
43
m = m_sprintf("%s/new", UNIX_MAIL);
44
else
45
m = m_sprintf("%s/new", mail_path);
46
mail_path = expand_twiddle(m);
47
new_free(&m);
48
49
if (!mail_path)
50
return 0;
51
if ((dp = opendir(mail_path)))
52
{
53
while ((dir = readdir(dp)))
54
{
55
if (!dir->d_ino || (dir->d_name[0] == '.'))
56
continue;
57
count++;
58
}
59
closedir(dp);
60
}
61
if (count > c)
62
{
63
c = count;
64
return c;
65
}
66
else if (count <= c)
67
{
68
c = count;
69
return -count;
70
}
71
return 0;
72
}
73
74
/*
75
* check_mail: This here thing counts up the number of pieces of mail and
76
* returns it as static string. If there are no mail messages, null is
77
* returned.
78
*/
79
80
char *check_qmail (void)
81
{
82
static int old_count = 0;
83
static char ret_str[12];
84
static int i = 0;
85
86
switch (get_int_var(MAIL_VAR))
87
{
88
case 0:
89
return NULL;
90
case 1:
91
{
92
char this[] = "\\|/-";
93
int count;
94
if ((count = check_qmail_status()) > 0)
95
{
96
set_display_target(NULL, LOG_CRAP);
97
if (do_hook(MAIL_LIST, "%s %s", "Mail", "Yes"))
98
put_it("%s", convert_output_format(fget_string_var(FORMAT_MAIL_FSET), "%s %s %s", update_clock(GET_TIME), "Mail", "Yes"));
99
reset_display_target();
100
if (i == 4)
101
i = 0;
102
sprintf(ret_str, "%c", this[i++]);
103
}
104
else if (count == 0)
105
i = 0;
106
return *ret_str ? ret_str : NULL;
107
}
108
case 2:
109
{
110
register int count = 0;
111
count = check_qmail_status();
112
if (count == 0)
113
{
114
old_count = 0;
115
return NULL;
116
}
117
if (count > 0)
118
{
119
if (count > old_count)
120
{
121
set_display_target(NULL, LOG_CRAP);
122
if (do_hook(MAIL_LIST, "%d %d", count - old_count, count))
123
put_it("%s", convert_output_format(fget_string_var(FORMAT_MAIL_FSET), "%s %s %s", update_clock(GET_TIME), "Mail", "Yes"));
124
reset_display_target();
125
}
126
127
old_count = count;
128
sprintf(ret_str, "%d", old_count);
129
return ret_str;
130
}
131
else
132
{
133
if (*ret_str)
134
return ret_str;
135
}
136
}
137
}
138
return NULL;
139
}
140
141
char *name = "Qmail";
142
143
int Qmail_Cleanup(IrcCommandDll **interp, Function_ptr *global_table)
144
{
145
remove_module_proc(VAR_PROC, name, NULL, NULL);
146
remove_module_proc(CHECK_EXT_MAIL_STATUS|TABLE_PROC, name, NULL, NULL);
147
remove_module_proc(CHECK_EXT_MAIL|TABLE_PROC, name, NULL, NULL);
148
return 3;
149
}
150
151
int Qmail_Init(IrcCommandDll **interp, Function_ptr *global_table)
152
{
153
initialize_module(name);
154
add_module_proc(VAR_PROC, name, "qmaildir", "~/Maildir", STR_TYPE_VAR, 0, NULL, NULL);
155
global[CHECK_EXT_MAIL_STATUS] = (Function_ptr) check_qmail_status;
156
global[CHECK_EXT_MAIL] = (Function_ptr) check_qmail;
157
return 0;
158
}
159
160