Parsowanie e-maili w C#

Posted: 6 czerwca 2012 in .NET, C#, Parsing
Tagi: , ,

Poniżej przedstawiam prosty program w konsoli (można go rozbudować czy też dorobić do niego GUI) do parsowania e-maili
napisany w języku C#
using System;
using System.IO;
using System.Collections.Generic;

namespace BatchReader_01 {
        public class Message {
                public static int counter = 0;
                public static Dictionary<string, int> dict = new Dictionary<string, int>();
                public static List<Message> list = new List<Message>();

                int number;
                String mSubject;
                String mData;
                String mFrom;
                public Message(ref StreamReader sr) {
                        list.Add(this);
                        ++counter;
                        number = counter;

                        string strLine;
                        strLine = sr.ReadLine();
                        mData = strLine.Substring(6);
                        strLine = sr.ReadLine();
                        mFrom = strLine.Substring(6);
                        strLine = sr.ReadLine();
                        mSubject = "";
                        while (!strLine.StartsWith("To")) {
                                mSubject += strLine;
                                strLine = sr.ReadLine();
                        }
                        mSubject = mSubject.Substring(9);
                        mSubject = mSubject.Replace('t', ' ');

                        strLine = sr.ReadLine();
                        while (strLine != "------------------------------") {
                                strLine = sr.ReadLine();
                        }

                        if (!mSubject.StartsWith("Re: ")) {
                                dict.Add(mSubject.Replace(" ", ""), number);
                        } else {
                                strLine = mSubject.Substring(4).Replace(" ", "");
                                if (!dict.ContainsKey(strLine))
                                        dict.Add(strLine, number);
                        }
                }
                //
                public void dump() {
                        Console.WriteLine("Messge " + number.ToString()+":");
                        Console.WriteLine("t" + mSubject);
                        Console.WriteLine("t" + mData);
                        Console.WriteLine("t" + mFrom);
                }
                //
                public static void listDump() {
                        foreach (Message obj in list)
                                obj.dump();
                }
                //
                public static void subjectsDump() {
                        // Get a collection of the keys (names).
                        ICollection<String> c = dict.Keys;
                        foreach (String str in c)
                                Console.WriteLine("index: {0}, {1}  {2}", dict[str], list[dict[str] - 1].mSubject, str);
                }
        }

        class Program {
                //-------------------------------------------------------
                public static void Main(string[] args) {
                        try {
                                //List<Message> list = new List<Message>();
                                StreamReader sr = File.OpenText("GDAlgorithms-list Digest, Vol 28, Issue 1.MSG");
                                string strLine;
                                while (null != (strLine = sr.ReadLine())) {
                                        if (strLine.StartsWith("Message:")) {
                                                Message m = new Message(ref sr);
                                                //list.Add(m);
                                        }
                                }
                                sr.Close();

                                Message.subjectsDump();

                                Message.listDump();

                        } catch (FileNotFoundException e) {
                                Console.WriteLine(e.Message);
                        }
                }
                //-----------------------------------------------
        }
}
//---------------------------------------------------------------

Projekt w VS2008 Express Edition (download)

Jak użyjesz kodu w swoim programie to napisz. Tak z ciekawości chciałbym wiedzieć 🙂

Proszę o komentarze i uwagi 🙂


Sometime I have this problem: Access denied for user ‚root’@’localhost’ (using password: NO) I have described solution below:

  • Start the mysql client process using this command (sudo /etc/init.d/mysql start):
    mysql -u root
  • Execute this command to be able to change any password:
    FLUSH PRIVILEGES;

    Then reset/update your password:

    SET PASSWORD FOR root@'localhost' = PASSWORD('password');

    If we have a mysql root account that can connect from everywhere, you should also do:

    UPDATE mysql.user SET Password=PASSWORD('newpassword') WHERE User='root';

    Alternate Method:

    USE mysql UPDATE user SET Password = PASSWORD('newpassword') WHERE Host = 'localhost' AND User = 'root';

    And if you have a root account that can access from everywhere:

    USE mysql UPDATE user SET Password = PASSWORD('newpassword') WHERE Host = '%' AND User = 'root';
  • Or faster:

USE mysql;
FLUSH PRIVILEGES;
UPDATE user SET Password = PASSWORD(‚dupa’) WHERE Host = ‚%’ AND User = ‚root’;

 

Then stop/restart the mysqld process:

sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start
or
sudo /etc/init.d/mysql restart

Copying data between tables:

 

INSERT INTO table SELECT * FROM base2.table;


Copying data between tables including fields list:

 

INSTER INTO client (client_id, word) SELECT 4, word FROM client WHERE client_id = 3;


Jak szybko stworzyć ładne conky w Ubuntu

Posted: 6 października 2011 in conky, Linux, Ubuntu
Tagi: , ,

Instalujemy conky:

sudo apt-get install conky

Tworzymy plik .conkyrc i zapisujemy w /home/user

background yes
font Snap.se:size=8
xftfont Snap.se:size=8
use_xft yes
xftalpha 0.1
update_interval 1.0
total_run_times 0
own_window yes
own_window_type override
own_window_transparent yes
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
double_buffer yes
draw_shades yes
draw_outline no
draw_borders no
draw_graph_borders no
minimum_size 206 5
maximum_width 175
default_color 110000
default_shade_color 110000
default_outline_color 110000
alignment top_right
gap_x 15
gap_y 35
no_buffers yes
cpu_avg_samples 2
override_utf8_locale no
uppercase no # set to yes if you want all text to be in uppercase
use_spacer no

TEXT
${font LCD:style=Bold:pixelsize=40}${alignc}${time %H:%M}${font Snap.se:size=8}

${font Aerial:style=Bold:pixelsize=12}SYSTEM${font Snap.se:size=8} ${hr 1 }

System: $alignr$sysname
Kernel: $alignr$kernel
Uptime: $alignr$uptime
Processes: ${alignr}$processes ($running_processes running)
Load: ${alignr}$loadavg

${color slate grey}CPU 0:${alignr}:CPU 1
${color red}${cpu cpu1}%  ${freq_g cpu1}GHz$alignr${color red}${cpu cpu2}%  ${freq_g cpu2}GHz

${cpubar 4 cpu1}
${cpugraph ff0000 ffffff}

RAM ${alignr}$mem / $memmax ($memperc%)
${membar 4}

Highest CPU $alignr CPU% MEM%
${hr 1}
${top name 1}$alignr${top cpu 1}${top mem 1}
${top name 2}$alignr${top cpu 2}${top mem 2}
${top name 3}$alignr${top cpu 3}${top mem 3}

Highest MEM $alignr CPU% MEM%
${hr 1}
${top_mem name 1}$alignr${top_mem cpu 1}${top_mem mem 1}
${top_mem name 2}$alignr${top_mem cpu 2}${top_mem mem 2}
${top_mem name 3}$alignr${top_mem cpu 3}${top_mem mem 3}

${font Aerial:style=Bold:pixelsize=12}FILESYSTEM ${font Snap.se:size=8}${hr 1}

Root: ${alignr}${fs_free /} / ${fs_size /}
${fs_bar 4 /}

Home: ${alignr}${fs_free /home} / ${fs_size /home}
${fs_bar 4 /home}

${font Aerial:style=Bold:pixelsize=12}NETWORK ${font Snap.se:size=8}${hr 1}

Hostname: $alignr$nodename
IP: ${execi 5 ~/ip_zew}
Down ${downspeed eth0} k/s ${alignr}Up ${upspeed eth0} k/s
${downspeedgraph eth0 25,107 00ff00 ffffff} ${alignr}${upspeedgraph eth0 25,107 ff0000 ffffff}
Total ${totaldown eth0} ${alignr}Total ${totalup eth0}
$stippled_hr
$alignc${color orange}GMAIL$color
$stippled_hr
${execi 600 ~/gmail}

${color #ddaa00}Port(s)${alignr}#Connections
$color Inbound: ${tcp_portmon 1 32767 count} Outbound: ${tcp_portmon 32768 61000 count}${alignr}ALL: ${tcp_portmon 1 65535 count}
${color #ddaa00}Inbound Connection ${alignr} Local Service/Port$color
${tcp_portmon 1 32767 rhost 0} ${alignr} ${tcp_portmon 1 32767 lservice 0}
${tcp_portmon 1 32767 rhost 1} ${alignr} ${tcp_portmon 1 32767 lservice 1}
${tcp_portmon 1 32767 rhost 2} ${alignr} ${tcp_portmon 1 32767 lservice 2}
${color #ddaa00}Outbound Connection ${alignr} Remote Service/Port$color
${tcp_portmon 32768 61000 rhost 0} ${alignr} ${tcp_portmon 32768 61000 rservice 0}
${tcp_portmon 32768 61000 rhost 1} ${alignr} ${tcp_portmon 32768 61000 rservice 1}
${tcp_portmon 32768 61000 rhost 2} ${alignr} ${tcp_portmon 32768 61000 rservice 2}

Tworzymy plik gmail (w /home/user)

#!/bin/bash

gmail_login="user"
gmail_password="password"

dane="$(wget --secure-protocol=TLSv1 --timeout=3 -t 1 -q -O - https://${gmail_login}:${gmail_password}@mail.google.com/mail/feed/atom --no-check-certificate | grep 'fullcount' | sed -e 's/.*<fullcount>//;s/</fullcount>.*//' 2>/dev/null)"

if [ -z "$dane" ]; then
   echo "Błąd połaczenia"
else
   let wynik=$dane;
   if [ $wynik = 0 ]; then
      echo "Nie masz nowych wiadomosci."
   elif [ $wynik = 1 ]; then
      echo "Masz 1 nową wiadomość."
   else
      if [ $wynik -lt 5 ]; then
         echo "Masz $wynik nowe wiadomości."
      else
         echo "Masz $wynik nowych wiadomości."
      fi
   fi
fi

Tworzymy plik ip_zew w /home/user (uruchomi conky po 20 sekundach od startu systemu)

#!/bin/bash myip=”$(wget http://checkip.dyndns.org -q -O – | sed ‚s/.*ss: //;s/</b.*//’)” echo $myip

Tworzymy plik autostart w /home/user

#!/bin/bash sleep 20 && conky

 

Aby conky uruchamiało się przy starcie klikamy System -> Preferencje -> Programy startowe i klikamy Dodaj i wskazujemy ścieżkę do pliku autostart

W rezultacie wyglądać będzie ono tak:

conky

conky

wget – pobieranie plików

Posted: 21 lutego 2011 in Uncategorized

Prosty sposób na pobranie danych z linków umieszczonych w pliku tekstowym list_of_files.txt

wget -i list_of_files.txt

🙂

Pobranie IP w ASP.NET

Posted: 18 lutego 2011 in Uncategorized

<%Response.Write(Request.ServerVariables(„remote_addr”))%>

lub

<% Response.Output.Write(„{0}”, HttpContext.Current.Request.ServerVariables[„REMOTE_ADDR”]); %>

Domyślny OS w GRUB w Linux

Posted: 17 lutego 2011 in Uncategorized

Domyślny OS w GRUB: grep „menuentry” /boot/grub/grub.cfg | awk ‚{ print NR-1, $0 } ‚ /etc/default/grub -> GRUB_DEFAULT=x sudo update-grub