
   /* AMX Mod script.
   *
   * (c) Copyright 2003, Dra
   * This file is provided as is (no warranties).
   *
   */
   #include <amxmod>
   /*
   * cvar:
   *    amx_mailan_delay  <seconds>  -  announce new mail after joined, 0
   disable
   *    amx_mailan_mode <flags> - announce new mail with
   *       "a" - green hud message
   *       "b" - voice
   *       "c" - auto show mail menu
   *
   * cmds:
   *    amx_mail  ( displays mail menu )
   */
   #define  ADMIN_MAIL  (1<<0)    /*  flag  "a"  access  can use amx_mail
   command */
   #define  SERVER_MAIL_ADM (1<<11)    /*  flag "l" access can read mails
   which are mailto server */
   #define MAX_MAIL_DAY 14    /* mail delete after 14 days */
   #define MAX_MAIL_NUM 64    /* mail delete after 64 mails */
   #define MAX_USER_NUM 64    /* only allow 64 mail user */
   new new_mail_msg[] = "You got new mail"
   new  send_to_msg[]  = "Send to who ( nick name or tag or * to all mail
   user)"
   new send_sub_msg[] = "Mail Subject"
   new send_msg_msg[] = "Mail Message ( Keep null will finsh mail message
   )"
   new send_end_msg[] = "Mail Send Finshed"
   new error_msg[] = "Error : Cant register new mail user"
   new mailfile[] = "addons/amx/amx_mail.txt"
   new bakfile[] = "addons/amx/amx_mail.bak"
   new   announce_spk[2][]   =   {"spk   ^"buttons/lever3  lever5^"","spk
   ^"message^""}
   new bool:cstrike_running
   new logfilename[256], mtxt[1280]
   new Float:utime[33]
   new Float:mailtime
   new  umailflag[33][30],  nmailnum[33],  umailnum[33], umailid[33][30],
   umailuid[33]
   new user_maxposition[33], user_position[33], usendid[33], msgmode[33]
   new mailnum, startid, datanum, nextline, mailunum, nextmailuid=2, mkey
   new mailuname[MAX_USER_NUM][32]
   new mailline[MAX_MAIL_NUM]
   new mailrdtime[MAX_MAIL_NUM][128]
   new mailrdid[MAX_MAIL_NUM][MAX_USER_NUM]
   new mailfrom[MAX_MAIL_NUM]
   new mailto[MAX_MAIL_NUM][64]
   new maildate[MAX_MAIL_NUM][16]
   new mailsub[MAX_MAIL_NUM][32]
   new mailmsg[MAX_MAIL_NUM][256]
   public send_msgmode(id) {
       if (msgmode[id]==0)
           return PLUGIN_CONTINUE
       new tmp[288]
       read_args(tmp,255)
       remove_quotes(tmp)
       if (msgmode[id]==1) {
           msgmode[id]=2
           copy(mailto[usendid[id]],31,tmp)
           set_hudmessage(0, 250, 0, 0.05, 0.09, 0, 2.0, 8.0)
           show_hudmessage(id,send_sub_msg)
           client_cmd(id,"messagemode")
       } else if (msgmode[id]==2) {
           msgmode[id]=3
           copy(mailsub[usendid[id]],31,tmp)
           set_hudmessage(50, 50, 250, 0.05, 0.12, 0, 2.0, 8.0)
           show_hudmessage(id,send_msg_msg)
           mailmsg[usendid[id]] = ""
           client_cmd(id,"messagemode")
       } else if (msgmode[id]==3) {
           new len = strlen(tmp)
           if (len) {
               if (mailmsg[usendid[id]][0])
                   add(mailmsg[usendid[id]],255,"^n")
               add(mailmsg[usendid[id]],255,tmp)
           }
           if (!len || strlen(mailmsg[usendid[id]])>254) {
               msgmode[id]=0
               add_mailfile(mailfile,usendid[id],0)
               get_mailnum(id)
               show_main_menu(id)
               set_hudmessage(0, 250, 0, -1.0, 0.6, 0, 2.0, 8.0)
               format(tmp,287,"%s^n^n%s",send_end_msg,mailmsg[usendid[id]
   ])
           } else {
               set_hudmessage(50, 50, 250, 0.05, 0.12, 0, 2.0, 8.0)
               format(tmp,287,"%s^n^n%s",send_msg_msg,mailmsg[usendid[id]
   ])
               client_cmd(id,"messagemode")
           }
           show_hudmessage(id,tmp)
       }
       return PLUGIN_HANDLED
   }
   public add_mailfile(usefile[],mid,sort) {
       datanum++
       mailtime=get_gametime()
       new text[288]
       if (!sort) {
           if (file_exists(usefile)) {
               format(text,287,"MailNum %d",datanum)
               write_file(usefile,text,0)
           } else {
               write_file(usefile,"MailNum 1")
               get_time("LastSort %m/%d-%H:%M",text,287)
               write_file(usefile,text)
               get_user_name(0,mailuname[1],31)
               umailuid[0] = 1
               format(text,287,"MailUser  1 ^"%s^"", mailuname[1])
               write_file(usefile,text)
               mailunum = 1
               nextline=3
               datanum=1
           }
       }
       mailline[mid]=nextline+1
       format(text,287,"----------------- %d -----------------",datanum)
       write_file(usefile,text)
       format(text,287,"MailReadTime %s",mailrdtime[mid])
       write_file(usefile,text)
       new mlen = copy(mtxt,511,"MailReadID")
       for (new i=0;i<nextmailuid;++i) {
           if (mailrdid[mid][i])
               mlen+=format(mtxt[mlen],511-mlen," %d",mailrdid[mid][i])
           else
               break
       }
       write_file(usefile,mtxt)
       format(text,287,"MailFrom %d",mailfrom[mid])
       write_file(usefile,text)
       format(text,287,"MailTo ^"%s^"",mailto[mid])
       write_file(usefile,text)
       format(text,287,"MailDate %s",maildate[mid])
       write_file(usefile,text)
       format(text,287,"MailSubject ^"%s^"",mailsub[mid])
       write_file(usefile,text)
       format(text,287,"MailMsg %s",mailmsg[mid])
       replace_quotes(text,287,1)
       write_file(usefile,text)
       nextline+=8
   }
   public add_mailuser(id) {
       new i, uid, text[64]
       if (nextmailuid<MAX_USER_NUM) {
           uid = nextmailuid
           nextmailuid++
       } else if (mailunum+1<MAX_USER_NUM) {
           for (i=1;i<MAX_USER_NUM;++i)
               if (mailuname[i][0]==0) {
                   uid = i
                   break
               }
       }
       if (!uid)    return 0
       if (!file_exists(mailfile)) {
           write_file(mailfile,"MailNum 0")
           get_time("LastSort %m/%d-%H:%M",text,63)
           write_file(mailfile,text)
           get_user_name(0,mailuname[1],31)
           umailuid[0] = 1
           format(text,63,"MailUser  1 ^"%s^"", mailuname[1])
           write_file(mailfile,text)
           mailunum = 1
           nextline=3
           datanum=0
       }
       umailuid[id] = uid
       get_user_name(id,mailuname[uid],31)
       format(text,63,"MailUser %2d ^"%s^"", uid, mailuname[uid])
       write_file(mailfile,text)
       nextline++
       mailunum++
       return uid
   }
   public admin_mail(id,level,cid) {
       if (!(get_user_flags(id) & level)){
           console_print(id,"You have no access to that command")
           return PLUGIN_HANDLED
       }
       get_mailnum(id)
       show_main_menu(id)
       return PLUGIN_CONTINUE
   }
   public client_putinserver(id) {
       if (id)    umailuid[id]=nmailnum[id]=umailnum[id]=0
       if (get_user_flags(id) & ADMIN_MAIL) {
           get_mailuser(id)
           get_mailnum(id)
           new Float:delay=get_cvar_float("amx_mailan_delay")
           if (nmailnum[id] && delay>0) {
               new param[2]
               param[0] = id
               set_task(delay,"announce_new_mail",0,param,1)
           }
       }
       return PLUGIN_CONTINUE
   }
   public announce_new_mail(param[]) {
       new id = param[0]
       new smode[8]
       get_cvar_string("amx_mailan_mode",smode,7)
       new imode=read_flags(smode)
       if (imode&1) {
           set_hudmessage(0, 200, 0, -1.0, 0.6, 0, 2.0, 8.0)
           show_hudmessage(id,new_mail_msg)
       }
       if (imode&2) {
           client_cmd(id,announce_spk[(random(100)>50 ? 1 : 0)])
       }
       if (imode&4) {
           show_main_menu(id)
       }
   }
   public show_main_menu(id) {
       new menu_body[256], mlen
       mkey = get_cvar_num("amx_max_option")-1
       if (mkey<3 || mkey>9) mkey=9
       if (cstrike_running)
           mlen = copy(menu_body,255,"\yAMX Mail Main: ^n\w^n")
       else
           mlen = copy(menu_body,255,"AMX Mail Main: ^n^n")
       if (get_user_flags(id) & SERVER_MAIL_ADM)
           mlen  +=  format(menu_body[mlen],255-mlen,"server  info^nmails
   number : %d/%d^nmail users :
   %d/%d^n^n",mailnum,MAX_MAIL_NUM,mailunum,MAX_USER_NUM)
       if (cstrike_running)
           copy(menu_body[mlen],255-mlen,"total  :  %d^nnew : %d^n^n\%c1.
   Mail List\w^n\%c2. Mail Read\w^n\w3. Mail Send\w^n^n%d. Exit")
       else
           copy(menu_body[mlen],255-mlen,"total  :  %d^nnew  :  %d^n^n%c.
   Mail List^n%c. Mail Read^n3. Mail Send^n^n%d. Exit")
       new c_list_m, c_read_m, keys = 4 | (1<<mkey)
       c_list_m = c_read_m = (cstrike_running ? 'd' : '#' )
       if (umailnum[id]){
           keys |= 3
           c_list_m = cstrike_running ? 'w' : '1'
           c_read_m = cstrike_running ? 'w' : '2'
       }
       format(menu_body,255,menu_body,umailnum[id],nmailnum[id],c_list_m,
   c_read_m,(mkey == 9 ? 0 : mkey+1))
       show_menu(id,keys,menu_body)
   }
   public copyfile(destfile[],sourcefile[]) {
       new line, txtsize
       delete_file(destfile)
       while(read_file(sourcefile,line++,mtxt,1023,txtsize))
           write_file(destfile,mtxt)
       log_to_file(logfilename,"Mail : copy file to %s.",destfile)
   }
   public disp_mail(id, umid) {
       new text[256], msg[256], date[16], i, mlen, a
       new mid=umailid[id][umid]
       mlen = format(mtxt, 1279, "%-13s
   %s^n","MailFrom",mailuname[mailfrom[mid]])
       mlen       +=       format(mtxt[mlen],      1279-mlen,      "%-13s
   %s^n","MailTo",mailto[mid])
       mlen       +=       format(mtxt[mlen],      1279-mlen,      "%-13s
   %s^n","MailDate",maildate[mid])
       mlen       +=       format(mtxt[mlen],      1279-mlen,      "%-13s
   %s%s^n","MailSubject",(umailflag[id][umid]==2     ?     "[b]    "    :
   (umailflag[id][umid]==1 ? "[n]" : "") ),mailsub[mid])
       mlen += copy(mtxt[mlen], 1279-mlen,
   "---------------------------------------------^n")
       mlen += copy(mtxt[mlen],1279-mlen,mailmsg[mid])
       format(msg,31,"Mail Read %2d", umid+1)
       show_motd(id, mtxt, msg)
       if (umailflag[id][umid]==1) {
           a=0
           get_time("%m/%d-%H:%M",date,15)
           if (strlen(mailrdtime[mid])>114)
               a = 12
           format(mailrdtime[mid],127,"%s %s",mailrdtime[mid][a],date)
           format(text,255,"MailReadTime %s",mailrdtime[mid])
           write_file(mailfile,text,mailline[mid])
           if (!umailuid[id])
               add_mailuser(id)
           mlen = copy(mtxt,511,"MailReadID")
           for (i=0;i<nextmailuid;++i) {
               if (mailrdid[mid][i]) {
                   mlen+=format(mtxt[mlen],511-mlen,"
   %d",mailrdid[mid][i])
               } else {
                   mailrdid[mid][i] = umailuid[id]
                   mlen+=format(mtxt[mlen],511-mlen,"
   %d",mailrdid[mid][i])
                   break
               }
           }
           write_file(mailfile,mtxt,mailline[mid]+1)
           umailflag[id][umid]=0
           nmailnum[id]--
       }
   }
   public get_day(date1[],date2[]) {
       new day
       day = (date2[3]-date1[3])*10+date2[4]-date1[4]
       day += ((date2[0]-date1[0])*10+date2[1]-date1[1])*30
       if (day<0) day+=360
       return day
   }
   public get_mailnum(id) {
       if (utime[id]>mailtime)
           return umailnum[id]
       new name[32], a, mid, i, j
       get_user_name(id,name,31)
       new mailadm = (get_user_flags(id) & SERVER_MAIL_ADM) ? 1 : 0
       umailnum[id]=nmailnum[id]=0
       for (i=mailnum-1;i>=0;--i) {
           mid=(startid+i)%MAX_MAIL_NUM
           if (mailfrom[mid] == umailuid[id]) {
               umailid[id][umailnum[id]]=mid
               umailflag[id][umailnum[id]++]=2
               if (umailnum[id]>29) break
               continue
           }
           if  (containi(name,mailto[mid])!=-1  || equal(mailto[mid],"*")
   || (mailadm && containi(mailuname[1],mailto[mid])!=-1))
               umailid[id][umailnum[id]]=mid
           else
               continue
           a=0
           if (umailuid[id]) {
               for (j=0;j<nextmailuid;++j) {
                   if (mailrdid[mid][j] == umailuid[id]) {
                       umailflag[id][umailnum[id]++]=0
                       a=1
                       break
                   }
               }
           }
           if (!a) {
               umailflag[id][umailnum[id]++]=1
               nmailnum[id]++
           }
           if (umailnum[id]>29) break
       }
       utime[id]=get_gametime()
       return umailnum[id]
   }
   public get_mailuser(id){
       new name[32]
       utime[id]=0.0
       if (id) {
           umailuid[id]=0
           get_user_name(id,name,31)
           for (new i=2;i<nextmailuid;++i) {
               if (equali(name, mailuname[i])) {
                   umailuid[id]=i
                   break
               }
           }
       } else
           umailuid[id]=1
       return umailuid[id]
   }
   public show_list_menu(id) {
       new menu_body[256]
       new keys = 1 | (1<<mkey)
       if (cstrike_running)
           format(menu_body,255,"\yAMX Mail List: ^n\w^ntotal : %d^nnew :
   %d^n^n1.  1-10^n\%c2. 11-20^n\%c3. 21-30^n\w^n%d Back",
               umailnum[id],nmailnum[id],(umailnum[id]>10  ?  'w' : 'd'),
   (umailnum[id]>20 ? 'w' : 'd'), (mkey==9 ? 0 : mkey+1))
       else
           format(menu_body,255,"AMX  Mail  List:  ^n^ntotal  : %d^nnew :
   %d^n^n1. 1-10^n%c. 11-20^n%c. 21-30^n^n%d. Back",
               umailnum[id],nmailnum[id],(umailnum[id]>10  ?  '2' : '#'),
   (umailnum[id]>20 ? '3' : '#'), (mkey==9 ? 0 : mkey+1))
       if (umailnum[id]>10)
           keys |= 2
       if (umailnum[id]>20)
           keys |= 4
       show_menu(id,keys,menu_body)
   }
   public list_mail(id,start) {
       new mlen, mid
       new end = min(start+10,umailnum[id])
       mlen  =  format(mtxt,1279,  "-----------  total  :  %d    new : %d
   -----------^n",umailnum[id],nmailnum[id])
       mlen += format(mtxt[mlen],1279-mlen,
   "   #     Date         From                                Subject^n")
       for (new i=start; i<start+10;++i) {
           if (i<end) {
               mid=umailid[id][i]
               mlen            +=            format(mtxt[mlen],1279-mlen,
   "%c%2d  %s  %-32s  %s^n",
                   (umailflag[id][i]==1  ?  'n'  : (umailflag[id][i]==2 ?
   'b'    :    '    ')),i+1,   maildate[mid],   mailuname[mailfrom[mid]],
   mailsub[mid])
           } else {
               mlen += copy(mtxt[mlen],1279-mlen, "^n")
           }
       }
       format(mtxt[mlen],1279-mlen,
   "----------------------------------------^n----- Entries %i - %i of %i
   -----",
           start+1,end,umailnum[id])
       new header[32]
       format(header,31,"Mail List : %2d - %2d",start+1,end)
       show_motd(id,mtxt,header)
   }
   public show_read_menu(id) {
       new menu_body[256], mlen, a, mid
       new keys = (1<<mkey)
       if (cstrike_running)
           mlen      =     format(menu_body,255,"\yAMX     Mail     Read:
   \R%d/%d^n\w^n",user_position[id]+1, user_maxposition[id])
       else
           mlen = format(menu_body,255,"%20s%d/%d^n^n","AMX Mail Read: ",
   user_position[id]+1, user_maxposition[id])
       new start = user_position[id]*(mkey-1)
       new posnum = user_maxposition[id] > 1 ? start+mkey-1 : mkey
       new  end  = user_maxposition[id] > 1 ? min(posnum, umailnum[id]) :
   umailnum[id]
       for (new i=start;i<posnum;++i) {
           if (i<end) {
               mid = umailid[id][i]
               keys |= (1<<a++)
               mlen   +=  format(menu_body[mlen],  255-mlen,  "%d.  %c%2d
   %s^n",  a, (umailflag[id][i] == 1 ? 'n' : (umailflag[id][i] == 2 ? 'b'
   : ' ')), i+1, mailsub[mid])
           } else {
               mlen += copy(menu_body[mlen], 255-mlen, "^n")
           }
       }
       if (user_maxposition[id] > 1) {
           keys |= (1<<mkey-1)
           mlen += format(menu_body[mlen], 255-mlen, "%d. More^n",mkey)
       }
       format(menu_body[mlen], 255-mlen, "%d. %s", mkey==9 ? 0 : mkey+1 ,
   !user_position[id] ? "Exit" : "Back")
       show_menu(id,keys,menu_body)
   }
   public show_send_menu(id) {
       new menu_body[256], mlen, a
       new keys = (1<<mkey)
       if (cstrike_running)
           mlen      =     format(menu_body,255,"\yAMX     Mail     Send:
   \R%d/%d^n\w^n",user_position[id]+1, user_maxposition[id])
       else
           mlen = format(menu_body,255,"%20s%d/%d^n^n","AMX Mail Send: ",
   user_position[id]+1, user_maxposition[id])
       new start = user_position[id]*(mkey-1)
       new posnum = user_maxposition[id] > 1 ? start+mkey-1 : mkey
       new  end  =  user_maxposition[id]  > 1 ? min(posnum, mailunum+2) :
   mailunum+2
       for (new i=start;i<posnum;++i) {
           if (i<end) {
               keys |= (1<<a++)
               if (i==0)
                   mlen   +=   copy(menu_body[mlen],   255-mlen,  "1.  to
   custom^n")
               else if (i==1)
                   mlen  +=  copy(menu_body[mlen],  255-mlen,  "2. to all
   mail user^n")
               else if (i==2) {
                   if (!mailuname[1][0])
                       get_user_name(0,mailuname[1],31)
                   mlen  +=  format(menu_body[mlen], 255-mlen, "3. %s^n",
   mailuname[1])
               } else {
                   if (umailuid[id] == i-1) {
                       keys -= (1<<a-1)
                       if (cstrike_running)
                           mlen   +=   format(menu_body[mlen],  255-mlen,
   "\d%d. %s^n\w", a, mailuname[i-1])
                       else
                           mlen  += format(menu_body[mlen], 255-mlen, "#.
   %s^n", mailuname[i-1])
                   } else
                       mlen  +=  format(menu_body[mlen],  255-mlen,  "%d.
   %s^n", a, mailuname[i-1])
               }
           } else {
               mlen += copy(menu_body[mlen], 255-mlen, "^n")
           }
       }
       if (user_maxposition[id] > 1) {
           keys |= (1<<mkey-1)
           mlen += format(menu_body[mlen], 255-mlen, "%d. More^n",mkey)
       }
       format(menu_body[mlen], 255-mlen, "%d. %s", mkey==9 ? 0 : mkey+1 ,
   !user_position[id] ? "Exit" : "Back" )
       show_menu(id,keys,menu_body)
   }
   public read_mailfile() {
       mailtime=get_gametime()
       new date2[16], line, txtsize, title[16], stats, a, mid
       new usefile[64], data[256], fileflag, number[8], lastsort[16]
       if (file_exists(mailfile)) {
           fileflag=1
           copy(usefile,63,mailfile)
       } else if (file_exists(bakfile))
           copy(usefile,63,bakfile)
       else {
           return 0
       }
       while(read_file(usefile,line++,mtxt,511,txtsize)) {
           if (!contain(mtxt,"MailNum")) {
               parse(mtxt,title,0,number,7)
               datanum = strtonum(number)
           } else if (!contain(mtxt,"MailUser")) {
               parse(mtxt,title,0,number,7,data,31)
               a = strtonum(number)
               if (a<MAX_USER_NUM) {
                   if (!mailuname[a][0])
                       mailunum++
                   copy(mailuname[a],31,data)
                   if (a>=nextmailuid)
                       nextmailuid=a+1
               }
           } else if (!contain(mtxt,"LastSort")) {
               parse(mtxt,title,0,lastsort,15)
           } else if (!contain(mtxt,"MailReadTime")) {
               stats |= 1
               strbreak(mtxt,title,0,mailrdtime[mid],127)
               mailline[mid] = line-1
           } else if (!contain(mtxt,"MailReadID")) {
               stats |= 2
               a=0
               strbreak(mtxt,title,0,mtxt,511)
               while (strbreak(mtxt,number,7,mtxt,511))
                   mailrdid[mid][a++]=strtonum(number)
           } else if (!contain(mtxt,"MailFrom")) {
               stats |= 4
               parse(mtxt,title,0,number,7)
               mailfrom[mid]=strtonum(number)
           } else if (!contain(mtxt,"MailTo")) {
               stats |= 8
               parse(mtxt,title,0,mailto[mid],63)
           } else if (!contain(mtxt,"MailDate")) {
               stats |= 16
               parse(mtxt,title,0,maildate[mid],15)
           } else if (!contain(mtxt,"MailSubject")) {
               stats |= 32
               parse(mtxt,title,0,mailsub[mid],31)
           } else if (!contain(mtxt,"MailMsg")) {
               stats |= 64
               strbreak(mtxt,title,0,mailmsg[mid],255)
               replace_quotes(mailmsg[mid],255,0)
           }
           if (stats == 127) {
               stats = 0
               if (mailnum<MAX_MAIL_NUM)
                   mailnum++
               if (mailnum<MAX_MAIL_NUM)
                   mid = mailnum
               else {
                   mid = startid
                   startid++
               }
           }
       }
       nextline = line-1
       log_to_file(logfilename,"Mail  :  %d  mail read from %s", mailnum,
   usefile)
       if (!mailnum) return 0
       get_time("%m/%d-%H:%M",date2,15)
       if (get_day(lastsort,date2)>0) {
           if (fileflag) {
               sort_mailfile(bakfile)
               copyfile(mailfile,bakfile)
           } else
               sort_mailfile(mailfile)
           datanum=mailnum
       }
       return mailnum
   }
   public sort_mailfile(usefile[]) {
       new text[256], i, j, k, a, b=2
       delete_file(usefile)
       format(text,255,"MailNum %d",mailnum)
       write_file(usefile,text)
       get_time("LastSort %m/%d-%H:%M",text,255)
       write_file(usefile,text)
       format(text,255,"MailUser  1 ^"%s^"",mailuname[1])
       write_file(usefile,text)
       nextline=3
       for (i=2;i<nextmailuid;++i) {
           if (!mailuname[i][0]) continue
           a=0
           for (j=0;j<mailnum;++j) {
               if (mailfrom[j] == i) {
                   mailfrom[j] = b
                   a=1
                   continue
               }
               for (k=0;k<nextmailuid;++k) {
                   if (mailrdid[j][k] == i) {
                       a=1
                       mailrdid[j][k] = b
                       break
                   } else if (!mailrdid[j][k])
                       break
               }
           }
           if (a) {
               if (i!=b) {
                   copy(mailuname[b],31,mailuname[i])
                   mailuname[i][0]=0
               }
               format(text,255,"MailUser %2d ^"%s^"", b, mailuname[b])
               write_file(usefile,text)
               nextline++
               b++
           } else {
               mailunum--
               mailuname[i][0]=0
           }
       }
       nextmailuid=mailunum+1
       datanum=0
       for (i=0;i<mailnum;++i)
           add_mailfile(usefile,i,1)
       log_to_file(logfilename,"Mail : sort data file %s.",usefile)
   }
   public replace_quotes(str1[],len1,flag) {
       new source[512]
       new slen = min(strlen(str1),len1)
       new a, s
       copy(source,511,str1)
       for (new i=0;i<slen;++i) {
           if (i+a >= len1)
               break
           if (flag && source[i] == 10) {
               str1[i+a] = 94
               a++
               str1[i+a] = 'n'
           } else if (!flag && source[i] == 94) {
               str1[i+a] = source[i]
               s = 1
           } else if (!flag && s && source[i] == 'n') {
               a--
               s = 0
               str1[i+a] = 10
           } else {
               str1[i+a] = source[i]
               if (s) s=0
           }
       }
       str1[slen+a] = 0
       return slen+a
   }
   public strbreak(const source[],str1[],len1,str2[],len2) {
       new s, q, e, slen1, slen2, slen = strlen(source)
       for (new i=0;i<slen;++i) {
           if (source[i]==' ') {
               if (!s) continue
               if (s==1&&!q) {
                   e=1
                   continue
               }
           }
           if (source[i]==34) {
               if (!q&&!s) {
                   s=1
                   q=1
                   continue
               } else if (s==q) {
                   e=1
                   q=0
                   continue
               }
           }
           if (s==e) s++
           if (s==1) {
               if (slen1<len1)
                   str1[slen1++]=source[i]
           } else if (slen2<len2)
               str2[slen2++]=source[i]
       }
       str1[slen1]=0
       str2[slen2]=0
       return slen1?(slen2?3:1):(slen2?2:0)
   }
   public action_main(id,key) {
       user_position[id]=0
       switch (key) {
           case 0 : show_list_menu(id)
           case 1 : {
               user_maxposition[id]       =      umailnum[id]>mkey      ?
   (umailnum[id]/(mkey-1)+(umailnum[id]%(mkey-1) ? 1 : 0)) : 1
               show_read_menu(id)
           } case 2 : {
               user_maxposition[id]       =      (mailunum+2)>mkey      ?
   ((mailunum+2)/(mkey-1)+((mailunum+2)%(mkey-1) ? 1 : 0)) : 1
               show_send_menu(id)
           }
       }
   }
   public action_read(id,key) {
       if (key==mkey) {
           if (--user_position[id]<0)
               show_main_menu(id)
           else
               show_read_menu(id)
       } else if (key==mkey-1) {
           if (user_maxposition[id]>1) {
               if (++user_position[id] >= user_maxposition[id])
                   user_position[id]=0
           } else {
               disp_mail(id,key)
           }
           show_read_menu(id)
       } else {
           disp_mail(id,user_position[id]*(mkey-1)+key)
           show_read_menu(id)
       }
   }
   public action_list(id,key) {
       if (key==mkey)
           show_main_menu(id)
       else {
           list_mail(id,10*key)
           show_list_menu(id)
       }
   }
   public action_send(id,key) {
       if (key==mkey) {
           if (--user_position[id]<0)
               show_main_menu(id)
           else
               show_send_menu(id)
           return PLUGIN_CONTINUE
       }
       if (key==mkey-1) {
           if (user_maxposition[id]>1) {
               if (++user_position[id] >= user_maxposition[id])
                   user_position[id]=0
               show_send_menu(id)
               return PLUGIN_CONTINUE
           }
       }
       if (!umailuid[id]) {
           if (!add_mailuser(id)) {
               set_hudmessage(200, 100, 0, -1.0, 0.6, 0, 2.0, 8.0)
               show_hudmessage(id,error_msg)
               return PLUGIN_HANDLED
           }
       }
       if (mailnum==MAX_MAIL_NUM) {
           usendid[id]=startid++
           startid%=MAX_MAIL_NUM
           for (new i=0;i<MAX_USER_NUM;++i)
               mailrdid[usendid[id]][i] = 0
           mailrdtime[usendid[id]] = ""
       } else {
           usendid[id]=mailnum++
       }
       mailfrom[usendid[id]]=umailuid[id]
       get_time("%m/%d-%H:%M",maildate[usendid[id]],15)
       new op = user_position[id]*(mkey-1)+key
       if (op==0) {
           msgmode[id]=1
           set_hudmessage(200, 200, 0, 0.05, 0.06, 0, 2.0, 8.0)
           show_hudmessage(id,send_to_msg)
           client_cmd(id,"messagemode")
       } else {
           msgmode[id]=2
           set_hudmessage(0, 250, 0, 0.05, 0.09, 0, 2.0, 8.0)
           show_hudmessage(id,send_sub_msg)
           client_cmd(id,"messagemode")
           copy(mailto[usendid[id]],31, (op==1 ? "*" : mailuname[op-1]))
       }
       return PLUGIN_CONTINUE
   }
   public plugin_init() {
       register_plugin("Amx Mail","0.9.4b","dra")
       register_menucmd(register_menuid("AMX      Mail      Main:      ")
   ,1023,"action_main")
       register_menucmd(register_menuid("AMX      Mail      Read:      ")
   ,1023,"action_read")
       register_menucmd(register_menuid("AMX      Mail      List:      ")
   ,1023,"action_list")
       register_menucmd(register_menuid("AMX      Mail      Send:      ")
   ,1023,"action_send")
       register_clcmd("amx_mail","admin_mail",ADMIN_MAIL,"- displays mail
   menu")
       register_clcmd("say","send_msgmode")
       register_cvar("amx_mailan_delay","15.0")
       register_cvar("amx_mailan_mode","abc")
       get_time("addons/amx/logs/admin%m%d.log",logfilename,255)
       new mod_name[32]
       get_modname(mod_name,31)
       cstrike_running = equal(mod_name,"cstrike") ? true : false
       read_mailfile()
       umailuid[0]=1
       return PLUGIN_CONTINUE
   }
