]> jfr.im git - irc/evilnet/x3.git/blob - src/mod-helpserv.c
mod-python: generalised the setting of the PYTHONPATH environment variable
[irc/evilnet/x3.git] / src / mod-helpserv.c
1 /* mod-helpserv.c - Support Helper assistant service
2 * Copyright 2002-2003 srvx Development Team
3 *
4 * This file is part of x3.
5 *
6 * x3 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21 /* Wishlist for mod-helpserv.c
22 * - FAQ responses
23 * - Get cmd_statsreport to sort by time and show requests closed
24 * - .. then make statsreport show weighted combination of time and requests closed :)
25 * - Allow manipulation of the persist types on a per-request basis... so
26 * if it's normally set to close requests after a user quits, but there
27 * is a long-term issue, then that single request can remain
28 */
29
30 #include "conf.h"
31 #include "global.h"
32 #include "modcmd.h"
33 #include "nickserv.h"
34 #include "opserv.h"
35 #include "saxdb.h"
36 #include "timeq.h"
37
38 #define HELPSERV_CONF_NAME "modules/helpserv"
39 #define HELPSERV_HELPFILE_NAME "mod-helpserv.help"
40 const char *helpserv_module_deps[] = { NULL };
41
42 /* db entries */
43 #define KEY_BOTS "bots"
44 #define KEY_LAST_STATS_UPDATE "last_stats_update"
45 #define KEY_NICK "nick"
46 #define KEY_DB_BADCHANS "badchans"
47 #define KEY_HELP_CHANNEL "help_channel"
48 #define KEY_PAGE_DEST "page_dest"
49 #define KEY_CMDWORD "cmdword"
50 #define KEY_PERSIST_LENGTH "persist_length"
51 #define KEY_REGISTERED "registered"
52 #define KEY_REGISTRAR "registrar"
53 #define KEY_IDWRAP "id_wrap"
54 #define KEY_REQ_MAXLEN "req_maxlen"
55 #define KEY_LAST_REQUESTID "last_requestid"
56 #define KEY_HELPERS "users"
57 #define KEY_HELPER_LEVEL "level"
58 #define KEY_HELPER_HELPMODE "helpmode"
59 #define KEY_HELPER_WEEKSTART "weekstart"
60 #define KEY_HELPER_STATS "stats"
61 #define KEY_HELPER_STATS_TIME "time"
62 #define KEY_HELPER_STATS_PICKUP "picked_up"
63 #define KEY_HELPER_STATS_CLOSE "closed"
64 #define KEY_HELPER_STATS_REASSIGNFROM "reassign_from"
65 #define KEY_HELPER_STATS_REASSIGNTO "reassign_to"
66 #define KEY_REQUESTS "requests"
67 #define KEY_REQUEST_HELPER "helper"
68 #define KEY_REQUEST_ASSIGNED "assigned"
69 #define KEY_REQUEST_HANDLE "handle"
70 #define KEY_REQUEST_TEXT "text"
71 #define KEY_REQUEST_OPENED "opened"
72 #define KEY_REQUEST_NICK "nick"
73 #define KEY_REQUEST_USERHOST "userhost"
74 #define KEY_REQUEST_CLOSED "closed"
75 #define KEY_REQUEST_CLOSEREASON "closereason"
76 #define KEY_NOTIFICATION "notification"
77 #define KEY_PRIVMSG_ONLY "privmsg_only"
78 #define KEY_REQ_ON_JOIN "req_on_join"
79 #define KEY_AUTO_VOICE "auto_voice"
80 #define KEY_AUTO_DEVOICE "auto_devoice"
81 #define KEY_LAST_ACTIVE "last_active"
82 #define KEY_JOIN_TOTAL "join_total"
83 #define KEY_ALERT_NEW "alert_new"
84 #define KEY_SUSPENDED "suspended"
85 #define KEY_EXPIRY "expiry"
86 #define KEY_ISSUED "issued"
87 #define KEY_SUSPENDER "suspender"
88 #define KEY_REASON "reason"
89
90 /* General */
91 #define HSFMT_TIME "%a, %d %b %Y %H:%M:%S %Z"
92 static const struct message_entry msgtab[] = {
93 { "HSMSG_READHELP_SUCCESS", "Read HelpServ help database in "FMT_TIME_T".%03ld seconds." },
94 { "HSMSG_INVALID_BOT", "This command requires a valid HelpServ bot name." },
95 { "HSMSG_ILLEGAL_CHANNEL", "$b%s$b is an illegal channel; cannot use it." },
96 { "HSMSG_INTERNAL_COMMAND", "$b%s$b appears to be an internal HelpServ command, sorry." },
97 { "HSMSG_NOT_IN_USERLIST", "%s lacks access to $b%s$b." },
98 { "HSMSG_LEVEL_TOO_LOW", "You lack access to this command." },
99 { "HSMSG_OPER_CMD", "This command can only be executed via $O." },
100 { "HSMSG_WTF_WHO_ARE_YOU", "$bUnable to find you on the %s userlist.$b This is a bug. Please report it." },
101 { "HSMSG_NO_USE_OPSERV", "This command cannot be used via $O. If you really need to use it, add yourself to the userlist." },
102 { "HSMSG_OPSERV_NEED_USER", "To use this command via $O, you must supply a user to target." },
103 { "HSMSG_PAGE_REQUEST", "Page from $b%s$b: $b%s$b" },
104 { "HSMSG_BAD_REQ_TYPE", "I don't know how to list $b%s$b requests." },
105
106 /* Greetings */
107 { "HSMSG_GREET_EXISTING_REQ", "Welcome back to %s. You have already opened request ID#%lu. Any messages you send to $S will be appended to that request." },
108 { "HSMSG_GREET_PRIVMSG_EXISTREQ", "Hello again. Your account has a previously opened request ID#%lu. This message and any further messages you send to $S will be appended to that request." },
109 { "HSMSG_REQUESTS_OPEN", "Hello. There are %d requests open. Use /msg %s LIST to view all requests, or use /msg %s NEXT to pickup the oldest request." },
110
111 /* User Management */
112 { "HSMSG_CANNOT_ADD", "You do not have permission to add users." },
113 { "HSMSG_CANNOT_DEL", "You do not have permission to delete users." },
114 { "HSMSG_CANNOT_CLVL", "You do not have permission to modify users' access." },
115 { "HSMSG_NO_SELF_CLVL", "You cannot change your own access." },
116 { "HSMSG_NO_BUMP_ACCESS", "You cannot give users the same or more access than yourself." },
117 { "HSMSG_NO_TRANSFER_SELF", "You cannot give ownership to your own account." },
118 { "HSMSG_ADDED_USER", "Added new $b%s$b %s to the user list." },
119 { "HSMSG_DELETED_USER", "Deleted $b%s$b %s from the user list." },
120 { "HSMSG_USER_EXISTS", "$b%s$b is already on the user list." },
121 { "HSMSG_INVALID_ACCESS", "$b%s$b is an invalid access level." },
122 { "HSMSG_CHANGED_ACCESS", "%s now has $b%s$b access." },
123 { "HSMSG_EXPIRATION_DONE", "%d eligible HelpServ bots have retired." },
124 { "HSMSG_BAD_WEEKDAY", "I do not know which day of the week $b%s$b is." },
125 { "HSMSG_WEEK_STARTS", "$b%s$b's weeks start on $b%s$b." },
126
127 /* Registration */
128 { "HSMSG_ILLEGAL_NICK", "$b%s$b is an illegal nick; cannot use it." },
129 { "HSMSG_NICK_EXISTS", "The nick %s is in use by someone else." },
130 { "HSMSG_REG_SUCCESS", "%s now has ownership of bot %s." },
131 { "HSMSG_NEED_UNREG_CONFIRM", "To unregister this bot, you must /msg $S unregister CONFIRM" },
132 { "HSMSG_ERROR_ADDING_SERVICE", "Error creating new user $b%s$b." },
133
134 /* Rename */
135 { "HSMSG_RENAMED", "%s has been renamed to $b%s$b." },
136 { "HSMSG_MOVE_SAME_CHANNEL", "You cannot move %s to the same channel it is on." },
137 { "HSMSG_INVALID_MOVE", "$b%s$b is not a valid nick or channel name." },
138 { "HSMSG_NEED_GIVEOWNERSHIP_CONFIRM", "To transfer ownership of this bot, you must /msg $S giveownership newowner CONFIRM" },
139 { "HSMSG_MULTIPLE_OWNERS", "There is more than one owner of %s; please use other commands to change ownership."},
140 { "HSMSG_NO_TRANSFER_SELF", "You cannot give ownership to your own account." },
141 { "HSMSG_OWNERSHIP_GIVEN", "Ownership of $b%s$b has been transferred to account $b%s$b." },
142
143 /* Queue settings */
144 { "HSMSG_INVALID_OPTION", "$b%s$b is not a valid option." },
145 { "HSMSG_QUEUE_OPTIONS", "HelpServ Queue Options:" },
146 { "HSMSG_SET_COMMAND_TYPE", "$bPageType $b %s" },
147 { "HSMSG_SET_ALERT_TYPE", "$bAlertPageType $b %s" },
148 { "HSMSG_SET_STATUS_TYPE", "$bStatusPageType $b %s" },
149 { "HSMSG_SET_COMMAND_TARGET", "$bPageTarget $b %s" },
150 { "HSMSG_SET_ALERT_TARGET", "$bAlertPageTarget $b %s" },
151 { "HSMSG_SET_STATUS_TARGET", "$bStatusPageTarget$b %s" },
152 { "HSMSG_SET_GREETING", "$bGreeting $b %s" },
153 { "HSMSG_SET_REQOPENED", "$bReqOpened $b %s" },
154 { "HSMSG_SET_REQASSIGNED", "$bReqAssigned $b %s" },
155 { "HSMSG_SET_REQCLOSED", "$bReqClosed $b %s" },
156 { "HSMSG_SET_IDLEDELAY", "$bIdleDelay $b %s" },
157 { "HSMSG_SET_WHINEDELAY", "$bWhineDelay $b %s" },
158 { "HSMSG_SET_WHINEINTERVAL", "$bWhineInterval $b %s" },
159 { "HSMSG_SET_EMPTYINTERVAL", "$bEmptyInterval $b %s" },
160 { "HSMSG_SET_STALEDELAY", "$bStaleDelay $b %s" },
161 { "HSMSG_SET_REQPERSIST", "$bReqPersist $b %s" },
162 { "HSMSG_SET_HELPERPERSIST", "$bHelperPersist $b %s" },
163 { "HSMSG_SET_NOTIFICATION", "$bNotification $b %s" },
164 { "HSMSG_SET_IDWRAP", "$bIDWrap $b %d" },
165 { "HSMSG_SET_REQMAXLEN", "$bReqMaxLen $b %d" },
166 { "HSMSG_SET_PRIVMSGONLY", "$bPrivmsgOnly $b %s" },
167 { "HSMSG_SET_REQONJOIN", "$bReqOnJoin $b %s" },
168 { "HSMSG_SET_AUTOVOICE", "$bAutoVoice $b %s" },
169 { "HSMSG_SET_AUTODEVOICE", "$bAutoDevoice $b %s" },
170 { "HSMSG_SET_JOINTOTAL", "$bJoinTotal $b %s" },
171 { "HSMSG_SET_ALERTNEW", "$bAlertNew $b %s" },
172 { "HSMSG_PAGE_NOTICE", "notice" },
173 { "HSMSG_PAGE_PRIVMSG", "privmsg" },
174 { "HSMSG_PAGE_ONOTICE", "onotice" },
175 { "HSMSG_LENGTH_PART", "part" },
176 { "HSMSG_LENGTH_QUIT", "quit" },
177 { "HSMSG_LENGTH_CLOSE", "close" },
178 { "HSMSG_NOTIFY_DROP", "ReqDrop" },
179 { "HSMSG_NOTIFY_USERCHANGES", "UserChanges" },
180 { "HSMSG_NOTIFY_ACCOUNTCHANGES", "AccountChanges" },
181 { "HSMSG_INVALID_INTERVAL", "Sorry, %s must be at least %s." },
182 { "HSMSG_0_DISABLED", "0 (Disabled)" },
183 { "HSMSG_NEED_MANAGER", "Only managers or higher can do this." },
184 { "HSMSG_SET_NEED_OPER", "This option can only be set by an oper." },
185
186 /* Requests */
187 { "HSMSG_REQ_INVALID", "$b%s$b is not a valid request ID, or there are no requests for that nick or account." },
188 { "HSMSG_REQ_NOT_YOURS_ASSIGNED_TO", "Request $b%lu$b is not assigned to you; it is currently assigned to %s." },
189 { "HSMSG_REQ_NOT_YOURS_UNASSIGNED", "Request $b%lu$b is not assigned to you; it is currently unassigned." },
190 { "HSMSG_REQ_FOUNDMANY", "The user you entered had multiple requests. The oldest one is being used." },
191 { "HSMSG_REQ_CLOSED", "Request $b%lu$b has been closed." },
192 { "HSMSG_REQ_NO_UNASSIGNED", "There are no unassigned requests." },
193 { "HSMSG_USERCMD_NO_REQUEST", "You must have an open request to use a user command." },
194 { "HSMSG_USERCMD_UNKNOWN", "I do not know the user command $b%s$b." },
195 { "HSMSG_REQ_YOU_NOT_IN_HELPCHAN_OPEN", "You cannot open this request as you are not in %s." },
196 { "HSMSG_REQ_YOU_NOT_IN_HELPCHAN", "You cannot be assigned this request as you are not in %s." },
197 { "HSMSG_REQ_HIM_NOT_IN_HELPCHAN", "%s cannot be assigned this request as they are not in %s." },
198 { "HSMSG_REQ_SELF_NOT_IN_OVERRIDE", "You are being assigned this request even though you are not in %s, because the restriction was overridden (you are a manager or owner). If you join and then part, this request will be marked unassigned." },
199 { "HSMSG_REQ_YOU_NOT_IN_OVERRIDE", "Note: You are being assigned this request even though you are not in %s, because the restriction was overridden by a manager or owner. If you join and then part, this request will be marked unassigned." },
200 { "HSMSG_REQ_HIM_NOT_IN_OVERRIDE", "Note: %s is being assigned this request even though they are not in %s, because the restriction was overridden (you are a manager or owner). If they join and then part, this request will be marked unassigned." },
201 { "HSMSG_REQ_ASSIGNED_YOU", "You have been assigned request ID#%lu:" },
202 { "HSMSG_REQ_REASSIGNED", "You have been assigned request ID#%lu (reassigned from %s):" },
203 { "HSMSG_REQ_INFO_1", "Request ID#%lu:" },
204 { "HSMSG_REQ_INFO_2a", " - Nick %s / Account %s" },
205 { "HSMSG_REQ_INFO_2b", " - Nick %s / Not authed" },
206 { "HSMSG_REQ_INFO_2c", " - Online somewhere / Account %s" },
207 { "HSMSG_REQ_INFO_2d", " - Not online / Account %s" },
208 { "HSMSG_REQ_INFO_2e", " - Not online / No account" },
209 { "HSMSG_REQ_INFO_3", " - Opened at %s (%s ago)" },
210 { "HSMSG_REQ_INFO_4", " - Message:" },
211 { "HSMSG_REQ_INFO_MESSAGE", " %s" },
212 { "HSMSG_REQ_ASSIGNED", "Your helper for request ID#%lu is %s (Current nick: %s)" },
213 { "HSMSG_REQ_ASSIGNED_AGAIN", "Your helper for request ID#%lu has been changed to %s (Current nick: %s)" },
214 { "HSMSG_REQ_UNASSIGNED", "Your helper for request ID#%lu has %s, so your request is no longer being handled by them. It has been placed near the front of the unhandled request queue, based on how long ago your request was opened." },
215 { "HSMSG_REQ_NEW", "Your message has been recorded and assigned request ID#%lu. A helper should contact you shortly." },
216 { "HSMSG_REQ_NEWONJOIN", "Welcome to %s. You have been assigned request ID#%lu. A helper should contact you shortly." },
217 { "HSMSG_REQ_ALERT", "A new request (ID#%lu) has been opened in %s. Use /msg %s PICKUP %lu to pickup this request, or use /msg %s NEXT to pickup the oldest request." },
218 { "HSMSG_REQ_UNHANDLED_TIME", "The oldest unhandled request has been waiting for %s." },
219 { "HSMSG_REQ_NO_UNHANDLED", "There are no other unhandled requests." },
220 { "HSMSG_REQ_PERSIST_QUIT", "Everything you tell me until you are helped (or you quit) will be recorded. If you disconnect, your request will be lost." },
221 { "HSMSG_REQ_PERSIST_PART", "Everything you tell me until you are helped (or you leave %s) will be recorded. If you part %s, your request will be lost." },
222 { "HSMSG_REQ_PERSIST_HANDLE", "Everything you tell me until you are helped will be recorded." },
223 { "HSMSG_REQ_MAXLEN", "Sorry, but your request has reached the maximum number of lines. Please wait to be assigned to a helper and continue explaining your request to them." },
224 { "HSMSQ_REQ_TEXT_ADDED", "Message from $b%s:$b %s" },
225 { "HSMSG_REQ_FOUND_ANOTHER", "Request ID#%lu has been closed. $S detected that you also have request ID#%lu open. If you send $S a message, it will be associated with that request." },
226
227 /* Messages that are inserted into request text */
228 { "HSMSG_REQMSG_NOTE_ADDED", "Your note for request ID#%lu has been recorded." },
229
230 /* Automatically generated page messages */
231 { "HSMSG_PAGE_NEW_REQUEST_AUTHED", "New request (ID#%lu) from $b%s$b (Account %s)" },
232 { "HSMSG_PAGE_NEW_REQUEST_UNAUTHED", "New request (ID#%lu) from $b%s$b (Not logged in)" },
233 { "HSMSG_PAGE_UPD_REQUEST_AUTHED", "Request ID#%lu has been updated by $b%s$b (Account %s). Request was initially opened at %s, and was last updated %s ago." },
234 { "HSMSG_PAGE_UPD_REQUEST_NOT_AUTHED", "Request ID#%lu has been updated by $b%s$b (not authed). Request was initially opened at %s, and was last updated %s ago." },
235 { "HSMSG_PAGE_CLOSE_REQUEST_1", "Request ID#%lu from $b%s$b (Account %s) has been closed by %s." },
236 { "HSMSG_PAGE_CLOSE_REQUEST_2", "Request ID#%lu from $b%s$b (Not authed) has been closed by %s." },
237 { "HSMSG_PAGE_CLOSE_REQUEST_3", "Request ID#%lu from an offline user (Account %s) has been closed by %s." },
238 { "HSMSG_PAGE_CLOSE_REQUEST_4", "Request ID#%lu from an offline user (no account) has been closed by %s." },
239 { "HSMSG_PAGE_ASSIGN_REQUEST_1", "Request ID#%lu from $b%s$b (Account %s) has been assigned to %s." },
240 { "HSMSG_PAGE_ASSIGN_REQUEST_2", "Request ID#%lu from $b%s$b (Not authed) has been assigned to %s." },
241 { "HSMSG_PAGE_ASSIGN_REQUEST_3", "Request ID#%lu from an offline user (Account %s) has been assigned to %s." },
242 { "HSMSG_PAGE_ASSIGN_REQUEST_4", "Request ID#%lu from an offline user (no account) has been assigned to %s." },
243 /* The last %s is still an I18N lose. Blame whoever decided to overload it so much. */
244 { "HSMSG_PAGE_HELPER_GONE_1", "Request ID#%lu from $b%s$b (Account %s) $bhas been unassigned$b, as its helper, %s has %s." },
245 { "HSMSG_PAGE_HELPER_GONE_2", "Request ID#%lu from $b%s$b (Not authed) $bhas been unassigned$b, as its helper, %s has %s." },
246 { "HSMSG_PAGE_HELPER_GONE_3", "Request ID#%lu from an offline user (Account %s) $bhas been unassigned$b, as its helper, %s has %s." },
247 { "HSMSG_PAGE_HELPER_GONE_4", "Request ID#%lu from an offline user (No account) $bhas been unassigned$b, as its helper, %s has %s." },
248 { "HSMSG_PAGE_WHINE_HEADER", "$b%u unhandled request(s)$b waiting at least $b%s$b (%u total)" },
249 { "HSMSG_PAGE_IDLE_HEADER", "$b%u users$b in %s $bidle at least %s$b:" },
250 { "HSMSG_PAGE_EMPTYALERT", "$b%s has no helpers present (%u unhandled request(s))$b" },
251 { "HSMSG_PAGE_ONLYTRIALALERT", "$b%s has no full helpers present (%d trial(s) present; %u unhandled request(s))$b" },
252 { "HSMSG_PAGE_FIRSTEMPTYALERT", "$b%s has no helpers present because %s has left (%u unhandled request(s))$b" },
253 { "HSMSG_PAGE_FIRSTONLYTRIALALERT", "$b%s has no full helpers present because %s has left (%d trial(s) present; %u unhandled request(s))$b" },
254 { "HSMSG_PAGE_EMPTYNOMORE", "%s has joined %s; cancelling the \"no helpers present\" alert" },
255
256 /* Notification messages */
257 { "HSMSG_NOTIFY_USER_QUIT", "The user for request ID#%lu, $b%s$b, has disconnected." },
258 { "HSMSG_NOTIFY_USER_MOVE", "The account for request ID#%lu, $b%s$b has been unregistered. It has been associated with user $b%s$b." },
259 { "HSMSG_NOTIFY_USER_NICK", "The user for request ID#%lu has changed their nick from $b%s$b to $b%s$b." },
260 { "HSMSG_NOTIFY_USER_FOUND", "The user for request ID#%lu is now online using nick $b%s$b." },
261 { "HSMSG_NOTIFY_HAND_RENAME", "The account for request ID#%lu has been renamed from $b%s$b to $b%s$b." },
262 { "HSMSG_NOTIFY_HAND_MOVE", "The account for request ID#%lu has been changed to $b%s$b from $b%s$b." },
263 { "HSMSG_NOTIFY_HAND_STUCK", "The user for request ID#%lu, $b%s$b, has re-authenticated to account $b%s$b from $b%s$b, and the request remained associated with the old handle." },
264 { "HSMSG_NOTIFY_HAND_AUTH", "The user for request ID#%lu, $b%s$b, has authenticated to account $b%s$b." },
265 { "HSMSG_NOTIFY_HAND_UNREG", "The account for request ID#%lu, $b%s$b, has been unregistered by $b%s$b." },
266 { "HSMSG_NOTIFY_HAND_MERGE", "The account for request ID#%lu, $b%s$b, has been merged with $b%s$b by %s." },
267 { "HSMSG_NOTIFY_ALLOWAUTH", "The user for request ID#%lu, $b%s$b, has been permitted by %s to authenticate to account $b%s$b without hostmask checking." },
268 { "HSMSG_NOTIFY_UNALLOWAUTH", "The user for request ID#%lu, $b%s$b, has had their ability to authenticate without hostmask checking revoked by %s." },
269 { "HSMSG_NOTIFY_FAILPW", "The user for request ID#%lu, $b%s$b, has attempted to authenticate to account $b%s$b, but used an incorrect password." },
270 { "HSMSG_NOTIFY_REQ_DROP_PART", "Request ID#%lu has been $bdropped$b because %s left the help channel." },
271 { "HSMSG_NOTIFY_REQ_DROP_QUIT", "Request ID#%lu has been $bdropped$b because %s quit IRC." },
272 { "HSMSG_NOTIFY_REQ_DROP_UNREGGED", "Request ID#%lu (account %s) has been $bdropped$b because the account was unregistered." },
273
274 /* Presence and request-related messages */
275 { "HSMSG_REQ_DROPPED_PART", "You have left $b%s$b. Your help request (ID#%lu) has been deleted." },
276 { "HSMSG_REQ_WARN_UNREG", "The account you were authenticated to ($b%s$b) has been unregistered. Therefore unless you register a new handle, or authenticate to another one, if you disconnect, your HelpServ $S (%s) request ID#%lu will be lost." },
277 { "HSMSG_REQ_DROPPED_UNREG", "By unregistering the account $b%s$b, HelpServ $S (%s) request ID#%lu was dropped, as there was nobody online to associate the request with." },
278 { "HSMSG_REQ_ASSIGNED_UNREG", "As the account $b%s$b was unregistered, HelpServ $S (%s) request ID#%lu was associated with you, as you were authenticated to that account. If you disconnect, then this request will be lost forever." },
279 { "HSMSG_REQ_AUTH_STUCK", "By authenticating to account $b%s$b, HelpServ $S (%s) request ID#%lu remained with the previous account $b%s$b (because the request will remain until closed). This means that if you send $S a message, it $uwill not$u be associated with this request." },
280 { "HSMSG_REQ_AUTH_MOVED", "By authenticating to account $b%s$b, HelpServ $S (%s) request ID#%lu ceased to be associated with your previously authenticated account ($b%s$b), and was transferred to your new account (because the request will only remain until you %s). This means that if you send $S a message, it $uwill$u be associated with this request." },
281
282 /* Lists */
283 { "HSMSG_BOTLIST_HEADER", "$bCurrent HelpServ bots:$b" },
284 { "HSMSG_USERLIST_HEADER", "$b%s users:$b" },
285 { "HSMSG_USERLIST_ZOOT_LVL", "%s $b%ss$b:" },
286 { "HSMSG_REQLIST_AUTH", "You are currently assigned these requests:" },
287 { "HSMSG_REQ_LIST_TOP_UNASSIGNED", "Listing $ball unassigned$b requests (%d in list)." },
288 { "HSMSG_REQ_LIST_TOP_ASSIGNED", "Listing $ball assigned$b requests (%d in list)." },
289 { "HSMSG_REQ_LIST_TOP_YOUR", "Listing $byour$b requests (%d in list)." },
290 { "HSMSG_REQ_LIST_TOP_ALL", "Listing $ball$b requests (%d in list)." },
291 { "HSMSG_REQ_LIST_NONE", "There are no matching requests." },
292 { "HSMSG_STATS_TOP", "Stats for %s user $b%s$b (week starts %s):" },
293 { "HSMSG_STATS_TIME", "$uTime spent helping in %s:$u" },
294 { "HSMSG_STATS_REQS", "$uRequest activity statistics:$u" },
295
296 /* Status report headers */
297 { "HSMSG_STATS_REPORT_0", "Stats report for current week" },
298 { "HSMSG_STATS_REPORT_1", "Stats report for one week ago" },
299 { "HSMSG_STATS_REPORT_2", "Stats report for two weeks ago" },
300 { "HSMSG_STATS_REPORT_3", "Stats report for three weeks ago" },
301
302 /* Help */
303 { "HSMSG_HELP_COMMAND_ALIAS", "$uAlias for:$u %s" },
304 { "HSMSG_HELP_COMMAND_HEADER", "Command help for: $b%s$b" },
305 { "HSMSG_HELP_COMMAND_UNKNOWN", "No help available for that command." },
306 { "HSMSG_HELP_TOPIC_HEADER", "Help topic: $b%s$b" },
307 { "HSMSG_HELP_DIVIDER", "=---------------------------------------=" },
308 { "HSMSG_HELP_FOOTER", "=------------- End of Help -------------=" },
309 { "HSMSG_COMMAND_BINDING", "%s is a binding of: %s" },
310
311 /* Channel [un]suspension */
312 { "HSMSG_ALREADY_SUSPENDED", "$b%s$b is already suspended." },
313 { "HSMSG_NOT_SUSPENDED", "$b%s$b is not suspended." },
314 { "HSMSG_SUSPENDED", "$b$S$b access to $b%s$b has been temporarily suspended." },
315 { "HSMSG_UNSUSPENDED", "$b$S$b access to $b%s$b has been restored." },
316 { "HSMSG_SUSPEND_NODELETE", "$b%s$b is protected from unregistration, and cannot be suspended." },
317 { "HSMSG_USER_SUSPENDED", "$b%s$b's access to $b%s$b has been suspended." },
318 { "HSMSG_USER_UNSUSPENDED", "$b%s$b's access to $b%s$b has been restored." },
319 { "HSMSG_BOT_NON_EXIST", "HelpServ bot %s does not exist." },
320
321 /* Responses to user commands */
322 { "HSMSG_YOU_BEING_HELPED", "You are already being helped." },
323 { "HSMSG_YOU_BEING_HELPED_BY", "You are already being helped by $b%s$b." },
324 { "HSMSG_WAIT_STATUS", "You are %d of %d in line; the first person has waited %s." },
325 { "HSMSG_NO_HELPER", "No one is currently helping you, please wait for a helper to pickup your request." },
326 { "HSMSG_HELPER", "You are currently being helped by %s." },
327 { NULL, NULL }
328 };
329
330 enum helpserv_level {
331 HlNone,
332 HlTrial,
333 HlHelper,
334 HlManager,
335 HlOwner,
336 HlOper,
337 HlCount
338 };
339
340 static const char *helpserv_level_names[] = {
341 "None",
342 "Trial",
343 "Helper",
344 "Manager",
345 "Owner",
346 "Oper",
347 NULL
348 };
349
350 enum page_type {
351 PAGE_NONE,
352 PAGE_NOTICE,
353 PAGE_PRIVMSG,
354 PAGE_ONOTICE,
355 PAGE_COUNT
356 };
357
358 static const struct {
359 char *db_name;
360 char *print_name;
361 irc_send_func func;
362 } page_types[] = {
363 { "none", "MSG_NONE", NULL },
364 { "notice", "HSMSG_PAGE_NOTICE", irc_notice },
365 { "privmsg", "HSMSG_PAGE_PRIVMSG", irc_privmsg },
366 { "onotice", "HSMSG_PAGE_ONOTICE", irc_wallchops },
367 { NULL, NULL, NULL }
368 };
369
370 enum page_source {
371 PGSRC_COMMAND,
372 PGSRC_ALERT,
373 PGSRC_STATUS,
374 PGSRC_COUNT
375 };
376
377 static const struct {
378 char *db_name;
379 char *print_target;
380 char *print_type;
381 } page_sources[] = {
382 { "command", "HSMSG_SET_COMMAND_TARGET", "HSMSG_SET_COMMAND_TYPE" },
383 { "alert", "HSMSG_SET_ALERT_TARGET", "HSMSG_SET_ALERT_TYPE" },
384 { "status", "HSMSG_SET_STATUS_TARGET", "HSMSG_SET_STATUS_TYPE" },
385 { NULL, NULL, NULL }
386 };
387
388 enum message_type {
389 MSGTYPE_GREETING,
390 MSGTYPE_REQ_OPENED,
391 MSGTYPE_REQ_ASSIGNED,
392 MSGTYPE_REQ_CLOSED,
393 MSGTYPE_REQ_DROPPED,
394 MSGTYPE_COUNT
395 };
396
397 static const struct {
398 char *db_name;
399 char *print_name;
400 } message_types[] = {
401 { "greeting", "HSMSG_SET_GREETING" },
402 { "reqopened", "HSMSG_SET_REQOPENED" },
403 { "reqassigned", "HSMSG_SET_REQASSIGNED" },
404 { "reqclosed", "HSMSG_SET_REQCLOSED" },
405 { "reqdropped", "HSMSG_SET_REQDROPPED" },
406 { NULL, NULL }
407 };
408
409 enum interval_type {
410 INTERVAL_IDLE_DELAY,
411 INTERVAL_WHINE_DELAY,
412 INTERVAL_WHINE_INTERVAL,
413 INTERVAL_EMPTY_INTERVAL,
414 INTERVAL_STALE_DELAY,
415 INTERVAL_COUNT
416 };
417
418 static const struct {
419 char *db_name;
420 char *print_name;
421 } interval_types[] = {
422 { "idledelay", "HSMSG_SET_IDLEDELAY" },
423 { "whinedelay", "HSMSG_SET_WHINEDELAY" },
424 { "whineinterval", "HSMSG_SET_WHINEINTERVAL" },
425 { "emptyinterval", "HSMSG_SET_EMPTYINTERVAL" },
426 { "staledelay", "HSMSG_SET_STALEDELAY" },
427 { NULL, NULL }
428 };
429
430 enum persistence_type {
431 PERSIST_T_REQUEST,
432 PERSIST_T_HELPER,
433 PERSIST_T_COUNT
434 };
435
436 static const struct {
437 char *db_name;
438 char *print_name;
439 } persistence_types[] = {
440 { "reqpersist", "HSMSG_SET_REQPERSIST" },
441 { "helperpersist", "HSMSG_SET_HELPERPERSIST" },
442 { NULL, NULL }
443 };
444
445 enum persistence_length {
446 PERSIST_PART,
447 PERSIST_QUIT,
448 PERSIST_CLOSE,
449 PERSIST_COUNT
450 };
451
452 static const struct {
453 char *db_name;
454 char *print_name;
455 } persistence_lengths[] = {
456 { "part", "HSMSG_LENGTH_PART" },
457 { "quit", "HSMSG_LENGTH_QUIT" },
458 { "close", "HSMSG_LENGTH_CLOSE" },
459 { NULL, NULL }
460 };
461
462 enum notification_type {
463 NOTIFY_NONE,
464 NOTIFY_DROP,
465 NOTIFY_USER,
466 NOTIFY_HANDLE,
467 NOTIFY_COUNT
468 };
469
470 static const struct {
471 char *db_name;
472 char *print_name;
473 } notification_types[] = {
474 { "none", "MSG_NONE" },
475 { "reqdrop", "HSMSG_NOTIFY_DROP" },
476 { "userchanges", "HSMSG_NOTIFY_USERCHANGES" },
477 { "accountchanges", "HSMSG_NOTIFY_ACCOUNTCHANGES" },
478 { NULL, NULL }
479 };
480
481 static const char *weekday_names[] = {
482 "Sunday",
483 "Monday",
484 "Tuesday",
485 "Wednesday",
486 "Thursday",
487 "Friday",
488 "Saturday",
489 NULL
490 };
491
492 static const char *statsreport_week[] = {
493 "HSMSG_STATS_REPORT_0",
494 "HSMSG_STATS_REPORT_1",
495 "HSMSG_STATS_REPORT_2",
496 "HSMSG_STATS_REPORT_3"
497 };
498
499 static struct {
500 const char *description;
501 const char *reqlogfile;
502 unsigned long db_backup_frequency;
503 unsigned int expire_age;
504 char user_escape;
505 } helpserv_conf;
506
507 static time_t last_stats_update;
508 static int shutting_down;
509 static FILE *reqlog_f;
510 static struct log_type *HS_LOG;
511
512 #define CMD_NEED_BOT 0x001
513 #define CMD_NOT_OVERRIDE 0x002
514 #define CMD_FROM_OPSERV_ONLY 0x004
515 #define CMD_IGNORE_EVENT 0x008
516 #define CMD_NEVER_FROM_OPSERV 0x010
517
518 struct helpserv_bot {
519 struct userNode *helpserv;
520
521 struct chanNode *helpchan;
522
523 struct chanNode *page_targets[PGSRC_COUNT];
524 enum page_type page_types[PGSRC_COUNT];
525 char *messages[MSGTYPE_COUNT];
526 unsigned long intervals[INTERVAL_COUNT];
527 enum notification_type notify;
528
529 /* This is a default; it can be changed on a per-request basis */
530 enum persistence_type persist_types[PERSIST_T_COUNT];
531
532 dict_t users; /* indexed by handle */
533
534 struct helpserv_request *unhandled; /* linked list of unhandled requests */
535 dict_t requests; /* indexed by request id */
536 unsigned long last_requestid;
537 unsigned long id_wrap;
538 unsigned long req_maxlen; /* Maxmimum request length in lines */
539
540 unsigned int privmsg_only : 1;
541 unsigned int req_on_join : 1;
542 unsigned int auto_voice : 1;
543 unsigned int auto_devoice : 1;
544 unsigned int join_total : 1;
545 unsigned int alert_new : 1;
546
547 unsigned int helpchan_empty : 1;
548
549 unsigned int suspended : 1;
550 time_t expiry, issued;
551 char *suspender;
552 char *reason;
553
554 time_t registered;
555 time_t last_active;
556 char *registrar;
557 };
558
559 struct helpserv_user {
560 struct handle_info *handle;
561 struct helpserv_bot *hs;
562 unsigned int help_mode : 1;
563 unsigned int week_start : 3;
564 enum helpserv_level level;
565 /* statistics */
566 time_t join_time; /* when they joined, or 0 if not in channel */
567 /* [0] through [3] are n weeks ago, [4] is the total of everything before that */
568 unsigned int time_per_week[5]; /* how long they've were in the channel the past 4 weeks */
569 unsigned int picked_up[5]; /* how many requests they have picked up */
570 unsigned int closed[5]; /* how many requests they have closed */
571 unsigned int reassigned_from[5]; /* how many requests reassigned from them to others */
572 unsigned int reassigned_to[5]; /* how many requests reassigned from others to them */
573 };
574
575 struct helpserv_request {
576 struct helpserv_user *helper;
577 struct helpserv_bot *hs;
578 struct string_list *text;
579 struct helpserv_request *next_unhandled;
580
581 struct helpserv_reqlist *parent_nick_list;
582 struct helpserv_reqlist *parent_hand_list;
583
584 /* One, but not both, of "user" and "handle" may be NULL,
585 * depending on what we know about the user.
586 *
587 * If persist == PERSIST_CLOSE when the user quits, then it
588 * switches to handle instead of user... and stays that way (it's
589 * possible to have >1 nick per handle, so you can't really decide
590 * to reassign a userNode to it unless they send another message
591 * to HelpServ).
592 */
593 struct userNode *user;
594 struct handle_info *handle;
595
596 unsigned long id;
597 time_t opened;
598 time_t assigned;
599 time_t updated;
600 };
601
602 #define DEFINE_LIST_ALLOC(STRUCTNAME) \
603 struct STRUCTNAME * STRUCTNAME##_alloc() {\
604 struct STRUCTNAME *newlist; \
605 newlist = malloc(sizeof(struct STRUCTNAME)); \
606 STRUCTNAME##_init(newlist); \
607 return newlist; \
608 }\
609 void STRUCTNAME##_free(void *data) {\
610 struct STRUCTNAME *list = data; /* void * to let dict_set_free_data use it */ \
611 STRUCTNAME##_clean(list); \
612 free(list); \
613 }
614
615 DECLARE_LIST(helpserv_botlist, struct helpserv_bot *);
616 DEFINE_LIST(helpserv_botlist, struct helpserv_bot *);
617 DEFINE_LIST_ALLOC(helpserv_botlist);
618
619 DECLARE_LIST(helpserv_reqlist, struct helpserv_request *);
620 DEFINE_LIST(helpserv_reqlist, struct helpserv_request *);
621 DEFINE_LIST_ALLOC(helpserv_reqlist);
622
623 DECLARE_LIST(helpserv_userlist, struct helpserv_user *);
624 DEFINE_LIST(helpserv_userlist, struct helpserv_user *);
625 DEFINE_LIST_ALLOC(helpserv_userlist);
626
627 struct helpfile *helpserv_helpfile;
628 static struct module *helpserv_module;
629 static dict_t helpserv_func_dict;
630 static dict_t helpserv_usercmd_dict; /* contains helpserv_usercmd_t */
631 static dict_t helpserv_option_dict;
632 static dict_t helpserv_bots_dict; /* indexed by nick */
633 static dict_t helpserv_bots_bychan_dict; /* indexed by chan, holds a struct helpserv_botlist */
634 /* QUESTION: why are these outside of any helpserv_bot struct? */
635 static dict_t helpserv_reqs_bynick_dict; /* indexed by nick, holds a struct helpserv_reqlist */
636 static dict_t helpserv_reqs_byhand_dict; /* indexed by handle, holds a struct helpserv_reqlist */
637 static dict_t helpserv_users_byhand_dict; /* indexed by handle, holds a struct helpserv_userlist */
638
639 /* This is so that overrides can "speak" from opserv */
640 extern struct userNode *opserv;
641
642 #define HELPSERV_SYNTAX() helpserv_help(hs, from_opserv, user, argv[0], 0)
643 #define HELPSERV_FUNC(NAME) int NAME(struct userNode *user, UNUSED_ARG(struct helpserv_bot *hs), int from_opserv, UNUSED_ARG(unsigned int argc), UNUSED_ARG(char *argv[]))
644 typedef HELPSERV_FUNC(helpserv_func_t);
645 #define HELPSERV_USERCMD(NAME) int NAME(UNUSED_ARG(struct helpserv_request *req), UNUSED_ARG(struct userNode *likely_helper), UNUSED_ARG(char *args), UNUSED_ARG(int argc), UNUSED_ARG(char *argv[]), UNUSED_ARG(struct userNode *user), UNUSED_ARG(struct helpserv_bot *hs))
646 typedef HELPSERV_USERCMD(helpserv_usercmd_t);
647 #define HELPSERV_OPTION(NAME) HELPSERV_FUNC(NAME)
648 typedef HELPSERV_OPTION(helpserv_option_func_t);
649
650 static HELPSERV_FUNC(cmd_help);
651
652 #define REQUIRE_PARMS(N) if (argc < N) { \
653 helpserv_notice(user, "MSG_MISSING_PARAMS", argv[0]); \
654 HELPSERV_SYNTAX(); \
655 return 0; }
656
657 /* For messages going to users being helped */
658 #define helpserv_msguser(target, format...) send_message_type((from_opserv ? 0 : hs->privmsg_only), (target), (from_opserv ? opserv : hs->helpserv) , ## format)
659 #define helpserv_user_reply(format...) send_message_type(req->hs->privmsg_only, req->user, req->hs->helpserv , ## format)
660 /* For messages going to helpers */
661 #define helpserv_notice(target, format...) send_message((target), (from_opserv ? opserv : hs->helpserv) , ## format)
662 #define helpserv_notify(helper, format...) do { struct userNode *_target; for (_target = (helper)->handle->users; _target; _target = _target->next_authed) { \
663 send_message(_target, (helper)->hs->helpserv, ## format); \
664 } } while (0)
665 #define helpserv_message(hs, target, id) do { if ((hs)->messages[id]) { \
666 if (from_opserv) \
667 send_message_type(4, (target), opserv, "%s", (hs)->messages[id]); \
668 else \
669 send_message_type(4 | hs->privmsg_only, (target), hs->helpserv, "%s", (hs)->messages[id]); \
670 } } while (0)
671 #define helpserv_page(TYPE, FORMAT...) do { \
672 struct chanNode *target=NULL; int msg_type=0; \
673 target = hs->page_targets[TYPE]; \
674 switch (hs->page_types[TYPE]) { \
675 case PAGE_NOTICE: msg_type = 0; break; \
676 case PAGE_PRIVMSG: msg_type = 1; break; \
677 case PAGE_ONOTICE: msg_type = 2; break; \
678 default: log_module(HS_LOG, LOG_ERROR, "helpserv_page() called but %s has an invalid page type %d.", hs->helpserv->nick, TYPE); \
679 case PAGE_NONE: target = NULL; break; \
680 } \
681 if (target) send_target_message(msg_type, target->name, hs->helpserv, ## FORMAT); \
682 } while (0)
683 #define helpserv_get_handle_info(user, text) smart_get_handle_info((from_opserv ? opserv : hs->helpserv) , (user), (text))
684
685 struct helpserv_cmd {
686 enum helpserv_level access;
687 helpserv_func_t *func;
688 double weight;
689 long flags;
690 };
691
692 static void run_empty_interval(void *data);
693
694 static void helpserv_interval(char *output, time_t interval) {
695 int num_hours = interval / 3600;
696 int num_minutes = (interval % 3600) / 60;
697 sprintf(output, "%u hour%s, %u minute%s", num_hours, num_hours == 1 ? "" : "s", num_minutes, num_minutes == 1 ? "" : "s");
698 }
699
700 static const char * helpserv_level2str(enum helpserv_level level) {
701 if (level <= HlOper) {
702 return helpserv_level_names[level];
703 } else {
704 log_module(HS_LOG, LOG_ERROR, "helpserv_level2str receieved invalid level %d.", level);
705 return "Error";
706 }
707 }
708
709 static enum helpserv_level helpserv_str2level(const char *msg) {
710 enum helpserv_level nn;
711 for (nn=HlNone; nn<=HlOper; nn++) {
712 if (!irccasecmp(msg, helpserv_level_names[nn]))
713 return nn;
714 }
715 log_module(HS_LOG, LOG_ERROR, "helpserv_str2level received invalid level %s.", msg);
716 return HlNone; /* Error */
717 }
718
719 static struct helpserv_user *GetHSUser(struct helpserv_bot *hs, struct handle_info *hi) {
720 return dict_find(hs->users, hi->handle, NULL);
721 }
722
723 static void helpserv_log_request(struct helpserv_request *req, const char *reason) {
724 char key[27+NICKLEN];
725 char userhost[USERLEN+HOSTLEN+2];
726 struct saxdb_context *ctx;
727 int res;
728
729 assert(req != NULL);
730 assert(reason != NULL);
731 if (!(ctx = saxdb_open_context(reqlog_f)))
732 return;
733 sprintf(key, "%s-" FMT_TIME_T "-%lu", req->hs->helpserv->nick, req->opened, req->id);
734 if ((res = setjmp(ctx->jbuf)) != 0) {
735 log_module(HS_LOG, LOG_ERROR, "Unable to log helpserv request: %s.", strerror(res));
736 } else {
737 saxdb_start_record(ctx, key, 1);
738 if (req->helper) {
739 saxdb_write_string(ctx, KEY_REQUEST_HELPER, req->helper->handle->handle);
740 saxdb_write_int(ctx, KEY_REQUEST_ASSIGNED, req->assigned);
741 }
742 if (req->handle) {
743 saxdb_write_string(ctx, KEY_REQUEST_HANDLE, req->handle->handle);
744 }
745 if (req->user) {
746 saxdb_write_string(ctx, KEY_REQUEST_NICK, req->user->nick);
747 sprintf(userhost, "%s@%s", req->user->ident, req->user->hostname);
748 saxdb_write_string(ctx, KEY_REQUEST_USERHOST, userhost);
749 }
750 saxdb_write_int(ctx, KEY_REQUEST_OPENED, req->opened);
751 saxdb_write_int(ctx, KEY_REQUEST_CLOSED, now);
752 saxdb_write_string(ctx, KEY_REQUEST_CLOSEREASON, reason);
753 saxdb_write_string_list(ctx, KEY_REQUEST_TEXT, req->text);
754 saxdb_end_record(ctx);
755 saxdb_close_context(ctx);
756 fflush(reqlog_f);
757 }
758 }
759
760 /* Searches for a request by number, nick, or account (num|nick|*account).
761 * As there can potentially be >1 match, it takes a reqlist. The return
762 * value is the "best" request found (explained in the comment block below).
763 *
764 * If num_results is not NULL, it is set to the number of potentially matching
765 * requests.
766 * If hs_user is not NULL, requests assigned to this helper will be given
767 * preference (oldest assigned, falling back to oldest if there are none).
768 */
769 static struct helpserv_request * smart_get_request(struct helpserv_bot *hs, struct helpserv_user *hs_user, const char *needle, int *num_results) {
770 struct helpserv_reqlist *reqlist, resultlist;
771 struct helpserv_request *req, *oldest=NULL, *oldest_assigned=NULL;
772 struct userNode *user;
773 unsigned int i;
774
775 if (num_results)
776 *num_results = 0;
777
778 if (*needle == '*') {
779 /* This test (handle) requires the least processing, so it's first */
780 if (!(reqlist = dict_find(helpserv_reqs_byhand_dict, needle+1, NULL)))
781 return NULL;
782 helpserv_reqlist_init(&resultlist);
783 for (i=0; i < reqlist->used; i++) {
784 req = reqlist->list[i];
785 if (req->hs == hs) {
786 helpserv_reqlist_append(&resultlist, req);
787 if (num_results)
788 (*num_results)++;
789 }
790 }
791 } else if (!needle[strspn(needle, "0123456789")]) {
792 /* The string is 100% numeric - a request id */
793 if (!(req = dict_find(hs->requests, needle, NULL)))
794 return NULL;
795 if (num_results)
796 *num_results = 1;
797 return req;
798 } else if ((user = GetUserH(needle))) {
799 /* And finally, search by nick */
800 if (!(reqlist = dict_find(helpserv_reqs_bynick_dict, needle, NULL)))
801 return NULL;
802 helpserv_reqlist_init(&resultlist);
803
804 for (i=0; i < reqlist->used; i++) {
805 req = reqlist->list[i];
806 if (req->hs == hs) {
807 helpserv_reqlist_append(&resultlist, req);
808 if (num_results)
809 (*num_results)++;
810 }
811 }
812 /* If the nick didn't have anything, try their handle */
813 if (!resultlist.used && user->handle_info) {
814 char star_handle[NICKSERV_HANDLE_LEN+2];
815
816 helpserv_reqlist_clean(&resultlist);
817 sprintf(star_handle, "*%s", user->handle_info->handle);
818
819 return smart_get_request(hs, hs_user, star_handle, num_results);
820 }
821 } else {
822 return NULL;
823 }
824
825 if (resultlist.used == 0) {
826 helpserv_reqlist_clean(&resultlist);
827 return NULL;
828 } else if (resultlist.used == 1) {
829 req = resultlist.list[0];
830 helpserv_reqlist_clean(&resultlist);
831 return req;
832 }
833
834 /* In case there is >1 request returned, use the oldest one assigned to
835 * the helper executing the command. Otherwise, use the oldest request.
836 * This may not be the intended result for cmd_pickup (first unhandled
837 * request may be better), or cmd_reassign (first handled request), but
838 * it's close enough, and there really aren't supposed to be multiple
839 * requests per person anyway; they're just side effects of authing. */
840
841 for (i=0; i < resultlist.used; i++) {
842 req = resultlist.list[i];
843 if (!oldest || req->opened < oldest->opened)
844 oldest = req;
845 if (hs_user && (!oldest_assigned || (req->helper == hs_user && req->opened < oldest_assigned->opened)))
846 oldest_assigned = req;
847 }
848
849 helpserv_reqlist_clean(&resultlist);
850
851 return oldest_assigned ? oldest_assigned : oldest;
852 }
853
854 static struct helpserv_request * create_request(struct userNode *user, struct helpserv_bot *hs, int from_join) {
855 struct helpserv_request *req = calloc(1, sizeof(struct helpserv_request));
856 char lbuf[3][MAX_LINE_SIZE], unh[INTERVALLEN], abuf[1][MAX_LINE_SIZE];
857 struct helpserv_reqlist *reqlist, *hand_reqlist;
858 struct helpserv_user *hs_user;
859 struct userNode *target, *next_un = NULL;
860 const unsigned int from_opserv = 0;
861 const char *fmt, *afmt;
862 dict_iterator_t it;
863
864 assert(req);
865
866 req->id = ++hs->last_requestid;
867 sprintf(unh, "%lu", req->id);
868 dict_insert(hs->requests, strdup(unh), req);
869
870 if (hs->id_wrap) {
871 unsigned long i;
872 char buf[12];
873 if (hs->last_requestid < hs->id_wrap) {
874 for (i=hs->last_requestid; i < hs->id_wrap; i++) {
875 sprintf(buf, "%lu", i);
876 if (!dict_find(hs->requests, buf, NULL)) {
877 hs->last_requestid = i-1;
878 break;
879 }
880 }
881 }
882 if (hs->last_requestid >= hs->id_wrap) {
883 for (i=1; i < hs->id_wrap; i++) {
884 sprintf(buf, "%lu", i);
885 if (!dict_find(hs->requests, buf, NULL)) {
886 hs->last_requestid = i-1;
887 break;
888 }
889 }
890 if (i >= hs->id_wrap) {
891 log_module(HS_LOG, LOG_INFO, "%s has more requests than its id_wrap.", hs->helpserv->nick);
892 }
893 }
894 }
895
896 req->hs = hs;
897 req->helper = NULL;
898 req->text = alloc_string_list(4);
899 req->user = user;
900 req->handle = user->handle_info;
901 if (from_join && self->burst) {
902 extern time_t burst_begin;
903 /* We need to keep all the requests during a burst join together,
904 * even if the burst takes more than 1 second. ircu seems to burst
905 * in reverse-join order. */
906 req->opened = burst_begin;
907 } else {
908 req->opened = now;
909 }
910 req->updated = now;
911
912 if (!hs->unhandled) {
913 hs->unhandled = req;
914 req->next_unhandled = NULL;
915 } else if (self->burst && hs->unhandled->opened >= req->opened) {
916 req->next_unhandled = hs->unhandled;
917 hs->unhandled = req;
918 } else if (self->burst) {
919 struct helpserv_request *unh;
920 /* Add the request to the beginning of the set of requests with
921 * req->opened having the same value. This makes reqonjoin create
922 * requests in the correct order while bursting. Note that this
923 * does not correct request ids, so they will be in reverse order
924 * though "/msg botname next" will work properly. */
925 for (unh = hs->unhandled; unh->next_unhandled && unh->next_unhandled->opened < req->opened; unh = unh->next_unhandled) ;
926 req->next_unhandled = unh->next_unhandled;
927 unh->next_unhandled = req;
928 } else {
929 struct helpserv_request *unh;
930 /* Add to the end */
931 for (unh = hs->unhandled; unh->next_unhandled; unh = unh->next_unhandled) ;
932 req->next_unhandled = NULL;
933 unh->next_unhandled = req;
934 }
935
936 if (!(reqlist = dict_find(helpserv_reqs_bynick_dict, user->nick, NULL))) {
937 reqlist = helpserv_reqlist_alloc();
938 dict_insert(helpserv_reqs_bynick_dict, user->nick, reqlist);
939 }
940 req->parent_nick_list = reqlist;
941 helpserv_reqlist_append(reqlist, req);
942
943 if (user->handle_info) {
944 if (!(hand_reqlist = dict_find(helpserv_reqs_byhand_dict, user->handle_info->handle, NULL))) {
945 hand_reqlist = helpserv_reqlist_alloc();
946 dict_insert(helpserv_reqs_byhand_dict, user->handle_info->handle, hand_reqlist);
947 }
948 req->parent_hand_list = hand_reqlist;
949 helpserv_reqlist_append(hand_reqlist, req);
950 } else {
951 req->parent_hand_list = NULL;
952 }
953
954 if (from_join) {
955 fmt = user_find_message(user, "HSMSG_REQ_NEWONJOIN");
956 sprintf(lbuf[0], fmt, hs->helpchan->name, req->id);
957 } else {
958 fmt = user_find_message(user, "HSMSG_REQ_NEW");
959 sprintf(lbuf[0], fmt, req->id);
960 }
961
962 if (req != hs->unhandled) {
963 intervalString(unh, now - hs->unhandled->opened, user->handle_info);
964 fmt = user_find_message(user, "HSMSG_REQ_UNHANDLED_TIME");
965 sprintf(lbuf[1], fmt, unh);
966 } else {
967 fmt = user_find_message(user, "HSMSG_REQ_NO_UNHANDLED");
968 sprintf(lbuf[1], fmt);
969 }
970
971 if (hs->alert_new) {
972 for (it = dict_first(hs->users); it; it = iter_next(it)) {
973 hs_user = iter_data(it);
974
975 afmt = user_find_message(user, "HSMSG_REQ_ALERT");
976 sprintf(abuf[0], afmt, req->id, hs->helpchan->name, hs->helpserv->nick, req->id, hs->helpserv->nick);
977 for (target = hs_user->handle->users; target; target = next_un) {
978 send_message_type(4, target, hs->helpserv, "%s", abuf[0]);
979 next_un = target->next_authed;
980 }
981 }
982 }
983
984 switch (hs->persist_types[PERSIST_T_REQUEST]) {
985 case PERSIST_PART:
986 fmt = user_find_message(user, "HSMSG_REQ_PERSIST_PART");
987 sprintf(lbuf[2], fmt, hs->helpchan->name, hs->helpchan->name);
988 break;
989 case PERSIST_QUIT:
990 fmt = user_find_message(user, "HSMSG_REQ_PERSIST_QUIT");
991 sprintf(lbuf[2], fmt);
992 break;
993 default:
994 log_module(HS_LOG, LOG_ERROR, "%s has an invalid req_persist.", hs->helpserv->nick);
995 case PERSIST_CLOSE:
996 if (user->handle_info) {
997 fmt = user_find_message(user, "HSMSG_REQ_PERSIST_HANDLE");
998 sprintf(lbuf[2], fmt);
999 } else {
1000 fmt = user_find_message(user, "HSMSG_REQ_PERSIST_QUIT");
1001 sprintf(lbuf[2], fmt);
1002 }
1003 break;
1004 }
1005 helpserv_message(hs, user, MSGTYPE_REQ_OPENED);
1006 if (from_opserv)
1007 send_message_type(4, user, opserv, "%s %s %s", lbuf[0], lbuf[1], lbuf[2]);
1008 else
1009 send_message_type(4, user, hs->helpserv, "%s %s %s", lbuf[0], lbuf[1], lbuf[2]);
1010
1011 if (hs->req_on_join && req == hs->unhandled && hs->helpchan_empty && !user->uplink->burst) {
1012 timeq_del(0, run_empty_interval, hs, TIMEQ_IGNORE_WHEN);
1013 run_empty_interval(hs);
1014 }
1015
1016 return req;
1017 }
1018
1019 /* Handle a message from a user to a HelpServ bot. */
1020 static void helpserv_usermsg(struct userNode *user, struct helpserv_bot *hs, char *text, char *argv[], int argc) {
1021 const int from_opserv = 0; /* for helpserv_notice */
1022 struct helpserv_request *req=NULL, *newest=NULL;
1023 struct helpserv_reqlist *reqlist, *hand_reqlist;
1024 unsigned int i;
1025
1026 if(argc < 1)
1027 return;
1028 if ((reqlist = dict_find(helpserv_reqs_bynick_dict, user->nick, NULL))) {
1029 for (i=0; i < reqlist->used; i++) {
1030 req = reqlist->list[i];
1031 if (req->hs != hs)
1032 continue;
1033 /* if (!newest || (newest->opened < req->opened)) ?? XXX: this is a noop.. commenting it out -rubin */
1034 /* newest is set to null 9 lines up, and isnt changed between.. */
1035 newest = req;
1036 }
1037
1038 /* If nothing was found, this will set req to NULL */
1039 req = newest;
1040 }
1041
1042 if (user->handle_info) {
1043 hand_reqlist = dict_find(helpserv_reqs_byhand_dict, user->handle_info->handle, NULL);
1044 if (!req && hand_reqlist) {
1045 /* Most recent first again */
1046 for (i=0; i < hand_reqlist->used; i++) {
1047 req = hand_reqlist->list[i];
1048 if ((req->hs != hs) || req->user)
1049 continue;
1050 if (!newest || (newest->opened < req->opened))
1051 newest = req;
1052 }
1053 req = newest;
1054
1055 if (req) {
1056 req->user = user;
1057 if (!reqlist) {
1058 reqlist = helpserv_reqlist_alloc();
1059 dict_insert(helpserv_reqs_bynick_dict, user->nick, reqlist);
1060 }
1061 req->parent_nick_list = reqlist;
1062 helpserv_reqlist_append(reqlist, req);
1063
1064 if (req->helper && (hs->notify >= NOTIFY_USER))
1065 helpserv_notify(req->helper, "HSMSG_NOTIFY_USER_FOUND", req->id, user->nick);
1066
1067 helpserv_msguser(user, "HSMSG_GREET_PRIVMSG_EXISTREQ", req->id);
1068 }
1069 }
1070 }
1071
1072 if (!req) {
1073 if (argv[1][0] == helpserv_conf.user_escape) {
1074 char *cmdname;
1075 helpserv_usercmd_t *usercmd;
1076 struct userNode *likely_helper;
1077
1078 /* Allow HELP user command but no other if no open request,
1079 this may change in the future if we make commands that
1080 dont need an open request */
1081
1082 /* Find somebody likely to be the helper */
1083 likely_helper = NULL;
1084
1085 cmdname = argv[1]+1;
1086
1087 /* Call the user command function */
1088 usercmd = dict_find(helpserv_usercmd_dict, cmdname, NULL);
1089 if (usercmd && !strcasecmp(cmdname, "HELP"))
1090 usercmd(req, likely_helper, NULL, argc, argv, user, hs);
1091 else
1092 helpserv_msguser(user, "HSMSG_USERCMD_NO_REQUEST");
1093
1094 return;
1095 }
1096 if ((hs->persist_types[PERSIST_T_REQUEST] == PERSIST_PART) && !GetUserMode(hs->helpchan, user)) {
1097 helpserv_msguser(user, "HSMSG_REQ_YOU_NOT_IN_HELPCHAN_OPEN", hs->helpchan->name);
1098 return;
1099 }
1100
1101 req = create_request(user, hs, 0);
1102 if (user->handle_info)
1103 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_NEW_REQUEST_AUTHED", req->id, user->nick, user->handle_info->handle);
1104 else
1105 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_NEW_REQUEST_UNAUTHED", req->id, user->nick);
1106 } else if (argv[1][0] == helpserv_conf.user_escape) {
1107 char *cmdname;
1108 helpserv_usercmd_t *usercmd;
1109 struct userNode *likely_helper;
1110
1111 /* Find somebody likely to be the helper */
1112 if (!req->helper)
1113 likely_helper = NULL;
1114 else if ((likely_helper = req->helper->handle->users) && !likely_helper->next_authed) {
1115 /* only one user it could be :> */
1116 } else for (likely_helper = req->helper->handle->users; likely_helper; likely_helper = likely_helper->next_authed)
1117 if (GetUserMode(hs->helpchan, likely_helper))
1118 break;
1119
1120 cmdname = argv[1]+1;
1121
1122 /* Call the user command function */
1123 usercmd = dict_find(helpserv_usercmd_dict, cmdname, NULL);
1124 if (usercmd)
1125 usercmd(req, likely_helper, text, argc, argv, user, hs);
1126 else
1127 helpserv_msguser(user, "HSMSG_USERCMD_UNKNOWN", cmdname);
1128 return;
1129 } else if (hs->intervals[INTERVAL_STALE_DELAY]
1130 && (req->updated < (time_t)(now - hs->intervals[INTERVAL_STALE_DELAY]))
1131 && (!hs->req_maxlen || req->text->used < hs->req_maxlen)) {
1132 char buf[MAX_LINE_SIZE], updatestr[INTERVALLEN], timestr[MAX_LINE_SIZE];
1133
1134 strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&req->opened));
1135 intervalString(updatestr, now - req->updated, user->handle_info);
1136 if (req->helper && (hs->notify >= NOTIFY_USER))
1137 if (user->handle_info)
1138 helpserv_notify(req->helper, "HSMSG_PAGE_UPD_REQUEST_AUTHED", req->id, user->nick, user->handle_info->handle, timestr, updatestr);
1139 else
1140 helpserv_notify(req->helper, "HSMSG_PAGE_UPD_REQUEST_NOT_AUTHED", req->id, user->nick, timestr, updatestr);
1141 else
1142 if (user->handle_info)
1143 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_UPD_REQUEST_AUTHED", req->id, user->nick, user->handle_info->handle, timestr, updatestr);
1144 else
1145 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_UPD_REQUEST_NOT_AUTHED", req->id, user->nick, timestr, updatestr);
1146 strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&now));
1147 snprintf(buf, MAX_LINE_SIZE, "[Stale request updated at %s]", timestr);
1148 string_list_append(req->text, strdup(buf));
1149 }
1150
1151 req->updated = now;
1152 if (!hs->req_maxlen || req->text->used < hs->req_maxlen)
1153 {
1154 string_list_append(req->text, strdup(text));
1155
1156 struct userNode *likely_helper;
1157 /* Find somebody likely to be the helper */
1158 if (!req->helper)
1159 likely_helper = NULL;
1160 else if ((likely_helper = req->helper->handle->users) && !likely_helper->next_authed) {
1161 /* only one user it could be :> */
1162 } else for (likely_helper = req->helper->handle->users; likely_helper; likely_helper = likely_helper->next_authed)
1163 if (GetUserMode(hs->helpchan, likely_helper))
1164 break;
1165
1166 if(likely_helper)
1167 send_target_message(1, likely_helper->nick, hs->helpserv, "HSMSQ_REQ_TEXT_ADDED", user->nick, text);
1168 }
1169 else
1170 helpserv_msguser(user, "HSMSG_REQ_MAXLEN");
1171 }
1172
1173 /* Handle messages direct to a HelpServ bot. */
1174 static void helpserv_botmsg(struct userNode *user, struct userNode *target, char *text, UNUSED_ARG(int server_qualified)) {
1175 struct helpserv_bot *hs;
1176 struct helpserv_cmd *cmd;
1177 struct helpserv_user *hs_user = NULL;
1178 char *argv[MAXNUMPARAMS];
1179 char *stext = strdup(text);
1180 int argc, argv_shift;
1181 const int from_opserv = 0; /* for helpserv_notice */
1182
1183 /* Ignore things consisting of empty lines or from ourselves */
1184 if (!*text || IsLocal(user))
1185 return;
1186
1187 hs = dict_find(helpserv_bots_dict, target->nick, NULL);
1188
1189
1190 /* XXX: For some unknown reason we are shifting +1 on the array and ignoring argv[0]; to avoid someone rightly assuming argv[0]
1191 * was the first argument later on and addressing random memory, were going to make argv[0] null. This whole thing is pretty unacceptable and needs fixed, though.*/
1192 argv[0] = NULL;
1193 argv_shift = 1;
1194 argc = split_line(stext, false, ArrayLength(argv)-argv_shift, argv+argv_shift);
1195 if (!argc)
1196 return;
1197
1198 /* XXX: why are we duplicating text here, and i don't see it being free'd anywhere.... */
1199 /* text = strdup(text); */
1200
1201 if (user->handle_info)
1202 hs_user = dict_find(hs->users, user->handle_info->handle, NULL);
1203
1204 if (hs->suspended && !IsOper(user)) {
1205 helpserv_notice(user, "HSMSG_SUSPENDED", hs->helpserv->nick);
1206 return;
1207 }
1208
1209 /* See if we should listen to their message as a command (helper)
1210 * or a help request (user) */
1211 if (!user->handle_info || !hs_user) {
1212 helpserv_usermsg(user, hs, text, argv, argc);
1213 return;
1214 }
1215
1216 cmd = dict_find(helpserv_func_dict, argv[argv_shift], NULL);
1217 if (!cmd) {
1218 helpserv_notice(user, "MSG_COMMAND_UNKNOWN", argv[argv_shift]);
1219 return;
1220 }
1221 if (cmd->flags & CMD_FROM_OPSERV_ONLY) {
1222 helpserv_notice(user, "HSMSG_OPER_CMD");
1223 return;
1224 }
1225 if (cmd->access > hs_user->level) {
1226 helpserv_notice(user, "HSMSG_LEVEL_TOO_LOW");
1227 return;
1228 }
1229 if (!cmd->func) {
1230 helpserv_notice(user, "HSMSG_INTERNAL_COMMAND", argv[argv_shift]);
1231 } else if (cmd->func(user, hs, 0, argc, argv+argv_shift)) {
1232 char *buf = unsplit_string(argv+argv_shift, argc, NULL);
1233 log_audit(HS_LOG, LOG_COMMAND, user, hs->helpserv, hs->helpchan->name, 0, buf);
1234 }
1235 }
1236
1237 /* Handle a control command from an IRC operator */
1238 static MODCMD_FUNC(cmd_helpserv) {
1239 struct helpserv_bot *hs = NULL;
1240 struct helpserv_cmd *subcmd;
1241 const int from_opserv = 1; /* for helpserv_notice */
1242 char botnick[NICKLEN+1]; /* in case command is unregister */
1243 int retval;
1244
1245 if (argc < 2) {
1246 send_help(user, opserv, helpserv_helpfile, NULL);
1247 return 0;
1248 }
1249
1250 if (!(subcmd = dict_find(helpserv_func_dict, argv[1], NULL))) {
1251 helpserv_notice(user, "MSG_COMMAND_UNKNOWN", argv[1]);
1252 return 0;
1253 }
1254
1255 if (!subcmd->func) {
1256 helpserv_notice(user, "HSMSG_INTERNAL_COMMAND", argv[1]);
1257 return 0;
1258 }
1259
1260 if ((subcmd->flags & CMD_NEED_BOT) && ((argc < 3) || !(hs = dict_find(helpserv_bots_dict, argv[2], NULL)))) {
1261 helpserv_notice(user, "HSMSG_INVALID_BOT");
1262 return 0;
1263 }
1264
1265 if (subcmd->flags & CMD_NEVER_FROM_OPSERV) {
1266 helpserv_notice(user, "HSMSG_NO_USE_OPSERV");
1267 return 0;
1268 }
1269
1270 if (hs) {
1271 argv[2] = argv[1];
1272 strcpy(botnick, hs->helpserv->nick);
1273 retval = subcmd->func(user, hs, 1, argc-2, argv+2);
1274 } else {
1275 strcpy(botnick, "No bot");
1276 retval = subcmd->func(user, hs, 1, argc-1, argv+1);
1277 }
1278
1279 return retval;
1280 }
1281
1282 static void helpserv_help(struct helpserv_bot *hs, int from_opserv, struct userNode *user, const char *topic, int from_user) {
1283 char cmdname[MAXLEN];
1284 unsigned int nn;
1285
1286 /* If there is no topic show the index */
1287 if (!topic && from_user)
1288 topic = "<userindex>";
1289 else if (!topic && !from_user)
1290 topic = "<index>";
1291
1292 /* make heading str (uppercase) */
1293 for (nn=0; topic[nn]; nn++)
1294 cmdname[nn] = toupper(topic[nn]);
1295 cmdname[nn] = 0;
1296
1297 send_message(user, (from_opserv ? opserv : hs->helpserv), "HSMSG_HELP_TOPIC_HEADER", cmdname);
1298 send_message(user, (from_opserv ? opserv : hs->helpserv), "HSMSG_HELP_DIVIDER");
1299 if (!send_help(user, (from_opserv ? opserv : hs->helpserv), helpserv_helpfile, topic))
1300 send_message(user, (from_opserv ? opserv : hs->helpserv), "MSG_TOPIC_UNKNOWN");
1301 send_message(user, (from_opserv ? opserv : hs->helpserv), "HSMSG_HELP_FOOTER");
1302
1303 }
1304
1305 static int append_entry(const char *key, UNUSED_ARG(void *data), void *extra) {
1306 struct helpfile_expansion *exp = extra;
1307 int row;
1308
1309 row = exp->value.table.length++;
1310 exp->value.table.contents[row] = calloc(1, sizeof(char*));
1311 exp->value.table.contents[row][0] = key;
1312 return 0;
1313 }
1314
1315 static struct helpfile_expansion helpserv_expand_variable(const char *variable) {
1316 struct helpfile_expansion exp;
1317
1318 if (!irccasecmp(variable, "index")) {
1319 exp.type = HF_TABLE;
1320 exp.value.table.length = 1;
1321 exp.value.table.width = 1;
1322 exp.value.table.flags = TABLE_REPEAT_ROWS;
1323 exp.value.table.contents = calloc(dict_size(helpserv_func_dict)+1, sizeof(char**));
1324 exp.value.table.contents[0] = calloc(1, sizeof(char*));
1325 exp.value.table.contents[0][0] = "Commands:";
1326 dict_foreach(helpserv_func_dict, append_entry, &exp);
1327 return exp;
1328 }
1329
1330 exp.type = HF_STRING;
1331 exp.value.str = NULL;
1332 return exp;
1333 }
1334
1335 static void helpserv_helpfile_read(void) {
1336 helpserv_helpfile = open_helpfile(HELPSERV_HELPFILE_NAME, helpserv_expand_variable);
1337 }
1338
1339 static HELPSERV_USERCMD(usercmd_wait) {
1340 struct helpserv_request *other;
1341 int pos, count;
1342 char buf[INTERVALLEN];
1343
1344 if (req->helper) {
1345 if (likely_helper)
1346 helpserv_user_reply("HSMSG_YOU_BEING_HELPED_BY", likely_helper->nick);
1347 else
1348 helpserv_user_reply("HSMSG_YOU_BEING_HELPED");
1349 return 0;
1350 }
1351
1352 for (other = req->hs->unhandled, pos = -1, count = 0;
1353 other;
1354 other = other->next_unhandled, ++count) {
1355 if (other == req)
1356 pos = count;
1357 }
1358 assert(pos >= 0);
1359 intervalString(buf, now - req->hs->unhandled->opened, req->user->handle_info);
1360 helpserv_user_reply("HSMSG_WAIT_STATUS", pos+1, count, buf);
1361
1362 return 0;
1363 }
1364
1365 static HELPSERV_USERCMD(usercmd_helper) {
1366 if (req->helper) {
1367 if (likely_helper)
1368 helpserv_user_reply("HSMSG_HELPER", likely_helper->nick);
1369 } else
1370 helpserv_user_reply("HSMSG_NO_HELPER");
1371
1372 return 0;
1373 }
1374
1375 static HELPSERV_USERCMD(usercmd_help) {
1376 const char *topic;
1377
1378 if (argc < 2)
1379 topic = NULL;
1380 else
1381 topic = unsplit_string(argv+2, argc-1, NULL);
1382 helpserv_help(hs, 0, user, topic, 1);
1383
1384 return 0;
1385 }
1386
1387 static HELPSERV_FUNC(cmd_help) {
1388 const char *topic;
1389
1390 if (argc < 2)
1391 topic = NULL;
1392 else
1393 topic = unsplit_string(argv+1, argc-1, NULL);
1394 helpserv_help(hs, from_opserv, user, topic, 0);
1395
1396 return 1;
1397 }
1398
1399 static HELPSERV_FUNC(cmd_readhelp) {
1400 struct timeval start, stop;
1401 struct helpfile *old_helpfile = helpserv_helpfile;
1402
1403 gettimeofday(&start, NULL);
1404 helpserv_helpfile_read();
1405 if (helpserv_helpfile) {
1406 close_helpfile(old_helpfile);
1407 } else {
1408 helpserv_helpfile = old_helpfile;
1409 }
1410 gettimeofday(&stop, NULL);
1411 stop.tv_sec -= start.tv_sec;
1412 stop.tv_usec -= start.tv_usec;
1413 if (stop.tv_usec < 0) {
1414 stop.tv_sec -= 1;
1415 stop.tv_usec += 1000000;
1416 }
1417 helpserv_notice(user, "HSMSG_READHELP_SUCCESS", stop.tv_sec, stop.tv_usec/1000);
1418
1419 return 1;
1420 }
1421
1422 static struct helpserv_user * helpserv_add_user(struct helpserv_bot *hs, struct handle_info *handle, enum helpserv_level level) {
1423 struct helpserv_user *hs_user;
1424 struct helpserv_userlist *userlist;
1425
1426 hs_user = calloc(1, sizeof(struct helpserv_user));
1427 hs_user->handle = handle;
1428 hs_user->hs = hs;
1429 hs_user->help_mode = 0;
1430 hs_user->level = level;
1431 hs_user->join_time = find_handle_in_channel(hs->helpchan, handle, NULL) ? now : 0;
1432 dict_insert(hs->users, handle->handle, hs_user);
1433
1434 if (!(userlist = dict_find(helpserv_users_byhand_dict, handle->handle, NULL))) {
1435 userlist = helpserv_userlist_alloc();
1436 dict_insert(helpserv_users_byhand_dict, handle->handle, userlist);
1437 }
1438 helpserv_userlist_append(userlist, hs_user);
1439
1440 return hs_user;
1441 }
1442
1443 static void helpserv_del_user(struct helpserv_bot *hs, struct helpserv_user *hs_user) {
1444 dict_remove(hs->users, hs_user->handle->handle);
1445 }
1446
1447 static int cmd_add_user(struct helpserv_bot *hs, int from_opserv, struct userNode *user, enum helpserv_level level, int argc, char *argv[]) {
1448 struct helpserv_user *actor, *new_user;
1449 struct handle_info *handle;
1450
1451 REQUIRE_PARMS(2);
1452
1453 if (!from_opserv) {
1454 actor = GetHSUser(hs, user->handle_info);
1455 if (actor->level < HlManager) {
1456 helpserv_notice(user, "HSMSG_CANNOT_ADD");
1457 return 0;
1458 }
1459 } else {
1460 actor = NULL;
1461 }
1462
1463 if (!(handle = helpserv_get_handle_info(user, argv[1])))
1464 return 0;
1465
1466 if (GetHSUser(hs, handle)) {
1467 helpserv_notice(user, "HSMSG_USER_EXISTS", handle->handle);
1468 return 0;
1469 }
1470
1471 if (!(from_opserv) && actor && (actor->level <= level)) {
1472 helpserv_notice(user, "HSMSG_NO_BUMP_ACCESS");
1473 return 0;
1474 }
1475
1476 new_user = helpserv_add_user(hs, handle, level);
1477
1478 helpserv_notice(user, "HSMSG_ADDED_USER", helpserv_level2str(level), handle->handle);
1479 return 1;
1480 }
1481
1482 static HELPSERV_FUNC(cmd_deluser) {
1483 struct helpserv_user *actor=NULL, *victim;
1484 struct handle_info *handle;
1485 enum helpserv_level level;
1486
1487 REQUIRE_PARMS(2);
1488
1489 if (!from_opserv) {
1490 actor = GetHSUser(hs, user->handle_info);
1491 if (actor->level < HlManager) {
1492 helpserv_notice(user, "HSMSG_CANNOT_DEL");
1493 return 0;
1494 }
1495 }
1496
1497 if (!(handle = helpserv_get_handle_info(user, argv[1])))
1498 return 0;
1499
1500 if (!(victim = GetHSUser(hs, handle))) {
1501 helpserv_notice(user, "HSMSG_NOT_IN_USERLIST", handle->handle, hs->helpserv->nick);
1502 return 0;
1503 }
1504
1505 if (!from_opserv && actor && (actor->level <= victim->level)) {
1506 helpserv_notice(user, "MSG_USER_OUTRANKED", victim->handle->handle);
1507 return 0;
1508 }
1509
1510 level = victim->level;
1511 helpserv_del_user(hs, victim);
1512 helpserv_notice(user, "HSMSG_DELETED_USER", helpserv_level2str(level), handle->handle);
1513 return 1;
1514 }
1515
1516 static int
1517 helpserv_user_comp(const void *arg_a, const void *arg_b)
1518 {
1519 const struct helpserv_user *a = *(struct helpserv_user**)arg_a;
1520 const struct helpserv_user *b = *(struct helpserv_user**)arg_b;
1521 int res;
1522 if (a->level != b->level)
1523 res = b->level - a->level;
1524 else
1525 res = irccasecmp(a->handle->handle, b->handle->handle);
1526 return res;
1527 }
1528
1529 static int show_helper_range(struct userNode *user, struct helpserv_bot *hs, int from_opserv, enum helpserv_level min_lvl, enum helpserv_level max_lvl) {
1530 struct helpserv_userlist users;
1531 struct helpfile_table tbl;
1532 struct helpserv_user *hs_user;
1533 dict_iterator_t it;
1534 /* enum helpserv_level last_level; Zoot style */
1535 unsigned int ii;
1536
1537 users.used = 0;
1538 users.size = dict_size(hs->users);
1539 users.list = alloca(users.size*sizeof(hs->users[0]));
1540 helpserv_notice(user, "HSMSG_USERLIST_HEADER", hs->helpserv->nick);
1541 for (it = dict_first(hs->users); it; it = iter_next(it)) {
1542 hs_user = iter_data(it);
1543 if (hs_user->level < min_lvl)
1544 continue;
1545 if (hs_user->level > max_lvl)
1546 continue;
1547 users.list[users.used++] = hs_user;
1548 }
1549 if (!users.used) {
1550 helpserv_notice(user, "MSG_NONE");
1551 return 1;
1552 }
1553 qsort(users.list, users.used, sizeof(users.list[0]), helpserv_user_comp);
1554 switch (user->handle_info->userlist_style) {
1555 default:
1556 case HI_STYLE_NORMAL:
1557 tbl.length = users.used + 1;
1558 tbl.width = 3;
1559 tbl.flags = TABLE_NO_FREE;
1560 tbl.contents = alloca(tbl.length * sizeof(tbl.contents[0]));
1561 tbl.contents[0] = alloca(tbl.width * sizeof(tbl.contents[0][0]));
1562 tbl.contents[0][0] = "Level";
1563 tbl.contents[0][1] = "Handle";
1564 tbl.contents[0][2] = "WeekStart";
1565 for (ii = 0; ii < users.used; ) {
1566 hs_user = users.list[ii++];
1567 tbl.contents[ii] = alloca(tbl.width * sizeof(tbl.contents[0][0]));
1568 tbl.contents[ii][0] = helpserv_level_names[hs_user->level];
1569 tbl.contents[ii][1] = hs_user->handle->handle;
1570 tbl.contents[ii][2] = weekday_names[hs_user->week_start];
1571 }
1572 table_send((from_opserv ? opserv : hs->helpserv), user->nick, 0, NULL, tbl);
1573 break;
1574 /*
1575 case HI_STYLE_ZOOT: default:
1576 last_level = HlNone;
1577 tbl.length = 0;
1578 tbl.width = 1;
1579 tbl.flags = TABLE_NO_FREE | TABLE_REPEAT_ROWS | TABLE_NO_HEADERS;
1580 tbl.contents = alloca(users.used * sizeof(tbl.contents[0]));
1581 for (ii = 0; ii < users.used; ) {
1582 hs_user = users.list[ii++];
1583 if (hs_user->level != last_level) {
1584 if (tbl.length) {
1585 helpserv_notice(user, "HSMSG_USERLIST_ZOOT_LVL", hs->helpserv->nick, helpserv_level_names[last_level]);
1586 table_send((from_opserv ? opserv : hs->helpserv), user->nick, 0, NULL, tbl);
1587 tbl.length = 0;
1588 }
1589 last_level = hs_user->level;
1590 }
1591 tbl.contents[tbl.length] = alloca(tbl.width * sizeof(tbl.contents[0][0]));
1592 tbl.contents[tbl.length++][0] = hs_user->handle->handle;
1593 }
1594 if (tbl.length) {
1595 helpserv_notice(user, "HSMSG_USERLIST_ZOOT_LVL", hs->helpserv->nick, helpserv_level_names[last_level]);
1596 table_send((from_opserv ? opserv : hs->helpserv), user->nick, 0, NULL, tbl);
1597 }
1598 */
1599 }
1600 return 1;
1601 }
1602
1603 static HELPSERV_FUNC(cmd_helpers) {
1604 return show_helper_range(user, hs, from_opserv, HlTrial, HlOwner);
1605 }
1606
1607 static HELPSERV_FUNC(cmd_wlist) {
1608 return show_helper_range(user, hs, from_opserv, HlOwner, HlOwner);
1609 }
1610
1611 static HELPSERV_FUNC(cmd_mlist) {
1612 return show_helper_range(user, hs, from_opserv, HlManager, HlManager);
1613 }
1614
1615 static HELPSERV_FUNC(cmd_hlist) {
1616 return show_helper_range(user, hs, from_opserv, HlHelper, HlHelper);
1617 }
1618
1619 static HELPSERV_FUNC(cmd_tlist) {
1620 return show_helper_range(user, hs, from_opserv, HlTrial, HlTrial);
1621 }
1622
1623 static HELPSERV_FUNC(cmd_addowner) {
1624 return cmd_add_user(hs, from_opserv, user, HlOwner, argc, argv);
1625 }
1626
1627 static HELPSERV_FUNC(cmd_addmanager) {
1628 return cmd_add_user(hs, from_opserv, user, HlManager, argc, argv);
1629 }
1630
1631 static HELPSERV_FUNC(cmd_addhelper) {
1632 return cmd_add_user(hs, from_opserv, user, HlHelper, argc, argv);
1633 }
1634
1635 static HELPSERV_FUNC(cmd_addtrial) {
1636 return cmd_add_user(hs, from_opserv, user, HlTrial, argc, argv);
1637 }
1638
1639 static HELPSERV_FUNC(cmd_clvl) {
1640 struct helpserv_user *actor=NULL, *victim;
1641 struct handle_info *handle;
1642 enum helpserv_level level;
1643
1644 REQUIRE_PARMS(3);
1645
1646 if (!from_opserv) {
1647 actor = GetHSUser(hs, user->handle_info);
1648 if (actor->level < HlManager) {
1649 helpserv_notice(user, "HSMSG_CANNOT_CLVL");
1650 return 0;
1651 }
1652 }
1653
1654 if (!(handle = helpserv_get_handle_info(user, argv[1])))
1655 return 0;
1656
1657 if (!(victim = GetHSUser(hs, handle))) {
1658 helpserv_notice(user, "HSMSG_NOT_IN_USERLIST", handle->handle, hs->helpserv->nick);
1659 return 0;
1660 }
1661
1662 if (((level = helpserv_str2level(argv[2])) == HlNone) || level == HlOper) {
1663 helpserv_notice(user, "HSMSG_INVALID_ACCESS", argv[2]);
1664 return 0;
1665 }
1666
1667 if (!(from_opserv) && actor) {
1668 if (actor == victim) {
1669 helpserv_notice(user, "HSMSG_NO_SELF_CLVL");
1670 return 0;
1671 }
1672
1673 if (actor->level <= victim->level) {
1674 helpserv_notice(user, "MSG_USER_OUTRANKED", victim->handle->handle);
1675 return 0;
1676 }
1677
1678 if (level >= actor->level) {
1679 helpserv_notice(user, "HSMSG_NO_BUMP_ACCESS");
1680 return 0;
1681 }
1682 }
1683
1684 victim->level = level;
1685 helpserv_notice(user, "HSMSG_CHANGED_ACCESS", handle->handle, helpserv_level2str(level));
1686
1687 return 1;
1688 }
1689
1690 static void free_request(void *data) {
1691 struct helpserv_request *req = data;
1692
1693 /* Logging */
1694 if (shutting_down && (req->hs->persist_types[PERSIST_T_REQUEST] != PERSIST_CLOSE || !req->handle)) {
1695 helpserv_log_request(req, "X3 shutdown");
1696 }
1697
1698 /* Clean up from the unhandled queue */
1699 if (req->hs->unhandled) {
1700 if (req->hs->unhandled == req) {
1701 req->hs->unhandled = req->next_unhandled;
1702 } else {
1703 struct helpserv_request *uh;
1704 for (uh=req->hs->unhandled; uh->next_unhandled && (uh->next_unhandled != req); uh = uh->next_unhandled);
1705 if (uh->next_unhandled) {
1706 uh->next_unhandled = req->next_unhandled;
1707 }
1708 }
1709 }
1710
1711 /* Clean up the lists */
1712 if (req->parent_nick_list) {
1713 if (req->parent_nick_list->used == 1) {
1714 dict_remove(helpserv_reqs_bynick_dict, req->user->nick);
1715 } else {
1716 helpserv_reqlist_remove(req->parent_nick_list, req);
1717 }
1718 }
1719 if (req->parent_hand_list) {
1720 if (req->parent_hand_list->used == 1) {
1721 dict_remove(helpserv_reqs_byhand_dict, req->handle->handle);
1722 } else {
1723 helpserv_reqlist_remove(req->parent_hand_list, req);
1724 }
1725 }
1726
1727 free_string_list(req->text);
1728 free(req);
1729 }
1730
1731 static HELPSERV_FUNC(cmd_close) {
1732 struct helpserv_request *req, *newest=NULL;
1733 struct helpserv_reqlist *nick_list, *hand_list;
1734 struct helpserv_user *hs_user=GetHSUser(hs, user->handle_info);
1735 struct userNode *req_user=NULL;
1736 char close_reason[MAXLEN], reqnum[12];
1737 unsigned long old_req;
1738 unsigned int i;
1739 int num_requests=0;
1740
1741 REQUIRE_PARMS(2);
1742
1743 assert(hs_user);
1744
1745 if (!(req = smart_get_request(hs, hs_user, argv[1], &num_requests))) {
1746 helpserv_notice(user, "HSMSG_REQ_INVALID", argv[1]);
1747 return 0;
1748 }
1749
1750 sprintf(reqnum, "%lu", req->id);
1751
1752 if (num_requests > 1)
1753 helpserv_notice(user, "HSMSG_REQ_FOUNDMANY");
1754
1755 if (hs_user->level < HlManager && req->helper != hs_user) {
1756 if (req->helper)
1757 helpserv_notice(user, "HSMSG_REQ_NOT_YOURS_ASSIGNED_TO", req->id, req->helper->handle->handle);
1758 else
1759 helpserv_notice(user, "HSMSG_REQ_NOT_YOURS_UNASSIGNED", req->id);
1760 return 0;
1761 }
1762
1763 helpserv_notice(user, "HSMSG_REQ_CLOSED", req->id);
1764 if (req->user) {
1765 req_user = req->user;
1766 helpserv_message(hs, req->user, MSGTYPE_REQ_CLOSED);
1767 if (req->handle)
1768 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_CLOSE_REQUEST_1", req->id, req->user->nick, req->handle->handle, user->nick);
1769 else
1770 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_CLOSE_REQUEST_2", req->id, req->user->nick, user->nick);
1771 } else {
1772 if (req->handle)
1773 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_CLOSE_REQUEST_3", req->id, req->handle->handle, user->nick);
1774 else
1775 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_CLOSE_REQUEST_4", req->id, user->nick);
1776 }
1777
1778 hs_user->closed[0]++;
1779 hs_user->closed[4]++;
1780
1781 /* Set these to keep track of the lists after the request is gone, but
1782 * not if free_request() will helpserv_reqlist_free() them. */
1783 nick_list = req->parent_nick_list;
1784 if (nick_list && (nick_list->used == 1))
1785 nick_list = NULL;
1786 hand_list = req->parent_hand_list;
1787 if (hand_list && (hand_list->used == 1))
1788 hand_list = NULL;
1789 old_req = req->id;
1790
1791 if (argc >= 3) {
1792 snprintf(close_reason, MAXLEN, "Closed by %s: %s", user->handle_info->handle, unsplit_string(argv+2, argc-2, NULL));
1793 } else {
1794 sprintf(close_reason, "Closed by %s", user->handle_info->handle);
1795 }
1796 helpserv_log_request(req, close_reason);
1797 dict_remove(hs->requests, reqnum);
1798
1799 /* Look for other requests associated with them */
1800 if (nick_list) {
1801 for (i=0; i < nick_list->used; i++) {
1802 req = nick_list->list[i];
1803
1804 if (req->hs != hs)
1805 continue;
1806 if (!newest || (newest->opened < req->opened))
1807 newest = req;
1808 }
1809
1810 if (newest)
1811 helpserv_msguser(newest->user, "HSMSG_REQ_FOUND_ANOTHER", old_req, newest->id);
1812 }
1813
1814 if (req_user && hs->auto_devoice) {
1815 struct modeNode *mn = GetUserMode(hs->helpchan, req_user);
1816 if ((!newest || !newest->helper) && mn && (mn->modes & MODE_VOICE)) {
1817 struct mod_chanmode change;
1818 mod_chanmode_init(&change);
1819 change.argc = 1;
1820 change.args[0].mode = MODE_REMOVE | MODE_VOICE;
1821 change.args[0].u.member = mn;
1822 mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
1823 }
1824 }
1825
1826 return 1;
1827 }
1828
1829 static HELPSERV_USERCMD(usercmd_close) {
1830 struct helpserv_request *newest=NULL;
1831 struct helpserv_reqlist *nick_list, *hand_list;
1832 char close_reason[MAXLEN], reqnum[12];
1833 struct userNode *req_user=NULL;
1834 unsigned long old_req;
1835 unsigned int i;
1836 int num_requests=0, from_opserv=0;
1837
1838 REQUIRE_PARMS(1);
1839
1840 sprintf(reqnum, "%lu", req->id);
1841
1842 if (num_requests > 1) {
1843 helpserv_notice(user, "HSMSG_REQ_FOUNDMANY");
1844 return 0;
1845 }
1846
1847 helpserv_notice(user, "HSMSG_REQ_CLOSED", req->id);
1848
1849 if (req->user) {
1850 req_user = req->user;
1851 helpserv_message(hs, req->user, MSGTYPE_REQ_CLOSED);
1852 if (req->handle)
1853 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_CLOSE_REQUEST_1", req->id, req->user->nick, req->handle->handle, user->nick);
1854 else
1855 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_CLOSE_REQUEST_2", req->id, req->user->nick, user->nick);
1856 } else {
1857 if (req->handle)
1858 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_CLOSE_REQUEST_3", req->id, req->handle->handle, user->nick);
1859 else
1860 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_CLOSE_REQUEST_4", req->id, user->nick);
1861 }
1862
1863 /* Set these to keep track of the lists after the request is gone, but
1864 * not if free_request() will helpserv_reqlist_free() them. */
1865 nick_list = req->parent_nick_list;
1866 if (nick_list && (nick_list->used == 1))
1867 nick_list = NULL;
1868 hand_list = req->parent_hand_list;
1869 if (hand_list && (hand_list->used == 1))
1870 hand_list = NULL;
1871 old_req = req->id;
1872
1873 if (argc >= 1) {
1874 if (user->handle_info)
1875 snprintf(close_reason, MAXLEN, "Closed by %s: %s", user->handle_info->handle, unsplit_string(argv+1, argc-1, NULL));
1876 else
1877 snprintf(close_reason, MAXLEN, "Closed by %s: %s", user->nick, unsplit_string(argv+1, argc-1, NULL));
1878 } else {
1879 if (user->handle_info)
1880 sprintf(close_reason, "Closed by %s", user->nick);
1881 else
1882 sprintf(close_reason, "Closed by %s", user->handle_info->handle);
1883 }
1884 helpserv_log_request(req, close_reason);
1885 dict_remove(hs->requests, reqnum);
1886
1887 /* Look for other requests associated with them */
1888 if (nick_list) {
1889 for (i=0; i < nick_list->used; i++) {
1890 req = nick_list->list[i];
1891
1892 if (req->hs != hs)
1893 continue;
1894 if (!newest || (newest->opened < req->opened))
1895 newest = req;
1896 }
1897
1898 if (newest)
1899 helpserv_msguser(newest->user, "HSMSG_REQ_FOUND_ANOTHER", old_req, newest->id);
1900 }
1901
1902 if (req_user && hs->auto_devoice) {
1903 struct modeNode *mn = GetUserMode(hs->helpchan, req_user);
1904 if ((!newest || !newest->helper) && mn && (mn->modes & MODE_VOICE)) {
1905 struct mod_chanmode change;
1906 mod_chanmode_init(&change);
1907 change.argc = 1;
1908 change.args[0].mode = MODE_REMOVE | MODE_VOICE;
1909 change.args[0].u.member = mn;
1910 mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
1911 }
1912 }
1913
1914 return 0;
1915 }
1916
1917 static HELPSERV_FUNC(cmd_list) {
1918 dict_iterator_t it;
1919 int searchtype;
1920 struct helpfile_table tbl;
1921 unsigned int line, total;
1922 struct helpserv_request *req;
1923
1924 if ((argc < 2) || !irccasecmp(argv[1], "unassigned")) {
1925 for (req = hs->unhandled, total=0; req; req = req->next_unhandled, total++) ;
1926 helpserv_notice(user, "HSMSG_REQ_LIST_TOP_UNASSIGNED", total);
1927 searchtype = 1; /* Unassigned */
1928 } else if (!irccasecmp(argv[1], "assigned")) {
1929 for (req = hs->unhandled, total=dict_size(hs->requests); req; req = req->next_unhandled, total--) ;
1930 helpserv_notice(user, "HSMSG_REQ_LIST_TOP_ASSIGNED", total);
1931 searchtype = 2; /* Assigned */
1932 } else if (!irccasecmp(argv[1], "me")) {
1933 for (total = 0, it = dict_first(hs->requests); it; it = iter_next(it)) {
1934 req = iter_data(it);
1935 if (req->helper && (req->helper->handle == user->handle_info))
1936 total++;
1937 }
1938 helpserv_notice(user, "HSMSG_REQ_LIST_TOP_YOUR", total);
1939 searchtype = 4;
1940 } else if (!irccasecmp(argv[1], "all")) {
1941 total = dict_size(hs->requests);
1942 helpserv_notice(user, "HSMSG_REQ_LIST_TOP_ALL", total);
1943 searchtype = 3; /* All */
1944 } else {
1945 helpserv_notice(user, "HSMSG_BAD_REQ_TYPE", argv[1]);
1946 return 0;
1947 }
1948
1949 if (!total) {
1950 helpserv_notice(user, "HSMSG_REQ_LIST_NONE");
1951 return 1;
1952 }
1953
1954 tbl.length = total+1;
1955 tbl.width = 5;
1956 tbl.flags = TABLE_NO_FREE;
1957 tbl.contents = alloca(tbl.length * sizeof(*tbl.contents));
1958 tbl.contents[0] = alloca(tbl.width * sizeof(**tbl.contents));
1959 tbl.contents[0][0] = "ID#";
1960 tbl.contents[0][1] = "User";
1961 tbl.contents[0][2] = "Helper";
1962 tbl.contents[0][3] = "Time open";
1963 tbl.contents[0][4] = "User status";
1964
1965 for (it=dict_first(hs->requests), line=0; it; it=iter_next(it)) {
1966 char opentime[INTERVALLEN], reqid[12], username[NICKLEN+2];
1967
1968 req = iter_data(it);
1969
1970 switch (searchtype) {
1971 case 1:
1972 if (req->helper)
1973 continue;
1974 break;
1975 case 2:
1976 if (!req->helper)
1977 continue;
1978 break;
1979 case 3:
1980 default:
1981 break;
1982 case 4:
1983 if (!req->helper || (req->helper->handle != user->handle_info))
1984 continue;
1985 break;
1986 }
1987
1988 line++;
1989
1990 tbl.contents[line] = alloca(tbl.width * sizeof(**tbl.contents));
1991 sprintf(reqid, "%lu", req->id);
1992 tbl.contents[line][0] = strdup(reqid);
1993 if (req->user) {
1994 strcpy(username, req->user->nick);
1995 } else {
1996 username[0] = '*';
1997 strcpy(username+1, req->handle->handle);
1998 }
1999 tbl.contents[line][1] = strdup(username);
2000 tbl.contents[line][2] = req->helper ? req->helper->handle->handle : "(Unassigned)";
2001 intervalString(opentime, now - req->opened, user->handle_info);
2002 tbl.contents[line][3] = strdup(opentime);
2003 tbl.contents[line][4] = ((req->user || req->handle->users) ? "Online" : "Offline");
2004 }
2005
2006 table_send((from_opserv ? opserv : hs->helpserv), user->nick, 0, NULL, tbl);
2007
2008 for (; line > 0; line--) {
2009 free((char *)tbl.contents[line][0]);
2010 free((char *)tbl.contents[line][1]);
2011 free((char *)tbl.contents[line][3]);
2012 }
2013
2014 return 1;
2015 }
2016
2017 static void helpserv_show(int from_opserv, struct helpserv_bot *hs, struct userNode *user, struct helpserv_request *req) {
2018 unsigned int nn;
2019 char buf[MAX_LINE_SIZE];
2020 char buf2[INTERVALLEN];
2021
2022 if (req->user)
2023 if (req->handle)
2024 helpserv_notice(user, "HSMSG_REQ_INFO_2a", req->user->nick, req->handle->handle);
2025 else
2026 helpserv_notice(user, "HSMSG_REQ_INFO_2b", req->user->nick);
2027 else if (req->handle)
2028 if (req->handle->users)
2029 helpserv_notice(user, "HSMSG_REQ_INFO_2c", req->handle->handle);
2030 else
2031 helpserv_notice(user, "HSMSG_REQ_INFO_2d", req->handle->handle);
2032 else
2033 helpserv_notice(user, "HSMSG_REQ_INFO_2e");
2034 strftime(buf, MAX_LINE_SIZE, HSFMT_TIME, localtime(&req->opened));
2035 intervalString(buf2, now - req->opened, user->handle_info);
2036 helpserv_notice(user, "HSMSG_REQ_INFO_3", buf, buf2);
2037 helpserv_notice(user, "HSMSG_REQ_INFO_4");
2038 for (nn=0; nn < req->text->used; nn++)
2039 helpserv_notice(user, "HSMSG_REQ_INFO_MESSAGE", req->text->list[nn]);
2040 }
2041
2042 /* actor is the one who executed the command... it should == user except from
2043 * cmd_assign */
2044 static int helpserv_assign(int from_opserv, struct helpserv_bot *hs, struct userNode *user, struct userNode *actor, struct helpserv_request *req) {
2045 struct helpserv_request *req2;
2046 struct helpserv_user *old_helper;
2047
2048 if (!user->handle_info)
2049 return 0;
2050 if ((hs->persist_types[PERSIST_T_HELPER] == PERSIST_PART) && !GetUserMode(hs->helpchan, user)) {
2051 struct helpserv_user *hsuser_actor = GetHSUser(hs, actor->handle_info);
2052 if (hsuser_actor->level < HlManager) {
2053 helpserv_notice(user, "HSMSG_REQ_YOU_NOT_IN_HELPCHAN", hs->helpchan->name);
2054 return 0;
2055 } else if (user != actor) {
2056 helpserv_notice(user, "HSMSG_REQ_YOU_NOT_IN_OVERRIDE", hs->helpchan->name);
2057 helpserv_notice(actor, "HSMSG_REQ_HIM_NOT_IN_OVERRIDE", user->nick, hs->helpchan->name);
2058 } else
2059 helpserv_notice(user, "HSMSG_REQ_SELF_NOT_IN_OVERRIDE", hs->helpchan->name);
2060 }
2061
2062 hs->last_active = now;
2063 if ((old_helper = req->helper)) {
2064 /* don't need to remove from the unhandled queue */
2065 } else if (hs->unhandled == req) {
2066 hs->unhandled = req->next_unhandled;
2067 } else for (req2 = hs->unhandled; req2; req2 = req2->next_unhandled) {
2068 if (req2->next_unhandled == req) {
2069 req2->next_unhandled = req->next_unhandled;
2070 break;
2071 }
2072 }
2073 req->next_unhandled = NULL;
2074 req->helper = GetHSUser(hs, user->handle_info);
2075 assert(req->helper);
2076 req->assigned = now;
2077
2078 if (old_helper) {
2079 helpserv_notice(user, "HSMSG_REQ_REASSIGNED", req->id, old_helper->handle->handle);
2080 req->helper->reassigned_to[0]++;
2081 req->helper->reassigned_to[4]++;
2082 old_helper->reassigned_from[0]++;
2083 old_helper->reassigned_from[4]++;
2084 } else {
2085 helpserv_notice(user, "HSMSG_REQ_ASSIGNED_YOU", req->id);
2086 req->helper->picked_up[0]++;
2087 req->helper->picked_up[4]++;
2088 }
2089 helpserv_show(from_opserv, hs, user, req);
2090 if (req->user) {
2091 helpserv_message(hs, req->user, MSGTYPE_REQ_ASSIGNED);
2092 if (old_helper) {
2093 helpserv_msguser(req->user, "HSMSG_REQ_ASSIGNED_AGAIN", req->id, user->handle_info->handle, user->nick);
2094 } else {
2095 helpserv_msguser(req->user, "HSMSG_REQ_ASSIGNED", req->id, user->handle_info->handle, user->nick);
2096 }
2097 if (req->handle)
2098 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_ASSIGN_REQUEST_1", req->id, req->user->nick, req->handle->handle, user->nick);
2099 else
2100 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_ASSIGN_REQUEST_2", req->id, req->user->nick, user->nick);
2101 } else {
2102 if (req->handle)
2103 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_ASSIGN_REQUEST_3", req->id, req->handle->handle, user->nick);
2104 else
2105 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_ASSIGN_REQUEST_4", req->id, user->nick);
2106 }
2107
2108 if (req->user && hs->auto_voice) {
2109 struct mod_chanmode change;
2110 mod_chanmode_init(&change);
2111 change.argc = 1;
2112 change.args[0].mode = MODE_VOICE;
2113 if ((change.args[0].u.member = GetUserMode(hs->helpchan, req->user)))
2114 mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
2115 }
2116
2117 return 1;
2118 }
2119
2120 static HELPSERV_FUNC(cmd_next) {
2121 struct helpserv_request *req;
2122
2123 if (!(req = hs->unhandled)) {
2124 helpserv_notice(user, "HSMSG_REQ_NO_UNASSIGNED");
2125 return 0;
2126 }
2127 return helpserv_assign(from_opserv, hs, user, user, req);
2128 }
2129
2130 static HELPSERV_FUNC(cmd_show) {
2131 struct helpserv_request *req;
2132 struct helpserv_user *hs_user=GetHSUser(hs, user->handle_info);
2133 int num_requests=0;
2134
2135 REQUIRE_PARMS(2);
2136
2137 if (!(req = smart_get_request(hs, hs_user, argv[1], &num_requests))) {
2138 helpserv_notice(user, "HSMSG_REQ_INVALID", argv[1]);
2139 return 0;
2140 }
2141
2142 if (num_requests > 1)
2143 helpserv_notice(user, "HSMSG_REQ_FOUNDMANY");
2144
2145 helpserv_notice(user, "HSMSG_REQ_INFO_1", req->id);
2146 helpserv_show(from_opserv, hs, user, req);
2147 return 1;
2148 }
2149
2150 static HELPSERV_USERCMD(usercmd_show) {
2151 int num_requests=0;
2152 int from_opserv=0;
2153
2154 if (num_requests > 1)
2155 helpserv_notice(user, "HSMSG_REQ_FOUNDMANY");
2156
2157 helpserv_notice(user, "HSMSG_REQ_INFO_1", req->id);
2158 helpserv_show(from_opserv, hs, user, req);
2159 return 0;
2160 }
2161
2162 static HELPSERV_FUNC(cmd_pickup) {
2163 struct helpserv_request *req;
2164 struct helpserv_user *hs_user=GetHSUser(hs, user->handle_info);
2165 int num_requests=0;
2166
2167 REQUIRE_PARMS(2);
2168
2169 assert(hs_user);
2170
2171 if (!(req = smart_get_request(hs, hs_user, argv[1], &num_requests))) {
2172 helpserv_notice(user, "HSMSG_REQ_INVALID", argv[1]);
2173 return 0;
2174 }
2175
2176 if (num_requests > 1)
2177 helpserv_notice(user, "HSMSG_REQ_FOUNDMANY");
2178
2179 return helpserv_assign(from_opserv, hs, user, user, req);
2180 }
2181
2182 static HELPSERV_FUNC(cmd_reassign) {
2183 struct helpserv_request *req;
2184 struct userNode *targetuser;
2185 struct helpserv_user *target;
2186 struct helpserv_user *hs_user=GetHSUser(hs, user->handle_info);
2187 int num_requests=0;
2188
2189 REQUIRE_PARMS(3);
2190
2191 assert(hs_user);
2192
2193 if (!(req = smart_get_request(hs, hs_user, argv[1], &num_requests))) {
2194 helpserv_notice(user, "HSMSG_REQ_INVALID", argv[1]);
2195 return 0;
2196 }
2197
2198 if (num_requests > 1)
2199 helpserv_notice(user, "HSMSG_REQ_FOUNDMANY");
2200
2201 if (!(targetuser = GetUserH(argv[2]))) {
2202 helpserv_notice(user, "MSG_NICK_UNKNOWN", argv[2]);
2203 return 0;
2204 }
2205
2206 if (!targetuser->handle_info) {
2207 helpserv_notice(user, "MSG_USER_AUTHENTICATE", targetuser->nick);
2208 return 0;
2209 }
2210
2211 if (!(target = GetHSUser(hs, targetuser->handle_info))) {
2212 helpserv_notice(user, "HSMSG_NOT_IN_USERLIST", targetuser->nick, hs->helpserv->nick);
2213 return 0;
2214 }
2215
2216 if ((hs->persist_types[PERSIST_T_HELPER] == PERSIST_PART) && !GetUserMode(hs->helpchan, user) && (hs_user->level < HlManager)) {
2217 helpserv_notice(user, "HSMSG_REQ_HIM_NOT_IN_HELPCHAN", targetuser->nick, hs->helpchan->name);
2218 return 0;
2219 }
2220
2221 helpserv_assign(from_opserv, hs, targetuser, user, req);
2222 return 1;
2223 }
2224
2225 static HELPSERV_FUNC(cmd_addnote) {
2226 char text[MAX_LINE_SIZE], timestr[MAX_LINE_SIZE], *note;
2227 struct helpserv_request *req;
2228 struct helpserv_user *hs_user=GetHSUser(hs, user->handle_info);
2229 int num_requests=0;
2230
2231 REQUIRE_PARMS(3);
2232
2233 if (!(req = smart_get_request(hs, hs_user, argv[1], &num_requests))) {
2234 helpserv_notice(user, "HSMSG_REQ_INVALID", argv[1]);
2235 return 0;
2236 }
2237
2238 if (num_requests > 1)
2239 helpserv_notice(user, "HSMSG_REQ_FOUNDMANY");
2240
2241 note = unsplit_string(argv+2, argc-2, NULL);
2242
2243 strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&now));
2244 snprintf(text, MAX_LINE_SIZE, "[Helper note at %s]:", timestr);
2245 string_list_append(req->text, strdup(text));
2246 snprintf(text, MAX_LINE_SIZE, " <%s> %s", user->handle_info->handle, note);
2247 string_list_append(req->text, strdup(text));
2248
2249 helpserv_notice(user, "HSMSG_REQMSG_NOTE_ADDED", req->id);
2250
2251 return 1;
2252 }
2253
2254 static HELPSERV_FUNC(cmd_page) {
2255 REQUIRE_PARMS(2);
2256
2257 helpserv_page(PGSRC_COMMAND, "HSMSG_PAGE_REQUEST", user->nick, unsplit_string(argv+1, argc-1, NULL));
2258
2259 return 1;
2260 }
2261
2262 static HELPSERV_FUNC(cmd_stats) {
2263 struct helpserv_user *target, *hs_user;
2264 struct handle_info *target_handle;
2265 struct helpfile_table tbl;
2266 int i;
2267 char intervalstr[INTERVALLEN], buf[16];
2268
2269 hs_user = from_opserv ? NULL : GetHSUser(hs, user->handle_info);
2270
2271 if (argc > 1) {
2272 if (!from_opserv && (hs_user->level < HlManager)) {
2273 helpserv_notice(user, "HSMSG_NEED_MANAGER");
2274 return 0;
2275 }
2276
2277 if (!(target_handle = helpserv_get_handle_info(user, argv[1]))) {
2278 return 0;
2279 }
2280
2281 if (!(target = GetHSUser(hs, target_handle))) {
2282 helpserv_notice(user, "HSMSG_NOT_IN_USERLIST", target_handle->handle, hs->helpserv->nick);
2283 return 0;
2284 }
2285 } else {
2286 if (from_opserv) {
2287 helpserv_notice(user, "HSMSG_OPSERV_NEED_USER");
2288 return 0;
2289 }
2290 target = hs_user;
2291 }
2292
2293 helpserv_notice(user, "HSMSG_STATS_TOP", hs->helpserv->nick, target->handle->handle, weekday_names[target->week_start]);
2294
2295 tbl.length = 6;
2296 tbl.width = 2;
2297 tbl.flags = TABLE_NO_FREE;
2298 tbl.contents = alloca(tbl.length * sizeof(*tbl.contents));
2299 tbl.contents[0] = alloca(tbl.width * sizeof(**tbl.contents));
2300 tbl.contents[0][0] = "";
2301 tbl.contents[0][1] = "Recorded time";
2302 for (i=0; i < 5; i++) {
2303 unsigned int week_time = target->time_per_week[i];
2304 tbl.contents[i+1] = alloca(tbl.width * sizeof(**tbl.contents));
2305 if ((i == 0 || i == 4) && target->join_time)
2306 week_time += now - target->join_time;
2307 helpserv_interval(intervalstr, week_time);
2308 tbl.contents[i+1][1] = strdup(intervalstr);
2309 }
2310 tbl.contents[1][0] = "This week";
2311 tbl.contents[2][0] = "Last week";
2312 tbl.contents[3][0] = "2 weeks ago";
2313 tbl.contents[4][0] = "3 weeks ago";
2314 tbl.contents[5][0] = "Total";
2315
2316 helpserv_notice(user, "HSMSG_STATS_TIME", hs->helpchan->name);
2317 table_send((from_opserv ? opserv : hs->helpserv), user->nick, 0, NULL, tbl);
2318
2319 for (i=1; i <= 5; i++)
2320 free((char *)tbl.contents[i][1]);
2321
2322 tbl.length = 5;
2323 tbl.width = 4;
2324 tbl.flags = TABLE_NO_FREE;
2325 tbl.contents = alloca(tbl.length * sizeof(*tbl.contents));
2326 tbl.contents[0] = alloca(tbl.width * sizeof(**tbl.contents));
2327 tbl.contents[1] = alloca(tbl.width * sizeof(**tbl.contents));
2328 tbl.contents[2] = alloca(tbl.width * sizeof(**tbl.contents));
2329 tbl.contents[3] = alloca(tbl.width * sizeof(**tbl.contents));
2330 tbl.contents[4] = alloca(tbl.width * sizeof(**tbl.contents));
2331 tbl.contents[0][0] = "Category";
2332 tbl.contents[0][1] = "This week";
2333 tbl.contents[0][2] = "Last week";
2334 tbl.contents[0][3] = "Total";
2335
2336 tbl.contents[1][0] = "Requests picked up";
2337 for (i=0; i < 3; i++) {
2338 sprintf(buf, "%u", target->picked_up[(i == 2 ? 4 : i)]);
2339 tbl.contents[1][i+1] = strdup(buf);
2340 }
2341 tbl.contents[2][0] = "Requests closed";
2342 for (i=0; i < 3; i++) {
2343 sprintf(buf, "%u", target->closed[(i == 2 ? 4 : i)]);
2344 tbl.contents[2][i+1] = strdup(buf);
2345 }
2346 tbl.contents[3][0] = "Reassigned from";
2347 for (i=0; i < 3; i++) {
2348 sprintf(buf, "%u", target->reassigned_from[(i == 2 ? 4 : i)]);
2349 tbl.contents[3][i+1] = strdup(buf);
2350 }
2351 tbl.contents[4][0] = "Reassigned to";
2352 for (i=0; i < 3; i++) {
2353 sprintf(buf, "%u", target->reassigned_to[(i == 2 ? 4 : i)]);
2354 tbl.contents[4][i+1] = strdup(buf);
2355 }
2356
2357 helpserv_notice(user, "HSMSG_STATS_REQS");
2358 table_send((from_opserv ? opserv : hs->helpserv), user->nick, 0, NULL, tbl);
2359
2360 for (i=1; i < 5; i++) {
2361 free((char *)tbl.contents[i][1]);
2362 free((char *)tbl.contents[i][2]);
2363 free((char *)tbl.contents[i][3]);
2364 }
2365
2366 return 1;
2367 }
2368
2369 static HELPSERV_FUNC(cmd_statsreport) {
2370 int use_privmsg=1;
2371 struct helpfile_table tbl;
2372 dict_iterator_t it;
2373 unsigned int line, i;
2374 struct userNode *srcbot = from_opserv ? opserv : hs->helpserv;
2375
2376 if ((argc > 1) && !irccasecmp(argv[1], "NOTICE"))
2377 use_privmsg = 0;
2378
2379 tbl.length = dict_size(hs->users)+1;
2380 tbl.width = 3;
2381 tbl.flags = TABLE_NO_FREE;
2382 tbl.contents = alloca(tbl.length * sizeof(*tbl.contents));
2383 tbl.contents[0] = alloca(tbl.width * sizeof(**tbl.contents));
2384 tbl.contents[0][0] = "Account";
2385 tbl.contents[0][1] = "Requests";
2386 tbl.contents[0][2] = "Time helping";
2387
2388 for (it=dict_first(hs->users), line=0; it; it=iter_next(it)) {
2389 struct helpserv_user *hs_user=iter_data(it);
2390
2391 tbl.contents[++line] = alloca(tbl.width * sizeof(**tbl.contents));
2392 tbl.contents[line][0] = hs_user->handle->handle;
2393 tbl.contents[line][1] = malloc(12);
2394 tbl.contents[line][2] = malloc(32); /* A bit more than needed */
2395 }
2396
2397 /* 4 to 1 instead of 3 to 0 because it's unsigned */
2398 for (i=4; i > 0; i--) {
2399 for (it=dict_first(hs->users), line=0; it; it=iter_next(it)) {
2400 struct helpserv_user *hs_user = iter_data(it);
2401 /* Time */
2402 unsigned int week_time = hs_user->time_per_week[i-1];
2403 if ((i==1) && hs_user->join_time)
2404 week_time += now - hs_user->join_time;
2405 helpserv_interval((char *)tbl.contents[++line][2], week_time);
2406
2407 /* Requests */
2408 sprintf((char *)tbl.contents[line][1], "%u", hs_user->picked_up[i-1]+hs_user->reassigned_to[i-1]);
2409 }
2410 send_target_message(use_privmsg, user->nick, srcbot, statsreport_week[i-1]);
2411 table_send(srcbot, user->nick, 0, (use_privmsg ? irc_privmsg : irc_notice), tbl);
2412 }
2413
2414 for (line=1; line <= dict_size(hs->users); line++) {
2415 free((char *)tbl.contents[line][1]);
2416 free((char *)tbl.contents[line][2]);
2417 }
2418
2419 return 1;
2420 }
2421
2422 static int
2423 helpserv_in_channel(struct helpserv_bot *hs, struct chanNode *channel) {
2424 enum page_source pgsrc;
2425 if (channel == hs->helpchan)
2426 return 1;
2427 for (pgsrc=0; pgsrc<PGSRC_COUNT; pgsrc++)
2428 if (channel == hs->page_targets[pgsrc])
2429 return 1;
2430 return 0;
2431 }
2432
2433 static HELPSERV_FUNC(cmd_move) {
2434 if (!hs) {
2435 helpserv_notice(user, "HSMSG_INVALID_BOT");
2436 return 0;
2437 }
2438
2439 REQUIRE_PARMS(2);
2440
2441 if (is_valid_nick(argv[1])) {
2442 char *newnick = argv[1], oldnick[NICKLEN];
2443
2444 strcpy(oldnick, hs->helpserv->nick);
2445
2446 if (GetUserH(newnick)) {
2447 helpserv_notice(user, "HSMSG_NICK_EXISTS", newnick);
2448 return 0;
2449 }
2450
2451 dict_remove2(helpserv_bots_dict, hs->helpserv->nick, 1);
2452 NickChange(hs->helpserv, newnick, 0);
2453 dict_insert(helpserv_bots_dict, hs->helpserv->nick, hs);
2454
2455 helpserv_notice(user, "HSMSG_RENAMED", oldnick, newnick);
2456
2457 global_message_args(MESSAGE_RECIPIENT_OPERS, "HSMSG_BOT_RENAMED", oldnick,
2458 hs->helpchan->name, newnick, user->nick);
2459
2460 return 1;
2461 } else if (IsChannelName(argv[1])) {
2462 struct chanNode *old_helpchan = hs->helpchan;
2463 char *newchan = argv[1], oldchan[CHANNELLEN], reason[MAXLEN];
2464 struct helpserv_botlist *botlist;
2465
2466 strcpy(oldchan, hs->helpchan->name);
2467
2468 if (!irccasecmp(oldchan, newchan)) {
2469 helpserv_notice(user, "HSMSG_MOVE_SAME_CHANNEL", hs->helpserv->nick);
2470 return 0;
2471 }
2472
2473 if (opserv_bad_channel(newchan)) {
2474 helpserv_notice(user, "HSMSG_ILLEGAL_CHANNEL", newchan);
2475 return 0;
2476 }
2477
2478 botlist = dict_find(helpserv_bots_bychan_dict, hs->helpchan->name, NULL);
2479 helpserv_botlist_remove(botlist, hs);
2480 if (botlist->used == 0) {
2481 dict_remove(helpserv_bots_bychan_dict, hs->helpchan->name);
2482 }
2483
2484 hs->helpchan = NULL;
2485 if (!helpserv_in_channel(hs, old_helpchan)) {
2486 snprintf(reason, MAXLEN, "Moved to %s by %s.", newchan, user->nick);
2487 DelChannelUser(hs->helpserv, old_helpchan, reason, 0);
2488 }
2489
2490 if (!(hs->helpchan = GetChannel(newchan))) {
2491 hs->helpchan = AddChannel(newchan, now, NULL, NULL, NULL);
2492 AddChannelUser(hs->helpserv, hs->helpchan)->modes |= MODE_CHANOP;
2493 } else if (!helpserv_in_channel(hs, old_helpchan)) {
2494 struct mod_chanmode change;
2495 mod_chanmode_init(&change);
2496 change.argc = 1;
2497 change.args[0].mode = MODE_CHANOP;
2498 change.args[0].u.member = AddChannelUser(hs->helpserv, hs->helpchan);
2499 mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
2500 }
2501
2502 if (!(botlist = dict_find(helpserv_bots_bychan_dict, hs->helpchan->name, NULL))) {
2503 botlist = helpserv_botlist_alloc();
2504 dict_insert(helpserv_bots_bychan_dict, hs->helpchan->name, botlist);
2505 }
2506 helpserv_botlist_append(botlist, hs);
2507
2508 global_message_args(MESSAGE_RECIPIENT_OPERS, "HSMSG_BOT_MOVED", hs->helpserv->nick,
2509 oldchan, newchan, user->nick);
2510
2511 return 1;
2512 } else {
2513 helpserv_notice(user, "HSMSG_INVALID_MOVE", argv[1]);
2514 return 0;
2515 }
2516 }
2517
2518 static HELPSERV_FUNC(cmd_bots) {
2519 dict_iterator_t it;
2520 struct helpfile_table tbl;
2521 unsigned int i;
2522
2523 helpserv_notice(user, "HSMSG_BOTLIST_HEADER");
2524
2525 tbl.length = dict_size(helpserv_bots_dict)+1;
2526 tbl.width = 4;
2527 tbl.flags = TABLE_NO_FREE;
2528 tbl.contents = alloca(tbl.length * sizeof(*tbl.contents));
2529 tbl.contents[0] = alloca(tbl.width * sizeof(**tbl.contents));
2530 tbl.contents[0][0] = "Bot";
2531 tbl.contents[0][1] = "Channel";
2532 tbl.contents[0][2] = "Owner";
2533 tbl.contents[0][3] = "Inactivity";
2534
2535 for (it=dict_first(helpserv_bots_dict), i=1; it; it=iter_next(it), i++) {
2536 dict_iterator_t it2;
2537 struct helpserv_bot *bot;
2538 struct helpserv_user *owner=NULL;
2539
2540 bot = iter_data(it);
2541
2542 for (it2=dict_first(bot->users); it2; it2=iter_next(it2)) {
2543 if (((struct helpserv_user *)iter_data(it2))->level == HlOwner) {
2544 owner = iter_data(it2);
2545 break;
2546 }
2547 }
2548
2549 tbl.contents[i] = alloca(tbl.width * sizeof(**tbl.contents));
2550 tbl.contents[i][0] = iter_key(it);
2551 tbl.contents[i][1] = bot->helpchan->name;
2552 tbl.contents[i][2] = owner ? owner->handle->handle : "None";
2553 tbl.contents[i][3] = alloca(INTERVALLEN);
2554 intervalString((char*)tbl.contents[i][3], now - bot->last_active, user->handle_info);
2555 }
2556
2557 table_send((from_opserv ? opserv : hs->helpserv), user->nick, 0, NULL, tbl);
2558
2559 return 1;
2560 }
2561
2562 static void helpserv_page_helper_gone(struct helpserv_bot *hs, struct helpserv_request *req, const char *reason) {
2563 const int from_opserv = 0;
2564
2565 if (!req->helper)
2566 return;
2567
2568 /* Let the user know that their request is now unhandled */
2569 if (req->user) {
2570 struct modeNode *mn = GetUserMode(hs->helpchan, req->user);
2571 helpserv_msguser(req->user, "HSMSG_REQ_UNASSIGNED", req->id, reason);
2572 if (hs->auto_devoice && mn && (mn->modes & MODE_VOICE)) {
2573 struct mod_chanmode change;
2574 mod_chanmode_init(&change);
2575 change.argc = 1;
2576 change.args[0].mode = MODE_REMOVE | MODE_VOICE;
2577 change.args[0].u.member = mn;
2578 mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
2579 }
2580 if(req->handle)
2581 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_HELPER_GONE_1", req->id, req->user->nick, req->handle->handle, req->helper->handle->handle, reason);
2582 else
2583 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_HELPER_GONE_2", req->id, req->user->nick, req->helper->handle->handle, reason);
2584 } else {
2585 if(req->handle)
2586 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_HELPER_GONE_3", req->id, req->handle->handle, req->helper->handle->handle, reason);
2587 else
2588 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_HELPER_GONE_2", req->id, req->helper->handle->handle, reason);
2589 }
2590
2591 /* Now put it back in the queue */
2592 if (hs->unhandled == NULL) {
2593 /* Nothing there, put it at the front */
2594 hs->unhandled = req;
2595 req->next_unhandled = NULL;
2596 } else {
2597 /* Should it be at the front? */
2598 if (hs->unhandled->opened >= req->opened) {
2599 req->next_unhandled = hs->unhandled;
2600 hs->unhandled = req;
2601 } else {
2602 struct helpserv_request *unhandled;
2603 /* Find the request that this should be inserted AFTER */
2604 for (unhandled=hs->unhandled; unhandled->next_unhandled && (unhandled->next_unhandled->opened < req->opened); unhandled = unhandled->next_unhandled);
2605 req->next_unhandled = unhandled->next_unhandled;
2606 unhandled->next_unhandled = req;
2607 }
2608 }
2609
2610 req->helper = NULL;
2611 }
2612
2613 /* This takes care of WHINE_DELAY and IDLE_DELAY */
2614 static void run_whine_interval(void *data) {
2615 struct helpserv_bot *hs=data;
2616 struct helpfile_table tbl;
2617 unsigned int i;
2618
2619 /* First, run the WHINE_DELAY */
2620 if (hs->intervals[INTERVAL_WHINE_DELAY]
2621 && (hs->page_types[PGSRC_ALERT] != PAGE_NONE)
2622 && (hs->page_targets[PGSRC_ALERT] != NULL)
2623 && (!hs->intervals[INTERVAL_EMPTY_INTERVAL] || !hs->helpchan_empty)) {
2624 struct helpserv_request *unh;
2625 struct helpserv_reqlist reqlist;
2626 unsigned int queuesize=0;
2627
2628 helpserv_reqlist_init(&reqlist);
2629
2630 for (unh = hs->unhandled; unh; unh = unh->next_unhandled) {
2631 queuesize++;
2632 if ((now - unh->opened) >= (time_t)hs->intervals[INTERVAL_WHINE_DELAY]) {
2633 helpserv_reqlist_append(&reqlist, unh);
2634 }
2635 }
2636
2637 if (reqlist.used) {
2638 char strwhinedelay[INTERVALLEN];
2639
2640 intervalString(strwhinedelay, (time_t)hs->intervals[INTERVAL_WHINE_DELAY], NULL);
2641 #if ANNOYING_ALERT_PAGES
2642 tbl.length = reqlist.used + 1;
2643 tbl.width = 4;
2644 tbl.flags = TABLE_NO_FREE;
2645 tbl.contents = alloca(tbl.length * sizeof(*tbl.contents));
2646 tbl.contents[0] = alloca(tbl.width * sizeof(**tbl.contents));
2647 tbl.contents[0][0] = "ID#";
2648 tbl.contents[0][1] = "Nick";
2649 tbl.contents[0][2] = "Account";
2650 tbl.contents[0][3] = "Waiting time";
2651
2652 for (i=1; i <= reqlist.used; i++) {
2653 char reqid[12], unh_time[INTERVALLEN];
2654 unh = reqlist.list[i-1];
2655
2656 tbl.contents[i] = alloca(tbl.width * sizeof(**tbl.contents));
2657 sprintf(reqid, "%lu", unh->id);
2658 tbl.contents[i][0] = strdup(reqid);
2659 tbl.contents[i][1] = unh->user ? unh->user->nick : "Not online";
2660 tbl.contents[i][2] = unh->handle ? unh->handle->handle : "Not authed";
2661 intervalString(unh_time, now - unh->opened, NULL);
2662 tbl.contents[i][3] = strdup(unh_time);
2663 }
2664
2665 helpserv_page(PGSRC_ALERT, "HSMSG_PAGE_WHINE_HEADER", reqlist.used, strwhinedelay, queuesize);
2666 table_send(hs->helpserv, hs->page_targets[PGSRC_ALERT]->name, 0, page_type_funcs[hs->page_types[PGSRC_ALERT]], tbl);
2667
2668 for (i=1; i <= reqlist.used; i++) {
2669 free((char *)tbl.contents[i][0]);
2670 free((char *)tbl.contents[i][3]);
2671 }
2672 #else
2673 helpserv_page(PGSRC_ALERT, "HSMSG_PAGE_WHINE_HEADER", reqlist.used, strwhinedelay, queuesize);
2674 #endif
2675 }
2676
2677 helpserv_reqlist_clean(&reqlist);
2678 }
2679
2680 /* Next run IDLE_DELAY */
2681 if (hs->intervals[INTERVAL_IDLE_DELAY]
2682 && (hs->page_types[PGSRC_STATUS] != PAGE_NONE)
2683 && (hs->page_targets[PGSRC_STATUS] != NULL)) {
2684 struct modeList mode_list;
2685
2686 modeList_init(&mode_list);
2687
2688 for (i=0; i < hs->helpchan->members.used; i++) {
2689 struct modeNode *mn = hs->helpchan->members.list[i];
2690 /* Ignore ops. Perhaps this should be a set option? */
2691 if (mn->modes & MODE_CHANOP)
2692 continue;
2693 /* Check if they've been idle long enough */
2694 if ((unsigned)(now - mn->idle_since) < hs->intervals[INTERVAL_IDLE_DELAY])
2695 continue;
2696 /* Add them to the list of idle people.. */
2697 modeList_append(&mode_list, mn);
2698 }
2699
2700 if (mode_list.used) {
2701 char stridledelay[INTERVALLEN];
2702
2703 tbl.length = mode_list.used + 1;
2704 tbl.width = 4;
2705 tbl.flags = TABLE_NO_FREE;
2706 tbl.contents = alloca(tbl.length * sizeof(*tbl.contents));
2707 tbl.contents[0] = alloca(tbl.width * sizeof(**tbl.contents));
2708 tbl.contents[0][0] = "Nick";
2709 tbl.contents[0][1] = "Account";
2710 tbl.contents[0][2] = "ID#";
2711 tbl.contents[0][3] = "Idle time";
2712
2713 for (i=1; i <= mode_list.used; i++) {
2714 char reqid[12], idle_time[INTERVALLEN];
2715 struct helpserv_reqlist *reqlist;
2716 struct modeNode *mn = mode_list.list[i-1];
2717
2718 tbl.contents[i] = alloca(tbl.width * sizeof(**tbl.contents));
2719 tbl.contents[i][0] = mn->user->nick;
2720 tbl.contents[i][1] = mn->user->handle_info ? mn->user->handle_info->handle : "Not authed";
2721
2722 if ((reqlist = dict_find(helpserv_reqs_bynick_dict, mn->user->nick, NULL))) {
2723 int j;
2724
2725 for (j = reqlist->used-1; j >= 0; j--) {
2726 struct helpserv_request *req = reqlist->list[j];
2727
2728 if (req->hs == hs) {
2729 sprintf(reqid, "%lu", req->id);
2730 break;
2731 }
2732 }
2733
2734 if (j < 0)
2735 strcpy(reqid, "None");
2736 } else {
2737 strcpy(reqid, "None");
2738 }
2739 tbl.contents[i][2] = strdup(reqid);
2740
2741 intervalString(idle_time, now - mn->idle_since, NULL);
2742 tbl.contents[i][3] = strdup(idle_time);
2743 }
2744
2745 intervalString(stridledelay, (time_t)hs->intervals[INTERVAL_IDLE_DELAY], NULL);
2746 helpserv_page(PGSRC_STATUS, "HSMSG_PAGE_IDLE_HEADER", mode_list.used, hs->helpchan->name, stridledelay);
2747 table_send(hs->helpserv, hs->page_targets[PGSRC_STATUS]->name, 0, page_types[hs->page_types[PGSRC_STATUS]].func, tbl);
2748
2749 for (i=1; i <= mode_list.used; i++) {
2750 free((char *)tbl.contents[i][2]);
2751 free((char *)tbl.contents[i][3]);
2752 }
2753 }
2754
2755 modeList_clean(&mode_list);
2756 }
2757
2758 if (hs->intervals[INTERVAL_WHINE_INTERVAL]) {
2759 timeq_add(now + hs->intervals[INTERVAL_WHINE_INTERVAL], run_whine_interval, hs);
2760 }
2761 }
2762
2763 /* Returns -1 if there's any helpers,
2764 * 0 if there are no helpers
2765 * >1 if there are trials (number of trials)
2766 */
2767 static int find_helpchan_helpers(struct helpserv_bot *hs) {
2768 int num_trials=0;
2769 dict_iterator_t it;
2770
2771 for (it=dict_first(hs->users); it; it=iter_next(it)) {
2772 struct helpserv_user *hs_user=iter_data(it);
2773
2774 if (find_handle_in_channel(hs->helpchan, hs_user->handle, NULL)) {
2775 if (hs_user->level >= HlHelper) {
2776 hs->helpchan_empty = 0;
2777 return -1;
2778 }
2779 num_trials++;
2780 }
2781 }
2782
2783 hs->helpchan_empty = 1;
2784 return num_trials;
2785 }
2786
2787
2788 static void run_empty_interval(void *data) {
2789 struct helpserv_bot *hs=data;
2790 int num_trials=find_helpchan_helpers(hs);
2791 unsigned int num_unh;
2792 struct helpserv_request *unh;
2793
2794 if (num_trials == -1)
2795 return;
2796 if (hs->req_on_join && !hs->unhandled)
2797 return;
2798
2799 for (num_unh=0, unh=hs->unhandled; unh; num_unh++)
2800 unh = unh->next_unhandled;
2801
2802 if (num_trials)
2803 helpserv_page(PGSRC_ALERT, "HSMSG_PAGE_ONLYTRIALALERT", hs->helpchan->name, num_trials, num_unh);
2804 else
2805 helpserv_page(PGSRC_ALERT, "HSMSG_PAGE_EMPTYALERT", hs->helpchan->name, num_unh);
2806
2807 if (hs->intervals[INTERVAL_EMPTY_INTERVAL])
2808 timeq_add(now + hs->intervals[INTERVAL_EMPTY_INTERVAL], run_empty_interval, hs);
2809 }
2810
2811 static void free_user(void *data) {
2812 struct helpserv_user *hs_user = data;
2813 struct helpserv_bot *hs = hs_user->hs;
2814 struct helpserv_userlist *userlist;
2815 dict_iterator_t it;
2816
2817 if (hs->requests) {
2818 for (it=dict_first(hs->requests); it; it=iter_next(it)) {
2819 struct helpserv_request *req = iter_data(it);
2820
2821 if (req->helper == hs_user)
2822 helpserv_page_helper_gone(hs, req, "been deleted");
2823 }
2824 }
2825
2826 userlist = dict_find(helpserv_users_byhand_dict, hs_user->handle->handle, NULL);
2827 if (userlist->used == 1) {
2828 dict_remove(helpserv_users_byhand_dict, hs_user->handle->handle);
2829 } else {
2830 helpserv_userlist_remove(userlist, hs_user);
2831 }
2832
2833 free(data);
2834 }
2835
2836 static struct helpserv_bot *register_helpserv(const char *nick, const char *help_channel, const char *registrar) {
2837 struct helpserv_bot *hs;
2838 struct helpserv_botlist *botlist;
2839
2840 /* Laziness dictates calloc, since there's a lot to set to NULL or 0, and
2841 * it's a harmless default */
2842 hs = calloc(1, sizeof(struct helpserv_bot));
2843
2844 if (!(hs->helpserv = AddService(nick, "+iok", helpserv_conf.description, NULL))) {
2845 free(hs);
2846 return NULL;
2847 }
2848
2849 reg_privmsg_func(hs->helpserv, helpserv_botmsg);
2850
2851 if (!(hs->helpchan = GetChannel(help_channel))) {
2852 hs->helpchan = AddChannel(help_channel, now, NULL, NULL, NULL);
2853 AddChannelUser(hs->helpserv, hs->helpchan)->modes |= MODE_CHANOP;
2854 } else if (!hs->suspended) {
2855 struct mod_chanmode change;
2856 mod_chanmode_init(&change);
2857 change.argc = 1;
2858 change.args[0].mode = MODE_CHANOP;
2859 change.args[0].u.member = AddChannelUser(hs->helpserv, hs->helpchan);
2860 mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
2861 }
2862
2863 if (registrar)
2864 hs->registrar = strdup(registrar);
2865
2866 hs->users = dict_new();
2867 /* Don't free keys - they use the handle_info's handle field */
2868 dict_set_free_data(hs->users, free_user);
2869 hs->requests = dict_new();
2870 dict_set_free_keys(hs->requests, free);
2871 dict_set_free_data(hs->requests, free_request);
2872
2873 dict_insert(helpserv_bots_dict, hs->helpserv->nick, hs);
2874
2875 if (!(botlist = dict_find(helpserv_bots_bychan_dict, hs->helpchan->name, NULL))) {
2876 botlist = helpserv_botlist_alloc();
2877 dict_insert(helpserv_bots_bychan_dict, hs->helpchan->name, botlist);
2878 }
2879 helpserv_botlist_append(botlist, hs);
2880
2881 return hs;
2882 }
2883
2884 static HELPSERV_FUNC(cmd_register) {
2885 char *nick, *helpchan;
2886 struct handle_info *handle;
2887
2888 REQUIRE_PARMS(4);
2889 nick = argv[1];
2890 if (!is_valid_nick(nick)) {
2891 helpserv_notice(user, "HSMSG_ILLEGAL_NICK", nick);
2892 return 0;
2893 }
2894 if (GetUserH(nick)) {
2895 helpserv_notice(user, "HSMSG_NICK_EXISTS", nick);
2896 return 0;
2897 }
2898 helpchan = argv[2];
2899 if (!IsChannelName(helpchan)) {
2900 helpserv_notice(user, "HSMSG_ILLEGAL_CHANNEL", helpchan);
2901 HELPSERV_SYNTAX();
2902 return 0;
2903 }
2904 if (opserv_bad_channel(helpchan)) {
2905 helpserv_notice(user, "HSMSG_ILLEGAL_CHANNEL", helpchan);
2906 return 0;
2907 }
2908 if (!(handle = helpserv_get_handle_info(user, argv[3])))
2909 return 0;
2910
2911 if (!(hs = register_helpserv(nick, helpchan, user->handle_info->handle))) {
2912 helpserv_notice(user, "HSMSG_ERROR_ADDING_SERVICE", nick);
2913 return 0;
2914 }
2915
2916 hs->registered = now;
2917 helpserv_add_user(hs, handle, HlOwner);
2918
2919 helpserv_notice(user, "HSMSG_REG_SUCCESS", handle->handle, nick);
2920
2921 /* Not sent to helpers, since they can't register HelpServ */
2922 global_message_args(MESSAGE_RECIPIENT_OPERS, "HSMSG_BOT_REGISTERED", nick,
2923 hs->helpchan->name, handle->handle, user->nick);
2924 return 1;
2925 }
2926
2927 static void unregister_helpserv(struct helpserv_bot *hs) {
2928 enum message_type msgtype;
2929
2930 timeq_del(0, NULL, hs, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_FUNC);
2931
2932 /* Requests before users so that it doesn't spam mentioning now-unhandled
2933 * requests because the users were deleted */
2934 dict_delete(hs->requests);
2935 hs->requests = NULL; /* so we don't try to look up requests in free_user() */
2936 dict_delete(hs->users);
2937 free(hs->registrar);
2938
2939 for (msgtype=0; msgtype<MSGTYPE_COUNT; msgtype++)
2940 free(hs->messages[msgtype]);
2941 }
2942
2943 static void helpserv_free_bot(void *data) {
2944 unregister_helpserv(data);
2945 free(data);
2946 }
2947
2948 static void helpserv_expire_suspension(void *data) {
2949 struct helpserv_bot *hs = data;
2950 struct chanNode *channel;
2951 struct mod_chanmode *change;
2952
2953 channel = hs->helpchan;
2954 hs->suspended = 0;
2955 hs->expiry = 0;
2956 hs->issued = 0;
2957 hs->reason = NULL;
2958 hs->suspender = NULL;
2959
2960 change = mod_chanmode_alloc(1);
2961 change->argc = 1;
2962 change->args[0].mode = MODE_CHANOP;
2963 change->args[0].u.member = AddChannelUser(hs->helpserv, channel);
2964
2965 mod_chanmode_announce(hs->helpserv, channel, change);
2966 mod_chanmode_free(change);
2967 }
2968
2969 static void helpserv_unregister(struct helpserv_bot *bot, const char *quit_fmt, const char *global_fmt, const char *actor) {
2970 char reason[MAXLEN], channame[CHANNELLEN], botname[NICKLEN];
2971 struct helpserv_botlist *botlist;
2972 size_t len;
2973
2974 if (bot->suspended && bot->expiry)
2975 timeq_del(bot->expiry, helpserv_expire_suspension, bot, 0);
2976
2977 botlist = dict_find(helpserv_bots_bychan_dict, bot->helpchan->name, NULL);
2978 helpserv_botlist_remove(botlist, bot);
2979 if (!botlist->used)
2980 dict_remove(helpserv_bots_bychan_dict, bot->helpchan->name);
2981 len = strlen(bot->helpserv->nick) + 1;
2982 safestrncpy(botname, bot->helpserv->nick, len);
2983 len = strlen(bot->helpchan->name) + 1;
2984 safestrncpy(channame, bot->helpchan->name, len);
2985 snprintf(reason, sizeof(reason), quit_fmt, actor);
2986 DelUser(bot->helpserv, NULL, 1, reason);
2987 dict_remove(helpserv_bots_dict, botname);
2988 global_message_args(MESSAGE_RECIPIENT_OPERS, global_fmt, botname, channame, actor);
2989 }
2990
2991 static HELPSERV_FUNC(cmd_unregister) {
2992 if (!from_opserv) {
2993 if (argc < 2 || strcmp(argv[1], "CONFIRM")) {
2994 helpserv_notice(user, "HSMSG_NEED_UNREG_CONFIRM");
2995 return 0;
2996 }
2997 log_audit(HS_LOG, LOG_COMMAND, user, hs->helpserv, hs->helpchan->name, 0, "unregister CONFIRM");
2998 }
2999
3000 helpserv_unregister(hs, "Unregistered by %s", "HSMSG_BOT_UNREGISTERED", user->nick);
3001 return from_opserv;
3002 }
3003
3004 static HELPSERV_FUNC(cmd_suspend) {
3005 char reason[MAXLEN];
3006 struct helpserv_bot *hsb;
3007 time_t expiry, duration;
3008
3009 REQUIRE_PARMS(3);
3010
3011 if(!strcmp(argv[2], "0"))
3012 expiry = 0;
3013 else if((duration = ParseInterval(argv[2])))
3014 expiry = now + duration;
3015 else
3016 {
3017 helpserv_notice(user, "MSG_INVALID_DURATION", argv[1]);
3018 return 0;
3019 }
3020
3021 hsb = dict_find(helpserv_bots_dict, argv[1], NULL);
3022 if (!hsb) {
3023 helpserv_notice(user, "HSMSG_BOT_NON_EXIST", argv[1]);
3024 return 0;
3025 }
3026
3027 unsplit_string(argv + 3, argc - 3, reason);
3028
3029 hsb->suspended = 1;
3030 hsb->issued = now;
3031 hsb->expiry = expiry;
3032 hsb->reason = strdup(reason);
3033 hsb->suspender = strdup(user->handle_info->handle);
3034
3035 if(hsb->expiry)
3036 timeq_add(hsb->expiry, helpserv_expire_suspension, hsb);
3037
3038 DelChannelUser(hsb->helpserv, hsb->helpchan, hsb->reason, 0);
3039 helpserv_notice(user, "HSMSG_SUSPENDED", hsb->helpchan->name);
3040 global_message_args(MESSAGE_RECIPIENT_OPERS | MESSAGE_RECIPIENT_HELPERS, "HSMSG_SUSPENDED_BY",
3041 hsb->helpchan->name, hsb->suspender);
3042
3043 return 0;
3044 }
3045
3046 static HELPSERV_FUNC(cmd_unsuspend) {
3047 struct helpserv_bot *hsb;
3048
3049 hsb = dict_find(helpserv_bots_dict, argv[1], NULL);
3050 if (!hsb) {
3051 helpserv_notice(user, "HSMSG_BOT_NON_EXIST", argv[1]);
3052 return 0;
3053 }
3054
3055 if(!hsb->suspended)
3056 {
3057 helpserv_notice(user, "HSMSG_NOT_SUSPENDED", hsb->helpchan->name);
3058 return 0;
3059 }
3060
3061 /* Expire the suspension and join ChanServ to the channel. */
3062 timeq_del(hsb->expiry, helpserv_expire_suspension, hsb, 0);
3063 helpserv_expire_suspension(hsb);
3064 helpserv_notice(user, "HSMSG_UNSUSPENDED", hsb->helpchan->name);
3065 global_message_args(MESSAGE_RECIPIENT_OPERS|MESSAGE_RECIPIENT_HELPERS, "HSMSG_UNSUSPENDED_BY",
3066 hsb->helpchan->name, user->handle_info->handle);
3067 return 1;
3068 }
3069
3070 static HELPSERV_FUNC(cmd_expire) {
3071 struct helpserv_botlist victims;
3072 struct helpserv_bot *bot;
3073 dict_iterator_t it, next;
3074 unsigned int count = 0;
3075
3076 memset(&victims, 0, sizeof(victims));
3077 for (it = dict_first(helpserv_bots_dict); it; it = next) {
3078 bot = iter_data(it);
3079 next = iter_next(it);
3080 if ((unsigned int)(now - bot->last_active) < helpserv_conf.expire_age)
3081 continue;
3082 helpserv_unregister(bot, "Registration expired due to inactivity", "HSMSG_BOT_EXPIRED", user->nick);
3083 count++;
3084 }
3085 helpserv_notice(user, "HSMSG_EXPIRATION_DONE", count);
3086 return 1;
3087 }
3088
3089 static HELPSERV_FUNC(cmd_giveownership) {
3090 struct handle_info *hi;
3091 struct helpserv_user *new_owner, *old_owner, *hs_user;
3092 dict_iterator_t it;
3093 char reason[MAXLEN];
3094
3095 if (!from_opserv && ((argc < 3) || strcmp(argv[2], "CONFIRM"))) {
3096 helpserv_notice(user, "HSMSG_NEED_GIVEOWNERSHIP_CONFIRM");
3097 return 0;
3098 }
3099 hi = helpserv_get_handle_info(user, argv[1]);
3100 if (!hi)
3101 return 0;
3102 new_owner = GetHSUser(hs, hi);
3103 if (!new_owner) {
3104 helpserv_notice(user, "HSMSG_NOT_IN_USERLIST", hi->handle, hs->helpserv->nick);
3105 return 0;
3106 }
3107 if (!from_opserv)
3108 old_owner = GetHSUser(hs, user->handle_info);
3109 else for (it = dict_first(hs->users), old_owner = NULL; it; it = iter_next(it)) {
3110 hs_user = iter_data(it);
3111 if (hs_user->level != HlOwner)
3112 continue;
3113 if (old_owner) {
3114 helpserv_notice(user, "HSMSG_MULTIPLE_OWNERS", hs->helpserv->nick);
3115 return 0;
3116 }
3117 old_owner = hs_user;
3118 }
3119 if (!from_opserv && (new_owner->handle == user->handle_info)) {
3120 helpserv_notice(user, "HSMSG_NO_TRANSFER_SELF");
3121 return 0;
3122 }
3123 if (old_owner)
3124 old_owner->level = HlManager;
3125 new_owner->level = HlOwner;
3126 helpserv_notice(user, "HSMSG_OWNERSHIP_GIVEN", hs->helpserv->nick, new_owner->handle->handle);
3127 sprintf(reason, "%s (%s) ownership transferred to %s by %s.", hs->helpserv->nick, hs->helpchan->name, new_owner->handle->handle, user->handle_info->handle);
3128 return 1;
3129 }
3130
3131 static HELPSERV_FUNC(cmd_weekstart) {
3132 struct handle_info *hi;
3133 struct helpserv_user *actor, *victim;
3134 int changed = 0;
3135
3136 REQUIRE_PARMS(2);
3137 actor = from_opserv ? NULL : GetHSUser(hs, user->handle_info);
3138 if (!(hi = helpserv_get_handle_info(user, argv[1])))
3139 return 0;
3140 if (!(victim = GetHSUser(hs, hi))) {
3141 helpserv_notice(user, "HSMSG_NOT_IN_USERLIST", hi->handle, hs->helpserv->nick);
3142 return 0;
3143 }
3144 if (actor && (actor->level <= victim->level) && (actor != victim)) {
3145 helpserv_notice(user, "MSG_USER_OUTRANKED", victim->handle->handle);
3146 return 0;
3147 }
3148 if (argc > 2 && (!actor || actor->level >= HlManager)) {
3149 int new_day = 7;
3150 switch (argv[2][0]) {
3151 case 's': case 'S':
3152 if ((argv[2][1] == 'u') || (argv[2][1] == 'U'))
3153 new_day = 0;
3154 else if ((argv[2][1] == 'a') || (argv[2][1] == 'A'))
3155 new_day = 6;
3156 break;
3157 case 'm': case 'M': new_day = 1; break;
3158 case 't': case 'T':
3159 if ((argv[2][1] == 'u') || (argv[2][1] == 'U'))
3160 new_day = 2;
3161 else if ((argv[2][1] == 'h') || (argv[2][1] == 'H'))
3162 new_day = 4;
3163 break;
3164 case 'w': case 'W': new_day = 3; break;
3165 case 'f': case 'F': new_day = 5; break;
3166 }
3167 if (new_day == 7) {
3168 helpserv_notice(user, "HSMSG_BAD_WEEKDAY", argv[2]);
3169 return 0;
3170 }
3171 victim->week_start = new_day;
3172 changed = 1;
3173 }
3174 helpserv_notice(user, "HSMSG_WEEK_STARTS", victim->handle->handle, weekday_names[victim->week_start]);
3175 return changed;
3176 }
3177
3178 static void set_page_target(struct helpserv_bot *hs, enum page_source idx, const char *target) {
3179 struct chanNode *new_target, *old_target;
3180
3181 if (target) {
3182 if (!IsChannelName(target)) {
3183 log_module(HS_LOG, LOG_ERROR, "%s has an invalid page target.", hs->helpserv->nick);
3184 return;
3185 }
3186 new_target = GetChannel(target);
3187 if (!new_target) {
3188 new_target = AddChannel(target, now, NULL, NULL, NULL);
3189 AddChannelUser(hs->helpserv, new_target);
3190 }
3191 } else {
3192 new_target = NULL;
3193 }
3194 if (new_target == hs->page_targets[idx])
3195 return;
3196 old_target = hs->page_targets[idx];
3197 hs->page_targets[idx] = NULL;
3198 if (old_target && !helpserv_in_channel(hs, old_target))
3199 DelChannelUser(hs->helpserv, old_target, "Changing page target.", 0);
3200 if (new_target && !helpserv_in_channel(hs, new_target)) {
3201 struct mod_chanmode change;
3202 mod_chanmode_init(&change);
3203 change.argc = 1;
3204 change.args[0].mode = MODE_CHANOP;
3205 change.args[0].u.member = AddChannelUser(hs->helpserv, new_target);
3206 mod_chanmode_announce(hs->helpserv, new_target, &change);
3207 }
3208 hs->page_targets[idx] = new_target;
3209 }
3210
3211 static int opt_page_target(struct userNode *user, struct helpserv_bot *hs, int from_opserv, int argc, char *argv[], enum page_source idx) {
3212 int changed = 0;
3213
3214 if (argc > 0) {
3215 if (!IsOper(user)) {
3216 helpserv_notice(user, "HSMSG_SET_NEED_OPER");
3217 return 0;
3218 }
3219 if (!strcmp(argv[0], "*")) {
3220 set_page_target(hs, idx, NULL);
3221 changed = 1;
3222 } else if (!IsChannelName(argv[0])) {
3223 helpserv_notice(user, "MSG_NOT_CHANNEL_NAME");
3224 return 0;
3225 } else {
3226 set_page_target(hs, idx, argv[0]);
3227 changed = 1;
3228 }
3229 }
3230 if (hs->page_targets[idx])
3231 helpserv_notice(user, page_sources[idx].print_target, hs->page_targets[idx]->name);
3232 else
3233 helpserv_notice(user, page_sources[idx].print_target, user_find_message(user, "MSG_NONE"));
3234 return changed;
3235 }
3236
3237 static HELPSERV_OPTION(opt_pagetarget_command) {
3238 return opt_page_target(user, hs, from_opserv, argc, argv, PGSRC_COMMAND);
3239 }
3240
3241 static HELPSERV_OPTION(opt_pagetarget_alert) {
3242 return opt_page_target(user, hs, from_opserv, argc, argv, PGSRC_ALERT);
3243 }
3244
3245 static HELPSERV_OPTION(opt_pagetarget_status) {
3246 return opt_page_target(user, hs, from_opserv, argc, argv, PGSRC_STATUS);
3247 }
3248
3249 static enum page_type page_type_from_name(const char *name) {
3250 enum page_type type;
3251 for (type=0; type<PAGE_COUNT; type++)
3252 if (!irccasecmp(page_types[type].db_name, name))
3253 return type;
3254 return PAGE_COUNT;
3255 }
3256
3257 static int opt_page_type(struct userNode *user, struct helpserv_bot *hs, int from_opserv, int argc, char *argv[], enum page_source idx) {
3258 enum page_type new_type;
3259 int changed=0;
3260
3261 if (argc > 0) {
3262 new_type = page_type_from_name(argv[0]);
3263 if (new_type == PAGE_COUNT) {
3264 helpserv_notice(user, "HSMSG_INVALID_OPTION", argv[0]);
3265 return 0;
3266 }
3267 hs->page_types[idx] = new_type;
3268 changed = 1;
3269 }
3270 helpserv_notice(user, page_sources[idx].print_type,
3271 user_find_message(user, page_types[hs->page_types[idx]].print_name));
3272 return changed;
3273 }
3274
3275 static HELPSERV_OPTION(opt_pagetype) {
3276 return opt_page_type(user, hs, from_opserv, argc, argv, PGSRC_COMMAND);
3277 }
3278
3279 static HELPSERV_OPTION(opt_alert_page_type) {
3280 return opt_page_type(user, hs, from_opserv, argc, argv, PGSRC_ALERT);
3281 }
3282
3283 static HELPSERV_OPTION(opt_status_page_type) {
3284 return opt_page_type(user, hs, from_opserv, argc, argv, PGSRC_STATUS);
3285 }
3286
3287 static int opt_message(struct userNode *user, struct helpserv_bot *hs, int from_opserv, int argc, char *argv[], enum message_type idx) {
3288 int changed=0;
3289
3290 if (argc > 0) {
3291 char *msg = unsplit_string(argv, argc, NULL);
3292 free(hs->messages[idx]);
3293 hs->messages[idx] = strcmp(msg, "*") ? strdup(msg) : NULL;
3294 changed = 1;
3295 }
3296 if (hs->messages[idx])
3297 helpserv_notice(user, message_types[idx].print_name, hs->messages[idx]);
3298 else
3299 helpserv_notice(user, message_types[idx].print_name, user_find_message(user, "MSG_NONE"));
3300 return changed;
3301 }
3302
3303 static HELPSERV_OPTION(opt_greeting) {
3304 return opt_message(user, hs, from_opserv, argc, argv, MSGTYPE_GREETING);
3305 }
3306
3307 static HELPSERV_OPTION(opt_req_opened) {
3308 return opt_message(user, hs, from_opserv, argc, argv, MSGTYPE_REQ_OPENED);
3309 }
3310
3311 static HELPSERV_OPTION(opt_req_assigned) {
3312 return opt_message(user, hs, from_opserv, argc, argv, MSGTYPE_REQ_ASSIGNED);
3313 }
3314
3315 static HELPSERV_OPTION(opt_req_closed) {
3316 return opt_message(user, hs, from_opserv, argc, argv, MSGTYPE_REQ_CLOSED);
3317 }
3318
3319 static int opt_interval(struct userNode *user, struct helpserv_bot *hs, int from_opserv, int argc, char *argv[], enum interval_type idx, unsigned int min) {
3320 char buf[INTERVALLEN];
3321 int changed=0;
3322
3323 if (argc > 0) {
3324 unsigned long new_int = ParseInterval(argv[0]);
3325 if (!new_int && strcmp(argv[0], "0")) {
3326 helpserv_notice(user, "MSG_INVALID_DURATION", argv[0]);
3327 return 0;
3328 }
3329 if (new_int && new_int < min) {
3330 intervalString(buf, min, user->handle_info);
3331 helpserv_notice(user, "HSMSG_INVALID_INTERVAL", user_find_message(user, interval_types[idx].print_name), buf);
3332 return 0;
3333 }
3334 hs->intervals[idx] = new_int;
3335 changed = 1;
3336 }
3337 if (hs->intervals[idx]) {
3338 intervalString(buf, hs->intervals[idx], user->handle_info);
3339 helpserv_notice(user, interval_types[idx].print_name, buf);
3340 } else
3341 helpserv_notice(user, interval_types[idx].print_name, user_find_message(user, "HSMSG_0_DISABLED"));
3342 return changed;
3343 }
3344
3345 static HELPSERV_OPTION(opt_idle_delay) {
3346 return opt_interval(user, hs, from_opserv, argc, argv, INTERVAL_IDLE_DELAY, 60);
3347 }
3348
3349 static HELPSERV_OPTION(opt_whine_delay) {
3350 return opt_interval(user, hs, from_opserv, argc, argv, INTERVAL_WHINE_DELAY, 60);
3351 }
3352
3353 static HELPSERV_OPTION(opt_whine_interval) {
3354 unsigned int old_val = hs->intervals[INTERVAL_WHINE_INTERVAL];
3355 int retval;
3356
3357 retval = opt_interval(user, hs, from_opserv, argc, argv, INTERVAL_WHINE_INTERVAL, 60);
3358
3359 if (!old_val && hs->intervals[INTERVAL_WHINE_INTERVAL]) {
3360 timeq_add(now + hs->intervals[INTERVAL_WHINE_INTERVAL], run_whine_interval, hs);
3361 } else if (old_val && !hs->intervals[INTERVAL_WHINE_INTERVAL]) {
3362 timeq_del(0, run_whine_interval, hs, TIMEQ_IGNORE_WHEN);
3363 }
3364
3365 return retval;
3366 }
3367
3368 static HELPSERV_OPTION(opt_empty_interval) {
3369 unsigned int old_val = hs->intervals[INTERVAL_EMPTY_INTERVAL];
3370 int retval;
3371
3372 retval = opt_interval(user, hs, from_opserv, argc, argv, INTERVAL_EMPTY_INTERVAL, 60);
3373
3374 if (!old_val && hs->intervals[INTERVAL_EMPTY_INTERVAL]) {
3375 timeq_add(now + hs->intervals[INTERVAL_EMPTY_INTERVAL], run_empty_interval, hs);
3376 } else if (old_val && !hs->intervals[INTERVAL_EMPTY_INTERVAL]) {
3377 timeq_del(0, run_empty_interval, hs, TIMEQ_IGNORE_WHEN);
3378 }
3379
3380 return retval;
3381 }
3382
3383 static HELPSERV_OPTION(opt_stale_delay) {
3384 return opt_interval(user, hs, from_opserv, argc, argv, INTERVAL_STALE_DELAY, 60);
3385 }
3386
3387 static enum persistence_length persistence_from_name(const char *name) {
3388 enum persistence_length pers;
3389 for (pers=0; pers<PERSIST_COUNT; pers++)
3390 if (!irccasecmp(name, persistence_lengths[pers].db_name))
3391 return pers;
3392 return PERSIST_COUNT;
3393 }
3394
3395 static int opt_persist(struct userNode *user, struct helpserv_bot *hs, int from_opserv, int argc, char *argv[], enum persistence_type idx) {
3396 int changed=0;
3397
3398 if (argc > 0) {
3399 enum persistence_length new_pers = persistence_from_name(argv[0]);
3400 if (new_pers == PERSIST_COUNT) {
3401 helpserv_notice(user, "HSMSG_INVALID_OPTION", argv[0]);
3402 return 0;
3403 }
3404 hs->persist_types[idx] = new_pers;
3405 changed = 1;
3406 }
3407 helpserv_notice(user, persistence_types[idx].print_name,
3408 user_find_message(user, persistence_lengths[hs->persist_types[idx]].print_name));
3409 return changed;
3410 }
3411
3412 static HELPSERV_OPTION(opt_request_persistence) {
3413 return opt_persist(user, hs, from_opserv, argc, argv, PERSIST_T_REQUEST);
3414 }
3415
3416 static HELPSERV_OPTION(opt_helper_persistence) {
3417 return opt_persist(user, hs, from_opserv, argc, argv, PERSIST_T_HELPER);
3418 }
3419
3420 static enum notification_type notification_from_name(const char *name) {
3421 enum notification_type notify;
3422 for (notify=0; notify<NOTIFY_COUNT; notify++)
3423 if (!irccasecmp(name, notification_types[notify].db_name))
3424 return notify;
3425 return NOTIFY_COUNT;
3426 }
3427
3428 static HELPSERV_OPTION(opt_notification) {
3429 int changed=0;
3430
3431 if (argc > 0) {
3432 enum notification_type new_notify = notification_from_name(argv[0]);
3433 if (new_notify == NOTIFY_COUNT) {
3434 helpserv_notice(user, "HSMSG_INVALID_OPTION", argv[0]);
3435 return 0;
3436 }
3437 if (!from_opserv && (new_notify == NOTIFY_HANDLE)) {
3438 helpserv_notice(user, "HSMSG_SET_NEED_OPER");
3439 return 0;
3440 }
3441 hs->notify = new_notify;
3442 changed = 1;
3443 }
3444 helpserv_notice(user, "HSMSG_SET_NOTIFICATION", user_find_message(user, notification_types[hs->notify].print_name));
3445 return changed;
3446 }
3447
3448 #define OPTION_UINT(var, name) do { \
3449 int changed=0; \
3450 if (argc > 0) { \
3451 (var) = strtoul(argv[0], NULL, 0); \
3452 changed = 1; \
3453 } \
3454 helpserv_notice(user, name, (var)); \
3455 return changed; \
3456 } while (0);
3457
3458 static HELPSERV_OPTION(opt_id_wrap) {
3459 OPTION_UINT(hs->id_wrap, "HSMSG_SET_IDWRAP");
3460 }
3461
3462 static HELPSERV_OPTION(opt_req_maxlen) {
3463 OPTION_UINT(hs->req_maxlen, "HSMSG_SET_REQMAXLEN");
3464 }
3465
3466 #define OPTION_BINARY(var, name) do { \
3467 int changed=0; \
3468 if (argc > 0) { \
3469 if (enabled_string(argv[0])) { \
3470 (var) = 1; \
3471 changed = 1; \
3472 } else if (disabled_string(argv[0])) { \
3473 (var) = 0; \
3474 changed = 1; \
3475 } else { \
3476 helpserv_notice(user, "MSG_INVALID_BINARY", argv[0]); \
3477 return 0; \
3478 } \
3479 } \
3480 helpserv_notice(user, name, user_find_message(user, (var) ? "MSG_ON" : "MSG_OFF")); \
3481 return changed; \
3482 } while (0);
3483
3484 static HELPSERV_OPTION(opt_privmsg_only) {
3485 OPTION_BINARY(hs->privmsg_only, "HSMSG_SET_PRIVMSGONLY");
3486 }
3487
3488 static HELPSERV_OPTION(opt_req_on_join) {
3489 OPTION_BINARY(hs->req_on_join, "HSMSG_SET_REQONJOIN");
3490 }
3491
3492 static HELPSERV_OPTION(opt_auto_voice) {
3493 OPTION_BINARY(hs->auto_voice, "HSMSG_SET_AUTOVOICE");
3494 }
3495
3496 static HELPSERV_OPTION(opt_auto_devoice) {
3497 OPTION_BINARY(hs->auto_devoice, "HSMSG_SET_AUTODEVOICE");
3498 }
3499
3500 static HELPSERV_OPTION(opt_join_total) {
3501 OPTION_BINARY(hs->join_total, "HSMSG_SET_JOINTOTAL");
3502 }
3503
3504 static HELPSERV_OPTION(opt_alert_new) {
3505 OPTION_BINARY(hs->alert_new, "HSMSG_SET_ALERTNEW");
3506 }
3507
3508 static HELPSERV_FUNC(cmd_set) {
3509 helpserv_option_func_t *opt;
3510
3511 if (argc < 2) {
3512 unsigned int i;
3513 helpserv_option_func_t *display[] = {
3514 opt_pagetarget_command, opt_pagetarget_alert, opt_pagetarget_status,
3515 opt_pagetype, opt_alert_page_type, opt_status_page_type,
3516 opt_greeting, opt_req_opened, opt_req_assigned, opt_req_closed,
3517 opt_idle_delay, opt_whine_delay, opt_whine_interval,
3518 opt_empty_interval, opt_stale_delay, opt_request_persistence,
3519 opt_helper_persistence, opt_notification, opt_id_wrap,
3520 opt_req_maxlen, opt_privmsg_only, opt_req_on_join, opt_auto_voice,
3521 opt_auto_devoice, opt_join_total, opt_alert_new
3522 };
3523
3524 helpserv_notice(user, "HSMSG_QUEUE_OPTIONS");
3525 for (i=0; i<ArrayLength(display); i++)
3526 display[i](user, hs, from_opserv, 0, argv);
3527 return 1;
3528 }
3529
3530 if (!(opt = dict_find(helpserv_option_dict, argv[1], NULL))) {
3531 helpserv_notice(user, "HSMSG_INVALID_OPTION", argv[1]);
3532 return 0;
3533 }
3534
3535 if ((argc > 2) && !from_opserv) {
3536 struct helpserv_user *hs_user;
3537
3538 if (!(hs_user = dict_find(hs->users, user->handle_info->handle, NULL))) {
3539 helpserv_notice(user, "HSMSG_WTF_WHO_ARE_YOU", hs->helpserv->nick);
3540 return 0;
3541 }
3542
3543 if (hs_user->level < HlManager) {
3544 helpserv_notice(user, "HSMSG_NEED_MANAGER");
3545 return 0;
3546 }
3547 }
3548 return opt(user, hs, from_opserv, argc-2, argv+2);
3549 }
3550
3551 const char *
3552 get_helpserv_id(const char *nick, struct userNode *user) {
3553 char id[MAX_LINE_SIZE];
3554 int tid = 0;
3555 struct helpserv_bot *hs;
3556 struct userNode *target;
3557 dict_iterator_t it;
3558 struct helpserv_request *req=NULL, *newest=NULL;
3559 struct helpserv_reqlist *reqlist;
3560 unsigned int i;
3561
3562 if (!IsChannelName(nick))
3563 target = GetUserN(nick);
3564 else {
3565 sprintf(id, "%s", "$i");
3566 return strdup(id);
3567 }
3568
3569 for (it=dict_first(helpserv_bots_dict); it; it=iter_next(it)) {
3570 hs = iter_data(it);
3571 if (strcasecmp(user->nick, hs->helpserv->nick))
3572 continue;
3573
3574 if ((reqlist = dict_find(helpserv_reqs_bynick_dict, target->nick, NULL))) {
3575 for (i=0; i < reqlist->used; i++) {
3576 req = reqlist->list[i];
3577 if (req->hs != hs)
3578 continue;
3579 if (!newest || (newest->opened < req->opened))
3580 newest = req;
3581 }
3582
3583 /* If nothing was found, this will set req to NULL */
3584 req = newest;
3585 }
3586
3587 if (req) {
3588 tid = req->id;
3589 break;
3590 }
3591 }
3592
3593 if (tid)
3594 sprintf(id, "ID#%lu", req->id);
3595 else
3596 sprintf(id, "%s", "$i");
3597
3598 return strdup(id);
3599 }
3600
3601 static int user_write_helper(const char *key, void *data, void *extra) {
3602 struct helpserv_user *hs_user = data;
3603 struct saxdb_context *ctx = extra;
3604 struct string_list strlist;
3605 char str[5][16], *strs[5];
3606 unsigned int i;
3607
3608 saxdb_start_record(ctx, key, 0);
3609 /* Helper identification. */
3610 saxdb_write_string(ctx, KEY_HELPER_LEVEL, helpserv_level2str(hs_user->level));
3611 saxdb_write_string(ctx, KEY_HELPER_HELPMODE, (hs_user->help_mode ? "1" : "0"));
3612 saxdb_write_int(ctx, KEY_HELPER_WEEKSTART, hs_user->week_start);
3613 /* Helper stats */
3614 saxdb_start_record(ctx, KEY_HELPER_STATS, 0);
3615 for (i=0; i < ArrayLength(strs); ++i)
3616 strs[i] = str[i];
3617 strlist.list = strs;
3618 strlist.used = 5;
3619 /* Time in help channel */
3620 for (i=0; i < strlist.used; i++) {
3621 unsigned int week_time = hs_user->time_per_week[i];
3622 if ((i==0 || i==4) && hs_user->join_time)
3623 week_time += now - hs_user->join_time;
3624 sprintf(str[i], "%u", week_time);
3625 }
3626 saxdb_write_string_list(ctx, KEY_HELPER_STATS_TIME, &strlist);
3627 /* Requests picked up */
3628 for (i=0; i < strlist.used; i++)
3629 sprintf(str[i], "%u", hs_user->picked_up[i]);
3630 saxdb_write_string_list(ctx, KEY_HELPER_STATS_PICKUP, &strlist);
3631 /* Requests closed */
3632 for (i=0; i < strlist.used; i++)
3633 sprintf(str[i], "%u", hs_user->closed[i]);
3634 saxdb_write_string_list(ctx, KEY_HELPER_STATS_CLOSE, &strlist);
3635 /* Requests reassigned from user */
3636 for (i=0; i < strlist.used; i++)
3637 sprintf(str[i], "%u", hs_user->reassigned_from[i]);
3638 saxdb_write_string_list(ctx, KEY_HELPER_STATS_REASSIGNFROM, &strlist);
3639 /* Requests reassigned to user */
3640 for (i=0; i < strlist.used; i++)
3641 sprintf(str[i], "%u", hs_user->reassigned_to[i]);
3642 saxdb_write_string_list(ctx, KEY_HELPER_STATS_REASSIGNTO, &strlist);
3643 /* End of stats and whole record. */
3644 saxdb_end_record(ctx);
3645 saxdb_end_record(ctx);
3646 return 0;
3647 }
3648
3649 static int user_read_helper(const char *key, void *data, void *extra) {
3650 struct record_data *rd = data;
3651 struct helpserv_bot *hs = extra;
3652 struct helpserv_user *hs_user;
3653 struct handle_info *handle;
3654 dict_t stats;
3655 enum helpserv_level level;
3656 char *str;
3657 struct string_list *strlist;
3658 unsigned int i;
3659
3660 if (rd->type != RECDB_OBJECT || !dict_size(rd->d.object)) {
3661 log_module(HS_LOG, LOG_ERROR, "Invalid user %s for %s.", key, hs->helpserv->nick);
3662 return 0;
3663 }
3664
3665 if (!(handle = get_handle_info(key))) {
3666 log_module(HS_LOG, LOG_ERROR, "Nonexistant account %s for %s.", key, hs->helpserv->nick);
3667 return 0;
3668 }
3669 str = database_get_data(rd->d.object, KEY_HELPER_LEVEL, RECDB_QSTRING);
3670 if (str) {
3671 level = helpserv_str2level(str);
3672 if (level == HlNone) {
3673 log_module(HS_LOG, LOG_ERROR, "Account %s has invalid level %s.", key, str);
3674 return 0;
3675 }
3676 } else {
3677 log_module(HS_LOG, LOG_ERROR, "Account %s has no level field for %s.", key, hs->helpserv->nick);
3678 return 0;
3679 }
3680
3681 hs_user = helpserv_add_user(hs, handle, level);
3682
3683 str = database_get_data(rd->d.object, KEY_HELPER_HELPMODE, RECDB_QSTRING);
3684 hs_user->help_mode = (str && strtol(str, NULL, 0)) ? 1 : 0;
3685 str = database_get_data(rd->d.object, KEY_HELPER_WEEKSTART, RECDB_QSTRING);
3686 hs_user->week_start = str ? strtol(str, NULL, 0) : 0;
3687
3688 /* Stats */
3689 stats = database_get_data(GET_RECORD_OBJECT(rd), KEY_HELPER_STATS, RECDB_OBJECT);
3690
3691 if (stats) {
3692 /* The tests for strlist->used are for converting the old format to the new one */
3693 strlist = database_get_data(stats, KEY_HELPER_STATS_TIME, RECDB_STRING_LIST);
3694 if (strlist) {
3695 for (i=0; i < 5 && i < strlist->used; i++)
3696 hs_user->time_per_week[i] = strtoul(strlist->list[i], NULL, 0);
3697 if (strlist->used == 4)
3698 hs_user->time_per_week[4] = hs_user->time_per_week[0]+hs_user->time_per_week[1]+hs_user->time_per_week[2]+hs_user->time_per_week[3];
3699 }
3700 strlist = database_get_data(stats, KEY_HELPER_STATS_PICKUP, RECDB_STRING_LIST);
3701 if (strlist) {
3702 for (i=0; i < 5 && i < strlist->used; i++)
3703 hs_user->picked_up[i] = strtoul(strlist->list[i], NULL, 0);
3704 if (strlist->used == 2)
3705 hs_user->picked_up[4] = hs_user->picked_up[0]+hs_user->picked_up[1];
3706 }
3707 strlist = database_get_data(stats, KEY_HELPER_STATS_CLOSE, RECDB_STRING_LIST);
3708 if (strlist) {
3709 for (i=0; i < 5 && i < strlist->used; i++)
3710 hs_user->closed[i] = strtoul(strlist->list[i], NULL, 0);
3711 if (strlist->used == 2)
3712 hs_user->closed[4] = hs_user->closed[0]+hs_user->closed[1];
3713 }
3714 strlist = database_get_data(stats, KEY_HELPER_STATS_REASSIGNFROM, RECDB_STRING_LIST);
3715 if (strlist) {
3716 for (i=0; i < 5 && i < strlist->used; i++)
3717 hs_user->reassigned_from[i] = strtoul(strlist->list[i], NULL, 0);
3718 if (strlist->used == 2)
3719 hs_user->reassigned_from[4] = hs_user->reassigned_from[0]+hs_user->reassigned_from[1];
3720 }
3721 strlist = database_get_data(stats, KEY_HELPER_STATS_REASSIGNTO, RECDB_STRING_LIST);
3722 if (strlist) {
3723 for (i=0; i < 5 && i < strlist->used; i++)
3724 hs_user->reassigned_to[i] = strtoul(strlist->list[i], NULL, 0);
3725 if (strlist->used == 2)
3726 hs_user->reassigned_to[4] = hs_user->reassigned_to[0]+hs_user->reassigned_to[1];
3727 }
3728 }
3729
3730 return 0;
3731 }
3732
3733 static int request_write_helper(const char *key, void *data, void *extra) {
3734 struct helpserv_request *request = data;
3735 struct saxdb_context *ctx = extra;
3736
3737 if (!request->handle)
3738 return 0;
3739
3740 saxdb_start_record(ctx, key, 0);
3741 if (request->helper) {
3742 saxdb_write_string(ctx, KEY_REQUEST_HELPER, request->helper->handle->handle);
3743 saxdb_write_int(ctx, KEY_REQUEST_ASSIGNED, request->assigned);
3744 }
3745 saxdb_write_string(ctx, KEY_REQUEST_HANDLE, request->handle->handle);
3746 saxdb_write_int(ctx, KEY_REQUEST_OPENED, request->opened);
3747 saxdb_write_string_list(ctx, KEY_REQUEST_TEXT, request->text);
3748 saxdb_end_record(ctx);
3749 return 0;
3750 }
3751
3752 static int request_read_helper(const char *key, void *data, void *extra) {
3753 struct record_data *rd = data;
3754 struct helpserv_bot *hs = extra;
3755 struct helpserv_request *request;
3756 struct string_list *strlist;
3757 char *str;
3758
3759 if (rd->type != RECDB_OBJECT || !dict_size(rd->d.object)) {
3760 log_module(HS_LOG, LOG_ERROR, "Invalid request %s:%s.", hs->helpserv->nick, key);
3761 return 0;
3762 }
3763
3764 request = calloc(1, sizeof(struct helpserv_request));
3765
3766 request->id = strtoul(key, NULL, 0);
3767 request->hs = hs;
3768 request->user = NULL;
3769 request->parent_nick_list = request->parent_hand_list = NULL;
3770
3771 str = database_get_data(rd->d.object, KEY_REQUEST_HANDLE, RECDB_QSTRING);
3772 if (!str || !(request->handle = get_handle_info(str))) {
3773 log_module(HS_LOG, LOG_ERROR, "Request %s:%s has an invalid or nonexistant account.", hs->helpserv->nick, key);
3774 free(request);
3775 return 0;
3776 }
3777 if (!(request->parent_hand_list = dict_find(helpserv_reqs_byhand_dict, request->handle->handle, NULL))) {
3778 request->parent_hand_list = helpserv_reqlist_alloc();
3779 dict_insert(helpserv_reqs_byhand_dict, request->handle->handle, request->parent_hand_list);
3780 }
3781 helpserv_reqlist_append(request->parent_hand_list, request);
3782
3783 str = database_get_data(rd->d.object, KEY_REQUEST_OPENED, RECDB_QSTRING);
3784 if (!str) {
3785 log_module(HS_LOG, LOG_ERROR, "Request %s:%s has a nonexistant opening time. Using time(NULL).", hs->helpserv->nick, key);
3786 request->opened = time(NULL);
3787 } else {
3788 request->opened = (time_t)strtoul(str, NULL, 0);
3789 }
3790
3791 str = database_get_data(rd->d.object, KEY_REQUEST_ASSIGNED, RECDB_QSTRING);
3792 if (str)
3793 request->assigned = (time_t)strtoul(str, NULL, 0);
3794
3795 str = database_get_data(rd->d.object, KEY_REQUEST_HELPER, RECDB_QSTRING);
3796 if (str) {
3797 if (!(request->helper = dict_find(hs->users, str, NULL))) {
3798 log_module(HS_LOG, LOG_ERROR, "Request %s:%s has an invalid or nonexistant helper.", hs->helpserv->nick, key);
3799 free(request);
3800 return 0;
3801 }
3802 } else {
3803 if (!hs->unhandled) {
3804 request->next_unhandled = NULL;
3805 hs->unhandled = request;
3806 } else if (hs->unhandled->opened > request->opened) {
3807 request->next_unhandled = hs->unhandled;
3808 hs->unhandled = request;
3809 } else {
3810 struct helpserv_request *unh;
3811 for (unh = hs->unhandled; unh->next_unhandled && (unh->next_unhandled->opened < request->opened); unh = unh->next_unhandled);
3812 request->next_unhandled = unh->next_unhandled;
3813 unh->next_unhandled = request;
3814 }
3815 }
3816
3817 strlist = database_get_data(rd->d.object, KEY_REQUEST_TEXT, RECDB_STRING_LIST);
3818 if (!strlist) {
3819 log_module(HS_LOG, LOG_ERROR, "Request %s:%s has no text.", hs->helpserv->nick, key);
3820 free(request);
3821 return 0;
3822 }
3823 request->text = string_list_copy(strlist);
3824
3825 dict_insert(hs->requests, strdup(key), request);
3826
3827 return 0;
3828 }
3829
3830 static int
3831 helpserv_bot_write(const char *key, void *data, void *extra) {
3832 const struct helpserv_bot *hs = data;
3833 struct saxdb_context *ctx = extra;
3834 enum page_source pagesrc;
3835 enum message_type msgtype;
3836 enum interval_type inttype;
3837 enum persistence_type persisttype;
3838 struct string_list *slist;
3839
3840 /* Entire bot */
3841 saxdb_start_record(ctx, key, 1);
3842
3843 /* Helper list */
3844 saxdb_start_record(ctx, KEY_HELPERS, 1);
3845 dict_foreach(hs->users, user_write_helper, ctx);
3846 saxdb_end_record(ctx);
3847
3848 /* Open requests */
3849 if (hs->persist_types[PERSIST_T_REQUEST] == PERSIST_CLOSE) {
3850 saxdb_start_record(ctx, KEY_REQUESTS, 0);
3851 dict_foreach(hs->requests, request_write_helper, ctx);
3852 saxdb_end_record(ctx);
3853 }
3854
3855 /* Other settings and state */
3856 saxdb_write_string(ctx, KEY_HELP_CHANNEL, hs->helpchan->name);
3857 slist = alloc_string_list(PGSRC_COUNT);
3858 for (pagesrc=0; pagesrc<PGSRC_COUNT; pagesrc++) {
3859 struct chanNode *target = hs->page_targets[pagesrc];
3860 string_list_append(slist, strdup(target ? target->name : "*"));
3861 }
3862 saxdb_write_string_list(ctx, KEY_PAGE_DEST, slist);
3863 free_string_list(slist);
3864 for (pagesrc=0; pagesrc<PGSRC_COUNT; pagesrc++) {
3865 const char *src = page_types[hs->page_types[pagesrc]].db_name;
3866 saxdb_write_string(ctx, page_sources[pagesrc].db_name, src);
3867 }
3868 for (msgtype=0; msgtype<MSGTYPE_COUNT; msgtype++) {
3869 const char *msg = hs->messages[msgtype];
3870 if (msg)
3871 saxdb_write_string(ctx, message_types[msgtype].db_name, msg);
3872 }
3873 for (inttype=0; inttype<INTERVAL_COUNT; inttype++) {
3874 if (!hs->intervals[inttype])
3875 continue;
3876 saxdb_write_int(ctx, interval_types[inttype].db_name, hs->intervals[inttype]);
3877 }
3878 for (persisttype=0; persisttype<PERSIST_T_COUNT; persisttype++) {
3879 const char *persist = persistence_lengths[hs->persist_types[persisttype]].db_name;
3880 saxdb_write_string(ctx, persistence_types[persisttype].db_name, persist);
3881 }
3882 saxdb_write_string(ctx, KEY_NOTIFICATION, notification_types[hs->notify].db_name);
3883 saxdb_write_int(ctx, KEY_REGISTERED, hs->registered);
3884 saxdb_write_int(ctx, KEY_IDWRAP, hs->id_wrap);
3885 saxdb_write_int(ctx, KEY_REQ_MAXLEN, hs->req_maxlen);
3886 saxdb_write_int(ctx, KEY_LAST_REQUESTID, hs->last_requestid);
3887 if (hs->registrar)
3888 saxdb_write_string(ctx, KEY_REGISTRAR, hs->registrar);
3889 saxdb_write_int(ctx, KEY_PRIVMSG_ONLY, hs->privmsg_only);
3890 saxdb_write_int(ctx, KEY_REQ_ON_JOIN, hs->req_on_join);
3891 saxdb_write_int(ctx, KEY_AUTO_VOICE, hs->auto_voice);
3892 saxdb_write_int(ctx, KEY_AUTO_DEVOICE, hs->auto_devoice);
3893 saxdb_write_int(ctx, KEY_JOIN_TOTAL, hs->join_total);
3894 saxdb_write_int(ctx, KEY_ALERT_NEW, hs->alert_new);
3895 saxdb_write_int(ctx, KEY_LAST_ACTIVE, hs->last_active);
3896
3897 if (hs->suspended) {
3898 saxdb_write_int(ctx, KEY_SUSPENDED, hs->suspended);
3899 saxdb_write_int(ctx, KEY_EXPIRY, hs->expiry);
3900 saxdb_write_int(ctx, KEY_ISSUED, hs->issued);
3901 saxdb_write_string(ctx, KEY_SUSPENDER, hs->suspender);
3902 saxdb_write_string(ctx, KEY_REASON, hs->reason);
3903 }
3904
3905 /* End bot record */
3906 saxdb_end_record(ctx);
3907 return 0;
3908 }
3909
3910 static int
3911 helpserv_saxdb_write(struct saxdb_context *ctx) {
3912 saxdb_start_record(ctx, KEY_BOTS, 1);
3913 dict_foreach(helpserv_bots_dict, helpserv_bot_write, ctx);
3914 saxdb_end_record(ctx);
3915 saxdb_write_int(ctx, KEY_LAST_STATS_UPDATE, last_stats_update);
3916 return 0;
3917 }
3918
3919 static int helpserv_bot_read(const char *key, void *data, UNUSED_ARG(void *extra)) {
3920 struct record_data *br = data, *raw_record;
3921 struct helpserv_bot *hs;
3922 char *registrar, *helpchannel_name, *str;
3923 dict_t users, requests;
3924 enum page_source pagesrc;
3925 enum message_type msgtype;
3926 enum interval_type inttype;
3927 enum persistence_type persisttype;
3928
3929 users = database_get_data(GET_RECORD_OBJECT(br), KEY_HELPERS, RECDB_OBJECT);
3930 if (!users) {
3931 log_module(HS_LOG, LOG_ERROR, "%s has no users.", key);
3932 return 0;
3933 }
3934 helpchannel_name = database_get_data(GET_RECORD_OBJECT(br), KEY_HELP_CHANNEL, RECDB_QSTRING);
3935 if (!helpchannel_name || !IsChannelName(helpchannel_name)) {
3936 log_module(HS_LOG, LOG_ERROR, "%s has an invalid channel name.", key);
3937 return 0;
3938 }
3939 registrar = database_get_data(GET_RECORD_OBJECT(br), KEY_REGISTRAR, RECDB_QSTRING);
3940
3941 hs = register_helpserv(key, helpchannel_name, registrar);
3942
3943 raw_record = dict_find(GET_RECORD_OBJECT(br), KEY_PAGE_DEST, NULL);
3944 switch (raw_record ? raw_record->type : RECDB_INVALID) {
3945 case RECDB_QSTRING:
3946 set_page_target(hs, PGSRC_COMMAND, GET_RECORD_QSTRING(raw_record));
3947 pagesrc = PGSRC_COMMAND + 1;
3948 break;
3949 case RECDB_STRING_LIST: {
3950 struct string_list *slist = GET_RECORD_STRING_LIST(raw_record);
3951 for (pagesrc=0; (pagesrc<slist->used) && (pagesrc<PGSRC_COUNT); pagesrc++) {
3952 const char *dest = slist->list[pagesrc];
3953 set_page_target(hs, pagesrc, strcmp(dest, "*") ? dest : NULL);
3954 }
3955 break;
3956 }
3957 default:
3958 set_page_target(hs, PGSRC_COMMAND, NULL);
3959 pagesrc = PGSRC_COMMAND + 1;
3960 break;
3961 }
3962 while (pagesrc < PGSRC_COUNT) {
3963 set_page_target(hs, pagesrc++, hs->page_targets[PGSRC_COMMAND] ? hs->page_targets[PGSRC_COMMAND]->name : NULL);
3964 }
3965
3966 for (pagesrc=0; pagesrc<PGSRC_COUNT; pagesrc++) {
3967 str = database_get_data(GET_RECORD_OBJECT(br), page_sources[pagesrc].db_name, RECDB_QSTRING);
3968 hs->page_types[pagesrc] = str ? page_type_from_name(str) : PAGE_NONE;
3969 }
3970
3971 for (msgtype=0; msgtype<MSGTYPE_COUNT; msgtype++) {
3972 str = database_get_data(GET_RECORD_OBJECT(br), message_types[msgtype].db_name, RECDB_QSTRING);
3973 hs->messages[msgtype] = str ? strdup(str) : NULL;
3974 }
3975
3976 for (inttype=0; inttype<INTERVAL_COUNT; inttype++) {
3977 str = database_get_data(GET_RECORD_OBJECT(br), interval_types[inttype].db_name, RECDB_QSTRING);
3978 hs->intervals[inttype] = str ? ParseInterval(str) : 0;
3979 }
3980 if (hs->intervals[INTERVAL_WHINE_INTERVAL])
3981 timeq_add(now + hs->intervals[INTERVAL_WHINE_INTERVAL], run_whine_interval, hs);
3982 if (hs->intervals[INTERVAL_EMPTY_INTERVAL])
3983 timeq_add(now + hs->intervals[INTERVAL_EMPTY_INTERVAL], run_empty_interval, hs);
3984
3985 for (persisttype=0; persisttype<PERSIST_T_COUNT; persisttype++) {
3986 str = database_get_data(GET_RECORD_OBJECT(br), persistence_types[persisttype].db_name, RECDB_QSTRING);
3987 hs->persist_types[persisttype] = str ? persistence_from_name(str) : PERSIST_QUIT;
3988 }
3989 str = database_get_data(GET_RECORD_OBJECT(br), KEY_NOTIFICATION, RECDB_QSTRING);
3990 hs->notify = str ? notification_from_name(str) : NOTIFY_NONE;
3991 str = database_get_data(GET_RECORD_OBJECT(br), KEY_REGISTERED, RECDB_QSTRING);
3992 if (str)
3993 hs->registered = (time_t)strtol(str, NULL, 0);
3994 str = database_get_data(GET_RECORD_OBJECT(br), KEY_IDWRAP, RECDB_QSTRING);
3995 if (str)
3996 hs->id_wrap = strtoul(str, NULL, 0);
3997 str = database_get_data(GET_RECORD_OBJECT(br), KEY_REQ_MAXLEN, RECDB_QSTRING);
3998 if (str)
3999 hs->req_maxlen = strtoul(str, NULL, 0);
4000 str = database_get_data(GET_RECORD_OBJECT(br), KEY_LAST_REQUESTID, RECDB_QSTRING);
4001 if (str)
4002 hs->last_requestid = strtoul(str, NULL, 0);
4003 str = database_get_data(GET_RECORD_OBJECT(br), KEY_PRIVMSG_ONLY, RECDB_QSTRING);
4004 hs->privmsg_only = str ? enabled_string(str) : 0;
4005 str = database_get_data(GET_RECORD_OBJECT(br), KEY_REQ_ON_JOIN, RECDB_QSTRING);
4006 hs->req_on_join = str ? enabled_string(str) : 0;
4007 str = database_get_data(GET_RECORD_OBJECT(br), KEY_AUTO_VOICE, RECDB_QSTRING);
4008 hs->auto_voice = str ? enabled_string(str) : 0;
4009 str = database_get_data(GET_RECORD_OBJECT(br), KEY_AUTO_DEVOICE, RECDB_QSTRING);
4010 hs->auto_devoice = str ? enabled_string(str) : 0;
4011 str = database_get_data(GET_RECORD_OBJECT(br), KEY_JOIN_TOTAL, RECDB_QSTRING);
4012 hs->join_total = str ? enabled_string(str) : 0;
4013 str = database_get_data(GET_RECORD_OBJECT(br), KEY_ALERT_NEW, RECDB_QSTRING);
4014 hs->alert_new = str ? enabled_string(str) : 0;
4015 str = database_get_data(GET_RECORD_OBJECT(br), KEY_LAST_ACTIVE, RECDB_QSTRING);
4016 hs->last_active = str ? atoi(str) : now;
4017
4018 str = database_get_data(GET_RECORD_OBJECT(br), KEY_SUSPENDED, RECDB_QSTRING);
4019 hs->suspended = str ? atoi(str) : 0;
4020 if (hs->suspended) {
4021 str = database_get_data(GET_RECORD_OBJECT(br), KEY_EXPIRY, RECDB_QSTRING);
4022 hs->expiry = str ? atoi(str) : 0;
4023 str = database_get_data(GET_RECORD_OBJECT(br), KEY_ISSUED, RECDB_QSTRING);
4024 hs->issued = str ? atoi(str) : 0;
4025 str = database_get_data(GET_RECORD_OBJECT(br), KEY_SUSPENDER, RECDB_QSTRING);
4026 hs->suspender = str ? str : 0;
4027 str = database_get_data(GET_RECORD_OBJECT(br), KEY_REASON, RECDB_QSTRING);
4028 hs->reason = str ? str : 0;
4029 }
4030
4031 dict_foreach(users, user_read_helper, hs);
4032
4033 requests = database_get_data(GET_RECORD_OBJECT(br), KEY_REQUESTS, RECDB_OBJECT);
4034 if (requests)
4035 dict_foreach(requests, request_read_helper, hs);
4036
4037 if(hs->suspended && hs->expiry)
4038 timeq_add(hs->expiry, helpserv_expire_suspension, hs);
4039
4040 return 0;
4041 }
4042
4043 static int
4044 helpserv_saxdb_read(struct dict *conf_db) {
4045 dict_t object;
4046 char *str;
4047
4048 if ((object = database_get_data(conf_db, KEY_BOTS, RECDB_OBJECT))) {
4049 dict_foreach(object, helpserv_bot_read, NULL);
4050 }
4051
4052 str = database_get_data(conf_db, KEY_LAST_STATS_UPDATE, RECDB_QSTRING);
4053 last_stats_update = str ? (time_t)strtol(str, NULL, 0) : now;
4054 return 0;
4055 }
4056
4057 static void helpserv_conf_read(void) {
4058 dict_t conf_node;
4059 const char *str;
4060
4061 if (!(conf_node = conf_get_data(HELPSERV_CONF_NAME, RECDB_OBJECT))) {
4062 log_module(HS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type", HELPSERV_CONF_NAME);
4063 return;
4064 }
4065
4066 str = database_get_data(conf_node, "db_backup_freq", RECDB_QSTRING);
4067 helpserv_conf.db_backup_frequency = str ? ParseInterval(str) : 7200;
4068
4069 str = database_get_data(conf_node, "description", RECDB_QSTRING);
4070 helpserv_conf.description = str ? str : "Help Queue Manager";
4071
4072 str = database_get_data(conf_node, "reqlogfile", RECDB_QSTRING);
4073 if (str && strlen(str))
4074 helpserv_conf.reqlogfile = str;
4075 else
4076 helpserv_conf.reqlogfile = NULL;
4077
4078 str = database_get_data(conf_node, "expiration", RECDB_QSTRING);
4079 helpserv_conf.expire_age = ParseInterval(str ? str : "60d");
4080 str = database_get_data(conf_node, "user_escape", RECDB_QSTRING);
4081 helpserv_conf.user_escape = str ? str[0] : '@';
4082
4083 if (reqlog_f) {
4084 fclose(reqlog_f);
4085 reqlog_f = NULL;
4086 }
4087 if (helpserv_conf.reqlogfile
4088 && !(reqlog_f = fopen(helpserv_conf.reqlogfile, "a"))) {
4089 log_module(HS_LOG, LOG_ERROR, "Unable to open request logfile (%s): %s", helpserv_conf.reqlogfile, strerror(errno));
4090 }
4091 }
4092
4093 static struct helpserv_cmd *
4094 helpserv_define_func(const char *name, helpserv_func_t *func, enum helpserv_level access, long flags) {
4095 struct helpserv_cmd *cmd = calloc(1, sizeof(struct helpserv_cmd));
4096
4097 cmd->access = access;
4098 cmd->weight = 1.0;
4099 cmd->func = func;
4100 cmd->flags = flags;
4101 dict_insert(helpserv_func_dict, name, cmd);
4102
4103 return cmd;
4104 }
4105
4106 /* Drop requests that persist until part when a user leaves the chan */
4107 static void handle_part(struct modeNode *mn, UNUSED_ARG(const char *reason)) {
4108 struct helpserv_botlist *botlist;
4109 struct helpserv_userlist *userlist;
4110 const int from_opserv = 0; /* for helpserv_notice */
4111 unsigned int i;
4112
4113 if ((botlist = dict_find(helpserv_bots_bychan_dict, mn->channel->name, NULL))) {
4114 for (i=0; i < botlist->used; i++) {
4115 struct helpserv_bot *hs;
4116 dict_iterator_t it;
4117
4118 hs = botlist->list[i];
4119 if (!hs->helpserv)
4120 continue;
4121 if (hs->persist_types[PERSIST_T_REQUEST] != PERSIST_PART)
4122 continue;
4123
4124 for (it=dict_first(hs->requests); it; it=iter_next(it)) {
4125 struct helpserv_request *req = iter_data(it);
4126
4127 if (mn->user != req->user)
4128 continue;
4129 if (req->text->used) {
4130 helpserv_message(hs, mn->user, MSGTYPE_REQ_DROPPED);
4131 helpserv_msguser(mn->user, "HSMSG_REQ_DROPPED_PART", mn->channel->name, req->id);
4132 if (req->helper && (hs->notify >= NOTIFY_DROP))
4133 helpserv_notify(req->helper, "HSMSG_NOTIFY_REQ_DROP_PART", req->id, mn->user->nick);
4134 }
4135 helpserv_log_request(req, "Dropped");
4136 dict_remove(hs->requests, iter_key(it));
4137 break;
4138 }
4139 }
4140 }
4141
4142 if (mn->user->handle_info && (userlist = dict_find(helpserv_users_byhand_dict, mn->user->handle_info->handle, NULL))) {
4143 for (i=0; i < userlist->used; i++) {
4144 struct helpserv_user *hs_user = userlist->list[i];
4145 struct helpserv_bot *hs = hs_user->hs;
4146 dict_iterator_t it;
4147
4148 if ((hs->helpserv == NULL) || (hs->helpchan != mn->channel) || find_handle_in_channel(hs->helpchan, mn->user->handle_info, mn->user))
4149 continue;
4150
4151 /* In case of the clock being set back for whatever reason,
4152 * minimize penalty. Don't duplicate this in handle_quit because
4153 * when users quit, handle_part is called for every channel first.
4154 */
4155 if (hs_user->join_time && (hs_user->join_time < now)) {
4156 hs_user->time_per_week[0] += (unsigned int)(now - hs_user->join_time);
4157 hs_user->time_per_week[4] += (unsigned int)(now - hs_user->join_time);
4158 }
4159 hs_user->join_time = 0;
4160
4161 for (it=dict_first(hs->requests); it; it=iter_next(it)) {
4162 struct helpserv_request *req=iter_data(it);
4163
4164 if ((hs->persist_types[PERSIST_T_HELPER] == PERSIST_PART)
4165 && (req->helper == hs_user)) {
4166 char reason[CHANNELLEN + 8];
4167 sprintf(reason, "parted %s", mn->channel->name);
4168 helpserv_page_helper_gone(hs, req, reason);
4169 }
4170 }
4171
4172 if (hs->intervals[INTERVAL_EMPTY_INTERVAL] && hs_user->level >= HlHelper) {
4173 int num_trials;
4174
4175 if ((num_trials = find_helpchan_helpers(hs)) >= 0) {
4176 unsigned int num_unh;
4177 struct helpserv_request *unh;
4178
4179 for (num_unh=0, unh=hs->unhandled; unh; num_unh++)
4180 unh = unh->next_unhandled;
4181
4182 if (num_trials) {
4183 helpserv_page(PGSRC_ALERT, "HSMSG_PAGE_FIRSTONLYTRIALALERT", hs->helpchan->name, mn->user->nick, num_trials, num_unh);
4184 } else {
4185 helpserv_page(PGSRC_ALERT, "HSMSG_PAGE_FIRSTEMPTYALERT", hs->helpchan->name, mn->user->nick, num_unh);
4186 }
4187 if (num_unh || !hs->req_on_join) {
4188 timeq_del(0, run_empty_interval, hs, TIMEQ_IGNORE_WHEN);
4189 timeq_add(now + hs->intervals[INTERVAL_EMPTY_INTERVAL], run_empty_interval, hs);
4190 }
4191 }
4192 }
4193 }
4194 }
4195 }
4196
4197 /* Drop requests that persist until part or quit when a user quits. Otherwise
4198 * set req->user to null (it's no longer valid) if they have a handle,
4199 * and drop it if they don't (nowhere to store the request).
4200 *
4201 * Unassign requests where req->helper persists until the helper parts or
4202 * quits. */
4203 static void handle_quit(struct userNode *user, UNUSED_ARG(struct userNode *killer), UNUSED_ARG(const char *why)) {
4204 struct helpserv_reqlist *reqlist;
4205 struct helpserv_userlist *userlist;
4206 unsigned int i, n;
4207
4208 if (IsLocal(user)) {
4209 struct helpserv_bot *hs;
4210 if ((hs = dict_find(helpserv_bots_dict, user->nick, NULL))) {
4211 hs->helpserv = NULL;
4212 }
4213 return;
4214 }
4215
4216 if ((reqlist = dict_find(helpserv_reqs_bynick_dict, user->nick, NULL))) {
4217 n = reqlist->used;
4218 for (i=0; i < n; i++) {
4219 struct helpserv_request *req = reqlist->list[0];
4220
4221 if ((req->hs->persist_types[PERSIST_T_REQUEST] == PERSIST_QUIT) || !req->handle) {
4222 char buf[12];
4223 sprintf(buf, "%lu", req->id);
4224
4225 if (req->helper && (req->hs->notify >= NOTIFY_DROP))
4226 helpserv_notify(req->helper, "HSMSG_NOTIFY_REQ_DROP_QUIT", req->id, req->user->nick);
4227
4228 helpserv_log_request(req, "Dropped");
4229 dict_remove(req->hs->requests, buf);
4230 } else {
4231 req->user = NULL;
4232 req->parent_nick_list = NULL;
4233 helpserv_reqlist_remove(reqlist, req);
4234
4235 if (req->helper && (req->hs->notify >= NOTIFY_USER))
4236 helpserv_notify(req->helper, "HSMSG_NOTIFY_USER_QUIT", req->id, user->nick);
4237 }
4238 }
4239
4240 dict_remove(helpserv_reqs_bynick_dict, user->nick);
4241 }
4242
4243 if (user->handle_info && (userlist = dict_find(helpserv_users_byhand_dict, user->handle_info->handle, NULL))) {
4244 for (i=0; i < userlist->used; i++) {
4245 struct helpserv_user *hs_user = userlist->list[i];
4246 struct helpserv_bot *hs = hs_user->hs;
4247 dict_iterator_t it;
4248
4249 if ((hs->helpserv == NULL) || user->next_authed || (user->handle_info->users != user))
4250 continue;
4251
4252 for (it=dict_first(hs->requests); it; it=iter_next(it)) {
4253 struct helpserv_request *req=iter_data(it);
4254
4255 if ((hs->persist_types[PERSIST_T_HELPER] == PERSIST_QUIT) && (req->helper == hs_user)) {
4256 helpserv_page_helper_gone(hs, req, "disconnected");
4257 }
4258 }
4259 }
4260 }
4261 }
4262
4263 static void associate_requests_bybot(struct helpserv_bot *hs, struct userNode *user, int force_greet) {
4264 struct helpserv_reqlist *reqlist, *hand_reqlist=NULL;
4265 struct helpserv_request *newest=NULL, *nicknewest=NULL;
4266 unsigned int i;
4267 const int from_opserv = 0; /* For helpserv_notice */
4268
4269 if (!(user->handle_info && (hand_reqlist = dict_find(helpserv_reqs_byhand_dict, user->handle_info->handle, NULL))) && !force_greet) {
4270 return;
4271 }
4272
4273 reqlist = dict_find(helpserv_reqs_bynick_dict, user->nick, NULL);
4274
4275 if (hand_reqlist) {
4276 for (i=0; i < hand_reqlist->used; i++) {
4277 struct helpserv_request *req=hand_reqlist->list[i];
4278
4279 if (req->user || (req->hs != hs))
4280 continue;
4281
4282 req->user = user;
4283 if (!reqlist) {
4284 reqlist = helpserv_reqlist_alloc();
4285 dict_insert(helpserv_reqs_bynick_dict, user->nick, reqlist);
4286 }
4287 req->parent_nick_list = reqlist;
4288 helpserv_reqlist_append(reqlist, req);
4289
4290 if (req->helper && (hs->notify >= NOTIFY_USER))
4291 helpserv_notify(req->helper, "HSMSG_NOTIFY_USER_FOUND", req->id, user->nick);
4292
4293 if (!newest || (newest->opened < req->opened))
4294 newest = req;
4295 }
4296 }
4297
4298 /* If it's supposed to force a greeting, only bail out if there are no
4299 * requests at all. If it's not supposed to force a greeting, bail out if
4300 * nothing was changed. */
4301 if (!(newest || (force_greet && reqlist)))
4302 return;
4303
4304 /* Possible conditions here:
4305 * 1. newest == NULL, force_greeting == 1, reqlist != NULL
4306 * 2. newest != NULL, force_greeting doesn't matter, reqlist != NULL */
4307
4308 /* Figure out which request will get their next message */
4309 for (i=0; i < reqlist->used; i++) {
4310 struct helpserv_request *req=reqlist->list[i];
4311
4312 if (req->hs != hs)
4313 continue;
4314
4315 if (!nicknewest || (nicknewest->opened < req->opened))
4316 nicknewest = req;
4317
4318 if (hs->auto_voice && req->helper)
4319 {
4320 struct mod_chanmode change;
4321 mod_chanmode_init(&change);
4322 change.argc = 1;
4323 change.args[0].mode = MODE_VOICE;
4324 if ((change.args[0].u.member = GetUserMode(hs->helpchan, user)))
4325 mod_chanmode_announce(hs->helpserv, hs->helpchan, &change);
4326 }
4327 }
4328
4329 if ((force_greet && nicknewest) || (newest && (nicknewest == newest))) {
4330 /* Let the user know. Either the user is forced to be greeted, or the
4331 * above has changed which request will get their next message. */
4332 helpserv_msguser(user, "HSMSG_GREET_EXISTING_REQ", hs->helpchan->name, nicknewest->id);
4333 }
4334 }
4335
4336 static void associate_requests_bychan(struct chanNode *chan, struct userNode *user, int force_greet) {
4337 struct helpserv_botlist *botlist;
4338 unsigned int i;
4339
4340 if (!(botlist = dict_find(helpserv_bots_bychan_dict, chan->name, NULL)))
4341 return;
4342
4343 for (i=0; i < botlist->used; i++)
4344 associate_requests_bybot(botlist->list[i], user, force_greet);
4345 }
4346
4347
4348 /* Greet users upon joining a helpserv channel (if greeting is set) and set
4349 * req->user to the user joining for all requests owned by the user's handle
4350 * (if any) with a req->user == NULL */
4351 static int handle_join(struct modeNode *mNode) {
4352 struct userNode *user = mNode->user;
4353 struct chanNode *chan = mNode->channel;
4354 struct helpserv_botlist *botlist;
4355 unsigned int i;
4356 const int from_opserv = 0; /* for helpserv_notice */
4357
4358 if (IsLocal(user))
4359 return 0;
4360
4361 if (!(botlist = dict_find(helpserv_bots_bychan_dict, chan->name, NULL)))
4362 return 0;
4363
4364 for (i=0; i < botlist->used; i++) {
4365 struct helpserv_bot *hs=botlist->list[i];
4366
4367 if (user->handle_info) {
4368 struct helpserv_user *hs_user;
4369
4370 if ((hs_user = dict_find(hs->users, user->handle_info->handle, NULL))) {
4371 if (!hs_user->join_time)
4372 hs_user->join_time = now;
4373
4374 if (hs->join_total) {
4375 if (hs_user->level >= HlHelper) {
4376 unsigned int total;
4377 struct helpserv_request *req;
4378
4379 for (req = hs->unhandled, total=0; req; req = req->next_unhandled, total++) ;
4380
4381 if (total > 0)
4382 helpserv_notice(user, "HSMSG_REQUESTS_OPEN", total, hs->helpserv->nick, hs->helpserv->nick);
4383 }
4384 }
4385
4386 if (hs_user->level >= HlHelper && hs->intervals[INTERVAL_EMPTY_INTERVAL] && hs->helpchan_empty) {
4387 hs->helpchan_empty = 0;
4388 timeq_del(0, run_empty_interval, hs, TIMEQ_IGNORE_WHEN);
4389 helpserv_page(PGSRC_ALERT, "HSMSG_PAGE_EMPTYNOMORE", user->nick, hs->helpchan->name);
4390 }
4391 continue; /* Don't want helpers to have request-on-join */
4392 }
4393 }
4394
4395 if (self->burst && !hs->req_on_join)
4396 continue;
4397
4398 associate_requests_bybot(hs, user, 1);
4399
4400 helpserv_message(hs, user, MSGTYPE_GREETING);
4401
4402 /* Make sure this is at the end (because of the continues) */
4403 if (hs->req_on_join) {
4404 struct helpserv_reqlist *reqlist;
4405 unsigned int j;
4406
4407 if ((reqlist = dict_find(helpserv_reqs_bynick_dict, user->nick, NULL))) {
4408 for (j=0; j < reqlist->used; j++)
4409 if (reqlist->list[i]->hs == hs)
4410 break;
4411 if (j < reqlist->used)
4412 continue;
4413 }
4414
4415 create_request(user, hs, 1);
4416 }
4417 }
4418 return 0;
4419 }
4420
4421 /* Update helpserv_reqs_bynick_dict upon nick change */
4422 static void handle_nickchange(struct userNode *user, const char *old_nick) {
4423 struct helpserv_reqlist *reqlist;
4424 unsigned int i;
4425
4426 if (!(reqlist = dict_find(helpserv_reqs_bynick_dict, old_nick, NULL)))
4427 return;
4428
4429 /* Don't free the list when we switch it over to the new nick. */
4430 dict_remove2(helpserv_reqs_bynick_dict, old_nick, 1);
4431 dict_insert(helpserv_reqs_bynick_dict, user->nick, reqlist);
4432
4433 for (i=0; i < reqlist->used; i++) {
4434 struct helpserv_request *req=reqlist->list[i];
4435
4436 if (req->helper && (req->hs->notify >= NOTIFY_USER))
4437 helpserv_notify(req->helper, "HSMSG_NOTIFY_USER_NICK", req->id, old_nick, user->nick);
4438 }
4439 }
4440
4441 /* Also update helpserv_reqs_byhand_dict upon handle rename */
4442 static void handle_nickserv_rename(struct handle_info *handle, const char *old_handle) {
4443 struct helpserv_reqlist *reqlist;
4444 struct helpserv_userlist *userlist;
4445 unsigned int i;
4446
4447 /* First, rename the handle in the requests dict */
4448 if ((reqlist = dict_find(helpserv_reqs_byhand_dict, old_handle, NULL))) {
4449 /* Don't free the list */
4450 dict_remove2(helpserv_reqs_byhand_dict, old_handle, 1);
4451 dict_insert(helpserv_reqs_byhand_dict, handle->handle, reqlist);
4452 }
4453
4454 /* Second, rename the handle in the users dict */
4455 if ((userlist = dict_find(helpserv_users_byhand_dict, old_handle, NULL))) {
4456 dict_remove2(helpserv_users_byhand_dict, old_handle, 1);
4457
4458 for (i=0; i < userlist->used; i++)
4459 dict_remove2(userlist->list[i]->hs->users, old_handle, 1);
4460
4461 dict_insert(helpserv_users_byhand_dict, handle->handle, userlist);
4462 for (i=0; i < userlist->used; i++)
4463 dict_insert(userlist->list[i]->hs->users, handle->handle, userlist->list[i]);
4464 }
4465
4466 if (reqlist) {
4467 for (i=0; i < reqlist->used; i++) {
4468 struct helpserv_request *req=reqlist->list[i];
4469
4470 if (req->helper && (req->hs->notify >= NOTIFY_HANDLE))
4471 helpserv_notify(req->helper, "HSMSG_NOTIFY_HAND_RENAME", req->id, old_handle, handle->handle);
4472 }
4473 }
4474 }
4475
4476 /* Deals with two cases:
4477 * 1. No handle -> handle
4478 * - Bots with a request assigned to both the user (w/o handle) and the
4479 * handle can exist in this case. When a message is sent,
4480 * helpserv_usermsg will append it to the most recently opened request.
4481 * - Requests assigned to the handle are NOT assigned to the user, since
4482 * multiple people can auth to the same handle at once. Wait for them to
4483 * join / privmsg before setting req->user.
4484 * 2. Handle -> handle
4485 * - Generally avoided, but sometimes the code may allow this.
4486 * - Requests that persist only until part/quit are brought along to the
4487 * new handle.
4488 * - Requests that persist until closed (stay saved with the handle) are
4489 * left with the old handle. This is to prevent the confusing situation
4490 * where some requests are carried over to the new handle, and some are
4491 * left (because req->handle is the same for all of them, but only some
4492 * have req->user set).
4493 * - In either of the above cases, if a user is on a bot's userlist and has
4494 * requests assigned to them, it will give them a list. */
4495 static void handle_nickserv_auth(struct userNode *user, struct handle_info *old_handle) {
4496 struct helpserv_reqlist *reqlist, *dellist=NULL, *hand_reqlist, *oldhand_reqlist;
4497 struct helpserv_userlist *userlist;
4498 unsigned int i, j;
4499 dict_iterator_t it;
4500 const int from_opserv = 0; /* for helpserv_notice */
4501
4502 if (!user->handle_info)
4503 return; /* Authed user is quitting */
4504
4505 if ((userlist = dict_find(helpserv_users_byhand_dict, user->handle_info->handle, NULL))) {
4506 for (i=0; i < userlist->used; i++) {
4507 struct helpserv_user *hs_user = userlist->list[i];
4508 struct helpserv_bot *hs = hs_user->hs;
4509 struct helpserv_reqlist helper_reqs;
4510 struct helpfile_table tbl;
4511
4512 if (!hs_user->join_time && find_handle_in_channel(hs->helpchan, hs_user->handle, NULL))
4513 hs_user->join_time = now;
4514
4515 helpserv_reqlist_init(&helper_reqs);
4516
4517 for (it=dict_first(hs->requests); it; it=iter_next(it)) {
4518 struct helpserv_request *req=iter_data(it);
4519
4520 if (req->helper == hs_user)
4521 helpserv_reqlist_append(&helper_reqs, req);
4522 }
4523
4524 if (helper_reqs.used) {
4525 tbl.length = helper_reqs.used+1;
4526 tbl.width = 5;
4527 tbl.flags = TABLE_NO_FREE;
4528 tbl.contents = alloca(tbl.length * sizeof(*tbl.contents));
4529 tbl.contents[0] = alloca(tbl.width * sizeof(**tbl.contents));
4530 tbl.contents[0][0] = "Bot";
4531 tbl.contents[0][1] = "ID#";
4532 tbl.contents[0][2] = "Nick";
4533 tbl.contents[0][3] = "Account";
4534 tbl.contents[0][4] = "Opened";
4535
4536 for (j=1; j <= helper_reqs.used; j++) {
4537 struct helpserv_request *req=helper_reqs.list[j-1];
4538 char reqid[12], timestr[MAX_LINE_SIZE];
4539
4540 tbl.contents[j] = alloca(tbl.width * sizeof(**tbl.contents));
4541 tbl.contents[j][0] = req->hs->helpserv->nick;
4542 sprintf(reqid, "%lu", req->id);
4543 tbl.contents[j][1] = strdup(reqid);
4544 tbl.contents[j][2] = req->user ? req->user->nick : "Not online";
4545 tbl.contents[j][3] = req->handle ? req->handle->handle : "Not authed";
4546 strftime(timestr, MAX_LINE_SIZE, HSFMT_TIME, localtime(&req->opened));
4547 tbl.contents[j][4] = strdup(timestr);
4548 }
4549
4550 helpserv_notice(user, "HSMSG_REQLIST_AUTH");
4551 table_send(hs->helpserv, user->nick, 0, NULL, tbl);
4552
4553 for (j=1; j <= helper_reqs.used; j++) {
4554 free((char *)tbl.contents[j][1]);
4555 free((char *)tbl.contents[j][4]);
4556 }
4557 }
4558
4559 helpserv_reqlist_clean(&helper_reqs);
4560 }
4561 }
4562
4563
4564 if (!(reqlist = dict_find(helpserv_reqs_bynick_dict, user->nick, NULL))) {
4565 for (i=0; i < user->channels.used; i++)
4566 associate_requests_bychan(user->channels.list[i]->channel, user, 0);
4567 return;
4568 }
4569
4570 if (!(hand_reqlist = dict_find(helpserv_reqs_byhand_dict, user->handle_info->handle, NULL))) {
4571 hand_reqlist = helpserv_reqlist_alloc();
4572 dict_insert(helpserv_reqs_byhand_dict, user->handle_info->handle, hand_reqlist);
4573 }
4574
4575 if (old_handle) {
4576 dellist = helpserv_reqlist_alloc();
4577 oldhand_reqlist = dict_find(helpserv_reqs_byhand_dict, old_handle->handle, NULL);
4578 } else {
4579 oldhand_reqlist = NULL;
4580 }
4581
4582 for (i=0; i < reqlist->used; i++) {
4583 struct helpserv_request *req = reqlist->list[i];
4584 struct helpserv_bot *hs=req->hs;
4585
4586 if (!old_handle || hs->persist_types[PERSIST_T_REQUEST] == PERSIST_PART || hs->persist_types[PERSIST_T_REQUEST] == PERSIST_QUIT) {
4587 /* The request needs to be assigned to the new handle; either it
4588 * only persists until part/quit (so it makes sense to keep it as
4589 * close to the user as possible, and if it's made persistent later
4590 * then it's attached to the new handle) or there is no old handle.
4591 */
4592
4593 req->handle = user->handle_info;
4594
4595 req->parent_hand_list = hand_reqlist;
4596 helpserv_reqlist_append(hand_reqlist, req);
4597
4598 if (oldhand_reqlist) {
4599 if (oldhand_reqlist->used == 1) {
4600 dict_remove(helpserv_reqs_byhand_dict, old_handle->handle);
4601 oldhand_reqlist = NULL;
4602 } else {
4603 helpserv_reqlist_remove(oldhand_reqlist, req);
4604 }
4605 }
4606
4607 if (old_handle) {
4608 char buf[CHANNELLEN + 14];
4609
4610 if (hs->persist_types[PERSIST_T_REQUEST] == PERSIST_PART) {
4611 sprintf(buf, "part channel %s", hs->helpchan->name);
4612 } else {
4613 strcpy(buf, "quit irc");
4614 }
4615
4616 helpserv_msguser(user, "HSMSG_REQ_AUTH_MOVED", user->handle_info->handle, hs->helpchan->name, req->id, old_handle->handle, buf);
4617 if (req->helper && (hs->notify >= NOTIFY_HANDLE))
4618 helpserv_notify(req->helper, "HSMSG_NOTIFY_HAND_MOVE", req->id, user->handle_info->handle, old_handle->handle);
4619 } else {
4620 if (req->helper && (hs->notify >= NOTIFY_HANDLE))
4621 helpserv_notify(req->helper, "HSMSG_NOTIFY_HAND_AUTH", req->id, user->nick, user->handle_info->handle);
4622 }
4623 } else {
4624 req->user = NULL;
4625 req->parent_nick_list = NULL;
4626 /* Would rather not mess with the list while iterating through
4627 * it */
4628 helpserv_reqlist_append(dellist, req);
4629
4630 helpserv_msguser(user, "HSMSG_REQ_AUTH_STUCK", user->handle_info->handle, hs->helpchan->name, req->id, old_handle->handle);
4631 if (req->helper && (hs->notify >= NOTIFY_HANDLE))
4632 helpserv_notify(req->helper, "HSMSG_NOTIFY_HAND_STUCK", req->id, user->nick, user->handle_info->handle, old_handle->handle);
4633 }
4634 }
4635
4636 if (old_handle) {
4637 if (dellist->used) {
4638 if (dellist->used == reqlist->used) {
4639 dict_remove(helpserv_reqs_bynick_dict, user->nick);
4640 } else {
4641 for (i=0; i < dellist->used; i++)
4642 helpserv_reqlist_remove(reqlist, dellist->list[i]);
4643 }
4644 }
4645 helpserv_reqlist_free(dellist);
4646 }
4647
4648 for (i=0; i < user->channels.used; i++)
4649 associate_requests_bychan(user->channels.list[i]->channel, user, 0);
4650 }
4651
4652
4653 /* Disassociate all requests from the handle. If any have req->user == NULL
4654 * then give them to the user doing the unregistration (if not an oper/helper)
4655 * otherwise the first nick it finds authed (it lets them know about this). If
4656 * there are no users authed to the handle online, the requests are lost. This
4657 * may result in the user having >1 request/bot, and messages go to the most
4658 * recently opened request.
4659 *
4660 * Also, remove the user from all bots that it has access in.
4661 * helpserv_del_user() will take care of unassigning the requests. */
4662 static void handle_nickserv_unreg(struct userNode *user, struct handle_info *handle) {
4663 struct helpserv_reqlist *hand_reqlist;
4664 struct helpserv_userlist *userlist;
4665 unsigned int i, n;
4666 const int from_opserv = 0; /* for helpserv_notice */
4667 struct helpserv_bot *hs; /* for helpserv_notice */
4668
4669 if ((userlist = dict_find(helpserv_users_byhand_dict, handle->handle, NULL))) {
4670 n=userlist->used;
4671
4672 /* Each time helpserv_del_user is called, that entry is going to be
4673 * taken out of userlist... so this should cope with that */
4674 for (i=0; i < n; i++) {
4675 struct helpserv_user *hs_user=userlist->list[0];
4676 helpserv_del_user(hs_user->hs, hs_user);
4677 }
4678 }
4679
4680 if (!(hand_reqlist = dict_find(helpserv_reqs_byhand_dict, handle->handle, NULL))) {
4681 return;
4682 }
4683
4684 n = hand_reqlist->used;
4685 for (i=0; i < n; i++) {
4686 struct helpserv_request *req=hand_reqlist->list[0];
4687 hs = req->hs;
4688
4689 req->handle = NULL;
4690 req->parent_hand_list = NULL;
4691 helpserv_reqlist_remove(hand_reqlist, req);
4692 if (user && req->helper && (hs->notify >= NOTIFY_HANDLE))
4693 helpserv_notify(req->helper, "HSMSG_NOTIFY_HAND_UNREG", req->id, handle->handle, user->nick);
4694
4695 if (!req->user) {
4696 if (!user) {
4697 /* This is probably an expire. Silently remove everything. */
4698
4699 char buf[12];
4700 if (req->helper && (hs->notify >= NOTIFY_DROP))
4701 helpserv_notify(req->helper, "HSMSG_NOTIFY_REQ_DROP_UNREGGED", req->id, req->handle->handle);
4702 sprintf(buf, "%lu", req->id);
4703 helpserv_log_request(req, "Account unregistered");
4704 dict_remove(req->hs->requests, buf);
4705 } else if (user->handle_info == handle) {
4706 req->user = user;
4707 if (!(req->parent_nick_list = dict_find(helpserv_reqs_bynick_dict, user->nick, NULL))) {
4708 req->parent_nick_list = helpserv_reqlist_alloc();
4709 dict_insert(helpserv_reqs_bynick_dict, user->nick, req->parent_nick_list);
4710 }
4711 helpserv_reqlist_append(req->parent_nick_list, req);
4712
4713 if (hs->persist_types[PERSIST_T_REQUEST] == PERSIST_CLOSE)
4714 helpserv_msguser(req->user, "HSMSG_REQ_WARN_UNREG", handle->handle, hs->helpchan->name, req->id);
4715 } else {
4716 if (handle->users) {
4717 req->user = handle->users;
4718
4719 if (!(req->parent_nick_list = dict_find(helpserv_reqs_bynick_dict, req->user->nick, NULL))) {
4720 req->parent_nick_list = helpserv_reqlist_alloc();
4721 dict_insert(helpserv_reqs_bynick_dict, req->user->nick, req->parent_nick_list);
4722 }
4723 helpserv_reqlist_append(req->parent_nick_list, req);
4724
4725 helpserv_msguser(req->user, "HSMSG_REQ_ASSIGNED_UNREG", handle->handle, hs->helpchan->name, req->id);
4726 if (req->helper && (hs->notify >= NOTIFY_USER))
4727 helpserv_notify(req->helper, "HSMSG_NOTIFY_USER_MOVE", req->id, handle->handle, req->user->nick);
4728 } else {
4729 char buf[12];
4730
4731 helpserv_notice(user, "HSMSG_REQ_DROPPED_UNREG", handle->handle, hs->helpchan->name, req->id);
4732 if (req->helper && (hs->notify >= NOTIFY_DROP))
4733 helpserv_notify(req->helper, "HSMSG_NOTIFY_REQ_DROP_UNREGGED", req->id, req->handle->handle);
4734 sprintf(buf, "%lu", req->id);
4735 helpserv_log_request(req, "Account unregistered");
4736 dict_remove(req->hs->requests, buf);
4737 }
4738 }
4739 }
4740 }
4741
4742 dict_remove(helpserv_reqs_byhand_dict, handle->handle);
4743 }
4744
4745 static void handle_nickserv_merge(struct userNode *user, struct handle_info *handle_to, struct handle_info *handle_from) {
4746 struct helpserv_reqlist *reqlist_from, *reqlist_to;
4747 unsigned int i;
4748
4749 reqlist_to = dict_find(helpserv_reqs_byhand_dict, handle_to->handle, NULL);
4750
4751 if ((reqlist_from = dict_find(helpserv_reqs_byhand_dict, handle_from->handle, NULL))) {
4752 for (i=0; i < reqlist_from->used; i++) {
4753 struct helpserv_request *req=reqlist_from->list[i];
4754
4755 if (!reqlist_to) {
4756 reqlist_to = helpserv_reqlist_alloc();
4757 dict_insert(helpserv_reqs_byhand_dict, handle_to->handle, reqlist_to);
4758 }
4759 req->parent_hand_list = reqlist_to;
4760 req->handle = handle_to;
4761 helpserv_reqlist_append(reqlist_to, req);
4762 }
4763 dict_remove(helpserv_reqs_byhand_dict, handle_from->handle);
4764 }
4765
4766 if (reqlist_to) {
4767 for (i=0; i < reqlist_to->used; i++) {
4768 struct helpserv_request *req=reqlist_to->list[i];
4769
4770 if (req->helper && (req->hs->notify >= NOTIFY_HANDLE)) {
4771 helpserv_notify(req->helper, "HSMSG_NOTIFY_HAND_MERGE", req->id, handle_to->handle, handle_from->handle, user->nick);
4772 }
4773 }
4774 }
4775 }
4776
4777 static void handle_nickserv_allowauth(struct userNode *user, struct userNode *target, struct handle_info *handle) {
4778 struct helpserv_reqlist *reqlist;
4779 unsigned int i;
4780
4781 if ((reqlist = dict_find(helpserv_reqs_bynick_dict, target->nick, NULL))) {
4782 for (i=0; i < reqlist->used; i++) {
4783 struct helpserv_request *req=reqlist->list[i];
4784
4785 if (req->helper && (req->hs->notify >= NOTIFY_HANDLE)) {
4786 if (handle) {
4787 helpserv_notify(req->helper, "HSMSG_NOTIFY_ALLOWAUTH", req->id, target->nick, user->nick, handle->handle);
4788 } else {
4789 helpserv_notify(req->helper, "HSMSG_NOTIFY_UNALLOWAUTH", req->id, target->nick, user->nick);
4790 }
4791 }
4792 }
4793 }
4794 }
4795
4796 static void handle_nickserv_failpw(struct userNode *user, struct handle_info *handle) {
4797 struct helpserv_reqlist *reqlist;
4798 unsigned int i;
4799
4800 if ((reqlist = dict_find(helpserv_reqs_bynick_dict, user->nick, NULL))) {
4801 for (i=0; i < reqlist->used; i++) {
4802 struct helpserv_request *req=reqlist->list[i];
4803 if (req->helper && (req->hs->notify >= NOTIFY_HANDLE))
4804 helpserv_notify(req->helper, "HSMSG_NOTIFY_FAILPW", req->id, user->nick, handle->handle);
4805 }
4806 }
4807 }
4808
4809 static time_t helpserv_next_stats(time_t after_when) {
4810 struct tm *timeinfo = localtime(&after_when);
4811
4812 /* This works because mktime(3) says it will accept out-of-range values
4813 * and fix them for us. tm_wday and tm_yday are ignored. */
4814 timeinfo->tm_mday++;
4815
4816 /* We want to run stats at midnight (local time). */
4817 timeinfo->tm_sec = timeinfo->tm_min = timeinfo->tm_hour = 0;
4818
4819 return mktime(timeinfo);
4820 }
4821
4822 /* If data != NULL, then don't add to the timeq */
4823 static void helpserv_run_stats(time_t when) {
4824 struct tm when_s;
4825 struct helpserv_bot *hs;
4826 struct helpserv_user *hs_user;
4827 int i;
4828 dict_iterator_t it, it2;
4829
4830 last_stats_update = when;
4831 localtime_r(&when, &when_s);
4832 for (it=dict_first(helpserv_bots_dict); it; it=iter_next(it)) {
4833 hs = iter_data(it);
4834
4835 for (it2=dict_first(hs->users); it2; it2=iter_next(it2)) {
4836 hs_user = iter_data(it2);
4837
4838 /* Skip the helper if it's not their week-start day. */
4839 if (hs_user->week_start != when_s.tm_wday)
4840 continue;
4841
4842 /* Adjust their credit if they are in-channel at rollover. */
4843 if (hs_user->join_time) {
4844 hs_user->time_per_week[0] += when - hs_user->join_time;
4845 hs_user->time_per_week[4] += when - hs_user->join_time;
4846 hs_user->join_time = when;
4847 }
4848
4849 /* Shift everything */
4850 for (i=3; i > 0; i--) {
4851 hs_user->time_per_week[i] = hs_user->time_per_week[i-1];
4852 hs_user->picked_up[i] = hs_user->picked_up[i-1];
4853 hs_user->closed[i] = hs_user->closed[i-1];
4854 hs_user->reassigned_from[i] = hs_user->reassigned_from[i-1];
4855 hs_user->reassigned_to[i] = hs_user->reassigned_to[i-1];
4856 }
4857
4858 /* Reset it for this week */
4859 hs_user->time_per_week[0] = hs_user->picked_up[0] = hs_user->closed[0] = hs_user->reassigned_from[0] = hs_user->reassigned_to[0] = 0;
4860 }
4861 }
4862 }
4863
4864 static void helpserv_timed_run_stats(UNUSED_ARG(void *data)) {
4865 helpserv_run_stats(now);
4866 timeq_add(helpserv_next_stats(now), helpserv_timed_run_stats, data);
4867 }
4868
4869 static void
4870 helpserv_define_option(const char *name, helpserv_option_func_t *func) {
4871 dict_insert(helpserv_option_dict, name, func);
4872 }
4873
4874 static void helpserv_db_cleanup(void) {
4875 shutting_down=1;
4876 unreg_part_func(handle_part);
4877 unreg_del_user_func(handle_quit);
4878 close_helpfile(helpserv_helpfile);
4879 dict_delete(helpserv_func_dict);
4880 dict_delete(helpserv_option_dict);
4881 dict_delete(helpserv_usercmd_dict);
4882 dict_delete(helpserv_bots_dict);
4883 dict_delete(helpserv_bots_bychan_dict);
4884 dict_delete(helpserv_reqs_bynick_dict);
4885 dict_delete(helpserv_reqs_byhand_dict);
4886 dict_delete(helpserv_users_byhand_dict);
4887
4888 if (reqlog_f)
4889 fclose(reqlog_f);
4890 }
4891
4892 int helpserv_init() {
4893 HS_LOG = log_register_type("HelpServ", "file:helpserv.log");
4894 conf_register_reload(helpserv_conf_read);
4895
4896 helpserv_func_dict = dict_new();
4897 dict_set_free_data(helpserv_func_dict, free);
4898 helpserv_define_func("HELP", cmd_help, HlNone, CMD_NOT_OVERRIDE|CMD_IGNORE_EVENT);
4899 helpserv_define_func("LIST", cmd_list, HlTrial, CMD_NEED_BOT|CMD_IGNORE_EVENT);
4900 helpserv_define_func("NEXT", cmd_next, HlTrial, CMD_NEED_BOT|CMD_NEVER_FROM_OPSERV);
4901 helpserv_define_func("PICKUP", cmd_pickup, HlTrial, CMD_NEED_BOT|CMD_NEVER_FROM_OPSERV);
4902 helpserv_define_func("REASSIGN", cmd_reassign, HlManager, CMD_NEED_BOT|CMD_NEVER_FROM_OPSERV);
4903 helpserv_define_func("CLOSE", cmd_close, HlTrial, CMD_NEED_BOT|CMD_NEVER_FROM_OPSERV);
4904 helpserv_define_func("SHOW", cmd_show, HlTrial, CMD_NEED_BOT|CMD_IGNORE_EVENT);
4905 helpserv_define_func("ADDNOTE", cmd_addnote, HlTrial, CMD_NEED_BOT);
4906 helpserv_define_func("ADDOWNER", cmd_addowner, HlOper, CMD_NEED_BOT|CMD_FROM_OPSERV_ONLY);
4907 helpserv_define_func("DELOWNER", cmd_deluser, HlOper, CMD_NEED_BOT|CMD_FROM_OPSERV_ONLY);
4908 helpserv_define_func("ADDTRIAL", cmd_addtrial, HlManager, CMD_NEED_BOT);
4909 helpserv_define_func("ADDHELPER", cmd_addhelper, HlManager, CMD_NEED_BOT);
4910 helpserv_define_func("ADDMANAGER", cmd_addmanager, HlOwner, CMD_NEED_BOT);
4911 helpserv_define_func("GIVEOWNERSHIP", cmd_giveownership, HlOwner, CMD_NEED_BOT);
4912 helpserv_define_func("DELUSER", cmd_deluser, HlManager, CMD_NEED_BOT);
4913 helpserv_define_func("HELPERS", cmd_helpers, HlNone, CMD_NEED_BOT);
4914 helpserv_define_func("WLIST", cmd_wlist, HlNone, CMD_NEED_BOT);
4915 helpserv_define_func("MLIST", cmd_mlist, HlNone, CMD_NEED_BOT);
4916 helpserv_define_func("HLIST", cmd_hlist, HlNone, CMD_NEED_BOT);
4917 helpserv_define_func("TLIST", cmd_tlist, HlNone, CMD_NEED_BOT);
4918 helpserv_define_func("CLVL", cmd_clvl, HlManager, CMD_NEED_BOT);
4919 helpserv_define_func("PAGE", cmd_page, HlTrial, CMD_NEED_BOT);
4920 helpserv_define_func("SET", cmd_set, HlHelper, CMD_NEED_BOT);
4921 helpserv_define_func("STATS", cmd_stats, HlTrial, CMD_NEED_BOT);
4922 helpserv_define_func("STATSREPORT", cmd_statsreport, HlManager, CMD_NEED_BOT);
4923 helpserv_define_func("UNREGISTER", cmd_unregister, HlOwner, CMD_NEED_BOT);
4924 helpserv_define_func("READHELP", cmd_readhelp, HlOper, CMD_FROM_OPSERV_ONLY);
4925 helpserv_define_func("REGISTER", cmd_register, HlOper, CMD_FROM_OPSERV_ONLY);
4926 helpserv_define_func("MOVE", cmd_move, HlOper, CMD_FROM_OPSERV_ONLY|CMD_NEED_BOT);
4927 helpserv_define_func("BOTS", cmd_bots, HlOper, CMD_FROM_OPSERV_ONLY|CMD_IGNORE_EVENT);
4928 helpserv_define_func("EXPIRE", cmd_expire, HlOper, CMD_FROM_OPSERV_ONLY);
4929 helpserv_define_func("SUSPEND", cmd_suspend, HlOper, CMD_FROM_OPSERV_ONLY);
4930 helpserv_define_func("UNSUSPEND", cmd_unsuspend, HlOper, CMD_FROM_OPSERV_ONLY);
4931 helpserv_define_func("WEEKSTART", cmd_weekstart, HlTrial, CMD_NEED_BOT);
4932
4933 helpserv_option_dict = dict_new();
4934 helpserv_define_option("PAGETARGET", opt_pagetarget_command);
4935 helpserv_define_option("ALERTPAGETARGET", opt_pagetarget_alert);
4936 helpserv_define_option("STATUSPAGETARGET", opt_pagetarget_status);
4937 helpserv_define_option("PAGE", opt_pagetype);
4938 helpserv_define_option("PAGETYPE", opt_pagetype);
4939 helpserv_define_option("ALERTPAGETYPE", opt_alert_page_type);
4940 helpserv_define_option("STATUSPAGETYPE", opt_status_page_type);
4941 helpserv_define_option("GREETING", opt_greeting);
4942 helpserv_define_option("REQOPENED", opt_req_opened);
4943 helpserv_define_option("REQASSIGNED", opt_req_assigned);
4944 helpserv_define_option("REQCLOSED", opt_req_closed);
4945 helpserv_define_option("IDLEDELAY", opt_idle_delay);
4946 helpserv_define_option("WHINEDELAY", opt_whine_delay);
4947 helpserv_define_option("WHINEINTERVAL", opt_whine_interval);
4948 helpserv_define_option("EMPTYINTERVAL", opt_empty_interval);
4949 helpserv_define_option("STALEDELAY", opt_stale_delay);
4950 helpserv_define_option("REQPERSIST", opt_request_persistence);
4951 helpserv_define_option("HELPERPERSIST", opt_helper_persistence);
4952 helpserv_define_option("NOTIFICATION", opt_notification);
4953 helpserv_define_option("REQMAXLEN", opt_req_maxlen);
4954 helpserv_define_option("IDWRAP", opt_id_wrap);
4955 helpserv_define_option("PRIVMSGONLY", opt_privmsg_only);
4956 helpserv_define_option("REQONJOIN", opt_req_on_join);
4957 helpserv_define_option("AUTOVOICE", opt_auto_voice);
4958 helpserv_define_option("AUTODEVOICE", opt_auto_devoice);
4959 helpserv_define_option("JOINTOTAL", opt_join_total);
4960 helpserv_define_option("ALERTNEW", opt_alert_new);
4961
4962 helpserv_usercmd_dict = dict_new();
4963 dict_insert(helpserv_usercmd_dict, "CLOSEREQ", usercmd_close);
4964 dict_insert(helpserv_usercmd_dict, "HELP", usercmd_help);
4965 dict_insert(helpserv_usercmd_dict, "HELPER", usercmd_helper);
4966 dict_insert(helpserv_usercmd_dict, "SHOWREQ", usercmd_show);
4967 dict_insert(helpserv_usercmd_dict, "WAIT", usercmd_wait);
4968
4969 helpserv_bots_dict = dict_new();
4970 dict_set_free_data(helpserv_bots_dict, helpserv_free_bot);
4971
4972 helpserv_bots_bychan_dict = dict_new();
4973 dict_set_free_data(helpserv_bots_bychan_dict, helpserv_botlist_free);
4974
4975 helpserv_reqs_bynick_dict = dict_new();
4976 dict_set_free_data(helpserv_reqs_bynick_dict, helpserv_reqlist_free);
4977 helpserv_reqs_byhand_dict = dict_new();
4978 dict_set_free_data(helpserv_reqs_byhand_dict, helpserv_reqlist_free);
4979
4980 helpserv_users_byhand_dict = dict_new();
4981 dict_set_free_data(helpserv_users_byhand_dict, helpserv_userlist_free);
4982
4983 saxdb_register("HelpServ", helpserv_saxdb_read, helpserv_saxdb_write);
4984 helpserv_helpfile_read();
4985
4986 /* Make up for downtime... though this will only really affect the
4987 * time_per_week */
4988 if (last_stats_update && (helpserv_next_stats(last_stats_update) < now)) {
4989 time_t statsrun = last_stats_update;
4990 while ((statsrun = helpserv_next_stats(statsrun)) < now)
4991 helpserv_run_stats(statsrun);
4992 }
4993 timeq_add(helpserv_next_stats(now), helpserv_timed_run_stats, NULL);
4994
4995 reg_join_func(handle_join);
4996 reg_part_func(handle_part); /* also deals with kick */
4997 reg_nick_change_func(handle_nickchange);
4998 reg_del_user_func(handle_quit);
4999
5000 reg_auth_func(handle_nickserv_auth);
5001 reg_handle_rename_func(handle_nickserv_rename);
5002 reg_unreg_func(handle_nickserv_unreg);
5003 reg_allowauth_func(handle_nickserv_allowauth);
5004 reg_failpw_func(handle_nickserv_failpw);
5005 reg_handle_merge_func(handle_nickserv_merge);
5006
5007 reg_exit_func(helpserv_db_cleanup);
5008
5009 helpserv_module = module_register("helpserv", HS_LOG, HELPSERV_HELPFILE_NAME, helpserv_expand_variable);
5010 modcmd_register(helpserv_module, "helpserv", cmd_helpserv, 1, MODCMD_REQUIRE_AUTHED|MODCMD_NO_LOG|MODCMD_NO_DEFAULT_BIND, "level", "800", NULL);
5011 message_register_table(msgtab);
5012 return 1;
5013 }
5014
5015 int
5016 helpserv_finalize(void) {
5017 return 1;
5018 }