/[LeafOK_CVS]/lbbs/src/login.c
ViewVC logotype

Diff of /lbbs/src/login.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.7 by sysadm, Sat Mar 19 14:44:21 2005 UTC Revision 1.67 by sysadm, Wed Nov 5 02:06:50 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                            login.c  -  description  /*
3                               -------------------   * login
4      begin                : Mon Oct 20 2004   *   - user authentication and online status manager
5      copyright            : (C) 2004 by Leaflet   *
6      email                : leaflet@leafok.com   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7   ***************************************************************************/   */
   
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 2 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
 #include "io.h"  
11  #include "database.h"  #include "database.h"
12  #include <mysql.h>  #include "io.h"
13    #include "ip_mask.h"
14    #include "log.h"
15    #include "login.h"
16    #include "screen.h"
17    #include "user_priv.h"
18    #include <ctype.h>
19    #include <errno.h>
20    #include <stdlib.h>
21    #include <string.h>
22  #include <regex.h>  #include <regex.h>
23    #include <unistd.h>
24    #include <mysql/mysql.h>
25    #include <sys/param.h>
26    
27    static const int BBS_username_min_len = 3; // common len = 5, special len = 3
28    static const int BBS_password_min_len = 5; // legacy len = 5, current len = 6
29    
30    static const int BBS_allowed_login_failures_within_interval = 10;
31    static const int BBS_login_failures_count_interval = 10; // minutes
32    static const int BBS_allowed_login_failures_per_account = 3;
33    
34  void  const int BBS_login_retry_times = 3;
35  login_fail ()  
36    int bbs_login(void)
37  {  {
38    char temp[256];          char username[BBS_username_max_len + 1];
39            char password[BBS_password_max_len + 1];
40            int i = 0;
41            int ok = 0;
42    
43            for (; !SYS_server_exit && !ok && i < BBS_login_retry_times; i++)
44            {
45                    prints("\033[1;33m请输入帐号\033[m(试用请输入`\033[1;36mguest\033[m', "
46                               "注册请输入`\033[1;31mnew\033[m'): ");
47                    iflush();
48    
49                    if (str_input(username, sizeof(username), DOECHO) < 0)
50                    {
51                            continue;
52                    }
53    
54                    if (strcmp(username, "guest") == 0)
55                    {
56                            load_guest_info();
57    
58                            return 0;
59                    }
60    
61                    if (strcmp(username, "new") == 0)
62                    {
63                            display_file(DATA_REGISTER, 1);
64    
65                            return -1;
66                    }
67    
68                    if (username[0] != '\0')
69                    {
70                            prints("\033[1;37m请输入密码\033[m: ");
71                            iflush();
72    
73                            if (str_input(password, sizeof(password), NOECHO) < 0)
74                            {
75                                    continue;
76                            }
77    
78                            ok = (check_user(username, password) == 0);
79                            iflush();
80                    }
81            }
82    
83    strcpy (temp, app_home_dir);          if (!ok)
84    strcat (temp, "data/login_error.txt");          {
85    display_file (temp);                  display_file(DATA_LOGIN_ERROR, 1);
86                    return -1;
87            }
88    
89    sleep (1);          log_common("User \"%s\"(%ld) login from %s:%d\n",
90                               BBS_username, BBS_priv.uid, hostaddr_client, port_client);
91    
92            return 0;
93  }  }
94    
95  int  int check_user(const char *username, const char *password)
 bbs_login ()  
96  {  {
97    char username[20], password[20];          MYSQL *db = NULL;
98    int count, ok;          MYSQL_RES *rs = NULL;
99            MYSQL_ROW row;
100            char sql[SQL_BUFFER_LEN];
101            int ret = 0;
102            int BBS_uid = 0;
103            char client_addr[IP_ADDR_LEN];
104            int i;
105            int ok = 1;
106            char user_tz_env[BBS_user_tz_max_len + 2];
107    
108            db = db_open();
109            if (db == NULL)
110            {
111                    ret = -1;
112                    goto cleanup;
113            }
114    
115            // Verify format
116            for (i = 0; ok && username[i] != '\0'; i++)
117            {
118                    if (!(isalpha(username[i]) || (i > 0 && (isdigit(username[i]) || username[i] == '_'))))
119                    {
120                            ok = 0;
121                    }
122            }
123            if (ok && (i < BBS_username_min_len || i > BBS_username_max_len))
124            {
125                    ok = 0;
126            }
127            for (i = 0; ok && password[i] != '\0'; i++)
128            {
129                    if (!isalnum(password[i]))
130                    {
131                            ok = 0;
132                    }
133            }
134            if (ok && (i < BBS_password_min_len || i > BBS_password_max_len))
135            {
136                    ok = 0;
137            }
138    
139            if (!ok)
140            {
141                    prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
142                    ret = 1;
143                    goto cleanup;
144            }
145    
146            // Begin transaction
147            if (mysql_query(db, "SET autocommit=0") != 0)
148            {
149                    log_error("SET autocommit=0 error: %s\n", mysql_error(db));
150                    ret = -1;
151                    goto cleanup;
152            }
153    
154            if (mysql_query(db, "BEGIN") != 0)
155            {
156                    log_error("Begin transaction error: %s\n", mysql_error(db));
157                    ret = -1;
158                    goto cleanup;
159            }
160    
161    //Input username          // Failed login attempts from the same source (subnet /24) during certain time period
162    count = 0;          strncpy(client_addr, hostaddr_client, sizeof(client_addr) - 1);
163    ok = 0;          client_addr[sizeof(client_addr) - 1] = '\0';
164    while (!ok)  
165      {          snprintf(sql, sizeof(sql),
166        prints                           "SELECT COUNT(*) AS err_count FROM user_err_login_log "
167          ("\033[1;33mʺ\033[m( `\033[1;36mguest\033[m', "                           "WHERE login_dt >= SUBDATE(NOW(), INTERVAL %d MINUTE) "
168           "ע`\033[1;31mnew\033[m'): ");                           "AND login_ip LIKE '%s'",
169        iflush ();                           BBS_login_failures_count_interval,
170                             ip_mask(client_addr, 1, '%'));
171            if (mysql_query(db, sql) != 0)
172            {
173                    log_error("Query user_list error: %s\n", mysql_error(db));
174                    ret = -1;
175                    goto cleanup;
176            }
177            if ((rs = mysql_store_result(db)) == NULL)
178            {
179                    log_error("Get user_list data failed\n");
180                    ret = -1;
181                    goto cleanup;
182            }
183            if ((row = mysql_fetch_row(rs)))
184            {
185                    if (atoi(row[0]) >= BBS_allowed_login_failures_within_interval)
186                    {
187                            prints("\033[1;31m来源存在多次失败登陆尝试,请稍后再试,或使用Web方式访问\033[m\r\n");
188                            ret = 1;
189                            goto cleanup;
190                    }
191            }
192            mysql_free_result(rs);
193            rs = NULL;
194    
195        str_input (username, 19, 0);          // Failed login attempts against the current username since last successful login
196        count++;          snprintf(sql, sizeof(sql),
197                             "SELECT COUNT(*) AS err_count FROM user_err_login_log "
198                             "LEFT JOIN user_list ON user_err_login_log.username = user_list.username "
199                             "LEFT JOIN user_pubinfo ON user_list.UID = user_pubinfo.UID "
200                             "WHERE user_err_login_log.username = '%s' "
201                             "AND (user_err_login_log.login_dt >= user_pubinfo.last_login_dt "
202                             "OR user_pubinfo.last_login_dt IS NULL)",
203                             username);
204            if (mysql_query(db, sql) != 0)
205            {
206                    log_error("Query user_list error: %s\n", mysql_error(db));
207                    ret = -1;
208                    goto cleanup;
209            }
210            if ((rs = mysql_store_result(db)) == NULL)
211            {
212                    log_error("Get user_list data failed\n");
213                    ret = -1;
214                    goto cleanup;
215            }
216            if ((row = mysql_fetch_row(rs)))
217            {
218                    if (atoi(row[0]) >= BBS_allowed_login_failures_per_account)
219                    {
220                            prints("\033[1;31m账户存在多次失败登陆尝试,请使用Web方式登录解锁\033[m\r\n");
221                            ret = 1;
222                            goto cleanup;
223                    }
224            }
225            mysql_free_result(rs);
226            rs = NULL;
227    
228        if (strcmp (username, "guest") == 0)          snprintf(sql, sizeof(sql),
229                             "SELECT UID, username, p_login FROM user_list "
230                             "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
231                             username, password);
232            if (mysql_query(db, sql) != 0)
233            {
234                    log_error("Query user_list error: %s\n", mysql_error(db));
235                    ret = -1;
236                    goto cleanup;
237            }
238            if ((rs = mysql_store_result(db)) == NULL)
239            {
240                    log_error("Get user_list data failed\n");
241                    ret = -1;
242                    goto cleanup;
243            }
244            if ((row = mysql_fetch_row(rs)))
245          {          {
246            load_guest_info ();                  BBS_uid = atoi(row[0]);
247            return 0;                  strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);
248                    BBS_username[sizeof(BBS_username) - 1] = '\0';
249                    int p_login = atoi(row[2]);
250    
251                    mysql_free_result(rs);
252                    rs = NULL;
253    
254                    // Add user login log
255                    snprintf(sql, sizeof(sql),
256                                     "INSERT INTO user_login_log(UID, login_dt, login_ip) "
257                                     "VALUES(%d, NOW(), '%s')",
258                                     BBS_uid, hostaddr_client);
259                    if (mysql_query(db, sql) != 0)
260                    {
261                            log_error("Insert into user_login_log error: %s\n", mysql_error(db));
262                            ret = -1;
263                            goto cleanup;
264                    }
265    
266                    // Commit transaction
267                    if (mysql_query(db, "COMMIT") != 0)
268                    {
269                            log_error("Commit transaction error: %s\n", mysql_error(db));
270                            ret = -1;
271                            goto cleanup;
272                    }
273    
274                    if (p_login == 0)
275                    {
276                            prints("\033[1;31m您目前无权登陆...\033[m\r\n");
277                            ret = 1;
278                            goto cleanup;
279                    }
280            }
281            else
282            {
283                    mysql_free_result(rs);
284                    rs = NULL;
285    
286                    snprintf(sql, sizeof(sql),
287                                     "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
288                                     "VALUES('%s', '%s', NOW(), '%s')",
289                                     username, password, hostaddr_client);
290                    if (mysql_query(db, sql) != 0)
291                    {
292                            log_error("Insert into user_err_login_log error: %s\n", mysql_error(db));
293                            ret = -1;
294                            goto cleanup;
295                    }
296    
297                    // Commit transaction
298                    if (mysql_query(db, "COMMIT") != 0)
299                    {
300                            log_error("Commit transaction error: %s\n", mysql_error(db));
301                            ret = -1;
302                            goto cleanup;
303                    }
304    
305                    prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
306                    ret = 1;
307                    goto cleanup;
308          }          }
309    
310        if (strcmp (username, "new") == 0)          // Set AUTOCOMMIT = 1
311            if (mysql_query(db, "SET autocommit=1") != 0)
312          {          {
313            if (user_register () == 0)                  log_error("SET autocommit=1 error: %s\n", mysql_error(db));
314              return 0;                  ret = -1;
315            else                  goto cleanup;
            return -2;  
316          }          }
317    
318        if (strlen (username) > 0)          ret = load_user_info(db, BBS_uid);
319    
320            switch (ret)
321          {          {
322            //Input password          case 0: // Login successfully
323            prints ("\033[1;37m\033[m: ");                  break;
324            iflush ();          case -1: // Load data error
325                    prints("\033[1;31m读取用户数据错误...\033[m\r\n");
326                    ret = -1;
327                    goto cleanup;
328            case -2: // Unused
329                    prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");
330                    ret = 1;
331                    goto cleanup;
332            case -3: // Dead
333                    prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
334                    ret = 1;
335                    goto cleanup;
336            default:
337                    ret = -2;
338                    goto cleanup;
339            }
340    
341            str_input (password, 19, 1);          snprintf(sql, sizeof(sql),
342                             "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
343                             "last_login_dt = NOW() WHERE UID = %d",
344                             BBS_uid);
345            if (mysql_query(db, sql) != 0)
346            {
347                    log_error("Update user_pubinfo error: %s\n", mysql_error(db));
348                    ret = -1;
349                    goto cleanup;
350            }
351    
352            ok = (check_user (username, password) == 0);          if (user_online_add(db) != 0)
353            {
354                    ret = -1;
355                    goto cleanup;
356          }          }
357        if (count >= 3 && !ok)  
358            BBS_last_access_tm = BBS_login_tm = time(NULL);
359    
360            // Set user tz to process env
361            if (BBS_user_tz[0] != '\0')
362          {          {
363            login_fail ();                  user_tz_env[0] = ':';
364            return -1;                  strncpy(user_tz_env + 1, BBS_user_tz, sizeof(user_tz_env) - 2);
365                    user_tz_env[sizeof(user_tz_env) - 1] = '\0';
366    
367                    if (setenv("TZ", user_tz_env, 1) == -1)
368                    {
369                            log_error("setenv(TZ = %s) error %d\n", user_tz_env, errno);
370                            return -3;
371                    }
372    
373                    tzset();
374          }          }
     }  
375    
376    return 0;  cleanup:
377            mysql_free_result(rs);
378            mysql_close(db);
379    
380            return ret;
381  }  }
382    
383  int  int load_user_info(MYSQL *db, int BBS_uid)
 check_user (char *username, char *password)  
384  {  {
385    MYSQL *db;          MYSQL_RES *rs = NULL;
386    MYSQL_RES *rs;          MYSQL_ROW row;
387    MYSQL_ROW row;          char sql[SQL_BUFFER_LEN];
388    char sql[1024];          int life;
389    long int BBS_uid;          time_t last_login_dt;
390    int ret;          int ret = 0;
391    
392    //Verify format          snprintf(sql, sizeof(sql),
393    if (ireg ("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 ||                           "SELECT life, UNIX_TIMESTAMP(last_login_dt), user_timezone, exp, nickname "
394        ireg ("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)                           "FROM user_pubinfo WHERE UID = %d",
395      {                           BBS_uid);
396        prints ("\033[1;31mûʽ...\033[m\r\n");          if (mysql_query(db, sql) != 0)
397        iflush ();          {
398        return 1;                  log_error("Query user_pubinfo error: %s\n", mysql_error(db));
399      }                  ret = -1;
400                    goto cleanup;
401    db = (MYSQL *) db_open ();          }
402    if (db == NULL)          if ((rs = mysql_store_result(db)) == NULL)
403      {          {
404        return -1;                  log_error("Get user_pubinfo data failed\n");
405      }                  ret = -1;
406                    goto cleanup;
407    sprintf (sql,          }
408             "select UID,username,p_login from user_list where username='%s' "          if ((row = mysql_fetch_row(rs)))
409             "and (password=MD5('%s') or password=PASSWORD('%s')) and "          {
410             "enable", username, password, password);                  life = atoi(row[0]);
411    if (mysql_query (db, sql) != 0)                  last_login_dt = (time_t)atol(row[1]);
412      {  
413        log_error ("Query user_list failed\n");                  strncpy(BBS_user_tz, row[2], sizeof(BBS_user_tz) - 1);
414        return -1;                  BBS_user_tz[sizeof(BBS_user_tz) - 1] = '\0';
415      }  
416    if ((rs = mysql_store_result (db)) == NULL)                  BBS_user_exp = atoi(row[3]);
417      {  
418        log_error ("Get user_list data failed\n");                  strncpy(BBS_nickname, row[4], sizeof(BBS_nickname));
419        return -1;                  BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
420      }          }
421    if (row = mysql_fetch_row (rs))          else
422      {          {
423        BBS_uid = atol (row[0]);                  ret = -1; // Data not found
424        strcpy (BBS_username, row[1]);                  goto cleanup;
425        if (atoi (row[2]) == 0)          }
426          {          mysql_free_result(rs);
427            mysql_free_result (rs);          rs = NULL;
428            mysql_close (db);  
429            if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
430            prints ("\033[1;31mĿǰȨ½...\033[m\r\n");                  time(NULL) - last_login_dt > 60 * 60 * 24 * life)
431            iflush ();          {
432            return 1;                  ret = -3; // Dead
433          }                  goto cleanup;
434      }          }
435    else  
436      {          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
437        mysql_free_result (rs);          {
438                    ret = -1;
439        sprintf (sql,                  goto cleanup;
440                 "insert delayed into user_err_login_log"          }
                "(username,password,login_dt,login_ip) values"  
                "('%s','%s',now(),'%s')", username, password, hostaddr_client);  
       if (mysql_query (db, sql) != 0)  
         {  
           log_error ("Insert into user_err_login_log failed\n");  
           return -1;  
         }  
   
       mysql_close (db);  
   
       prints ("\033[1;31mû...\033[m\r\n");  
       iflush ();  
       return 1;  
     }  
   mysql_free_result (rs);  
   
   BBS_passwd_complex = verify_pass_complexity (password, username, 6);  
   
   ret = load_user_info (db, BBS_uid);  
   
   switch (ret)  
     {  
     case 0:                     //Login successfully  
       return 0;  
       break;  
     case -1:                    //Load data error  
       prints ("\033[1;31mȡûݴ...\033[m\r\n");  
       iflush ();  
       return -1;  
       break;  
     case -2:                    //Unused  
       return 0;  
       break;  
     case -3:                    //Dead  
       prints ("\033[1;31mźѾԶ뿪ǵ磡\033[m\r\n");  
       iflush ();  
       return 1;  
     default:  
       return -2;  
     }  
441    
442    mysql_close (db);  cleanup:
443            mysql_free_result(rs);
444    
445    return 0;          return ret;
446  }  }
447    
448  int  int load_guest_info(void)
 load_user_info (MYSQL * db, long int BBS_uid)  
449  {  {
450    MYSQL_RES *rs;          MYSQL *db = NULL;
451    MYSQL_ROW row;          int ret = 0;
452    char sql[1024];  
453    long int BBS_auth_uid = 0;          db = db_open();
454    int life;          if (db == NULL)
455    time_t last_login_dt;          {
456                    ret = -1;
457    sprintf (sql,                  goto cleanup;
458             "select life,UNIX_TIMESTAMP(last_login_dt) "          }
            "from user_pubinfo where UID=%ld limit 1", BBS_uid);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Query user_pubinfo failed\n");  
       return -1;  
     }  
   if ((rs = mysql_store_result (db)) == NULL)  
     {  
       log_error ("Get user_pubinfo data failed\n");  
       return -1;  
     }  
   if (row = mysql_fetch_row (rs))  
     {  
       life = atoi (row[0]);  
       last_login_dt = (time_t) atol (row[1]);  
     }  
   else  
     {  
       mysql_free_result (rs);  
       return (-1);              //Data not found  
     }  
   mysql_free_result (rs);  
   
   if (time (0) - last_login_dt >= 60 * 60 * 24 * life)  
     {  
       return (-3);              //Dead  
     }  
   
   sprintf (sql,  
            "select AUID from user_auth where UID=%ld"  
            " and enable and expire_dt>now()", BBS_uid);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Query user_auth failed\n");  
       return -1;  
     }  
   if ((rs = mysql_store_result (db)) == NULL)  
     {  
       log_error ("Get user_auth data failed\n");  
       return -1;  
     }  
   if (row = mysql_fetch_row (rs))  
     {  
       BBS_auth_uid = atol (row[0]);  
     }  
   else  
     {  
       BBS_auth_uid = 0;  
     }  
   mysql_free_result (rs);  
   
   sprintf (sql,  
            "insert delayed into user_login_log"  
            "(uid,login_dt,login_ip) values(%ld"  
            ",now(),'%s')", BBS_uid, hostaddr_client);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Insert into user_login_log failed\n");  
       return -1;  
     }  
   
   load_priv (db, &BBS_priv, BBS_uid, BBS_auth_uid,  
              (!BBS_passwd_complex ? S_MAN_M : S_NONE) |  
              (BBS_auth_uid ? S_NONE : S_MAIL));  
   
   BBS_last_access_tm = BBS_login_tm = time (0);  
   BBS_last_sub_tm = time (0) - 60;  
   
   sprintf (sql,  
            "update user_pubinfo set visit_count=visit_count+1,"  
            "last_login_dt=now() where uid=%ld", BBS_uid);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Update user_pubinfo failed\n");  
       return -1;  
     }  
459    
460    return 0;          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
461            BBS_username[sizeof(BBS_username) - 1] = '\0';
462    
463            BBS_user_exp = 0;
464    
465            strncpy(BBS_nickname, "Guest", sizeof(BBS_nickname));
466            BBS_nickname[sizeof(BBS_nickname) - 1] = '\0';
467    
468            if (load_priv(db, &BBS_priv, 0) != 0)
469            {
470                    ret = -1;
471                    goto cleanup;
472            }
473    
474            if (user_online_add(db) != 0)
475            {
476                    ret = -1;
477                    goto cleanup;
478            }
479    
480            BBS_last_access_tm = BBS_login_tm = time(NULL);
481    
482    cleanup:
483            mysql_close(db);
484    
485            return ret;
486  }  }
487    
488  int  int user_online_add(MYSQL *db)
 load_guest_info (MYSQL * db, long int BBS_uid)  
489  {  {
490    MYSQL_RES *rs;          char sql[SQL_BUFFER_LEN];
   MYSQL_ROW row;  
491    
492    db = (MYSQL *) db_open ();          snprintf(sql, sizeof(sql),
493    if (db == NULL)                           "INSERT INTO visit_log(dt, IP) VALUES(NOW(), '%s')",
494      {                           hostaddr_client);
495        return -1;          if (mysql_query(db, sql) != 0)
496      }          {
497                    log_error("Add visit log error: %s\n", mysql_error(db));
498                    return -1;
499            }
500    
501    strcpy (BBS_username, "guest");          if (user_online_del(db) != 0)
502            {
503                    return -2;
504            }
505    
506    load_priv (db, &BBS_priv, 0, 0, S_NONE);          snprintf(sql, sizeof(sql),
507                             "INSERT INTO user_online(SID, UID, ip, current_action, login_tm, last_tm) "
508                             "VALUES('Telnet_Process_%d', %d, '%s', 'LOGIN', NOW(), NOW())",
509                             getpid(), BBS_priv.uid, hostaddr_client);
510            if (mysql_query(db, sql) != 0)
511            {
512                    log_error("Add user_online error: %s\n", mysql_error(db));
513                    return -3;
514            }
515    
516            return 0;
517    }
518    
519    int user_online_del(MYSQL *db)
520    {
521            char sql[SQL_BUFFER_LEN];
522    
523            snprintf(sql, sizeof(sql),
524                             "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
525                             getpid());
526            if (mysql_query(db, sql) != 0)
527            {
528                    log_error("Delete user_online error: %s\n", mysql_error(db));
529                    return -1;
530            }
531    
532    BBS_last_access_tm = BBS_login_tm = time (0);          return 0;
533    BBS_last_sub_tm = time (0) - 60;  }
534    
535    int user_online_exp(MYSQL *db)
536    {
537            char sql[SQL_BUFFER_LEN];
538    
539            // +1 exp for every 5 minutes online since last logout
540            // but at most 24 hours worth of exp can be gained in Telnet session
541            snprintf(sql, sizeof(sql),
542                             "UPDATE user_pubinfo SET exp = exp + FLOOR(LEAST(TIMESTAMPDIFF("
543                             "SECOND, GREATEST(last_login_dt, IF(last_logout_dt IS NULL, last_login_dt, last_logout_dt)), NOW()"
544                             ") / 60 / 5, 12 * 24)), last_logout_dt = NOW() "
545                             "WHERE UID = %d",
546                             BBS_priv.uid);
547            if (mysql_query(db, sql) != 0)
548            {
549                    log_error("Update user_pubinfo error: %s\n", mysql_error(db));
550                    return -1;
551            }
552    
553            return 0;
554    }
555    
556    int user_online_update(const char *action)
557    {
558            MYSQL *db = NULL;
559            char sql[SQL_BUFFER_LEN];
560    
561            if ((action == NULL || strcmp(BBS_current_action, action) == 0) &&
562                    time(NULL) - BBS_current_action_tm < BBS_current_action_refresh_interval) // No change
563            {
564                    return 0;
565            }
566    
567            if (action != NULL)
568            {
569                    strncpy(BBS_current_action, action, sizeof(BBS_current_action) - 1);
570                    BBS_current_action[sizeof(BBS_current_action) - 1] = '\0';
571            }
572    
573            BBS_current_action_tm = time(NULL);
574    
575            db = db_open();
576            if (db == NULL)
577            {
578                    log_error("db_open() error: %s\n", mysql_error(db));
579                    return -1;
580            }
581    
582            snprintf(sql, sizeof(sql),
583                             "UPDATE user_online SET current_action = '%s', last_tm = NOW() "
584                             "WHERE SID = 'Telnet_Process_%d'",
585                             BBS_current_action, getpid());
586            if (mysql_query(db, sql) != 0)
587            {
588                    log_error("Update user_online error: %s\n", mysql_error(db));
589                    return -2;
590            }
591    
592    mysql_close (db);          mysql_close(db);
593    
594    return 0;          return 1;
595  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1